- Notifications
You must be signed in to change notification settings - Fork1k
fix(agent/agentcontainers): fixTestDevcontainerDiscovery/AutoStart
flake#19179
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -71,6 +71,7 @@ func (f *fakeContainerCLI) ExecAs(ctx context.Context, name, user string, args . | ||
// fakeDevcontainerCLI implements the agentcontainers.DevcontainerCLI | ||
// interface for testing. | ||
type fakeDevcontainerCLI struct { | ||
up func(workspaceFolder, configPath string) (string, error) | ||
upID string | ||
upErr error | ||
upErrC chan func() error // If set, send to return err, close to return upErr. | ||
@@ -79,9 +80,14 @@ type fakeDevcontainerCLI struct { | ||
readConfig agentcontainers.DevcontainerConfig | ||
readConfigErr error | ||
readConfigErrC chan func(envs []string) error | ||
configMap map[string]agentcontainers.DevcontainerConfig // By config path | ||
} | ||
func (f *fakeDevcontainerCLI) Up(ctx context.Context, workspaceFolder, configPath string, _ ...agentcontainers.DevcontainerCLIUpOptions) (string, error) { | ||
if f.up != nil { | ||
return f.up(workspaceFolder, configPath) | ||
} | ||
if f.upErrC != nil { | ||
select { | ||
case <-ctx.Done(): | ||
@@ -109,7 +115,12 @@ func (f *fakeDevcontainerCLI) Exec(ctx context.Context, _, _ string, cmd string, | ||
return f.execErr | ||
} | ||
func (f *fakeDevcontainerCLI) ReadConfig(ctx context.Context, _, configPath string, envs []string, _ ...agentcontainers.DevcontainerCLIReadConfigOptions) (agentcontainers.DevcontainerConfig, error) { | ||
if f.configMap != nil { | ||
if v, found := f.configMap[configPath]; found { | ||
return v, f.readConfigErr | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
} | ||
if f.readConfigErrC != nil { | ||
select { | ||
case <-ctx.Done(): | ||
@@ -3576,193 +3587,112 @@ func TestDevcontainerDiscovery(t *testing.T) { | ||
name string | ||
agentDir string | ||
fs map[string]string | ||
configMap map[string]agentcontainers.DevcontainerConfig | ||
expectDevcontainerCount int | ||
expectUpCalledCountint | ||
}{ | ||
{ | ||
name: "SingleEnabled", | ||
agentDir: "/home/coder", | ||
expectDevcontainerCount: 1, | ||
expectUpCalledCount: 1, | ||
fs: map[string]string{ | ||
"/home/coder/.git/HEAD": "", | ||
"/home/coder/.devcontainer/devcontainer.json": "", | ||
}, | ||
configMap: map[string]agentcontainers.DevcontainerConfig{ | ||
"/home/coder/.devcontainer/devcontainer.json": { | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "SingleDisabled", | ||
agentDir: "/home/coder", | ||
expectDevcontainerCount: 1, | ||
expectUpCalledCount: 0, | ||
fs: map[string]string{ | ||
"/home/coder/.git/HEAD": "", | ||
"/home/coder/.devcontainer/devcontainer.json": "", | ||
}, | ||
configMap: map[string]agentcontainers.DevcontainerConfig{ | ||
"/home/coder/.devcontainer/devcontainer.json": { | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "OneEnabledOneDisabled", | ||
agentDir: "/home/coder", | ||
expectDevcontainerCount: 2, | ||
expectUpCalledCount: 1, | ||
fs: map[string]string{ | ||
"/home/coder/.git/HEAD": "", | ||
"/home/coder/.devcontainer/devcontainer.json": "", | ||
"/home/coder/project/.devcontainer.json": "", | ||
}, | ||
configMap: map[string]agentcontainers.DevcontainerConfig{ | ||
"/home/coder/.devcontainer/devcontainer.json": { | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"/home/coder/project/.devcontainer.json": { | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "MultipleEnabled", | ||
agentDir: "/home/coder", | ||
expectDevcontainerCount: 2, | ||
expectUpCalledCount: 2, | ||
fs: map[string]string{ | ||
"/home/coder/.git/HEAD": "", | ||
"/home/coder/.devcontainer/devcontainer.json": "", | ||
"/home/coder/project/.devcontainer.json": "", | ||
}, | ||
configMap: map[string]agentcontainers.DevcontainerConfig{ | ||
"/home/coder/.devcontainer/devcontainer.json": { | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"/home/coder/project/.devcontainer.json": { | ||
Configuration: agentcontainers.DevcontainerConfiguration{ | ||
Customizations: agentcontainers.DevcontainerCustomizations{ | ||
Coder: agentcontainers.CoderCustomization{ | ||
AutoStart: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
@@ -3775,43 +3705,73 @@ func TestDevcontainerDiscovery(t *testing.T) { | ||
ctx = testutil.Context(t, testutil.WaitShort) | ||
logger = testutil.Logger(t) | ||
mClock = quartz.NewMock(t) | ||
upCalledMu sync.Mutex | ||
upCalledFor = map[string]bool{} | ||
fCCLI = &fakeContainerCLI{} | ||
fDCCLI = &fakeDevcontainerCLI{ | ||
configMap: tt.configMap, | ||
up: func(_, configPath string) (string, error) { | ||
upCalledMu.Lock() | ||
upCalledFor[configPath] = true | ||
upCalledMu.Unlock() | ||
return "", nil | ||
}, | ||
} | ||
r = chi.NewRouter() | ||
) | ||
api := agentcontainers.NewAPI(logger, | ||
agentcontainers.WithClock(mClock), | ||
agentcontainers.WithWatcher(watcher.NewNoop()), | ||
agentcontainers.WithFileSystem(initFS(t, tt.fs)), | ||
agentcontainers.WithManifestInfo("owner", "workspace", "parent-agent", "/home/coder"), | ||
agentcontainers.WithContainerCLI(fCCLI), | ||
agentcontainers.WithDevcontainerCLI(fDCCLI), | ||
agentcontainers.WithProjectDiscovery(true), | ||
agentcontainers.WithDiscoveryAutostart(true), | ||
) | ||
api.Start() | ||
r.Mount("/", api.Routes()) | ||
// Given: We allow the discover routing to progress | ||
var got codersdk.WorkspaceAgentListContainersResponse | ||
require.Eventuallyf(t, func() bool { | ||
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx) | ||
rec := httptest.NewRecorder() | ||
r.ServeHTTP(rec, req) | ||
got = codersdk.WorkspaceAgentListContainersResponse{} | ||
err := json.NewDecoder(rec.Body).Decode(&got) | ||
require.NoError(t, err) | ||
upCalledMu.Lock() | ||
upCalledCount := len(upCalledFor) | ||
upCalledMu.Unlock() | ||
return len(got.Devcontainers) >= tt.expectDevcontainerCount && upCalledCount >= tt.expectUpCalledCount | ||
}, testutil.WaitShort, testutil.IntervalFast, "dev containers never found") | ||
// Close the API. We expect this not to fail because we should have finished | ||
// at this point. | ||
err := api.Close() | ||
require.NoError(t, err) | ||
// Then: We expect to find the expected devcontainers | ||
assert.Len(t, got.Devcontainers, tt.expectDevcontainerCount) | ||
// And: We expect `up` to have been called the expected amount of times. | ||
assert.Len(t, upCalledFor, tt.expectUpCalledCount) | ||
// And: `up` was called on the correct containers | ||
for configPath, config := range tt.configMap { | ||
autoStart := config.Configuration.Customizations.Coder.AutoStart | ||
wasUpCalled := upCalledFor[configPath] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Will this handle the case where we call | ||
require.Equal(t, autoStart, wasUpCalled) | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggest | ||
} | ||
}) | ||
} | ||
Uh oh!
There was an error while loading.Please reload this page.