From 22b8c731c7f06a4623dc9554bd92d83aa7ad914a Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 3 Mar 2022 14:19:01 +0100 Subject: [PATCH] prevent getCanonicalContainerName to crash Signed-off-by: Nicolas De Loof --- pkg/compose/compose.go | 4 ++++ pkg/compose/convergence_test.go | 17 ++++++++++------- pkg/compose/down_test.go | 15 ++++++++++++--- pkg/compose/kill_test.go | 10 ++++++++-- pkg/compose/ps_test.go | 5 ++++- pkg/compose/stop_test.go | 5 ++++- 6 files changed, 42 insertions(+), 14 deletions(-) diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index ed241c625..830980764 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -71,6 +71,10 @@ func (s *composeService) stderr() io.Writer { } func getCanonicalContainerName(c moby.Container) string { + if len(c.Names) == 0 { + // corner case, sometime happens on removal. return short ID as a safeguard value + return c.ID[:12] + } // Names return container canonical name /foo + link aliases /linked_by/foo for _, name := range c.Names { if strings.LastIndex(name, "/") == 0 { diff --git a/pkg/compose/convergence_test.go b/pkg/compose/convergence_test.go index 5f1ccf27e..bd41db30a 100644 --- a/pkg/compose/convergence_test.go +++ b/pkg/compose/convergence_test.go @@ -78,7 +78,7 @@ func TestServiceLinks(t *testing.T) { apiClient := mocks.NewMockAPIClient(mockCtrl) cli := mocks.NewMockCli(mockCtrl) tested.dockerCli = cli - cli.EXPECT().Client().Return(apiClient) + cli.EXPECT().Client().Return(apiClient).AnyTimes() s.Links = []string{"db"} @@ -100,7 +100,7 @@ func TestServiceLinks(t *testing.T) { apiClient := mocks.NewMockAPIClient(mockCtrl) cli := mocks.NewMockCli(mockCtrl) tested.dockerCli = cli - cli.EXPECT().Client().Return(apiClient) + cli.EXPECT().Client().Return(apiClient).AnyTimes() s.Links = []string{"db:db"} @@ -122,7 +122,7 @@ func TestServiceLinks(t *testing.T) { apiClient := mocks.NewMockAPIClient(mockCtrl) cli := mocks.NewMockCli(mockCtrl) tested.dockerCli = cli - cli.EXPECT().Client().Return(apiClient) + cli.EXPECT().Client().Return(apiClient).AnyTimes() s.Links = []string{"db:dbname"} @@ -144,7 +144,7 @@ func TestServiceLinks(t *testing.T) { apiClient := mocks.NewMockAPIClient(mockCtrl) cli := mocks.NewMockCli(mockCtrl) tested.dockerCli = cli - cli.EXPECT().Client().Return(apiClient) + cli.EXPECT().Client().Return(apiClient).AnyTimes() s.Links = []string{"db:dbname"} s.ExternalLinks = []string{"db1:db2"} @@ -170,7 +170,7 @@ func TestServiceLinks(t *testing.T) { apiClient := mocks.NewMockAPIClient(mockCtrl) cli := mocks.NewMockCli(mockCtrl) tested.dockerCli = cli - cli.EXPECT().Client().Return(apiClient) + cli.EXPECT().Client().Return(apiClient).AnyTimes() s.Links = []string{} s.ExternalLinks = []string{} @@ -200,8 +200,11 @@ func TestServiceLinks(t *testing.T) { func TestWaitDependencies(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() - api := mocks.NewMockAPIClient(mockCtrl) - tested.apiClient = api + + apiClient := mocks.NewMockAPIClient(mockCtrl) + cli := mocks.NewMockCli(mockCtrl) + tested.dockerCli = cli + cli.EXPECT().Client().Return(apiClient).AnyTimes() t.Run("should skip dependencies with scale 0", func(t *testing.T) { dbService := types.ServiceConfig{Name: "db", Scale: 0} diff --git a/pkg/compose/down_test.go b/pkg/compose/down_test.go index b862ce154..5f820e30d 100644 --- a/pkg/compose/down_test.go +++ b/pkg/compose/down_test.go @@ -34,8 +34,11 @@ import ( func TestDown(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() + api := mocks.NewMockAPIClient(mockCtrl) - tested.apiClient = api + cli := mocks.NewMockCli(mockCtrl) + tested.dockerCli = cli + cli.EXPECT().Client().Return(api).AnyTimes() api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return( []moby.Container{ @@ -67,8 +70,11 @@ func TestDown(t *testing.T) { func TestDownRemoveOrphans(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() + api := mocks.NewMockAPIClient(mockCtrl) - tested.apiClient = api + cli := mocks.NewMockCli(mockCtrl) + tested.dockerCli = cli + cli.EXPECT().Client().Return(api).AnyTimes() api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return( []moby.Container{ @@ -99,8 +105,11 @@ func TestDownRemoveOrphans(t *testing.T) { func TestDownRemoveVolumes(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() + api := mocks.NewMockAPIClient(mockCtrl) - tested.apiClient = api + cli := mocks.NewMockCli(mockCtrl) + tested.dockerCli = cli + cli.EXPECT().Client().Return(api).AnyTimes() api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return( []moby.Container{testContainer("service1", "123", false)}, nil) diff --git a/pkg/compose/kill_test.go b/pkg/compose/kill_test.go index 91cc71844..b8dcc3d68 100644 --- a/pkg/compose/kill_test.go +++ b/pkg/compose/kill_test.go @@ -39,8 +39,11 @@ var tested = composeService{} func TestKillAll(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() + api := mocks.NewMockAPIClient(mockCtrl) - tested.apiClient = api + cli := mocks.NewMockCli(mockCtrl) + tested.dockerCli = cli + cli.EXPECT().Client().Return(api).AnyTimes() project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{testService("service1"), testService("service2")}} @@ -61,8 +64,11 @@ func TestKillSignal(t *testing.T) { const serviceName = "service1" mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() + api := mocks.NewMockAPIClient(mockCtrl) - tested.apiClient = api + cli := mocks.NewMockCli(mockCtrl) + tested.dockerCli = cli + cli.EXPECT().Client().Return(api).AnyTimes() project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{testService(serviceName)}} listOptions := moby.ContainerListOptions{ diff --git a/pkg/compose/ps_test.go b/pkg/compose/ps_test.go index 5de346729..2f17616e4 100644 --- a/pkg/compose/ps_test.go +++ b/pkg/compose/ps_test.go @@ -34,8 +34,11 @@ import ( func TestPs(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() + api := mocks.NewMockAPIClient(mockCtrl) - tested.apiClient = api + cli := mocks.NewMockCli(mockCtrl) + tested.dockerCli = cli + cli.EXPECT().Client().Return(api).AnyTimes() ctx := context.Background() args := filters.NewArgs(projectFilter(strings.ToLower(testProject))) diff --git a/pkg/compose/stop_test.go b/pkg/compose/stop_test.go index 29cecbd3f..e5848780e 100644 --- a/pkg/compose/stop_test.go +++ b/pkg/compose/stop_test.go @@ -33,8 +33,11 @@ import ( func TestStopTimeout(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() + api := mocks.NewMockAPIClient(mockCtrl) - tested.apiClient = api + cli := mocks.NewMockCli(mockCtrl) + tested.dockerCli = cli + cli.EXPECT().Client().Return(api).AnyTimes() ctx := context.Background() api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return(