fix(coderd): deflake TestWorkspaceBuildStatus audit log assertion (#26232)

Fixes a flake in `TestWorkspaceBuildStatus` where the test asserted an
exact audit log count immediately after the stop build completed:

```
workspacebuilds_test.go:1261: Error: "[...]" should have 7 item(s), but has 6
```

The audit log for a workspace build is exported asynchronously relative
to what `AwaitWorkspaceBuildJobCompleted` observes, so the strict
`require.Len` could run before the stop log was recorded. The assertion
now polls with `require.Eventually` until the expected log count and
stop action appear, matching the existing poll pattern in the file.

Also fixes the same race in
`TestWorkspaceDormant/StartWakesUpDormantWorkspace`
(`workspaces_test.go`), flagged during review as a sibling risk: its
exact `require.Len(t, auditor.AuditLogs(), 2)` after build completion is
now an equivalent `require.Eventually` poll.

Verified with `go test ./coderd -run TestWorkspaceBuildStatus -count=10`
and `go test ./coderd -run
'TestWorkspaceDormant/StartWakesUpDormantWorkspace' -count=5`.

Closes https://github.com/coder/internal/issues/1565 (PLAT-304).

🤖 Generated by Coder Agents on behalf of @jscottmiller
This commit is contained in:
J. Scott Miller
2026-06-11 15:33:02 -05:00
committed by GitHub
parent 9b351c3602
commit 381c4202d2
2 changed files with 23 additions and 12 deletions
+9 -2
View File
@@ -1258,8 +1258,15 @@ func TestWorkspaceBuildStatus(t *testing.T) {
// assert an audit log has been created for workspace stopping
numLogs++ // add an audit log for workspace_build stop
require.Len(t, auditor.AuditLogs(), numLogs)
require.Equal(t, database.AuditActionStop, auditor.AuditLogs()[numLogs-1].Action)
// Audit logs are written asynchronously to build completion, so poll
// until the expected log appears.
require.Eventually(t, func() bool {
return len(auditor.AuditLogs()) == numLogs &&
auditor.Contains(t, database.AuditLog{
Action: database.AuditActionStop,
ResourceType: database.ResourceTypeWorkspaceBuild,
})
}, testutil.WaitShort, testutil.IntervalFast)
_ = closeDaemon.Close()
// after successful cancel is "canceled"
+14 -10
View File
@@ -4610,16 +4610,20 @@ func TestWorkspaceDormant(t *testing.T) {
require.NoError(t, err, "fetch updated workspace")
require.Nil(t, updatedWs.DormantAt)
// There should be an audit log for both the dormancy update and the start.
require.Len(t, auditor.AuditLogs(), 2)
require.True(t, auditor.Contains(t, database.AuditLog{
Action: database.AuditActionWrite,
ResourceType: database.ResourceTypeWorkspace,
}))
require.True(t, auditor.Contains(t, database.AuditLog{
Action: database.AuditActionStart,
ResourceType: database.ResourceTypeWorkspaceBuild,
}))
// There should be an audit log for both the dormancy update and the
// start. Audit logs are written asynchronously to build completion,
// so poll until both appear.
require.Eventually(t, func() bool {
return len(auditor.AuditLogs()) == 2 &&
auditor.Contains(t, database.AuditLog{
Action: database.AuditActionWrite,
ResourceType: database.ResourceTypeWorkspace,
}) &&
auditor.Contains(t, database.AuditLog{
Action: database.AuditActionStart,
ResourceType: database.ResourceTypeWorkspaceBuild,
})
}, testutil.WaitShort, testutil.IntervalFast)
})
}