From 4b03d7fa0c604209424c5a9205f2a2f03ead68b2 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Mon, 18 May 2015 12:56:21 -0700 Subject: [PATCH] handle collision Signed-off-by: Victor Vieux --- cluster/swarm/cluster.go | 16 ++++++++++++++-- cluster/swarm/cluster_test.go | 20 +++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index b08f988288..440066ce7b 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -388,6 +388,8 @@ func (c *Cluster) Container(IDOrName string) *cluster.Container { } } + candidates := []*cluster.Container{} + // Match name, /name or engine/name. for _, container := range containers { for _, name := range container.Names { @@ -397,20 +399,30 @@ func (c *Cluster) Container(IDOrName string) *cluster.Container { } } + if size := len(candidates); size == 1 { + return candidates[0] + } else if size > 1 { + return nil + } + // Match Container ID prefix. for _, container := range containers { if strings.HasPrefix(container.Id, IDOrName) { - return container + candidates = append(candidates, container) } } // Match Swarm ID prefix. for _, container := range containers { if strings.HasPrefix(container.Config.SwarmID(), IDOrName) { - return container + candidates = append(candidates, container) } } + if len(candidates) == 1 { + return candidates[0] + } + return nil } diff --git a/cluster/swarm/cluster_test.go b/cluster/swarm/cluster_test.go index 3cfb6dab5f..b870908836 100644 --- a/cluster/swarm/cluster_test.go +++ b/cluster/swarm/cluster_test.go @@ -27,24 +27,24 @@ func TestContainerLookup(t *testing.T) { } container1 := &cluster.Container{ Container: dockerclient.Container{ - Id: "container-id1", + Id: "container1-id", Names: []string{"/container1-name1", "/container1-name2"}, }, Config: cluster.BuildContainerConfig(dockerclient.ContainerConfig{ Labels: map[string]string{ - "com.docker.swarm.id": "swarm-id1", + "com.docker.swarm.id": "swarm1-id", }, }), } container2 := &cluster.Container{ Container: dockerclient.Container{ - Id: "container-id2", + Id: "container2-id", Names: []string{"/con"}, }, Config: cluster.BuildContainerConfig(dockerclient.ContainerConfig{ Labels: map[string]string{ - "com.docker.swarm.id": "swarm-id2", + "com.docker.swarm.id": "swarm2-id", }, }), } @@ -56,9 +56,10 @@ func TestContainerLookup(t *testing.T) { assert.Nil(t, c.Container("invalid-id")) assert.Nil(t, c.Container("")) // Container ID lookup. - assert.NotNil(t, c.Container("container-id1")) + assert.NotNil(t, c.Container("container1-id")) // Container ID prefix lookup. - assert.NotNil(t, c.Container("container-")) + assert.NotNil(t, c.Container("container1-")) + assert.Nil(t, c.Container("container")) // Container name lookup. assert.NotNil(t, c.Container("container1-name1")) assert.NotNil(t, c.Container("container1-name2")) @@ -66,11 +67,12 @@ func TestContainerLookup(t *testing.T) { assert.NotNil(t, c.Container("test-engine/container1-name1")) assert.NotNil(t, c.Container("test-engine/container1-name2")) // Swarm ID lookup. - assert.NotNil(t, c.Container("swarm-id1")) + assert.NotNil(t, c.Container("swarm1-id")) // Swarm ID prefix lookup. - assert.NotNil(t, c.Container("swarm-")) + assert.NotNil(t, c.Container("swarm1-")) + assert.Nil(t, c.Container("swarm")) // Match name before ID prefix cc := c.Container("con") assert.NotNil(t, cc) - assert.Equal(t, cc.Id, "container-id2") + assert.Equal(t, cc.Id, "container2-id") }