Skip to content

Commit

Permalink
fix: Make sure we don't remove the first character if container name …
Browse files Browse the repository at this point in the history
…doesn't have a slash
  • Loading branch information
0x20F committed Feb 19, 2022
1 parent f393754 commit f274a09
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docker/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 32 additions & 1 deletion docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -30,6 +40,7 @@ func (w *MockWrapper) RunningContainers() []dockerTypes.Container {

func before() {
CustomWrapper(&MockWrapper{})
replica.Mocks.Clear()
}

func TestRunningContainers(t *testing.T) {
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit f274a09

Please sign in to comment.