From d06059d4a9ec7237b7a80be0fb8cb63ebbdefcda Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Tue, 29 Jul 2025 12:29:40 +0000 Subject: [PATCH 1/7] chore(agent/agentcontainers): test current prebuilds integration --- agent/agentcontainers/api_test.go | 256 ++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) diff --git a/agent/agentcontainers/api_test.go b/agent/agentcontainers/api_test.go index e6e25ed92558e..0cb380944aeb7 100644 --- a/agent/agentcontainers/api_test.go +++ b/agent/agentcontainers/api_test.go @@ -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) + + 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) +} From ed4065702fb65b1c3171aff21ac998bdbd37a591 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Tue, 29 Jul 2025 13:36:32 +0000 Subject: [PATCH 2/7] fix: appease linter --- agent/agentcontainers/api_test.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/agent/agentcontainers/api_test.go b/agent/agentcontainers/api_test.go index 0cb380944aeb7..fa78de02f4687 100644 --- a/agent/agentcontainers/api_test.go +++ b/agent/agentcontainers/api_test.go @@ -3905,9 +3905,9 @@ func TestDevcontainerPrebuildSupport(t *testing.T) { MergedConfiguration: agentcontainers.DevcontainerMergedConfiguration{ Customizations: agentcontainers.DevcontainerMergedCustomizations{ Coder: []agentcontainers.CoderCustomization{ - agentcontainers.CoderCustomization{ + { Apps: []agentcontainers.SubAgentApp{ - agentcontainers.SubAgentApp{ + { Slug: "zed", URL: prebuildAppURL, }, @@ -3929,10 +3929,8 @@ func TestDevcontainerPrebuildSupport(t *testing.T) { 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 - } + <-ctx.Done() + return nil }), ) @@ -4018,9 +4016,9 @@ func TestDevcontainerPrebuildSupport(t *testing.T) { MergedConfiguration: agentcontainers.DevcontainerMergedConfiguration{ Customizations: agentcontainers.DevcontainerMergedCustomizations{ Coder: []agentcontainers.CoderCustomization{ - agentcontainers.CoderCustomization{ + { Apps: []agentcontainers.SubAgentApp{ - agentcontainers.SubAgentApp{ + { Slug: "zed", URL: userAppURL, }, @@ -4042,10 +4040,8 @@ func TestDevcontainerPrebuildSupport(t *testing.T) { 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 - } + <-ctx.Done() + return nil }), ) From 0d08b468d8ed1d4fcb90ffa1b740600d29811501 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Wed, 30 Jul 2025 11:04:07 +0000 Subject: [PATCH 3/7] fix: skip test on windows --- agent/agentcontainers/api_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/agent/agentcontainers/api_test.go b/agent/agentcontainers/api_test.go index fa78de02f4687..6a5aa8040b9d9 100644 --- a/agent/agentcontainers/api_test.go +++ b/agent/agentcontainers/api_test.go @@ -3822,6 +3822,10 @@ func TestDevcontainerDiscovery(t *testing.T) { func TestDevcontainerPrebuildSupport(t *testing.T) { t.Parallel() + if runtime.GOOS == "windows" { + t.Skip("Dev Container tests are not supported on Windows") + } + var ( ctx = testutil.Context(t, testutil.WaitShort) logger = testutil.Logger(t) @@ -3839,6 +3843,7 @@ func TestDevcontainerPrebuildSupport(t *testing.T) { WorkspaceFolder: "/home/coder/coder", ConfigPath: "/home/coder/coder/.devcontainer/devcontainer.json", } + testContainer = newFakeContainer("test-container-id", testDC.ConfigPath, testDC.WorkspaceFolder) prebuildOwner = "prebuilds" From d744bd3771c7aba05ea88aafff35996ea51ac158 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Wed, 30 Jul 2025 13:56:30 +0000 Subject: [PATCH 4/7] refactor: use fakes instead of mocks --- agent/agentcontainers/api_test.go | 185 +++++++++--------------------- 1 file changed, 55 insertions(+), 130 deletions(-) diff --git a/agent/agentcontainers/api_test.go b/agent/agentcontainers/api_test.go index 6a5aa8040b9d9..340853a91d360 100644 --- a/agent/agentcontainers/api_test.go +++ b/agent/agentcontainers/api_test.go @@ -3827,16 +3827,12 @@ func TestDevcontainerPrebuildSupport(t *testing.T) { } var ( - ctx = testutil.Context(t, testutil.WaitShort) - logger = testutil.Logger(t) - mClock = quartz.NewMock(t) - tickerTrap = mClock.Trap().TickerFunc("updaterLoop") + ctx = testutil.Context(t, testutil.WaitShort) + logger = testutil.Logger(t) - mCtrl = gomock.NewController(t) - mCCLI = acmock.NewMockContainerCLI(mCtrl) - mDCCLI = acmock.NewMockDevcontainerCLI(mCtrl) - - fSAC = &fakeSubAgentClient{} + fDCCLI = &fakeDevcontainerCLI{readConfigErrC: make(chan func(envs []string) error, 1)} + fCCLI = &fakeContainerCLI{arch: runtime.GOARCH} + fSAC = &fakeSubAgentClient{} testDC = codersdk.WorkspaceAgentDevcontainer{ ID: uuid.New(), @@ -3855,17 +3851,12 @@ func TestDevcontainerPrebuildSupport(t *testing.T) { 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) + fCCLI.containers = codersdk.WorkspaceAgentListContainersResponse{} api := agentcontainers.NewAPI(logger, // We want this first `agentcontainers.API` to have a manifest info @@ -3877,72 +3868,43 @@ func TestDevcontainerPrebuildSupport(t *testing.T) { []codersdk.WorkspaceAgentScript{{ID: testDC.ID, LogSourceID: uuid.New()}}, ), agentcontainers.WithSubAgentClient(fSAC), - agentcontainers.WithContainerCLI(mCCLI), - agentcontainers.WithDevcontainerCLI(mDCCLI), - agentcontainers.WithClock(mClock), + agentcontainers.WithContainerCLI(fCCLI), + agentcontainers.WithDevcontainerCLI(fDCCLI), agentcontainers.WithWatcher(watcher.NewNoop()), ) api.Start() - tickerTrap.MustWait(ctx).MustRelease(ctx) - tickerTrap.Close() + fCCLI.containers = codersdk.WorkspaceAgentListContainersResponse{ + Containers: []codersdk.WorkspaceAgentContainer{testContainer}, + } // 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 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{ - { - Apps: []agentcontainers.SubAgentApp{ - { - Slug: "zed", - URL: prebuildAppURL, - }, - }, - }, + fDCCLI.upID = testContainer.ID + fDCCLI.readConfig = agentcontainers.DevcontainerConfig{ + MergedConfiguration: agentcontainers.DevcontainerMergedConfiguration{ + Customizations: agentcontainers.DevcontainerMergedCustomizations{ + Coder: []agentcontainers.CoderCustomization{{ + Apps: []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 { - <-ctx.Done() - return nil - }), - ) + var readConfigEnvVars []string + testutil.RequireSend(ctx, t, fDCCLI.readConfigErrC, func(env []string) error { + readConfigEnvVars = env + return nil + }) // When: We create the dev container resource - err = api.CreateDevcontainer(testDC.WorkspaceFolder, testDC.ConfigPath) + err := api.CreateDevcontainer(testDC.WorkspaceFolder, testDC.ConfigPath) require.NoError(t, err) + require.Contains(t, readConfigEnvVars, "CODER_WORKSPACE_OWNER_NAME="+prebuildOwner) + require.Contains(t, readConfigEnvVars, "CODER_WORKSPACE_NAME="+prebuildWorkspace) + // Then: We there to be only 1 agent. require.Len(t, fSAC.agents, 1) @@ -3968,14 +3930,6 @@ func TestDevcontainerPrebuildSupport(t *testing.T) { // 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 @@ -3987,74 +3941,45 @@ func TestDevcontainerPrebuildSupport(t *testing.T) { []codersdk.WorkspaceAgentScript{{ID: testDC.ID, LogSourceID: uuid.New()}}, ), agentcontainers.WithSubAgentClient(fSAC), - agentcontainers.WithContainerCLI(mCCLI), - agentcontainers.WithDevcontainerCLI(mDCCLI), - agentcontainers.WithClock(mClock), + agentcontainers.WithContainerCLI(fCCLI), + agentcontainers.WithDevcontainerCLI(fDCCLI), agentcontainers.WithWatcher(watcher.NewNoop()), ) api.Start() - defer api.Close() + defer func() { + close(fDCCLI.readConfigErrC) - tickerTrap.MustWait(ctx).MustRelease(ctx) - tickerTrap.Close() + api.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{ - { - Apps: []agentcontainers.SubAgentApp{ - { - Slug: "zed", - URL: userAppURL, - }, - }, - }, + fDCCLI.upID = testContainer.ID + fDCCLI.readConfig = agentcontainers.DevcontainerConfig{ + MergedConfiguration: agentcontainers.DevcontainerMergedConfiguration{ + Customizations: agentcontainers.DevcontainerMergedCustomizations{ + Coder: []agentcontainers.CoderCustomization{{ + Apps: []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 { - <-ctx.Done() - return nil - }), - ) + testutil.RequireSend(ctx, t, fDCCLI.readConfigErrC, func(env []string) error { + readConfigEnvVars = env + 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. + // Then: We expect the environment variables were passed correctly. + require.Contains(t, readConfigEnvVars, "CODER_WORKSPACE_OWNER_NAME="+userOwner) + require.Contains(t, readConfigEnvVars, "CODER_WORKSPACE_NAME="+userWorkspace) + + // And: We expect there to be only 1 agent. require.Len(t, fSAC.agents, 1) // And: We expect _a separate agent_ to have been created. From 9381e2882575ff212671381c29886f4873649775 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Thu, 31 Jul 2025 11:17:27 +0000 Subject: [PATCH 5/7] test: add integration-style test --- agent/agent_test.go | 193 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) diff --git a/agent/agent_test.go b/agent/agent_test.go index d87148be9ad15..9755cf291535c 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -2458,6 +2458,199 @@ func TestAgent_DevcontainersDisabledForSubAgent(t *testing.T) { require.Contains(t, err.Error(), "Dev Container integration inside other Dev Containers is explicitly not supported.") } +// TestAgent_DevcontainerPrebuildClaim tests that we correctly handle +// the claiming process for running devcontainers. +// +// You can run it manually as follows: +// +// CODER_TEST_USE_DOCKER=1 go test -count=1 ./agent -run TestAgent_DevcontainerPrebuildClaim +func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { + if os.Getenv("CODER_TEST_USE_DOCKER") != "1" { + t.Skip("Set CODER_TEST_USE_DOCKER=1 to run this test") + } + if _, err := exec.LookPath("devcontainer"); err != nil { + t.Skip("This test requires the devcontainer CLI: npm install -g @devcontainers/cli") + } + + pool, err := dockertest.NewPool("") + require.NoError(t, err, "Could not connect to docker") + + var ( + ctx = testutil.Context(t, testutil.WaitShort) + + devcontainerID = uuid.New() + devcontainerLogSourceID = uuid.New() + + workspaceFolder = filepath.Join(t.TempDir(), "project") + devcontainerPath = filepath.Join(workspaceFolder, ".devcontainer") + devcontainerConfig = filepath.Join(devcontainerPath, "devcontainer.json") + ) + + // Given: A devcontainer project. + t.Logf("Workspace folder: %s", workspaceFolder) + + err = os.MkdirAll(devcontainerPath, 0o755) + require.NoError(t, err, "create dev container directory") + + err = os.WriteFile(devcontainerConfig, []byte(`{ + "name": "project", + "image": "busybox:latest", + "cmd": ["sleep", "infinity"], + "runArgs": ["--label=`+agentcontainers.DevcontainerIsTestRunLabel+`=true"], + "customizations": { + "coder": { + "apps": [{ + "slug": "zed", + "url": "zed://ssh/${localEnv:CODER_WORKSPACE_AGENT_NAME}.${localEnv:CODER_WORKSPACE_NAME}.${localEnv:CODER_WORKSPACE_OWNER_NAME}.coder${containerWorkspaceFolder}" + }] + } + } + }`), 0o600) + require.NoError(t, err, "write devcontainer config") + + // Given: A manifest with a devcontainer to be started. + manifest := agentsdk.Manifest{ + OwnerName: "prebuilds", + WorkspaceName: "prebuilds-xyz-123", + + Devcontainers: []codersdk.WorkspaceAgentDevcontainer{ + {ID: devcontainerID, Name: "test", WorkspaceFolder: workspaceFolder}, + }, + Scripts: []codersdk.WorkspaceAgentScript{ + {ID: devcontainerID, LogSourceID: devcontainerLogSourceID}, + }, + } + + conn, client, _, _, _ := setupAgent(t, manifest, 0, func(_ *agenttest.Client, o *agent.Options) { + o.Devcontainers = true + o.DevcontainerAPIOptions = append(o.DevcontainerAPIOptions, + agentcontainers.WithContainerLabelIncludeFilter(agentcontainers.DevcontainerLocalFolderLabel, workspaceFolder), + agentcontainers.WithContainerLabelIncludeFilter(agentcontainers.DevcontainerIsTestRunLabel, "true"), + ) + }) + + testutil.Eventually(ctx, t, func(ctx context.Context) bool { + return slices.Contains(client.GetLifecycleStates(), codersdk.WorkspaceAgentLifecycleReady) + }, testutil.IntervalMedium, "agent not ready") + + var dcPrebuild codersdk.WorkspaceAgentDevcontainer + testutil.Eventually(ctx, t, func(ctx context.Context) bool { + resp, err := conn.ListContainers(ctx) + require.NoError(t, err) + + for _, dc := range resp.Devcontainers { + if dc.Container == nil { + continue + } + + v, ok := dc.Container.Labels[agentcontainers.DevcontainerLocalFolderLabel] + if ok && v == workspaceFolder { + dcPrebuild = dc + return true + } + } + + return false + }, testutil.IntervalMedium, "devcontainer not found") + defer func() { + pool.Client.RemoveContainer(docker.RemoveContainerOptions{ + ID: dcPrebuild.Container.ID, + RemoveVolumes: true, + Force: true, + }) + }() + + subAgents := client.GetSubAgents() + require.Len(t, subAgents, 1) + + subAgent := subAgents[0] + subAgentID, err := uuid.FromBytes(subAgent.GetId()) + require.NoError(t, err) + + subAgentApps, err := client.GetSubAgentApps(subAgentID) + require.NoError(t, err) + require.Len(t, subAgentApps, 1) + + subAgentApp := subAgentApps[0] + require.Equal(t, "zed://ssh/project.prebuilds-xyz-123.prebuilds.coder/workspaces/project", subAgentApp.GetUrl()) + + // Close the client and connection + client.Close() + conn.Close() + + // Given: A manifest with a devcontainer to be started. + manifest = agentsdk.Manifest{ + OwnerName: "user", + WorkspaceName: "user-workspace", + + Devcontainers: []codersdk.WorkspaceAgentDevcontainer{ + {ID: devcontainerID, Name: "test", WorkspaceFolder: workspaceFolder}, + }, + Scripts: []codersdk.WorkspaceAgentScript{ + {ID: devcontainerID, LogSourceID: devcontainerLogSourceID}, + }, + } + + conn, client, _, _, _ = setupAgent(t, manifest, 0, func(_ *agenttest.Client, o *agent.Options) { + o.Devcontainers = true + o.DevcontainerAPIOptions = append(o.DevcontainerAPIOptions, + agentcontainers.WithContainerLabelIncludeFilter(agentcontainers.DevcontainerLocalFolderLabel, workspaceFolder), + agentcontainers.WithContainerLabelIncludeFilter(agentcontainers.DevcontainerIsTestRunLabel, "true"), + ) + }) + + testutil.Eventually(ctx, t, func(ctx context.Context) bool { + return slices.Contains(client.GetLifecycleStates(), codersdk.WorkspaceAgentLifecycleReady) + }, testutil.IntervalMedium, "agent not ready") + + var dcClaimed codersdk.WorkspaceAgentDevcontainer + testutil.Eventually(ctx, t, func(ctx context.Context) bool { + resp, err := conn.ListContainers(ctx) + require.NoError(t, err) + + for _, dc := range resp.Devcontainers { + if dc.Container == nil { + continue + } + + v, ok := dc.Container.Labels[agentcontainers.DevcontainerLocalFolderLabel] + if ok && v == workspaceFolder { + dcClaimed = dc + return true + } + } + + return false + }, testutil.IntervalMedium, "devcontainer not found") + defer func() { + if dcClaimed.Container.ID != dcPrebuild.Container.ID { + pool.Client.RemoveContainer(docker.RemoveContainerOptions{ + ID: dcClaimed.Container.ID, + RemoveVolumes: true, + Force: true, + }) + } + }() + + // Then: We expect the claimed devcontainer and prebuild devcontainer + // to be using the same underlying container. + require.Equal(t, dcPrebuild.Container.ID, dcClaimed.Container.ID) + + subAgents = client.GetSubAgents() + require.Len(t, subAgents, 1) + + subAgent = subAgents[0] + subAgentID, err = uuid.FromBytes(subAgent.GetId()) + require.NoError(t, err) + + subAgentApps, err = client.GetSubAgentApps(subAgentID) + require.NoError(t, err) + require.Len(t, subAgentApps, 1) + + subAgentApp = subAgentApps[0] + require.Equal(t, "zed://ssh/project.user-workspace.user.coder/workspaces/project", subAgentApp.GetUrl()) +} + func TestAgent_Dial(t *testing.T) { t.Parallel() From f0dbef112dd3c4ee23b5fb86a3a4dce47a4a1a0a Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Thu, 31 Jul 2025 14:04:45 +0000 Subject: [PATCH 6/7] chore: sprinkle some comments --- agent/agent_test.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/agent/agent_test.go b/agent/agent_test.go index 9755cf291535c..ea13cd3b21c21 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -2492,6 +2492,7 @@ func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { err = os.MkdirAll(devcontainerPath, 0o755) require.NoError(t, err, "create dev container directory") + // Given: This devcontainer project specifies an app that uses the owner name and workspace name. err = os.WriteFile(devcontainerConfig, []byte(`{ "name": "project", "image": "busybox:latest", @@ -2508,7 +2509,7 @@ func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { }`), 0o600) require.NoError(t, err, "write devcontainer config") - // Given: A manifest with a devcontainer to be started. + // Given: A manifest with a prebuild username and workspace name. manifest := agentsdk.Manifest{ OwnerName: "prebuilds", WorkspaceName: "prebuilds-xyz-123", @@ -2521,6 +2522,7 @@ func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { }, } + // When: We create an agent with devcontainers enabled. conn, client, _, _, _ := setupAgent(t, manifest, 0, func(_ *agenttest.Client, o *agent.Options) { o.Devcontainers = true o.DevcontainerAPIOptions = append(o.DevcontainerAPIOptions, @@ -2560,6 +2562,7 @@ func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { }) }() + // Then: We expect a sub agent to have been created. subAgents := client.GetSubAgents() require.Len(t, subAgents, 1) @@ -2567,18 +2570,20 @@ func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { subAgentID, err := uuid.FromBytes(subAgent.GetId()) require.NoError(t, err) + // And: We expect there to be 1 app. subAgentApps, err := client.GetSubAgentApps(subAgentID) require.NoError(t, err) require.Len(t, subAgentApps, 1) + // And: This app should contain the prebuild workspace name and owner name. subAgentApp := subAgentApps[0] require.Equal(t, "zed://ssh/project.prebuilds-xyz-123.prebuilds.coder/workspaces/project", subAgentApp.GetUrl()) - // Close the client and connection + // Given: We close the client and connection client.Close() conn.Close() - // Given: A manifest with a devcontainer to be started. + // Given: A new manifest with a regular user owner name and workspace name. manifest = agentsdk.Manifest{ OwnerName: "user", WorkspaceName: "user-workspace", @@ -2591,6 +2596,7 @@ func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { }, } + // When: We create an agent with devcontainers enabled. conn, client, _, _, _ = setupAgent(t, manifest, 0, func(_ *agenttest.Client, o *agent.Options) { o.Devcontainers = true o.DevcontainerAPIOptions = append(o.DevcontainerAPIOptions, @@ -2636,6 +2642,7 @@ func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { // to be using the same underlying container. require.Equal(t, dcPrebuild.Container.ID, dcClaimed.Container.ID) + // And: We expect there to be a sub agent created. subAgents = client.GetSubAgents() require.Len(t, subAgents, 1) @@ -2643,10 +2650,12 @@ func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { subAgentID, err = uuid.FromBytes(subAgent.GetId()) require.NoError(t, err) + // And: We expect there to be an app. subAgentApps, err = client.GetSubAgentApps(subAgentID) require.NoError(t, err) require.Len(t, subAgentApps, 1) + // And: We expect this app to have the user's owner name and workspace name. subAgentApp = subAgentApps[0] require.Equal(t, "zed://ssh/project.user-workspace.user.coder/workspaces/project", subAgentApp.GetUrl()) } From 9127e2676bbf90eaa0a8d45a4dd801d170dfcb35 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Thu, 31 Jul 2025 14:10:05 +0000 Subject: [PATCH 7/7] chore: sprinkle some nolints --- agent/agent_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/agent/agent_test.go b/agent/agent_test.go index ea13cd3b21c21..15c653d56ad62 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -2464,6 +2464,8 @@ func TestAgent_DevcontainersDisabledForSubAgent(t *testing.T) { // You can run it manually as follows: // // CODER_TEST_USE_DOCKER=1 go test -count=1 ./agent -run TestAgent_DevcontainerPrebuildClaim +// +//nolint:paralleltest // This test sets an environment variable. func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { if os.Getenv("CODER_TEST_USE_DOCKER") != "1" { t.Skip("Set CODER_TEST_USE_DOCKER=1 to run this test") @@ -2523,6 +2525,7 @@ func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { } // When: We create an agent with devcontainers enabled. + //nolint:dogsled conn, client, _, _, _ := setupAgent(t, manifest, 0, func(_ *agenttest.Client, o *agent.Options) { o.Devcontainers = true o.DevcontainerAPIOptions = append(o.DevcontainerAPIOptions, @@ -2597,6 +2600,7 @@ func TestAgent_DevcontainerPrebuildClaim(t *testing.T) { } // When: We create an agent with devcontainers enabled. + //nolint:dogsled conn, client, _, _, _ = setupAgent(t, manifest, 0, func(_ *agenttest.Client, o *agent.Options) { o.Devcontainers = true o.DevcontainerAPIOptions = append(o.DevcontainerAPIOptions, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy