Merge pull request #1655 from Christopher-Bui/short-container-id

Allow finding docker containers by short name
This commit is contained in:
David Ashpole 2017-05-18 10:07:24 -07:00 committed by GitHub
commit dd5df2c73e
2 changed files with 33 additions and 3 deletions

View File

@ -566,9 +566,24 @@ func (self *manager) getDockerContainer(containerName string) (*containerData, e
Namespace: docker.DockerNamespace,
Name: containerName,
}]
// Look for container by short prefix name if no exact match found.
if !ok {
return nil, fmt.Errorf("unable to find Docker container %q", containerName)
for contName, c := range self.containers {
if contName.Namespace == docker.DockerNamespace && strings.HasPrefix(contName.Name, containerName) {
if cont == nil {
cont = c
} else {
return nil, fmt.Errorf("unable to find container. Container %q is not unique", containerName)
}
}
}
if cont == nil {
return nil, fmt.Errorf("unable to find Docker container %q", containerName)
}
}
return cont, nil
}

View File

@ -280,7 +280,8 @@ func TestSubcontainersInfo(t *testing.T) {
func TestDockerContainersInfo(t *testing.T) {
containers := []string{
"/docker/c1",
"/docker/c1a",
"/docker/c2a",
}
query := &info.ContainerInfoRequest{
@ -289,13 +290,27 @@ func TestDockerContainersInfo(t *testing.T) {
m, _, _ := expectManagerWithContainers(containers, query, t)
result, err := m.DockerContainer("c1", query)
result, err := m.DockerContainer("c1a", query)
if err != nil {
t.Fatalf("expected to succeed: %s", err)
}
if result.Name != containers[0] {
t.Errorf("Unexpected container %q in result. Expected container %q", result.Name, containers[0])
}
result, err = m.DockerContainer("c2", query)
if err != nil {
t.Fatalf("expected to succeed: %s", err)
}
if result.Name != containers[1] {
t.Errorf("Unexpected container %q in result. Expected container %q", result.Name, containers[1])
}
result, err = m.DockerContainer("c", query)
expectedError := "unable to find container. Container \"c\" is not unique"
if err == nil {
t.Errorf("expected error %q but received %q", expectedError, err)
}
}
func TestNewNilManager(t *testing.T) {