Skip to content
Merged
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
72 changes: 27 additions & 45 deletions pkg/agent/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/<id>/agent); container invocations use
// <home>/contexts/<ctx>/workspaces/<id>. prepareHome selects
// PrepareAgentHomeDir (create) over FindAgentHomeDir (lookup).
func resolveAgentWorkspaceDir(
agentDir, context, workspaceID string,
prepareHome bool,
) (string, error) {
if context == "" {
context = config.DefaultContext
}
Expand All @@ -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 {
Expand Down
203 changes: 203 additions & 0 deletions pkg/agent/workspace_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package agent

import (
"os"
"path/filepath"
"testing"

"github.com/devsy-org/devsy/pkg/config"
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"
Expand Down Expand Up @@ -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/<id>/agent); container
// state is the bare .../workspaces/<id> 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)
}
Loading