cancel_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // +build !windows
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package e2e
  15. import (
  16. "bytes"
  17. "fmt"
  18. "os/exec"
  19. "strings"
  20. "syscall"
  21. "testing"
  22. "time"
  23. "gotest.tools/v3/assert"
  24. "gotest.tools/v3/icmd"
  25. . "github.com/docker/compose-cli/utils/e2e"
  26. )
  27. func TestComposeCancel(t *testing.T) {
  28. c := NewParallelE2eCLI(t, binDir)
  29. s := NewMetricsServer(c.MetricsSocket())
  30. s.Start()
  31. defer s.Stop()
  32. started := false
  33. for i := 0; i < 30; i++ {
  34. c.RunDockerCmd("help", "ps")
  35. if len(s.GetUsage()) > 0 {
  36. started = true
  37. fmt.Printf(" [%s] Server up in %d ms\n", t.Name(), i*100)
  38. break
  39. }
  40. time.Sleep(100 * time.Millisecond)
  41. }
  42. assert.Assert(t, started, "Metrics mock server not available after 3 secs")
  43. t.Run("metrics on cancel Compose build", func(t *testing.T) {
  44. s.ResetUsage()
  45. c.RunDockerCmd("compose", "ls")
  46. buildProjectPath := "../compose/fixtures/build-infinite/compose.yaml"
  47. // require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal.
  48. // sending kill signal
  49. cmd, stdout, stderr, err := StartWithNewGroupID(c.NewDockerCmd("compose", "-f", buildProjectPath, "build", "--progress", "plain"))
  50. assert.NilError(t, err)
  51. c.WaitForCondition(func() (bool, string) {
  52. out := stdout.String()
  53. errors := stderr.String()
  54. return strings.Contains(out, "RUN sleep infinity"), fmt.Sprintf("'RUN sleep infinity' not found in : \n%s\nStderr: \n%s\n", out, errors)
  55. }, 30*time.Second, 1*time.Second)
  56. err = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT) // simulate Ctrl-C : send signal to processGroup, children will have same groupId by default
  57. assert.NilError(t, err)
  58. c.WaitForCondition(func() (bool, string) {
  59. out := stdout.String()
  60. errors := stderr.String()
  61. return strings.Contains(out, "CANCELED"), fmt.Sprintf("'CANCELED' not found in : \n%s\nStderr: \n%s\n", out, errors)
  62. }, 10*time.Second, 1*time.Second)
  63. usage := s.GetUsage()
  64. assert.DeepEqual(t, []string{
  65. `{"command":"compose ls","context":"moby","source":"cli","status":"success"}`,
  66. `{"command":"compose build","context":"moby","source":"cli","status":"canceled"}`,
  67. }, usage)
  68. })
  69. }
  70. func StartWithNewGroupID(command icmd.Cmd) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer, error) {
  71. cmd := exec.Command(command.Command[0], command.Command[1:]...)
  72. cmd.Env = command.Env
  73. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  74. var stdout bytes.Buffer
  75. var stderr bytes.Buffer
  76. cmd.Stdout = &stdout
  77. cmd.Stderr = &stderr
  78. err := cmd.Start()
  79. return cmd, &stdout, &stderr, err
  80. }