sandbox_test.go 3.8 KB

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