@@ -653,58 +653,89 @@ func TestSkippingHardLimitedPresets(t *testing.T) {
653
653
t .Skip ("This test requires postgres" )
654
654
}
655
655
656
- templateDeleted := false
657
-
658
- clock := quartz .NewMock (t )
659
- ctx := testutil .Context (t ,testutil .WaitShort )
660
- cfg := codersdk.PrebuildsConfig {}
661
- logger := slogtest .Make (
662
- t ,& slogtest.Options {IgnoreErrors :true },
663
- ).Leveled (slog .LevelDebug )
664
- db ,pubSub := dbtestutil .NewDB (t )
665
- controller := prebuilds .NewStoreReconciler (db ,pubSub ,cfg ,logger ,clock ,prometheus .NewRegistry ())
666
-
667
- ownerID := uuid .New ()
668
- dbgen .User (t ,db , database.User {
669
- ID :ownerID ,
670
- })
671
- org ,template := setupTestDBTemplate (t ,db ,ownerID ,templateDeleted )
672
- templateVersionID := setupTestDBTemplateVersion (ctx ,t ,clock ,db ,pubSub ,org .ID ,ownerID ,template .ID )
673
- preset := setupTestDBPreset (t ,db ,templateVersionID ,1 ,uuid .New ().String ())
674
- prebuiltWorkspace := setupTestDBPrebuild (
675
- t ,
676
- clock ,
677
- db ,
678
- pubSub ,
679
- database .WorkspaceTransitionStart ,
680
- database .ProvisionerJobStatusFailed ,
681
- org .ID ,
682
- preset ,
683
- template .ID ,
684
- templateVersionID ,
685
- )
686
- _ = prebuiltWorkspace
687
-
688
- // Make sure we have only one workspace. Corresponding job has failed status.
689
- workspaces ,err := db .GetWorkspacesByTemplateID (ctx ,template .ID )
690
- require .NoError (t ,err )
691
- workspaceCount := len (workspaces )
692
- require .Equal (t ,1 ,workspaceCount )
656
+ testCases := []struct {
657
+ name string
658
+ hardLimit int64
659
+ isHardLimitHit bool
660
+ }{
661
+ {
662
+ name :"hard limit is hit - skip creation of prebuilt workspace" ,
663
+ hardLimit :1 ,
664
+ isHardLimitHit :true ,
665
+ },
666
+ {
667
+ name :"hard limit is not hit - try to create prebuilt workspace again" ,
668
+ hardLimit :2 ,
669
+ isHardLimitHit :false ,
670
+ },
671
+ }
693
672
694
- // Advance clock to avoid backoff.
695
- clock .Advance (time .Second ).MustWait (ctx )
673
+ for _ ,tc := range testCases {
674
+ t .Run (tc .name ,func (t * testing.T ) {
675
+ t .Parallel ()
696
676
697
- // ReconcileAll won't have any effect because we hit hard limit.
698
- require .NoError (t ,controller .ReconcileAll (ctx ))
677
+ templateDeleted := false
699
678
700
- // Make we didn't try to create 2nd workspace, because we hit hard limit before.
701
- workspaces ,err = db .GetWorkspacesByTemplateID (ctx ,template .ID )
702
- require .NoError (t ,err )
703
- workspaceCount = len (workspaces )
704
- require .Equal (t ,1 ,workspaceCount )
679
+ clock := quartz .NewMock (t )
680
+ ctx := testutil .Context (t ,testutil .WaitShort )
681
+ cfg := codersdk.PrebuildsConfig {
682
+ FailureHardLimit :tc .hardLimit ,
683
+ }
684
+ logger := slogtest .Make (
685
+ t ,& slogtest.Options {IgnoreErrors :true },
686
+ ).Leveled (slog .LevelDebug )
687
+ db ,pubSub := dbtestutil .NewDB (t )
688
+ controller := prebuilds .NewStoreReconciler (db ,pubSub ,cfg ,logger ,clock ,prometheus .NewRegistry ())
689
+
690
+ ownerID := uuid .New ()
691
+ dbgen .User (t ,db , database.User {
692
+ ID :ownerID ,
693
+ })
694
+ org ,template := setupTestDBTemplate (t ,db ,ownerID ,templateDeleted )
695
+ templateVersionID := setupTestDBTemplateVersion (ctx ,t ,clock ,db ,pubSub ,org .ID ,ownerID ,template .ID )
696
+ preset := setupTestDBPreset (t ,db ,templateVersionID ,1 ,uuid .New ().String ())
697
+ prebuiltWorkspace := setupTestDBPrebuild (
698
+ t ,
699
+ clock ,
700
+ db ,
701
+ pubSub ,
702
+ database .WorkspaceTransitionStart ,
703
+ database .ProvisionerJobStatusFailed ,
704
+ org .ID ,
705
+ preset ,
706
+ template .ID ,
707
+ templateVersionID ,
708
+ )
709
+ _ = prebuiltWorkspace
710
+
711
+ // Make sure we have only one workspace. Corresponding job has failed status.
712
+ workspaces ,err := db .GetWorkspacesByTemplateID (ctx ,template .ID )
713
+ require .NoError (t ,err )
714
+ workspaceCount := len (workspaces )
715
+ require .Equal (t ,1 ,workspaceCount )
716
+
717
+ // Advance clock to avoid backoff.
718
+ clock .Advance (time .Second ).MustWait (ctx )
719
+
720
+ // ReconcileAll won't have any effect because we hit hard limit.
721
+ require .NoError (t ,controller .ReconcileAll (ctx ))
722
+
723
+ // Make we didn't try to create 2nd workspace, because we hit hard limit before.
724
+ workspaces ,err = db .GetWorkspacesByTemplateID (ctx ,template .ID )
725
+ require .NoError (t ,err )
726
+
727
+ if tc .isHardLimitHit {
728
+ workspaceCount = len (workspaces )
729
+ require .Equal (t ,1 ,workspaceCount )
730
+ }else {
731
+ workspaceCount = len (workspaces )
732
+ require .Equal (t ,2 ,workspaceCount )
733
+ }
705
734
706
- workspacesInJSON ,err := json .Marshal (workspaces )
707
- fmt .Printf ("workspacesInJSON: %s\n " ,workspacesInJSON )
735
+ workspacesInJSON ,err := json .Marshal (workspaces )
736
+ fmt .Printf ("workspacesInJSON: %s\n " ,workspacesInJSON )
737
+ })
738
+ }
708
739
}
709
740
710
741
func TestRunLoop (t * testing.T ) {