macvlan_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // +build !windows
  2. package macvlan // import "github.com/docker/docker/integration/network/macvlan"
  3. import (
  4. "context"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/docker/docker/client"
  9. "github.com/docker/docker/integration/internal/container"
  10. net "github.com/docker/docker/integration/internal/network"
  11. n "github.com/docker/docker/integration/network"
  12. "github.com/docker/docker/internal/test/daemon"
  13. "gotest.tools/assert"
  14. "gotest.tools/skip"
  15. )
  16. func TestDockerNetworkMacvlanPersistance(t *testing.T) {
  17. // verify the driver automatically provisions the 802.1q link (dm-dummy0.60)
  18. skip.If(t, testEnv.IsRemoteDaemon)
  19. d := daemon.New(t)
  20. d.StartWithBusybox(t)
  21. defer d.Stop(t)
  22. master := "dm-dummy0"
  23. n.CreateMasterDummy(t, master)
  24. defer n.DeleteInterface(t, master)
  25. c := d.NewClientT(t)
  26. netName := "dm-persist"
  27. net.CreateNoError(context.Background(), t, c, netName,
  28. net.WithMacvlan("dm-dummy0.60"),
  29. )
  30. assert.Check(t, n.IsNetworkAvailable(c, netName))
  31. d.Restart(t)
  32. assert.Check(t, n.IsNetworkAvailable(c, netName))
  33. }
  34. func TestDockerNetworkMacvlan(t *testing.T) {
  35. skip.If(t, testEnv.IsRemoteDaemon)
  36. for _, tc := range []struct {
  37. name string
  38. test func(client.APIClient) func(*testing.T)
  39. }{
  40. {
  41. name: "Subinterface",
  42. test: testMacvlanSubinterface,
  43. }, {
  44. name: "OverlapParent",
  45. test: testMacvlanOverlapParent,
  46. }, {
  47. name: "NilParent",
  48. test: testMacvlanNilParent,
  49. }, {
  50. name: "InternalMode",
  51. test: testMacvlanInternalMode,
  52. }, {
  53. name: "Addressing",
  54. test: testMacvlanAddressing,
  55. },
  56. } {
  57. d := daemon.New(t)
  58. d.StartWithBusybox(t)
  59. c := d.NewClientT(t)
  60. t.Run(tc.name, tc.test(c))
  61. d.Stop(t)
  62. // FIXME(vdemeester) clean network
  63. }
  64. }
  65. func testMacvlanOverlapParent(client client.APIClient) func(*testing.T) {
  66. return func(t *testing.T) {
  67. // verify the same parent interface cannot be used if already in use by an existing network
  68. master := "dm-dummy0"
  69. n.CreateMasterDummy(t, master)
  70. defer n.DeleteInterface(t, master)
  71. netName := "dm-subinterface"
  72. parentName := "dm-dummy0.40"
  73. net.CreateNoError(context.Background(), t, client, netName,
  74. net.WithMacvlan(parentName),
  75. )
  76. assert.Check(t, n.IsNetworkAvailable(client, netName))
  77. _, err := net.Create(context.Background(), client, "dm-parent-net-overlap",
  78. net.WithMacvlan(parentName),
  79. )
  80. assert.Check(t, err != nil)
  81. // delete the network while preserving the parent link
  82. err = client.NetworkRemove(context.Background(), netName)
  83. assert.NilError(t, err)
  84. assert.Check(t, n.IsNetworkNotAvailable(client, netName))
  85. // verify the network delete did not delete the predefined link
  86. n.LinkExists(t, master)
  87. }
  88. }
  89. func testMacvlanSubinterface(client client.APIClient) func(*testing.T) {
  90. return func(t *testing.T) {
  91. // verify the same parent interface cannot be used if already in use by an existing network
  92. master := "dm-dummy0"
  93. parentName := "dm-dummy0.20"
  94. n.CreateMasterDummy(t, master)
  95. defer n.DeleteInterface(t, master)
  96. n.CreateVlanInterface(t, master, parentName, "20")
  97. netName := "dm-subinterface"
  98. net.CreateNoError(context.Background(), t, client, netName,
  99. net.WithMacvlan(parentName),
  100. )
  101. assert.Check(t, n.IsNetworkAvailable(client, netName))
  102. // delete the network while preserving the parent link
  103. err := client.NetworkRemove(context.Background(), netName)
  104. assert.NilError(t, err)
  105. assert.Check(t, n.IsNetworkNotAvailable(client, netName))
  106. // verify the network delete did not delete the predefined link
  107. n.LinkExists(t, parentName)
  108. }
  109. }
  110. func testMacvlanNilParent(client client.APIClient) func(*testing.T) {
  111. return func(t *testing.T) {
  112. // macvlan bridge mode - dummy parent interface is provisioned dynamically
  113. netName := "dm-nil-parent"
  114. net.CreateNoError(context.Background(), t, client, netName,
  115. net.WithMacvlan(""),
  116. )
  117. assert.Check(t, n.IsNetworkAvailable(client, netName))
  118. ctx := context.Background()
  119. id1 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  120. id2 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  121. _, err := container.Exec(ctx, client, id2, []string{"ping", "-c", "1", id1})
  122. assert.Check(t, err == nil)
  123. }
  124. }
  125. func testMacvlanInternalMode(client client.APIClient) func(*testing.T) {
  126. return func(t *testing.T) {
  127. // macvlan bridge mode - dummy parent interface is provisioned dynamically
  128. netName := "dm-internal"
  129. net.CreateNoError(context.Background(), t, client, netName,
  130. net.WithMacvlan(""),
  131. net.WithInternal(),
  132. )
  133. assert.Check(t, n.IsNetworkAvailable(client, netName))
  134. ctx := context.Background()
  135. id1 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  136. id2 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  137. timeoutCtx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
  138. defer cancel()
  139. _, err := container.Exec(timeoutCtx, client, id1, []string{"ping", "-c", "1", "-w", "1", "8.8.8.8"})
  140. // FIXME(vdemeester) check the time of error ?
  141. assert.Check(t, err != nil)
  142. assert.Check(t, timeoutCtx.Err() == context.DeadlineExceeded)
  143. _, err = container.Exec(ctx, client, id2, []string{"ping", "-c", "1", id1})
  144. assert.Check(t, err == nil)
  145. }
  146. }
  147. func testMacvlanMultiSubnet(client client.APIClient) func(*testing.T) {
  148. return func(t *testing.T) {
  149. netName := "dualstackbridge"
  150. net.CreateNoError(context.Background(), t, client, netName,
  151. net.WithMacvlan(""),
  152. net.WithIPv6(),
  153. net.WithIPAM("172.28.100.0/24", ""),
  154. net.WithIPAM("172.28.102.0/24", "172.28.102.254"),
  155. net.WithIPAM("2001:db8:abc2::/64", ""),
  156. net.WithIPAM("2001:db8:abc4::/64", "2001:db8:abc4::254"),
  157. )
  158. assert.Check(t, n.IsNetworkAvailable(client, netName))
  159. // start dual stack containers and verify the user specified --ip and --ip6 addresses on subnets 172.28.100.0/24 and 2001:db8:abc2::/64
  160. ctx := context.Background()
  161. id1 := container.Run(ctx, t, client,
  162. container.WithNetworkMode("dualstackbridge"),
  163. container.WithIPv4("dualstackbridge", "172.28.100.20"),
  164. container.WithIPv6("dualstackbridge", "2001:db8:abc2::20"),
  165. )
  166. id2 := container.Run(ctx, t, client,
  167. container.WithNetworkMode("dualstackbridge"),
  168. container.WithIPv4("dualstackbridge", "172.28.100.21"),
  169. container.WithIPv6("dualstackbridge", "2001:db8:abc2::21"),
  170. )
  171. c1, err := client.ContainerInspect(ctx, id1)
  172. assert.NilError(t, err)
  173. // verify ipv4 connectivity to the explicit --ipv address second to first
  174. _, err = container.Exec(ctx, client, id2, []string{"ping", "-c", "1", c1.NetworkSettings.Networks["dualstackbridge"].IPAddress})
  175. assert.NilError(t, err)
  176. // verify ipv6 connectivity to the explicit --ipv6 address second to first
  177. _, err = container.Exec(ctx, client, id2, []string{"ping6", "-c", "1", c1.NetworkSettings.Networks["dualstackbridge"].GlobalIPv6Address})
  178. assert.NilError(t, err)
  179. // start dual stack containers and verify the user specified --ip and --ip6 addresses on subnets 172.28.102.0/24 and 2001:db8:abc4::/64
  180. id3 := container.Run(ctx, t, client,
  181. container.WithNetworkMode("dualstackbridge"),
  182. container.WithIPv4("dualstackbridge", "172.28.102.20"),
  183. container.WithIPv6("dualstackbridge", "2001:db8:abc4::20"),
  184. )
  185. id4 := container.Run(ctx, t, client,
  186. container.WithNetworkMode("dualstackbridge"),
  187. container.WithIPv4("dualstackbridge", "172.28.102.21"),
  188. container.WithIPv6("dualstackbridge", "2001:db8:abc4::21"),
  189. )
  190. c3, err := client.ContainerInspect(ctx, id3)
  191. assert.NilError(t, err)
  192. // verify ipv4 connectivity to the explicit --ipv address from third to fourth
  193. _, err = container.Exec(ctx, client, id4, []string{"ping", "-c", "1", c3.NetworkSettings.Networks["dualstackbridge"].IPAddress})
  194. assert.NilError(t, err)
  195. // verify ipv6 connectivity to the explicit --ipv6 address from third to fourth
  196. _, err = container.Exec(ctx, client, id4, []string{"ping6", "-c", "1", c3.NetworkSettings.Networks["dualstackbridge"].GlobalIPv6Address})
  197. assert.NilError(t, err)
  198. // Inspect the v4 gateway to ensure the proper default GW was assigned
  199. assert.Equal(t, c1.NetworkSettings.Networks["dualstackbridge"].Gateway, "172.28.100.1")
  200. // Inspect the v6 gateway to ensure the proper default GW was assigned
  201. assert.Equal(t, c1.NetworkSettings.Networks["dualstackbridge"].IPv6Gateway, "2001:db8:abc2::1")
  202. // Inspect the v4 gateway to ensure the proper explicitly assigned default GW was assigned
  203. assert.Equal(t, c3.NetworkSettings.Networks["dualstackbridge"].Gateway, "172.28.102.254")
  204. // Inspect the v6 gateway to ensure the proper explicitly assigned default GW was assigned
  205. assert.Equal(t, c3.NetworkSettings.Networks["dualstackbridge"].IPv6Gateway, "2001:db8.abc4::254")
  206. }
  207. }
  208. func testMacvlanAddressing(client client.APIClient) func(*testing.T) {
  209. return func(t *testing.T) {
  210. // Ensure the default gateways, next-hops and default dev devices are properly set
  211. netName := "dualstackbridge"
  212. net.CreateNoError(context.Background(), t, client, netName,
  213. net.WithMacvlan(""),
  214. net.WithIPv6(),
  215. net.WithOption("macvlan_mode", "bridge"),
  216. net.WithIPAM("172.28.130.0/24", ""),
  217. net.WithIPAM("2001:db8:abca::/64", "2001:db8:abca::254"),
  218. )
  219. assert.Check(t, n.IsNetworkAvailable(client, netName))
  220. ctx := context.Background()
  221. id1 := container.Run(ctx, t, client,
  222. container.WithNetworkMode("dualstackbridge"),
  223. )
  224. // Validate macvlan bridge mode defaults gateway sets the default IPAM next-hop inferred from the subnet
  225. result, err := container.Exec(ctx, client, id1, []string{"ip", "route"})
  226. assert.NilError(t, err)
  227. assert.Check(t, strings.Contains(result.Combined(), "default via 172.28.130.1 dev eth0"))
  228. // Validate macvlan bridge mode sets the v6 gateway to the user specified default gateway/next-hop
  229. result, err = container.Exec(ctx, client, id1, []string{"ip", "-6", "route"})
  230. assert.NilError(t, err)
  231. assert.Check(t, strings.Contains(result.Combined(), "default via 2001:db8:abca::254 dev eth0"))
  232. }
  233. }