start_stop_test.go 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. "strings"
  17. "testing"
  18. testify "github.com/stretchr/testify/assert"
  19. "gotest.tools/v3/assert"
  20. )
  21. func TestStartStop(t *testing.T) {
  22. c := NewParallelE2eCLI(t, binDir)
  23. const projectName = "e2e-start-stop"
  24. getProjectRegx := func(status string) string {
  25. // match output with random spaces like:
  26. // e2e-start-stop running(3)
  27. return fmt.Sprintf("%s\\s+%s\\(%d\\)", projectName, status, 2)
  28. }
  29. getServiceRegx := func(service string, status string) string {
  30. // match output with random spaces like:
  31. // e2e-start-stop-db-1 "echo hello" db running
  32. return fmt.Sprintf("%s-%s-1.+%s\\s+%s", projectName, service, service, status)
  33. }
  34. t.Run("Up a project", func(t *testing.T) {
  35. res := c.RunDockerCmd("compose", "-f", "./fixtures/start-stop/compose.yaml", "--project-name", projectName, "up", "-d")
  36. assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-start-stop-simple-1 Started"), res.Combined())
  37. res = c.RunDockerCmd("compose", "ls", "--all")
  38. testify.Regexp(t, getProjectRegx("running"), res.Stdout())
  39. res = c.RunDockerCmd("compose", "--project-name", projectName, "ps")
  40. testify.Regexp(t, getServiceRegx("simple", "running"), res.Stdout())
  41. testify.Regexp(t, getServiceRegx("another", "running"), res.Stdout())
  42. })
  43. t.Run("stop project", func(t *testing.T) {
  44. c.RunDockerCmd("compose", "-f", "./fixtures/start-stop/compose.yaml", "--project-name", projectName, "stop")
  45. res := c.RunDockerCmd("compose", "ls")
  46. assert.Assert(t, !strings.Contains(res.Combined(), "e2e-start-stop"), res.Combined())
  47. res = c.RunDockerCmd("compose", "ls", "--all")
  48. testify.Regexp(t, getProjectRegx("exited"), res.Stdout())
  49. res = c.RunDockerCmd("compose", "--project-name", projectName, "ps")
  50. assert.Assert(t, !strings.Contains(res.Combined(), "e2e-start-stop-words-1"), res.Combined())
  51. res = c.RunDockerCmd("compose", "--project-name", projectName, "ps", "--all")
  52. testify.Regexp(t, getServiceRegx("simple", "exited"), res.Stdout())
  53. testify.Regexp(t, getServiceRegx("another", "exited"), res.Stdout())
  54. })
  55. t.Run("start project", func(t *testing.T) {
  56. c.RunDockerCmd("compose", "-f", "./fixtures/start-stop/compose.yaml", "--project-name", projectName, "start")
  57. res := c.RunDockerCmd("compose", "ls")
  58. testify.Regexp(t, getProjectRegx("running"), res.Stdout())
  59. })
  60. t.Run("pause project", func(t *testing.T) {
  61. c.RunDockerCmd("compose", "-f", "./fixtures/start-stop/compose.yaml", "--project-name", projectName, "pause")
  62. res := c.RunDockerCmd("compose", "ls", "--all")
  63. testify.Regexp(t, getProjectRegx("paused"), res.Stdout())
  64. })
  65. t.Run("unpause project", func(t *testing.T) {
  66. c.RunDockerCmd("compose", "-f", "./fixtures/start-stop/compose.yaml", "--project-name", projectName, "unpause")
  67. res := c.RunDockerCmd("compose", "ls")
  68. testify.Regexp(t, getProjectRegx("running"), res.Stdout())
  69. })
  70. t.Run("down", func(t *testing.T) {
  71. _ = c.RunDockerCmd("compose", "--project-name", projectName, "down")
  72. })
  73. }