sandbox_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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) (NetworkController, []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. cfgOptions, err := OptionBoltdbWithRandomDBFile()
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. c, err := New(append(cfgOptions, config.OptionDriverConfig(netType, genericOption))...)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  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. c, _ := getTestEnv(t)
  54. ctrlr := c.(*controller)
  55. sbx, err := ctrlr.NewSandbox("sandbox0")
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. if err := sbx.Delete(); err != nil {
  60. t.Fatal(err)
  61. }
  62. if len(ctrlr.sandboxes) != 0 {
  63. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  64. }
  65. osl.GC()
  66. }
  67. // // If different priorities are specified, internal option and ipv6 addresses mustn't influence endpoint order
  68. func TestSandboxAddMultiPrio(t *testing.T) {
  69. if !testutils.IsRunningInContainer() {
  70. defer testutils.SetupTestOSContext(t)()
  71. }
  72. opts := [][]NetworkOption{
  73. {NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)},
  74. {NetworkOptionInternalNetwork()},
  75. {},
  76. }
  77. c, nws := getTestEnv(t, opts...)
  78. ctrlr := c.(*controller)
  79. sbx, err := ctrlr.NewSandbox("sandbox1")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. sid := sbx.ID()
  84. ep1, err := nws[0].CreateEndpoint("ep1")
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. ep2, err := nws[1].CreateEndpoint("ep2")
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. ep3, err := nws[2].CreateEndpoint("ep3")
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. if err := ep1.Join(sbx, JoinOptionPriority(1)); err != nil {
  97. t.Fatal(err)
  98. }
  99. if err := ep2.Join(sbx, JoinOptionPriority(2)); err != nil {
  100. t.Fatal(err)
  101. }
  102. if err := ep3.Join(sbx, JoinOptionPriority(3)); err != nil {
  103. t.Fatal(err)
  104. }
  105. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
  106. t.Fatal("Expected ep3 to be at the top of the heap. But did not find ep3 at the top of the heap")
  107. }
  108. if len(sbx.Endpoints()) != 3 {
  109. t.Fatal("Expected 3 endpoints to be connected to the sandbox.")
  110. }
  111. if err := ep3.Leave(sbx); err != nil {
  112. t.Fatal(err)
  113. }
  114. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep2.ID() {
  115. 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")
  116. }
  117. if err := ep2.Leave(sbx); err != nil {
  118. t.Fatal(err)
  119. }
  120. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep1.ID() {
  121. 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")
  122. }
  123. // Re-add ep3 back
  124. if err := ep3.Join(sbx, JoinOptionPriority(3)); err != nil {
  125. t.Fatal(err)
  126. }
  127. if ctrlr.sandboxes[sid].endpoints[0].ID() != ep3.ID() {
  128. 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")
  129. }
  130. if err := sbx.Delete(); err != nil {
  131. t.Fatal(err)
  132. }
  133. if len(ctrlr.sandboxes) != 0 {
  134. t.Fatalf("controller sandboxes is not empty. len = %d", len(ctrlr.sandboxes))
  135. }
  136. osl.GC()
  137. }
  138. func TestSandboxAddSamePrio(t *testing.T) {
  139. if !testutils.IsRunningInContainer() {
  140. defer testutils.SetupTestOSContext(t)()
  141. }
  142. opts := [][]NetworkOption{
  143. {},
  144. {},
  145. {NetworkOptionEnableIPv6(true), NetworkOptionIpam(ipamapi.DefaultIPAM, "", nil, []*IpamConf{{PreferredPool: "fe90::/64"}}, nil)},
  146. {NetworkOptionInternalNetwork()},
  147. }
  148. c, nws := getTestEnv(t, opts...)
  149. ctrlr := c.(*controller)
  150. sbx, err := ctrlr.NewSandbox("sandbox1")
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. sid := sbx.ID()
  155. epNw1, err := nws[1].CreateEndpoint("ep1")
  156. if err != nil {
  157. t.Fatal(err)
  158. }
  159. epIPv6, err := nws[2].CreateEndpoint("ep2")
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. epInternal, err := nws[3].CreateEndpoint("ep3")
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. epNw0, err := nws[0].CreateEndpoint("ep4")
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. if err := epNw1.Join(sbx); err != nil {
  172. t.Fatal(err)
  173. }
  174. if err := epIPv6.Join(sbx); err != nil {
  175. t.Fatal(err)
  176. }
  177. if err := epInternal.Join(sbx); err != nil {
  178. t.Fatal(err)
  179. }
  180. if err := epNw0.Join(sbx); err != nil {
  181. t.Fatal(err)
  182. }
  183. // order should now be: epIPv6, epNw0, epNw1, epInternal
  184. if len(sbx.Endpoints()) != 4 {
  185. t.Fatal("Expected 4 endpoints to be connected to the sandbox.")
  186. }
  187. // IPv6 has precedence over IPv4
  188. if ctrlr.sandboxes[sid].endpoints[0].ID() != epIPv6.ID() {
  189. t.Fatal("Expected epIPv6 to be at the top of the heap. But did not find epIPv6 at the top of the heap")
  190. }
  191. // internal network has lowest precedence
  192. if ctrlr.sandboxes[sid].endpoints[3].ID() != epInternal.ID() {
  193. t.Fatal("Expected epInternal to be at the bottom of the heap. But did not find epInternal at the bottom of the heap")
  194. }
  195. if err := epIPv6.Leave(sbx); err != nil {
  196. t.Fatal(err)
  197. }
  198. // 'test_nw_0' has precedence over 'test_nw_1'
  199. if ctrlr.sandboxes[sid].endpoints[0].ID() != epNw0.ID() {
  200. 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")
  201. }
  202. if err := epNw1.Leave(sbx); err != nil {
  203. t.Fatal(err)
  204. }
  205. if err := sbx.Delete(); err != nil {
  206. t.Fatal(err)
  207. }
  208. if len(ctrlr.sandboxes) != 0 {
  209. t.Fatalf("controller containers is not empty. len = %d", len(ctrlr.sandboxes))
  210. }
  211. osl.GC()
  212. }