Browse code

Fix incorrect `Scope` in `network ls/inspect` with duplicate network names

This fix tries to address the issue raised in 30242 where the `Scope`
field always changed to `swarm` in the ouput of `docker network ls/inspect`
when duplicate networks name exist.

The reason for the issue was that `buildNetworkResource()` use network name
(which may not be unique) to check for the scope.

This fix fixes the issue by always use network ID in `buildNetworkResource()`.

A test has been added. The test fails before the fix and passes after the fix.

This fix fixes 30242.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
(cherry picked from commit 05a831a775be5e8d752deaef620e629deb15cb89)
Signed-off-by: Victor Vieux <vieux@docker.com>

Yong Tang authored on 2017/01/19 00:48:09
Showing 2 changed files
... ...
@@ -163,7 +163,7 @@ func (n *networkRouter) buildNetworkResource(nw libnetwork.Network) *types.Netwo
163 163
 	r.Created = info.Created()
164 164
 	r.Scope = info.Scope()
165 165
 	if n.clusterProvider.IsManager() {
166
-		if _, err := n.clusterProvider.GetNetwork(nw.Name()); err == nil {
166
+		if _, err := n.clusterProvider.GetNetwork(nw.ID()); err == nil {
167 167
 			r.Scope = "swarm"
168 168
 		}
169 169
 	} else if info.Dynamic() {
... ...
@@ -3,6 +3,7 @@
3 3
 package main
4 4
 
5 5
 import (
6
+	"encoding/json"
6 7
 	"fmt"
7 8
 	"net/http"
8 9
 	"os"
... ...
@@ -13,6 +14,7 @@ import (
13 13
 	"syscall"
14 14
 	"time"
15 15
 
16
+	"github.com/docker/docker/api/types"
16 17
 	"github.com/docker/docker/api/types/swarm"
17 18
 	"github.com/docker/docker/pkg/integration/checker"
18 19
 	"github.com/go-check/check"
... ...
@@ -1310,3 +1312,56 @@ func (s *DockerSwarmSuite) TestAPISwarmSecretsDelete(c *check.C) {
1310 1310
 	c.Assert(err, checker.IsNil)
1311 1311
 	c.Assert(status, checker.Equals, http.StatusNotFound, check.Commentf("secret delete: %s", string(out)))
1312 1312
 }
1313
+
1314
+// Test case for 30242, where duplicate networks, with different drivers `bridge` and `overlay`,
1315
+// caused both scopes to be `swarm` for `docker network inspect` and `docker network ls`.
1316
+// This test makes sure the fixes correctly output scopes instead.
1317
+func (s *DockerSwarmSuite) TestAPIDuplicateNetworks(c *check.C) {
1318
+	d := s.AddDaemon(c, true, true)
1319
+
1320
+	name := "foo"
1321
+	networkCreateRequest := types.NetworkCreateRequest{
1322
+		Name: name,
1323
+		NetworkCreate: types.NetworkCreate{
1324
+			CheckDuplicate: false,
1325
+		},
1326
+	}
1327
+
1328
+	var n1 types.NetworkCreateResponse
1329
+	networkCreateRequest.NetworkCreate.Driver = "bridge"
1330
+
1331
+	status, out, err := d.SockRequest("POST", "/networks/create", networkCreateRequest)
1332
+	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
1333
+	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(out)))
1334
+
1335
+	c.Assert(json.Unmarshal(out, &n1), checker.IsNil)
1336
+
1337
+	var n2 types.NetworkCreateResponse
1338
+	networkCreateRequest.NetworkCreate.Driver = "overlay"
1339
+
1340
+	status, out, err = d.SockRequest("POST", "/networks/create", networkCreateRequest)
1341
+	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
1342
+	c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(out)))
1343
+
1344
+	c.Assert(json.Unmarshal(out, &n2), checker.IsNil)
1345
+
1346
+	var r1 types.NetworkResource
1347
+
1348
+	status, out, err = d.SockRequest("GET", "/networks/"+n1.ID, nil)
1349
+	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
1350
+	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(out)))
1351
+
1352
+	c.Assert(json.Unmarshal(out, &r1), checker.IsNil)
1353
+
1354
+	c.Assert(r1.Scope, checker.Equals, "local")
1355
+
1356
+	var r2 types.NetworkResource
1357
+
1358
+	status, out, err = d.SockRequest("GET", "/networks/"+n2.ID, nil)
1359
+	c.Assert(err, checker.IsNil, check.Commentf(string(out)))
1360
+	c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(out)))
1361
+
1362
+	c.Assert(json.Unmarshal(out, &r2), checker.IsNil)
1363
+
1364
+	c.Assert(r2.Scope, checker.Equals, "swarm")
1365
+}