Browse code

Fix #19100 and fix a typo

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2016/01/14 15:58:54
Showing 2 changed files
... ...
@@ -31,12 +31,22 @@ func (daemon *Daemon) ContainerStart(name string, hostConfig *containertypes.Hos
31 31
 		// creating a container, not during start.
32 32
 		if hostConfig != nil {
33 33
 			logrus.Warn("DEPRECATED: Setting host configuration options when the container starts is deprecated and will be removed in Docker 1.12")
34
+			oldNetworkMode := container.HostConfig.NetworkMode
34 35
 			if err := daemon.setSecurityOptions(container, hostConfig); err != nil {
35 36
 				return err
36 37
 			}
37 38
 			if err := daemon.setHostConfig(container, hostConfig); err != nil {
38 39
 				return err
39 40
 			}
41
+			newNetworkMode := container.HostConfig.NetworkMode
42
+			if string(oldNetworkMode) != string(newNetworkMode) {
43
+				// if user has change the network mode on starting, clean up the
44
+				// old networks. It is a deprecated feature and will be removed in Docker 1.12
45
+				container.NetworkSettings.Networks = nil
46
+				if err := container.ToDisk(); err != nil {
47
+					return err
48
+				}
49
+			}
40 50
 			container.InitDNSHostConfig()
41 51
 		}
42 52
 	} else {
... ...
@@ -924,7 +924,7 @@ func (s *DockerNetworkSuite) TestDockerNetworkInspectCreatedContainer(c *check.C
924 924
 	c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should return 'bridge' network"))
925 925
 }
926 926
 
927
-func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMulipleNetworks(c *check.C) {
927
+func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMultipleNetworks(c *check.C) {
928 928
 	dockerCmd(c, "network", "create", "test")
929 929
 	dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top")
930 930
 	c.Assert(waitRun("foo"), checker.IsNil)
... ...
@@ -1081,3 +1081,46 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectLink(c *check.C) {
1081 1081
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2")
1082 1082
 	c.Assert(err, check.IsNil)
1083 1083
 }
1084
+
1085
+// #19100 This is a deprecated feature test, it should be remove in Docker 1.12
1086
+func (s *DockerNetworkSuite) TestDockerNetworkStartAPIWithHostconfig(c *check.C) {
1087
+	netName := "test"
1088
+	conName := "foo"
1089
+	dockerCmd(c, "network", "create", netName)
1090
+	dockerCmd(c, "create", "--name", conName, "busybox", "top")
1091
+
1092
+	config := map[string]interface{}{
1093
+		"HostConfig": map[string]interface{}{
1094
+			"NetworkMode": netName,
1095
+		},
1096
+	}
1097
+	_, _, err := sockRequest("POST", "/containers/"+conName+"/start", config)
1098
+	c.Assert(err, checker.IsNil)
1099
+	c.Assert(waitRun(conName), checker.IsNil)
1100
+	networks, err := inspectField(conName, "NetworkSettings.Networks")
1101
+	c.Assert(err, checker.IsNil)
1102
+	c.Assert(networks, checker.Contains, netName, check.Commentf(fmt.Sprintf("Should contain '%s' network", netName)))
1103
+	c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network"))
1104
+}
1105
+
1106
+func (s *DockerNetworkSuite) TestDockerNetworkDisconnectDefault(c *check.C) {
1107
+	netWorkName1 := "test1"
1108
+	netWorkName2 := "test2"
1109
+	containerName := "foo"
1110
+
1111
+	dockerCmd(c, "network", "create", netWorkName1)
1112
+	dockerCmd(c, "network", "create", netWorkName2)
1113
+	dockerCmd(c, "create", "--name", containerName, "busybox", "top")
1114
+	dockerCmd(c, "network", "connect", netWorkName1, containerName)
1115
+	dockerCmd(c, "network", "connect", netWorkName2, containerName)
1116
+	dockerCmd(c, "network", "disconnect", "bridge", containerName)
1117
+
1118
+	dockerCmd(c, "start", containerName)
1119
+	c.Assert(waitRun(containerName), checker.IsNil)
1120
+	networks, err := inspectField(containerName, "NetworkSettings.Networks")
1121
+	c.Assert(err, checker.IsNil)
1122
+	c.Assert(networks, checker.Contains, netWorkName1, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName1)))
1123
+	c.Assert(networks, checker.Contains, netWorkName2, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName2)))
1124
+	c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network"))
1125
+
1126
+}