Browse code

Move netmode validation to server

Signed-off-by: John Howard <jhoward@microsoft.com>

John Howard authored on 2015/07/10 07:12:36
Showing 15 changed files
... ...
@@ -148,7 +148,7 @@ RUN set -x \
148 148
 	&& rm -rf "$GOPATH"
149 149
 
150 150
 # Get the "docker-py" source so we can run their integration tests
151
-ENV DOCKER_PY_COMMIT 8a87001d09852058f08a807ab6e8491d57ca1e88
151
+ENV DOCKER_PY_COMMIT 139850f3f3b17357bab5ba3edfb745fb14043764
152 152
 RUN git clone https://github.com/docker/docker-py.git /docker-py \
153 153
 	&& cd /docker-py \
154 154
 	&& git checkout -q $DOCKER_PY_COMMIT
... ...
@@ -264,6 +264,10 @@ func (container *Container) Start() (err error) {
264 264
 		return err
265 265
 	}
266 266
 
267
+	// Make sure NetworkMode has an acceptable value. We do this to ensure
268
+	// backwards API compatibility.
269
+	container.hostConfig = runconfig.SetDefaultNetModeIfBlank(container.hostConfig)
270
+
267 271
 	if err := container.initializeNetworking(); err != nil {
268 272
 		return err
269 273
 	}
... ...
@@ -913,11 +913,6 @@ func (container *Container) configureNetwork(networkName, service, networkDriver
913 913
 func (container *Container) initializeNetworking() error {
914 914
 	var err error
915 915
 
916
-	// Make sure NetworkMode has an acceptable value before
917
-	// initializing networking.
918
-	if container.hostConfig.NetworkMode == runconfig.NetworkMode("") {
919
-		container.hostConfig.NetworkMode = runconfig.NetworkMode("default")
920
-	}
921 916
 	if container.hostConfig.NetworkMode.IsContainer() {
922 917
 		// we need to get the hosts files from the container to join
923 918
 		nc, err := container.getNetworkedContainer()
924 919
new file mode 100644
... ...
@@ -0,0 +1,162 @@
0
+package main
1
+
2
+import (
3
+	"os/exec"
4
+	"strings"
5
+
6
+	"github.com/docker/docker/runconfig"
7
+	"github.com/go-check/check"
8
+)
9
+
10
+// GH14530. Validates combinations of --net= with other options
11
+
12
+// stringCheckPS is how the output of PS starts in order to validate that
13
+// the command executed in a container did really run PS correctly.
14
+const stringCheckPS = "PID   USER"
15
+
16
+// checkContains is a helper function that validates a command output did
17
+// contain what was expected.
18
+func checkContains(expected string, out string, c *check.C) {
19
+	if !strings.Contains(out, expected) {
20
+		c.Fatalf("Expected '%s', got '%s'", expected, out)
21
+	}
22
+}
23
+
24
+func (s *DockerSuite) TestNetHostname(c *check.C) {
25
+
26
+	var (
27
+		out    string
28
+		err    error
29
+		runCmd *exec.Cmd
30
+	)
31
+
32
+	runCmd = exec.Command(dockerBinary, "run", "-h=name", "busybox", "ps")
33
+	if out, _, err = runCommandWithOutput(runCmd); err != nil {
34
+		c.Fatalf(out, err)
35
+	}
36
+	checkContains(stringCheckPS, out, c)
37
+
38
+	runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "ps")
39
+	if out, _, err = runCommandWithOutput(runCmd); err != nil {
40
+		c.Fatalf(out, err)
41
+	}
42
+	checkContains(stringCheckPS, out, c)
43
+
44
+	runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=bridge", "busybox", "ps")
45
+	if out, _, err = runCommandWithOutput(runCmd); err != nil {
46
+		c.Fatalf(out, err)
47
+	}
48
+	checkContains(stringCheckPS, out, c)
49
+
50
+	runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=none", "busybox", "ps")
51
+	if out, _, err = runCommandWithOutput(runCmd); err != nil {
52
+		c.Fatalf(out, err)
53
+	}
54
+	checkContains(stringCheckPS, out, c)
55
+
56
+	runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=host", "busybox", "ps")
57
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
58
+		c.Fatalf(out, err)
59
+	}
60
+	checkContains(runconfig.ErrConflictNetworkHostname.Error(), out, c)
61
+
62
+	runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=container:other", "busybox", "ps")
63
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
64
+		c.Fatalf(out, err, c)
65
+	}
66
+	checkContains(runconfig.ErrConflictNetworkHostname.Error(), out, c)
67
+
68
+	runCmd = exec.Command(dockerBinary, "run", "--net=container", "busybox", "ps")
69
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
70
+		c.Fatalf(out, err, c)
71
+	}
72
+	checkContains("--net: invalid net mode: invalid container format container:<name|id>", out, c)
73
+
74
+	runCmd = exec.Command(dockerBinary, "run", "--net=weird", "busybox", "ps")
75
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
76
+		c.Fatalf(out, err)
77
+	}
78
+	checkContains("invalid --net: weird", out, c)
79
+}
80
+
81
+func (s *DockerSuite) TestConflictContainerNetworkAndLinks(c *check.C) {
82
+	var (
83
+		out    string
84
+		err    error
85
+		runCmd *exec.Cmd
86
+	)
87
+
88
+	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
89
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
90
+		c.Fatalf(out, err)
91
+	}
92
+	checkContains(runconfig.ErrConflictContainerNetworkAndLinks.Error(), out, c)
93
+
94
+	runCmd = exec.Command(dockerBinary, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
95
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
96
+		c.Fatalf(out, err)
97
+	}
98
+	checkContains(runconfig.ErrConflictHostNetworkAndLinks.Error(), out, c)
99
+}
100
+
101
+func (s *DockerSuite) TestConflictNetworkModeAndOptions(c *check.C) {
102
+	var (
103
+		out    string
104
+		err    error
105
+		runCmd *exec.Cmd
106
+	)
107
+
108
+	runCmd = exec.Command(dockerBinary, "run", "--net=host", "--dns=8.8.8.8", "busybox", "ps")
109
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
110
+		c.Fatalf(out, err)
111
+	}
112
+	checkContains(runconfig.ErrConflictNetworkAndDNS.Error(), out, c)
113
+
114
+	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
115
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
116
+		c.Fatalf(out, err)
117
+	}
118
+	checkContains(runconfig.ErrConflictNetworkAndDNS.Error(), out, c)
119
+
120
+	runCmd = exec.Command(dockerBinary, "run", "--net=host", "--add-host=name:8.8.8.8", "busybox", "ps")
121
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
122
+		c.Fatalf(out, err)
123
+	}
124
+	checkContains(runconfig.ErrConflictNetworkHosts.Error(), out, c)
125
+
126
+	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps")
127
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
128
+		c.Fatalf(out, err)
129
+	}
130
+	checkContains(runconfig.ErrConflictNetworkHosts.Error(), out, c)
131
+
132
+	runCmd = exec.Command(dockerBinary, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
133
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
134
+		c.Fatalf(out, err)
135
+	}
136
+	checkContains(runconfig.ErrConflictContainerNetworkAndMac.Error(), out, c)
137
+
138
+	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
139
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
140
+		c.Fatalf(out, err)
141
+	}
142
+	checkContains(runconfig.ErrConflictContainerNetworkAndMac.Error(), out, c)
143
+
144
+	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "-P", "busybox", "ps")
145
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
146
+		c.Fatalf(out, err)
147
+	}
148
+	checkContains(runconfig.ErrConflictNetworkPublishPorts.Error(), out, c)
149
+
150
+	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "-p", "8080", "busybox", "ps")
151
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
152
+		c.Fatalf(out, err)
153
+	}
154
+	checkContains(runconfig.ErrConflictNetworkPublishPorts.Error(), out, c)
155
+
156
+	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps")
157
+	if out, _, err = runCommandWithOutput(runCmd); err == nil {
158
+		c.Fatalf(out, err)
159
+	}
160
+	checkContains(runconfig.ErrConflictNetworkExposePorts.Error(), out, c)
161
+}
... ...
@@ -158,44 +158,6 @@ type Config struct {
158 158
 	Labels          map[string]string     // List of labels set to this container
159 159
 }
160 160
 
161
-// ContainerConfigWrapper is a Config wrapper that hold the container Config (portable)
162
-// and the corresponding HostConfig (non-portable).
163
-type ContainerConfigWrapper struct {
164
-	*Config
165
-	InnerHostConfig *HostConfig `json:"HostConfig,omitempty"`
166
-	Cpuset          string      `json:",omitempty"` // Deprecated. Exported for backwards compatibility.
167
-	*HostConfig                 // Deprecated. Exported to read attrubutes from json that are not in the inner host config structure.
168
-
169
-}
170
-
171
-// GetHostConfig gets the HostConfig of the Config.
172
-// It's mostly there to handle Deprecated fields of the ContainerConfigWrapper
173
-func (w *ContainerConfigWrapper) GetHostConfig() *HostConfig {
174
-	hc := w.HostConfig
175
-
176
-	if hc == nil && w.InnerHostConfig != nil {
177
-		hc = w.InnerHostConfig
178
-	} else if w.InnerHostConfig != nil {
179
-		if hc.Memory != 0 && w.InnerHostConfig.Memory == 0 {
180
-			w.InnerHostConfig.Memory = hc.Memory
181
-		}
182
-		if hc.MemorySwap != 0 && w.InnerHostConfig.MemorySwap == 0 {
183
-			w.InnerHostConfig.MemorySwap = hc.MemorySwap
184
-		}
185
-		if hc.CPUShares != 0 && w.InnerHostConfig.CPUShares == 0 {
186
-			w.InnerHostConfig.CPUShares = hc.CPUShares
187
-		}
188
-
189
-		hc = w.InnerHostConfig
190
-	}
191
-
192
-	if hc != nil && w.Cpuset != "" && hc.CpusetCpus == "" {
193
-		hc.CpusetCpus = w.Cpuset
194
-	}
195
-
196
-	return hc
197
-}
198
-
199 161
 // DecodeContainerConfig decodes a json encoded config into a ContainerConfigWrapper
200 162
 // struct and returns both a Config and an HostConfig struct
201 163
 // Be aware this function is not checking whether the resulted structs are nil,
... ...
@@ -208,5 +170,13 @@ func DecodeContainerConfig(src io.Reader) (*Config, *HostConfig, error) {
208 208
 		return nil, nil, err
209 209
 	}
210 210
 
211
-	return w.Config, w.GetHostConfig(), nil
211
+	hc := w.getHostConfig()
212
+
213
+	// Certain parameters need daemon-side validation that cannot be done
214
+	// on the client, as only the daemon knows what is valid for the platform.
215
+	if err := ValidateNetMode(w.Config, hc); err != nil {
216
+		return nil, nil, err
217
+	}
218
+
219
+	return w.Config, hc, nil
212 220
 }
213 221
new file mode 100644
... ...
@@ -0,0 +1,47 @@
0
+// +build !windows
1
+
2
+package runconfig
3
+
4
+// ContainerConfigWrapper is a Config wrapper that hold the container Config (portable)
5
+// and the corresponding HostConfig (non-portable).
6
+type ContainerConfigWrapper struct {
7
+	*Config
8
+	InnerHostConfig *HostConfig `json:"HostConfig,omitempty"`
9
+	Cpuset          string      `json:",omitempty"` // Deprecated. Exported for backwards compatibility.
10
+	*HostConfig                 // Deprecated. Exported to read attributes from json that are not in the inner host config structure.
11
+}
12
+
13
+// getHostConfig gets the HostConfig of the Config.
14
+// It's mostly there to handle Deprecated fields of the ContainerConfigWrapper
15
+func (w *ContainerConfigWrapper) getHostConfig() *HostConfig {
16
+	hc := w.HostConfig
17
+
18
+	if hc == nil && w.InnerHostConfig != nil {
19
+		hc = w.InnerHostConfig
20
+	} else if w.InnerHostConfig != nil {
21
+		if hc.Memory != 0 && w.InnerHostConfig.Memory == 0 {
22
+			w.InnerHostConfig.Memory = hc.Memory
23
+		}
24
+		if hc.MemorySwap != 0 && w.InnerHostConfig.MemorySwap == 0 {
25
+			w.InnerHostConfig.MemorySwap = hc.MemorySwap
26
+		}
27
+		if hc.CPUShares != 0 && w.InnerHostConfig.CPUShares == 0 {
28
+			w.InnerHostConfig.CPUShares = hc.CPUShares
29
+		}
30
+		if hc.CpusetCpus != "" && w.InnerHostConfig.CpusetCpus == "" {
31
+			w.InnerHostConfig.CpusetCpus = hc.CpusetCpus
32
+		}
33
+
34
+		hc = w.InnerHostConfig
35
+	}
36
+
37
+	if hc != nil && w.Cpuset != "" && hc.CpusetCpus == "" {
38
+		hc.CpusetCpus = w.Cpuset
39
+	}
40
+
41
+	// Make sure NetworkMode has an acceptable value. We do this to ensure
42
+	// backwards compatible API behaviour.
43
+	hc = SetDefaultNetModeIfBlank(hc)
44
+
45
+	return hc
46
+}
0 47
new file mode 100644
... ...
@@ -0,0 +1,13 @@
0
+package runconfig
1
+
2
+// ContainerConfigWrapper is a Config wrapper that hold the container Config (portable)
3
+// and the corresponding HostConfig (non-portable).
4
+type ContainerConfigWrapper struct {
5
+	*Config
6
+	HostConfig *HostConfig `json:"HostConfig,omitempty"`
7
+}
8
+
9
+// getHostConfig gets the HostConfig of the Config.
10
+func (w *ContainerConfigWrapper) getHostConfig() *HostConfig {
11
+	return w.HostConfig
12
+}
... ...
@@ -298,16 +298,6 @@ type HostConfig struct {
298 298
 	ConsoleSize      [2]int           // Initial console size on Windows
299 299
 }
300 300
 
301
-// MergeConfigs merges the specified container Config and HostConfig.
302
-// It creates a ContainerConfigWrapper.
303
-func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrapper {
304
-	return &ContainerConfigWrapper{
305
-		config,
306
-		hostConfig,
307
-		"", nil,
308
-	}
309
-}
310
-
311 301
 // DecodeHostConfig creates a HostConfig based on the specified Reader.
312 302
 // It assumes the content of the reader will be JSON, and decodes it.
313 303
 func DecodeHostConfig(src io.Reader) (*HostConfig, error) {
... ...
@@ -318,7 +308,19 @@ func DecodeHostConfig(src io.Reader) (*HostConfig, error) {
318 318
 		return nil, err
319 319
 	}
320 320
 
321
-	hc := w.GetHostConfig()
322
-
321
+	hc := w.getHostConfig()
323 322
 	return hc, nil
324 323
 }
324
+
325
+// SetDefaultNetModeIfBlank changes the NetworkMode in a HostConfig structure
326
+// to default if it is not populated. This ensures backwards compatibility after
327
+// the validation of the network mode was moved from the docker CLI to the
328
+// docker daemon.
329
+func SetDefaultNetModeIfBlank(hc *HostConfig) *HostConfig {
330
+	if hc != nil {
331
+		if hc.NetworkMode == NetworkMode("") {
332
+			hc.NetworkMode = NetworkMode("default")
333
+		}
334
+	}
335
+	return hc
336
+}
... ...
@@ -1,3 +1,5 @@
1
+// +build !windows
2
+
1 3
 package runconfig
2 4
 
3 5
 import (
... ...
@@ -8,6 +10,7 @@ import (
8 8
 	"testing"
9 9
 )
10 10
 
11
+// TODO Windows: This will need addressing for a Windows daemon.
11 12
 func TestNetworkModeTest(t *testing.T) {
12 13
 	networkModes := map[NetworkMode][]bool{
13 14
 		// private, bridge, host, container, none, default
... ...
@@ -58,3 +58,13 @@ func (n NetworkMode) IsContainer() bool {
58 58
 func (n NetworkMode) IsNone() bool {
59 59
 	return n == "none"
60 60
 }
61
+
62
+// MergeConfigs merges the specified container Config and HostConfig.
63
+// It creates a ContainerConfigWrapper.
64
+func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrapper {
65
+	return &ContainerConfigWrapper{
66
+		config,
67
+		hostConfig,
68
+		"", nil,
69
+	}
70
+}
... ...
@@ -18,3 +18,12 @@ func (n NetworkMode) NetworkName() string {
18 18
 	}
19 19
 	return ""
20 20
 }
21
+
22
+// MergeConfigs merges the specified container Config and HostConfig.
23
+// It creates a ContainerConfigWrapper.
24
+func MergeConfigs(config *Config, hostConfig *HostConfig) *ContainerConfigWrapper {
25
+	return &ContainerConfigWrapper{
26
+		config,
27
+		hostConfig,
28
+	}
29
+}
... ...
@@ -31,20 +31,6 @@ var (
31 31
 	ErrConflictNetworkExposePorts = fmt.Errorf("Conflicting options: --expose and the network mode (--expose)")
32 32
 )
33 33
 
34
-// validateNM is the set of fields passed to validateNetMode()
35
-type validateNM struct {
36
-	netMode        NetworkMode
37
-	flHostname     *string
38
-	flLinks        opts.ListOpts
39
-	flDNS          opts.ListOpts
40
-	flExtraHosts   opts.ListOpts
41
-	flMacAddress   *string
42
-	flPublish      opts.ListOpts
43
-	flPublishAll   *bool
44
-	flExpose       opts.ListOpts
45
-	flVolumeDriver string
46
-}
47
-
48 34
 // Parse parses the specified args for the specified command and generates a Config,
49 35
 // a HostConfig and returns them with the specified command.
50 36
 // If the specified args are not valid, it will return an error.
... ...
@@ -143,27 +129,6 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
143 143
 		attachStderr = flAttach.Get("stderr")
144 144
 	)
145 145
 
146
-	netMode, err := parseNetMode(*flNetMode)
147
-	if err != nil {
148
-		return nil, nil, cmd, fmt.Errorf("--net: invalid net mode: %v", err)
149
-	}
150
-
151
-	vals := validateNM{
152
-		netMode:      netMode,
153
-		flHostname:   flHostname,
154
-		flLinks:      flLinks,
155
-		flDNS:        flDNS,
156
-		flExtraHosts: flExtraHosts,
157
-		flMacAddress: flMacAddress,
158
-		flPublish:    flPublish,
159
-		flPublishAll: flPublishAll,
160
-		flExpose:     flExpose,
161
-	}
162
-
163
-	if err := validateNetMode(&vals); err != nil {
164
-		return nil, nil, cmd, err
165
-	}
166
-
167 146
 	// Validate the input mac address
168 147
 	if *flMacAddress != "" {
169 148
 		if _, err := opts.ValidateMACAddress(*flMacAddress); err != nil {
... ...
@@ -371,7 +336,7 @@ func Parse(cmd *flag.FlagSet, args []string) (*Config, *HostConfig, *flag.FlagSe
371 371
 		DNSSearch:        flDNSSearch.GetAll(),
372 372
 		ExtraHosts:       flExtraHosts.GetAll(),
373 373
 		VolumesFrom:      flVolumesFrom.GetAll(),
374
-		NetworkMode:      netMode,
374
+		NetworkMode:      NetworkMode(*flNetMode),
375 375
 		IpcMode:          ipcMode,
376 376
 		PidMode:          pidMode,
377 377
 		UTSMode:          utsMode,
... ...
@@ -204,77 +204,6 @@ func TestParseLxcConfOpt(t *testing.T) {
204 204
 
205 205
 }
206 206
 
207
-func TestNetHostname(t *testing.T) {
208
-	if _, _, _, err := parseRun([]string{"-h=name", "img", "cmd"}); err != nil {
209
-		t.Fatalf("Unexpected error: %s", err)
210
-	}
211
-
212
-	if _, _, _, err := parseRun([]string{"--net=host", "img", "cmd"}); err != nil {
213
-		t.Fatalf("Unexpected error: %s", err)
214
-	}
215
-
216
-	if _, _, _, err := parseRun([]string{"-h=name", "--net=bridge", "img", "cmd"}); err != nil {
217
-		t.Fatalf("Unexpected error: %s", err)
218
-	}
219
-
220
-	if _, _, _, err := parseRun([]string{"-h=name", "--net=none", "img", "cmd"}); err != nil {
221
-		t.Fatalf("Unexpected error: %s", err)
222
-	}
223
-
224
-	if _, _, _, err := parseRun([]string{"-h=name", "--net=host", "img", "cmd"}); err != ErrConflictNetworkHostname {
225
-		t.Fatalf("Expected error ErrConflictNetworkHostname, got: %s", err)
226
-	}
227
-
228
-	if _, _, _, err := parseRun([]string{"-h=name", "--net=container:other", "img", "cmd"}); err != ErrConflictNetworkHostname {
229
-		t.Fatalf("Expected error ErrConflictNetworkHostname, got: %s", err)
230
-	}
231
-	if _, _, _, err := parseRun([]string{"--net=container", "img", "cmd"}); err == nil || err.Error() != "--net: invalid net mode: invalid container format container:<name|id>" {
232
-		t.Fatalf("Expected error with --net=container, got : %v", err)
233
-	}
234
-	if _, _, _, err := parseRun([]string{"--net=weird", "img", "cmd"}); err == nil || err.Error() != "--net: invalid net mode: invalid --net: weird" {
235
-		t.Fatalf("Expected error with --net=weird, got: %s", err)
236
-	}
237
-}
238
-
239
-func TestConflictContainerNetworkAndLinks(t *testing.T) {
240
-	if _, _, _, err := parseRun([]string{"--net=container:other", "--link=zip:zap", "img", "cmd"}); err != ErrConflictContainerNetworkAndLinks {
241
-		t.Fatalf("Expected error ErrConflictContainerNetworkAndLinks, got: %s", err)
242
-	}
243
-	if _, _, _, err := parseRun([]string{"--net=host", "--link=zip:zap", "img", "cmd"}); err != ErrConflictHostNetworkAndLinks {
244
-		t.Fatalf("Expected error ErrConflictHostNetworkAndLinks, got: %s", err)
245
-	}
246
-}
247
-
248
-func TestConflictNetworkModeAndOptions(t *testing.T) {
249
-	if _, _, _, err := parseRun([]string{"--net=host", "--dns=8.8.8.8", "img", "cmd"}); err != ErrConflictNetworkAndDNS {
250
-		t.Fatalf("Expected error ErrConflictNetworkAndDns, got %s", err)
251
-	}
252
-	if _, _, _, err := parseRun([]string{"--net=container:other", "--dns=8.8.8.8", "img", "cmd"}); err != ErrConflictNetworkAndDNS {
253
-		t.Fatalf("Expected error ErrConflictNetworkAndDns, got %s", err)
254
-	}
255
-	if _, _, _, err := parseRun([]string{"--net=host", "--add-host=name:8.8.8.8", "img", "cmd"}); err != ErrConflictNetworkHosts {
256
-		t.Fatalf("Expected error ErrConflictNetworkAndDns, got %s", err)
257
-	}
258
-	if _, _, _, err := parseRun([]string{"--net=container:other", "--add-host=name:8.8.8.8", "img", "cmd"}); err != ErrConflictNetworkHosts {
259
-		t.Fatalf("Expected error ErrConflictNetworkAndDns, got %s", err)
260
-	}
261
-	if _, _, _, err := parseRun([]string{"--net=host", "--mac-address=92:d0:c6:0a:29:33", "img", "cmd"}); err != ErrConflictContainerNetworkAndMac {
262
-		t.Fatalf("Expected error ErrConflictContainerNetworkAndMac, got %s", err)
263
-	}
264
-	if _, _, _, err := parseRun([]string{"--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "img", "cmd"}); err != ErrConflictContainerNetworkAndMac {
265
-		t.Fatalf("Expected error ErrConflictContainerNetworkAndMac, got %s", err)
266
-	}
267
-	if _, _, _, err := parseRun([]string{"--net=container:other", "-P", "img", "cmd"}); err != ErrConflictNetworkPublishPorts {
268
-		t.Fatalf("Expected error ErrConflictNetworkPublishPorts, got %s", err)
269
-	}
270
-	if _, _, _, err := parseRun([]string{"--net=container:other", "-p", "8080", "img", "cmd"}); err != ErrConflictNetworkPublishPorts {
271
-		t.Fatalf("Expected error ErrConflictNetworkPublishPorts, got %s", err)
272
-	}
273
-	if _, _, _, err := parseRun([]string{"--net=container:other", "--expose", "8000-9000", "img", "cmd"}); err != ErrConflictNetworkExposePorts {
274
-		t.Fatalf("Expected error ErrConflictNetworkExposePorts, got %s", err)
275
-	}
276
-}
277
-
278 207
 // Simple parse with MacAddress validatation
279 208
 func TestParseWithMacAddress(t *testing.T) {
280 209
 	invalidMacAddress := "--mac-address=invalidMacAddress"
... ...
@@ -7,51 +7,53 @@ import (
7 7
 	"strings"
8 8
 )
9 9
 
10
-func parseNetMode(netMode string) (NetworkMode, error) {
11
-	parts := strings.Split(netMode, ":")
10
+// ValidateNetMode ensures that the various combinations of requested
11
+// network settings are valid.
12
+func ValidateNetMode(c *Config, hc *HostConfig) error {
13
+	// We may not be passed a host config, such as in the case of docker commit
14
+	if hc == nil {
15
+		return nil
16
+	}
17
+	parts := strings.Split(string(hc.NetworkMode), ":")
12 18
 	switch mode := parts[0]; mode {
13 19
 	case "default", "bridge", "none", "host":
14 20
 	case "container":
15 21
 		if len(parts) < 2 || parts[1] == "" {
16
-			return "", fmt.Errorf("invalid container format container:<name|id>")
22
+			return fmt.Errorf("--net: invalid net mode: invalid container format container:<name|id>")
17 23
 		}
18 24
 	default:
19
-		return "", fmt.Errorf("invalid --net: %s", netMode)
25
+		return fmt.Errorf("invalid --net: %s", hc.NetworkMode)
20 26
 	}
21
-	return NetworkMode(netMode), nil
22
-}
23
-
24
-func validateNetMode(vals *validateNM) error {
25 27
 
26
-	if (vals.netMode.IsHost() || vals.netMode.IsContainer()) && *vals.flHostname != "" {
28
+	if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && c.Hostname != "" {
27 29
 		return ErrConflictNetworkHostname
28 30
 	}
29 31
 
30
-	if vals.netMode.IsHost() && vals.flLinks.Len() > 0 {
32
+	if hc.NetworkMode.IsHost() && len(hc.Links) > 0 {
31 33
 		return ErrConflictHostNetworkAndLinks
32 34
 	}
33 35
 
34
-	if vals.netMode.IsContainer() && vals.flLinks.Len() > 0 {
36
+	if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
35 37
 		return ErrConflictContainerNetworkAndLinks
36 38
 	}
37 39
 
38
-	if (vals.netMode.IsHost() || vals.netMode.IsContainer()) && vals.flDNS.Len() > 0 {
40
+	if (hc.NetworkMode.IsHost() || hc.NetworkMode.IsContainer()) && len(hc.DNS) > 0 {
39 41
 		return ErrConflictNetworkAndDNS
40 42
 	}
41 43
 
42
-	if (vals.netMode.IsContainer() || vals.netMode.IsHost()) && vals.flExtraHosts.Len() > 0 {
44
+	if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && len(hc.ExtraHosts) > 0 {
43 45
 		return ErrConflictNetworkHosts
44 46
 	}
45 47
 
46
-	if (vals.netMode.IsContainer() || vals.netMode.IsHost()) && *vals.flMacAddress != "" {
48
+	if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
47 49
 		return ErrConflictContainerNetworkAndMac
48 50
 	}
49 51
 
50
-	if vals.netMode.IsContainer() && (vals.flPublish.Len() > 0 || *vals.flPublishAll == true) {
52
+	if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
51 53
 		return ErrConflictNetworkPublishPorts
52 54
 	}
53 55
 
54
-	if vals.netMode.IsContainer() && vals.flExpose.Len() > 0 {
56
+	if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
55 57
 		return ErrConflictNetworkExposePorts
56 58
 	}
57 59
 	return nil
... ...
@@ -5,16 +5,18 @@ import (
5 5
 	"strings"
6 6
 )
7 7
 
8
-func parseNetMode(netMode string) (NetworkMode, error) {
9
-	parts := strings.Split(netMode, ":")
8
+// ValidateNetMode ensures that the various combinations of requested
9
+// network settings are valid.
10
+func ValidateNetMode(c *Config, hc *HostConfig) error {
11
+	// We may not be passed a host config, such as in the case of docker commit
12
+	if hc == nil {
13
+		return nil
14
+	}
15
+	parts := strings.Split(string(hc.NetworkMode), ":")
10 16
 	switch mode := parts[0]; mode {
11 17
 	case "default", "none":
12 18
 	default:
13
-		return "", fmt.Errorf("invalid --net: %s", netMode)
19
+		return fmt.Errorf("invalid --net: %s", hc.NetworkMode)
14 20
 	}
15
-	return NetworkMode(netMode), nil
16
-}
17
-
18
-func validateNetMode(vals *validateNM) error {
19 21
 	return nil
20 22
 }