diff --git a/e2e/README.md b/e2e/README.md index 23d4a89a1f..8239552f80 100644 --- a/e2e/README.md +++ b/e2e/README.md @@ -33,6 +33,25 @@ mise run cli:test:e2e:focus "pattern" `cli:test:e2e:act:focus` runs a focused pattern through [act](https://github.com/nektos/act). +The Docker lifecycle environment regression covers values containing `=` from both +`--workspace-env` and `--workspace-env-file`, including restart and `remoteEnv` precedence: + +```bash +mise run cli:test:e2e:focus "lifecycle hooks preserve equals signs" +``` + +When testing local changes with Docker on macOS, run this from `e2e/` and point +`DEVSY_AGENT_BINARY` at the local Linux agent matching the Docker architecture: + +```bash +DEVSY_AGENT_BINARY="$PWD/bin/devsy-linux-arm64" \ + GOMAXPROCS=1 GOFLAGS=-p=1 mise exec -- go tool ginkgo --focus \ + "lifecycle hooks preserve equals signs" +``` + +This bypasses cached release agents; otherwise the container can run a released +binary rather than the code under test. + ## Kubernetes tests Specs labeled `up-provider-kubernetes` or `build` need a kind cluster. The setup task diff --git a/e2e/tests/up/microsandbox_session.go b/e2e/tests/up/microsandbox_session.go index a2ae5b2766..77601e3ed9 100644 --- a/e2e/tests/up/microsandbox_session.go +++ b/e2e/tests/up/microsandbox_session.go @@ -18,6 +18,7 @@ import ( const ( microsandboxResourceBuiltin = provider.DeprecatedMicrosandboxSource osLinux = "linux" + osDarwin = "darwin" microsandboxExternalProvider = "github.com/devsy-org/devsy-provider-microsandbox@v0.1.10" microsandboxRootUser = "root" ) @@ -31,7 +32,7 @@ func skipIfNoMicrosandbox(ctx context.Context) { microsandboxUnavailable("microsandbox requires KVM (/dev/kvm not accessible)") } _ = kvm.Close() - case runtime.GOOS == "darwin" && runtime.GOARCH == "arm64": + case runtime.GOOS == osDarwin && runtime.GOARCH == "arm64": // Apple silicon supports microsandbox via the hypervisor framework. default: microsandboxUnavailable("microsandbox requires Apple silicon or Linux with KVM") diff --git a/e2e/tests/up/provider_docker.go b/e2e/tests/up/provider_docker.go index 8b8a546b66..97f20037e0 100644 --- a/e2e/tests/up/provider_docker.go +++ b/e2e/tests/up/provider_docker.go @@ -7,10 +7,13 @@ import ( "os/exec" "path" "path/filepath" + "runtime" + "slices" "strings" "time" "github.com/devsy-org/devsy/e2e/framework" + devsyconfig "github.com/devsy-org/devsy/pkg/config" docker "github.com/devsy-org/devsy/pkg/docker" "github.com/devsy-org/devsy/pkg/flags/names" provider2 "github.com/devsy-org/devsy/pkg/provider" @@ -20,6 +23,8 @@ import ( "github.com/onsi/gomega" ) +const envEqualsIsolatedHomeLabel = "env-equals-isolated-home" + var _ = ginkgo.Describe( "testing up command for docker customizations", ginkgo.Label("up-provider-docker"), @@ -31,6 +36,23 @@ var _ = ginkgo.Describe( dtc = &dockerTestContext{} dtc.initialDir, err = os.Getwd() framework.ExpectNoError(err) + if slices.Contains( + ginkgo.CurrentSpecReport().Labels(), + envEqualsIsolatedHomeLabel, + ) { + homeParent := os.TempDir() + if runtime.GOOS == osDarwin { + homeParent = "/tmp" + } + home, err := os.MkdirTemp(homeParent, "devsy-e2e-") + framework.ExpectNoError(err) + ginkgo.DeferCleanup(func() { + framework.ExpectNoError(os.RemoveAll(home)) + }) + devsyconfig.ResetPathManager() + ginkgo.DeferCleanup(devsyconfig.ResetPathManager) + framework.SetEnvForSpec(devsyconfig.EnvHome, home) + } dtc.dockerHelper = &docker.DockerHelper{DockerCommand: "docker"} dtc.f, err = framework.SetupDockerProvider( @@ -245,6 +267,100 @@ var _ = ginkgo.Describe( gomega.Expect(unsetLine).To(gomega.Equal("UNSET=true")) }, ginkgo.SpecTimeout(framework.TimeoutShort())) + ginkgo.It( + "lifecycle hooks preserve equals signs in environment values", + ginkgo.Label(envEqualsIsolatedHomeLabel), + func(ctx context.Context) { + initialConfig, err := devsyconfig.LoadConfig("", "") + framework.ExpectNoError(err) + sentinelContext := fmt.Sprintf("env-equals-sentinel-%d", time.Now().UnixNano()) + gomega.Expect(initialConfig.Contexts).NotTo( + gomega.HaveKey(sentinelContext), + "sentinel context must not preexist in the isolated home", + ) + framework.ExpectNoError(dtc.f.DevsyContextCreate(ctx, sentinelContext)) + framework.ExpectNoError(dtc.f.DevsyContextUse(ctx, sentinelContext)) + + priorConfig, err := devsyconfig.LoadConfig("", "") + framework.ExpectNoError(err) + priorContext := priorConfig.DefaultContext + gomega.Expect(priorContext).To(gomega.Equal(sentinelContext)) + + contextName := fmt.Sprintf("env-equals-%d", time.Now().UnixNano()) + gomega.Expect(priorConfig.Contexts).NotTo( + gomega.HaveKey(contextName), + "test context must not preexist in the isolated home", + ) + framework.ExpectNoError(dtc.f.DevsyContextCreate(ctx, contextName)) + ginkgo.DeferCleanup(func(cleanupCtx context.Context) { + if err := dtc.f.DevsyContextUse(cleanupCtx, priorContext); err != nil { + framework.ExpectNoError(err) + return + } + if err := dtc.f.DevsyContextDelete(cleanupCtx, contextName); err != nil { + framework.ExpectNoError(err) + return + } + currentConfig, err := devsyconfig.LoadConfig("", "") + framework.ExpectNoError(err) + gomega.Expect(currentConfig.DefaultContext).To(gomega.Equal(priorContext)) + }) + framework.ExpectNoError(dtc.f.DevsyContextUse(ctx, contextName)) + framework.ExpectNoError( + dtc.f.DevsyProviderAdd(ctx, "docker", "-o", "DOCKER_PATH=docker"), + ) + + tempDir, err := setupWorkspace( + "tests/up/testdata/docker-workspace-env-equals", dtc.initialDir, dtc.f, + ) + framework.ExpectNoError(err) + + framework.ExpectNoError(dtc.f.DevsyUp(ctx, tempDir, + "--workspace-env", "PLAIN=hello", + "--workspace-env", "ONE_EQ=a=b", + "--workspace-env", "TWO_EQ=a=b=c", + "--workspace-env", "COMMA_EQ=x=1,y=2", + "--workspace-env", "TRAILING_EQ=z=", + "--workspace-env-file", filepath.Join(tempDir, "workspace.env"), + )) + + expected := "PLAIN=hello\nONE_EQ=a=b\nTWO_EQ=a=b=c\nCOMMA_EQ=x=1,y=2\n" + + "TRAILING_EQ=z=\nFILE_ONE_EQ=k=v" + expectedHook := expected + "\nREMOTE_EQ=remote=a=b" + expectedSSH := expectedHook + expectedEnvFile := expectedHook + for _, phase := range []string{"postCreate", "postStart"} { + out, err := readLifecycleFile(tempDir, phase+".env.out") + framework.ExpectNoError(err) + gomega.Expect(out).To(gomega.Equal(expectedHook), phase) + } + + out, err := dtc.execSSHCapture(ctx, tempDir, `bash -l -c ' +printf "PLAIN=%s\nONE_EQ=%s\nTWO_EQ=%s\nCOMMA_EQ=%s\nTRAILING_EQ=%s\nFILE_ONE_EQ=%s\nREMOTE_EQ=%s\n" \ + "$PLAIN" "$ONE_EQ" "$TWO_EQ" "$COMMA_EQ" "$TRAILING_EQ" "$FILE_ONE_EQ" "$REMOTE_EQ"'`) + framework.ExpectNoError(err) + gomega.Expect(out).To(gomega.Equal(expectedSSH), "SSH environment") + + // Filter in the container so unrelated persisted values never reach test logs. + out, err = dtc.execSSHCapture(ctx, tempDir, `jq -r '.env | +"PLAIN=\(.PLAIN)\nONE_EQ=\(.ONE_EQ)\n" + +"TWO_EQ=\(.TWO_EQ)\nCOMMA_EQ=\(.COMMA_EQ)\n" + +"TRAILING_EQ=\(.TRAILING_EQ)\nFILE_ONE_EQ=\(.FILE_ONE_EQ)\n" + +"REMOTE_EQ=\(.REMOTE_EQ)"' /etc/envfile.json`) + framework.ExpectNoError(err) + gomega.Expect(out).To(gomega.Equal(expectedEnvFile), "persisted environment") + + // Remove the old output to prove postStartCommand runs again after restart. + framework.ExpectNoError(os.Remove(filepath.Join(tempDir, "postStart.env.out"))) + framework.ExpectNoError(dtc.f.DevsyWorkspaceStop(ctx, tempDir)) + framework.ExpectNoError(dtc.f.DevsyUp(ctx, tempDir)) + out, err = readLifecycleFile(tempDir, "postStart.env.out") + framework.ExpectNoError(err) + gomega.Expect(out).To(gomega.Equal(expectedHook), "postStart after restart") + }, + ginkgo.SpecTimeout(framework.TimeoutModerate()), + ) + ginkgo.It("mounts", func(ctx context.Context) { tempDir, err := dtc.setupAndUp(ctx, "tests/up/testdata/docker-mounts", "--debug") framework.ExpectNoError(err) diff --git a/e2e/tests/up/provider_microsandbox.go b/e2e/tests/up/provider_microsandbox.go index 7acc396926..d068d3f3d5 100644 --- a/e2e/tests/up/provider_microsandbox.go +++ b/e2e/tests/up/provider_microsandbox.go @@ -364,7 +364,7 @@ func assertMicrosandboxHostEntries( func microsandboxHostOwner(ctx context.Context, entry string) string { args := []string{"-c", "%u:%g", entry} - if runtime.GOOS == "darwin" { + if runtime.GOOS == osDarwin { args = []string{"-f", "%u:%g", entry} } // #nosec G204 -- fixed stat executable and test-created path diff --git a/e2e/tests/up/testdata/docker-workspace-env-equals/.devcontainer/devcontainer.json b/e2e/tests/up/testdata/docker-workspace-env-equals/.devcontainer/devcontainer.json new file mode 100644 index 0000000000..2efe0fa439 --- /dev/null +++ b/e2e/tests/up/testdata/docker-workspace-env-equals/.devcontainer/devcontainer.json @@ -0,0 +1,13 @@ +{ + "image": "ghcr.io/devsy-org/test-images/go:1", + "userEnvProbe": "loginInteractiveShell", + "waitFor": "postStartCommand", + "containerEnv": { + "REMOTE_EQ": "inherited=value" + }, + "remoteEnv": { + "REMOTE_EQ": "remote=a=b" + }, + "postCreateCommand": "bash .devcontainer/dump-env.sh postCreate", + "postStartCommand": "bash .devcontainer/dump-env.sh postStart" +} diff --git a/e2e/tests/up/testdata/docker-workspace-env-equals/.devcontainer/dump-env.sh b/e2e/tests/up/testdata/docker-workspace-env-equals/.devcontainer/dump-env.sh new file mode 100644 index 0000000000..eabfa5c71e --- /dev/null +++ b/e2e/tests/up/testdata/docker-workspace-env-equals/.devcontainer/dump-env.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# Writes only the synthetic test variables; never dump the full environment. +phase="$1" +out="$phase.env.out" +: >"$out" +for name in PLAIN ONE_EQ TWO_EQ COMMA_EQ TRAILING_EQ FILE_ONE_EQ REMOTE_EQ; do + printf '%s=%s\n' "$name" "${!name}" >>"$out" +done diff --git a/e2e/tests/up/testdata/docker-workspace-env-equals/workspace.env b/e2e/tests/up/testdata/docker-workspace-env-equals/workspace.env new file mode 100644 index 0000000000..7c308eaa9c --- /dev/null +++ b/e2e/tests/up/testdata/docker-workspace-env-equals/workspace.env @@ -0,0 +1 @@ +FILE_ONE_EQ=k=v diff --git a/hack/ci/suites.yaml b/hack/ci/suites.yaml index 83b3856940..db546ab55b 100644 --- a/hack/ci/suites.yaml +++ b/hack/ci/suites.yaml @@ -143,6 +143,8 @@ e2e: ungated: - label: gh-forwarding reason: sub-label that always accompanies the ssh suite label + - label: env-equals-isolated-home + reason: sub-label that always accompanies the up-provider-docker suite label - label: up-provider-podman-machine-bind reason: darwin-only spec that needs a local Podman machine; run manually on macOS - label: gpg diff --git a/pkg/devcontainer/config/prepareprobe.go b/pkg/devcontainer/config/prepareprobe.go index 18edce7906..fe037eed9b 100644 --- a/pkg/devcontainer/config/prepareprobe.go +++ b/pkg/devcontainer/config/prepareprobe.go @@ -39,8 +39,11 @@ func PrepareCmdUser(cmd *exec.Cmd, userName string) error { func patchEnvVars(env []string, patches map[string]string) []string { newEnv := map[string]string{} for _, v := range env { - t := strings.Split(v, "=") - newEnv[t[0]] = t[1] + name, value, ok := strings.Cut(v, "=") + if !ok || name == "" { + continue + } + newEnv[name] = value } // apply patches diff --git a/pkg/devcontainer/config/prepareprobe_test.go b/pkg/devcontainer/config/prepareprobe_test.go new file mode 100644 index 0000000000..9c442a9d40 --- /dev/null +++ b/pkg/devcontainer/config/prepareprobe_test.go @@ -0,0 +1,157 @@ +//go:build !windows + +package config + +import ( + "maps" + "slices" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + patchEnvTestPlainValue = "hello" + patchEnvTestHome = "HOME" + patchEnvTestUser = "USER" + patchEnvTestLogname = "LOGNAME" + patchEnvTestOkEntry = "OK=1" +) + +func envToMap(t *testing.T, entries []string) map[string]string { + t.Helper() + got := make(map[string]string, len(entries)) + for _, entry := range entries { + name, value, ok := strings.Cut(entry, "=") + require.True(t, ok, "output entry %q has no separator", entry) + _, duplicate := got[name] + require.False(t, duplicate, "output has more than one entry for %q", name) + got[name] = value + } + return got +} + +//nolint:funlen // Table of input vectors. +func TestPatchEnvVars(t *testing.T) { + tests := []struct { + name string + env []string + patches map[string]string + expected map[string]string + }{ + { + name: "plain value", + env: []string{"PLAIN=" + patchEnvTestPlainValue}, + expected: map[string]string{"PLAIN": patchEnvTestPlainValue}, + }, + { + name: "one additional equals sign", + env: []string{"ONE_EQ=a=b"}, + expected: map[string]string{"ONE_EQ": "a=b"}, + }, + { + name: "A=b=c retains the complete value", + env: []string{"A=b=c"}, + expected: map[string]string{"A": "b=c"}, + }, + { + name: "repeated equals signs", + env: []string{"TWO_EQ=a=b=c"}, + expected: map[string]string{"TWO_EQ": "a=b=c"}, + }, + { + name: "commas and equals signs", + env: []string{"COMMA_EQ=x=1,y=2"}, + expected: map[string]string{"COMMA_EQ": "x=1,y=2"}, + }, + { + name: "trailing equals sign", + env: []string{"TRAILING_EQ=z="}, + expected: map[string]string{"TRAILING_EQ": "z="}, + }, + { + name: "value begins with equals", + env: []string{"LEADING_EQ==a"}, + expected: map[string]string{"LEADING_EQ": "=a"}, + }, + { + name: "empty value", + env: []string{"EMPTY="}, + expected: map[string]string{"EMPTY": ""}, + }, + { + name: "whitespace tab and newline are literal", + env: []string{"WS= a=b\t\nc=d "}, + expected: map[string]string{"WS": " a=b\t\nc=d "}, + }, + { + name: "quotes and Unicode are literal", + env: []string{"LITERAL=\"a=b\"'\u00e9'"}, + expected: map[string]string{"LITERAL": "\"a=b\"'\u00e9'"}, + }, + { + name: "empty inherited slice keeps patches", + patches: map[string]string{patchEnvTestHome: "/home/u", patchEnvTestUser: "u"}, + expected: map[string]string{patchEnvTestHome: "/home/u", patchEnvTestUser: "u"}, + }, + { + name: "last duplicate inherited entry wins", + env: []string{"A=first", "A=last=part"}, + expected: map[string]string{"A": "last=part"}, + }, + { + name: "patch overrides inherited value with equals sign", + env: []string{ + patchEnvTestHome + "=/old=dir", + patchEnvTestUser + "=old", + patchEnvTestLogname + "=old", + }, + patches: map[string]string{ + patchEnvTestHome: "/new=dir", + patchEnvTestUser: "user=name", + patchEnvTestLogname: "login=name", + }, + expected: map[string]string{ + patchEnvTestHome: "/new=dir", + patchEnvTestUser: "user=name", + patchEnvTestLogname: "login=name", + }, + }, + { + name: "entry without separator is ignored", + env: []string{"BROKEN", patchEnvTestOkEntry}, + expected: map[string]string{"OK": "1"}, + }, + { + name: "empty entry is ignored", + env: []string{"", patchEnvTestOkEntry}, + expected: map[string]string{"OK": "1"}, + }, + { + name: "empty name is ignored", + env: []string{"=invalid", patchEnvTestOkEntry}, + expected: map[string]string{"OK": "1"}, + }, + { + name: "unrelated key is untouched by patches", + env: []string{"KEEP=a=b", patchEnvTestHome + "=/old"}, + patches: map[string]string{patchEnvTestHome: "/new"}, + expected: map[string]string{"KEEP": "a=b", patchEnvTestHome: "/new"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + envBefore := slices.Clone(tt.env) + patchesBefore := maps.Clone(tt.patches) + + got := patchEnvVars(tt.env, tt.patches) + + assert.Equal(t, tt.expected, envToMap(t, got)) + assert.Equal(t, envBefore, tt.env, "env input mutated") + assert.Equal(t, patchesBefore, tt.patches, "patches input mutated") + }) + } +}