sandbox_test.go 4.6 KB

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