macvlan_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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(context.Context, client.APIClient) func(*testing.T)
  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(ctx, c)
  72. })
  73. // FIXME(vdemeester) clean network
  74. }
  75. }
  76. func testMacvlanOverlapParent(ctx context.Context, client client.APIClient) func(*testing.T) {
  77. return func(t *testing.T) {
  78. // verify the same parent interface cannot be used if already in use by an existing network
  79. master := "dm-dummy0"
  80. n.CreateMasterDummy(ctx, t, master)
  81. defer n.DeleteInterface(ctx, t, master)
  82. netName := "dm-subinterface"
  83. parentName := "dm-dummy0.40"
  84. net.CreateNoError(ctx, t, client, netName,
  85. net.WithMacvlan(parentName),
  86. )
  87. assert.Check(t, n.IsNetworkAvailable(ctx, client, netName))
  88. _, err := net.Create(ctx, client, "dm-parent-net-overlap",
  89. net.WithMacvlan(parentName),
  90. )
  91. assert.Check(t, err != nil)
  92. // delete the network while preserving the parent link
  93. err = client.NetworkRemove(ctx, netName)
  94. assert.NilError(t, err)
  95. assert.Check(t, n.IsNetworkNotAvailable(ctx, client, netName))
  96. // verify the network delete did not delete the predefined link
  97. n.LinkExists(ctx, t, master)
  98. }
  99. }
  100. func testMacvlanSubinterface(ctx context.Context, client client.APIClient) func(*testing.T) {
  101. return func(t *testing.T) {
  102. // verify the same parent interface cannot be used if already in use by an existing network
  103. master := "dm-dummy0"
  104. parentName := "dm-dummy0.20"
  105. n.CreateMasterDummy(ctx, t, master)
  106. defer n.DeleteInterface(ctx, t, master)
  107. n.CreateVlanInterface(ctx, t, master, parentName, "20")
  108. netName := "dm-subinterface"
  109. net.CreateNoError(ctx, t, client, netName,
  110. net.WithMacvlan(parentName),
  111. )
  112. assert.Check(t, n.IsNetworkAvailable(ctx, client, netName))
  113. // delete the network while preserving the parent link
  114. err := client.NetworkRemove(ctx, netName)
  115. assert.NilError(t, err)
  116. assert.Check(t, n.IsNetworkNotAvailable(ctx, client, netName))
  117. // verify the network delete did not delete the predefined link
  118. n.LinkExists(ctx, t, parentName)
  119. }
  120. }
  121. func testMacvlanNilParent(ctx context.Context, client client.APIClient) func(*testing.T) {
  122. return func(t *testing.T) {
  123. // macvlan bridge mode - dummy parent interface is provisioned dynamically
  124. netName := "dm-nil-parent"
  125. net.CreateNoError(ctx, t, client, netName,
  126. net.WithMacvlan(""),
  127. )
  128. assert.Check(t, n.IsNetworkAvailable(ctx, client, netName))
  129. id1 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  130. id2 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  131. _, err := container.Exec(ctx, client, id2, []string{"ping", "-c", "1", id1})
  132. assert.Check(t, err == nil)
  133. }
  134. }
  135. func testMacvlanInternalMode(ctx context.Context, client client.APIClient) func(*testing.T) {
  136. return func(t *testing.T) {
  137. // macvlan bridge mode - dummy parent interface is provisioned dynamically
  138. netName := "dm-internal"
  139. net.CreateNoError(ctx, t, client, netName,
  140. net.WithMacvlan(""),
  141. net.WithInternal(),
  142. )
  143. assert.Check(t, n.IsNetworkAvailable(ctx, client, netName))
  144. id1 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  145. id2 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  146. result, _ := container.Exec(ctx, client, id1, []string{"ping", "-c", "1", "8.8.8.8"})
  147. assert.Check(t, strings.Contains(result.Combined(), "Network is unreachable"))
  148. _, err := container.Exec(ctx, client, id2, []string{"ping", "-c", "1", id1})
  149. assert.Check(t, err == nil)
  150. }
  151. }
  152. func testMacvlanMultiSubnet(ctx context.Context, client client.APIClient) func(*testing.T) {
  153. return func(t *testing.T) {
  154. netName := "dualstackbridge"
  155. net.CreateNoError(ctx, t, client, netName,
  156. net.WithMacvlan(""),
  157. net.WithIPv6(),
  158. net.WithIPAM("172.28.100.0/24", ""),
  159. net.WithIPAM("172.28.102.0/24", "172.28.102.254"),
  160. net.WithIPAM("2001:db8:abc2::/64", ""),
  161. net.WithIPAM("2001:db8:abc4::/64", "2001:db8:abc4::254"),
  162. )
  163. assert.Check(t, n.IsNetworkAvailable(ctx, client, netName))
  164. // 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
  165. id1 := container.Run(ctx, t, client,
  166. container.WithNetworkMode("dualstackbridge"),
  167. container.WithIPv4("dualstackbridge", "172.28.100.20"),
  168. container.WithIPv6("dualstackbridge", "2001:db8:abc2::20"),
  169. )
  170. id2 := container.Run(ctx, t, client,
  171. container.WithNetworkMode("dualstackbridge"),
  172. container.WithIPv4("dualstackbridge", "172.28.100.21"),
  173. container.WithIPv6("dualstackbridge", "2001:db8:abc2::21"),
  174. )
  175. c1, err := client.ContainerInspect(ctx, id1)
  176. assert.NilError(t, err)
  177. // verify ipv4 connectivity to the explicit --ipv address second to first
  178. _, err = container.Exec(ctx, client, id2, []string{"ping", "-c", "1", c1.NetworkSettings.Networks["dualstackbridge"].IPAddress})
  179. assert.NilError(t, err)
  180. // verify ipv6 connectivity to the explicit --ipv6 address second to first
  181. _, err = container.Exec(ctx, client, id2, []string{"ping6", "-c", "1", c1.NetworkSettings.Networks["dualstackbridge"].GlobalIPv6Address})
  182. assert.NilError(t, err)
  183. // 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
  184. id3 := container.Run(ctx, t, client,
  185. container.WithNetworkMode("dualstackbridge"),
  186. container.WithIPv4("dualstackbridge", "172.28.102.20"),
  187. container.WithIPv6("dualstackbridge", "2001:db8:abc4::20"),
  188. )
  189. id4 := container.Run(ctx, t, client,
  190. container.WithNetworkMode("dualstackbridge"),
  191. container.WithIPv4("dualstackbridge", "172.28.102.21"),
  192. container.WithIPv6("dualstackbridge", "2001:db8:abc4::21"),
  193. )
  194. c3, err := client.ContainerInspect(ctx, id3)
  195. assert.NilError(t, err)
  196. // verify ipv4 connectivity to the explicit --ipv address from third to fourth
  197. _, err = container.Exec(ctx, client, id4, []string{"ping", "-c", "1", c3.NetworkSettings.Networks["dualstackbridge"].IPAddress})
  198. assert.NilError(t, err)
  199. // verify ipv6 connectivity to the explicit --ipv6 address from third to fourth
  200. _, err = container.Exec(ctx, client, id4, []string{"ping6", "-c", "1", c3.NetworkSettings.Networks["dualstackbridge"].GlobalIPv6Address})
  201. assert.NilError(t, err)
  202. // Inspect the v4 gateway to ensure the proper default GW was assigned
  203. assert.Equal(t, c1.NetworkSettings.Networks["dualstackbridge"].Gateway, "172.28.100.1")
  204. // Inspect the v6 gateway to ensure the proper default GW was assigned
  205. assert.Equal(t, c1.NetworkSettings.Networks["dualstackbridge"].IPv6Gateway, "2001:db8:abc2::1")
  206. // Inspect the v4 gateway to ensure the proper explicitly assigned default GW was assigned
  207. assert.Equal(t, c3.NetworkSettings.Networks["dualstackbridge"].Gateway, "172.28.102.254")
  208. // Inspect the v6 gateway to ensure the proper explicitly assigned default GW was assigned
  209. assert.Equal(t, c3.NetworkSettings.Networks["dualstackbridge"].IPv6Gateway, "2001:db8:abc4::254")
  210. }
  211. }
  212. func testMacvlanAddressing(ctx context.Context, client client.APIClient) func(*testing.T) {
  213. return func(t *testing.T) {
  214. // Ensure the default gateways, next-hops and default dev devices are properly set
  215. netName := "dualstackbridge"
  216. net.CreateNoError(ctx, t, client, netName,
  217. net.WithMacvlan(""),
  218. net.WithIPv6(),
  219. net.WithOption("macvlan_mode", "bridge"),
  220. net.WithIPAM("172.28.130.0/24", ""),
  221. net.WithIPAM("2001:db8:abca::/64", "2001:db8:abca::254"),
  222. )
  223. assert.Check(t, n.IsNetworkAvailable(ctx, client, netName))
  224. id1 := container.Run(ctx, t, client,
  225. container.WithNetworkMode("dualstackbridge"),
  226. )
  227. // Validate macvlan bridge mode defaults gateway sets the default IPAM next-hop inferred from the subnet
  228. result, err := container.Exec(ctx, client, id1, []string{"ip", "route"})
  229. assert.NilError(t, err)
  230. assert.Check(t, strings.Contains(result.Combined(), "default via 172.28.130.1 dev eth0"))
  231. // Validate macvlan bridge mode sets the v6 gateway to the user specified default gateway/next-hop
  232. result, err = container.Exec(ctx, client, id1, []string{"ip", "-6", "route"})
  233. assert.NilError(t, err)
  234. assert.Check(t, strings.Contains(result.Combined(), "default via 2001:db8:abca::254 dev eth0"))
  235. }
  236. }