pause_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "gotest.tools/v3/assert"
  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. assert.Assert(t, err != nil, "a should no longer respond")
  55. var netErr net.Error
  56. assert.ErrorType(t, err, &netErr, "expected a network error")
  57. errors.As(err, &netErr)
  58. assert.Assert(t, netErr.Timeout(), "Error should have indicated a timeout")
  59. HTTPGetWithRetry(t, urls["b"], http.StatusOK, 50*time.Millisecond, 5*time.Second)
  60. // unpause a and verify that both containers work again
  61. cli.RunDockerComposeCmd(t, "unpause", "a")
  62. for _, url := range urls {
  63. HTTPGetWithRetry(t, url, http.StatusOK, 50*time.Millisecond, 5*time.Second)
  64. }
  65. }
  66. func TestPauseServiceNotRunning(t *testing.T) {
  67. cli := NewParallelCLI(t, WithEnv(
  68. "COMPOSE_PROJECT_NAME=e2e-pause-svc-not-running",
  69. "COMPOSE_FILE=./fixtures/pause/compose.yaml"))
  70. cleanup := func() {
  71. cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0")
  72. }
  73. cleanup()
  74. t.Cleanup(cleanup)
  75. // pause a and verify that it can no longer be hit but b still can
  76. res := cli.RunDockerComposeCmdNoCheck(t, "pause", "a")
  77. // TODO: `docker pause` errors in this case, should Compose be consistent?
  78. res.Assert(t, icmd.Expected{ExitCode: 0})
  79. }
  80. func TestPauseServiceAlreadyPaused(t *testing.T) {
  81. cli := NewParallelCLI(t, WithEnv(
  82. "COMPOSE_PROJECT_NAME=e2e-pause-svc-already-paused",
  83. "COMPOSE_FILE=./fixtures/pause/compose.yaml"))
  84. cleanup := func() {
  85. cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0")
  86. }
  87. cleanup()
  88. t.Cleanup(cleanup)
  89. // launch a and wait for it to come up
  90. cli.RunDockerComposeCmd(t, "up", "--menu=false", "--wait", "a")
  91. HTTPGetWithRetry(t, urlForService(t, cli, "a", 80), http.StatusOK, 50*time.Millisecond, 10*time.Second)
  92. // pause a twice - first time should pass, second time fail
  93. cli.RunDockerComposeCmd(t, "pause", "a")
  94. res := cli.RunDockerComposeCmdNoCheck(t, "pause", "a")
  95. res.Assert(t, icmd.Expected{ExitCode: 1, Err: "already paused"})
  96. }
  97. func TestPauseServiceDoesNotExist(t *testing.T) {
  98. cli := NewParallelCLI(t, WithEnv(
  99. "COMPOSE_PROJECT_NAME=e2e-pause-svc-not-exist",
  100. "COMPOSE_FILE=./fixtures/pause/compose.yaml"))
  101. cleanup := func() {
  102. cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0")
  103. }
  104. cleanup()
  105. t.Cleanup(cleanup)
  106. // pause a and verify that it can no longer be hit but b still can
  107. res := cli.RunDockerComposeCmdNoCheck(t, "pause", "does_not_exist")
  108. // TODO: `compose down does_not_exist` and similar error, this should too
  109. res.Assert(t, icmd.Expected{ExitCode: 0})
  110. }
  111. func urlForService(t testing.TB, cli *CLI, service string, targetPort int) string {
  112. t.Helper()
  113. return fmt.Sprintf(
  114. "http://localhost:%d",
  115. publishedPortForService(t, cli, service, targetPort),
  116. )
  117. }
  118. func publishedPortForService(t testing.TB, cli *CLI, service string, targetPort int) int {
  119. t.Helper()
  120. res := cli.RunDockerComposeCmd(t, "ps", "--format=json", service)
  121. var svc struct {
  122. Publishers []struct {
  123. TargetPort int
  124. PublishedPort int
  125. }
  126. }
  127. assert.NilError(t, json.Unmarshal([]byte(res.Stdout()), &svc),
  128. "Failed to parse `%s` output", res.Cmd.String())
  129. var found bool
  130. var port int
  131. for _, pp := range svc.Publishers {
  132. if pp.TargetPort == targetPort {
  133. found = true
  134. port = pp.PublishedPort
  135. }
  136. }
  137. assert.Assert(t, found, "No published port for target port %d\nService: %s", targetPort, res.Combined())
  138. return port
  139. }