|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package e2e |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "net" |
| 23 | + "net/http" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + "gotest.tools/v3/icmd" |
| 29 | +) |
| 30 | + |
| 31 | +func TestPause(t *testing.T) { |
| 32 | + cli := NewParallelCLI(t, WithEnv( |
| 33 | + "COMPOSE_PROJECT_NAME=e2e-pause", |
| 34 | + "COMPOSE_FILE=./fixtures/pause/compose.yaml")) |
| 35 | + |
| 36 | + cleanup := func() { |
| 37 | + cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0") |
| 38 | + } |
| 39 | + cleanup() |
| 40 | + t.Cleanup(cleanup) |
| 41 | + |
| 42 | + // launch both services and verify that they are accessible |
| 43 | + cli.RunDockerComposeCmd(t, "up", "-d") |
| 44 | + urls := map[string]string{ |
| 45 | + "a": urlForService(t, cli, "a", 80), |
| 46 | + "b": urlForService(t, cli, "b", 80), |
| 47 | + } |
| 48 | + for _, url := range urls { |
| 49 | + HTTPGetWithRetry(t, url, http.StatusOK, 50*time.Millisecond, 5*time.Second) |
| 50 | + } |
| 51 | + |
| 52 | + // pause a and verify that it can no longer be hit but b still can |
| 53 | + cli.RunDockerComposeCmd(t, "pause", "a") |
| 54 | + httpClient := http.Client{Timeout: 250 * time.Millisecond} |
| 55 | + resp, err := httpClient.Get(urls["a"]) |
| 56 | + if resp != nil { |
| 57 | + _ = resp.Body.Close() |
| 58 | + } |
| 59 | + require.Error(t, err, "a should no longer respond") |
| 60 | + require.True(t, err.(net.Error).Timeout(), "Error should have indicated a timeout") |
| 61 | + HTTPGetWithRetry(t, urls["b"], http.StatusOK, 50*time.Millisecond, 5*time.Second) |
| 62 | + |
| 63 | + // unpause a and verify that both containers work again |
| 64 | + cli.RunDockerComposeCmd(t, "unpause", "a") |
| 65 | + for _, url := range urls { |
| 66 | + HTTPGetWithRetry(t, url, http.StatusOK, 50*time.Millisecond, 5*time.Second) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestPauseServiceNotRunning(t *testing.T) { |
| 71 | + cli := NewParallelCLI(t, WithEnv( |
| 72 | + "COMPOSE_PROJECT_NAME=e2e-pause-svc-not-running", |
| 73 | + "COMPOSE_FILE=./fixtures/pause/compose.yaml")) |
| 74 | + |
| 75 | + cleanup := func() { |
| 76 | + cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0") |
| 77 | + } |
| 78 | + cleanup() |
| 79 | + t.Cleanup(cleanup) |
| 80 | + |
| 81 | + // pause a and verify that it can no longer be hit but b still can |
| 82 | + res := cli.RunDockerComposeCmdNoCheck(t, "pause", "a") |
| 83 | + |
| 84 | + // TODO: `docker pause` errors in this case, should Compose be consistent? |
| 85 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 86 | +} |
| 87 | + |
| 88 | +func TestPauseServiceAlreadyPaused(t *testing.T) { |
| 89 | + cli := NewParallelCLI(t, WithEnv( |
| 90 | + "COMPOSE_PROJECT_NAME=e2e-pause-svc-already-paused", |
| 91 | + "COMPOSE_FILE=./fixtures/pause/compose.yaml")) |
| 92 | + |
| 93 | + cleanup := func() { |
| 94 | + cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0") |
| 95 | + } |
| 96 | + cleanup() |
| 97 | + t.Cleanup(cleanup) |
| 98 | + |
| 99 | + // launch a and wait for it to come up |
| 100 | + cli.RunDockerComposeCmd(t, "up", "-d", "a") |
| 101 | + HTTPGetWithRetry(t, urlForService(t, cli, "a", 80), http.StatusOK, 50*time.Millisecond, 5*time.Second) |
| 102 | + |
| 103 | + // pause a twice - first time should pass, second time fail |
| 104 | + cli.RunDockerComposeCmd(t, "pause", "a") |
| 105 | + res := cli.RunDockerComposeCmdNoCheck(t, "pause", "a") |
| 106 | + res.Assert(t, icmd.Expected{ExitCode: 1, Err: "already paused"}) |
| 107 | +} |
| 108 | + |
| 109 | +func TestPauseServiceDoesNotExist(t *testing.T) { |
| 110 | + cli := NewParallelCLI(t, WithEnv( |
| 111 | + "COMPOSE_PROJECT_NAME=e2e-pause-svc-not-exist", |
| 112 | + "COMPOSE_FILE=./fixtures/pause/compose.yaml")) |
| 113 | + |
| 114 | + cleanup := func() { |
| 115 | + cli.RunDockerComposeCmd(t, "down", "-v", "--remove-orphans", "-t", "0") |
| 116 | + } |
| 117 | + cleanup() |
| 118 | + t.Cleanup(cleanup) |
| 119 | + |
| 120 | + // pause a and verify that it can no longer be hit but b still can |
| 121 | + res := cli.RunDockerComposeCmdNoCheck(t, "pause", "does_not_exist") |
| 122 | + // TODO: `compose down does_not_exist` and similar error, this should too |
| 123 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 124 | +} |
| 125 | + |
| 126 | +func urlForService(t testing.TB, cli *CLI, service string, targetPort int) string { |
| 127 | + t.Helper() |
| 128 | + return fmt.Sprintf( |
| 129 | + "http://localhost:%d", |
| 130 | + publishedPortForService(t, cli, service, targetPort), |
| 131 | + ) |
| 132 | +} |
| 133 | + |
| 134 | +func publishedPortForService(t testing.TB, cli *CLI, service string, targetPort int) int { |
| 135 | + t.Helper() |
| 136 | + res := cli.RunDockerComposeCmd(t, "ps", "--format=json", service) |
| 137 | + var psOut []struct { |
| 138 | + Publishers []struct { |
| 139 | + TargetPort int |
| 140 | + PublishedPort int |
| 141 | + } |
| 142 | + } |
| 143 | + require.NoError(t, json.Unmarshal([]byte(res.Stdout()), &psOut), |
| 144 | + "Failed to parse `%s` output", res.Cmd.String()) |
| 145 | + require.Len(t, psOut, 1, "Expected exactly 1 service") |
| 146 | + svc := psOut[0] |
| 147 | + for _, pp := range svc.Publishers { |
| 148 | + if pp.TargetPort == targetPort { |
| 149 | + return pp.PublishedPort |
| 150 | + } |
| 151 | + } |
| 152 | + require.Failf(t, "No published port for target port", |
| 153 | + "Target port: %d\nService: %s", targetPort, res.Combined()) |
| 154 | + return -1 |
| 155 | +} |
0 commit comments