kill_test.go 3.8 KB

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