Skip to content

Commit 162ba60

Browse files
author
Vincent Demeester
authored
Merge pull request moby#37195 from adshmh/refactor-integration-test-create-network-calls-macvlan
Refactor macvlan network integration tests to use network.Create
2 parents 712dd62 + 0418893 commit 162ba60

File tree

3 files changed

+142
-89
lines changed

3 files changed

+142
-89
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package network
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/docker/docker/api/types"
8+
"github.com/docker/docker/client"
9+
"github.com/gotestyourself/gotestyourself/assert"
10+
)
11+
12+
func createNetwork(ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) (string, error) {
13+
config := types.NetworkCreate{}
14+
15+
for _, op := range ops {
16+
op(&config)
17+
}
18+
19+
n, err := client.NetworkCreate(ctx, name, config)
20+
return n.ID, err
21+
}
22+
23+
// Create creates a network with the specified options
24+
func Create(ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) (string, error) {
25+
return createNetwork(ctx, client, name, ops...)
26+
}
27+
28+
// CreateNoError creates a network with the specified options and verifies there were no errors
29+
func CreateNoError(t *testing.T, ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) string { // nolint: golint
30+
t.Helper()
31+
32+
name, err := createNetwork(ctx, client, name, ops...)
33+
assert.NilError(t, err)
34+
return name
35+
}

integration/internal/network/ops.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package network
2+
3+
import (
4+
"github.com/docker/docker/api/types"
5+
"github.com/docker/docker/api/types/network"
6+
)
7+
8+
// WithDriver sets the driver of the network
9+
func WithDriver(driver string) func(*types.NetworkCreate) {
10+
return func(n *types.NetworkCreate) {
11+
n.Driver = driver
12+
}
13+
}
14+
15+
// WithIPv6 Enables IPv6 on the network
16+
func WithIPv6() func(*types.NetworkCreate) {
17+
return func(n *types.NetworkCreate) {
18+
n.EnableIPv6 = true
19+
}
20+
}
21+
22+
// WithMacvlan sets the network as macvlan with the specified parent
23+
func WithMacvlan(parent string) func(*types.NetworkCreate) {
24+
return func(n *types.NetworkCreate) {
25+
n.Driver = "macvlan"
26+
if parent != "" {
27+
n.Options = map[string]string{
28+
"parent": parent,
29+
}
30+
}
31+
}
32+
}
33+
34+
// WithOption adds the specified key/value pair to network's options
35+
func WithOption(key, value string) func(*types.NetworkCreate) {
36+
return func(n *types.NetworkCreate) {
37+
if n.Options == nil {
38+
n.Options = map[string]string{}
39+
}
40+
n.Options[key] = value
41+
}
42+
}
43+
44+
// WithIPAM adds an IPAM with the specified Subnet and Gateway to the network
45+
func WithIPAM(subnet, gateway string) func(*types.NetworkCreate) {
46+
return func(n *types.NetworkCreate) {
47+
if n.IPAM == nil {
48+
n.IPAM = &network.IPAM{}
49+
}
50+
51+
n.IPAM.Config = append(n.IPAM.Config, network.IPAMConfig{
52+
Subnet: subnet,
53+
Gateway: gateway,
54+
AuxAddress: map[string]string{},
55+
})
56+
}
57+
}

integration/network/macvlan/macvlan_test.go

+50-89
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"time"
88

99
"github.com/docker/docker/api/types"
10-
"github.com/docker/docker/api/types/network"
1110
"github.com/docker/docker/client"
1211
"github.com/docker/docker/integration/internal/container"
12+
net "github.com/docker/docker/integration/internal/network"
1313
n "github.com/docker/docker/integration/network"
1414
"github.com/docker/docker/internal/test/daemon"
1515
"github.com/gotestyourself/gotestyourself/assert"
@@ -33,16 +33,13 @@ func TestDockerNetworkMacvlanPersistance(t *testing.T) {
3333
client, err := d.NewClient()
3434
assert.NilError(t, err)
3535

36-
_, err = client.NetworkCreate(context.Background(), "dm-persist", types.NetworkCreate{
37-
Driver: "macvlan",
38-
Options: map[string]string{
39-
"parent": "dm-dummy0.60",
40-
},
41-
})
42-
assert.NilError(t, err)
43-
assert.Check(t, n.IsNetworkAvailable(client, "dm-persist"))
36+
netName := "dm-persist"
37+
net.CreateNoError(t, context.Background(), client, netName,
38+
net.WithMacvlan("dm-dummy0.60"),
39+
)
40+
assert.Check(t, n.IsNetworkAvailable(client, netName))
4441
d.Restart(t)
45-
assert.Check(t, n.IsNetworkAvailable(client, "dm-persist"))
42+
assert.Check(t, n.IsNetworkAvailable(client, netName))
4643
}
4744

4845
func TestDockerNetworkMacvlan(t *testing.T) {
@@ -91,56 +88,50 @@ func testMacvlanOverlapParent(client client.APIClient) func(*testing.T) {
9188
n.CreateMasterDummy(t, master)
9289
defer n.DeleteInterface(t, master)
9390

94-
_, err := client.NetworkCreate(context.Background(), "dm-subinterface", types.NetworkCreate{
95-
Driver: "macvlan",
96-
Options: map[string]string{
97-
"parent": "dm-dummy0.40",
98-
},
99-
})
100-
assert.NilError(t, err)
101-
assert.Check(t, n.IsNetworkAvailable(client, "dm-subinterface"))
91+
netName := "dm-subinterface"
92+
parentName := "dm-dummy0.40"
93+
net.CreateNoError(t, context.Background(), client, netName,
94+
net.WithMacvlan(parentName),
95+
)
96+
assert.Check(t, n.IsNetworkAvailable(client, netName))
10297

103-
_, err = client.NetworkCreate(context.Background(), "dm-parent-net-overlap", types.NetworkCreate{
104-
Driver: "macvlan",
105-
Options: map[string]string{
106-
"parent": "dm-dummy0.40",
107-
},
108-
})
98+
_, err := net.Create(context.Background(), client, "dm-parent-net-overlap",
99+
net.WithMacvlan(parentName),
100+
)
109101
assert.Check(t, err != nil)
102+
110103
// delete the network while preserving the parent link
111-
err = client.NetworkRemove(context.Background(), "dm-subinterface")
104+
err = client.NetworkRemove(context.Background(), netName)
112105
assert.NilError(t, err)
113106

114-
assert.Check(t, n.IsNetworkNotAvailable(client, "dm-subinterface"))
107+
assert.Check(t, n.IsNetworkNotAvailable(client, netName))
115108
// verify the network delete did not delete the predefined link
116-
n.LinkExists(t, "dm-dummy0")
109+
n.LinkExists(t, master)
117110
}
118111
}
119112

120113
func testMacvlanSubinterface(client client.APIClient) func(*testing.T) {
121114
return func(t *testing.T) {
122115
// verify the same parent interface cannot be used if already in use by an existing network
123116
master := "dm-dummy0"
117+
parentName := "dm-dummy0.20"
124118
n.CreateMasterDummy(t, master)
125119
defer n.DeleteInterface(t, master)
126-
n.CreateVlanInterface(t, master, "dm-dummy0.20", "20")
120+
n.CreateVlanInterface(t, master, parentName, "20")
127121

128-
_, err := client.NetworkCreate(context.Background(), "dm-subinterface", types.NetworkCreate{
129-
Driver: "macvlan",
130-
Options: map[string]string{
131-
"parent": "dm-dummy0.20",
132-
},
133-
})
134-
assert.NilError(t, err)
135-
assert.Check(t, n.IsNetworkAvailable(client, "dm-subinterface"))
122+
netName := "dm-subinterface"
123+
net.CreateNoError(t, context.Background(), client, netName,
124+
net.WithMacvlan(parentName),
125+
)
126+
assert.Check(t, n.IsNetworkAvailable(client, netName))
136127

137128
// delete the network while preserving the parent link
138-
err = client.NetworkRemove(context.Background(), "dm-subinterface")
129+
err := client.NetworkRemove(context.Background(), netName)
139130
assert.NilError(t, err)
140131

141-
assert.Check(t, n.IsNetworkNotAvailable(client, "dm-subinterface"))
132+
assert.Check(t, n.IsNetworkNotAvailable(client, netName))
142133
// verify the network delete did not delete the predefined link
143-
n.LinkExists(t, "dm-dummy0.20")
134+
n.LinkExists(t, parentName)
144135
}
145136
}
146137

@@ -190,34 +181,17 @@ func testMacvlanInternalMode(client client.APIClient) func(*testing.T) {
190181

191182
func testMacvlanMultiSubnet(client client.APIClient) func(*testing.T) {
192183
return func(t *testing.T) {
193-
_, err := client.NetworkCreate(context.Background(), "dualstackbridge", types.NetworkCreate{
194-
Driver: "macvlan",
195-
EnableIPv6: true,
196-
IPAM: &network.IPAM{
197-
Config: []network.IPAMConfig{
198-
{
199-
Subnet: "172.28.100.0/24",
200-
AuxAddress: map[string]string{},
201-
},
202-
{
203-
Subnet: "172.28.102.0/24",
204-
Gateway: "172.28.102.254",
205-
AuxAddress: map[string]string{},
206-
},
207-
{
208-
Subnet: "2001:db8:abc2::/64",
209-
AuxAddress: map[string]string{},
210-
},
211-
{
212-
Subnet: "2001:db8:abc4::/64",
213-
Gateway: "2001:db8:abc4::254",
214-
AuxAddress: map[string]string{},
215-
},
216-
},
217-
},
218-
})
219-
assert.NilError(t, err)
220-
assert.Check(t, n.IsNetworkAvailable(client, "dualstackbridge"))
184+
netName := "dualstackbridge"
185+
net.CreateNoError(t, context.Background(), client, netName,
186+
net.WithMacvlan(""),
187+
net.WithIPv6(),
188+
net.WithIPAM("172.28.100.0/24", ""),
189+
net.WithIPAM("172.28.102.0/24", "172.28.102.254"),
190+
net.WithIPAM("2001:db8:abc2::/64", ""),
191+
net.WithIPAM("2001:db8:abc4::/64", "2001:db8:abc4::254"),
192+
)
193+
194+
assert.Check(t, n.IsNetworkAvailable(client, netName))
221195

222196
// 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
223197
ctx := context.Background()
@@ -276,28 +250,15 @@ func testMacvlanMultiSubnet(client client.APIClient) func(*testing.T) {
276250
func testMacvlanAddressing(client client.APIClient) func(*testing.T) {
277251
return func(t *testing.T) {
278252
// Ensure the default gateways, next-hops and default dev devices are properly set
279-
_, err := client.NetworkCreate(context.Background(), "dualstackbridge", types.NetworkCreate{
280-
Driver: "macvlan",
281-
EnableIPv6: true,
282-
Options: map[string]string{
283-
"macvlan_mode": "bridge",
284-
},
285-
IPAM: &network.IPAM{
286-
Config: []network.IPAMConfig{
287-
{
288-
Subnet: "172.28.130.0/24",
289-
AuxAddress: map[string]string{},
290-
},
291-
{
292-
Subnet: "2001:db8:abca::/64",
293-
Gateway: "2001:db8:abca::254",
294-
AuxAddress: map[string]string{},
295-
},
296-
},
297-
},
298-
})
299-
assert.NilError(t, err)
300-
assert.Check(t, n.IsNetworkAvailable(client, "dualstackbridge"))
253+
netName := "dualstackbridge"
254+
net.CreateNoError(t, context.Background(), client, netName,
255+
net.WithMacvlan(""),
256+
net.WithIPv6(),
257+
net.WithOption("macvlan_mode", "bridge"),
258+
net.WithIPAM("172.28.130.0/24", ""),
259+
net.WithIPAM("2001:db8:abca::/64", "2001:db8:abca::254"),
260+
)
261+
assert.Check(t, n.IsNetworkAvailable(client, netName))
301262

302263
ctx := context.Background()
303264
id1 := container.Run(t, ctx, client,

0 commit comments

Comments
 (0)