diff --git a/agent/agentcontainers/api.go b/agent/agentcontainers/api.go index 6d2c46b961122..eb003c2ed8d54 100644 --- a/agent/agentcontainers/api.go +++ b/agent/agentcontainers/api.go @@ -571,7 +571,8 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code slog.F("config_file", configFile), ) - if len(api.containerLabelIncludeFilter) > 0 { + // Filter out devcontainer tests, unless explicitly set in include filters. + if len(api.containerLabelIncludeFilter) > 0 || container.Labels[DevcontainerIsTestRunLabel] == "true" { var ok bool for label, value := range api.containerLabelIncludeFilter { if v, found := container.Labels[label]; found && v == value { diff --git a/agent/agentcontainers/api_test.go b/agent/agentcontainers/api_test.go index b6bae46c835c9..e0197db919f8f 100644 --- a/agent/agentcontainers/api_test.go +++ b/agent/agentcontainers/api_test.go @@ -747,6 +747,7 @@ func TestAPI(t *testing.T) { knownDevcontainers []codersdk.WorkspaceAgentDevcontainer wantStatus int wantCount int + wantTestContainer bool verify func(t *testing.T, devcontainers []codersdk.WorkspaceAgentDevcontainer) }{ { @@ -993,6 +994,13 @@ func TestAPI(t *testing.T) { assert.Len(t, names, 4, "should have four unique devcontainer names") }, }, + { + name: "Include test containers", + lister: &fakeContainerCLI{}, + wantStatus: http.StatusOK, + wantTestContainer: true, + wantCount: 1, // Will be appended. + }, } for _, tt := range tests { @@ -1005,6 +1013,18 @@ func TestAPI(t *testing.T) { mClock.Set(time.Now()).MustWait(testutil.Context(t, testutil.WaitShort)) tickerTrap := mClock.Trap().TickerFunc("updaterLoop") + // This container should be ignored unless explicitly included. + tt.lister.containers.Containers = append(tt.lister.containers.Containers, codersdk.WorkspaceAgentContainer{ + ID: "test-container-1", + FriendlyName: "test-container-1", + Running: true, + Labels: map[string]string{ + agentcontainers.DevcontainerLocalFolderLabel: "/workspace/test1", + agentcontainers.DevcontainerConfigFileLabel: "/workspace/test1/.devcontainer/devcontainer.json", + agentcontainers.DevcontainerIsTestRunLabel: "true", + }, + }) + // Setup router with the handler under test. r := chi.NewRouter() apiOptions := []agentcontainers.Option{ @@ -1013,6 +1033,12 @@ func TestAPI(t *testing.T) { agentcontainers.WithWatcher(watcher.NewNoop()), } + if tt.wantTestContainer { + apiOptions = append(apiOptions, agentcontainers.WithContainerLabelIncludeFilter( + agentcontainers.DevcontainerIsTestRunLabel, "true", + )) + } + // Generate matching scripts for the known devcontainers // (required to extract log source ID). var scripts []codersdk.WorkspaceAgentScript diff --git a/agent/agentcontainers/devcontainer.go b/agent/agentcontainers/devcontainer.go index f13963d7b63d7..3980872c32e3e 100644 --- a/agent/agentcontainers/devcontainer.go +++ b/agent/agentcontainers/devcontainer.go @@ -18,6 +18,9 @@ const ( // DevcontainerConfigFileLabel is the label that contains the path to // the devcontainer.json configuration file. DevcontainerConfigFileLabel = "devcontainer.config_file" + // DevcontainerIsTestRunLabel is set if the devcontainer is part of a test + // and should be excluded. + DevcontainerIsTestRunLabel = "devcontainer.is_test_run" // The default workspace folder inside the devcontainer. DevcontainerDefaultContainerWorkspaceFolder = "/workspaces" )