macvlan_test.go 9.8 KB

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