fix: align autostart tests with persisted next_start_at (#26037)

`TestExecutorAutostartOK` and its sibling positive autostart tests
compute the autobuild tick from
`sched.Next(workspace.LatestBuild.CreatedAt)`, but the server persists
`next_start_at` from the build's completion time. When build creation
and completion straddle the schedule's next fire time, the persisted
value advances past the test's tick, the executor's eligibility query
(`next_start_at <= tick`) drops the workspace, and the test fails with
an empty transitions map. This surfaced in flaky test runs.

Add `coderdtest.NextAutostartTick(t, workspace)` which returns
`*workspace.NextStartAt`, and use it across the affected positive
autostart paths in `coderd/autobuild`, `coderd`, and
`enterprise/coderd`.

Generated with assistance from Coder Agents.
This commit is contained in:
Zach
2026-06-05 09:55:32 -06:00
committed by GitHub
parent 1f8a8e8356
commit d1f2dec4ff
4 changed files with 25 additions and 13 deletions
+4 -4
View File
@@ -1315,7 +1315,7 @@ func TestWorkspaceAutobuild(t *testing.T) {
ws = coderdtest.MustTransitionWorkspace(t, client, ws.ID, codersdk.WorkspaceTransitionStart, codersdk.WorkspaceTransitionStop)
// Assert that autostart works when the workspace isn't dormant..
tickTime := sched.Next(ws.LatestBuild.CreatedAt)
tickTime := coderdtest.NextAutostartTick(t, ws)
p, err := coderdtest.GetProvisionerForTags(db, time.Now(), ws.OrganizationID, nil)
require.NoError(t, err)
coderdtest.UpdateProvisionerLastSeenAt(t, db, p.ID, tickTime)
@@ -1518,7 +1518,7 @@ func TestWorkspaceAutobuild(t *testing.T) {
require.NoError(t, err)
// Kick of an autostart build.
tickTime := sched.Next(ws.LatestBuild.CreatedAt)
tickTime := coderdtest.NextAutostartTick(t, ws)
p, err := coderdtest.GetProvisionerForTags(db, time.Now(), ws.OrganizationID, nil)
require.NoError(t, err)
coderdtest.UpdateProvisionerLastSeenAt(t, db, p.ID, tickTime)
@@ -1545,12 +1545,12 @@ func TestWorkspaceAutobuild(t *testing.T) {
// Reset the workspace to the stopped state so we can try
// to autostart again.
coderdtest.MustTransitionWorkspace(t, client, ws.ID, codersdk.WorkspaceTransitionStart, codersdk.WorkspaceTransitionStop, func(req *codersdk.CreateWorkspaceBuildRequest) {
ws = coderdtest.MustTransitionWorkspace(t, client, ws.ID, codersdk.WorkspaceTransitionStart, codersdk.WorkspaceTransitionStop, func(req *codersdk.CreateWorkspaceBuildRequest) {
req.TemplateVersionID = ws.LatestBuild.TemplateVersionID
})
// Force an autostart transition again.
tickTime2 := sched.Next(firstBuild.CreatedAt)
tickTime2 := coderdtest.NextAutostartTick(t, ws)
coderdtest.UpdateProvisionerLastSeenAt(t, db, p.ID, tickTime2)
tickCh <- tickTime2
stats = <-statsCh