1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #37195 from adshmh/refactor-integration-test-create-network-calls-macvlan

Refactor macvlan network integration tests to use network.Create
This commit is contained in:
Vincent Demeester 2018-06-08 09:46:04 +02:00 committed by GitHub
commit 162ba6016d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 142 additions and 89 deletions

View file

@ -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
}

View file

@ -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{},
})
}
}

View file

@ -7,9 +7,9 @@ import (
"time" "time"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/docker/integration/internal/container" "github.com/docker/docker/integration/internal/container"
net "github.com/docker/docker/integration/internal/network"
n "github.com/docker/docker/integration/network" n "github.com/docker/docker/integration/network"
"github.com/docker/docker/internal/test/daemon" "github.com/docker/docker/internal/test/daemon"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
@ -33,16 +33,13 @@ func TestDockerNetworkMacvlanPersistance(t *testing.T) {
client, err := d.NewClient() client, err := d.NewClient()
assert.NilError(t, err) assert.NilError(t, err)
_, err = client.NetworkCreate(context.Background(), "dm-persist", types.NetworkCreate{ netName := "dm-persist"
Driver: "macvlan", net.CreateNoError(t, context.Background(), client, netName,
Options: map[string]string{ net.WithMacvlan("dm-dummy0.60"),
"parent": "dm-dummy0.60", )
}, assert.Check(t, n.IsNetworkAvailable(client, netName))
})
assert.NilError(t, err)
assert.Check(t, n.IsNetworkAvailable(client, "dm-persist"))
d.Restart(t) d.Restart(t)
assert.Check(t, n.IsNetworkAvailable(client, "dm-persist")) assert.Check(t, n.IsNetworkAvailable(client, netName))
} }
func TestDockerNetworkMacvlan(t *testing.T) { func TestDockerNetworkMacvlan(t *testing.T) {
@ -91,29 +88,25 @@ func testMacvlanOverlapParent(client client.APIClient) func(*testing.T) {
n.CreateMasterDummy(t, master) n.CreateMasterDummy(t, master)
defer n.DeleteInterface(t, master) defer n.DeleteInterface(t, master)
_, err := client.NetworkCreate(context.Background(), "dm-subinterface", types.NetworkCreate{ netName := "dm-subinterface"
Driver: "macvlan", parentName := "dm-dummy0.40"
Options: map[string]string{ net.CreateNoError(t, context.Background(), client, netName,
"parent": "dm-dummy0.40", net.WithMacvlan(parentName),
}, )
}) assert.Check(t, n.IsNetworkAvailable(client, netName))
assert.NilError(t, err)
assert.Check(t, n.IsNetworkAvailable(client, "dm-subinterface"))
_, err = client.NetworkCreate(context.Background(), "dm-parent-net-overlap", types.NetworkCreate{ _, err := net.Create(context.Background(), client, "dm-parent-net-overlap",
Driver: "macvlan", net.WithMacvlan(parentName),
Options: map[string]string{ )
"parent": "dm-dummy0.40",
},
})
assert.Check(t, err != nil) assert.Check(t, err != nil)
// delete the network while preserving the parent link // 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.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 // 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) { return func(t *testing.T) {
// verify the same parent interface cannot be used if already in use by an existing network // verify the same parent interface cannot be used if already in use by an existing network
master := "dm-dummy0" master := "dm-dummy0"
parentName := "dm-dummy0.20"
n.CreateMasterDummy(t, master) n.CreateMasterDummy(t, master)
defer n.DeleteInterface(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{ netName := "dm-subinterface"
Driver: "macvlan", net.CreateNoError(t, context.Background(), client, netName,
Options: map[string]string{ net.WithMacvlan(parentName),
"parent": "dm-dummy0.20", )
}, assert.Check(t, n.IsNetworkAvailable(client, netName))
})
assert.NilError(t, err)
assert.Check(t, n.IsNetworkAvailable(client, "dm-subinterface"))
// delete the network while preserving the parent link // 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.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 // 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) { func testMacvlanMultiSubnet(client client.APIClient) func(*testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
_, err := client.NetworkCreate(context.Background(), "dualstackbridge", types.NetworkCreate{ netName := "dualstackbridge"
Driver: "macvlan", net.CreateNoError(t, context.Background(), client, netName,
EnableIPv6: true, net.WithMacvlan(""),
IPAM: &network.IPAM{ net.WithIPv6(),
Config: []network.IPAMConfig{ net.WithIPAM("172.28.100.0/24", ""),
{ net.WithIPAM("172.28.102.0/24", "172.28.102.254"),
Subnet: "172.28.100.0/24", net.WithIPAM("2001:db8:abc2::/64", ""),
AuxAddress: map[string]string{}, net.WithIPAM("2001:db8:abc4::/64", "2001:db8:abc4::254"),
}, )
{
Subnet: "172.28.102.0/24", assert.Check(t, n.IsNetworkAvailable(client, netName))
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"))
// 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 // 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() ctx := context.Background()
@ -276,28 +250,15 @@ func testMacvlanMultiSubnet(client client.APIClient) func(*testing.T) {
func testMacvlanAddressing(client client.APIClient) func(*testing.T) { func testMacvlanAddressing(client client.APIClient) func(*testing.T) {
return func(t *testing.T) { return func(t *testing.T) {
// Ensure the default gateways, next-hops and default dev devices are properly set // Ensure the default gateways, next-hops and default dev devices are properly set
_, err := client.NetworkCreate(context.Background(), "dualstackbridge", types.NetworkCreate{ netName := "dualstackbridge"
Driver: "macvlan", net.CreateNoError(t, context.Background(), client, netName,
EnableIPv6: true, net.WithMacvlan(""),
Options: map[string]string{ net.WithIPv6(),
"macvlan_mode": "bridge", net.WithOption("macvlan_mode", "bridge"),
}, net.WithIPAM("172.28.130.0/24", ""),
IPAM: &network.IPAM{ net.WithIPAM("2001:db8:abca::/64", "2001:db8:abca::254"),
Config: []network.IPAMConfig{ )
{ assert.Check(t, n.IsNetworkAvailable(client, netName))
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"))
ctx := context.Background() ctx := context.Background()
id1 := container.Run(t, ctx, client, id1 := container.Run(t, ctx, client,