@@ -239,7 +239,7 @@ func TestWorkspaceAutobuild(t *testing.T) {
239
239
require .Len (t , stats .Transitions , 0 )
240
240
})
241
241
242
- t .Run ("InactiveTTLOK " , func (t * testing.T ) {
242
+ t .Run ("DormancyThresholdOK " , func (t * testing.T ) {
243
243
t .Parallel ()
244
244
245
245
var (
@@ -274,8 +274,8 @@ func TestWorkspaceAutobuild(t *testing.T) {
274
274
coderdtest .AwaitTemplateVersionJobCompleted (t , client , version .ID )
275
275
276
276
ws := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
277
- build := coderdtest . AwaitWorkspaceBuildJobCompleted ( t , client , ws . LatestBuild . ID )
278
- require . Equal (t , codersdk . WorkspaceStatusRunning , build . Status )
277
+
278
+ coderdtest . AwaitWorkspaceBuildJobCompleted (t , client , ws . LatestBuild . ID )
279
279
280
280
// Reset the audit log so we can verify a log is generated.
281
281
auditRecorder .ResetLogs ()
@@ -287,29 +287,43 @@ func TestWorkspaceAutobuild(t *testing.T) {
287
287
// failure TTL.
288
288
require .Len (t , stats .Transitions , 1 )
289
289
require .Equal (t , stats .Transitions [ws .ID ], database .WorkspaceTransitionStop )
290
- require .Len (t , auditRecorder .AuditLogs (), 1 )
291
-
292
- auditLog := auditRecorder .AuditLogs ()[0 ]
293
- require .Equal (t , auditLog .Action , database .AuditActionWrite )
294
-
295
- var fields audit.AdditionalFields
296
- err := json .Unmarshal (auditLog .AdditionalFields , & fields )
297
- require .NoError (t , err )
298
- require .Equal (t , ws .Name , fields .WorkspaceName )
299
- require .Equal (t , database .BuildReasonAutolock , fields .BuildReason )
300
290
301
- // The workspace should be dormant.
302
291
ws = coderdtest .MustWorkspace (t , client , ws .ID )
303
292
require .NotNil (t , ws .DormantAt )
304
- lastUsedAt := ws .LastUsedAt
305
293
306
- err = client .UpdateWorkspaceDormancy (ctx , ws .ID , codersdk.UpdateWorkspaceDormancy {Dormant : false })
294
+ coderdtest .AwaitWorkspaceBuildJobCompleted (t , client , ws .LatestBuild .ID )
295
+ // We should get 2 audit logs, one for stopping the workspace, and one for
296
+ // making it dormant.
297
+ alogs := auditRecorder .AuditLogs ()
298
+ require .Len (t , alogs , 2 )
299
+
300
+ for _ , alog := range alogs {
301
+ require .Equal (t , int32 (http .StatusOK ), alog .StatusCode )
302
+
303
+ switch alog .Action {
304
+ case database .AuditActionWrite :
305
+ require .Equal (t , database .ResourceTypeWorkspace , alog .ResourceType )
306
+ case database .AuditActionStop :
307
+ var fields audit.AdditionalFields
308
+ err := json .Unmarshal (alog .AdditionalFields , & fields )
309
+ require .NoError (t , err )
310
+ require .Equal (t , ws .Name , fields .WorkspaceName )
311
+ require .Equal (t , database .BuildReasonAutolock , fields .BuildReason )
312
+
313
+ default :
314
+ t .Fatalf ("unexpected audit log (%+v)" , alog )
315
+ }
316
+ }
317
+
318
+ dormantLastUsedAt := ws .LastUsedAt
319
+ // nolint:gocritic // this test is not testing RBAC.
320
+ err := client .UpdateWorkspaceDormancy (ctx , ws .ID , codersdk.UpdateWorkspaceDormancy {Dormant : false })
307
321
require .NoError (t , err )
308
322
309
323
// Assert that we updated our last_used_at so that we don't immediately
310
324
// retrigger another lock action.
311
325
ws = coderdtest .MustWorkspace (t , client , ws .ID )
312
- require .True (t , ws .LastUsedAt .After (lastUsedAt ))
326
+ require .True (t , ws .LastUsedAt .After (dormantLastUsedAt ))
313
327
})
314
328
315
329
// This test serves as a regression prevention for generating
@@ -390,7 +404,7 @@ func TestWorkspaceAutobuild(t *testing.T) {
390
404
}
391
405
})
392
406
393
- t .Run ("InactiveTTLTooEarly " , func (t * testing.T ) {
407
+ t .Run ("DormancyThresholdTooEarly " , func (t * testing.T ) {
394
408
t .Parallel ()
395
409
396
410
var (
@@ -473,7 +487,7 @@ func TestWorkspaceAutobuild(t *testing.T) {
473
487
474
488
// Assert that a stopped workspace that breaches the inactivity threshold
475
489
// does not trigger a build transition but is still placed in the
476
- // lock state.
490
+ // dormant state.
477
491
t .Run ("InactiveStoppedWorkspaceNoTransition" , func (t * testing.T ) {
478
492
t .Parallel ()
479
493
0 commit comments