compose/pkg/e2e/up_interactive_test.go
Nicolas De Loof 82d8bcb288 fix(use_api_socket): hand the registry credentials over like docker run does
Align the credentials use_api_socket gives a service with what
`docker run --use-api-socket` does in the Docker CLI:

- the config file is created with mode 0600 instead of the 0444 default
  of a config file, so that only the container's root reads it;
- a service which sets DOCKER_CONFIG itself gets the socket but no
  credentials, as the comment of the function always said;
- nothing is injected, and DOCKER_CONFIG is not set, when the host has no
  registry credentials, and they are not looked up when no service is to
  get them.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
2026-11-02 15:41:36 +01:00

131 lines
5.9 KiB
Go

//go:build e2e
/*
Copyright 2026 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 (
"testing"
"time"
)
// Scenarios locking the observable behavior of the foreground (non-detached)
// `compose up`. Most e2e coverage of the start phase runs detached.
func TestUpInteractiveHookLogs(t *testing.T) {
NewScenario(t, "a foreground up must run the hooks around the service and stream their output into its session").
Step("up shows both hooks' output, and the service reads what pre_start wrote before it started",
ComposeCmd("up", "--menu=false").Within(120*time.Second),
StdoutContains("pre-start-hook-stdout"),
StdoutContains("app-read:pre-start-file-content"),
StdoutContains("post-start-hook-ran"),
ServiceState("app", "exited"))
}
func TestUpInteractiveIgnoresHookRunnerExit(t *testing.T) {
NewScenario(t, "a foreground up must not take a pre_start runner's exit for the application's").
Step("up reports the exit code of the service, not the successful exit of its hook",
ComposeCmd("up", "--menu=false", "--exit-code-from", "app").MayFail().Within(60*time.Second),
ExitCode(3),
OutputContains("hook-ran"))
}
func TestUpInteractiveImmediateExit(t *testing.T) {
NewScenario(t, "a foreground up --abort-on-container-exit must notice a container that exits as soon as it starts").
Step("up aborts on the instantly exiting container instead of waiting for the long-running one",
ComposeCmd("up", "--menu=false", "--abort-on-container-exit").Within(60*time.Second),
OutputContains("quick-1 exited with code 0"),
ServiceState("quick", "exited"),
ServiceState("long", "exited"))
}
func TestUpInteractiveRestartsExitedContainer(t *testing.T) {
NewScenario(t, "a foreground up must start a container that exited earlier, reusing it").
Step("up -d leaves the service exited after its first run",
ComposeCmd("up", "-d"),
Eventually(ServiceState("app", "exited"), 15*time.Second)).
Step("a foreground up runs the same container a second time",
ComposeCmd("up", "--menu=false").Within(60*time.Second),
NotRecreated("app"),
StdoutContains("second-run"),
ServiceState("app", "exited"))
}
func TestUpInteractiveWaitsForHealthy(t *testing.T) {
NewScenario(t, "a foreground up must not start a service before its service_healthy dependency is healthy").
Step("up starts web only once db reports healthy",
ComposeCmd("up", "--menu=false", "--abort-on-container-exit").Within(120*time.Second),
StdoutContains("web-saw-ready"),
OutputNotContains("web-too-early"),
ServiceState("web", "exited"))
}
func TestUpInteractiveRecreateStreamsNewContainer(t *testing.T) {
NewScenario(t, "a foreground up after a config change must recreate the service and stream the new container's output").
Step("up -d creates the service with its first configuration",
ComposeCmd("up", "-d"),
Eventually(ServiceState("app", "exited"), 15*time.Second)).
Step("a foreground up with another value recreates it and shows the new output",
ComposeCmd("up", "--menu=false").WithEnv("VALUE=two").Within(60*time.Second),
Recreated("app"),
ContainerEnv("app", "VALUE", "two"),
StdoutContains("value=two"),
OutputNotContains("value=one"))
}
func TestUpInteractiveDependencyTimeout(t *testing.T) {
NewScenario(t, "a foreground up with --wait-timeout must fail once a dependency stays unhealthy, without starting the dependent").
Step("up fails on the dependency timeout; web exists but was never started",
ComposeCmd("up", "--menu=false", "--wait-timeout", "3").MayFail().Within(60*time.Second),
ExitCode(1),
OutputContains("timeout waiting for dependencies"),
OutputNotContains("deadline exceeded"),
OutputNotContains("web-started"),
ServiceState("db", "running"),
// web's container is created in the create phase, before the start
// phase blocks on db's health: "created" (not "no container") is
// what says it was prepared but never started.
ServiceState("web", "created"))
}
// Regression test for https://github.com/docker/compose/issues/14386: a foreground
// up left a use_api_socket service without its config file, DOCKER_CONFIG
// pointing at a file that was never created.
//
// use_api_socket is applied by rewriting the project (a #apisocket config the
// service mounts at /run/secrets/docker/config.json): a foreground up must
// start the containers from that same rewritten project, as a detached one
// does. The file is injected when the host has registry credentials to hand
// over.
func TestUpInteractiveInjectsAPISocketConfig(t *testing.T) {
withRegistryCredentials(NewScenario(t, "a foreground up must give a use_api_socket service its docker config file, as a detached up does")).
Step("the service finds the config file in its session output",
ComposeCmd("up", "--menu=false").Within(60*time.Second),
StdoutContains("apisocket-config-present"),
OutputNotContains("apisocket-config-absent"),
ServiceState("app", "exited"))
}
func TestUpInteractiveDryRun(t *testing.T) {
// the progress lines name the container after the project
s := NewScenario(t, "a foreground up --dry-run must report the container creation, never start it and leave nothing behind")
s.Step("up --dry-run reports the creation, no start, and leaves no container behind",
ComposeCmd("up", "--menu=false", "--dry-run").Within(60*time.Second),
OutputContains("Container "+s.Project()+"-app-1 Created"),
OutputNotContains(s.Project()+"-app-1 Start"),
ServiceNotCreated("app"))
}