Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(agent/agentcontainers): filter out "is test run" devcontainers #18568

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
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion agent/agentcontainers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {

This comment was marked as off-topic.

var ok bool
for label, value := range api.containerLabelIncludeFilter {
if v, found := container.Labels[label]; found && v == value {
Expand Down
26 changes: 26 additions & 0 deletions agent/agentcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}{
{
Expand Down Expand Up @@ -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 {
Expand All @@ -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{
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions agent/agentcontainers/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
Loading