kill_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "fmt"
  17. "path/filepath"
  18. "strings"
  19. "testing"
  20. moby "github.com/docker/docker/api/types"
  21. containerType "github.com/docker/docker/api/types/container"
  22. "github.com/docker/docker/api/types/filters"
  23. "github.com/docker/docker/api/types/network"
  24. "github.com/docker/docker/api/types/volume"
  25. "go.uber.org/mock/gomock"
  26. "gotest.tools/v3/assert"
  27. compose "github.com/docker/compose/v2/pkg/api"
  28. )
  29. const testProject = "testProject"
  30. func TestKillAll(t *testing.T) {
  31. mockCtrl := gomock.NewController(t)
  32. defer mockCtrl.Finish()
  33. api, cli := prepareMocks(mockCtrl)
  34. tested := composeService{
  35. dockerCli: cli,
  36. }
  37. name := strings.ToLower(testProject)
  38. ctx := context.Background()
  39. api.EXPECT().ContainerList(ctx, containerType.ListOptions{
  40. Filters: filters.NewArgs(projectFilter(name), hasConfigHashLabel()),
  41. }).Return(
  42. []moby.Container{testContainer("service1", "123", false), testContainer("service1", "456", false), testContainer("service2", "789", false)}, nil)
  43. api.EXPECT().VolumeList(
  44. gomock.Any(),
  45. volume.ListOptions{
  46. Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject))),
  47. }).
  48. Return(volume.ListResponse{}, nil)
  49. api.EXPECT().NetworkList(gomock.Any(), network.ListOptions{Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)))}).
  50. Return([]network.Summary{
  51. {ID: "abc123", Name: "testProject_default"},
  52. }, nil)
  53. api.EXPECT().ContainerKill(anyCancellableContext(), "123", "").Return(nil)
  54. api.EXPECT().ContainerKill(anyCancellableContext(), "456", "").Return(nil)
  55. api.EXPECT().ContainerKill(anyCancellableContext(), "789", "").Return(nil)
  56. err := tested.kill(ctx, name, compose.KillOptions{})
  57. assert.NilError(t, err)
  58. }
  59. func TestKillSignal(t *testing.T) {
  60. const serviceName = "service1"
  61. mockCtrl := gomock.NewController(t)
  62. defer mockCtrl.Finish()
  63. api, cli := prepareMocks(mockCtrl)
  64. tested := composeService{
  65. dockerCli: cli,
  66. }
  67. name := strings.ToLower(testProject)
  68. listOptions := containerType.ListOptions{
  69. Filters: filters.NewArgs(projectFilter(name), serviceFilter(serviceName), hasConfigHashLabel()),
  70. }
  71. ctx := context.Background()
  72. api.EXPECT().ContainerList(ctx, listOptions).Return([]moby.Container{testContainer(serviceName, "123", false)}, nil)
  73. api.EXPECT().VolumeList(
  74. gomock.Any(),
  75. volume.ListOptions{
  76. Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject))),
  77. }).
  78. Return(volume.ListResponse{}, nil)
  79. api.EXPECT().NetworkList(gomock.Any(), network.ListOptions{Filters: filters.NewArgs(projectFilter(strings.ToLower(testProject)))}).
  80. Return([]network.Summary{
  81. {ID: "abc123", Name: "testProject_default"},
  82. }, nil)
  83. api.EXPECT().ContainerKill(anyCancellableContext(), "123", "SIGTERM").Return(nil)
  84. err := tested.kill(ctx, name, compose.KillOptions{Services: []string{serviceName}, Signal: "SIGTERM"})
  85. assert.NilError(t, err)
  86. }
  87. func testContainer(service string, id string, oneOff bool) moby.Container {
  88. // canonical docker names in the API start with a leading slash, some
  89. // parts of Compose code will attempt to strip this off, so make sure
  90. // it's consistently present
  91. name := "/" + strings.TrimPrefix(id, "/")
  92. return moby.Container{
  93. ID: id,
  94. Names: []string{name},
  95. Labels: containerLabels(service, oneOff),
  96. State: ContainerExited,
  97. }
  98. }
  99. func containerLabels(service string, oneOff bool) map[string]string {
  100. workingdir := "/src/pkg/compose/testdata"
  101. composefile := filepath.Join(workingdir, "compose.yaml")
  102. labels := map[string]string{
  103. compose.ServiceLabel: service,
  104. compose.ConfigFilesLabel: composefile,
  105. compose.WorkingDirLabel: workingdir,
  106. compose.ProjectLabel: strings.ToLower(testProject)}
  107. if oneOff {
  108. labels[compose.OneoffLabel] = "True"
  109. }
  110. return labels
  111. }
  112. func anyCancellableContext() gomock.Matcher {
  113. ctxWithCancel, cancel := context.WithCancel(context.Background())
  114. cancel()
  115. return gomock.AssignableToTypeOf(ctxWithCancel)
  116. }
  117. func projectFilterListOpt(withOneOff bool) containerType.ListOptions {
  118. filter := filters.NewArgs(
  119. projectFilter(strings.ToLower(testProject)),
  120. hasConfigHashLabel(),
  121. )
  122. if !withOneOff {
  123. filter.Add("label", fmt.Sprintf("%s=False", compose.OneoffLabel))
  124. }
  125. return containerType.ListOptions{
  126. Filters: filter,
  127. All: true,
  128. }
  129. }