@@ -3,6 +3,7 @@ package prebuilds_test
3
3
import (
4
4
"context"
5
5
"database/sql"
6
+ "encoding/json"
6
7
"fmt"
7
8
"sync"
8
9
"testing"
@@ -645,6 +646,67 @@ func TestDeletionOfPrebuiltWorkspaceWithInvalidPreset(t *testing.T) {
645
646
require .Equal (t ,database .WorkspaceTransitionDelete ,builds [0 ].Transition )
646
647
}
647
648
649
+ func TestSkippingHardLimitedPresets (t * testing.T ) {
650
+ t .Parallel ()
651
+
652
+ if ! dbtestutil .WillUsePostgres () {
653
+ t .Skip ("This test requires postgres" )
654
+ }
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 )
693
+
694
+ // Advance clock to avoid backoff.
695
+ clock .Advance (time .Second ).MustWait (ctx )
696
+
697
+ // ReconcileAll won't have any effect because we hit hard limit.
698
+ require .NoError (t ,controller .ReconcileAll (ctx ))
699
+
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 )
705
+
706
+ workspacesInJSON ,err := json .Marshal (workspaces )
707
+ fmt .Printf ("workspacesInJSON: %s\n " ,workspacesInJSON )
708
+ }
709
+
648
710
func TestRunLoop (t * testing.T ) {
649
711
t .Parallel ()
650
712