Browse code

Added network package to integration/internal to refactor integration tests calls to client.NetworkCreate

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>

Arash Deshmeh authored on 2018/06/02 22:44:45
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,35 @@
0
+package network
1
+
2
+import (
3
+	"context"
4
+	"testing"
5
+
6
+	"github.com/docker/docker/api/types"
7
+	"github.com/docker/docker/client"
8
+	"github.com/gotestyourself/gotestyourself/assert"
9
+)
10
+
11
+func createNetwork(ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) (string, error) {
12
+	config := types.NetworkCreate{}
13
+
14
+	for _, op := range ops {
15
+		op(&config)
16
+	}
17
+
18
+	n, err := client.NetworkCreate(ctx, name, config)
19
+	return n.ID, err
20
+}
21
+
22
+// Create creates a network with the specified options
23
+func Create(ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) (string, error) {
24
+	return createNetwork(ctx, client, name, ops...)
25
+}
26
+
27
+// CreateNoError creates a network with the specified options and verifies there were no errors
28
+func CreateNoError(t *testing.T, ctx context.Context, client client.APIClient, name string, ops ...func(*types.NetworkCreate)) string { // nolint: golint
29
+	t.Helper()
30
+
31
+	name, err := createNetwork(ctx, client, name, ops...)
32
+	assert.NilError(t, err)
33
+	return name
34
+}
0 35
new file mode 100644
... ...
@@ -0,0 +1,57 @@
0
+package network
1
+
2
+import (
3
+	"github.com/docker/docker/api/types"
4
+	"github.com/docker/docker/api/types/network"
5
+)
6
+
7
+// WithDriver sets the driver of the network
8
+func WithDriver(driver string) func(*types.NetworkCreate) {
9
+	return func(n *types.NetworkCreate) {
10
+		n.Driver = driver
11
+	}
12
+}
13
+
14
+// WithIPv6 Enables IPv6 on the network
15
+func WithIPv6() func(*types.NetworkCreate) {
16
+	return func(n *types.NetworkCreate) {
17
+		n.EnableIPv6 = true
18
+	}
19
+}
20
+
21
+// WithMacvlan sets the network as macvlan with the specified parent
22
+func WithMacvlan(parent string) func(*types.NetworkCreate) {
23
+	return func(n *types.NetworkCreate) {
24
+		n.Driver = "macvlan"
25
+		if parent != "" {
26
+			n.Options = map[string]string{
27
+				"parent": parent,
28
+			}
29
+		}
30
+	}
31
+}
32
+
33
+// WithOption adds the specified key/value pair to network's options
34
+func WithOption(key, value string) func(*types.NetworkCreate) {
35
+	return func(n *types.NetworkCreate) {
36
+		if n.Options == nil {
37
+			n.Options = map[string]string{}
38
+		}
39
+		n.Options[key] = value
40
+	}
41
+}
42
+
43
+// WithIPAM adds an IPAM with the specified Subnet and Gateway to the network
44
+func WithIPAM(subnet, gateway string) func(*types.NetworkCreate) {
45
+	return func(n *types.NetworkCreate) {
46
+		if n.IPAM == nil {
47
+			n.IPAM = &network.IPAM{}
48
+		}
49
+
50
+		n.IPAM.Config = append(n.IPAM.Config, network.IPAMConfig{
51
+			Subnet:     subnet,
52
+			Gateway:    gateway,
53
+			AuxAddress: map[string]string{},
54
+		})
55
+	}
56
+}