metrics_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 e2e
  14. import (
  15. "bytes"
  16. "fmt"
  17. "os/exec"
  18. "strings"
  19. "syscall"
  20. "testing"
  21. "time"
  22. "gotest.tools/v3/assert"
  23. "gotest.tools/v3/icmd"
  24. . "github.com/docker/compose-cli/utils/e2e"
  25. )
  26. func TestComposeMetrics(t *testing.T) {
  27. c := NewParallelE2eCLI(t, binDir)
  28. s := NewMetricsServer(c.MetricsSocket())
  29. s.Start()
  30. defer s.Stop()
  31. started := false
  32. for i := 0; i < 30; i++ {
  33. c.RunDockerCmd("help", "ps")
  34. if len(s.GetUsage()) > 0 {
  35. started = true
  36. fmt.Printf(" [%s] Server up in %d ms\n", t.Name(), i*100)
  37. break
  38. }
  39. time.Sleep(100 * time.Millisecond)
  40. }
  41. assert.Assert(t, started, "Metrics mock server not available after 3 secs")
  42. t.Run("catch specific failure metrics", func(t *testing.T) {
  43. s.ResetUsage()
  44. res := c.RunDockerOrExitError("compose", "-f", "../compose/fixtures/does-not-exist/compose.yml", "build")
  45. res.Assert(t, icmd.Expected{ExitCode: 14, Err: "compose/fixtures/does-not-exist/compose.yml: no such file or directory"})
  46. res = c.RunDockerOrExitError("compose", "-f", "../compose/fixtures/wrong-composefile/compose.yml", "up", "-d")
  47. res.Assert(t, icmd.Expected{ExitCode: 15, Err: "services.simple Additional property wrongField is not allowed"})
  48. res = c.RunDockerOrExitError("compose", "up")
  49. res.Assert(t, icmd.Expected{ExitCode: 14, Err: "can't find a suitable configuration file in this directory or any parent: not found"})
  50. res = c.RunDockerOrExitError("compose", "up", "-f", "../compose/fixtures/wrong-composefile/compose.yml")
  51. res.Assert(t, icmd.Expected{ExitCode: 16, Err: "unknown shorthand flag: 'f' in -f"})
  52. res = c.RunDockerOrExitError("compose", "up", "--file", "../compose/fixtures/wrong-composefile/compose.yml")
  53. res.Assert(t, icmd.Expected{ExitCode: 16, Err: "unknown flag: --file"})
  54. res = c.RunDockerOrExitError("compose", "donw", "--file", "../compose/fixtures/wrong-composefile/compose.yml")
  55. res.Assert(t, icmd.Expected{ExitCode: 16, Err: `unknown docker command: "compose donw"`})
  56. res = c.RunDockerOrExitError("compose", "--file", "../compose/fixtures/wrong-composefile/unknown-image.yml", "pull")
  57. res.Assert(t, icmd.Expected{ExitCode: 18, Err: `pull access denied for unknownimage, repository does not exist or may require 'docker login'`})
  58. res = c.RunDockerOrExitError("compose", "--file", "../compose/fixtures/wrong-composefile/build-error.yml", "build")
  59. res.Assert(t, icmd.Expected{ExitCode: 17, Err: `line 17: unknown instruction: WRONG`})
  60. res = c.RunDockerOrExitError("compose", "--file", "../compose/fixtures/wrong-composefile/build-error.yml", "up")
  61. res.Assert(t, icmd.Expected{ExitCode: 17, Err: `line 17: unknown instruction: WRONG`})
  62. res = c.RunDockerOrExitError("compose", "--file", "../compose/fixtures/wrong-composefile/unknown-image.yml", "up")
  63. res.Assert(t, icmd.Expected{ExitCode: 17, Err: `pull access denied, repository does not exist or may require authorization`})
  64. usage := s.GetUsage()
  65. assert.DeepEqual(t, []string{
  66. `{"command":"compose build","context":"moby","source":"cli","status":"failure-file-not-found"}`,
  67. `{"command":"compose up","context":"moby","source":"cli","status":"failure-compose-parse"}`,
  68. `{"command":"compose up","context":"moby","source":"cli","status":"failure-file-not-found"}`,
  69. `{"command":"compose up","context":"moby","source":"cli","status":"failure-cmd-syntax"}`,
  70. `{"command":"compose up","context":"moby","source":"cli","status":"failure-cmd-syntax"}`,
  71. `{"command":"compose","context":"moby","source":"cli","status":"failure-cmd-syntax"}`,
  72. `{"command":"compose pull","context":"moby","source":"cli","status":"failure-pull"}`,
  73. `{"command":"compose build","context":"moby","source":"cli","status":"failure-build"}`,
  74. `{"command":"compose up","context":"moby","source":"cli","status":"failure-build"}`,
  75. `{"command":"compose up","context":"moby","source":"cli","status":"failure-build"}`,
  76. }, usage)
  77. })
  78. }
  79. func TestComposeCancel(t *testing.T) {
  80. c := NewParallelE2eCLI(t, binDir)
  81. s := NewMetricsServer(c.MetricsSocket())
  82. s.Start()
  83. defer s.Stop()
  84. started := false
  85. for i := 0; i < 30; i++ {
  86. c.RunDockerCmd("help", "ps")
  87. if len(s.GetUsage()) > 0 {
  88. started = true
  89. fmt.Printf(" [%s] Server up in %d ms\n", t.Name(), i*100)
  90. break
  91. }
  92. time.Sleep(100 * time.Millisecond)
  93. }
  94. assert.Assert(t, started, "Metrics mock server not available after 3 secs")
  95. t.Run("metrics on cancel Compose build", func(t *testing.T) {
  96. s.ResetUsage()
  97. c.RunDockerCmd("compose", "ls")
  98. buildProjectPath := "../compose/fixtures/build-infinite/docker-compose.yml"
  99. // require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal.
  100. // sending kill signal
  101. cmd, stdout, stderr, err := StartWithNewGroupID(c.NewDockerCmd("compose", "-f", buildProjectPath, "build", "--progress", "plain"))
  102. assert.NilError(t, err)
  103. c.WaitForCondition(func() (bool, string) {
  104. out := stdout.String()
  105. errors := stderr.String()
  106. return strings.Contains(out, "RUN sleep infinity"), fmt.Sprintf("'RUN sleep infinity' not found in : \n%s\nStderr: \n%s\n", out, errors)
  107. }, 30*time.Second, 1*time.Second)
  108. err = syscall.Kill(-cmd.Process.Pid, syscall.SIGINT) // simulate Ctrl-C : send signal to processGroup, children will have same groupId by default
  109. assert.NilError(t, err)
  110. c.WaitForCondition(func() (bool, string) {
  111. out := stdout.String()
  112. errors := stderr.String()
  113. return strings.Contains(out, "CANCELED"), fmt.Sprintf("'CANCELED' not found in : \n%s\nStderr: \n%s\n", out, errors)
  114. }, 10*time.Second, 1*time.Second)
  115. usage := s.GetUsage()
  116. assert.DeepEqual(t, []string{
  117. `{"command":"compose ls","context":"moby","source":"cli","status":"success"}`,
  118. `{"command":"compose build","context":"moby","source":"cli","status":"canceled"}`,
  119. }, usage)
  120. })
  121. }
  122. func StartWithNewGroupID(command icmd.Cmd) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer, error) {
  123. cmd := exec.Command(command.Command[0], command.Command[1:]...)
  124. cmd.Env = command.Env
  125. cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
  126. var stdout bytes.Buffer
  127. var stderr bytes.Buffer
  128. cmd.Stdout = &stdout
  129. cmd.Stderr = &stderr
  130. err := cmd.Start()
  131. return cmd, &stdout, &stderr, err
  132. }