Browse code

Merge pull request #19291 from coolljt0725/fix_19100

Fix #19100 and fix a typo

Tibor Vass authored on 2016/01/15 01:40:29
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 {
... ...
@@ -940,7 +940,7 @@ func (s *DockerNetworkSuite) TestDockerNetworkInspectCreatedContainer(c *check.C
940 940
 	c.Assert(networks, checker.Contains, "bridge", check.Commentf("Should return 'bridge' network"))
941 941
 }
942 942
 
943
-func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMulipleNetworks(c *check.C) {
943
+func (s *DockerNetworkSuite) TestDockerNetworkRestartWithMultipleNetworks(c *check.C) {
944 944
 	dockerCmd(c, "network", "create", "test")
945 945
 	dockerCmd(c, "run", "--name=foo", "-d", "busybox", "top")
946 946
 	c.Assert(waitRun("foo"), checker.IsNil)
... ...
@@ -1097,3 +1097,46 @@ func (s *DockerSuite) TestUserDefinedNetworkConnectDisconnectLink(c *check.C) {
1097 1097
 	_, _, err = dockerCmdWithError("exec", "second", "ping", "-c", "1", "FirstInFoo2")
1098 1098
 	c.Assert(err, check.IsNil)
1099 1099
 }
1100
+
1101
+// #19100 This is a deprecated feature test, it should be remove in Docker 1.12
1102
+func (s *DockerNetworkSuite) TestDockerNetworkStartAPIWithHostconfig(c *check.C) {
1103
+	netName := "test"
1104
+	conName := "foo"
1105
+	dockerCmd(c, "network", "create", netName)
1106
+	dockerCmd(c, "create", "--name", conName, "busybox", "top")
1107
+
1108
+	config := map[string]interface{}{
1109
+		"HostConfig": map[string]interface{}{
1110
+			"NetworkMode": netName,
1111
+		},
1112
+	}
1113
+	_, _, err := sockRequest("POST", "/containers/"+conName+"/start", config)
1114
+	c.Assert(err, checker.IsNil)
1115
+	c.Assert(waitRun(conName), checker.IsNil)
1116
+	networks, err := inspectField(conName, "NetworkSettings.Networks")
1117
+	c.Assert(err, checker.IsNil)
1118
+	c.Assert(networks, checker.Contains, netName, check.Commentf(fmt.Sprintf("Should contain '%s' network", netName)))
1119
+	c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network"))
1120
+}
1121
+
1122
+func (s *DockerNetworkSuite) TestDockerNetworkDisconnectDefault(c *check.C) {
1123
+	netWorkName1 := "test1"
1124
+	netWorkName2 := "test2"
1125
+	containerName := "foo"
1126
+
1127
+	dockerCmd(c, "network", "create", netWorkName1)
1128
+	dockerCmd(c, "network", "create", netWorkName2)
1129
+	dockerCmd(c, "create", "--name", containerName, "busybox", "top")
1130
+	dockerCmd(c, "network", "connect", netWorkName1, containerName)
1131
+	dockerCmd(c, "network", "connect", netWorkName2, containerName)
1132
+	dockerCmd(c, "network", "disconnect", "bridge", containerName)
1133
+
1134
+	dockerCmd(c, "start", containerName)
1135
+	c.Assert(waitRun(containerName), checker.IsNil)
1136
+	networks, err := inspectField(containerName, "NetworkSettings.Networks")
1137
+	c.Assert(err, checker.IsNil)
1138
+	c.Assert(networks, checker.Contains, netWorkName1, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName1)))
1139
+	c.Assert(networks, checker.Contains, netWorkName2, check.Commentf(fmt.Sprintf("Should contain '%s' network", netWorkName2)))
1140
+	c.Assert(networks, checker.Not(checker.Contains), "bridge", check.Commentf("Should not contain 'bridge' network"))
1141
+
1142
+}