2022-09-16 18:13:42 +02:00
|
|
|
//go:build !windows
|
|
|
|
// +build !windows
|
|
|
|
|
2022-01-07 18:40:59 +01:00
|
|
|
/*
|
2022-08-02 21:25:59 +02:00
|
|
|
Copyright 2022 Docker Compose CLI authors
|
2022-01-07 18:40:59 +01:00
|
|
|
|
|
|
|
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 (
|
2022-08-02 21:25:59 +02:00
|
|
|
"context"
|
2023-09-26 00:57:12 +02:00
|
|
|
"errors"
|
2022-08-02 21:25:59 +02:00
|
|
|
"os/exec"
|
2023-05-25 16:23:16 +02:00
|
|
|
"strings"
|
2022-08-02 21:25:59 +02:00
|
|
|
"syscall"
|
2022-01-07 18:40:59 +01:00
|
|
|
"testing"
|
2022-08-02 21:25:59 +02:00
|
|
|
"time"
|
2022-01-07 18:40:59 +01:00
|
|
|
|
2022-09-08 18:55:07 +02:00
|
|
|
"github.com/docker/compose/v2/pkg/utils"
|
2022-08-02 21:25:59 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-05-25 16:23:16 +02:00
|
|
|
"gotest.tools/v3/assert"
|
2022-01-07 18:40:59 +01:00
|
|
|
"gotest.tools/v3/icmd"
|
|
|
|
)
|
|
|
|
|
2022-06-30 15:52:14 +02:00
|
|
|
func TestUpServiceUnhealthy(t *testing.T) {
|
2022-06-15 21:55:58 +02:00
|
|
|
c := NewParallelCLI(t)
|
2022-01-07 18:40:59 +01:00
|
|
|
const projectName = "e2e-start-fail"
|
|
|
|
|
2022-06-15 21:32:00 +02:00
|
|
|
res := c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/start-fail/compose.yaml", "--project-name", projectName, "up", "-d")
|
2023-01-26 16:44:40 +01:00
|
|
|
res.Assert(t, icmd.Expected{ExitCode: 1, Err: `container e2e-start-fail-fail-1 is unhealthy`})
|
2022-01-07 18:40:59 +01:00
|
|
|
|
2022-06-15 21:55:58 +02:00
|
|
|
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
2022-01-07 18:40:59 +01:00
|
|
|
}
|
2022-08-02 21:25:59 +02:00
|
|
|
|
|
|
|
func TestUpDependenciesNotStopped(t *testing.T) {
|
|
|
|
c := NewParallelCLI(t, WithEnv(
|
|
|
|
"COMPOSE_PROJECT_NAME=up-deps-stop",
|
|
|
|
))
|
|
|
|
|
|
|
|
reset := func() {
|
|
|
|
c.RunDockerComposeCmdNoCheck(t, "down", "-t=0", "--remove-orphans", "-v")
|
|
|
|
}
|
|
|
|
reset()
|
|
|
|
t.Cleanup(reset)
|
|
|
|
|
|
|
|
t.Log("Launching orphan container (background)")
|
|
|
|
c.RunDockerComposeCmd(t,
|
|
|
|
"-f=./fixtures/ups-deps-stop/orphan.yaml",
|
|
|
|
"up",
|
|
|
|
"--wait",
|
|
|
|
"--detach",
|
|
|
|
"orphan",
|
|
|
|
)
|
|
|
|
RequireServiceState(t, c, "orphan", "running")
|
|
|
|
|
|
|
|
t.Log("Launching app container with implicit dependency")
|
2022-09-08 18:55:07 +02:00
|
|
|
upOut := &utils.SafeBuffer{}
|
|
|
|
testCmd := c.NewDockerComposeCmd(t,
|
|
|
|
"-f=./fixtures/ups-deps-stop/compose.yaml",
|
|
|
|
"up",
|
|
|
|
"app",
|
|
|
|
)
|
|
|
|
|
up: fix various race/deadlock conditions on exit (#10934)
If running `up` in foreground mode (i.e. not `-d`),
when exiting via `Ctrl-C`, Compose stops all the
services it launched directly as part of that `up`
command.
In one of the E2E tests (`TestUpDependenciesNotStopped`),
this was occasionally flaking because the stop
behavior was racy: the return might not block on
the stop operation because it gets added to the
error group in a goroutine. As a result, it was
possible for no services to get terminated on exit.
There were a few other related pieces here that
I uncovered and tried to fix while stressing this.
For example, the printer could cause a deadlock if
an event was sent to it after it stopped.
Also, an error group wasn't really appropriate here;
each goroutine is a different operation for printing,
signal-handling, etc. If one part fails, we don't
actually want printing to stop, for example. This has
been switched to a `multierror.Group`, which has the
same API but coalesces errors instead of canceling a
context the moment the first one fails and returning
that single error.
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2023-08-31 16:47:14 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
|
|
|
t.Cleanup(cancel)
|
2022-09-08 18:55:07 +02:00
|
|
|
|
|
|
|
cmd, err := StartWithNewGroupID(ctx, testCmd, upOut, nil)
|
2023-05-25 16:23:16 +02:00
|
|
|
assert.NilError(t, err, "Failed to run compose up")
|
2022-08-02 21:25:59 +02:00
|
|
|
|
|
|
|
t.Log("Waiting for containers to be in running state")
|
|
|
|
upOut.RequireEventuallyContains(t, "hello app")
|
|
|
|
RequireServiceState(t, c, "app", "running")
|
|
|
|
RequireServiceState(t, c, "dependency", "running")
|
|
|
|
|
|
|
|
t.Log("Simulating Ctrl-C")
|
2022-09-08 18:55:07 +02:00
|
|
|
require.NoError(t, syscall.Kill(-cmd.Process.Pid, syscall.SIGINT),
|
2022-08-02 21:25:59 +02:00
|
|
|
"Failed to send SIGINT to compose up process")
|
|
|
|
|
|
|
|
t.Log("Waiting for `compose up` to exit")
|
2022-09-08 18:55:07 +02:00
|
|
|
err = cmd.Wait()
|
2022-08-02 21:25:59 +02:00
|
|
|
if err != nil {
|
2023-09-26 00:57:12 +02:00
|
|
|
var exitErr *exec.ExitError
|
|
|
|
errors.As(err, &exitErr)
|
up: fix various race/deadlock conditions on exit (#10934)
If running `up` in foreground mode (i.e. not `-d`),
when exiting via `Ctrl-C`, Compose stops all the
services it launched directly as part of that `up`
command.
In one of the E2E tests (`TestUpDependenciesNotStopped`),
this was occasionally flaking because the stop
behavior was racy: the return might not block on
the stop operation because it gets added to the
error group in a goroutine. As a result, it was
possible for no services to get terminated on exit.
There were a few other related pieces here that
I uncovered and tried to fix while stressing this.
For example, the printer could cause a deadlock if
an event was sent to it after it stopped.
Also, an error group wasn't really appropriate here;
each goroutine is a different operation for printing,
signal-handling, etc. If one part fails, we don't
actually want printing to stop, for example. This has
been switched to a `multierror.Group`, which has the
same API but coalesces errors instead of canceling a
context the moment the first one fails and returning
that single error.
Signed-off-by: Milas Bowman <milas.bowman@docker.com>
2023-08-31 16:47:14 +02:00
|
|
|
if exitErr.ExitCode() == -1 {
|
|
|
|
t.Fatalf("`compose up` was killed: %v", err)
|
|
|
|
}
|
2022-08-02 21:25:59 +02:00
|
|
|
require.EqualValues(t, exitErr.ExitCode(), 130)
|
|
|
|
}
|
|
|
|
|
|
|
|
RequireServiceState(t, c, "app", "exited")
|
|
|
|
// dependency should still be running
|
|
|
|
RequireServiceState(t, c, "dependency", "running")
|
|
|
|
RequireServiceState(t, c, "orphan", "running")
|
|
|
|
}
|
2022-11-10 10:56:07 +01:00
|
|
|
|
|
|
|
func TestUpWithBuildDependencies(t *testing.T) {
|
|
|
|
c := NewParallelCLI(t)
|
|
|
|
|
|
|
|
t.Run("up with service using image build by an another service", func(t *testing.T) {
|
|
|
|
// ensure local test run does not reuse previously build image
|
|
|
|
c.RunDockerOrExitError(t, "rmi", "built-image-dependency")
|
|
|
|
|
|
|
|
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/dependencies",
|
|
|
|
"-f", "fixtures/dependencies/service-image-depends-on.yaml", "up", "-d")
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/dependencies",
|
|
|
|
"-f", "fixtures/dependencies/service-image-depends-on.yaml", "down", "--rmi", "all")
|
|
|
|
})
|
|
|
|
|
|
|
|
res.Assert(t, icmd.Success)
|
|
|
|
})
|
|
|
|
}
|
2022-12-19 15:38:26 +01:00
|
|
|
|
|
|
|
func TestUpWithDependencyExit(t *testing.T) {
|
|
|
|
c := NewParallelCLI(t)
|
|
|
|
|
|
|
|
t.Run("up with dependency to exit before being healthy", func(t *testing.T) {
|
|
|
|
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/dependencies",
|
|
|
|
"-f", "fixtures/dependencies/dependency-exit.yaml", "up", "-d")
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
c.RunDockerComposeCmd(t, "--project-name", "dependencies", "down")
|
|
|
|
})
|
|
|
|
|
2023-01-26 16:44:40 +01:00
|
|
|
res.Assert(t, icmd.Expected{ExitCode: 1, Err: "dependency failed to start: container dependencies-db-1 exited (1)"})
|
2022-12-19 15:38:26 +01:00
|
|
|
})
|
|
|
|
}
|
2023-05-25 16:23:16 +02:00
|
|
|
|
|
|
|
func TestScaleDoesntRecreate(t *testing.T) {
|
|
|
|
c := NewCLI(t)
|
|
|
|
const projectName = "compose-e2e-scale"
|
|
|
|
t.Cleanup(func() {
|
|
|
|
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
|
|
|
})
|
|
|
|
|
|
|
|
c.RunDockerComposeCmd(t, "-f", "fixtures/simple-composefile/compose.yaml", "--project-name", projectName, "up", "-d")
|
|
|
|
|
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "fixtures/simple-composefile/compose.yaml", "--project-name", projectName, "up", "--scale", "simple=2", "-d")
|
|
|
|
assert.Check(t, !strings.Contains(res.Combined(), "Recreated"))
|
|
|
|
|
|
|
|
}
|
2023-07-10 18:27:59 +02:00
|
|
|
|
|
|
|
func TestUpWithDependencyNotRequired(t *testing.T) {
|
|
|
|
c := NewCLI(t)
|
|
|
|
const projectName = "compose-e2e-dependency-not-required"
|
|
|
|
t.Cleanup(func() {
|
|
|
|
c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
|
|
|
|
})
|
|
|
|
|
|
|
|
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dependencies/deps-not-required.yaml", "--project-name", projectName,
|
2023-07-18 18:24:55 +02:00
|
|
|
"--profile", "not-required", "up", "-d")
|
2023-07-10 18:27:59 +02:00
|
|
|
assert.Assert(t, strings.Contains(res.Combined(), "foo"), res.Combined())
|
2023-07-18 18:24:55 +02:00
|
|
|
assert.Assert(t, strings.Contains(res.Combined(), " optional dependency \"bar\" failed to start"), res.Combined())
|
2023-07-10 18:27:59 +02:00
|
|
|
}
|