mirror of
https://github.com/docker/compose.git
synced 2025-04-08 17:05:13 +02:00
Kill Unit test mocking docker client API
Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
parent
5ddcc84a3d
commit
b8093e668a
local
@ -33,12 +33,12 @@ import (
|
||||
)
|
||||
|
||||
// NewComposeService create a local implementation of the compose.Service API
|
||||
func NewComposeService(apiClient *client.Client) compose.Service {
|
||||
func NewComposeService(apiClient client.APIClient) compose.Service {
|
||||
return &composeService{apiClient: apiClient}
|
||||
}
|
||||
|
||||
type composeService struct {
|
||||
apiClient *client.Client
|
||||
apiClient client.APIClient
|
||||
}
|
||||
|
||||
func (s *composeService) Up(ctx context.Context, project *types.Project, options compose.UpOptions) error {
|
||||
|
92
local/compose/kill_test.go
Normal file
92
local/compose/kill_test.go
Normal file
@ -0,0 +1,92 @@
|
||||
/*
|
||||
Copyright 2020 Docker Compose CLI authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package compose
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/compose-spec/compose-go/types"
|
||||
apitypes "github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"gotest.tools/v3/assert"
|
||||
|
||||
"github.com/docker/compose-cli/api/compose"
|
||||
"github.com/docker/compose-cli/local/mocks"
|
||||
)
|
||||
|
||||
var (
|
||||
s = composeService{}
|
||||
projectListOpts = apitypes.ContainerListOptions{
|
||||
Filters: filters.NewArgs(projectFilter("myProject")),
|
||||
All: true,
|
||||
}
|
||||
)
|
||||
|
||||
func TestKillAll(t *testing.T) {
|
||||
mockCtrl := gomock.NewController(t)
|
||||
defer mockCtrl.Finish()
|
||||
api := mocks.NewMockAPIClient(mockCtrl)
|
||||
s.apiClient = api
|
||||
|
||||
project := types.Project{Name: "myProject", Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
|
||||
|
||||
ctx := context.Background()
|
||||
api.EXPECT().ContainerList(ctx, projectListOpts).Return([]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789")}, nil)
|
||||
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "").Return(nil)
|
||||
api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
|
||||
api.EXPECT().ContainerKill(anyCancellableContext(), "789", "").Return(nil)
|
||||
|
||||
err := s.Kill(ctx, &project, compose.KillOptions{})
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
||||
func TestKillSignal(t *testing.T) {
|
||||
mockCtrl := gomock.NewController(t)
|
||||
defer mockCtrl.Finish()
|
||||
api := mocks.NewMockAPIClient(mockCtrl)
|
||||
s.apiClient = api
|
||||
|
||||
project := types.Project{Name: "myProject", Services: []types.ServiceConfig{testService("service1")}}
|
||||
|
||||
ctx := context.Background()
|
||||
api.EXPECT().ContainerList(ctx, projectListOpts).Return([]apitypes.Container{testContainer("service1", "123")}, nil)
|
||||
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
|
||||
|
||||
err := s.Kill(ctx, &project, compose.KillOptions{Signal: "SIGTERM"})
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
||||
func testService(name string) types.ServiceConfig {
|
||||
return types.ServiceConfig{Name: name}
|
||||
}
|
||||
|
||||
func testContainer(service string, id string) apitypes.Container {
|
||||
return apitypes.Container{
|
||||
ID: id,
|
||||
Names: []string{id},
|
||||
Labels: map[string]string{compose.ServiceTag: service},
|
||||
}
|
||||
}
|
||||
|
||||
func anyCancellableContext() gomock.Matcher {
|
||||
ctxWithCancel, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
return gomock.AssignableToTypeOf(ctxWithCancel)
|
||||
}
|
1812
local/mocks/mock_docker_api.go
Normal file
1812
local/mocks/mock_docker_api.go
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user