From f274a09d4771316017d2206f90c6575ad37b3309 Mon Sep 17 00:00:00 2001 From: 0x20F <0x20fa@gmail.com> Date: Sat, 19 Feb 2022 09:01:30 +0100 Subject: [PATCH] fix: Make sure we don't remove the first character if container name doesn't have a slash --- docker/api.go | 4 +++- docker/docker_test.go | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/docker/api.go b/docker/api.go index 1415cb6..5703c54 100644 --- a/docker/api.go +++ b/docker/api.go @@ -30,8 +30,10 @@ func RunningContainers() []types.Container { ports = append(ports, fmt.Sprintf("%d/%s", port.PublicPort, port.Type)) } + name := strings.TrimPrefix(container.Names[0], "/") + c := types.Container{ - Name: container.Names[0][1:], // Remove the leading / + Name: name, Image: container.Image, Ports: strings.Join(ports, ", "), Status: container.Status, diff --git a/docker/docker_test.go b/docker/docker_test.go index 0677fdb..1f1247b 100644 --- a/docker/docker_test.go +++ b/docker/docker_test.go @@ -12,7 +12,17 @@ import ( type MockWrapper struct{} func (w *MockWrapper) RunningContainers() []dockerTypes.Container { - replica.MockFn() + _, rv := replica.MockFn() + + if rv != nil { + var containers []dockerTypes.Container + + if rv[0] != nil { + containers = rv[0].([]dockerTypes.Container) + } + + return containers + } return []dockerTypes.Container{ { @@ -30,6 +40,7 @@ func (w *MockWrapper) RunningContainers() []dockerTypes.Container { func before() { CustomWrapper(&MockWrapper{}) + replica.Mocks.Clear() } func TestRunningContainers(t *testing.T) { @@ -57,6 +68,26 @@ func TestApiWrapperRemovesContainerNameSlash(t *testing.T) { } } +func TestApiWrapperDoesntRemoveFirstContainerNameCharacterIfNoSlash(t *testing.T) { + before() + + // Mock the return value to return one without slash + replica.Mocks.SetReturnValues("RunningContainers", []dockerTypes.Container{ + { + ID: "1", + Image: "image1", + Names: []string{"agent1337"}, + }, + }) + + containers := RunningContainers() + + // Make sure the container name isn't missing the first character + if containers[0].Name != "agent1337" { + t.Errorf("Expected container name to be whole even if docker didn't send back one with a slash, got %s", containers[0].Name) + } +} + func TestContainerKeys(t *testing.T) { before()