cancel_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //go:build !windows
  2. // +build !windows
  3. /*
  4. Copyright 2020 Docker Compose CLI authors
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. package e2e
  16. import (
  17. "context"
  18. "fmt"
  19. "os/exec"
  20. "strings"
  21. "syscall"
  22. "testing"
  23. "time"
  24. "github.com/docker/compose/v2/pkg/utils"
  25. "gotest.tools/v3/assert"
  26. "gotest.tools/v3/icmd"
  27. )
  28. func TestComposeCancel(t *testing.T) {
  29. c := NewParallelCLI(t)
  30. t.Run("metrics on cancel Compose build", func(t *testing.T) {
  31. const buildProjectPath = "fixtures/build-infinite/compose.yaml"
  32. ctx, cancel := context.WithCancel(context.Background())
  33. defer cancel()
  34. // require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal.
  35. // sending kill signal
  36. var stdout, stderr utils.SafeBuffer
  37. cmd, err := StartWithNewGroupID(
  38. ctx,
  39. c.NewDockerComposeCmd(t, "-f", buildProjectPath, "build", "--progress", "plain"),
  40. &stdout,
  41. &stderr,
  42. )
  43. assert.NilError(t, err)
  44. processDone := make(chan error, 1)
  45. go func() {
  46. defer close(processDone)
  47. processDone <- cmd.Wait()
  48. }()
  49. c.WaitForCondition(t, func() (bool, string) {
  50. out := stdout.String()
  51. errors := stderr.String()
  52. return strings.Contains(out,
  53. "RUN sleep infinity"), fmt.Sprintf("'RUN sleep infinity' not found in : \n%s\nStderr: \n%s\n", out,
  54. errors)
  55. }, 30*time.Second, 1*time.Second)
  56. // simulate Ctrl-C : send signal to processGroup, children will have same groupId by default
  57. err = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT)
  58. assert.NilError(t, err)
  59. select {
  60. case <-ctx.Done():
  61. t.Fatal("test context canceled")
  62. case err := <-processDone:
  63. // TODO(milas): Compose should really not return exit code 130 here,
  64. // this is an old hack for the compose-cli wrapper
  65. assert.Error(t, err, "exit status 130",
  66. "STDOUT:\n%s\nSTDERR:\n%s\n", stdout.String(), stderr.String())
  67. case <-time.After(10 * time.Second):
  68. t.Fatal("timeout waiting for Compose exit")
  69. }
  70. })
  71. }
  72. func StartWithNewGroupID(ctx context.Context, command icmd.Cmd, stdout *utils.SafeBuffer, stderr *utils.SafeBuffer) (*exec.Cmd, error) {
  73. cmd := exec.CommandContext(ctx, command.Command[0], command.Command[1:]...)
  74. cmd.Env = command.Env
  75. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  76. if stdout != nil {
  77. cmd.Stdout = stdout
  78. }
  79. if stderr != nil {
  80. cmd.Stderr = stderr
  81. }
  82. err := cmd.Start()
  83. return cmd, err
  84. }