From 381c4202d2791692536e6d1b6502a1e9d163035b Mon Sep 17 00:00:00 2001 From: "J. Scott Miller" Date: Thu, 11 Jun 2026 15:33:02 -0500 Subject: [PATCH] fix(coderd): deflake TestWorkspaceBuildStatus audit log assertion (#26232) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- coderd/workspacebuilds_test.go | 11 +++++++++-- coderd/workspaces_test.go | 24 ++++++++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/coderd/workspacebuilds_test.go b/coderd/workspacebuilds_test.go index 800076eaff..b625bb6f7c 100644 --- a/coderd/workspacebuilds_test.go +++ b/coderd/workspacebuilds_test.go @@ -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" diff --git a/coderd/workspaces_test.go b/coderd/workspaces_test.go index b1c8136b07..09ad56ca66 100644 --- a/coderd/workspaces_test.go +++ b/coderd/workspaces_test.go @@ -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) }) }