Skip to content

Commit 22b8c73

Browse files
committed
prevent getCanonicalContainerName to crash
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent f86f252 commit 22b8c73

File tree

6 files changed

+42
-14
lines changed

6 files changed

+42
-14
lines changed

pkg/compose/compose.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ func (s *composeService) stderr() io.Writer {
7171
}
7272

7373
func getCanonicalContainerName(c moby.Container) string {
74+
if len(c.Names) == 0 {
75+
// corner case, sometime happens on removal. return short ID as a safeguard value
76+
return c.ID[:12]
77+
}
7478
// Names return container canonical name /foo + link aliases /linked_by/foo
7579
for _, name := range c.Names {
7680
if strings.LastIndex(name, "/") == 0 {

pkg/compose/convergence_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestServiceLinks(t *testing.T) {
7878
apiClient := mocks.NewMockAPIClient(mockCtrl)
7979
cli := mocks.NewMockCli(mockCtrl)
8080
tested.dockerCli = cli
81-
cli.EXPECT().Client().Return(apiClient)
81+
cli.EXPECT().Client().Return(apiClient).AnyTimes()
8282

8383
s.Links = []string{"db"}
8484

@@ -100,7 +100,7 @@ func TestServiceLinks(t *testing.T) {
100100
apiClient := mocks.NewMockAPIClient(mockCtrl)
101101
cli := mocks.NewMockCli(mockCtrl)
102102
tested.dockerCli = cli
103-
cli.EXPECT().Client().Return(apiClient)
103+
cli.EXPECT().Client().Return(apiClient).AnyTimes()
104104

105105
s.Links = []string{"db:db"}
106106

@@ -122,7 +122,7 @@ func TestServiceLinks(t *testing.T) {
122122
apiClient := mocks.NewMockAPIClient(mockCtrl)
123123
cli := mocks.NewMockCli(mockCtrl)
124124
tested.dockerCli = cli
125-
cli.EXPECT().Client().Return(apiClient)
125+
cli.EXPECT().Client().Return(apiClient).AnyTimes()
126126

127127
s.Links = []string{"db:dbname"}
128128

@@ -144,7 +144,7 @@ func TestServiceLinks(t *testing.T) {
144144
apiClient := mocks.NewMockAPIClient(mockCtrl)
145145
cli := mocks.NewMockCli(mockCtrl)
146146
tested.dockerCli = cli
147-
cli.EXPECT().Client().Return(apiClient)
147+
cli.EXPECT().Client().Return(apiClient).AnyTimes()
148148

149149
s.Links = []string{"db:dbname"}
150150
s.ExternalLinks = []string{"db1:db2"}
@@ -170,7 +170,7 @@ func TestServiceLinks(t *testing.T) {
170170
apiClient := mocks.NewMockAPIClient(mockCtrl)
171171
cli := mocks.NewMockCli(mockCtrl)
172172
tested.dockerCli = cli
173-
cli.EXPECT().Client().Return(apiClient)
173+
cli.EXPECT().Client().Return(apiClient).AnyTimes()
174174

175175
s.Links = []string{}
176176
s.ExternalLinks = []string{}
@@ -200,8 +200,11 @@ func TestServiceLinks(t *testing.T) {
200200
func TestWaitDependencies(t *testing.T) {
201201
mockCtrl := gomock.NewController(t)
202202
defer mockCtrl.Finish()
203-
api := mocks.NewMockAPIClient(mockCtrl)
204-
tested.apiClient = api
203+
204+
apiClient := mocks.NewMockAPIClient(mockCtrl)
205+
cli := mocks.NewMockCli(mockCtrl)
206+
tested.dockerCli = cli
207+
cli.EXPECT().Client().Return(apiClient).AnyTimes()
205208

206209
t.Run("should skip dependencies with scale 0", func(t *testing.T) {
207210
dbService := types.ServiceConfig{Name: "db", Scale: 0}

pkg/compose/down_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ import (
3434
func TestDown(t *testing.T) {
3535
mockCtrl := gomock.NewController(t)
3636
defer mockCtrl.Finish()
37+
3738
api := mocks.NewMockAPIClient(mockCtrl)
38-
tested.apiClient = api
39+
cli := mocks.NewMockCli(mockCtrl)
40+
tested.dockerCli = cli
41+
cli.EXPECT().Client().Return(api).AnyTimes()
3942

4043
api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return(
4144
[]moby.Container{
@@ -67,8 +70,11 @@ func TestDown(t *testing.T) {
6770
func TestDownRemoveOrphans(t *testing.T) {
6871
mockCtrl := gomock.NewController(t)
6972
defer mockCtrl.Finish()
73+
7074
api := mocks.NewMockAPIClient(mockCtrl)
71-
tested.apiClient = api
75+
cli := mocks.NewMockCli(mockCtrl)
76+
tested.dockerCli = cli
77+
cli.EXPECT().Client().Return(api).AnyTimes()
7278

7379
api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return(
7480
[]moby.Container{
@@ -99,8 +105,11 @@ func TestDownRemoveOrphans(t *testing.T) {
99105
func TestDownRemoveVolumes(t *testing.T) {
100106
mockCtrl := gomock.NewController(t)
101107
defer mockCtrl.Finish()
108+
102109
api := mocks.NewMockAPIClient(mockCtrl)
103-
tested.apiClient = api
110+
cli := mocks.NewMockCli(mockCtrl)
111+
tested.dockerCli = cli
112+
cli.EXPECT().Client().Return(api).AnyTimes()
104113

105114
api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return(
106115
[]moby.Container{testContainer("service1", "123", false)}, nil)

pkg/compose/kill_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ var tested = composeService{}
3939
func TestKillAll(t *testing.T) {
4040
mockCtrl := gomock.NewController(t)
4141
defer mockCtrl.Finish()
42+
4243
api := mocks.NewMockAPIClient(mockCtrl)
43-
tested.apiClient = api
44+
cli := mocks.NewMockCli(mockCtrl)
45+
tested.dockerCli = cli
46+
cli.EXPECT().Client().Return(api).AnyTimes()
4447

4548
project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
4649

@@ -61,8 +64,11 @@ func TestKillSignal(t *testing.T) {
6164
const serviceName = "service1"
6265
mockCtrl := gomock.NewController(t)
6366
defer mockCtrl.Finish()
67+
6468
api := mocks.NewMockAPIClient(mockCtrl)
65-
tested.apiClient = api
69+
cli := mocks.NewMockCli(mockCtrl)
70+
tested.dockerCli = cli
71+
cli.EXPECT().Client().Return(api).AnyTimes()
6672

6773
project := types.Project{Name: strings.ToLower(testProject), Services: []types.ServiceConfig{testService(serviceName)}}
6874
listOptions := moby.ContainerListOptions{

pkg/compose/ps_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ import (
3434
func TestPs(t *testing.T) {
3535
mockCtrl := gomock.NewController(t)
3636
defer mockCtrl.Finish()
37+
3738
api := mocks.NewMockAPIClient(mockCtrl)
38-
tested.apiClient = api
39+
cli := mocks.NewMockCli(mockCtrl)
40+
tested.dockerCli = cli
41+
cli.EXPECT().Client().Return(api).AnyTimes()
3942

4043
ctx := context.Background()
4144
args := filters.NewArgs(projectFilter(strings.ToLower(testProject)))

pkg/compose/stop_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ import (
3333
func TestStopTimeout(t *testing.T) {
3434
mockCtrl := gomock.NewController(t)
3535
defer mockCtrl.Finish()
36+
3637
api := mocks.NewMockAPIClient(mockCtrl)
37-
tested.apiClient = api
38+
cli := mocks.NewMockCli(mockCtrl)
39+
tested.dockerCli = cli
40+
cli.EXPECT().Client().Return(api).AnyTimes()
3841

3942
ctx := context.Background()
4043
api.EXPECT().ContainerList(gomock.Any(), projectFilterListOpt()).Return(

0 commit comments

Comments
 (0)