pause_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "encoding/json"
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "testing"
  20. "time"
  21. "github.com/stretchr/testify/require"
  22. "gotest.tools/v3/icmd"
  23. )
  24. func TestPause(t *testing.T) {
  25. cli := NewParallelCLI(t, WithEnv(
  26. "COMPOSE_PROJECT_NAME=e2e-pause",
  27. "COMPOSE_FILE=./fixtures/pause/compose.yaml"))
  28. cleanup := func() {
  29. cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0")
  30. }
  31. cleanup()
  32. t.Cleanup(cleanup)
  33. // launch both services and verify that they are accessible
  34. cli.RunDockerComposeCmd(t, "up", "-d")
  35. urls := map[string]string{
  36. "a": urlForService(t, cli, "a", 80),
  37. "b": urlForService(t, cli, "b", 80),
  38. }
  39. for _, url := range urls {
  40. HTTPGetWithRetry(t, url, http.StatusOK, 50*time.Millisecond, 5*time.Second)
  41. }
  42. // pause a and verify that it can no longer be hit but b still can
  43. cli.RunDockerComposeCmd(t, "pause", "a")
  44. httpClient := http.Client{Timeout: 250 * time.Millisecond}
  45. resp, err := httpClient.Get(urls["a"])
  46. if resp != nil {
  47. _ = resp.Body.Close()
  48. }
  49. require.Error(t, err, "a should no longer respond")
  50. require.True(t, err.(net.Error).Timeout(), "Error should have indicated a timeout")
  51. HTTPGetWithRetry(t, urls["b"], http.StatusOK, 50*time.Millisecond, 5*time.Second)
  52. // unpause a and verify that both containers work again
  53. cli.RunDockerComposeCmd(t, "unpause", "a")
  54. for _, url := range urls {
  55. HTTPGetWithRetry(t, url, http.StatusOK, 50*time.Millisecond, 5*time.Second)
  56. }
  57. }
  58. func TestPauseServiceNotRunning(t *testing.T) {
  59. cli := NewParallelCLI(t, WithEnv(
  60. "COMPOSE_PROJECT_NAME=e2e-pause-svc-not-running",
  61. "COMPOSE_FILE=./fixtures/pause/compose.yaml"))
  62. cleanup := func() {
  63. cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0")
  64. }
  65. cleanup()
  66. t.Cleanup(cleanup)
  67. // pause a and verify that it can no longer be hit but b still can
  68. res := cli.RunDockerComposeCmdNoCheck(t, "pause", "a")
  69. // TODO: `docker pause` errors in this case, should Compose be consistent?
  70. res.Assert(t, icmd.Expected{ExitCode: 0})
  71. }
  72. func TestPauseServiceAlreadyPaused(t *testing.T) {
  73. cli := NewParallelCLI(t, WithEnv(
  74. "COMPOSE_PROJECT_NAME=e2e-pause-svc-already-paused",
  75. "COMPOSE_FILE=./fixtures/pause/compose.yaml"))
  76. cleanup := func() {
  77. cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0")
  78. }
  79. cleanup()
  80. t.Cleanup(cleanup)
  81. // launch a and wait for it to come up
  82. cli.RunDockerComposeCmd(t, "up", "-d", "a")
  83. HTTPGetWithRetry(t, urlForService(t, cli, "a", 80), http.StatusOK, 50*time.Millisecond, 5*time.Second)
  84. // pause a twice - first time should pass, second time fail
  85. cli.RunDockerComposeCmd(t, "pause", "a")
  86. res := cli.RunDockerComposeCmdNoCheck(t, "pause", "a")
  87. res.Assert(t, icmd.Expected{ExitCode: 1, Err: "already paused"})
  88. }
  89. func TestPauseServiceDoesNotExist(t *testing.T) {
  90. cli := NewParallelCLI(t, WithEnv(
  91. "COMPOSE_PROJECT_NAME=e2e-pause-svc-not-exist",
  92. "COMPOSE_FILE=./fixtures/pause/compose.yaml"))
  93. cleanup := func() {
  94. cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0")
  95. }
  96. cleanup()
  97. t.Cleanup(cleanup)
  98. // pause a and verify that it can no longer be hit but b still can
  99. res := cli.RunDockerComposeCmdNoCheck(t, "pause", "does_not_exist")
  100. // TODO: `compose down does_not_exist` and similar error, this should too
  101. res.Assert(t, icmd.Expected{ExitCode: 0})
  102. }
  103. func urlForService(t testing.TB, cli *CLI, service string, targetPort int) string {
  104. t.Helper()
  105. return fmt.Sprintf(
  106. "http://localhost:%d",
  107. publishedPortForService(t, cli, service, targetPort),
  108. )
  109. }
  110. func publishedPortForService(t testing.TB, cli *CLI, service string, targetPort int) int {
  111. t.Helper()
  112. res := cli.RunDockerComposeCmd(t, "ps", "--format=json", service)
  113. var psOut []struct {
  114. Publishers []struct {
  115. TargetPort int
  116. PublishedPort int
  117. }
  118. }
  119. require.NoError(t, json.Unmarshal([]byte(res.Stdout()), &psOut),
  120. "Failed to parse `%s` output", res.Cmd.String())
  121. require.Len(t, psOut, 1, "Expected exactly 1 service")
  122. svc := psOut[0]
  123. for _, pp := range svc.Publishers {
  124. if pp.TargetPort == targetPort {
  125. return pp.PublishedPort
  126. }
  127. }
  128. require.Failf(t, "No published port for target port",
  129. "Target port: %d\nService: %s", targetPort, res.Combined())
  130. return -1
  131. }