diff --git a/pkg/e2e/compose_build_test.go b/pkg/e2e/compose_build_test.go index 9483a10e4..68df43f8a 100644 --- a/pkg/e2e/compose_build_test.go +++ b/pkg/e2e/compose_build_test.go @@ -18,7 +18,6 @@ package e2e import ( "net/http" - "os" "strings" "testing" "time" @@ -81,11 +80,6 @@ func TestLocalComposeBuild(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.Assert(t, icmd.Expected{ ExitCode: 1, diff --git a/pkg/e2e/compose_environment_test.go b/pkg/e2e/compose_environment_test.go index e70a2536d..60ec3d793 100644 --- a/pkg/e2e/compose_environment_test.go +++ b/pkg/e2e/compose_environment_test.go @@ -17,7 +17,6 @@ package e2e import ( - "os" "strings" "testing" @@ -43,13 +42,10 @@ func TestEnvPriority(t *testing.T) { // 4. Dockerfile // 5. Variable is not defined t.Run("compose file priority", func(t *testing.T) { - os.Setenv("WHEREAMI", "shell") //nolint:errcheck - defer os.Unsetenv("WHEREAMI") //nolint:errcheck - - res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env.yaml", - "--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override", - "run", "--rm", "-e", "WHEREAMI", "env-compose-priority") - + cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose-with-env.yaml", + "--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override", "run", + "--rm", "-e", "WHEREAMI", "env-compose-priority") + res := icmd.RunCmd(cmd, icmd.WithEnv("WHEREAMI=shell")) assert.Equal(t, strings.TrimSpace(res.Stdout()), "Compose File") }) @@ -60,12 +56,10 @@ func TestEnvPriority(t *testing.T) { // 4. Dockerfile // 5. Variable is not defined t.Run("shell priority", func(t *testing.T) { - os.Setenv("WHEREAMI", "shell") //nolint:errcheck - defer os.Unsetenv("WHEREAMI") //nolint:errcheck - - res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml", - "--project-directory", projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override", - "run", "--rm", "-e", "WHEREAMI", "env-compose-priority") + cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-priority/compose.yaml", "--project-directory", + projectDir, "--env-file", "./fixtures/environment/env-priority/.env.override", "run", "--rm", "-e", + "WHEREAMI", "env-compose-priority") + res := icmd.RunCmd(cmd, icmd.WithEnv("WHEREAMI=shell")) assert.Equal(t, strings.TrimSpace(res.Stdout()), "shell") }) @@ -137,11 +131,9 @@ func TestEnvInterpolation(t *testing.T) { // 4. Dockerfile // 5. Variable is not defined t.Run("shell priority from run command", func(t *testing.T) { - os.Setenv("WHEREAMI", "shell") //nolint:errcheck - defer os.Unsetenv("WHEREAMI") //nolint:errcheck - res := c.RunDockerComposeCmd(t, "-f", "./fixtures/environment/env-interpolation/compose.yaml", + cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/environment/env-interpolation/compose.yaml", "--project-directory", projectDir, "config") - + res := icmd.RunCmd(cmd, icmd.WithEnv("WHEREAMI=shell")) res.Assert(t, icmd.Expected{Out: `IMAGE: default_env:shell`}) }) } diff --git a/pkg/e2e/framework.go b/pkg/e2e/framework.go index fabf90cd8..37f5483a2 100644 --- a/pkg/e2e/framework.go +++ b/pkg/e2e/framework.go @@ -30,6 +30,7 @@ import ( "time" "github.com/pkg/errors" + "github.com/stretchr/testify/require" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" "gotest.tools/v3/icmd" @@ -154,28 +155,29 @@ func CopyFile(sourceFile string, destinationFile string) error { return err } +// BaseEnvironment provides the minimal environment variables used across all +// Docker / Compose commands. +func (c *CLI) BaseEnvironment() []string { + return []string{ + "DOCKER_CONFIG=" + c.ConfigDir, + "KUBECONFIG=invalid", + } +} + // NewCmd creates a cmd object configured with the test environment set func (c *CLI) NewCmd(command string, args ...string) icmd.Cmd { - env := append(os.Environ(), - "DOCKER_CONFIG="+c.ConfigDir, - "KUBECONFIG=invalid", - ) return icmd.Cmd{ Command: append([]string{command}, args...), - Env: env, + Env: c.BaseEnvironment(), } } // 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 { - env := append(os.Environ(), - append(envvars, - "DOCKER_CONFIG="+c.ConfigDir, - "KUBECONFIG=invalid")..., - ) + cmdEnv := append(c.BaseEnvironment(), envvars...) return icmd.Cmd{ Command: append([]string{command}, args...), - Env: env, + Env: cmdEnv, } } @@ -234,13 +236,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 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 { - composeBinary, err := findExecutable(DockerComposeExecutableName, []string{"../../bin", "../../../bin"}) - assert.NilError(t, err) - return icmd.RunCmd(c.NewCmd(composeBinary, args...)) + return c.NewCmd(ComposeStandalonePath(t), 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