cucumber_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "path/filepath"
  19. "regexp"
  20. "strings"
  21. "testing"
  22. "github.com/cucumber/godog"
  23. "github.com/cucumber/godog/colors"
  24. "github.com/mattn/go-shellwords"
  25. "gotest.tools/v3/icmd"
  26. "github.com/docker/compose/v2/pkg/e2e"
  27. )
  28. func TestCucumber(t *testing.T) {
  29. testingOptions := godog.Options{
  30. TestingT: t,
  31. Paths: []string{"./cucumber-features"},
  32. Output: colors.Colored(os.Stdout),
  33. Format: "pretty",
  34. }
  35. status := godog.TestSuite{
  36. Name: "godogs",
  37. Options: &testingOptions,
  38. ScenarioInitializer: setup,
  39. }.Run()
  40. if status == 2 {
  41. t.SkipNow()
  42. }
  43. if status != 0 {
  44. t.Fatalf("zero status code expected, %d received", status)
  45. }
  46. }
  47. func setup(s *godog.ScenarioContext) {
  48. t := s.TestingT()
  49. projectName := strings.Split(t.Name(), "/")[1]
  50. cli := e2e.NewCLI(t, e2e.WithEnv(
  51. fmt.Sprintf("COMPOSE_PROJECT_NAME=%s", projectName),
  52. ))
  53. th := testHelper{
  54. T: t,
  55. CLI: cli,
  56. ProjectName: projectName,
  57. }
  58. s.Before(func(ctx context.Context, sc *godog.Scenario) (context.Context, error) {
  59. cli.RunDockerComposeCmd(t, "down", "--remove-orphans", "-v", "-t", "0")
  60. return ctx, nil
  61. })
  62. s.After(func(ctx context.Context, sc *godog.Scenario, err error) (context.Context, error) {
  63. cli.RunDockerComposeCmd(t, "down", "--remove-orphans", "-v", "-t", "0")
  64. return ctx, nil
  65. })
  66. s.Step(`^a compose file$`, th.setComposeFile)
  67. s.Step(`^a dockerfile$`, th.setDockerfile)
  68. s.Step(`^I run "compose (.*)"$`, th.runComposeCommand)
  69. s.Step(`^I run "docker (.*)"$`, th.runDockerCommand)
  70. s.Step(`service "(.*)" is "(.*)"$`, th.serviceIsStatus)
  71. s.Step(`output contains "(.*)"$`, th.outputContains(true))
  72. s.Step(`output does not contain "(.*)"$`, th.outputContains(false))
  73. s.Step(`exit code is (\d+)$`, th.exitCodeIs)
  74. }
  75. type testHelper struct {
  76. T *testing.T
  77. ProjectName string
  78. ComposeFile string
  79. TestDir string
  80. CommandOutput string
  81. CommandExitCode int
  82. CLI *e2e.CLI
  83. }
  84. func (th *testHelper) serviceIsStatus(service, status string) error {
  85. serviceContainerName := fmt.Sprintf("%s-%s-1", strings.ToLower(th.ProjectName), service)
  86. statusRegex := fmt.Sprintf("%s.*%s", serviceContainerName, status)
  87. res := th.CLI.RunDockerComposeCmd(th.T, "ps", "-a")
  88. r, _ := regexp.Compile(statusRegex)
  89. if !r.MatchString(res.Combined()) {
  90. return fmt.Errorf("Missing/incorrect ps output:\n%s\nregex:\n%s", res.Combined(), statusRegex)
  91. }
  92. return nil
  93. }
  94. func (th *testHelper) outputContains(expected bool) func(string) error {
  95. return func(substring string) error {
  96. contains := strings.Contains(th.CommandOutput, substring)
  97. if contains && !expected {
  98. return fmt.Errorf("Unexpected substring in output: %s\noutput: %s", substring, th.CommandOutput)
  99. } else if !contains && expected {
  100. return fmt.Errorf("Missing substring in output: %s\noutput: %s", substring, th.CommandOutput)
  101. }
  102. return nil
  103. }
  104. }
  105. func (th *testHelper) exitCodeIs(exitCode int) error {
  106. if exitCode != th.CommandExitCode {
  107. return fmt.Errorf("Wrong exit code: %d expected: %d || command output: %s", th.CommandExitCode, exitCode, th.CommandOutput)
  108. }
  109. return nil
  110. }
  111. func (th *testHelper) runComposeCommand(command string) error {
  112. commandArgs, err := shellwords.Parse(command)
  113. if err != nil {
  114. return err
  115. }
  116. commandArgs = append([]string{"-f", "-"}, commandArgs...)
  117. cmd := th.CLI.NewDockerComposeCmd(th.T, commandArgs...)
  118. cmd.Stdin = strings.NewReader(th.ComposeFile)
  119. cmd.Dir = th.TestDir
  120. res := icmd.RunCmd(cmd)
  121. th.CommandOutput = res.Combined()
  122. th.CommandExitCode = res.ExitCode
  123. return nil
  124. }
  125. func (th *testHelper) runDockerCommand(command string) error {
  126. commandArgs, err := shellwords.Parse(command)
  127. if err != nil {
  128. return err
  129. }
  130. cmd := th.CLI.NewDockerCmd(th.T, commandArgs...)
  131. cmd.Dir = th.TestDir
  132. res := icmd.RunCmd(cmd)
  133. th.CommandOutput = res.Combined()
  134. th.CommandExitCode = res.ExitCode
  135. return nil
  136. }
  137. func (th *testHelper) setComposeFile(composeString string) error {
  138. th.ComposeFile = composeString
  139. return nil
  140. }
  141. func (th *testHelper) setDockerfile(dockerfileString string) error {
  142. tempDir := th.T.TempDir()
  143. th.TestDir = tempDir
  144. err := os.WriteFile(filepath.Join(tempDir, "Dockerfile"), []byte(dockerfileString), 0o644)
  145. if err != nil {
  146. return err
  147. }
  148. return nil
  149. }