From a64c9357ca4500af96bf3f02a1d2cd8ade5761f2 Mon Sep 17 00:00:00 2001 From: Yong Tang Date: Wed, 18 Jan 2017 07:48:09 -0800 Subject: [PATCH] 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 (cherry picked from commit 05a831a775be5e8d752deaef620e629deb15cb89) Signed-off-by: Victor Vieux --- api/server/router/network/network_routes.go | 2 +- integration-cli/docker_api_swarm_test.go | 55 +++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/api/server/router/network/network_routes.go b/api/server/router/network/network_routes.go index 81b4d7da86..7bfc499552 100644 --- a/api/server/router/network/network_routes.go +++ b/api/server/router/network/network_routes.go @@ -163,7 +163,7 @@ func (n *networkRouter) buildNetworkResource(nw libnetwork.Network) *types.Netwo r.Created = info.Created() r.Scope = info.Scope() if n.clusterProvider.IsManager() { - if _, err := n.clusterProvider.GetNetwork(nw.Name()); err == nil { + if _, err := n.clusterProvider.GetNetwork(nw.ID()); err == nil { r.Scope = "swarm" } } else if info.Dynamic() { diff --git a/integration-cli/docker_api_swarm_test.go b/integration-cli/docker_api_swarm_test.go index 39bf721211..1f8eaec6de 100644 --- a/integration-cli/docker_api_swarm_test.go +++ b/integration-cli/docker_api_swarm_test.go @@ -3,6 +3,7 @@ package main import ( + "encoding/json" "fmt" "net/http" "os" @@ -13,6 +14,7 @@ import ( "syscall" "time" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/pkg/integration/checker" "github.com/go-check/check" @@ -1310,3 +1312,56 @@ func (s *DockerSwarmSuite) TestAPISwarmSecretsDelete(c *check.C) { c.Assert(err, checker.IsNil) c.Assert(status, checker.Equals, http.StatusNotFound, check.Commentf("secret delete: %s", string(out))) } + +// Test case for 30242, where duplicate networks, with different drivers `bridge` and `overlay`, +// caused both scopes to be `swarm` for `docker network inspect` and `docker network ls`. +// This test makes sure the fixes correctly output scopes instead. +func (s *DockerSwarmSuite) TestAPIDuplicateNetworks(c *check.C) { + d := s.AddDaemon(c, true, true) + + name := "foo" + networkCreateRequest := types.NetworkCreateRequest{ + Name: name, + NetworkCreate: types.NetworkCreate{ + CheckDuplicate: false, + }, + } + + var n1 types.NetworkCreateResponse + networkCreateRequest.NetworkCreate.Driver = "bridge" + + status, out, err := d.SockRequest("POST", "/networks/create", networkCreateRequest) + c.Assert(err, checker.IsNil, check.Commentf(string(out))) + c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(out))) + + c.Assert(json.Unmarshal(out, &n1), checker.IsNil) + + var n2 types.NetworkCreateResponse + networkCreateRequest.NetworkCreate.Driver = "overlay" + + status, out, err = d.SockRequest("POST", "/networks/create", networkCreateRequest) + c.Assert(err, checker.IsNil, check.Commentf(string(out))) + c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(out))) + + c.Assert(json.Unmarshal(out, &n2), checker.IsNil) + + var r1 types.NetworkResource + + status, out, err = d.SockRequest("GET", "/networks/"+n1.ID, nil) + c.Assert(err, checker.IsNil, check.Commentf(string(out))) + c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(out))) + + c.Assert(json.Unmarshal(out, &r1), checker.IsNil) + + c.Assert(r1.Scope, checker.Equals, "local") + + var r2 types.NetworkResource + + status, out, err = d.SockRequest("GET", "/networks/"+n2.ID, nil) + c.Assert(err, checker.IsNil, check.Commentf(string(out))) + c.Assert(status, checker.Equals, http.StatusOK, check.Commentf(string(out))) + + c.Assert(json.Unmarshal(out, &r2), checker.IsNil) + + c.Assert(r2.Scope, checker.Equals, "swarm") +}