cucumber_test.go 5.1 KB

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