macvlan_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/daemon"
  12. "gotest.tools/v3/assert"
  13. "gotest.tools/v3/skip"
  14. )
  15. func TestDockerNetworkMacvlanPersistance(t *testing.T) {
  16. // verify the driver automatically provisions the 802.1q link (dm-dummy0.60)
  17. skip.If(t, testEnv.IsRemoteDaemon)
  18. skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
  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. skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
  37. for _, tc := range []struct {
  38. name string
  39. test func(client.APIClient) func(*testing.T)
  40. }{
  41. {
  42. name: "Subinterface",
  43. test: testMacvlanSubinterface,
  44. }, {
  45. name: "OverlapParent",
  46. test: testMacvlanOverlapParent,
  47. }, {
  48. name: "NilParent",
  49. test: testMacvlanNilParent,
  50. }, {
  51. name: "InternalMode",
  52. test: testMacvlanInternalMode,
  53. }, {
  54. name: "MultiSubnet",
  55. test: testMacvlanMultiSubnet,
  56. }, {
  57. name: "Addressing",
  58. test: testMacvlanAddressing,
  59. },
  60. } {
  61. d := daemon.New(t)
  62. d.StartWithBusybox(t)
  63. c := d.NewClientT(t)
  64. t.Run(tc.name, tc.test(c))
  65. d.Stop(t)
  66. // FIXME(vdemeester) clean network
  67. }
  68. }
  69. func testMacvlanOverlapParent(client client.APIClient) func(*testing.T) {
  70. return func(t *testing.T) {
  71. // verify the same parent interface cannot be used if already in use by an existing network
  72. master := "dm-dummy0"
  73. n.CreateMasterDummy(t, master)
  74. defer n.DeleteInterface(t, master)
  75. netName := "dm-subinterface"
  76. parentName := "dm-dummy0.40"
  77. net.CreateNoError(context.Background(), t, client, netName,
  78. net.WithMacvlan(parentName),
  79. )
  80. assert.Check(t, n.IsNetworkAvailable(client, netName))
  81. _, err := net.Create(context.Background(), client, "dm-parent-net-overlap",
  82. net.WithMacvlan(parentName),
  83. )
  84. assert.Check(t, err != nil)
  85. // delete the network while preserving the parent link
  86. err = client.NetworkRemove(context.Background(), netName)
  87. assert.NilError(t, err)
  88. assert.Check(t, n.IsNetworkNotAvailable(client, netName))
  89. // verify the network delete did not delete the predefined link
  90. n.LinkExists(t, master)
  91. }
  92. }
  93. func testMacvlanSubinterface(client client.APIClient) func(*testing.T) {
  94. return func(t *testing.T) {
  95. // verify the same parent interface cannot be used if already in use by an existing network
  96. master := "dm-dummy0"
  97. parentName := "dm-dummy0.20"
  98. n.CreateMasterDummy(t, master)
  99. defer n.DeleteInterface(t, master)
  100. n.CreateVlanInterface(t, master, parentName, "20")
  101. netName := "dm-subinterface"
  102. net.CreateNoError(context.Background(), t, client, netName,
  103. net.WithMacvlan(parentName),
  104. )
  105. assert.Check(t, n.IsNetworkAvailable(client, netName))
  106. // delete the network while preserving the parent link
  107. err := client.NetworkRemove(context.Background(), netName)
  108. assert.NilError(t, err)
  109. assert.Check(t, n.IsNetworkNotAvailable(client, netName))
  110. // verify the network delete did not delete the predefined link
  111. n.LinkExists(t, parentName)
  112. }
  113. }
  114. func testMacvlanNilParent(client client.APIClient) func(*testing.T) {
  115. return func(t *testing.T) {
  116. // macvlan bridge mode - dummy parent interface is provisioned dynamically
  117. netName := "dm-nil-parent"
  118. net.CreateNoError(context.Background(), t, client, netName,
  119. net.WithMacvlan(""),
  120. )
  121. assert.Check(t, n.IsNetworkAvailable(client, netName))
  122. ctx := context.Background()
  123. id1 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  124. id2 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  125. _, err := container.Exec(ctx, client, id2, []string{"ping", "-c", "1", id1})
  126. assert.Check(t, err == nil)
  127. }
  128. }
  129. func testMacvlanInternalMode(client client.APIClient) func(*testing.T) {
  130. return func(t *testing.T) {
  131. // macvlan bridge mode - dummy parent interface is provisioned dynamically
  132. netName := "dm-internal"
  133. net.CreateNoError(context.Background(), t, client, netName,
  134. net.WithMacvlan(""),
  135. net.WithInternal(),
  136. )
  137. assert.Check(t, n.IsNetworkAvailable(client, netName))
  138. ctx := context.Background()
  139. id1 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  140. id2 := container.Run(ctx, t, client, container.WithNetworkMode(netName))
  141. result, _ := container.Exec(ctx, client, id1, []string{"ping", "-c", "1", "8.8.8.8"})
  142. assert.Check(t, strings.Contains(result.Combined(), "Network is unreachable"))
  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. }