Skip to content

chore(agent/agentcontainers): test current prebuilds integration #19074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 31, 2025
Merged
Changes from 1 commit
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
Next Next commit
chore(agent/agentcontainers): test current prebuilds integration
  • Loading branch information
DanielleMaywood committed Jul 31, 2025
commit d06059d4a9ec7237b7a80be0fb8cb63ebbdefcda
256 changes: 256 additions & 0 deletions agent/agentcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3815,3 +3815,259 @@ func TestDevcontainerDiscovery(t *testing.T) {
}
})
}

// TestDevcontainerPrebuildSupport validates that devcontainers survive the transition
// from prebuild to claimed workspace, ensuring the existing container is reused
// with updated configuration rather than being recreated.
func TestDevcontainerPrebuildSupport(t *testing.T) {
t.Parallel()

var (
ctx = testutil.Context(t, testutil.WaitShort)
logger = testutil.Logger(t)
mClock = quartz.NewMock(t)
tickerTrap = mClock.Trap().TickerFunc("updaterLoop")

mCtrl = gomock.NewController(t)
mCCLI = acmock.NewMockContainerCLI(mCtrl)
mDCCLI = acmock.NewMockDevcontainerCLI(mCtrl)

fSAC = &fakeSubAgentClient{}

testDC = codersdk.WorkspaceAgentDevcontainer{
ID: uuid.New(),
WorkspaceFolder: "/home/coder/coder",
ConfigPath: "/home/coder/coder/.devcontainer/devcontainer.json",
}
testContainer = newFakeContainer("test-container-id", testDC.ConfigPath, testDC.WorkspaceFolder)

prebuildOwner = "prebuilds"
prebuildWorkspace = "prebuilds-xyz-123"
prebuildAppURL = "prebuilds.zed"

userOwner = "user"
userWorkspace = "user-workspace"
userAppURL = "user.zed"
)

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

// ==================================================
// PHASE 1: Prebuild workspace creates devcontainer
// ==================================================

// Given: There are no containers initially.
mCCLI.EXPECT().List(gomock.Any()).Return(codersdk.WorkspaceAgentListContainersResponse{
Containers: []codersdk.WorkspaceAgentContainer{},
}, nil)

api := agentcontainers.NewAPI(logger,
// We want this first `agentcontainers.API` to have a manifest info
// that is consistent with what a prebuild workspace would have.
agentcontainers.WithManifestInfo(prebuildOwner, prebuildWorkspace, "dev", "/home/coder"),
// Given: We start with a single dev container resource.
agentcontainers.WithDevcontainers(
[]codersdk.WorkspaceAgentDevcontainer{testDC},
[]codersdk.WorkspaceAgentScript{{ID: testDC.ID, LogSourceID: uuid.New()}},
),
agentcontainers.WithSubAgentClient(fSAC),
agentcontainers.WithContainerCLI(mCCLI),
agentcontainers.WithDevcontainerCLI(mDCCLI),
agentcontainers.WithClock(mClock),
agentcontainers.WithWatcher(watcher.NewNoop()),
)
api.Start()

tickerTrap.MustWait(ctx).MustRelease(ctx)
tickerTrap.Close()

// Given: We allow the dev container to be created.
mDCCLI.EXPECT().Up(gomock.Any(), testDC.WorkspaceFolder, testDC.ConfigPath, gomock.Any()).
Return("test-container-id", nil)

mCCLI.EXPECT().List(gomock.Any()).Return(codersdk.WorkspaceAgentListContainersResponse{
Containers: []codersdk.WorkspaceAgentContainer{testContainer},
}, nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these also be InOrder?


gomock.InOrder(
mCCLI.EXPECT().DetectArchitecture(gomock.Any(), "test-container-id").Return(runtime.GOARCH, nil),

// Verify prebuild environment variables are passed to devcontainer
mDCCLI.EXPECT().ReadConfig(gomock.Any(),
testDC.WorkspaceFolder,
testDC.ConfigPath,
gomock.Cond(func(envs []string) bool {
return slices.Contains(envs, "CODER_WORKSPACE_OWNER_NAME="+prebuildOwner) &&
slices.Contains(envs, "CODER_WORKSPACE_NAME="+prebuildWorkspace)
}),
).Return(agentcontainers.DevcontainerConfig{
MergedConfiguration: agentcontainers.DevcontainerMergedConfiguration{
Customizations: agentcontainers.DevcontainerMergedCustomizations{
Coder: []agentcontainers.CoderCustomization{
agentcontainers.CoderCustomization{
Apps: []agentcontainers.SubAgentApp{
agentcontainers.SubAgentApp{
Slug: "zed",
URL: prebuildAppURL,
},
},
},
},
},
},
}, nil),

mCCLI.EXPECT().ExecAs(gomock.Any(), testContainer.ID, "root", "mkdir", "-p", "/.coder-agent").Return(nil, nil),
mCCLI.EXPECT().Copy(gomock.Any(), testContainer.ID, coderBin, "/.coder-agent/coder").Return(nil),
mCCLI.EXPECT().ExecAs(gomock.Any(), testContainer.ID, "root", "chmod", "0755", "/.coder-agent", "/.coder-agent/coder").Return(nil, nil),
mCCLI.EXPECT().ExecAs(gomock.Any(), testContainer.ID, "root", "/bin/sh", "-c", "chown $(id -u):$(id -g) /.coder-agent/coder").Return(nil, nil),

// We want to mock how the `Exec` function works when starting an agent. This should
// run until the given `ctx` is done.
mDCCLI.EXPECT().Exec(gomock.Any(),
testDC.WorkspaceFolder, testDC.ConfigPath,
"/.coder-agent/coder", []string{"agent"}, gomock.Any(), gomock.Any(),
).Do(func(ctx context.Context, _, _, _ string, _ []string, _ ...agentcontainers.DevcontainerCLIExecOptions) error {
select {
case <-ctx.Done():
return nil
}
}),
)

// When: We create the dev container resource
err = api.CreateDevcontainer(testDC.WorkspaceFolder, testDC.ConfigPath)
require.NoError(t, err)

// Then: We there to be only 1 agent.
require.Len(t, fSAC.agents, 1)

// And: We expect only 1 agent to have been created.
require.Len(t, fSAC.created, 1)
firstAgent := fSAC.created[0]

// And: We expect this agent to be the current agent.
_, found := fSAC.agents[firstAgent.ID]
require.True(t, found, "first agent expected to be current agent")

// And: We expect there to be a single app.
require.Len(t, firstAgent.Apps, 1)
firstApp := firstAgent.Apps[0]

// And: We expect this app to have the pre-claim URL.
require.Equal(t, prebuildAppURL, firstApp.URL)

// Given: We now close the API
api.Close()

// =============================================================
// PHASE 2: User claims workspace, devcontainer should be reused
// =============================================================

// Given: We have a running container.
mCCLI.EXPECT().List(gomock.Any()).Return(codersdk.WorkspaceAgentListContainersResponse{
Containers: []codersdk.WorkspaceAgentContainer{testContainer},
}, nil)

mClock = quartz.NewMock(t)
tickerTrap = mClock.Trap().TickerFunc("updaterLoop")

// Given: We create a new claimed API
api = agentcontainers.NewAPI(logger,
// We want this second `agentcontainers.API` to have a manifest info
// that is consistent with what a claimed workspace would have.
agentcontainers.WithManifestInfo(userOwner, userWorkspace, "dev", "/home/coder"),
// Given: We start with a single dev container resource.
agentcontainers.WithDevcontainers(
[]codersdk.WorkspaceAgentDevcontainer{testDC},
[]codersdk.WorkspaceAgentScript{{ID: testDC.ID, LogSourceID: uuid.New()}},
),
agentcontainers.WithSubAgentClient(fSAC),
agentcontainers.WithContainerCLI(mCCLI),
agentcontainers.WithDevcontainerCLI(mDCCLI),
agentcontainers.WithClock(mClock),
agentcontainers.WithWatcher(watcher.NewNoop()),
)
api.Start()
defer api.Close()

tickerTrap.MustWait(ctx).MustRelease(ctx)
tickerTrap.Close()

// Given: We allow the dev container to be created.
mDCCLI.EXPECT().Up(gomock.Any(), testDC.WorkspaceFolder, testDC.ConfigPath, gomock.Any()).
Return("test-container-id", nil)

mCCLI.EXPECT().List(gomock.Any()).Return(codersdk.WorkspaceAgentListContainersResponse{
Containers: []codersdk.WorkspaceAgentContainer{testContainer},
}, nil)

gomock.InOrder(
mCCLI.EXPECT().DetectArchitecture(gomock.Any(), "test-container-id").Return(runtime.GOARCH, nil),

// Verify claimed workspace environment variables are passed to devcontainer
mDCCLI.EXPECT().ReadConfig(gomock.Any(),
testDC.WorkspaceFolder,
testDC.ConfigPath,
gomock.Cond(func(envs []string) bool {
return slices.Contains(envs, "CODER_WORKSPACE_OWNER_NAME="+userOwner) &&
slices.Contains(envs, "CODER_WORKSPACE_NAME="+userWorkspace)
}),
).Return(agentcontainers.DevcontainerConfig{
MergedConfiguration: agentcontainers.DevcontainerMergedConfiguration{
Customizations: agentcontainers.DevcontainerMergedCustomizations{
Coder: []agentcontainers.CoderCustomization{
agentcontainers.CoderCustomization{
Apps: []agentcontainers.SubAgentApp{
agentcontainers.SubAgentApp{
Slug: "zed",
URL: userAppURL,
},
},
},
},
},
},
}, nil),

mCCLI.EXPECT().ExecAs(gomock.Any(), testContainer.ID, "root", "mkdir", "-p", "/.coder-agent").Return(nil, nil),
mCCLI.EXPECT().Copy(gomock.Any(), testContainer.ID, coderBin, "/.coder-agent/coder").Return(nil),
mCCLI.EXPECT().ExecAs(gomock.Any(), testContainer.ID, "root", "chmod", "0755", "/.coder-agent", "/.coder-agent/coder").Return(nil, nil),
mCCLI.EXPECT().ExecAs(gomock.Any(), testContainer.ID, "root", "/bin/sh", "-c", "chown $(id -u):$(id -g) /.coder-agent/coder").Return(nil, nil),

// We want to mock how the `Exec` function works when starting an agent. This should
// run until the given `ctx` is done.
mDCCLI.EXPECT().Exec(gomock.Any(),
testDC.WorkspaceFolder, testDC.ConfigPath,
"/.coder-agent/coder", []string{"agent"}, gomock.Any(), gomock.Any(),
).Do(func(ctx context.Context, _, _, _ string, _ []string, _ ...agentcontainers.DevcontainerCLIExecOptions) error {
select {
case <-ctx.Done():
return nil
}
}),
)

// When: We create the dev container resource.
err = api.CreateDevcontainer(testDC.WorkspaceFolder, testDC.ConfigPath)
require.NoError(t, err)

// Then: We expect there to be only 1 agent.
require.Len(t, fSAC.agents, 1)

// And: We expect _a separate agent_ to have been created.
require.Len(t, fSAC.created, 2)
secondAgent := fSAC.created[1]

// And: We expect this new agent to be the current agent.
_, found = fSAC.agents[secondAgent.ID]
require.True(t, found, "second agent expected to be current agent")

// And: We expect there to be a single app.
require.Len(t, secondAgent.Apps, 1)
secondApp := secondAgent.Apps[0]

// And: We expect this app to have the post-claim URL.
require.Equal(t, userAppURL, secondApp.URL)
}