macvlan_test.go 9.6 KB

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