mirror of https://github.com/docker/compose.git
Add down unit tests
Signed-off-by: Guillaume Tardif <guillaume.tardif@gmail.com>
This commit is contained in:
parent
b8093e668a
commit
4140196eb0
|
@ -64,10 +64,8 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt
|
||||||
|
|
||||||
var observedState Containers
|
var observedState Containers
|
||||||
observedState, err = s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
observedState, err = s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
||||||
Filters: filters.NewArgs(
|
Filters: filters.NewArgs(projectFilter(project.Name)),
|
||||||
projectFilter(project.Name),
|
All: true,
|
||||||
),
|
|
||||||
All: true,
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -73,11 +73,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, options c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
networks, err := s.apiClient.NetworkList(ctx, moby.NetworkListOptions{
|
networks, err := s.apiClient.NetworkList(ctx, moby.NetworkListOptions{Filters: filters.NewArgs(projectFilter(projectName))})
|
||||||
Filters: filters.NewArgs(
|
|
||||||
projectFilter(projectName),
|
|
||||||
),
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -137,13 +133,15 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
|
||||||
return eg.Wait()
|
return eg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func projectFilterListOpt(projectName string) moby.ContainerListOptions {
|
||||||
|
return moby.ContainerListOptions{
|
||||||
|
Filters: filters.NewArgs(projectFilter(projectName)),
|
||||||
|
All: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *composeService) projectFromContainerLabels(ctx context.Context, projectName string) (*types.Project, error) {
|
func (s *composeService) projectFromContainerLabels(ctx context.Context, projectName string) (*types.Project, error) {
|
||||||
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
containers, err := s.apiClient.ContainerList(ctx, projectFilterListOpt(projectName))
|
||||||
Filters: filters.NewArgs(
|
|
||||||
projectFilter(projectName),
|
|
||||||
),
|
|
||||||
All: true,
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
/*
|
||||||
|
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/golang/mock/gomock"
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
|
|
||||||
|
apitypes "github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
|
||||||
|
"github.com/docker/compose-cli/api/compose"
|
||||||
|
"github.com/docker/compose-cli/local/mocks"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDown(t *testing.T) {
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
api := mocks.NewMockAPIClient(mockCtrl)
|
||||||
|
tested.apiClient = api
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
|
||||||
|
[]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
|
||||||
|
|
||||||
|
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
|
||||||
|
api.EXPECT().ContainerStop(ctx, "456", nil).Return(nil)
|
||||||
|
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)
|
||||||
|
|
||||||
|
api.EXPECT().ContainerRemove(ctx, "123", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
|
||||||
|
api.EXPECT().ContainerRemove(ctx, "456", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
|
||||||
|
api.EXPECT().ContainerRemove(ctx, "789", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
|
||||||
|
|
||||||
|
api.EXPECT().NetworkList(ctx, apitypes.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return([]apitypes.NetworkResource{{ID: "myProject_default"}}, nil)
|
||||||
|
|
||||||
|
api.EXPECT().NetworkRemove(ctx, "myProject_default").Return(nil)
|
||||||
|
|
||||||
|
err := tested.Down(ctx, testProject, compose.DownOptions{})
|
||||||
|
assert.NilError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDownRemoveOrphans(t *testing.T) {
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
api := mocks.NewMockAPIClient(mockCtrl)
|
||||||
|
tested.apiClient = api
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
|
||||||
|
[]apitypes.Container{testContainer("service1", "123"), testContainer("service2", "789"), testContainer("service_orphan", "321")}, nil).Times(2)
|
||||||
|
|
||||||
|
api.EXPECT().ContainerStop(ctx, "123", nil).Return(nil)
|
||||||
|
api.EXPECT().ContainerStop(ctx, "789", nil).Return(nil)
|
||||||
|
api.EXPECT().ContainerStop(ctx, "321", nil).Return(nil)
|
||||||
|
|
||||||
|
api.EXPECT().ContainerRemove(ctx, "123", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
|
||||||
|
api.EXPECT().ContainerRemove(ctx, "789", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
|
||||||
|
api.EXPECT().ContainerRemove(ctx, "321", apitypes.ContainerRemoveOptions{Force: true}).Return(nil)
|
||||||
|
|
||||||
|
api.EXPECT().NetworkList(ctx, apitypes.NetworkListOptions{Filters: filters.NewArgs(projectFilter(testProject))}).Return([]apitypes.NetworkResource{{ID: "myProject_default"}}, nil)
|
||||||
|
|
||||||
|
api.EXPECT().NetworkRemove(ctx, "myProject_default").Return(nil)
|
||||||
|
|
||||||
|
err := tested.Down(ctx, testProject, compose.DownOptions{RemoveOrphans: true})
|
||||||
|
assert.NilError(t, err)
|
||||||
|
}
|
|
@ -20,40 +20,36 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
"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"
|
"github.com/golang/mock/gomock"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
|
|
||||||
|
"github.com/compose-spec/compose-go/types"
|
||||||
|
apitypes "github.com/docker/docker/api/types"
|
||||||
|
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"github.com/docker/compose-cli/api/compose"
|
||||||
"github.com/docker/compose-cli/local/mocks"
|
"github.com/docker/compose-cli/local/mocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
const testProject = "testProject"
|
||||||
s = composeService{}
|
|
||||||
projectListOpts = apitypes.ContainerListOptions{
|
var tested = composeService{}
|
||||||
Filters: filters.NewArgs(projectFilter("myProject")),
|
|
||||||
All: true,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestKillAll(t *testing.T) {
|
func TestKillAll(t *testing.T) {
|
||||||
mockCtrl := gomock.NewController(t)
|
mockCtrl := gomock.NewController(t)
|
||||||
defer mockCtrl.Finish()
|
defer mockCtrl.Finish()
|
||||||
api := mocks.NewMockAPIClient(mockCtrl)
|
api := mocks.NewMockAPIClient(mockCtrl)
|
||||||
s.apiClient = api
|
tested.apiClient = api
|
||||||
|
|
||||||
project := types.Project{Name: "myProject", Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
|
project := types.Project{Name: testProject, Services: []types.ServiceConfig{testService("service1"), testService("service2")}}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
api.EXPECT().ContainerList(ctx, projectListOpts).Return([]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789")}, nil)
|
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return(
|
||||||
|
[]apitypes.Container{testContainer("service1", "123"), testContainer("service1", "456"), testContainer("service2", "789")}, nil)
|
||||||
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "").Return(nil)
|
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "").Return(nil)
|
||||||
api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
|
api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
|
||||||
api.EXPECT().ContainerKill(anyCancellableContext(), "789", "").Return(nil)
|
api.EXPECT().ContainerKill(anyCancellableContext(), "789", "").Return(nil)
|
||||||
|
|
||||||
err := s.Kill(ctx, &project, compose.KillOptions{})
|
err := tested.Kill(ctx, &project, compose.KillOptions{})
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,15 +57,15 @@ func TestKillSignal(t *testing.T) {
|
||||||
mockCtrl := gomock.NewController(t)
|
mockCtrl := gomock.NewController(t)
|
||||||
defer mockCtrl.Finish()
|
defer mockCtrl.Finish()
|
||||||
api := mocks.NewMockAPIClient(mockCtrl)
|
api := mocks.NewMockAPIClient(mockCtrl)
|
||||||
s.apiClient = api
|
tested.apiClient = api
|
||||||
|
|
||||||
project := types.Project{Name: "myProject", Services: []types.ServiceConfig{testService("service1")}}
|
project := types.Project{Name: testProject, Services: []types.ServiceConfig{testService("service1")}}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
api.EXPECT().ContainerList(ctx, projectListOpts).Return([]apitypes.Container{testContainer("service1", "123")}, nil)
|
api.EXPECT().ContainerList(ctx, projectFilterListOpt(testProject)).Return([]apitypes.Container{testContainer("service1", "123")}, nil)
|
||||||
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
|
api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
|
||||||
|
|
||||||
err := s.Kill(ctx, &project, compose.KillOptions{Signal: "SIGTERM"})
|
err := tested.Kill(ctx, &project, compose.KillOptions{Signal: "SIGTERM"})
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +77,7 @@ func testContainer(service string, id string) apitypes.Container {
|
||||||
return apitypes.Container{
|
return apitypes.Container{
|
||||||
ID: id,
|
ID: id,
|
||||||
Names: []string{id},
|
Names: []string{id},
|
||||||
Labels: map[string]string{compose.ServiceTag: service},
|
Labels: map[string]string{serviceLabel: service, configFilesLabel: "testdata/docker-compose.yml", workingDirLabel: "testdata", projectLabel: testProject},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,10 +30,8 @@ import (
|
||||||
|
|
||||||
func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
|
func (s *composeService) Ps(ctx context.Context, projectName string, options compose.PsOptions) ([]compose.ContainerSummary, error) {
|
||||||
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
|
||||||
Filters: filters.NewArgs(
|
Filters: filters.NewArgs(projectFilter(projectName)),
|
||||||
projectFilter(projectName),
|
All: options.All,
|
||||||
),
|
|
||||||
All: options.All,
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
services:
|
||||||
|
service1:
|
||||||
|
image: nginx
|
||||||
|
service2:
|
||||||
|
image: mysql
|
Loading…
Reference in New Issue