Browse code

Store endpoint config on network connect to a stopped container

Signed-off-by: Alessandro Boch <aboch@docker.com>

Alessandro Boch authored on 2016/02/03 10:25:28
Showing 2 changed files
... ...
@@ -743,6 +743,9 @@ func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName
743 743
 		if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil {
744 744
 			return err
745 745
 		}
746
+		if endpointConfig != nil {
747
+			container.NetworkSettings.Networks[idOrName] = endpointConfig
748
+		}
746 749
 	} else {
747 750
 		if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
748 751
 			return err
... ...
@@ -1126,18 +1126,22 @@ func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIP(c *check.C) {
1126 1126
 	// run a container on first network specifying the ip addresses
1127 1127
 	dockerCmd(c, "run", "-d", "--name", "c0", "--net=n0", "--ip", "172.28.99.88", "--ip6", "2001:db8:1234::9988", "busybox", "top")
1128 1128
 	c.Assert(waitRun("c0"), check.IsNil)
1129
+	verifyIPAddressConfig(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
1129 1130
 	verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
1130 1131
 
1131 1132
 	// connect the container to the second network specifying an ip addresses
1132 1133
 	dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n1", "c0")
1134
+	verifyIPAddressConfig(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
1133 1135
 	verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
1134 1136
 
1135 1137
 	// Stop and restart the container
1136 1138
 	dockerCmd(c, "stop", "c0")
1137 1139
 	dockerCmd(c, "start", "c0")
1138 1140
 
1139
-	// verify requested addresses are applied
1141
+	// verify requested addresses are applied and configs are still there
1142
+	verifyIPAddressConfig(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
1140 1143
 	verifyIPAddresses(c, "c0", "n0", "172.28.99.88", "2001:db8:1234::9988")
1144
+	verifyIPAddressConfig(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
1141 1145
 	verifyIPAddresses(c, "c0", "n1", "172.30.55.44", "2001:db8:abcd::5544")
1142 1146
 
1143 1147
 	// Still it should fail to connect to the default network with a specified IP (whatever ip)
... ...
@@ -1147,6 +1151,29 @@ func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIP(c *check.C) {
1147 1147
 
1148 1148
 }
1149 1149
 
1150
+func (s *DockerNetworkSuite) TestDockerNetworkConnectPreferredIPStoppedContainer(c *check.C) {
1151
+	// create a container
1152
+	dockerCmd(c, "create", "--name", "c0", "busybox", "top")
1153
+
1154
+	// create a network
1155
+	dockerCmd(c, "network", "create", "--subnet=172.30.0.0/16", "--subnet=2001:db8:abcd::/64", "n0")
1156
+	assertNwIsAvailable(c, "n0")
1157
+
1158
+	// connect the container to the network specifying an ip addresses
1159
+	dockerCmd(c, "network", "connect", "--ip", "172.30.55.44", "--ip6", "2001:db8:abcd::5544", "n0", "c0")
1160
+	verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
1161
+
1162
+	// start the container, verify config has not changed and ip addresses are assigned
1163
+	dockerCmd(c, "start", "c0")
1164
+	c.Assert(waitRun("c0"), check.IsNil)
1165
+	verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
1166
+	verifyIPAddresses(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
1167
+
1168
+	// stop the container and check ip config has not changed
1169
+	dockerCmd(c, "stop", "c0")
1170
+	verifyIPAddressConfig(c, "c0", "n0", "172.30.55.44", "2001:db8:abcd::5544")
1171
+}
1172
+
1150 1173
 func (s *DockerNetworkSuite) TestDockerNetworkUnsupportedRequiredIP(c *check.C) {
1151 1174
 	// requested IP is not supported on predefined networks
1152 1175
 	for _, mode := range []string{"none", "host", "bridge", "default"} {
... ...
@@ -1175,6 +1202,18 @@ func checkUnsupportedNetworkAndIP(c *check.C, nwMode string) {
1175 1175
 	c.Assert(out, checker.Contains, runconfig.ErrUnsupportedNetworkAndIP.Error())
1176 1176
 }
1177 1177
 
1178
+func verifyIPAddressConfig(c *check.C, cName, nwname, ipv4, ipv6 string) {
1179
+	if ipv4 != "" {
1180
+		out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAMConfig.IPv4Address", nwname))
1181
+		c.Assert(strings.TrimSpace(out), check.Equals, ipv4)
1182
+	}
1183
+
1184
+	if ipv6 != "" {
1185
+		out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAMConfig.IPv6Address", nwname))
1186
+		c.Assert(strings.TrimSpace(out), check.Equals, ipv6)
1187
+	}
1188
+}
1189
+
1178 1190
 func verifyIPAddresses(c *check.C, cName, nwname, ipv4, ipv6 string) {
1179 1191
 	out := inspectField(c, cName, fmt.Sprintf("NetworkSettings.Networks.%s.IPAddress", nwname))
1180 1192
 	c.Assert(strings.TrimSpace(out), check.Equals, ipv4)