mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix(agent/agentcontainers): filter out "is test run" devcontainers (#18568)
This commit is contained in:
@@ -591,7 +591,8 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
|
|||||||
slog.F("config_file", configFile),
|
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
|
var ok bool
|
||||||
for label, value := range api.containerLabelIncludeFilter {
|
for label, value := range api.containerLabelIncludeFilter {
|
||||||
if v, found := container.Labels[label]; found && v == value {
|
if v, found := container.Labels[label]; found && v == value {
|
||||||
|
|||||||
@@ -749,6 +749,7 @@ func TestAPI(t *testing.T) {
|
|||||||
knownDevcontainers []codersdk.WorkspaceAgentDevcontainer
|
knownDevcontainers []codersdk.WorkspaceAgentDevcontainer
|
||||||
wantStatus int
|
wantStatus int
|
||||||
wantCount int
|
wantCount int
|
||||||
|
wantTestContainer bool
|
||||||
verify func(t *testing.T, devcontainers []codersdk.WorkspaceAgentDevcontainer)
|
verify func(t *testing.T, devcontainers []codersdk.WorkspaceAgentDevcontainer)
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -995,6 +996,13 @@ func TestAPI(t *testing.T) {
|
|||||||
assert.Len(t, names, 4, "should have four unique devcontainer names")
|
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 {
|
for _, tt := range tests {
|
||||||
@@ -1007,6 +1015,18 @@ func TestAPI(t *testing.T) {
|
|||||||
mClock.Set(time.Now()).MustWait(testutil.Context(t, testutil.WaitShort))
|
mClock.Set(time.Now()).MustWait(testutil.Context(t, testutil.WaitShort))
|
||||||
tickerTrap := mClock.Trap().TickerFunc("updaterLoop")
|
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.
|
// Setup router with the handler under test.
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
apiOptions := []agentcontainers.Option{
|
apiOptions := []agentcontainers.Option{
|
||||||
@@ -1016,6 +1036,12 @@ func TestAPI(t *testing.T) {
|
|||||||
agentcontainers.WithWatcher(watcher.NewNoop()),
|
agentcontainers.WithWatcher(watcher.NewNoop()),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tt.wantTestContainer {
|
||||||
|
apiOptions = append(apiOptions, agentcontainers.WithContainerLabelIncludeFilter(
|
||||||
|
agentcontainers.DevcontainerIsTestRunLabel, "true",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
// Generate matching scripts for the known devcontainers
|
// Generate matching scripts for the known devcontainers
|
||||||
// (required to extract log source ID).
|
// (required to extract log source ID).
|
||||||
var scripts []codersdk.WorkspaceAgentScript
|
var scripts []codersdk.WorkspaceAgentScript
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ const (
|
|||||||
// DevcontainerConfigFileLabel is the label that contains the path to
|
// DevcontainerConfigFileLabel is the label that contains the path to
|
||||||
// the devcontainer.json configuration file.
|
// the devcontainer.json configuration file.
|
||||||
DevcontainerConfigFileLabel = "devcontainer.config_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.
|
// The default workspace folder inside the devcontainer.
|
||||||
DevcontainerDefaultContainerWorkspaceFolder = "/workspaces"
|
DevcontainerDefaultContainerWorkspaceFolder = "/workspaces"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user