pause_test.go 4.8 KB

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