mirror of
https://github.com/docker/compose.git
synced 2026-11-28 02:49:57 +00:00
Some checks failed
ci / validate (lint) (push) Has been cancelled
ci / validate (relay-lint) (push) Has been cancelled
ci / validate (relay-test) (push) Has been cancelled
ci / validate (validate-docs) (push) Has been cancelled
ci / validate (validate-go-mod) (push) Has been cancelled
ci / validate (validate-headers) (push) Has been cancelled
ci / validate (validate-mocks) (push) Has been cancelled
ci / binary (push) Has been cancelled
ci / bin-image-test (push) Has been cancelled
ci / relay-image-test (push) Has been cancelled
ci / test (push) Has been cancelled
ci / e2e (plugin, oldstable, graphdriver) (push) Has been cancelled
ci / e2e (standalone, oldstable, graphdriver) (push) Has been cancelled
ci / e2e (plugin, stable, containerd) (push) Has been cancelled
ci / e2e (plugin, stable, graphdriver) (push) Has been cancelled
ci / e2e (standalone, stable, containerd) (push) Has been cancelled
ci / e2e (standalone, stable, graphdriver) (push) Has been cancelled
ci / e2e (plugin, test, graphdriver, jobs extension) (push) Has been cancelled
merge / bin-image-prepare (push) Has been cancelled
merge / relay-image (push) Has been cancelled
merge / module-image (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
zizmor / zizmor (push) Has been cancelled
ci / binary-finalize (push) Has been cancelled
ci / coverage (push) Has been cancelled
ci / ci-ok (push) Has been cancelled
ci / release (push) Has been cancelled
merge / bin-image (push) Has been cancelled
"docker buildx create" refuses to use the default context when its TLS data is loaded from DOCKER_TLS_VERIFY/DOCKER_CERT_PATH: could not create a builder instance with TLS data loaded from environment. Please use `docker context create <context-name>` ... Now that these variables are forwarded to the tests, add a CreateBuilder helper that copies the default context to a "<builder>-ctx" context and creates the builder on it when TLS is configured through the environment. The context cannot share the builder's name because buildx treats every context as an implicit builder. Use it in the four tests that create a builder: TestBuildPlatformsWithCorrectBuildxConfig, TestBuildPrivileged, TestBuildBuilder and TestBuildEntitlements. Signed-off-by: Ricardo Branco <rbranco@suse.de>
575 lines
18 KiB
Go
575 lines
18 KiB
Go
/*
|
|
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 (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
cp "github.com/otiai10/copy"
|
|
"gotest.tools/v3/assert"
|
|
"gotest.tools/v3/icmd"
|
|
"gotest.tools/v3/poll"
|
|
|
|
"github.com/docker/compose/v5/cmd/compose"
|
|
)
|
|
|
|
var (
|
|
// DockerExecutableName is the OS dependent Docker CLI binary name
|
|
DockerExecutableName = "docker"
|
|
|
|
// DockerComposeExecutableName is the OS dependent Docker CLI binary name
|
|
DockerComposeExecutableName = "docker-" + compose.PluginName
|
|
|
|
// DockerScanExecutableName is the OS dependent Docker Scan plugin binary name
|
|
DockerScanExecutableName = "docker-scan"
|
|
|
|
// DockerBuildxExecutableName is the Os dependent Buildx plugin binary name
|
|
DockerBuildxExecutableName = "docker-buildx"
|
|
|
|
// DockerModelExecutableName is the Os dependent Docker-Model plugin binary name
|
|
DockerModelExecutableName = "docker-model"
|
|
|
|
// WindowsExecutableSuffix is the Windows executable suffix
|
|
WindowsExecutableSuffix = ".exe"
|
|
)
|
|
|
|
func init() {
|
|
if runtime.GOOS == "windows" {
|
|
DockerExecutableName += WindowsExecutableSuffix
|
|
DockerComposeExecutableName += WindowsExecutableSuffix
|
|
DockerScanExecutableName += WindowsExecutableSuffix
|
|
DockerBuildxExecutableName += WindowsExecutableSuffix
|
|
}
|
|
}
|
|
|
|
// CLI is used to wrap the CLI for end to end testing
|
|
type CLI struct {
|
|
// ConfigDir for Docker configuration (set as DOCKER_CONFIG)
|
|
ConfigDir string
|
|
|
|
// HomeDir for tools that look for user files (set as HOME)
|
|
HomeDir string
|
|
|
|
// env overrides to apply to every invoked command
|
|
//
|
|
// To populate, use WithEnv when creating a CLI instance.
|
|
env []string
|
|
}
|
|
|
|
// CLIOption to customize behavior for all commands for a CLI instance.
|
|
type CLIOption func(c *CLI)
|
|
|
|
// NewParallelCLI marks the parent test as parallel and returns a CLI instance
|
|
// suitable for usage across child tests.
|
|
func NewParallelCLI(t *testing.T, opts ...CLIOption) *CLI {
|
|
t.Helper()
|
|
t.Parallel()
|
|
return NewCLI(t, opts...)
|
|
}
|
|
|
|
// NewCLI creates a CLI instance for running E2E tests.
|
|
func NewCLI(t testing.TB, opts ...CLIOption) *CLI {
|
|
t.Helper()
|
|
|
|
configDir := t.TempDir()
|
|
copyLocalConfig(t, configDir)
|
|
initializePlugins(t, configDir)
|
|
initializeContextDir(t, configDir)
|
|
|
|
c := &CLI{
|
|
ConfigDir: configDir,
|
|
HomeDir: t.TempDir(),
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(c)
|
|
}
|
|
c.RunDockerComposeCmdNoCheck(t, "version")
|
|
return c
|
|
}
|
|
|
|
// WithEnv sets environment variables that will be passed to commands.
|
|
func WithEnv(env ...string) CLIOption {
|
|
return func(c *CLI) {
|
|
c.env = append(c.env, env...)
|
|
}
|
|
}
|
|
|
|
func copyLocalConfig(t testing.TB, configDir string) {
|
|
t.Helper()
|
|
|
|
// copy local config.json if exists
|
|
localConfig := filepath.Join(os.Getenv("HOME"), ".docker", "config.json")
|
|
// if no config present just continue
|
|
if _, err := os.Stat(localConfig); err != nil {
|
|
// copy the local config.json to the test config dir
|
|
CopyFile(t, localConfig, filepath.Join(configDir, "config.json"))
|
|
}
|
|
}
|
|
|
|
// initializePlugins copies the necessary plugin files to the temporary config
|
|
// directory for the test.
|
|
func initializePlugins(t testing.TB, configDir string) {
|
|
t.Cleanup(func() {
|
|
if t.Failed() {
|
|
if conf, err := os.ReadFile(filepath.Join(configDir, "config.json")); err == nil {
|
|
t.Logf("Config: %s\n", string(conf))
|
|
}
|
|
t.Log("Contents of config dir:")
|
|
for _, p := range dirContents(configDir) {
|
|
t.Logf(" - %s", p)
|
|
}
|
|
}
|
|
})
|
|
|
|
assert.NilError(t, os.MkdirAll(filepath.Join(configDir, "cli-plugins"), 0o755),
|
|
"Failed to create cli-plugins directory")
|
|
composePlugin, err := findExecutable(DockerComposeExecutableName)
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
t.Logf("WARNING: docker-compose cli-plugin not found")
|
|
}
|
|
|
|
if err == nil {
|
|
CopyFile(t, composePlugin, filepath.Join(configDir, "cli-plugins", DockerComposeExecutableName))
|
|
buildxPlugin, err := findPluginExecutable(DockerBuildxExecutableName)
|
|
if err != nil {
|
|
t.Logf("WARNING: docker-buildx cli-plugin not found, using default buildx installation.")
|
|
} else {
|
|
CopyFile(t, buildxPlugin, filepath.Join(configDir, "cli-plugins", DockerBuildxExecutableName))
|
|
}
|
|
// We don't need a functional scan plugin, but a valid plugin binary
|
|
CopyFile(t, composePlugin, filepath.Join(configDir, "cli-plugins", DockerScanExecutableName))
|
|
|
|
modelPlugin, err := findPluginExecutable(DockerModelExecutableName)
|
|
if err != nil {
|
|
t.Logf("WARNING: docker-model cli-plugin not found")
|
|
} else {
|
|
CopyFile(t, modelPlugin, filepath.Join(configDir, "cli-plugins", DockerModelExecutableName))
|
|
}
|
|
}
|
|
}
|
|
|
|
func initializeContextDir(t testing.TB, configDir string) {
|
|
dockerUserDir := ".docker/contexts"
|
|
userDir, err := os.UserHomeDir()
|
|
assert.NilError(t, err, "Failed to get user home directory")
|
|
userContextsDir := filepath.Join(userDir, dockerUserDir)
|
|
if checkExists(userContextsDir) {
|
|
dstContexts := filepath.Join(configDir, "contexts")
|
|
assert.NilError(t, cp.Copy(userContextsDir, dstContexts), "Failed to copy contexts directory")
|
|
}
|
|
}
|
|
|
|
func checkExists(path string) bool {
|
|
_, err := os.Stat(path)
|
|
return err == nil
|
|
}
|
|
|
|
func dirContents(dir string) []string {
|
|
var res []string
|
|
_ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
res = append(res, path)
|
|
return nil
|
|
})
|
|
return res
|
|
}
|
|
|
|
func findExecutable(executableName string) (string, error) {
|
|
bin := os.Getenv("COMPOSE_E2E_BIN_PATH")
|
|
if bin == "" {
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
buildPath := filepath.Join(filepath.Dir(filename), "..", "..", "bin", "build")
|
|
var err error
|
|
bin, err = filepath.Abs(filepath.Join(buildPath, executableName))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
if _, err := os.Stat(bin); err == nil {
|
|
return bin, nil
|
|
}
|
|
return "", fmt.Errorf("looking for %q: %w", bin, fs.ErrNotExist)
|
|
}
|
|
|
|
func findPluginExecutable(pluginExecutableName string) (string, error) {
|
|
dockerUserDir := ".docker/cli-plugins"
|
|
userDir, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
candidates := []string{
|
|
filepath.Join(userDir, dockerUserDir),
|
|
"/usr/local/lib/docker/cli-plugins",
|
|
"/usr/local/libexec/docker/cli-plugins",
|
|
"/usr/lib/docker/cli-plugins",
|
|
"/usr/libexec/docker/cli-plugins",
|
|
}
|
|
for _, path := range candidates {
|
|
bin, err := filepath.Abs(filepath.Join(path, pluginExecutableName))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if _, err := os.Stat(bin); err == nil {
|
|
return bin, nil
|
|
}
|
|
}
|
|
|
|
return "", fmt.Errorf("plugin not found %s: %w", pluginExecutableName, os.ErrNotExist)
|
|
}
|
|
|
|
// CopyFile copies a file from a sourceFile to a destinationFile setting permissions to 0755
|
|
func CopyFile(t testing.TB, sourceFile string, destinationFile string) {
|
|
t.Helper()
|
|
|
|
src, err := os.Open(sourceFile)
|
|
assert.NilError(t, err, "Failed to open source file: %s")
|
|
//nolint:errcheck
|
|
defer src.Close()
|
|
|
|
dst, err := os.OpenFile(destinationFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o755)
|
|
assert.NilError(t, err, "Failed to open destination file: %s", destinationFile)
|
|
//nolint:errcheck
|
|
defer dst.Close()
|
|
|
|
_, err = io.Copy(dst, src)
|
|
assert.NilError(t, err, "Failed to copy file: %s", sourceFile)
|
|
}
|
|
|
|
// BaseEnvironment provides the minimal environment variables used across all
|
|
// Docker / Compose commands.
|
|
func (c *CLI) BaseEnvironment() []string {
|
|
env := []string{
|
|
"HOME=" + c.HomeDir,
|
|
"USER=" + os.Getenv("USER"),
|
|
"DOCKER_CONFIG=" + c.ConfigDir,
|
|
"KUBECONFIG=invalid",
|
|
"PATH=" + os.Getenv("PATH"),
|
|
}
|
|
for _, name := range []string{"DOCKER_HOST", "DOCKER_CONTEXT", "DOCKER_TLS_VERIFY", "DOCKER_CERT_PATH", "DOCKER_API_VERSION"} {
|
|
if value, ok := os.LookupEnv(name); ok {
|
|
env = append(env, name+"="+value)
|
|
}
|
|
}
|
|
|
|
if coverdir, ok := os.LookupEnv("GOCOVERDIR"); ok {
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
root := filepath.Join(filepath.Dir(filename), "..", "..")
|
|
coverdir = filepath.Join(root, coverdir)
|
|
env = append(env, "GOCOVERDIR="+coverdir)
|
|
}
|
|
return env
|
|
}
|
|
|
|
// NewCmd creates a cmd object configured with the test environment set
|
|
func (c *CLI) NewCmd(command string, args ...string) icmd.Cmd {
|
|
return icmd.Cmd{
|
|
Command: append([]string{command}, args...),
|
|
Env: append(c.BaseEnvironment(), c.env...),
|
|
}
|
|
}
|
|
|
|
// NewCmdWithEnv creates a cmd object configured with the test environment set with additional env vars
|
|
func (c *CLI) NewCmdWithEnv(envvars []string, command string, args ...string) icmd.Cmd {
|
|
// base env -> CLI overrides -> cmd overrides
|
|
cmdEnv := append(c.BaseEnvironment(), c.env...)
|
|
cmdEnv = append(cmdEnv, envvars...)
|
|
return icmd.Cmd{
|
|
Command: append([]string{command}, args...),
|
|
Env: cmdEnv,
|
|
}
|
|
}
|
|
|
|
// MetricsSocket get the path where test metrics will be sent
|
|
func (c *CLI) MetricsSocket() string {
|
|
return filepath.Join(c.ConfigDir, "docker-cli.sock")
|
|
}
|
|
|
|
// NewDockerCmd creates a docker cmd without running it
|
|
func (c *CLI) NewDockerCmd(t testing.TB, args ...string) icmd.Cmd {
|
|
t.Helper()
|
|
for _, arg := range args {
|
|
if arg == compose.PluginName {
|
|
t.Fatal("This test called 'RunDockerCmd' for 'compose'. Please prefer 'RunDockerComposeCmd' to be able to test as a plugin and standalone")
|
|
}
|
|
}
|
|
return c.NewCmd(DockerExecutableName, args...)
|
|
}
|
|
|
|
// RunDockerOrExitError runs a docker command and returns a result
|
|
func (c *CLI) RunDockerOrExitError(t testing.TB, args ...string) *icmd.Result {
|
|
t.Helper()
|
|
t.Logf("\t[%s] docker %s\n", t.Name(), strings.Join(args, " "))
|
|
return icmd.RunCmd(c.NewDockerCmd(t, args...))
|
|
}
|
|
|
|
// RunCmd runs a command, expects no error and returns a result
|
|
func (c *CLI) RunCmd(t testing.TB, args ...string) *icmd.Result {
|
|
t.Helper()
|
|
t.Logf("\t[%s] %s\n", t.Name(), strings.Join(args, " "))
|
|
assert.Assert(t, len(args) >= 1, "require at least one command in parameters")
|
|
res := icmd.RunCmd(c.NewCmd(args[0], args[1:]...))
|
|
res.Assert(t, icmd.Success)
|
|
return res
|
|
}
|
|
|
|
// RunCmdInDir runs a command in a given dir, expects no error and returns a result
|
|
func (c *CLI) RunCmdInDir(t testing.TB, dir string, args ...string) *icmd.Result {
|
|
t.Helper()
|
|
t.Logf("\t[%s] %s\n", t.Name(), strings.Join(args, " "))
|
|
assert.Assert(t, len(args) >= 1, "require at least one command in parameters")
|
|
cmd := c.NewCmd(args[0], args[1:]...)
|
|
cmd.Dir = dir
|
|
res := icmd.RunCmd(cmd)
|
|
res.Assert(t, icmd.Success)
|
|
return res
|
|
}
|
|
|
|
// RunDockerCmd runs a docker command, expects no error and returns a result
|
|
func (c *CLI) RunDockerCmd(t testing.TB, args ...string) *icmd.Result {
|
|
t.Helper()
|
|
res := c.RunDockerOrExitError(t, args...)
|
|
res.Assert(t, icmd.Success)
|
|
return res
|
|
}
|
|
|
|
// RunDockerComposeCmd runs a docker compose command, expects no error and returns a result
|
|
func (c *CLI) RunDockerComposeCmd(t testing.TB, args ...string) *icmd.Result {
|
|
t.Helper()
|
|
res := c.RunDockerComposeCmdNoCheck(t, args...)
|
|
res.Assert(t, icmd.Success)
|
|
return res
|
|
}
|
|
|
|
// RunDockerComposeCmdNoCheck runs a docker compose command, don't presume of any expectation and returns a result
|
|
func (c *CLI) RunDockerComposeCmdNoCheck(t testing.TB, args ...string) *icmd.Result {
|
|
t.Helper()
|
|
cmd := c.NewDockerComposeCmd(t, args...)
|
|
cmd.Stdout = os.Stdout
|
|
t.Logf("Running command: %s", strings.Join(cmd.Command, " "))
|
|
return icmd.RunCmd(cmd)
|
|
}
|
|
|
|
// NewDockerComposeCmd creates a command object for Compose, either in plugin
|
|
// or standalone mode (based on build tags).
|
|
func (c *CLI) NewDockerComposeCmd(t testing.TB, args ...string) icmd.Cmd {
|
|
t.Helper()
|
|
if composeStandaloneMode {
|
|
return c.NewCmd(ComposeStandalonePath(t), args...)
|
|
}
|
|
args = append([]string{"compose"}, args...)
|
|
return c.NewCmd(DockerExecutableName, args...)
|
|
}
|
|
|
|
// ComposeStandalonePath returns the path to the locally-built Compose
|
|
// standalone binary from the repo.
|
|
//
|
|
// This function will fail the test immediately if invoked when not running
|
|
// in standalone test mode.
|
|
func ComposeStandalonePath(t testing.TB) string {
|
|
t.Helper()
|
|
if !composeStandaloneMode {
|
|
t.Fatal("Not running in standalone mode")
|
|
}
|
|
composeBinary, err := findExecutable(DockerComposeExecutableName)
|
|
assert.NilError(t, err, "Could not find standalone Compose binary (%q)",
|
|
DockerComposeExecutableName)
|
|
return composeBinary
|
|
}
|
|
|
|
func IsHealthy(service string) func(res *icmd.Result) bool {
|
|
return func(res *icmd.Result) bool {
|
|
type state struct {
|
|
Name string `json:"name"`
|
|
Health string `json:"health"`
|
|
}
|
|
|
|
decoder := json.NewDecoder(strings.NewReader(res.Stdout()))
|
|
for decoder.More() {
|
|
ps := state{}
|
|
err := decoder.Decode(&ps)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if ps.Name == service && ps.Health == "healthy" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
// WaitForCmdResult try to execute a cmd until resulting output matches given predicate
|
|
func (c *CLI) WaitForCmdResult(
|
|
t testing.TB,
|
|
command icmd.Cmd,
|
|
predicate func(*icmd.Result) bool,
|
|
timeout time.Duration,
|
|
delay time.Duration,
|
|
) {
|
|
t.Helper()
|
|
assert.Assert(t, timeout.Nanoseconds() > delay.Nanoseconds(), "timeout must be greater than delay")
|
|
var res *icmd.Result
|
|
checkStopped := func(logt poll.LogT) poll.Result {
|
|
fmt.Printf("\t[%s] %s\n", t.Name(), strings.Join(command.Command, " "))
|
|
res = icmd.RunCmd(command)
|
|
if !predicate(res) {
|
|
return poll.Continue("Cmd output did not match requirement: %q", res.Combined())
|
|
}
|
|
return poll.Success()
|
|
}
|
|
poll.WaitOn(t, checkStopped, poll.WithDelay(delay), poll.WithTimeout(timeout))
|
|
}
|
|
|
|
// WaitForCondition wait for predicate to execute to true
|
|
func (c *CLI) WaitForCondition(
|
|
t testing.TB,
|
|
predicate func() (bool, string),
|
|
timeout time.Duration,
|
|
delay time.Duration,
|
|
) {
|
|
t.Helper()
|
|
checkStopped := func(logt poll.LogT) poll.Result {
|
|
pass, description := predicate()
|
|
if !pass {
|
|
return poll.Continue("Condition not met: %q", description)
|
|
}
|
|
return poll.Success()
|
|
}
|
|
poll.WaitOn(t, checkStopped, poll.WithDelay(delay), poll.WithTimeout(timeout))
|
|
}
|
|
|
|
// Lines split output into lines
|
|
func Lines(output string) []string {
|
|
return strings.Split(strings.TrimSpace(output), "\n")
|
|
}
|
|
|
|
// HTTPGetWithRetry performs an HTTP GET on an `endpoint`, using retryDelay also as a request timeout.
|
|
// In the case of an error or the response status is not the expected one, it retries the same request,
|
|
// returning the response body as a string (empty if we could not reach it)
|
|
func HTTPGetWithRetry(
|
|
t testing.TB,
|
|
endpoint string,
|
|
expectedStatus int,
|
|
retryDelay time.Duration,
|
|
timeout time.Duration,
|
|
) string {
|
|
t.Helper()
|
|
var (
|
|
r *http.Response
|
|
err error
|
|
)
|
|
client := &http.Client{
|
|
Timeout: retryDelay,
|
|
}
|
|
fmt.Printf("\t[%s] GET %s\n", t.Name(), endpoint)
|
|
checkUp := func(t poll.LogT) poll.Result {
|
|
r, err = client.Get(endpoint)
|
|
if err != nil {
|
|
return poll.Continue("reaching %q: Error %s", endpoint, err.Error())
|
|
}
|
|
if r.StatusCode == expectedStatus {
|
|
return poll.Success()
|
|
}
|
|
return poll.Continue("reaching %q: %d != %d", endpoint, r.StatusCode, expectedStatus)
|
|
}
|
|
poll.WaitOn(t, checkUp, poll.WithDelay(retryDelay), poll.WithTimeout(timeout))
|
|
if r != nil {
|
|
b, err := io.ReadAll(r.Body)
|
|
assert.NilError(t, err)
|
|
return string(b)
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (c *CLI) cleanupWithDown(t testing.TB, project string, args ...string) {
|
|
t.Helper()
|
|
c.RunDockerComposeCmd(t, append([]string{"-p", project, "down", "-v", "--remove-orphans"}, args...)...)
|
|
}
|
|
|
|
// ServicePublishedPort returns the ephemeral host port mapped to targetPort
|
|
// on the named service in the given compose project. It requires the service
|
|
// to be already running. The test fails immediately if the mapping cannot be
|
|
// resolved.
|
|
func (c *CLI) ServicePublishedPort(t testing.TB, project, service string, targetPort int) int {
|
|
t.Helper()
|
|
res := c.RunDockerComposeCmd(t, "-p", project, "port", service, strconv.Itoa(targetPort))
|
|
addr := strings.TrimSpace(res.Stdout())
|
|
_, portStr, err := net.SplitHostPort(addr)
|
|
if err != nil {
|
|
t.Fatalf("ServicePublishedPort: cannot parse compose port output %q: %v", addr, err)
|
|
}
|
|
port, err := strconv.Atoi(portStr)
|
|
if err != nil {
|
|
t.Fatalf("ServicePublishedPort: invalid port number %q: %v", portStr, err)
|
|
}
|
|
return port
|
|
}
|
|
|
|
// CreateBuilder creates a buildx builder named name and fails the test on error.
|
|
//
|
|
// "docker buildx create" refuses to run against the default context when its
|
|
// TLS settings come from the environment (DOCKER_TLS_VERIFY, DOCKER_CERT_PATH),
|
|
// so in that case the default context is first copied to a context named
|
|
// "<name>-ctx" (buildx treats every context as an implicit builder, so it
|
|
// cannot share the builder's name) and the builder is created on top of it.
|
|
// The context lives in the per-test DOCKER_CONFIG directory, so it needs no
|
|
// cleanup.
|
|
func (c *CLI) CreateBuilder(t testing.TB, name string, args ...string) *icmd.Result {
|
|
t.Helper()
|
|
create := []string{"buildx", "create", "--name", name, "--use", "--bootstrap"}
|
|
create = append(create, args...)
|
|
if os.Getenv("DOCKER_TLS_VERIFY") != "" || os.Getenv("DOCKER_CERT_PATH") != "" {
|
|
ctx := name + "-ctx"
|
|
c.RunDockerCmd(t, "context", "create", ctx, "--from", "default")
|
|
create = append(create, ctx)
|
|
}
|
|
return c.RunDockerCmd(t, create...)
|
|
}
|
|
|
|
// BuilderName returns a buildx builder name that is unique to the test,
|
|
// preventing container-name collisions when tests run in parallel on the
|
|
// same Docker daemon.
|
|
func BuilderName(t testing.TB, base string) string {
|
|
t.Helper()
|
|
safe := strings.Map(func(r rune) rune {
|
|
switch {
|
|
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '-':
|
|
return r
|
|
default:
|
|
return '-'
|
|
}
|
|
}, t.Name())
|
|
name := base + "-" + safe
|
|
if len(name) > 60 {
|
|
name = strings.TrimRight(name[:60], "-")
|
|
}
|
|
return name
|
|
}
|