restart_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "time"
  19. testify "github.com/stretchr/testify/assert"
  20. "gotest.tools/v3/assert"
  21. )
  22. func assertServiceStatus(t *testing.T, projectName, service, status string, ps string) {
  23. // match output with random spaces like:
  24. // e2e-start-stop-db-1 alpine:latest "echo hello" db 1 minutes ago Exited (0) 1 minutes ago
  25. regx := fmt.Sprintf("%s-%s-1.+%s\\s+.+%s.+", projectName, service, service, status)
  26. testify.Regexp(t, regx, ps)
  27. }
  28. func TestRestart(t *testing.T) {
  29. c := NewParallelCLI(t)
  30. const projectName = "e2e-restart"
  31. t.Run("Up a project", func(t *testing.T) {
  32. // This is just to ensure the containers do NOT exist
  33. c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  34. res := c.RunDockerComposeCmd(t, "-f", "./fixtures/restart-test/compose.yaml", "--project-name", projectName, "up", "-d")
  35. assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-restart-restart-1 Started"), res.Combined())
  36. c.WaitForCmdResult(t, c.NewDockerComposeCmd(t, "--project-name", projectName, "ps", "-a", "--format",
  37. "json"),
  38. StdoutContains(`"State":"exited"`), 10*time.Second, 1*time.Second)
  39. res = c.RunDockerComposeCmd(t, "--project-name", projectName, "ps", "-a")
  40. assertServiceStatus(t, projectName, "restart", "Exited", res.Stdout())
  41. c.RunDockerComposeCmd(t, "-f", "./fixtures/restart-test/compose.yaml", "--project-name", projectName, "restart")
  42. // Give the same time but it must NOT exit
  43. time.Sleep(time.Second)
  44. res = c.RunDockerComposeCmd(t, "--project-name", projectName, "ps")
  45. assertServiceStatus(t, projectName, "restart", "Up", res.Stdout())
  46. // Clean up
  47. c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
  48. })
  49. }
  50. func TestRestartWithDependencies(t *testing.T) {
  51. c := NewParallelCLI(t, WithEnv(
  52. "COMPOSE_PROJECT_NAME=e2e-restart-deps",
  53. ))
  54. baseService := "nginx"
  55. depWithRestart := "with-restart"
  56. depNoRestart := "no-restart"
  57. t.Cleanup(func() {
  58. c.RunDockerComposeCmd(t, "down", "--remove-orphans")
  59. })
  60. c.RunDockerComposeCmd(t, "-f", "./fixtures/restart-test/compose-depends-on.yaml", "up", "-d")
  61. res := c.RunDockerComposeCmd(t, "restart", baseService)
  62. fmt.Println(res.Combined())
  63. assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf("Container e2e-restart-deps-%s-1 Started", baseService)), res.Combined())
  64. assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf("Container e2e-restart-deps-%s-1 Started", depWithRestart)), res.Combined())
  65. assert.Assert(t, !strings.Contains(res.Combined(), depNoRestart), res.Combined())
  66. }
  67. func TestRestartWithProfiles(t *testing.T) {
  68. c := NewParallelCLI(t, WithEnv(
  69. "COMPOSE_PROJECT_NAME=e2e-restart-profiles",
  70. ))
  71. t.Cleanup(func() {
  72. c.RunDockerComposeCmd(t, "down", "--remove-orphans")
  73. })
  74. c.RunDockerComposeCmd(t, "-f", "./fixtures/restart-test/compose.yaml", "--profile", "test", "up", "-d")
  75. res := c.RunDockerComposeCmd(t, "restart", "test")
  76. fmt.Println(res.Combined())
  77. assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-restart-profiles-test-1 Started"), res.Combined())
  78. }