pause_test.go 4.8 KB

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