cucumber_test.go 3.5 KB

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