@@ -3716,9 +3716,9 @@ func createPrebuiltWorkspace(
3716
3716
job := dbgen .ProvisionerJob (t , db , nil , database.ProvisionerJob {
3717
3717
Type : database .ProvisionerJobTypeWorkspaceBuild ,
3718
3718
OrganizationID : orgID ,
3719
-
3720
- CreatedAt : now . Add ( - 1 * time . Minute ) ,
3721
- Error : jobError ,
3719
+ CreatedAt : now . Add ( - 1 * time . Minute ),
3720
+ CompletedAt : sql. NullTime { Time : now , Valid : true } ,
3721
+ Error : jobError ,
3722
3722
})
3723
3723
3724
3724
// create ready agents
@@ -3888,6 +3888,95 @@ func TestWorkspacePrebuildsView(t *testing.T) {
3888
3888
}
3889
3889
}
3890
3890
3891
+ func TestGetRunningPrebuiltWorkspaces (t * testing.T ) {
3892
+ t .Parallel ()
3893
+ if ! dbtestutil .WillUsePostgres () {
3894
+ t .SkipNow ()
3895
+ }
3896
+
3897
+ now := dbtime .Now ()
3898
+ orgID := uuid .New ()
3899
+ userID := uuid .New ()
3900
+
3901
+ testCases := []struct {
3902
+ name string
3903
+ readyAgents int
3904
+ notReadyAgents int
3905
+ expectRows int
3906
+ expectReady bool
3907
+ }{
3908
+ {
3909
+ name : "one ready agent" ,
3910
+ readyAgents : 1 ,
3911
+ notReadyAgents : 0 ,
3912
+ expectRows : 1 ,
3913
+ expectReady : true ,
3914
+ },
3915
+ {
3916
+ name : "one not ready agent" ,
3917
+ readyAgents : 0 ,
3918
+ notReadyAgents : 1 ,
3919
+ expectRows : 1 ,
3920
+ expectReady : false ,
3921
+ },
3922
+ {
3923
+ name : "one ready, one not ready" ,
3924
+ readyAgents : 1 ,
3925
+ notReadyAgents : 1 ,
3926
+ expectRows : 1 ,
3927
+ expectReady : false ,
3928
+ },
3929
+ {
3930
+ name : "both ready" ,
3931
+ readyAgents : 2 ,
3932
+ notReadyAgents : 0 ,
3933
+ expectRows : 1 ,
3934
+ expectReady : true ,
3935
+ },
3936
+ {
3937
+ name : "five ready, one not ready" ,
3938
+ readyAgents : 5 ,
3939
+ notReadyAgents : 1 ,
3940
+ expectRows : 1 ,
3941
+ expectReady : false ,
3942
+ },
3943
+ }
3944
+
3945
+ for _ , tc := range testCases {
3946
+ t .Run (tc .name , func (t * testing.T ) {
3947
+ t .Parallel ()
3948
+
3949
+ sqlDB := testSQLDB (t )
3950
+ err := migrations .Up (sqlDB )
3951
+ require .NoError (t , err )
3952
+ db := database .New (sqlDB )
3953
+
3954
+ ctx := testutil .Context (t , testutil .WaitShort )
3955
+
3956
+ dbgen .Organization (t , db , database.Organization {
3957
+ ID : orgID ,
3958
+ })
3959
+ dbgen .User (t , db , database.User {
3960
+ ID : userID ,
3961
+ })
3962
+
3963
+ tmpl := createTemplate (t , db , orgID , userID )
3964
+ tmplV1 := createTmplVersionAndPreset (t , db , tmpl , tmpl .ActiveVersionID , now , nil )
3965
+ createPrebuiltWorkspace (ctx , t , db , tmpl , tmplV1 , orgID , now , & createPrebuiltWorkspaceOpts {
3966
+ readyAgents : tc .readyAgents ,
3967
+ notReadyAgents : tc .notReadyAgents ,
3968
+ })
3969
+
3970
+ workspacePrebuilds , err := db .GetRunningPrebuiltWorkspaces (ctx )
3971
+ require .NoError (t , err )
3972
+ require .Len (t , workspacePrebuilds , tc .expectRows )
3973
+ if tc .expectRows > 0 {
3974
+ require .Equal (t , tc .expectReady , workspacePrebuilds [0 ].Ready )
3975
+ }
3976
+ })
3977
+ }
3978
+ }
3979
+
3891
3980
func TestGetPresetsBackoff (t * testing.T ) {
3892
3981
t .Parallel ()
3893
3982
if ! dbtestutil .WillUsePostgres () {
0 commit comments