sandbox_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package libnetwork
  2. import (
  3. "testing"
  4. "github.com/docker/libnetwork/config"
  5. "github.com/docker/libnetwork/netlabel"
  6. "github.com/docker/libnetwork/options"
  7. "github.com/docker/libnetwork/osl"
  8. "github.com/docker/libnetwork/testutils"
  9. )
  10. func createEmptyCtrlr() *controller {
  11. return &controller{sandboxes: sandboxTable{}}
  12. }
  13. func getTestEnv(t *testing.T) (NetworkController, Network, Network) {
  14. netType := "bridge"
  15. option := options.Generic{
  16. "EnableIPForwarding": true,
  17. }
  18. genericOption := make(map[string]interface{})
  19. genericOption[netlabel.GenericData] = option
  20. cfgOptions, err := OptionBoltdbWithRandomDBFile()
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. c, err := New(append(cfgOptions, config.OptionDriverConfig(netType, genericOption))...)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. name1 := "test_nw_1"
  29. netOption1 := options.Generic{
  30. netlabel.GenericData: options.Generic{
  31. "BridgeName": name1,
  32. },
  33. }
  34. n1, err := c.NewNetwork(netType, name1, NetworkOptionGeneric(netOption1))
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. name2 := "test_nw_2"
  39. netOption2 := options.Generic{
  40. netlabel.GenericData: options.Generic{
  41. "BridgeName": name2,
  42. },
  43. }
  44. n2, err := c.NewNetwork(netType, name2, NetworkOptionGeneric(netOption2))
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. return c, n1, n2
  49. }
  50. func TestSandboxAddEmpty(t *testing.T) {
  51. ctrlr := createEmptyCtrlr()
  52. sbx, err := ctrlr.NewSandbox("sandbox0")
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. if err := sbx.Delete(); err != nil {
  57. t.Fatal(err)
  58. }
  59. if len(ctrlr.sandboxes) != 0 {
  60. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  61. }
  62. osl.GC()
  63. }
  64. func TestSandboxAddMultiPrio(t *testing.T) {
  65. if !testutils.IsRunningInContainer() {
  66. defer testutils.SetupTestOSContext(t)()
  67. }
  68. c, nw, _ := getTestEnv(t)
  69. ctrlr := c.(*controller)
  70. sbx, err := ctrlr.NewSandbox("sandbox1")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. sid := sbx.ID()
  75. ep1, err := nw.CreateEndpoint("ep1")
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. ep2, err := nw.CreateEndpoint("ep2")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. ep3, err := nw.CreateEndpoint("ep3")
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. if err := ep1.Join(sbx, JoinOptionPriority(ep1, 1)); err != nil {
  88. t.Fatal(err)
  89. }
  90. if err := ep2.Join(sbx, JoinOptionPriority(ep2, 2)); err != nil {
  91. t.Fatal(err)
  92. }
  93. if err := ep3.Join(sbx, JoinOptionPriority(ep3, 3)); err != nil {
  94. t.Fatal(err)
  95. }
  96. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
  97. t.Fatal("Expected ep3 to be at the top of the heap. But did not find ep3 at the top of the heap")
  98. }
  99. if len(sbx.Endpoints()) != 3 {
  100. t.Fatal("Expected 3 endpoints to be connected to the sandbox.")
  101. }
  102. if err := ep3.Leave(sbx); err != nil {
  103. t.Fatal(err)
  104. }
  105. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep2.ID() {
  106. t.Fatal("Expected ep2 to be at the top of the heap after removing ep3. But did not find ep2 at the top of the heap")
  107. }
  108. if err := ep2.Leave(sbx); err != nil {
  109. t.Fatal(err)
  110. }
  111. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep1.ID() {
  112. t.Fatal("Expected ep1 to be at the top of the heap after removing ep2. But did not find ep1 at the top of the heap")
  113. }
  114. // Re-add ep3 back
  115. if err := ep3.Join(sbx, JoinOptionPriority(ep3, 3)); err != nil {
  116. t.Fatal(err)
  117. }
  118. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
  119. t.Fatal("Expected ep3 to be at the top of the heap after adding ep3 back. But did not find ep3 at the top of the heap")
  120. }
  121. if err := sbx.Delete(); err != nil {
  122. t.Fatal(err)
  123. }
  124. if len(ctrlr.sandboxes) != 0 {
  125. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  126. }
  127. osl.GC()
  128. }
  129. func TestSandboxAddSamePrio(t *testing.T) {
  130. if !testutils.IsRunningInContainer() {
  131. defer testutils.SetupTestOSContext(t)()
  132. }
  133. c, nw1, nw2 := getTestEnv(t)
  134. ctrlr := c.(*controller)
  135. sbx, err := ctrlr.NewSandbox("sandbox1")
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. sid := sbx.ID()
  140. ep1, err := nw1.CreateEndpoint("ep1")
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. ep2, err := nw2.CreateEndpoint("ep2")
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. if err := ep1.Join(sbx); err != nil {
  149. t.Fatal(err)
  150. }
  151. if err := ep2.Join(sbx); err != nil {
  152. t.Fatal(err)
  153. }
  154. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep1.ID() {
  155. t.Fatal("Expected ep1 to be at the top of the heap. But did not find ep1 at the top of the heap")
  156. }
  157. if err := ep1.Leave(sbx); err != nil {
  158. t.Fatal(err)
  159. }
  160. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep2.ID() {
  161. t.Fatal("Expected ep2 to be at the top of the heap after removing ep3. But did not find ep2 at the top of the heap")
  162. }
  163. if err := ep2.Leave(sbx); err != nil {
  164. t.Fatal(err)
  165. }
  166. if err := sbx.Delete(); err != nil {
  167. t.Fatal(err)
  168. }
  169. if len(ctrlr.sandboxes) != 0 {
  170. t.Fatalf("controller containers is not empty. len = %d", len(ctrlr.sandboxes))
  171. }
  172. osl.GC()
  173. }