sandbox_test.go 6.0 KB

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