@@ -473,7 +473,7 @@ func TestPostWorkspacesByOrganization(t *testing.T) {
473
473
require .Equal (t , http .StatusConflict , apiErr .StatusCode ())
474
474
})
475
475
476
- t .Run ("Create " , func (t * testing.T ) {
476
+ t .Run ("CreateWithAuditLogs " , func (t * testing.T ) {
477
477
t .Parallel ()
478
478
auditor := audit .NewMock ()
479
479
client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true , Auditor : auditor })
@@ -483,16 +483,10 @@ func TestPostWorkspacesByOrganization(t *testing.T) {
483
483
coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
484
484
workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
485
485
coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
486
-
487
- require .Eventually (t , func () bool {
488
- if len (auditor .AuditLogs ()) < 6 {
489
- return false
490
- }
491
- return auditor .AuditLogs ()[4 ].Action == database .AuditActionCreate
492
- }, testutil .WaitMedium , testutil .IntervalFast )
486
+ verifyAuditWorkspaceCreated (t , auditor , workspace .Name )
493
487
})
494
488
495
- t .Run ("CreateFromVersion " , func (t * testing.T ) {
489
+ t .Run ("CreateFromVersionWithAuditLogs " , func (t * testing.T ) {
496
490
t .Parallel ()
497
491
auditor := audit .NewMock ()
498
492
client := coderdtest .New (t , & coderdtest.Options {IncludeProvisionerDaemon : true , Auditor : auditor })
@@ -513,12 +507,7 @@ func TestPostWorkspacesByOrganization(t *testing.T) {
513
507
514
508
require .Equal (t , testWorkspaceBuild .TemplateVersionID , versionTest .ID )
515
509
require .Equal (t , defaultWorkspaceBuild .TemplateVersionID , versionDefault .ID )
516
- require .Eventually (t , func () bool {
517
- if len (auditor .AuditLogs ()) < 6 {
518
- return false
519
- }
520
- return auditor .AuditLogs ()[4 ].Action == database .AuditActionCreate
521
- }, testutil .WaitMedium , testutil .IntervalFast )
510
+ verifyAuditWorkspaceCreated (t , auditor , defaultWorkspace .Name )
522
511
})
523
512
524
513
t .Run ("InvalidCombinationOfTemplateAndTemplateVersion" , func (t * testing.T ) {
@@ -2919,3 +2908,25 @@ func TestWorkspaceDormant(t *testing.T) {
2919
2908
coderdtest .MustTransitionWorkspace (t , client , workspace .ID , database .WorkspaceTransitionStop , database .WorkspaceTransitionStart )
2920
2909
})
2921
2910
}
2911
+
2912
+ func verifyAuditWorkspaceCreated (t * testing.T , auditor * audit.MockAuditor , workspaceName string ) {
2913
+ var auditLogs []database.AuditLog
2914
+ ok := assert .Eventually (t , func () bool {
2915
+ auditLogs = auditor .AuditLogs ()
2916
+
2917
+ for _ , auditLog := range auditLogs {
2918
+ if auditLog .Action == database .AuditActionCreate &&
2919
+ auditLog .ResourceType == database .ResourceTypeWorkspace &&
2920
+ auditLog .ResourceTarget == workspaceName {
2921
+ return true
2922
+ }
2923
+ }
2924
+ return false
2925
+ }, testutil .WaitMedium , testutil .IntervalFast )
2926
+
2927
+ if ! ok {
2928
+ for i , auditLog := range auditLogs {
2929
+ t .Logf ("%d. Audit: ID=%s action=%s resourceID=%s resourceType=%s resourceTarget=%s" , i + 1 , auditLog .ID , auditLog .Action , auditLog .ResourceID , auditLog .ResourceType , auditLog .ResourceTarget )
2930
+ }
2931
+ }
2932
+ }
0 commit comments