sandbox_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package osl
  2. import (
  3. "os"
  4. "runtime"
  5. "testing"
  6. "github.com/docker/docker/pkg/reexec"
  7. "github.com/docker/libnetwork/ns"
  8. "github.com/docker/libnetwork/testutils"
  9. )
  10. func TestMain(m *testing.M) {
  11. if reexec.Init() {
  12. return
  13. }
  14. os.Exit(m.Run())
  15. }
  16. func TestSandboxCreate(t *testing.T) {
  17. defer testutils.SetupTestOSContext(t)()
  18. key, err := newKey(t)
  19. if err != nil {
  20. t.Fatalf("Failed to obtain a key: %v", err)
  21. }
  22. s, err := NewSandbox(key, true, false)
  23. if err != nil {
  24. t.Fatalf("Failed to create a new sandbox: %v", err)
  25. }
  26. runtime.LockOSThread()
  27. if s.Key() != key {
  28. t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
  29. }
  30. tbox, err := newInfo(ns.NlHandle(), t)
  31. if err != nil {
  32. t.Fatalf("Failed to generate new sandbox info: %v", err)
  33. }
  34. for _, i := range tbox.Info().Interfaces() {
  35. err = s.AddInterface(i.SrcName(), i.DstName(),
  36. tbox.InterfaceOptions().Bridge(i.Bridge()),
  37. tbox.InterfaceOptions().Address(i.Address()),
  38. tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6()))
  39. if err != nil {
  40. t.Fatalf("Failed to add interfaces to sandbox: %v", err)
  41. }
  42. }
  43. err = s.SetGateway(tbox.Info().Gateway())
  44. if err != nil {
  45. t.Fatalf("Failed to set gateway to sandbox: %v", err)
  46. }
  47. err = s.SetGatewayIPv6(tbox.Info().GatewayIPv6())
  48. if err != nil {
  49. t.Fatalf("Failed to set ipv6 gateway to sandbox: %v", err)
  50. }
  51. verifySandbox(t, s, []string{"0", "1", "2"})
  52. err = s.Destroy()
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. verifyCleanup(t, s, true)
  57. }
  58. func TestSandboxCreateTwice(t *testing.T) {
  59. defer testutils.SetupTestOSContext(t)()
  60. key, err := newKey(t)
  61. if err != nil {
  62. t.Fatalf("Failed to obtain a key: %v", err)
  63. }
  64. _, err = NewSandbox(key, true, false)
  65. if err != nil {
  66. t.Fatalf("Failed to create a new sandbox: %v", err)
  67. }
  68. runtime.LockOSThread()
  69. // Create another sandbox with the same key to see if we handle it
  70. // gracefully.
  71. s, err := NewSandbox(key, true, false)
  72. if err != nil {
  73. t.Fatalf("Failed to create a new sandbox: %v", err)
  74. }
  75. runtime.LockOSThread()
  76. err = s.Destroy()
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. GC()
  81. verifyCleanup(t, s, false)
  82. }
  83. func TestSandboxGC(t *testing.T) {
  84. key, err := newKey(t)
  85. if err != nil {
  86. t.Fatalf("Failed to obtain a key: %v", err)
  87. }
  88. s, err := NewSandbox(key, true, false)
  89. if err != nil {
  90. t.Fatalf("Failed to create a new sandbox: %v", err)
  91. }
  92. err = s.Destroy()
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. GC()
  97. verifyCleanup(t, s, false)
  98. }
  99. func TestAddRemoveInterface(t *testing.T) {
  100. defer testutils.SetupTestOSContext(t)()
  101. key, err := newKey(t)
  102. if err != nil {
  103. t.Fatalf("Failed to obtain a key: %v", err)
  104. }
  105. s, err := NewSandbox(key, true, false)
  106. if err != nil {
  107. t.Fatalf("Failed to create a new sandbox: %v", err)
  108. }
  109. runtime.LockOSThread()
  110. if s.Key() != key {
  111. t.Fatalf("s.Key() returned %s. Expected %s", s.Key(), key)
  112. }
  113. tbox, err := newInfo(ns.NlHandle(), t)
  114. if err != nil {
  115. t.Fatalf("Failed to generate new sandbox info: %v", err)
  116. }
  117. for _, i := range tbox.Info().Interfaces() {
  118. err = s.AddInterface(i.SrcName(), i.DstName(),
  119. tbox.InterfaceOptions().Bridge(i.Bridge()),
  120. tbox.InterfaceOptions().Address(i.Address()),
  121. tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6()))
  122. if err != nil {
  123. t.Fatalf("Failed to add interfaces to sandbox: %v", err)
  124. }
  125. }
  126. verifySandbox(t, s, []string{"0", "1", "2"})
  127. interfaces := s.Info().Interfaces()
  128. if err := interfaces[0].Remove(); err != nil {
  129. t.Fatalf("Failed to remove interfaces from sandbox: %v", err)
  130. }
  131. verifySandbox(t, s, []string{"1", "2"})
  132. i := tbox.Info().Interfaces()[0]
  133. if err := s.AddInterface(i.SrcName(), i.DstName(),
  134. tbox.InterfaceOptions().Bridge(i.Bridge()),
  135. tbox.InterfaceOptions().Address(i.Address()),
  136. tbox.InterfaceOptions().AddressIPv6(i.AddressIPv6())); err != nil {
  137. t.Fatalf("Failed to add interfaces to sandbox: %v", err)
  138. }
  139. verifySandbox(t, s, []string{"1", "2", "3"})
  140. err = s.Destroy()
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. GC()
  145. verifyCleanup(t, s, false)
  146. }