Skip to content

Commit eb29bba

Browse files
committed
fix coderd and cli tests
1 parent 7a3c8a3 commit eb29bba

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

agent/agentcontainers/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,9 @@ func (api *API) cleanupSubAgents(ctx context.Context) error {
869869
if err != nil {
870870
return xerrors.Errorf("list agents: %w", err)
871871
}
872+
if len(agents) == 0 {
873+
return nil
874+
}
872875

873876
api.mu.Lock()
874877
defer api.mu.Unlock()

cli/open_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ func TestOpenVSCodeDevContainer(t *testing.T) {
306306
containerFolder := "/workspace/coder"
307307

308308
ctrl := gomock.NewController(t)
309-
mcl := acmock.NewMockContainerCLI(ctrl)
310-
mcl.EXPECT().List(gomock.Any()).Return(
309+
mccli := acmock.NewMockContainerCLI(ctrl)
310+
mccli.EXPECT().List(gomock.Any()).Return(
311311
codersdk.WorkspaceAgentListContainersResponse{
312312
Containers: []codersdk.WorkspaceAgentContainer{
313313
{
@@ -327,6 +327,8 @@ func TestOpenVSCodeDevContainer(t *testing.T) {
327327
},
328328
}, nil,
329329
).AnyTimes()
330+
// DetectArchitecture always returns "<none>" for this test to disable agent injection.
331+
mccli.EXPECT().DetectArchitecture(gomock.Any(), gomock.Any()).Return("<none>", nil).AnyTimes()
330332

331333
client, workspace, agentToken := setupWorkspaceForAgent(t, func(agents []*proto.Agent) []*proto.Agent {
332334
agents[0].Directory = agentDir
@@ -337,7 +339,7 @@ func TestOpenVSCodeDevContainer(t *testing.T) {
337339

338340
_ = agenttest.New(t, client.URL, agentToken, func(o *agent.Options) {
339341
o.ExperimentalDevcontainersEnabled = true
340-
o.ContainerAPIOptions = append(o.ContainerAPIOptions, agentcontainers.WithContainerCLI(mcl))
342+
o.ContainerAPIOptions = append(o.ContainerAPIOptions, agentcontainers.WithContainerCLI(mccli))
341343
})
342344
_ = coderdtest.NewWorkspaceAgentWaiter(t, client, workspace.ID).Wait()
343345

@@ -481,8 +483,8 @@ func TestOpenVSCodeDevContainer_NoAgentDirectory(t *testing.T) {
481483
containerFolder := "/workspace/coder"
482484

483485
ctrl := gomock.NewController(t)
484-
mcl := acmock.NewMockContainerCLI(ctrl)
485-
mcl.EXPECT().List(gomock.Any()).Return(
486+
mccli := acmock.NewMockContainerCLI(ctrl)
487+
mccli.EXPECT().List(gomock.Any()).Return(
486488
codersdk.WorkspaceAgentListContainersResponse{
487489
Containers: []codersdk.WorkspaceAgentContainer{
488490
{
@@ -502,6 +504,8 @@ func TestOpenVSCodeDevContainer_NoAgentDirectory(t *testing.T) {
502504
},
503505
}, nil,
504506
).AnyTimes()
507+
// DetectArchitecture always returns "<none>" for this test to disable agent injection.
508+
mccli.EXPECT().DetectArchitecture(gomock.Any(), gomock.Any()).Return("<none>", nil).AnyTimes()
505509

506510
client, workspace, agentToken := setupWorkspaceForAgent(t, func(agents []*proto.Agent) []*proto.Agent {
507511
agents[0].Name = agentName
@@ -511,7 +515,7 @@ func TestOpenVSCodeDevContainer_NoAgentDirectory(t *testing.T) {
511515

512516
_ = agenttest.New(t, client.URL, agentToken, func(o *agent.Options) {
513517
o.ExperimentalDevcontainersEnabled = true
514-
o.ContainerAPIOptions = append(o.ContainerAPIOptions, agentcontainers.WithContainerCLI(mcl))
518+
o.ContainerAPIOptions = append(o.ContainerAPIOptions, agentcontainers.WithContainerCLI(mccli))
515519
})
516520
_ = coderdtest.NewWorkspaceAgentWaiter(t, client, workspace.ID).Wait()
517521

coderd/workspaceagents_test.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,14 +1397,15 @@ func TestWorkspaceAgentRecreateDevcontainer(t *testing.T) {
13971397
agentcontainers.DevcontainerConfigFileLabel: configFile,
13981398
}
13991399
devContainer = codersdk.WorkspaceAgentContainer{
1400-
ID: uuid.NewString(),
1401-
CreatedAt: dbtime.Now(),
1402-
FriendlyName: testutil.GetRandomName(t),
1403-
Image: "busybox:latest",
1404-
Labels: dcLabels,
1405-
Running: true,
1406-
Status: "running",
1407-
DevcontainerDirty: true,
1400+
ID: uuid.NewString(),
1401+
CreatedAt: dbtime.Now(),
1402+
FriendlyName: testutil.GetRandomName(t),
1403+
Image: "busybox:latest",
1404+
Labels: dcLabels,
1405+
Running: true,
1406+
Status: "running",
1407+
DevcontainerDirty: true,
1408+
DevcontainerStatus: codersdk.WorkspaceAgentDevcontainerStatusRunning,
14081409
}
14091410
plainContainer = codersdk.WorkspaceAgentContainer{
14101411
ID: uuid.NewString(),
@@ -1419,29 +1420,31 @@ func TestWorkspaceAgentRecreateDevcontainer(t *testing.T) {
14191420

14201421
for _, tc := range []struct {
14211422
name string
1422-
setupMock func(*acmock.MockContainerCLI, *acmock.MockDevcontainerCLI) (status int)
1423+
setupMock func(mccli *acmock.MockContainerCLI, mdccli *acmock.MockDevcontainerCLI) (status int)
14231424
}{
14241425
{
14251426
name: "Recreate",
1426-
setupMock: func(mcl *acmock.MockContainerCLI, mdccli *acmock.MockDevcontainerCLI) int {
1427-
mcl.EXPECT().List(gomock.Any()).Return(codersdk.WorkspaceAgentListContainersResponse{
1427+
setupMock: func(mccli *acmock.MockContainerCLI, mdccli *acmock.MockDevcontainerCLI) int {
1428+
mccli.EXPECT().List(gomock.Any()).Return(codersdk.WorkspaceAgentListContainersResponse{
14281429
Containers: []codersdk.WorkspaceAgentContainer{devContainer},
14291430
}, nil).AnyTimes()
1431+
// DetectArchitecture always returns "<none>" for this test to disable agent injection.
1432+
mccli.EXPECT().DetectArchitecture(gomock.Any(), devContainer.ID).Return("<none>", nil).AnyTimes()
14301433
mdccli.EXPECT().Up(gomock.Any(), workspaceFolder, configFile, gomock.Any()).Return("someid", nil).Times(1)
14311434
return 0
14321435
},
14331436
},
14341437
{
14351438
name: "Container does not exist",
1436-
setupMock: func(mcl *acmock.MockContainerCLI, mdccli *acmock.MockDevcontainerCLI) int {
1437-
mcl.EXPECT().List(gomock.Any()).Return(codersdk.WorkspaceAgentListContainersResponse{}, nil).AnyTimes()
1439+
setupMock: func(mccli *acmock.MockContainerCLI, mdccli *acmock.MockDevcontainerCLI) int {
1440+
mccli.EXPECT().List(gomock.Any()).Return(codersdk.WorkspaceAgentListContainersResponse{}, nil).AnyTimes()
14381441
return http.StatusNotFound
14391442
},
14401443
},
14411444
{
14421445
name: "Not a devcontainer",
1443-
setupMock: func(mcl *acmock.MockContainerCLI, mdccli *acmock.MockDevcontainerCLI) int {
1444-
mcl.EXPECT().List(gomock.Any()).Return(codersdk.WorkspaceAgentListContainersResponse{
1446+
setupMock: func(mccli *acmock.MockContainerCLI, mdccli *acmock.MockDevcontainerCLI) int {
1447+
mccli.EXPECT().List(gomock.Any()).Return(codersdk.WorkspaceAgentListContainersResponse{
14451448
Containers: []codersdk.WorkspaceAgentContainer{plainContainer},
14461449
}, nil).AnyTimes()
14471450
return http.StatusNotFound
@@ -1452,9 +1455,9 @@ func TestWorkspaceAgentRecreateDevcontainer(t *testing.T) {
14521455
t.Parallel()
14531456

14541457
ctrl := gomock.NewController(t)
1455-
mcl := acmock.NewMockContainerCLI(ctrl)
1458+
mccli := acmock.NewMockContainerCLI(ctrl)
14561459
mdccli := acmock.NewMockDevcontainerCLI(ctrl)
1457-
wantStatus := tc.setupMock(mcl, mdccli)
1460+
wantStatus := tc.setupMock(mccli, mdccli)
14581461
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
14591462
client, db := coderdtest.NewWithDatabase(t, &coderdtest.Options{
14601463
Logger: &logger,
@@ -1471,7 +1474,7 @@ func TestWorkspaceAgentRecreateDevcontainer(t *testing.T) {
14711474
o.ExperimentalDevcontainersEnabled = true
14721475
o.ContainerAPIOptions = append(
14731476
o.ContainerAPIOptions,
1474-
agentcontainers.WithContainerCLI(mcl),
1477+
agentcontainers.WithContainerCLI(mccli),
14751478
agentcontainers.WithDevcontainerCLI(mdccli),
14761479
agentcontainers.WithWatcher(watcher.NewNoop()),
14771480
)

0 commit comments

Comments
 (0)