Skip to content

Commit 434b546

Browse files
authored
fix(agent/agentcontainers): filter out "is test run" devcontainers (#18568)
1 parent 42fd1c1 commit 434b546

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

agent/agentcontainers/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
591591
slog.F("config_file", configFile),
592592
)
593593

594-
if len(api.containerLabelIncludeFilter) > 0 {
594+
// Filter out devcontainer tests, unless explicitly set in include filters.
595+
if len(api.containerLabelIncludeFilter) > 0 || container.Labels[DevcontainerIsTestRunLabel] == "true" {
595596
var ok bool
596597
for label, value := range api.containerLabelIncludeFilter {
597598
if v, found := container.Labels[label]; found && v == value {

agent/agentcontainers/api_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ func TestAPI(t *testing.T) {
749749
knownDevcontainers []codersdk.WorkspaceAgentDevcontainer
750750
wantStatus int
751751
wantCount int
752+
wantTestContainer bool
752753
verify func(t *testing.T, devcontainers []codersdk.WorkspaceAgentDevcontainer)
753754
}{
754755
{
@@ -995,6 +996,13 @@ func TestAPI(t *testing.T) {
995996
assert.Len(t, names, 4, "should have four unique devcontainer names")
996997
},
997998
},
999+
{
1000+
name: "Include test containers",
1001+
lister: &fakeContainerCLI{},
1002+
wantStatus: http.StatusOK,
1003+
wantTestContainer: true,
1004+
wantCount: 1, // Will be appended.
1005+
},
9981006
}
9991007

10001008
for _, tt := range tests {
@@ -1007,6 +1015,18 @@ func TestAPI(t *testing.T) {
10071015
mClock.Set(time.Now()).MustWait(testutil.Context(t, testutil.WaitShort))
10081016
tickerTrap := mClock.Trap().TickerFunc("updaterLoop")
10091017

1018+
// This container should be ignored unless explicitly included.
1019+
tt.lister.containers.Containers = append(tt.lister.containers.Containers, codersdk.WorkspaceAgentContainer{
1020+
ID: "test-container-1",
1021+
FriendlyName: "test-container-1",
1022+
Running: true,
1023+
Labels: map[string]string{
1024+
agentcontainers.DevcontainerLocalFolderLabel: "/workspace/test1",
1025+
agentcontainers.DevcontainerConfigFileLabel: "/workspace/test1/.devcontainer/devcontainer.json",
1026+
agentcontainers.DevcontainerIsTestRunLabel: "true",
1027+
},
1028+
})
1029+
10101030
// Setup router with the handler under test.
10111031
r := chi.NewRouter()
10121032
apiOptions := []agentcontainers.Option{
@@ -1016,6 +1036,12 @@ func TestAPI(t *testing.T) {
10161036
agentcontainers.WithWatcher(watcher.NewNoop()),
10171037
}
10181038

1039+
if tt.wantTestContainer {
1040+
apiOptions = append(apiOptions, agentcontainers.WithContainerLabelIncludeFilter(
1041+
agentcontainers.DevcontainerIsTestRunLabel, "true",
1042+
))
1043+
}
1044+
10191045
// Generate matching scripts for the known devcontainers
10201046
// (required to extract log source ID).
10211047
var scripts []codersdk.WorkspaceAgentScript

agent/agentcontainers/devcontainer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const (
1818
// DevcontainerConfigFileLabel is the label that contains the path to
1919
// the devcontainer.json configuration file.
2020
DevcontainerConfigFileLabel = "devcontainer.config_file"
21+
// DevcontainerIsTestRunLabel is set if the devcontainer is part of a test
22+
// and should be excluded.
23+
DevcontainerIsTestRunLabel = "devcontainer.is_test_run"
2124
// The default workspace folder inside the devcontainer.
2225
DevcontainerDefaultContainerWorkspaceFolder = "/workspaces"
2326
)

0 commit comments

Comments
 (0)