|
| 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