|
| 1 | +/* |
| 2 | +Copyright 2023 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 | +package e2e |
| 17 | + |
| 18 | +import ( |
| 19 | + "gotest.tools/v3/assert" |
| 20 | + "gotest.tools/v3/icmd" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | +) |
| 24 | + |
| 25 | +func TestPostStartHookInError(t *testing.T) { |
| 26 | + c := NewParallelCLI(t) |
| 27 | + const projectName = "hooks-post-start-failure" |
| 28 | + |
| 29 | + t.Cleanup(func() { |
| 30 | + c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "-v", "--remove-orphans", "-t", "0") |
| 31 | + }) |
| 32 | + |
| 33 | + res := c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/hooks/poststart/compose-error.yaml", "--project-name", projectName, "up", "-d") |
| 34 | + res.Assert(t, icmd.Expected{ExitCode: 1}) |
| 35 | + assert.Assert(t, strings.Contains(res.Combined(), "Error response from daemon: container"), res.Combined()) |
| 36 | + assert.Assert(t, strings.Contains(res.Combined(), "is not running"), res.Combined()) |
| 37 | +} |
| 38 | + |
| 39 | +func TestPostStartHookSuccess(t *testing.T) { |
| 40 | + c := NewParallelCLI(t) |
| 41 | + const projectName = "hooks-post-start-success" |
| 42 | + |
| 43 | + t.Cleanup(func() { |
| 44 | + c.RunDockerComposeCmd(t, "--project-name", projectName, "down", "-v", "--remove-orphans", "-t", "0") |
| 45 | + }) |
| 46 | + |
| 47 | + res := c.RunDockerComposeCmd(t, "-f", "fixtures/hooks/poststart/compose-success.yaml", "--project-name", projectName, "up", "-d") |
| 48 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 49 | +} |
| 50 | + |
| 51 | +func TestPreStopHookSuccess(t *testing.T) { |
| 52 | + c := NewParallelCLI(t) |
| 53 | + const projectName = "hooks-pre-stop-success" |
| 54 | + |
| 55 | + t.Cleanup(func() { |
| 56 | + c.RunDockerComposeCmd(t, "-f", "fixtures/hooks/prestop/compose-success.yaml", "--project-name", projectName, "down", "-v", "--remove-orphans", "-t", "0") |
| 57 | + }) |
| 58 | + |
| 59 | + res := c.RunDockerComposeCmd(t, "-f", "fixtures/hooks/prestop/compose-success.yaml", "--project-name", projectName, "up", "-d") |
| 60 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 61 | + |
| 62 | + res = c.RunDockerComposeCmd(t, "-f", "fixtures/hooks/prestop/compose-success.yaml", "--project-name", projectName, "down", "-v", "--remove-orphans", "-t", "0") |
| 63 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 64 | +} |
| 65 | + |
| 66 | +func TestPreStopHookInError(t *testing.T) { |
| 67 | + c := NewParallelCLI(t) |
| 68 | + const projectName = "hooks-pre-stop-failure" |
| 69 | + |
| 70 | + t.Cleanup(func() { |
| 71 | + c.RunDockerComposeCmd(t, "-f", "fixtures/hooks/prestop/compose-success.yaml", "--project-name", projectName, "down", "-v", "--remove-orphans", "-t", "0") |
| 72 | + }) |
| 73 | + |
| 74 | + res := c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/hooks/prestop/compose-error.yaml", "--project-name", projectName, "up", "-d") |
| 75 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 76 | + |
| 77 | + res = c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/hooks/prestop/compose-error.yaml", "--project-name", projectName, "down", "-v", "--remove-orphans", "-t", "0") |
| 78 | + res.Assert(t, icmd.Expected{ExitCode: 1}) |
| 79 | + assert.Assert(t, strings.Contains(res.Combined(), "sample hook exited with status 127")) |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +func TestPreStopHookSuccessWithPreviousStop(t *testing.T) { |
| 84 | + c := NewParallelCLI(t) |
| 85 | + const projectName = "hooks-pre-stop-success-with-previous-stop" |
| 86 | + |
| 87 | + t.Cleanup(func() { |
| 88 | + res := c.RunDockerComposeCmd(t, "-f", "fixtures/hooks/compose.yaml", "--project-name", projectName, "down", "-v", "--remove-orphans", "-t", "0") |
| 89 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 90 | + }) |
| 91 | + |
| 92 | + res := c.RunDockerComposeCmd(t, "-f", "fixtures/hooks/compose.yaml", "--project-name", projectName, "up", "-d") |
| 93 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 94 | + |
| 95 | + res = c.RunDockerComposeCmd(t, "-f", "fixtures/hooks/compose.yaml", "--project-name", projectName, "stop", "sample") |
| 96 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 97 | +} |
| 98 | + |
| 99 | +func TestPostStartAndPreStopHook(t *testing.T) { |
| 100 | + c := NewParallelCLI(t) |
| 101 | + const projectName = "hooks-post-start-and-pre-stop" |
| 102 | + |
| 103 | + t.Cleanup(func() { |
| 104 | + res := c.RunDockerComposeCmd(t, "-f", "fixtures/hooks/compose.yaml", "--project-name", projectName, "down", "-v", "--remove-orphans", "-t", "0") |
| 105 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 106 | + }) |
| 107 | + |
| 108 | + res := c.RunDockerComposeCmd(t, "-f", "fixtures/hooks/compose.yaml", "--project-name", projectName, "up", "-d") |
| 109 | + res.Assert(t, icmd.Expected{ExitCode: 0}) |
| 110 | +} |
0 commit comments