Browse code

Move Linux-only integration/network tests into Linux-only file

Signed-off-by: Rob Murray <rob.murray@docker.com>

Rob Murray authored on 2024/11/25 19:51:06
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,186 @@
0
+package network // import "github.com/docker/docker/integration/network"
1
+
2
+import (
3
+	"bytes"
4
+	"fmt"
5
+	"os/exec"
6
+	"strings"
7
+	"testing"
8
+
9
+	containertypes "github.com/docker/docker/api/types/container"
10
+	networktypes "github.com/docker/docker/api/types/network"
11
+	"github.com/docker/docker/integration/internal/container"
12
+	"github.com/docker/docker/integration/internal/network"
13
+	"github.com/docker/docker/testutil"
14
+	"github.com/docker/docker/testutil/daemon"
15
+	"gotest.tools/v3/assert"
16
+	is "gotest.tools/v3/assert/cmp"
17
+	"gotest.tools/v3/icmd"
18
+	"gotest.tools/v3/skip"
19
+)
20
+
21
+func TestRunContainerWithBridgeNone(t *testing.T) {
22
+	skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
23
+	skip.If(t, testEnv.IsUserNamespace)
24
+	skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
25
+
26
+	ctx := testutil.StartSpan(baseContext, t)
27
+
28
+	d := daemon.New(t)
29
+	d.StartWithBusybox(ctx, t, "-b", "none")
30
+	defer d.Stop(t)
31
+
32
+	c := d.NewClientT(t)
33
+
34
+	id1 := container.Run(ctx, t, c)
35
+	defer c.ContainerRemove(ctx, id1, containertypes.RemoveOptions{Force: true})
36
+
37
+	result, err := container.Exec(ctx, c, id1, []string{"ip", "l"})
38
+	assert.NilError(t, err)
39
+	assert.Check(t, is.Equal(false, strings.Contains(result.Combined(), "eth0")), "There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled")
40
+
41
+	id2 := container.Run(ctx, t, c, container.WithNetworkMode("bridge"))
42
+	defer c.ContainerRemove(ctx, id2, containertypes.RemoveOptions{Force: true})
43
+
44
+	result, err = container.Exec(ctx, c, id2, []string{"ip", "l"})
45
+	assert.NilError(t, err)
46
+	assert.Check(t, is.Equal(false, strings.Contains(result.Combined(), "eth0")), "There shouldn't be eth0 in container in bridge mode when bridge network is disabled")
47
+
48
+	nsCommand := "ls -l /proc/self/ns/net | awk -F '->' '{print $2}'"
49
+	cmd := exec.Command("sh", "-c", nsCommand)
50
+	stdout := bytes.NewBuffer(nil)
51
+	cmd.Stdout = stdout
52
+	err = cmd.Run()
53
+	assert.NilError(t, err, "Failed to get current process network namespace: %+v", err)
54
+
55
+	id3 := container.Run(ctx, t, c, container.WithNetworkMode("host"))
56
+	defer c.ContainerRemove(ctx, id3, containertypes.RemoveOptions{Force: true})
57
+
58
+	result, err = container.Exec(ctx, c, id3, []string{"sh", "-c", nsCommand})
59
+	assert.NilError(t, err)
60
+	assert.Check(t, is.Equal(stdout.String(), result.Combined()), "The network namespace of container should be the same with host when --net=host and bridge network is disabled")
61
+}
62
+
63
+func TestHostIPv4BridgeLabel(t *testing.T) {
64
+	skip.If(t, testEnv.IsRemoteDaemon)
65
+	skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
66
+	ctx := testutil.StartSpan(baseContext, t)
67
+
68
+	d := daemon.New(t)
69
+	d.Start(t)
70
+	defer d.Stop(t)
71
+	c := d.NewClientT(t)
72
+	defer c.Close()
73
+
74
+	ipv4SNATAddr := "172.0.0.172"
75
+	// Create a bridge network with --opt com.docker.network.host_ipv4=172.0.0.172
76
+	bridgeName := "hostIPv4Bridge"
77
+	network.CreateNoError(ctx, t, c, bridgeName,
78
+		network.WithDriver("bridge"),
79
+		network.WithOption("com.docker.network.host_ipv4", ipv4SNATAddr),
80
+		network.WithOption("com.docker.network.bridge.name", bridgeName),
81
+	)
82
+	defer network.RemoveNoError(ctx, t, c, bridgeName)
83
+	out, err := c.NetworkInspect(ctx, bridgeName, networktypes.InspectOptions{Verbose: true})
84
+	assert.NilError(t, err)
85
+	assert.Assert(t, len(out.IPAM.Config) > 0)
86
+	// Make sure the SNAT rule exists
87
+	testutil.RunCommand(ctx, "iptables", "-t", "nat", "-C", "POSTROUTING", "-s", out.IPAM.Config[0].Subnet, "!", "-o", bridgeName, "-j", "SNAT", "--to-source", ipv4SNATAddr).Assert(t, icmd.Success)
88
+}
89
+
90
+func TestDefaultNetworkOpts(t *testing.T) {
91
+	skip.If(t, testEnv.IsRemoteDaemon)
92
+	skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
93
+	ctx := testutil.StartSpan(baseContext, t)
94
+
95
+	tests := []struct {
96
+		name       string
97
+		mtu        int
98
+		configFrom bool
99
+		args       []string
100
+	}{
101
+		{
102
+			name: "default value",
103
+			mtu:  1500,
104
+			args: []string{},
105
+		},
106
+		{
107
+			name: "cmdline value",
108
+			mtu:  1234,
109
+			args: []string{"--default-network-opt", "bridge=com.docker.network.driver.mtu=1234"},
110
+		},
111
+		{
112
+			name:       "config-from value",
113
+			configFrom: true,
114
+			mtu:        1233,
115
+			args:       []string{"--default-network-opt", "bridge=com.docker.network.driver.mtu=1234"},
116
+		},
117
+	}
118
+
119
+	for _, tc := range tests {
120
+		t.Run(tc.name, func(t *testing.T) {
121
+			ctx := testutil.StartSpan(ctx, t)
122
+			d := daemon.New(t)
123
+			d.StartWithBusybox(ctx, t, tc.args...)
124
+			defer d.Stop(t)
125
+			c := d.NewClientT(t)
126
+			defer c.Close()
127
+
128
+			if tc.configFrom {
129
+				// Create a new network config
130
+				network.CreateNoError(ctx, t, c, "from-net", func(create *networktypes.CreateOptions) {
131
+					create.ConfigOnly = true
132
+					create.Options = map[string]string{
133
+						"com.docker.network.driver.mtu": fmt.Sprint(tc.mtu),
134
+					}
135
+				})
136
+				defer c.NetworkRemove(ctx, "from-net")
137
+			}
138
+
139
+			// Create a new network
140
+			networkName := "testnet"
141
+			networkId := network.CreateNoError(ctx, t, c, networkName, func(create *networktypes.CreateOptions) {
142
+				if tc.configFrom {
143
+					create.ConfigFrom = &networktypes.ConfigReference{
144
+						Network: "from-net",
145
+					}
146
+				}
147
+			})
148
+			defer c.NetworkRemove(ctx, networkName)
149
+
150
+			// Check the MTU of the bridge itself, before any devices are connected. (The
151
+			// bridge's MTU will be set to the minimum MTU of anything connected to it, but
152
+			// it's set explicitly on the bridge anyway - so it doesn't look like the option
153
+			// was ignored.)
154
+			cmd := exec.Command("ip", "link", "show", "br-"+networkId[:12])
155
+			output, err := cmd.CombinedOutput()
156
+			assert.NilError(t, err)
157
+			assert.Check(t, is.Contains(string(output), fmt.Sprintf(" mtu %d ", tc.mtu)), "Bridge MTU should have been set to %d", tc.mtu)
158
+
159
+			// Start a container to inspect the MTU of its network interface
160
+			id1 := container.Run(ctx, t, c, container.WithNetworkMode(networkName))
161
+			defer c.ContainerRemove(ctx, id1, containertypes.RemoveOptions{Force: true})
162
+
163
+			result, err := container.Exec(ctx, c, id1, []string{"ip", "l", "show", "eth0"})
164
+			assert.NilError(t, err)
165
+			assert.Check(t, is.Contains(result.Combined(), fmt.Sprintf(" mtu %d ", tc.mtu)), "Network MTU should have been set to %d", tc.mtu)
166
+		})
167
+	}
168
+}
169
+
170
+func TestForbidDuplicateNetworkNames(t *testing.T) {
171
+	ctx := testutil.StartSpan(baseContext, t)
172
+
173
+	d := daemon.New(t)
174
+	d.StartWithBusybox(ctx, t)
175
+	defer d.Stop(t)
176
+
177
+	c := d.NewClientT(t)
178
+	defer c.Close()
179
+
180
+	network.CreateNoError(ctx, t, c, "testnet")
181
+	defer network.RemoveNoError(ctx, t, c, "testnet")
182
+
183
+	_, err := c.NetworkCreate(ctx, "testnet", networktypes.CreateOptions{})
184
+	assert.Error(t, err, "Error response from daemon: network with name testnet already exists", "2nd NetworkCreate call should have failed")
185
+}
... ...
@@ -1,70 +1,17 @@
1 1
 package network // import "github.com/docker/docker/integration/network"
2 2
 
3 3
 import (
4
-	"bytes"
5 4
 	"encoding/json"
6
-	"fmt"
7 5
 	"net/http"
8
-	"os/exec"
9
-	"strings"
10 6
 	"testing"
11 7
 
12
-	containertypes "github.com/docker/docker/api/types/container"
13 8
 	networktypes "github.com/docker/docker/api/types/network"
14
-	"github.com/docker/docker/integration/internal/container"
15
-	"github.com/docker/docker/integration/internal/network"
16 9
 	"github.com/docker/docker/testutil"
17
-	"github.com/docker/docker/testutil/daemon"
18 10
 	"github.com/docker/docker/testutil/request"
19 11
 	"gotest.tools/v3/assert"
20 12
 	is "gotest.tools/v3/assert/cmp"
21
-	"gotest.tools/v3/icmd"
22
-	"gotest.tools/v3/skip"
23 13
 )
24 14
 
25
-func TestRunContainerWithBridgeNone(t *testing.T) {
26
-	skip.If(t, testEnv.IsRemoteDaemon, "cannot start daemon on remote test run")
27
-	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
28
-	skip.If(t, testEnv.IsUserNamespace)
29
-	skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
30
-
31
-	ctx := testutil.StartSpan(baseContext, t)
32
-
33
-	d := daemon.New(t)
34
-	d.StartWithBusybox(ctx, t, "-b", "none")
35
-	defer d.Stop(t)
36
-
37
-	c := d.NewClientT(t)
38
-
39
-	id1 := container.Run(ctx, t, c)
40
-	defer c.ContainerRemove(ctx, id1, containertypes.RemoveOptions{Force: true})
41
-
42
-	result, err := container.Exec(ctx, c, id1, []string{"ip", "l"})
43
-	assert.NilError(t, err)
44
-	assert.Check(t, is.Equal(false, strings.Contains(result.Combined(), "eth0")), "There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled")
45
-
46
-	id2 := container.Run(ctx, t, c, container.WithNetworkMode("bridge"))
47
-	defer c.ContainerRemove(ctx, id2, containertypes.RemoveOptions{Force: true})
48
-
49
-	result, err = container.Exec(ctx, c, id2, []string{"ip", "l"})
50
-	assert.NilError(t, err)
51
-	assert.Check(t, is.Equal(false, strings.Contains(result.Combined(), "eth0")), "There shouldn't be eth0 in container in bridge mode when bridge network is disabled")
52
-
53
-	nsCommand := "ls -l /proc/self/ns/net | awk -F '->' '{print $2}'"
54
-	cmd := exec.Command("sh", "-c", nsCommand)
55
-	stdout := bytes.NewBuffer(nil)
56
-	cmd.Stdout = stdout
57
-	err = cmd.Run()
58
-	assert.NilError(t, err, "Failed to get current process network namespace: %+v", err)
59
-
60
-	id3 := container.Run(ctx, t, c, container.WithNetworkMode("host"))
61
-	defer c.ContainerRemove(ctx, id3, containertypes.RemoveOptions{Force: true})
62
-
63
-	result, err = container.Exec(ctx, c, id3, []string{"sh", "-c", nsCommand})
64
-	assert.NilError(t, err)
65
-	assert.Check(t, is.Equal(stdout.String(), result.Combined()), "The network namespace of container should be the same with host when --net=host and bridge network is disabled")
66
-}
67
-
68 15
 // TestNetworkInvalidJSON tests that POST endpoints that expect a body return
69 16
 // the correct error when sending invalid JSON requests.
70 17
 func TestNetworkInvalidJSON(t *testing.T) {
... ...
@@ -156,131 +103,3 @@ func TestNetworkList(t *testing.T) {
156 156
 		})
157 157
 	}
158 158
 }
159
-
160
-func TestHostIPv4BridgeLabel(t *testing.T) {
161
-	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
162
-	skip.If(t, testEnv.IsRemoteDaemon)
163
-	skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
164
-	ctx := testutil.StartSpan(baseContext, t)
165
-
166
-	d := daemon.New(t)
167
-	d.Start(t)
168
-	defer d.Stop(t)
169
-	c := d.NewClientT(t)
170
-	defer c.Close()
171
-
172
-	ipv4SNATAddr := "172.0.0.172"
173
-	// Create a bridge network with --opt com.docker.network.host_ipv4=172.0.0.172
174
-	bridgeName := "hostIPv4Bridge"
175
-	network.CreateNoError(ctx, t, c, bridgeName,
176
-		network.WithDriver("bridge"),
177
-		network.WithOption("com.docker.network.host_ipv4", ipv4SNATAddr),
178
-		network.WithOption("com.docker.network.bridge.name", bridgeName),
179
-	)
180
-	defer network.RemoveNoError(ctx, t, c, bridgeName)
181
-	out, err := c.NetworkInspect(ctx, bridgeName, networktypes.InspectOptions{Verbose: true})
182
-	assert.NilError(t, err)
183
-	assert.Assert(t, len(out.IPAM.Config) > 0)
184
-	// Make sure the SNAT rule exists
185
-	testutil.RunCommand(ctx, "iptables", "-t", "nat", "-C", "POSTROUTING", "-s", out.IPAM.Config[0].Subnet, "!", "-o", bridgeName, "-j", "SNAT", "--to-source", ipv4SNATAddr).Assert(t, icmd.Success)
186
-}
187
-
188
-func TestDefaultNetworkOpts(t *testing.T) {
189
-	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
190
-	skip.If(t, testEnv.IsRemoteDaemon)
191
-	skip.If(t, testEnv.IsRootless, "rootless mode has different view of network")
192
-	ctx := testutil.StartSpan(baseContext, t)
193
-
194
-	tests := []struct {
195
-		name       string
196
-		mtu        int
197
-		configFrom bool
198
-		args       []string
199
-	}{
200
-		{
201
-			name: "default value",
202
-			mtu:  1500,
203
-			args: []string{},
204
-		},
205
-		{
206
-			name: "cmdline value",
207
-			mtu:  1234,
208
-			args: []string{"--default-network-opt", "bridge=com.docker.network.driver.mtu=1234"},
209
-		},
210
-		{
211
-			name:       "config-from value",
212
-			configFrom: true,
213
-			mtu:        1233,
214
-			args:       []string{"--default-network-opt", "bridge=com.docker.network.driver.mtu=1234"},
215
-		},
216
-	}
217
-
218
-	for _, tc := range tests {
219
-		t.Run(tc.name, func(t *testing.T) {
220
-			ctx := testutil.StartSpan(ctx, t)
221
-			d := daemon.New(t)
222
-			d.StartWithBusybox(ctx, t, tc.args...)
223
-			defer d.Stop(t)
224
-			c := d.NewClientT(t)
225
-			defer c.Close()
226
-
227
-			if tc.configFrom {
228
-				// Create a new network config
229
-				network.CreateNoError(ctx, t, c, "from-net", func(create *networktypes.CreateOptions) {
230
-					create.ConfigOnly = true
231
-					create.Options = map[string]string{
232
-						"com.docker.network.driver.mtu": fmt.Sprint(tc.mtu),
233
-					}
234
-				})
235
-				defer c.NetworkRemove(ctx, "from-net")
236
-			}
237
-
238
-			// Create a new network
239
-			networkName := "testnet"
240
-			networkId := network.CreateNoError(ctx, t, c, networkName, func(create *networktypes.CreateOptions) {
241
-				if tc.configFrom {
242
-					create.ConfigFrom = &networktypes.ConfigReference{
243
-						Network: "from-net",
244
-					}
245
-				}
246
-			})
247
-			defer c.NetworkRemove(ctx, networkName)
248
-
249
-			// Check the MTU of the bridge itself, before any devices are connected. (The
250
-			// bridge's MTU will be set to the minimum MTU of anything connected to it, but
251
-			// it's set explicitly on the bridge anyway - so it doesn't look like the option
252
-			// was ignored.)
253
-			cmd := exec.Command("ip", "link", "show", "br-"+networkId[:12])
254
-			output, err := cmd.CombinedOutput()
255
-			assert.NilError(t, err)
256
-			assert.Check(t, is.Contains(string(output), fmt.Sprintf(" mtu %d ", tc.mtu)), "Bridge MTU should have been set to %d", tc.mtu)
257
-
258
-			// Start a container to inspect the MTU of its network interface
259
-			id1 := container.Run(ctx, t, c, container.WithNetworkMode(networkName))
260
-			defer c.ContainerRemove(ctx, id1, containertypes.RemoveOptions{Force: true})
261
-
262
-			result, err := container.Exec(ctx, c, id1, []string{"ip", "l", "show", "eth0"})
263
-			assert.NilError(t, err)
264
-			assert.Check(t, is.Contains(result.Combined(), fmt.Sprintf(" mtu %d ", tc.mtu)), "Network MTU should have been set to %d", tc.mtu)
265
-		})
266
-	}
267
-}
268
-
269
-func TestForbidDuplicateNetworkNames(t *testing.T) {
270
-	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
271
-
272
-	ctx := testutil.StartSpan(baseContext, t)
273
-
274
-	d := daemon.New(t)
275
-	d.StartWithBusybox(ctx, t)
276
-	defer d.Stop(t)
277
-
278
-	c := d.NewClientT(t)
279
-	defer c.Close()
280
-
281
-	network.CreateNoError(ctx, t, c, "testnet")
282
-	defer network.RemoveNoError(ctx, t, c, "testnet")
283
-
284
-	_, err := c.NetworkCreate(ctx, "testnet", networktypes.CreateOptions{})
285
-	assert.Error(t, err, "Error response from daemon: network with name testnet already exists", "2nd NetworkCreate call should have failed")
286
-}