sandbox_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package libnetwork
  2. import (
  3. "runtime"
  4. "strconv"
  5. "testing"
  6. "github.com/docker/docker/libnetwork/config"
  7. "github.com/docker/docker/libnetwork/ipamapi"
  8. "github.com/docker/docker/libnetwork/netlabel"
  9. "github.com/docker/docker/libnetwork/options"
  10. "github.com/docker/docker/libnetwork/osl"
  11. "github.com/docker/docker/libnetwork/testutils"
  12. "gotest.tools/v3/skip"
  13. )
  14. func getTestEnv(t *testing.T, opts ...[]NetworkOption) (*Controller, []*Network) {
  15. skip.If(t, runtime.GOOS == "windows", "test only works on linux")
  16. const netType = "bridge"
  17. c, err := New(
  18. OptionBoltdbWithRandomDBFile(t),
  19. config.OptionDriverConfig(netType, map[string]any{
  20. netlabel.GenericData: options.Generic{"EnableIPForwarding": true},
  21. }),
  22. )
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. t.Cleanup(c.Stop)
  27. if len(opts) == 0 {
  28. return c, nil
  29. }
  30. nwList := make([]*Network, 0, len(opts))
  31. for i, opt := range opts {
  32. name := "test_nw_" + strconv.Itoa(i)
  33. newOptions := []NetworkOption{
  34. NetworkOptionGeneric(options.Generic{
  35. netlabel.GenericData: options.Generic{"BridgeName": name},
  36. }),
  37. }
  38. newOptions = append(newOptions, opt...)
  39. n, err := c.NewNetwork(netType, name, "", newOptions...)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. nwList = append(nwList, n)
  44. }
  45. return c, nwList
  46. }
  47. func TestSandboxAddEmpty(t *testing.T) {
  48. ctrlr, _ := getTestEnv(t)
  49. sbx, err := ctrlr.NewSandbox("sandbox0")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. if err := sbx.Delete(); err != nil {
  54. t.Fatal(err)
  55. }
  56. if len(ctrlr.sandboxes) != 0 {
  57. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  58. }
  59. osl.GC()
  60. }
  61. // // If different priorities are specified, internal option and ipv6 addresses mustn't influence endpoint order
  62. func TestSandboxAddMultiPrio(t *testing.T) {
  63. defer testutils.SetupTestOSContext(t)()
  64. opts := [][]NetworkOption{
  65. {NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)},
  66. {NetworkOptionInternalNetwork()},
  67. {},
  68. }
  69. ctrlr, nws := getTestEnv(t, opts...)
  70. sbx, err := ctrlr.NewSandbox("sandbox1")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. sid := sbx.ID()
  75. ep1, err := nws[0].CreateEndpoint("ep1")
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. ep2, err := nws[1].CreateEndpoint("ep2")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. ep3, err := nws[2].CreateEndpoint("ep3")
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. if err := ep1.Join(sbx, JoinOptionPriority(1)); err != nil {
  88. t.Fatal(err)
  89. }
  90. if err := ep2.Join(sbx, JoinOptionPriority(2)); err != nil {
  91. t.Fatal(err)
  92. }
  93. if err := ep3.Join(sbx, JoinOptionPriority(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(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. defer testutils.SetupTestOSContext(t)()
  131. opts := [][]NetworkOption{
  132. {},
  133. {},
  134. {NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)},
  135. {NetworkOptionInternalNetwork()},
  136. }
  137. ctrlr, nws := getTestEnv(t, opts...)
  138. sbx, err := ctrlr.NewSandbox("sandbox1")
  139. if err != nil {
  140. t.Fatal(err)
  141. }
  142. sid := sbx.ID()
  143. epNw1, err := nws[1].CreateEndpoint("ep1")
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. epIPv6, err := nws[2].CreateEndpoint("ep2")
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. epInternal, err := nws[3].CreateEndpoint("ep3")
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. epNw0, err := nws[0].CreateEndpoint("ep4")
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. if err := epNw1.Join(sbx); err != nil {
  160. t.Fatal(err)
  161. }
  162. if err := epIPv6.Join(sbx); err != nil {
  163. t.Fatal(err)
  164. }
  165. if err := epInternal.Join(sbx); err != nil {
  166. t.Fatal(err)
  167. }
  168. if err := epNw0.Join(sbx); err != nil {
  169. t.Fatal(err)
  170. }
  171. // order should now be: epIPv6, epNw0, epNw1, epInternal
  172. if len(sbx.Endpoints()) != 4 {
  173. t.Fatal("Expected 4 endpoints to be connected to the sandbox.")
  174. }
  175. // IPv6 has precedence over IPv4
  176. if ctrlr.sandboxes[sid].endpoints[0].ID() != epIPv6.ID() {
  177. t.Fatal("Expected epIPv6 to be at the top of the heap. But did not find epIPv6 at the top of the heap")
  178. }
  179. // internal network has lowest precedence
  180. if ctrlr.sandboxes[sid].endpoints[3].ID() != epInternal.ID() {
  181. t.Fatal("Expected epInternal to be at the bottom of the heap. But did not find epInternal at the bottom of the heap")
  182. }
  183. if err := epIPv6.Leave(sbx); err != nil {
  184. t.Fatal(err)
  185. }
  186. // 'test_nw_0' has precedence over 'test_nw_1'
  187. if ctrlr.sandboxes[sid].endpoints[0].ID() != epNw0.ID() {
  188. t.Fatal("Expected epNw0 to be at the top of the heap after removing epIPv6. But did not find epNw0 at the top of the heap")
  189. }
  190. if err := epNw1.Leave(sbx); err != nil {
  191. t.Fatal(err)
  192. }
  193. if err := sbx.Delete(); err != nil {
  194. t.Fatal(err)
  195. }
  196. if len(ctrlr.sandboxes) != 0 {
  197. t.Fatalf("controller containers is not empty. len = %d", len(ctrlr.sandboxes))
  198. }
  199. osl.GC()
  200. }