metrics_test.go 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. "fmt"
  16. "testing"
  17. "time"
  18. "gotest.tools/v3/assert"
  19. "gotest.tools/v3/icmd"
  20. . "github.com/docker/compose-cli/utils/e2e"
  21. )
  22. func TestComposeMetrics(t *testing.T) {
  23. c := NewParallelE2eCLI(t, binDir)
  24. s := NewMetricsServer(c.MetricsSocket())
  25. s.Start()
  26. defer s.Stop()
  27. started := false
  28. for i := 0; i < 30; i++ {
  29. c.RunDockerCmd("help", "ps")
  30. if len(s.GetUsage()) > 0 {
  31. started = true
  32. fmt.Printf(" [%s] Server up in %d ms\n", t.Name(), i*100)
  33. break
  34. }
  35. time.Sleep(100 * time.Millisecond)
  36. }
  37. assert.Assert(t, started, "Metrics mock server not available after 3 secs")
  38. t.Run("catch specific failure metrics", func(t *testing.T) {
  39. s.ResetUsage()
  40. res := c.RunDockerOrExitError("compose", "-f", "../compose/fixtures/does-not-exist/compose.yml", "build")
  41. res.Assert(t, icmd.Expected{ExitCode: 14, Err: "compose/fixtures/does-not-exist/compose.yml: no such file or directory"})
  42. res = c.RunDockerOrExitError("compose", "-f", "../compose/fixtures/wrong-composefile/compose.yml", "up", "-d")
  43. res.Assert(t, icmd.Expected{ExitCode: 15, Err: "services.simple Additional property wrongField is not allowed"})
  44. res = c.RunDockerOrExitError("compose", "up")
  45. res.Assert(t, icmd.Expected{ExitCode: 14, Err: "can't find a suitable configuration file in this directory or any parent: not found"})
  46. res = c.RunDockerOrExitError("compose", "up", "-f", "../compose/fixtures/wrong-composefile/compose.yml")
  47. res.Assert(t, icmd.Expected{ExitCode: 16, Err: "unknown shorthand flag: 'f' in -f"})
  48. res = c.RunDockerOrExitError("compose", "up", "--file", "../compose/fixtures/wrong-composefile/compose.yml")
  49. res.Assert(t, icmd.Expected{ExitCode: 16, Err: "unknown flag: --file"})
  50. res = c.RunDockerOrExitError("compose", "donw", "--file", "../compose/fixtures/wrong-composefile/compose.yml")
  51. res.Assert(t, icmd.Expected{ExitCode: 16, Err: `unknown docker command: "compose donw"`})
  52. res = c.RunDockerOrExitError("compose", "--file", "../compose/fixtures/wrong-composefile/unknown-image.yml", "pull")
  53. res.Assert(t, icmd.Expected{ExitCode: 18, Err: `pull access denied for unknownimage, repository does not exist or may require 'docker login'`})
  54. res = c.RunDockerOrExitError("compose", "--file", "../compose/fixtures/wrong-composefile/build-error.yml", "build")
  55. res.Assert(t, icmd.Expected{ExitCode: 17, Err: `line 17: unknown instruction: WRONG`})
  56. res = c.RunDockerOrExitError("compose", "--file", "../compose/fixtures/wrong-composefile/build-error.yml", "up")
  57. res.Assert(t, icmd.Expected{ExitCode: 17, Err: `line 17: unknown instruction: WRONG`})
  58. res = c.RunDockerOrExitError("compose", "--file", "../compose/fixtures/wrong-composefile/unknown-image.yml", "up")
  59. res.Assert(t, icmd.Expected{ExitCode: 17, Err: `pull access denied, repository does not exist or may require authorization`})
  60. usage := s.GetUsage()
  61. assert.DeepEqual(t, []string{
  62. `{"command":"compose build","context":"moby","source":"cli","status":"failure-file-not-found"}`,
  63. `{"command":"compose up","context":"moby","source":"cli","status":"failure-compose-parse"}`,
  64. `{"command":"compose up","context":"moby","source":"cli","status":"failure-file-not-found"}`,
  65. `{"command":"compose up","context":"moby","source":"cli","status":"failure-cmd-syntax"}`,
  66. `{"command":"compose up","context":"moby","source":"cli","status":"failure-cmd-syntax"}`,
  67. `{"command":"compose","context":"moby","source":"cli","status":"failure-cmd-syntax"}`,
  68. `{"command":"compose pull","context":"moby","source":"cli","status":"failure-pull"}`,
  69. `{"command":"compose build","context":"moby","source":"cli","status":"failure-build"}`,
  70. `{"command":"compose up","context":"moby","source":"cli","status":"failure-build"}`,
  71. `{"command":"compose up","context":"moby","source":"cli","status":"failure-build"}`,
  72. }, usage)
  73. })
  74. }