sandbox_unix_test.go 5.8 KB

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