Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit b8093e6

Browse files
committed
Kill Unit test mocking docker client API
Signed-off-by: Guillaume Tardif <[email protected]>
1 parent 5ddcc84 commit b8093e6

File tree

3 files changed

+1906
-2
lines changed

3 files changed

+1906
-2
lines changed

local/compose/compose.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ import (
3333
)
3434

3535
// NewComposeService create a local implementation of the compose.Service API
36-
func NewComposeService(apiClient *client.Client) compose.Service {
36+
func NewComposeService(apiClient client.APIClient) compose.Service {
3737
return &composeService{apiClient: apiClient}
3838
}
3939

4040
type composeService struct {
41-
apiClient *client.Client
41+
apiClient client.APIClient
4242
}
4343

4444
func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {

local/compose/kill_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/compose-spec/compose-go/types"
24+
apitypes "github.com/docker/docker/api/types"
25+
"github.com/docker/docker/api/types/filters"
26+
27+
"github.com/golang/mock/gomock"
28+
"gotest.tools/v3/assert"
29+
30+
"github.com/docker/compose-cli/api/compose"
31+
"github.com/docker/compose-cli/local/mocks"
32+
)
33+
34+
var (
35+
s = composeService{}
36+
projectListOpts = apitypes.ContainerListOptions{
37+
Filters: filters.NewArgs(projectFilter("myProject")),
38+
All: true,
39+
}
40+
)
41+
42+
func TestKillAll(t *testing.T) {
43+
mockCtrl := gomock.NewController(t)
44+
defer mockCtrl.Finish()
45+
api := mocks.NewMockAPIClient(mockCtrl)
46+
s.apiClient = api
47+
48+
project := types.Project{Name: "myProject", Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
49+
50+
ctx := context.Background()
51+
api.EXPECT().ContainerList(ctx, projectListOpts).Return([]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789")}, nil)
52+
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "").Return(nil)
53+
api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
54+
api.EXPECT().ContainerKill(anyCancellableContext(), "789", "").Return(nil)
55+
56+
err := s.Kill(ctx, &project, compose.KillOptions{})
57+
assert.NilError(t, err)
58+
}
59+
60+
func TestKillSignal(t *testing.T) {
61+
mockCtrl := gomock.NewController(t)
62+
defer mockCtrl.Finish()
63+
api := mocks.NewMockAPIClient(mockCtrl)
64+
s.apiClient = api
65+
66+
project := types.Project{Name: "myProject", Services: []types.ServiceConfig{testService("service1")}}
67+
68+
ctx := context.Background()
69+
api.EXPECT().ContainerList(ctx, projectListOpts).Return([]apitypes.Container{testContainer("service1", "123")}, nil)
70+
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
71+
72+
err := s.Kill(ctx, &project, compose.KillOptions{Signal: "SIGTERM"})
73+
assert.NilError(t, err)
74+
}
75+
76+
func testService(name string) types.ServiceConfig {
77+
return types.ServiceConfig{Name: name}
78+
}
79+
80+
func testContainer(service string, id string) apitypes.Container {
81+
return apitypes.Container{
82+
ID: id,
83+
Names: []string{id},
84+
Labels: map[string]string{compose.ServiceTag: service},
85+
}
86+
}
87+
88+
func anyCancellableContext() gomock.Matcher {
89+
ctxWithCancel, cancel := context.WithCancel(context.Background())
90+
cancel()
91+
return gomock.AssignableToTypeOf(ctxWithCancel)
92+
}

0 commit comments

Comments
 (0)