@@ -9,14 +9,20 @@ import (
9
9
"time"
10
10
11
11
"github.com/google/uuid"
12
+ "github.com/prometheus/client_golang/prometheus"
12
13
"github.com/stretchr/testify/assert"
13
14
"github.com/stretchr/testify/require"
14
15
"go.uber.org/goleak"
15
16
17
+ "cdr.dev/slog"
16
18
"cdr.dev/slog/sloggers/slogtest"
19
+ "github.com/coder/coder/v2/coderd/coderdtest"
17
20
"github.com/coder/coder/v2/coderd/database"
21
+ "github.com/coder/coder/v2/coderd/database/dbauthz"
18
22
"github.com/coder/coder/v2/coderd/database/dbgen"
19
23
"github.com/coder/coder/v2/coderd/database/dbtestutil"
24
+ "github.com/coder/coder/v2/coderd/provisionerdserver"
25
+ "github.com/coder/coder/v2/coderd/rbac"
20
26
"github.com/coder/coder/v2/coderd/unhanger"
21
27
"github.com/coder/coder/v2/provisionersdk"
22
28
"github.com/coder/coder/v2/testutil"
@@ -37,7 +43,7 @@ func TestDetectorNoJobs(t *testing.T) {
37
43
statsCh = make (chan unhanger.Stats )
38
44
)
39
45
40
- detector := unhanger .New (ctx , db , pubsub , log , tickCh ).WithStatsChannel (statsCh )
46
+ detector := unhanger .New (ctx , wrapDBAuthz ( db , log ) , pubsub , log , tickCh ).WithStatsChannel (statsCh )
41
47
detector .Start ()
42
48
tickCh <- time .Now ()
43
49
@@ -84,7 +90,7 @@ func TestDetectorNoHungJobs(t *testing.T) {
84
90
})
85
91
}
86
92
87
- detector := unhanger .New (ctx , db , pubsub , log , tickCh ).WithStatsChannel (statsCh )
93
+ detector := unhanger .New (ctx , wrapDBAuthz ( db , log ) , pubsub , log , tickCh ).WithStatsChannel (statsCh )
88
94
detector .Start ()
89
95
tickCh <- now
90
96
@@ -190,7 +196,7 @@ func TestDetectorHungWorkspaceBuild(t *testing.T) {
190
196
t .Log ("previous job ID: " , previousWorkspaceBuildJob .ID )
191
197
t .Log ("current job ID: " , currentWorkspaceBuildJob .ID )
192
198
193
- detector := unhanger .New (ctx , db , pubsub , log , tickCh ).WithStatsChannel (statsCh )
199
+ detector := unhanger .New (ctx , wrapDBAuthz ( db , log ) , pubsub , log , tickCh ).WithStatsChannel (statsCh )
194
200
detector .Start ()
195
201
tickCh <- now
196
202
@@ -313,7 +319,7 @@ func TestDetectorHungWorkspaceBuildNoOverrideState(t *testing.T) {
313
319
t .Log ("previous job ID: " , previousWorkspaceBuildJob .ID )
314
320
t .Log ("current job ID: " , currentWorkspaceBuildJob .ID )
315
321
316
- detector := unhanger .New (ctx , db , pubsub , log , tickCh ).WithStatsChannel (statsCh )
322
+ detector := unhanger .New (ctx , wrapDBAuthz ( db , log ) , pubsub , log , tickCh ).WithStatsChannel (statsCh )
317
323
detector .Start ()
318
324
tickCh <- now
319
325
@@ -406,7 +412,7 @@ func TestDetectorHungWorkspaceBuildNoOverrideStateIfNoExistingBuild(t *testing.T
406
412
407
413
t .Log ("current job ID: " , currentWorkspaceBuildJob .ID )
408
414
409
- detector := unhanger .New (ctx , db , pubsub , log , tickCh ).WithStatsChannel (statsCh )
415
+ detector := unhanger .New (ctx , wrapDBAuthz ( db , log ) , pubsub , log , tickCh ).WithStatsChannel (statsCh )
410
416
detector .Start ()
411
417
tickCh <- now
412
418
@@ -469,29 +475,42 @@ func TestDetectorHungOtherJobTypes(t *testing.T) {
469
475
Type : database .ProvisionerJobTypeTemplateVersionImport ,
470
476
Input : []byte ("{}" ),
471
477
})
472
-
473
- // Template dry-run job.
474
- templateDryRunJob = dbgen .ProvisionerJob (t , db , pubsub , database.ProvisionerJob {
475
- CreatedAt : tenMinAgo ,
476
- UpdatedAt : sixMinAgo ,
477
- StartedAt : sql.NullTime {
478
- Time : tenMinAgo ,
479
- Valid : true ,
480
- },
478
+ _ = dbgen .TemplateVersion (t , db , database.TemplateVersion {
481
479
OrganizationID : org .ID ,
482
- InitiatorID : user .ID ,
483
- Provisioner : database .ProvisionerTypeEcho ,
484
- StorageMethod : database .ProvisionerStorageMethodFile ,
485
- FileID : file .ID ,
486
- Type : database .ProvisionerJobTypeTemplateVersionDryRun ,
487
- Input : []byte ("{}" ),
480
+ JobID : templateImportJob .ID ,
481
+ CreatedBy : user .ID ,
488
482
})
489
483
)
490
484
485
+ // Template dry-run job.
486
+ dryRunVersion := dbgen .TemplateVersion (t , db , database.TemplateVersion {
487
+ OrganizationID : org .ID ,
488
+ CreatedBy : user .ID ,
489
+ })
490
+ input , err := json .Marshal (provisionerdserver.TemplateVersionDryRunJob {
491
+ TemplateVersionID : dryRunVersion .ID ,
492
+ })
493
+ require .NoError (t , err )
494
+ templateDryRunJob := dbgen .ProvisionerJob (t , db , pubsub , database.ProvisionerJob {
495
+ CreatedAt : tenMinAgo ,
496
+ UpdatedAt : sixMinAgo ,
497
+ StartedAt : sql.NullTime {
498
+ Time : tenMinAgo ,
499
+ Valid : true ,
500
+ },
501
+ OrganizationID : org .ID ,
502
+ InitiatorID : user .ID ,
503
+ Provisioner : database .ProvisionerTypeEcho ,
504
+ StorageMethod : database .ProvisionerStorageMethodFile ,
505
+ FileID : file .ID ,
506
+ Type : database .ProvisionerJobTypeTemplateVersionDryRun ,
507
+ Input : input ,
508
+ })
509
+
491
510
t .Log ("template import job ID: " , templateImportJob .ID )
492
511
t .Log ("template dry-run job ID: " , templateDryRunJob .ID )
493
512
494
- detector := unhanger .New (ctx , db , pubsub , log , tickCh ).WithStatsChannel (statsCh )
513
+ detector := unhanger .New (ctx , wrapDBAuthz ( db , log ) , pubsub , log , tickCh ).WithStatsChannel (statsCh )
495
514
detector .Start ()
496
515
tickCh <- now
497
516
@@ -564,11 +583,16 @@ func TestDetectorHungCanceledJob(t *testing.T) {
564
583
Type : database .ProvisionerJobTypeTemplateVersionImport ,
565
584
Input : []byte ("{}" ),
566
585
})
586
+ _ = dbgen .TemplateVersion (t , db , database.TemplateVersion {
587
+ OrganizationID : org .ID ,
588
+ JobID : templateImportJob .ID ,
589
+ CreatedBy : user .ID ,
590
+ })
567
591
)
568
592
569
593
t .Log ("template import job ID: " , templateImportJob .ID )
570
594
571
- detector := unhanger .New (ctx , db , pubsub , log , tickCh ).WithStatsChannel (statsCh )
595
+ detector := unhanger .New (ctx , wrapDBAuthz ( db , log ) , pubsub , log , tickCh ).WithStatsChannel (statsCh )
572
596
detector .Start ()
573
597
tickCh <- now
574
598
@@ -657,6 +681,11 @@ func TestDetectorPushesLogs(t *testing.T) {
657
681
Type : database .ProvisionerJobTypeTemplateVersionImport ,
658
682
Input : []byte ("{}" ),
659
683
})
684
+ _ = dbgen .TemplateVersion (t , db , database.TemplateVersion {
685
+ OrganizationID : org .ID ,
686
+ JobID : templateImportJob .ID ,
687
+ CreatedBy : user .ID ,
688
+ })
660
689
)
661
690
662
691
t .Log ("template import job ID: " , templateImportJob .ID )
@@ -678,7 +707,7 @@ func TestDetectorPushesLogs(t *testing.T) {
678
707
require .Len (t , logs , 10 )
679
708
}
680
709
681
- detector := unhanger .New (ctx , db , pubsub , log , tickCh ).WithStatsChannel (statsCh )
710
+ detector := unhanger .New (ctx , wrapDBAuthz ( db , log ) , pubsub , log , tickCh ).WithStatsChannel (statsCh )
682
711
detector .Start ()
683
712
684
713
// Create pubsub subscription to listen for new log events.
@@ -752,7 +781,7 @@ func TestDetectorMaxJobsPerRun(t *testing.T) {
752
781
// Create unhanger.MaxJobsPerRun + 1 hung jobs.
753
782
now := time .Now ()
754
783
for i := 0 ; i < unhanger .MaxJobsPerRun + 1 ; i ++ {
755
- dbgen .ProvisionerJob (t , db , pubsub , database.ProvisionerJob {
784
+ pj := dbgen .ProvisionerJob (t , db , pubsub , database.ProvisionerJob {
756
785
CreatedAt : now .Add (- time .Hour ),
757
786
UpdatedAt : now .Add (- time .Hour ),
758
787
StartedAt : sql.NullTime {
@@ -767,9 +796,14 @@ func TestDetectorMaxJobsPerRun(t *testing.T) {
767
796
Type : database .ProvisionerJobTypeTemplateVersionImport ,
768
797
Input : []byte ("{}" ),
769
798
})
799
+ _ = dbgen .TemplateVersion (t , db , database.TemplateVersion {
800
+ OrganizationID : org .ID ,
801
+ JobID : pj .ID ,
802
+ CreatedBy : user .ID ,
803
+ })
770
804
}
771
805
772
- detector := unhanger .New (ctx , db , pubsub , log , tickCh ).WithStatsChannel (statsCh )
806
+ detector := unhanger .New (ctx , wrapDBAuthz ( db , log ) , pubsub , log , tickCh ).WithStatsChannel (statsCh )
773
807
detector .Start ()
774
808
tickCh <- now
775
809
@@ -788,3 +822,14 @@ func TestDetectorMaxJobsPerRun(t *testing.T) {
788
822
detector .Close ()
789
823
detector .Wait ()
790
824
}
825
+
826
+ // wrapDBAuthz adds our Authorization/RBAC around the given database store, to
827
+ // ensure the unhanger has the right permissions to do its work.
828
+ func wrapDBAuthz (db database.Store , logger slog.Logger ) database.Store {
829
+ return dbauthz .New (
830
+ db ,
831
+ rbac .NewStrictCachingAuthorizer (prometheus .NewRegistry ()),
832
+ logger ,
833
+ coderdtest .AccessControlStorePointer (),
834
+ )
835
+ }
0 commit comments