restart_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 TestRestart(t *testing.T) {
  23. c := NewParallelE2eCLI(t, binDir)
  24. const projectName = "e2e-restart"
  25. getServiceRegx := func(service string, status string) string {
  26. // match output with random spaces like:
  27. // e2e-start-stop-db-1 "echo hello" db running
  28. return fmt.Sprintf("%s-%s-1.+%s\\s+%s", projectName, service, service, status)
  29. }
  30. t.Run("Up a project", func(t *testing.T) {
  31. // This is just to ensure the containers do NOT exist
  32. c.RunDockerOrExitError("compose", "--project-name", projectName, "down")
  33. res := c.RunDockerOrExitError("compose", "-f", "./fixtures/restart-test/compose.yaml", "--project-name", projectName, "up", "-d")
  34. assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-restart-restart-1 Started"), res.Combined())
  35. c.WaitForCmdResult(c.NewDockerCmd("compose", "--project-name", projectName, "ps", "-a", "--format", "json"),
  36. StdoutContains(`"State":"exited"`),
  37. 10*time.Second, 1*time.Second)
  38. res = c.RunDockerOrExitError("compose", "--project-name", projectName, "ps", "-a")
  39. testify.Regexp(t, getServiceRegx("restart", "exited"), res.Stdout())
  40. _ = c.RunDockerOrExitError("compose", "-f", "./fixtures/restart-test/compose.yaml", "--project-name", projectName, "restart")
  41. // Give the same time but it must NOT exit
  42. time.Sleep(time.Second)
  43. res = c.RunDockerOrExitError("compose", "--project-name", projectName, "ps")
  44. testify.Regexp(t, getServiceRegx("restart", "running"), res.Stdout())
  45. // Clean up
  46. c.RunDockerOrExitError("compose", "--project-name", projectName, "down")
  47. })
  48. }