cucumber_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2022 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 cucumber
  14. import (
  15. "context"
  16. "fmt"
  17. "os"
  18. "regexp"
  19. "strings"
  20. "testing"
  21. "github.com/cucumber/godog"
  22. "github.com/cucumber/godog/colors"
  23. "github.com/docker/compose/v2/pkg/e2e"
  24. "gotest.tools/v3/icmd"
  25. )
  26. func TestCucumber(t *testing.T) {
  27. testingOptions := godog.Options{
  28. TestingT: t,
  29. Paths: []string{"./cucumber-features"},
  30. Output: colors.Colored(os.Stdout),
  31. Format: "pretty",
  32. }
  33. status := godog.TestSuite{
  34. Name: "godogs",
  35. Options: &testingOptions,
  36. ScenarioInitializer: setup,
  37. }.Run()
  38. if status == 2 {
  39. t.SkipNow()
  40. }
  41. if status != 0 {
  42. t.Fatalf("zero status code expected, %d received", status)
  43. }
  44. }
  45. func setup(s *godog.ScenarioContext) {
  46. t := s.TestingT()
  47. cli := e2e.NewCLI(t, e2e.WithEnv(
  48. fmt.Sprintf("COMPOSE_PROJECT_NAME=%s", strings.Split(t.Name(), "/")[1]),
  49. ))
  50. th := testHelper{
  51. T: t,
  52. CLI: cli,
  53. }
  54. s.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
  55. cli.RunDockerComposeCmd(t, "down", "--remove-orphans", "-v", "-t", "0")
  56. return ctx, nil
  57. })
  58. s.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
  59. cli.RunDockerComposeCmd(t, "down", "--remove-orphans", "-v", "-t", "0")
  60. return ctx, nil
  61. })
  62. s.Step(`^a compose file$`, th.setComposeFile)
  63. s.Step(`^I run "compose (.*)"$`, th.runComposeCommand)
  64. s.Step(`service "(.*)" is "(.*)"$`, th.serviceIsStatus)
  65. s.Step(`output contains "(.*)"$`, th.outputContains)
  66. s.Step(`exit code is (\d+)$`, th.exitCodeIs)
  67. }
  68. type testHelper struct {
  69. T *testing.T
  70. ComposeFile string
  71. CommandOutput string
  72. CommandExitCode int
  73. CLI *e2e.CLI
  74. }
  75. func (th *testHelper) serviceIsStatus(service, status string) error {
  76. res := th.CLI.RunDockerComposeCmd(th.T, "ps", "-a")
  77. statusRegex := fmt.Sprintf("%s\\s+%s", service, status)
  78. r, _ := regexp.Compile(statusRegex)
  79. if !r.MatchString(res.Combined()) {
  80. return fmt.Errorf("Missing/incorrect ps output:\n%s", res.Combined())
  81. }
  82. return nil
  83. }
  84. func (th *testHelper) outputContains(substring string) error {
  85. if !strings.Contains(th.CommandOutput, substring) {
  86. return fmt.Errorf("Missing output substring: %s\noutput: %s", substring, th.CommandOutput)
  87. }
  88. return nil
  89. }
  90. func (th *testHelper) exitCodeIs(exitCode int) error {
  91. if exitCode != th.CommandExitCode {
  92. return fmt.Errorf("Wrong exit code: %d expected: %d", th.CommandExitCode, exitCode)
  93. }
  94. return nil
  95. }
  96. func (th *testHelper) runComposeCommand(command string) error {
  97. commandArgs := []string{"-f", "-"}
  98. commandArgs = append(commandArgs, strings.Split(command, " ")...)
  99. cmd := th.CLI.NewDockerComposeCmd(th.T, commandArgs...)
  100. cmd.Stdin = strings.NewReader(th.ComposeFile)
  101. res := icmd.RunCmd(cmd)
  102. th.CommandOutput = res.Combined()
  103. th.CommandExitCode = res.ExitCode
  104. return nil
  105. }
  106. func (th *testHelper) setComposeFile(composeString string) error {
  107. th.ComposeFile = composeString
  108. return nil
  109. }