From 88fadbd6d04d0ff69921ad2b1e83011010b0f35a Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sun, 18 Oct 2026 06:16:44 -0600 Subject: [PATCH] refactor(agent): share workspace dir resolution --- pkg/agent/workspace.go | 72 +++++-------- pkg/agent/workspace_test.go | 203 ++++++++++++++++++++++++++++++++++++ 2 files changed, 230 insertions(+), 45 deletions(-) diff --git a/pkg/agent/workspace.go b/pkg/agent/workspace.go index d370cd39c2..5c0c745bdd 100644 --- a/pkg/agent/workspace.go +++ b/pkg/agent/workspace.go @@ -158,7 +158,15 @@ func IsHostAgentInvocation(agentDir string) bool { return !containerDetector() } -func GetAgentBinariesDir(agentDir, context, workspaceID string) (string, error) { +// resolveAgentWorkspaceDir returns the workspace dir for an invocation without +// checking that it exists. Host invocations use the provider layout +// (.../workspaces//agent); container invocations use +// /contexts//workspaces/. prepareHome selects +// PrepareAgentHomeDir (create) over FindAgentHomeDir (lookup). +func resolveAgentWorkspaceDir( + agentDir, context, workspaceID string, + prepareHome bool, +) (string, error) { if context == "" { context = config.DefaultContext } @@ -167,72 +175,46 @@ func GetAgentBinariesDir(agentDir, context, workspaceID string) (string, error) if err != nil { return "", err } - return GetAgentBinariesDirFromWorkspaceDir(workspaceDir) + return workspaceDir, nil } - homeDir, err := FindAgentHomeDir(agentDir) + findHome := FindAgentHomeDir + if prepareHome { + findHome = PrepareAgentHomeDir + } + homeDir, err := findHome(agentDir) if err != nil { return "", err } - workspaceDir := filepath.Join(homeDir, "contexts", context, "workspaces", workspaceID) + return filepath.Join(homeDir, "contexts", context, "workspaces", workspaceID), nil +} +func GetAgentBinariesDir(agentDir, context, workspaceID string) (string, error) { + workspaceDir, err := resolveAgentWorkspaceDir(agentDir, context, workspaceID, false) + if err != nil { + return "", err + } return GetAgentBinariesDirFromWorkspaceDir(workspaceDir) } func GetAgentWorkspaceDir(agentDir, context, workspaceID string) (string, error) { - if context == "" { - context = config.DefaultContext - } - if IsHostAgentInvocation(agentDir) { - workspaceDir, err := provider2.GetWorkspaceAgentDir(context, workspaceID) - if err != nil { - return "", err - } - if _, statErr := os.Stat(workspaceDir); statErr == nil { - return workspaceDir, nil - } - return "", os.ErrNotExist - } - - homeDir, err := FindAgentHomeDir(agentDir) + workspaceDir, err := resolveAgentWorkspaceDir(agentDir, context, workspaceID, false) if err != nil { return "", err } - - workspaceDir := filepath.Join(homeDir, "contexts", context, "workspaces", workspaceID) - - _, err = os.Stat(workspaceDir) - if err == nil { - return workspaceDir, nil + if _, statErr := os.Stat(workspaceDir); statErr != nil { + return "", os.ErrNotExist } - - return "", os.ErrNotExist + return workspaceDir, nil } func CreateAgentWorkspaceDir(agentDir, context, workspaceID string) (string, error) { - if context == "" { - context = config.DefaultContext - } - if IsHostAgentInvocation(agentDir) { - workspaceDir, err := provider2.GetWorkspaceAgentDir(context, workspaceID) - if err != nil { - return "", err - } - // #nosec G301 -- mirrors the legacy 0o755 perms below. - if err := os.MkdirAll(workspaceDir, 0o755); err != nil { - return "", err - } - return workspaceDir, nil - } - - homeDir, err := PrepareAgentHomeDir(agentDir) + workspaceDir, err := resolveAgentWorkspaceDir(agentDir, context, workspaceID, true) if err != nil { return "", err } - workspaceDir := filepath.Join(homeDir, "contexts", context, "workspaces", workspaceID) - // #nosec G301 -- TODO Consider using a more secure permission setting and ownership if needed. err = os.MkdirAll(workspaceDir, 0o755) if err != nil { diff --git a/pkg/agent/workspace_test.go b/pkg/agent/workspace_test.go index dd291959b9..83fa27e98d 100644 --- a/pkg/agent/workspace_test.go +++ b/pkg/agent/workspace_test.go @@ -1,6 +1,7 @@ package agent import ( + "os" "path/filepath" "testing" @@ -8,6 +9,7 @@ import ( provider2 "github.com/devsy-org/devsy/pkg/provider" "github.com/devsy-org/devsy/pkg/types" "github.com/devsy-org/devsy/pkg/util" + "github.com/stretchr/testify/require" ) const explicitAgentDir = "/some/dir" @@ -130,3 +132,204 @@ func TestIsHostAgentInvocation_IgnoresDevsyHome(t *testing.T) { t.Fatal("IsHostAgentInvocation should still report host when only DEVSY_HOME is set") } } + +const ( + workspaceDirTestContext = "dev" + workspaceDirTestID = "ws1" +) + +type workspaceDirMode struct { + name string + host bool +} + +var workspaceDirModes = []workspaceDirMode{ + {name: "container", host: false}, + {name: "host", host: true}, +} + +type workspaceDirCase struct { + name string + context string + id string + workspace bool // create the resolved workspace dir + binaries bool // create binaries/ inside it + wantErr error // nil: success +} + +// workspaceDirRoot prepares a devsy home for one invocation mode. It returns +// the agentDir argument for the public functions and the root holding +// contexts/. Host invocations resolve through DEVSY_HOME and pass "". +func workspaceDirRoot(t *testing.T, host bool) (string, string) { + t.Helper() + root := t.TempDir() + if host { + withContainerDetector(t, func() bool { return false }) + t.Setenv(config.EnvHome, root) + return "", root + } + require.NoError(t, os.MkdirAll(filepath.Join(root, "contexts"), 0o700)) + return root, root +} + +// wantWorkspaceDir is the layout the public functions must resolve to. Host +// state lives under WorkspaceAgentDir (.../workspaces//agent); container +// state is the bare .../workspaces/ directory. +func wantWorkspaceDir(root string, host bool, context, id string) string { + dir := filepath.Join(root, "contexts", context, "workspaces", id) + if host { + return filepath.Join(dir, "agent") + } + return dir +} + +func resolvedContext(context string) string { + if context == "" { + return config.DefaultContext + } + return context +} + +func TestGetAgentBinariesDir(t *testing.T) { + cases := []workspaceDirCase{ + { + name: "binaries present", + context: workspaceDirTestContext, + id: workspaceDirTestID, + workspace: true, + binaries: true, + }, + { + name: "binaries default context", + id: workspaceDirTestID, + workspace: true, + binaries: true, + }, + { + name: "binaries dir not checked", + context: workspaceDirTestContext, + id: workspaceDirTestID, + workspace: true, + }, + { + name: "missing workspace", + context: workspaceDirTestContext, + id: workspaceDirTestID, + wantErr: os.ErrNotExist, + }, + } + for _, mode := range workspaceDirModes { + for _, tc := range cases { + t.Run(mode.name+"/"+tc.name, func(t *testing.T) { + agentDir, root := workspaceDirRoot(t, mode.host) + dir := wantWorkspaceDir(root, mode.host, resolvedContext(tc.context), tc.id) + if tc.workspace { + require.NoError(t, os.MkdirAll(dir, 0o700)) + } + if tc.binaries { + require.NoError(t, os.MkdirAll(filepath.Join(dir, "binaries"), 0o700)) + } + + got, err := GetAgentBinariesDir(agentDir, tc.context, tc.id) + if tc.wantErr != nil { + require.ErrorIs(t, err, tc.wantErr) + require.Equal(t, tc.wantErr, err) + require.Empty(t, got) + return + } + require.NoError(t, err) + require.Equal(t, filepath.Join(dir, "binaries"), got) + }) + } + } +} + +func TestGetAgentWorkspaceDir(t *testing.T) { + cases := []workspaceDirCase{ + { + name: "present", + context: workspaceDirTestContext, + id: workspaceDirTestID, + workspace: true, + }, + { + name: "workspace default context", + id: workspaceDirTestID, + workspace: true, + }, + { + name: "missing", + context: workspaceDirTestContext, + id: workspaceDirTestID, + wantErr: os.ErrNotExist, + }, + } + for _, mode := range workspaceDirModes { + for _, tc := range cases { + t.Run(mode.name+"/"+tc.name, func(t *testing.T) { + agentDir, root := workspaceDirRoot(t, mode.host) + dir := wantWorkspaceDir(root, mode.host, resolvedContext(tc.context), tc.id) + if tc.workspace { + require.NoError(t, os.MkdirAll(dir, 0o700)) + } + + got, err := GetAgentWorkspaceDir(agentDir, tc.context, tc.id) + if tc.wantErr != nil { + require.ErrorIs(t, err, tc.wantErr) + require.Equal(t, tc.wantErr, err) + require.Empty(t, got) + return + } + require.NoError(t, err) + require.Equal(t, dir, got) + }) + } + } +} + +func TestCreateAgentWorkspaceDir(t *testing.T) { + cases := []workspaceDirCase{ + { + name: "creates dir", + context: workspaceDirTestContext, + id: workspaceDirTestID, + }, + { + name: "create default context", + id: workspaceDirTestID, + }, + } + for _, mode := range workspaceDirModes { + for _, tc := range cases { + t.Run(mode.name+"/"+tc.name, func(t *testing.T) { + agentDir, root := workspaceDirRoot(t, mode.host) + want := wantWorkspaceDir(root, mode.host, resolvedContext(tc.context), tc.id) + + got, err := CreateAgentWorkspaceDir(agentDir, tc.context, tc.id) + require.NoError(t, err) + require.Equal(t, want, got) + require.DirExists(t, got) + }) + } + } +} + +// Container invocations without contexts/ under agentDir: Get* must report no +// devsy home, while Create* prepares the directory as home. +func TestAgentWorkspaceDir_FindVsPrepareHome(t *testing.T) { + root := t.TempDir() + withContainerDetector(t, func() bool { return true }) + + _, err := GetAgentWorkspaceDir(root, workspaceDirTestContext, workspaceDirTestID) + require.ErrorIs(t, err, ErrFindAgentHomeDir) + _, err = GetAgentBinariesDir(root, workspaceDirTestContext, workspaceDirTestID) + require.ErrorIs(t, err, ErrFindAgentHomeDir) + + got, err := CreateAgentWorkspaceDir(root, workspaceDirTestContext, workspaceDirTestID) + require.NoError(t, err) + want := filepath.Join( + root, "contexts", workspaceDirTestContext, "workspaces", workspaceDirTestID, + ) + require.Equal(t, want, got) + require.DirExists(t, got) +}