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