mirror of
https://github.com/docker/compose.git
synced 2025-07-27 15:44:08 +02:00
Merge pull request #9563 from milas/e2e-env
e2e: isolate test command env from system env
This commit is contained in:
commit
3f10753178
@ -18,7 +18,6 @@ package e2e
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -81,11 +80,6 @@ func TestLocalComposeBuild(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("build failed with ssh default value", func(t *testing.T) {
|
t.Run("build failed with ssh default value", func(t *testing.T) {
|
||||||
//unset SSH_AUTH_SOCK to be sure we don't have a default value for the SSH Agent
|
|
||||||
defaultSSHAUTHSOCK := os.Getenv("SSH_AUTH_SOCK")
|
|
||||||
os.Unsetenv("SSH_AUTH_SOCK") //nolint:errcheck
|
|
||||||
defer os.Setenv("SSH_AUTH_SOCK", defaultSSHAUTHSOCK) //nolint:errcheck
|
|
||||||
|
|
||||||
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test", "build", "--ssh", "")
|
res := c.RunDockerComposeCmdNoCheck(t, "--project-directory", "fixtures/build-test", "build", "--ssh", "")
|
||||||
res.Assert(t, icmd.Expected{
|
res.Assert(t, icmd.Expected{
|
||||||
ExitCode: 1,
|
ExitCode: 1,
|
||||||
|
@ -17,7 +17,6 @@
|
|||||||
package e2e
|
package e2e
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -43,13 +42,11 @@ func TestEnvPriority(t *testing.T) {
|
|||||||
// 4. Dockerfile
|
// 4. Dockerfile
|
||||||
// 5. Variable is not defined
|
// 5. Variable is not defined
|
||||||
t.Run("compose file priority", func(t *testing.T) {
|
t.Run("compose file priority", func(t *testing.T) {
|
||||||
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
|
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
|
||||||
defer os.Unsetenv("WHEREAMI") //nolint:errcheck
|
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override", "run",
|
||||||
|
"--rm", "-e", "WHEREAMI", "env-compose-priority")
|
||||||
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env.yaml",
|
cmd.Env = append(cmd.Env, "WHEREAMI=shell")
|
||||||
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
|
res := icmd.RunCmd(cmd)
|
||||||
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
|
|
||||||
|
|
||||||
assert.Equal(t, strings.TrimSpace(res.Stdout()), "Compose File")
|
assert.Equal(t, strings.TrimSpace(res.Stdout()), "Compose File")
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -60,12 +57,11 @@ func TestEnvPriority(t *testing.T) {
|
|||||||
// 4. Dockerfile
|
// 4. Dockerfile
|
||||||
// 5. Variable is not defined
|
// 5. Variable is not defined
|
||||||
t.Run("shell priority", func(t *testing.T) {
|
t.Run("shell priority", func(t *testing.T) {
|
||||||
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
|
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml", "--project-directory",
|
||||||
defer os.Unsetenv("WHEREAMI") //nolint:errcheck
|
projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override", "run", "--rm", "-e",
|
||||||
|
"WHEREAMI", "env-compose-priority")
|
||||||
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml",
|
cmd.Env = append(cmd.Env, "WHEREAMI=shell")
|
||||||
"--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override",
|
res := icmd.RunCmd(cmd)
|
||||||
"run", "--rm", "-e", "WHEREAMI", "env-compose-priority")
|
|
||||||
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
|
assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell")
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -137,11 +133,10 @@ func TestEnvInterpolation(t *testing.T) {
|
|||||||
// 4. Dockerfile
|
// 4. Dockerfile
|
||||||
// 5. Variable is not defined
|
// 5. Variable is not defined
|
||||||
t.Run("shell priority from run command", func(t *testing.T) {
|
t.Run("shell priority from run command", func(t *testing.T) {
|
||||||
os.Setenv("WHEREAMI", "shell") //nolint:errcheck
|
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-interpolation/compose.yaml",
|
||||||
defer os.Unsetenv("WHEREAMI") //nolint:errcheck
|
|
||||||
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-interpolation/compose.yaml",
|
|
||||||
"--project-directory", projectDir, "config")
|
"--project-directory", projectDir, "config")
|
||||||
|
cmd.Env = append(cmd.Env, "WHEREAMI=shell")
|
||||||
|
res := icmd.RunCmd(cmd)
|
||||||
res.Assert(t, icmd.Expected{Out: `IMAGE: default_env:shell`})
|
res.Assert(t, icmd.Expected{Out: `IMAGE: default_env:shell`})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -19,11 +19,13 @@ package e2e
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,26 +33,38 @@ const ddevVersion = "v1.19.1"
|
|||||||
|
|
||||||
func TestComposeRunDdev(t *testing.T) {
|
func TestComposeRunDdev(t *testing.T) {
|
||||||
if !composeStandaloneMode {
|
if !composeStandaloneMode {
|
||||||
t.Skip("Not running on standalone mode.")
|
t.Skip("Not running in plugin mode - ddev only supports invoking standalone `docker-compose`")
|
||||||
}
|
}
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
t.Skip("Running on Windows. Skipping...")
|
t.Skip("Running on Windows. Skipping...")
|
||||||
}
|
}
|
||||||
_ = os.Setenv("DDEV_DEBUG", "true")
|
|
||||||
|
|
||||||
c := NewParallelCLI(t)
|
// ddev shells out to `docker` and `docker-compose` (standalone), so a
|
||||||
dir, err := os.MkdirTemp("", t.Name()+"-")
|
// temporary directory is created with symlinks to system Docker and the
|
||||||
assert.NilError(t, err)
|
// locally-built standalone Compose binary to use as PATH
|
||||||
|
requiredTools := []string{
|
||||||
|
findToolInPath(t, DockerExecutableName),
|
||||||
|
ComposeStandalonePath(t),
|
||||||
|
findToolInPath(t, "tar"),
|
||||||
|
findToolInPath(t, "gzip"),
|
||||||
|
}
|
||||||
|
pathDir := t.TempDir()
|
||||||
|
for _, tool := range requiredTools {
|
||||||
|
require.NoError(t, os.Symlink(tool, filepath.Join(pathDir, filepath.Base(tool))),
|
||||||
|
"Could not create symlink for %q", tool)
|
||||||
|
}
|
||||||
|
|
||||||
// ddev needs to be able to find mkcert to figure out where certs are.
|
c := NewCLI(t, WithEnv(
|
||||||
_ = os.Setenv("PATH", fmt.Sprintf("%s:%s", os.Getenv("PATH"), dir))
|
"DDEV_DEBUG=true",
|
||||||
|
fmt.Sprintf("PATH=%s", pathDir),
|
||||||
|
))
|
||||||
|
|
||||||
siteName := filepath.Base(dir)
|
ddevDir := t.TempDir()
|
||||||
|
siteName := filepath.Base(ddevDir)
|
||||||
|
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
_ = c.RunCmdInDir(t, dir, "./ddev", "delete", "-Oy")
|
_ = c.RunCmdInDir(t, ddevDir, "./ddev", "delete", "-Oy")
|
||||||
_ = c.RunCmdInDir(t, dir, "./ddev", "poweroff")
|
_ = c.RunCmdInDir(t, ddevDir, "./ddev", "poweroff")
|
||||||
_ = os.RemoveAll(dir)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
osName := "linux"
|
osName := "linux"
|
||||||
@ -59,28 +73,34 @@ func TestComposeRunDdev(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
compressedFilename := fmt.Sprintf("ddev_%s-%s.%s.tar.gz", osName, runtime.GOARCH, ddevVersion)
|
compressedFilename := fmt.Sprintf("ddev_%s-%s.%s.tar.gz", osName, runtime.GOARCH, ddevVersion)
|
||||||
c.RunCmdInDir(t, dir, "curl", "-LO",
|
c.RunCmdInDir(t, ddevDir, "curl", "-LO", fmt.Sprintf("https://github.com/drud/ddev/releases/download/%s/%s",
|
||||||
fmt.Sprintf("https://github.com/drud/ddev/releases/download/%s/%s",
|
ddevVersion,
|
||||||
ddevVersion,
|
compressedFilename))
|
||||||
compressedFilename))
|
|
||||||
|
|
||||||
c.RunCmdInDir(t, dir, "tar", "-xzf", compressedFilename)
|
c.RunCmdInDir(t, ddevDir, "tar", "-xzf", compressedFilename)
|
||||||
|
|
||||||
// Create a simple index.php we can test against.
|
// Create a simple index.php we can test against.
|
||||||
c.RunCmdInDir(t, dir, "sh", "-c", "echo '<?php\nprint \"ddev is working\";' >index.php")
|
c.RunCmdInDir(t, ddevDir, "sh", "-c", "echo '<?php\nprint \"ddev is working\";' >index.php")
|
||||||
|
|
||||||
c.RunCmdInDir(t, dir, "./ddev", "config", "--auto")
|
c.RunCmdInDir(t, ddevDir, "./ddev", "config", "--auto")
|
||||||
c.RunCmdInDir(t, dir, "./ddev", "config", "global", "--use-docker-compose-from-path")
|
c.RunCmdInDir(t, ddevDir, "./ddev", "config", "global", "--use-docker-compose-from-path")
|
||||||
vRes := c.RunCmdInDir(t, dir, "./ddev", "version")
|
vRes := c.RunCmdInDir(t, ddevDir, "./ddev", "version")
|
||||||
out := vRes.Stdout()
|
out := vRes.Stdout()
|
||||||
fmt.Printf("ddev version: %s\n", out)
|
fmt.Printf("ddev version: %s\n", out)
|
||||||
|
|
||||||
c.RunCmdInDir(t, dir, "./ddev", "poweroff")
|
c.RunCmdInDir(t, ddevDir, "./ddev", "poweroff")
|
||||||
|
|
||||||
c.RunCmdInDir(t, dir, "./ddev", "start", "-y")
|
c.RunCmdInDir(t, ddevDir, "./ddev", "start", "-y")
|
||||||
|
|
||||||
curlRes := c.RunCmdInDir(t, dir, "curl", "-sSL", fmt.Sprintf("http://%s.ddev.site", siteName))
|
curlRes := c.RunCmdInDir(t, ddevDir, "curl", "-sSL", fmt.Sprintf("http://%s.ddev.site", siteName))
|
||||||
out = curlRes.Stdout()
|
out = curlRes.Stdout()
|
||||||
fmt.Println(out)
|
fmt.Println(out)
|
||||||
assert.Assert(t, strings.Contains(out, "ddev is working"), "Could not start project")
|
assert.Assert(t, strings.Contains(out, "ddev is working"), "Could not start project")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func findToolInPath(t testing.TB, name string) string {
|
||||||
|
t.Helper()
|
||||||
|
binPath, err := exec.LookPath(name)
|
||||||
|
require.NoError(t, err, "Could not find %q in path", name)
|
||||||
|
return binPath
|
||||||
|
}
|
||||||
|
@ -30,8 +30,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
is "gotest.tools/v3/assert/cmp"
|
|
||||||
"gotest.tools/v3/icmd"
|
"gotest.tools/v3/icmd"
|
||||||
"gotest.tools/v3/poll"
|
"gotest.tools/v3/poll"
|
||||||
|
|
||||||
@ -59,19 +59,59 @@ func init() {
|
|||||||
|
|
||||||
// CLI is used to wrap the CLI for end to end testing
|
// CLI is used to wrap the CLI for end to end testing
|
||||||
type CLI struct {
|
type CLI struct {
|
||||||
|
// ConfigDir for Docker configuration (set as DOCKER_CONFIG)
|
||||||
ConfigDir string
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewParallelCLI returns a configured CLI with t.Parallel() set
|
// CLIOption to customize behavior for all commands for a CLI instance.
|
||||||
func NewParallelCLI(t *testing.T) *CLI {
|
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()
|
t.Parallel()
|
||||||
return NewCLI(t)
|
return NewCLI(t, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCLI returns a CLI to use for E2E tests
|
// NewCLI creates a CLI instance for running E2E tests.
|
||||||
func NewCLI(t testing.TB) *CLI {
|
func NewCLI(t testing.TB, opts ...CLIOption) *CLI {
|
||||||
d, err := ioutil.TempDir("", "")
|
t.Helper()
|
||||||
assert.Check(t, is.Nil(err))
|
|
||||||
|
configDir := t.TempDir()
|
||||||
|
initializePlugins(t, configDir)
|
||||||
|
|
||||||
|
c := &CLI{
|
||||||
|
ConfigDir: configDir,
|
||||||
|
HomeDir: t.TempDir(),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
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...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// initializePlugins copies the necessary plugin files to the temporary config
|
||||||
|
// directory for the test.
|
||||||
|
func initializePlugins(t testing.TB, d string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
if t.Failed() {
|
if t.Failed() {
|
||||||
@ -101,8 +141,6 @@ func NewCLI(t testing.TB) *CLI {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &CLI{ConfigDir: d}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func dirContents(dir string) []string {
|
func dirContents(dir string) []string {
|
||||||
@ -154,28 +192,33 @@ func CopyFile(sourceFile string, destinationFile string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BaseEnvironment provides the minimal environment variables used across all
|
||||||
|
// Docker / Compose commands.
|
||||||
|
func (c *CLI) BaseEnvironment() []string {
|
||||||
|
return []string{
|
||||||
|
"HOME=" + c.HomeDir,
|
||||||
|
"USER=" + os.Getenv("USER"),
|
||||||
|
"DOCKER_CONFIG=" + c.ConfigDir,
|
||||||
|
"KUBECONFIG=invalid",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NewCmd creates a cmd object configured with the test environment set
|
// NewCmd creates a cmd object configured with the test environment set
|
||||||
func (c *CLI) NewCmd(command string, args ...string) icmd.Cmd {
|
func (c *CLI) NewCmd(command string, args ...string) icmd.Cmd {
|
||||||
env := append(os.Environ(),
|
|
||||||
"DOCKER_CONFIG="+c.ConfigDir,
|
|
||||||
"KUBECONFIG=invalid",
|
|
||||||
)
|
|
||||||
return icmd.Cmd{
|
return icmd.Cmd{
|
||||||
Command: append([]string{command}, args...),
|
Command: append([]string{command}, args...),
|
||||||
Env: env,
|
Env: append(c.BaseEnvironment(), c.env...),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCmdWithEnv creates a cmd object configured with the test environment set with additional env vars
|
// 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 {
|
func (c *CLI) NewCmdWithEnv(envvars []string, command string, args ...string) icmd.Cmd {
|
||||||
env := append(os.Environ(),
|
// base env -> CLI overrides -> cmd overrides
|
||||||
append(envvars,
|
cmdEnv := append(c.BaseEnvironment(), c.env...)
|
||||||
"DOCKER_CONFIG="+c.ConfigDir,
|
cmdEnv = append(cmdEnv, envvars...)
|
||||||
"KUBECONFIG=invalid")...,
|
|
||||||
)
|
|
||||||
return icmd.Cmd{
|
return icmd.Cmd{
|
||||||
Command: append([]string{command}, args...),
|
Command: append([]string{command}, args...),
|
||||||
Env: env,
|
Env: cmdEnv,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,13 +277,34 @@ func (c *CLI) RunDockerComposeCmd(t testing.TB, args ...string) *icmd.Result {
|
|||||||
|
|
||||||
// RunDockerComposeCmdNoCheck runs a docker compose command, don't presume of any expectation and returns a result
|
// 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 {
|
func (c *CLI) RunDockerComposeCmdNoCheck(t testing.TB, args ...string) *icmd.Result {
|
||||||
|
return icmd.RunCmd(c.NewDockerComposeCmd(t, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
if composeStandaloneMode {
|
||||||
composeBinary, err := findExecutable(DockerComposeExecutableName, []string{"../../bin", "../../../bin"})
|
return c.NewCmd(ComposeStandalonePath(t), args...)
|
||||||
assert.NilError(t, err)
|
|
||||||
return icmd.RunCmd(c.NewCmd(composeBinary, args...))
|
|
||||||
}
|
}
|
||||||
args = append([]string{"compose"}, args...)
|
args = append([]string{"compose"}, args...)
|
||||||
return icmd.RunCmd(c.NewCmd(DockerExecutableName, 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 {
|
||||||
|
require.Fail(t, "Not running in standalone mode")
|
||||||
|
}
|
||||||
|
composeBinary, err := findExecutable(DockerComposeExecutableName, []string{"../../bin", "../../../bin"})
|
||||||
|
require.NoError(t, err, "Could not find standalone Compose binary (%q)",
|
||||||
|
DockerComposeExecutableName)
|
||||||
|
return composeBinary
|
||||||
}
|
}
|
||||||
|
|
||||||
// StdoutContains returns a predicate on command result expecting a string in stdout
|
// StdoutContains returns a predicate on command result expecting a string in stdout
|
||||||
|
Loading…
x
Reference in New Issue
Block a user