Include container names in `network inspect`

This commit makes `docker network inspect` print container names as
service discovery is based on container name.

Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
This commit is contained in:
Zhang Wei 2015-11-03 00:29:58 +08:00
parent f267e4be54
commit fbed26297d
3 changed files with 43 additions and 0 deletions

View File

@ -237,6 +237,7 @@ func buildEndpointResource(e libnetwork.Endpoint) types.EndpointResource {
}
er.EndpointID = e.ID()
er.Name = e.Name()
ei := e.Info()
if ei == nil {
return er

View File

@ -361,6 +361,7 @@ type NetworkResource struct {
// EndpointResource contains network resources allocated and used for a container in a network
type EndpointResource struct {
Name string
EndpointID string
MacAddress string
IPv4Address string

View File

@ -293,6 +293,47 @@ func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
c.Assert(out, checker.Contains, "Error: No such network: nonexistent")
}
func (s *DockerSuite) TestDockerInspectNetworkWithContainerName(c *check.C) {
dockerCmd(c, "network", "create", "brNetForInspect")
assertNwIsAvailable(c, "brNetForInspect")
defer func() {
dockerCmd(c, "network", "rm", "brNetForInspect")
assertNwNotAvailable(c, "brNetForInspect")
}()
out, _ := dockerCmd(c, "run", "-d", "--name", "testNetInspect1", "--net", "brNetForInspect", "busybox", "top")
c.Assert(waitRun("testNetInspect1"), check.IsNil)
containerID := strings.TrimSpace(out)
defer func() {
// we don't stop container by name, because we'll rename it later
dockerCmd(c, "stop", containerID)
}()
out, _ = dockerCmd(c, "network", "inspect", "brNetForInspect")
networkResources := []types.NetworkResource{}
err := json.Unmarshal([]byte(out), &networkResources)
c.Assert(err, check.IsNil)
c.Assert(networkResources, checker.HasLen, 1)
container, ok := networkResources[0].Containers[containerID]
c.Assert(ok, checker.True)
c.Assert(container.Name, checker.Equals, "testNetInspect1")
// rename container and check docker inspect output update
newName := "HappyNewName"
dockerCmd(c, "rename", "testNetInspect1", newName)
// check whether network inspect works properly
out, _ = dockerCmd(c, "network", "inspect", "brNetForInspect")
newNetRes := []types.NetworkResource{}
err = json.Unmarshal([]byte(out), &newNetRes)
c.Assert(err, check.IsNil)
c.Assert(newNetRes, checker.HasLen, 1)
container1, ok := newNetRes[0].Containers[containerID]
c.Assert(ok, checker.True)
c.Assert(container1.Name, checker.Equals, newName)
}
func (s *DockerNetworkSuite) TestDockerNetworkConnectDisconnect(c *check.C) {
dockerCmd(c, "network", "create", "test")
assertNwIsAvailable(c, "test")