mirror of
https://github.com/moby/moby.git
synced 2025-07-29 07:21:35 +03:00
Return error in case docker network inspect
is ambiguous
This fix is partially based on comment https://github.com/docker/docker/issues/30242#issuecomment-273517205 Currently, `docker network inspect` relies on `FindNetwork()` which does not take into consideration that multiple networks with the same name might exist. This fix propose to return `docker network inspect` in a similiar fashion like other commands: 1. Lookup full ID 2. Lookup full name 3. Lookup partial ID If multiple networks exist, an error will be returned. NOTE: this fix is not a complete fix for the issue raised in https://github.com/docker/docker/issues/30242#issuecomment-273517205 where SwarmKit is unable to update when multiple networks with the same name exit. To fix that issue requires multiple places when `FindNetwork()` is called. Because of the impact of changing `FindNetwork()`, this fix focus on the issue in `docker network inspect`. A separate PR will be created to address https://github.com/docker/docker/issues/30242#issuecomment-273517205 An integration test has been added. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
@ -14,6 +14,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/swarm"
|
||||
"github.com/docker/docker/integration-cli/checker"
|
||||
"github.com/docker/docker/integration-cli/daemon"
|
||||
@ -1665,3 +1666,77 @@ func (s *DockerSwarmSuite) TestSwarmReadonlyRootfs(c *check.C) {
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, "true")
|
||||
}
|
||||
|
||||
func (s *DockerSwarmSuite) TestNetworkInspectWithDuplicateNames(c *check.C) {
|
||||
d := s.AddDaemon(c, true, true)
|
||||
|
||||
name := "foo"
|
||||
networkCreateRequest := types.NetworkCreateRequest{
|
||||
Name: name,
|
||||
NetworkCreate: types.NetworkCreate{
|
||||
CheckDuplicate: false,
|
||||
Driver: "bridge",
|
||||
},
|
||||
}
|
||||
|
||||
var n1 types.NetworkCreateResponse
|
||||
status, body, err := d.SockRequest("POST", "/networks/create", networkCreateRequest)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(string(body)))
|
||||
c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(body)))
|
||||
c.Assert(json.Unmarshal(body, &n1), checker.IsNil)
|
||||
|
||||
// Full ID always works
|
||||
out, err := d.Cmd("network", "inspect", "--format", "{{.ID}}", n1.ID)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, n1.ID)
|
||||
|
||||
// Name works if it is unique
|
||||
out, err = d.Cmd("network", "inspect", "--format", "{{.ID}}", name)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, n1.ID)
|
||||
|
||||
var n2 types.NetworkCreateResponse
|
||||
status, body, err = d.SockRequest("POST", "/networks/create", networkCreateRequest)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(string(body)))
|
||||
c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(body)))
|
||||
c.Assert(json.Unmarshal(body, &n2), checker.IsNil)
|
||||
|
||||
// Full ID always works
|
||||
out, err = d.Cmd("network", "inspect", "--format", "{{.ID}}", n1.ID)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, n1.ID)
|
||||
|
||||
out, err = d.Cmd("network", "inspect", "--format", "{{.ID}}", n2.ID)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, n2.ID)
|
||||
|
||||
// Name with duplicates
|
||||
out, err = d.Cmd("network", "inspect", "--format", "{{.ID}}", name)
|
||||
c.Assert(err, checker.NotNil, check.Commentf(out))
|
||||
c.Assert(out, checker.Contains, "network foo is ambiguous (2 matches found based on name)")
|
||||
|
||||
out, err = d.Cmd("network", "rm", n2.ID)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
|
||||
// Dupliates with name but with different driver
|
||||
networkCreateRequest.NetworkCreate.Driver = "overlay"
|
||||
|
||||
status, body, err = d.SockRequest("POST", "/networks/create", networkCreateRequest)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(string(body)))
|
||||
c.Assert(status, checker.Equals, http.StatusCreated, check.Commentf(string(body)))
|
||||
c.Assert(json.Unmarshal(body, &n2), checker.IsNil)
|
||||
|
||||
// Full ID always works
|
||||
out, err = d.Cmd("network", "inspect", "--format", "{{.ID}}", n1.ID)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, n1.ID)
|
||||
|
||||
out, err = d.Cmd("network", "inspect", "--format", "{{.ID}}", n2.ID)
|
||||
c.Assert(err, checker.IsNil, check.Commentf(out))
|
||||
c.Assert(strings.TrimSpace(out), checker.Equals, n2.ID)
|
||||
|
||||
// Name with duplicates
|
||||
out, err = d.Cmd("network", "inspect", "--format", "{{.ID}}", name)
|
||||
c.Assert(err, checker.NotNil, check.Commentf(out))
|
||||
c.Assert(out, checker.Contains, "network foo is ambiguous (2 matches found based on name)")
|
||||
}
|
||||
|
Reference in New Issue
Block a user