From 1926820daaf2002fb304fffa000f69fcc0beda51 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Thu, 18 Mar 2021 09:45:01 -0300 Subject: [PATCH] Add e2e tests Signed-off-by: Ulysses Souza --- .../compose/fixtures/restart-test/compose.yml | 4 ++ local/e2e/compose/restart_test.go | 65 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 local/e2e/compose/fixtures/restart-test/compose.yml create mode 100644 local/e2e/compose/restart_test.go diff --git a/local/e2e/compose/fixtures/restart-test/compose.yml b/local/e2e/compose/fixtures/restart-test/compose.yml new file mode 100644 index 00000000..dd87ec20 --- /dev/null +++ b/local/e2e/compose/fixtures/restart-test/compose.yml @@ -0,0 +1,4 @@ +services: + restart: + image: busybox + command: ash -c "if [[ -f /tmp/restart.lock ]] ; then sleep infinity; else touch /tmp/restart.lock; fi" diff --git a/local/e2e/compose/restart_test.go b/local/e2e/compose/restart_test.go new file mode 100644 index 00000000..3bc30eef --- /dev/null +++ b/local/e2e/compose/restart_test.go @@ -0,0 +1,65 @@ +/* + Copyright 2020 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package e2e + +import ( + "fmt" + "strings" + "testing" + "time" + + testify "github.com/stretchr/testify/assert" + "gotest.tools/v3/assert" + + . "github.com/docker/compose-cli/utils/e2e" +) + +func TestRestart(t *testing.T) { + c := NewParallelE2eCLI(t, binDir) + const projectName = "e2e-restart" + + getServiceRegx := func(service string, status string) string { + // match output with random spaces like: + // e2e-start-stop_db_1 db running + return fmt.Sprintf("%s_%s_1\\s+%s\\s+%s", projectName, service, service, status) + } + + t.Run("Up a project", func(t *testing.T) { + // This is just to ensure the containers do NOT exist + c.RunDockerOrExitError("compose", "--project-name", projectName, "down") + + res := c.RunDockerOrExitError("compose", "-f", "./fixtures/restart-test/compose.yml", "--project-name", projectName, "up", "-d") + assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-restart_restart_1 Started"), res.Combined()) + + // Give the time for it to exit + time.Sleep(time.Second) + + res = c.RunDockerOrExitError("compose", "--project-name", projectName, "ps", "-a") + testify.Regexp(t, getServiceRegx("restart", "exited"), res.Stdout()) + + _ = c.RunDockerOrExitError("compose", "-f", "./fixtures/restart-test/compose.yml", "--project-name", projectName, "restart") + + // Give the same time but it must NOT exit + time.Sleep(time.Second) + + res = c.RunDockerOrExitError("compose", "--project-name", projectName, "ps") + testify.Regexp(t, getServiceRegx("restart", "running"), res.Stdout()) + + // Clean up + c.RunDockerOrExitError("compose", "--project-name", projectName, "down") + }) +}