sandbox_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 getTestEnv(t *testing.T, empty bool) (NetworkController, Network, Network) {
  11. netType := "bridge"
  12. option := options.Generic{
  13. "EnableIPForwarding": true,
  14. }
  15. genericOption := make(map[string]interface{})
  16. genericOption[netlabel.GenericData] = option
  17. cfgOptions, err := OptionBoltdbWithRandomDBFile()
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. c, err := New(append(cfgOptions, config.OptionDriverConfig(netType, genericOption))...)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. if empty {
  26. return c, nil, nil
  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. c, _, _ := getTestEnv(t, true)
  52. ctrlr := c.(*controller)
  53. sbx, err := ctrlr.NewSandbox("sandbox0")
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. if err := sbx.Delete(); err != nil {
  58. t.Fatal(err)
  59. }
  60. if len(ctrlr.sandboxes) != 0 {
  61. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  62. }
  63. osl.GC()
  64. }
  65. func TestSandboxAddMultiPrio(t *testing.T) {
  66. if !testutils.IsRunningInContainer() {
  67. defer testutils.SetupTestOSContext(t)()
  68. }
  69. c, nw, _ := getTestEnv(t, false)
  70. ctrlr := c.(*controller)
  71. sbx, err := ctrlr.NewSandbox("sandbox1")
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. sid := sbx.ID()
  76. ep1, err := nw.CreateEndpoint("ep1")
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. ep2, err := nw.CreateEndpoint("ep2")
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. ep3, err := nw.CreateEndpoint("ep3")
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. if err := ep1.Join(sbx, JoinOptionPriority(ep1, 1)); err != nil {
  89. t.Fatal(err)
  90. }
  91. if err := ep2.Join(sbx, JoinOptionPriority(ep2, 2)); err != nil {
  92. t.Fatal(err)
  93. }
  94. if err := ep3.Join(sbx, JoinOptionPriority(ep3, 3)); err != nil {
  95. t.Fatal(err)
  96. }
  97. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
  98. t.Fatal("Expected ep3 to be at the top of the heap. But did not find ep3 at the top of the heap")
  99. }
  100. if len(sbx.Endpoints()) != 3 {
  101. t.Fatal("Expected 3 endpoints to be connected to the sandbox.")
  102. }
  103. if err := ep3.Leave(sbx); err != nil {
  104. t.Fatal(err)
  105. }
  106. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep2.ID() {
  107. 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")
  108. }
  109. if err := ep2.Leave(sbx); err != nil {
  110. t.Fatal(err)
  111. }
  112. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep1.ID() {
  113. 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")
  114. }
  115. // Re-add ep3 back
  116. if err := ep3.Join(sbx, JoinOptionPriority(ep3, 3)); err != nil {
  117. t.Fatal(err)
  118. }
  119. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
  120. 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")
  121. }
  122. if err := sbx.Delete(); err != nil {
  123. t.Fatal(err)
  124. }
  125. if len(ctrlr.sandboxes) != 0 {
  126. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  127. }
  128. osl.GC()
  129. }
  130. func TestSandboxAddSamePrio(t *testing.T) {
  131. if !testutils.IsRunningInContainer() {
  132. defer testutils.SetupTestOSContext(t)()
  133. }
  134. c, nw1, nw2 := getTestEnv(t, false)
  135. ctrlr := c.(*controller)
  136. sbx, err := ctrlr.NewSandbox("sandbox1")
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. sid := sbx.ID()
  141. ep1, err := nw1.CreateEndpoint("ep1")
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. ep2, err := nw2.CreateEndpoint("ep2")
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. if err := ep1.Join(sbx); err != nil {
  150. t.Fatal(err)
  151. }
  152. if err := ep2.Join(sbx); err != nil {
  153. t.Fatal(err)
  154. }
  155. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep1.ID() {
  156. t.Fatal("Expected ep1 to be at the top of the heap. But did not find ep1 at the top of the heap")
  157. }
  158. if err := ep1.Leave(sbx); err != nil {
  159. t.Fatal(err)
  160. }
  161. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep2.ID() {
  162. 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")
  163. }
  164. if err := ep2.Leave(sbx); err != nil {
  165. t.Fatal(err)
  166. }
  167. if err := sbx.Delete(); err != nil {
  168. t.Fatal(err)
  169. }
  170. if len(ctrlr.sandboxes) != 0 {
  171. t.Fatalf("controller containers is not empty. len = %d", len(ctrlr.sandboxes))
  172. }
  173. osl.GC()
  174. }