Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pkg/evaluator/provider/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package provider

import (
"os"
"testing"

"github.com/docker/docker-agent/pkg/testutil"
)

func TestMain(m *testing.M) {
os.Exit(testutil.RunWithTempHome(m.Run))
}

func TestTempHomeIsolation(t *testing.T) {
testutil.RequireTempHome(t)
}
16 changes: 16 additions & 0 deletions pkg/fake/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fake

import (
"os"
"testing"

"github.com/docker/docker-agent/pkg/testutil"
)

func TestMain(m *testing.M) {
os.Exit(testutil.RunWithTempHome(m.Run))
}

func TestTempHomeIsolation(t *testing.T) {
testutil.RequireTempHome(t)
}
16 changes: 16 additions & 0 deletions pkg/model/provider/anthropic/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package anthropic

import (
"os"
"testing"

"github.com/docker/docker-agent/pkg/testutil"
)

func TestMain(m *testing.M) {
os.Exit(testutil.RunWithTempHome(m.Run))
}

func TestTempHomeIsolation(t *testing.T) {
testutil.RequireTempHome(t)
}
16 changes: 16 additions & 0 deletions pkg/model/provider/base/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package base

import (
"os"
"testing"

"github.com/docker/docker-agent/pkg/testutil"
)

func TestMain(m *testing.M) {
os.Exit(testutil.RunWithTempHome(m.Run))
}

func TestTempHomeIsolation(t *testing.T) {
testutil.RequireTempHome(t)
}
16 changes: 16 additions & 0 deletions pkg/model/provider/dmr/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dmr

import (
"os"
"testing"

"github.com/docker/docker-agent/pkg/testutil"
)

func TestMain(m *testing.M) {
os.Exit(testutil.RunWithTempHome(m.Run))
}

func TestTempHomeIsolation(t *testing.T) {
testutil.RequireTempHome(t)
}
16 changes: 16 additions & 0 deletions pkg/model/provider/openai/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package openai

import (
"os"
"testing"

"github.com/docker/docker-agent/pkg/testutil"
)

func TestMain(m *testing.M) {
os.Exit(testutil.RunWithTempHome(m.Run))
}

func TestTempHomeIsolation(t *testing.T) {
testutil.RequireTempHome(t)
}
16 changes: 16 additions & 0 deletions pkg/remote/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package remote

import (
"os"
"testing"

"github.com/docker/docker-agent/pkg/testutil"
)

func TestMain(m *testing.M) {
os.Exit(testutil.RunWithTempHome(m.Run))
}

func TestTempHomeIsolation(t *testing.T) {
testutil.RequireTempHome(t)
}
7 changes: 6 additions & 1 deletion pkg/runtime/harness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/docker/docker-agent/pkg/session"
"github.com/docker/docker-agent/pkg/session/sqlitestore"
"github.com/docker/docker-agent/pkg/team"
"github.com/docker/docker-agent/pkg/testutil"
"github.com/docker/docker-agent/pkg/tools"
)

Expand Down Expand Up @@ -70,14 +71,18 @@ func TestMain(m *testing.M) {
}
paths.SetConfigDir(configDir)

code := m.Run()
code := testutil.RunWithTempHome(m.Run)

paths.SetConfigDir("")
_ = os.RemoveAll(configDir)
_ = os.RemoveAll(dir)
os.Exit(code)
}

func TestTempHomeIsolation(t *testing.T) {
testutil.RequireTempHome(t)
}

// useHarnessShim points PATH at the shared shim directory and sets the
// stdout the named harness program ("codex" or "claude") emits when the
// runtime invokes it. PATH is prepended, not replaced, so the shim can
Expand Down
12 changes: 8 additions & 4 deletions pkg/runtime/lazy_model_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package runtime
import (
"context"
"errors"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -85,14 +86,17 @@ func TestLazyModelStore_DefersError(t *testing.T) {
// unreachable. This exercises the exact store the request loop and session
// compaction use in production.
func TestLazyModelStore_GatesCustomProvider(t *testing.T) {
// Point the cache at an empty home so there is no on-disk catalog to fall
// back on: a cold start, as in a freshly-spawned pod. t.Setenv forbids
// Point the cache at an empty directory so there is no on-disk catalog to
// fall back on: a cold start, as in a freshly-spawned pod. t.Setenv forbids
// t.Parallel, which is fine here. modelsdev.NewStore resolves the cache
// dir via os.UserCacheDir, which derives from HOME on macOS and from
// XDG_CACHE_HOME or HOME on Linux.
// dir via os.UserCacheDir, which follows HOME on macOS, %LocalAppData% on
// Windows and an absolute XDG_CACHE_HOME on Linux. RunWithTempHome sets
// those for the whole suite, so they must be overridden here too.
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("LocalAppData", filepath.Join(home, "AppData", "Local"))

var store ModelStore = &lazyModelStore{}

Expand Down
48 changes: 48 additions & 0 deletions pkg/skills/cache_isolation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package skills

import (
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestSuiteCacheStaysInsideTempHome is the regression test for the review on
// #5018. TestLoadWithRemoteSources caches the remote skill under
// paths.GetCacheDir(), which follows %LocalAppData% on Windows and an absolute
// XDG_CACHE_HOME on Linux rather than HOME. Re-run it together with the
// isolation guard in a child process whose cache roots are preset outside the
// temporary home: the child suite must resolve its cache inside the temporary
// tree and leave the preset roots empty.
func TestSuiteCacheStaysInsideTempHome(t *testing.T) {
if testing.Short() {
t.Skip("re-runs the test binary")
}

self, err := os.Executable()
require.NoError(t, err)

preset := t.TempDir()
cmd := exec.CommandContext(t.Context(), self,
"-test.run=^(TestTempHomeIsolation|TestLoadWithRemoteSources)$",
"-test.count=1",
"-test.timeout=2m",
)
// The child must create its own temporary home instead of inheriting the
// one this suite runs in; an empty marker value means "not set". The
// presets stand in for a developer's real cache root.
cmd.Env = append(os.Environ(),
"TESTUTIL_TEMP_HOME=",
"XDG_CACHE_HOME="+preset,
"LocalAppData="+preset,
)

output, err := cmd.CombinedOutput()
require.NoError(t, err, "child suite failed:\n%s", output)

entries, err := os.ReadDir(preset)
require.NoError(t, err)
assert.Empty(t, entries, "child suite wrote into the preset cache root %q", preset)
}
7 changes: 6 additions & 1 deletion pkg/skills/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/docker/docker-agent/pkg/httpclient"
"github.com/docker/docker-agent/pkg/testutil"
)

// TestMain swaps the SSRF-safe HTTP client for the loopback-allowing
Expand All @@ -16,7 +17,11 @@ func TestMain(m *testing.M) {
skillsHTTPClient = httpclient.NewSafeClient(remoteHTTPTimeout, true)
transport := http.DefaultTransport.(*http.Transport).Clone()
skillsHTTPClient.Transport = transport
code := m.Run()
code := testutil.RunWithTempHome(m.Run)
transport.CloseIdleConnections()
os.Exit(code)
}

func TestTempHomeIsolation(t *testing.T) {
testutil.RequireTempHome(t)
}
16 changes: 16 additions & 0 deletions pkg/telemetry/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package telemetry

import (
"os"
"testing"

"github.com/docker/docker-agent/pkg/testutil"
)

func TestMain(m *testing.M) {
os.Exit(testutil.RunWithTempHome(m.Run))
}

func TestTempHomeIsolation(t *testing.T) {
testutil.RequireTempHome(t)
}
42 changes: 42 additions & 0 deletions pkg/testutil/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package testutil

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

// TestCacheVariables pins the resolver input for every platform: these are
// the variables os.UserCacheDir (and therefore pkg/paths.GetCacheDir) reads.
func TestCacheVariables(t *testing.T) {
t.Parallel()

home := t.TempDir()
windows := envValue{name: "LocalAppData", value: filepath.Join(home, "AppData", "Local")}
xdg := envValue{name: "XDG_CACHE_HOME", value: filepath.Join(home, ".cache")}

tests := []struct {
goos string
want []envValue
}{
{"windows", []envValue{windows}},
{"linux", []envValue{xdg}},
{"freebsd", []envValue{xdg}},
// The macOS cache directory derives from HOME, which homeVariables
// already redirects.
{"darwin", nil},
}
for _, tt := range tests {
t.Run(tt.goos, func(t *testing.T) {
t.Parallel()

got := cacheVariables(tt.goos, home)
assert.Equal(t, tt.want, got)
for _, variable := range got {
assert.True(t, filepath.IsAbs(variable.value), "%s must be absolute", variable.name)
assert.True(t, withinDir(home, variable.value), "%s=%s must stay inside the temporary home", variable.name, variable.value)
}
})
}
}
Loading