From 8a3da41a28ec50386216822b27a7ab0ad309d11b Mon Sep 17 00:00:00 2001 From: Arash Deshmeh Date: Sat, 2 Jun 2018 09:44:45 -0400 Subject: [PATCH 1/2] Added network package to integration/internal to refactor integration tests calls to client.NetworkCreate Signed-off-by: Arash Deshmeh Upstream-commit: 0e3012cfff1e81f7c779b1310f6069dc909d7987 Component: engine --- .../integration/internal/network/network.go | 35 ++++++++++++ .../integration/internal/network/ops.go | 57 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 components/engine/integration/internal/network/network.go create mode 100644 components/engine/integration/internal/network/ops.go diff --git a/components/engine/integration/internal/network/network.go b/components/engine/integration/internal/network/network.go new file mode 100644 index 0000000000..b9550362f0 --- /dev/null +++ b/components/engine/integration/internal/network/network.go @@ -0,0 +1,35 @@ +package network + +import ( + "context" + "testing" + + "github.com/docker/docker/api/types" + "github.com/docker/docker/client" + "github.com/gotestyourself/gotestyourself/assert" +) + +func createNetwork(ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) (string, error) { + config := types.NetworkCreate{} + + for _, op := range ops { + op(&config) + } + + n, err := client.NetworkCreate(ctx, name, config) + return n.ID, err +} + +// Create creates a network with the specified options +func Create(ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) (string, error) { + return createNetwork(ctx, client, name, ops...) +} + +// CreateNoError creates a network with the specified options and verifies there were no errors +func CreateNoError(t *testing.T, ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) string { // nolint: golint + t.Helper() + + name, err := createNetwork(ctx, client, name, ops...) + assert.NilError(t, err) + return name +} diff --git a/components/engine/integration/internal/network/ops.go b/components/engine/integration/internal/network/ops.go new file mode 100644 index 0000000000..f7639ff307 --- /dev/null +++ b/components/engine/integration/internal/network/ops.go @@ -0,0 +1,57 @@ +package network + +import ( + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/network" +) + +// WithDriver sets the driver of the network +func WithDriver(driver string) func(*types.NetworkCreate) { + return func(n *types.NetworkCreate) { + n.Driver = driver + } +} + +// WithIPv6 Enables IPv6 on the network +func WithIPv6() func(*types.NetworkCreate) { + return func(n *types.NetworkCreate) { + n.EnableIPv6 = true + } +} + +// WithMacvlan sets the network as macvlan with the specified parent +func WithMacvlan(parent string) func(*types.NetworkCreate) { + return func(n *types.NetworkCreate) { + n.Driver = "macvlan" + if parent != "" { + n.Options = map[string]string{ + "parent": parent, + } + } + } +} + +// WithOption adds the specified key/value pair to network's options +func WithOption(key, value string) func(*types.NetworkCreate) { + return func(n *types.NetworkCreate) { + if n.Options == nil { + n.Options = map[string]string{} + } + n.Options[key] = value + } +} + +// WithIPAM adds an IPAM with the specified Subnet and Gateway to the network +func WithIPAM(subnet, gateway string) func(*types.NetworkCreate) { + return func(n *types.NetworkCreate) { + if n.IPAM == nil { + n.IPAM = &network.IPAM{} + } + + n.IPAM.Config = append(n.IPAM.Config, network.IPAMConfig{ + Subnet: subnet, + Gateway: gateway, + AuxAddress: map[string]string{}, + }) + } +} From f422827dd45a81e7fde2aaa987b540a25fc0bf14 Mon Sep 17 00:00:00 2001 From: Arash Deshmeh Date: Sat, 2 Jun 2018 09:45:59 -0400 Subject: [PATCH 2/2] refactored integration tests under integration/network/macvlan to use network.Create Signed-off-by: Arash Deshmeh Upstream-commit: 0418893f0b5a7413edd950cfabd2337161edc705 Component: engine --- .../network/macvlan/macvlan_test.go | 139 +++++++----------- 1 file changed, 50 insertions(+), 89 deletions(-) diff --git a/components/engine/integration/network/macvlan/macvlan_test.go b/components/engine/integration/network/macvlan/macvlan_test.go index 7358af873b..7d9acd813e 100644 --- a/components/engine/integration/network/macvlan/macvlan_test.go +++ b/components/engine/integration/network/macvlan/macvlan_test.go @@ -7,9 +7,9 @@ import ( "time" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/network" "github.com/docker/docker/client" "github.com/docker/docker/integration/internal/container" + net "github.com/docker/docker/integration/internal/network" n "github.com/docker/docker/integration/network" "github.com/docker/docker/internal/test/daemon" "github.com/gotestyourself/gotestyourself/assert" @@ -33,16 +33,13 @@ func TestDockerNetworkMacvlanPersistance(t *testing.T) { client, err := d.NewClient() assert.NilError(t, err) - _, err = client.NetworkCreate(context.Background(), "dm-persist", types.NetworkCreate{ - Driver: "macvlan", - Options: map[string]string{ - "parent": "dm-dummy0.60", - }, - }) - assert.NilError(t, err) - assert.Check(t, n.IsNetworkAvailable(client, "dm-persist")) + netName := "dm-persist" + net.CreateNoError(t, context.Background(), client, netName, + net.WithMacvlan("dm-dummy0.60"), + ) + assert.Check(t, n.IsNetworkAvailable(client, netName)) d.Restart(t) - assert.Check(t, n.IsNetworkAvailable(client, "dm-persist")) + assert.Check(t, n.IsNetworkAvailable(client, netName)) } func TestDockerNetworkMacvlan(t *testing.T) { @@ -91,29 +88,25 @@ func testMacvlanOverlapParent(client client.APIClient) func(*testing.T) { n.CreateMasterDummy(t, master) defer n.DeleteInterface(t, master) - _, err := client.NetworkCreate(context.Background(), "dm-subinterface", types.NetworkCreate{ - Driver: "macvlan", - Options: map[string]string{ - "parent": "dm-dummy0.40", - }, - }) - assert.NilError(t, err) - assert.Check(t, n.IsNetworkAvailable(client, "dm-subinterface")) + netName := "dm-subinterface" + parentName := "dm-dummy0.40" + net.CreateNoError(t, context.Background(), client, netName, + net.WithMacvlan(parentName), + ) + assert.Check(t, n.IsNetworkAvailable(client, netName)) - _, err = client.NetworkCreate(context.Background(), "dm-parent-net-overlap", types.NetworkCreate{ - Driver: "macvlan", - Options: map[string]string{ - "parent": "dm-dummy0.40", - }, - }) + _, err := net.Create(context.Background(), client, "dm-parent-net-overlap", + net.WithMacvlan(parentName), + ) assert.Check(t, err != nil) + // delete the network while preserving the parent link - err = client.NetworkRemove(context.Background(), "dm-subinterface") + err = client.NetworkRemove(context.Background(), netName) assert.NilError(t, err) - assert.Check(t, n.IsNetworkNotAvailable(client, "dm-subinterface")) + assert.Check(t, n.IsNetworkNotAvailable(client, netName)) // verify the network delete did not delete the predefined link - n.LinkExists(t, "dm-dummy0") + n.LinkExists(t, master) } } @@ -121,26 +114,24 @@ func testMacvlanSubinterface(client client.APIClient) func(*testing.T) { return func(t *testing.T) { // verify the same parent interface cannot be used if already in use by an existing network master := "dm-dummy0" + parentName := "dm-dummy0.20" n.CreateMasterDummy(t, master) defer n.DeleteInterface(t, master) - n.CreateVlanInterface(t, master, "dm-dummy0.20", "20") + n.CreateVlanInterface(t, master, parentName, "20") - _, err := client.NetworkCreate(context.Background(), "dm-subinterface", types.NetworkCreate{ - Driver: "macvlan", - Options: map[string]string{ - "parent": "dm-dummy0.20", - }, - }) - assert.NilError(t, err) - assert.Check(t, n.IsNetworkAvailable(client, "dm-subinterface")) + netName := "dm-subinterface" + net.CreateNoError(t, context.Background(), client, netName, + net.WithMacvlan(parentName), + ) + assert.Check(t, n.IsNetworkAvailable(client, netName)) // delete the network while preserving the parent link - err = client.NetworkRemove(context.Background(), "dm-subinterface") + err := client.NetworkRemove(context.Background(), netName) assert.NilError(t, err) - assert.Check(t, n.IsNetworkNotAvailable(client, "dm-subinterface")) + assert.Check(t, n.IsNetworkNotAvailable(client, netName)) // verify the network delete did not delete the predefined link - n.LinkExists(t, "dm-dummy0.20") + n.LinkExists(t, parentName) } } @@ -190,34 +181,17 @@ func testMacvlanInternalMode(client client.APIClient) func(*testing.T) { func testMacvlanMultiSubnet(client client.APIClient) func(*testing.T) { return func(t *testing.T) { - _, err := client.NetworkCreate(context.Background(), "dualstackbridge", types.NetworkCreate{ - Driver: "macvlan", - EnableIPv6: true, - IPAM: &network.IPAM{ - Config: []network.IPAMConfig{ - { - Subnet: "172.28.100.0/24", - AuxAddress: map[string]string{}, - }, - { - Subnet: "172.28.102.0/24", - Gateway: "172.28.102.254", - AuxAddress: map[string]string{}, - }, - { - Subnet: "2001:db8:abc2::/64", - AuxAddress: map[string]string{}, - }, - { - Subnet: "2001:db8:abc4::/64", - Gateway: "2001:db8:abc4::254", - AuxAddress: map[string]string{}, - }, - }, - }, - }) - assert.NilError(t, err) - assert.Check(t, n.IsNetworkAvailable(client, "dualstackbridge")) + netName := "dualstackbridge" + net.CreateNoError(t, context.Background(), client, netName, + net.WithMacvlan(""), + net.WithIPv6(), + net.WithIPAM("172.28.100.0/24", ""), + net.WithIPAM("172.28.102.0/24", "172.28.102.254"), + net.WithIPAM("2001:db8:abc2::/64", ""), + net.WithIPAM("2001:db8:abc4::/64", "2001:db8:abc4::254"), + ) + + assert.Check(t, n.IsNetworkAvailable(client, netName)) // 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 ctx := context.Background() @@ -276,28 +250,15 @@ func testMacvlanMultiSubnet(client client.APIClient) func(*testing.T) { func testMacvlanAddressing(client client.APIClient) func(*testing.T) { return func(t *testing.T) { // Ensure the default gateways, next-hops and default dev devices are properly set - _, err := client.NetworkCreate(context.Background(), "dualstackbridge", types.NetworkCreate{ - Driver: "macvlan", - EnableIPv6: true, - Options: map[string]string{ - "macvlan_mode": "bridge", - }, - IPAM: &network.IPAM{ - Config: []network.IPAMConfig{ - { - Subnet: "172.28.130.0/24", - AuxAddress: map[string]string{}, - }, - { - Subnet: "2001:db8:abca::/64", - Gateway: "2001:db8:abca::254", - AuxAddress: map[string]string{}, - }, - }, - }, - }) - assert.NilError(t, err) - assert.Check(t, n.IsNetworkAvailable(client, "dualstackbridge")) + netName := "dualstackbridge" + net.CreateNoError(t, context.Background(), client, netName, + net.WithMacvlan(""), + net.WithIPv6(), + net.WithOption("macvlan_mode", "bridge"), + net.WithIPAM("172.28.130.0/24", ""), + net.WithIPAM("2001:db8:abca::/64", "2001:db8:abca::254"), + ) + assert.Check(t, n.IsNetworkAvailable(client, netName)) ctx := context.Background() id1 := container.Run(t, ctx, client,