@@ -3,7 +3,6 @@ package prebuilds_test
3
3
import (
4
4
"context"
5
5
"database/sql"
6
- "encoding/json"
7
6
"fmt"
8
7
"sync"
9
8
"testing"
@@ -653,6 +652,7 @@ func TestSkippingHardLimitedPresets(t *testing.T) {
653
652
t .Skip ("This test requires postgres" )
654
653
}
655
654
655
+ // Test cases verify the behavior of prebuild creation depending on configured failure limits
656
656
testCases := []struct {
657
657
name string
658
658
hardLimit int64
@@ -687,13 +687,16 @@ func TestSkippingHardLimitedPresets(t *testing.T) {
687
687
db ,pubSub := dbtestutil .NewDB (t )
688
688
controller := prebuilds .NewStoreReconciler (db ,pubSub ,cfg ,logger ,clock ,prometheus .NewRegistry ())
689
689
690
+ // Set up test environment with a template, version, and preset
690
691
ownerID := uuid .New ()
691
692
dbgen .User (t ,db , database.User {
692
693
ID :ownerID ,
693
694
})
694
695
org ,template := setupTestDBTemplate (t ,db ,ownerID ,templateDeleted )
695
696
templateVersionID := setupTestDBTemplateVersion (ctx ,t ,clock ,db ,pubSub ,org .ID ,ownerID ,template .ID )
696
697
preset := setupTestDBPreset (t ,db ,templateVersionID ,1 ,uuid .New ().String ())
698
+
699
+ // Create a failed prebuild workspace that counts toward the hard failure limit.
697
700
prebuiltWorkspace := setupTestDBPrebuild (
698
701
t ,
699
702
clock ,
@@ -708,36 +711,32 @@ func TestSkippingHardLimitedPresets(t *testing.T) {
708
711
)
709
712
_ = prebuiltWorkspace
710
713
711
- //Make sure we have only oneworkspace. Corresponding job has failedstatus.
714
+ //Verify initial state: one failedworkspace exists
712
715
workspaces ,err := db .GetWorkspacesByTemplateID (ctx ,template .ID )
713
716
require .NoError (t ,err )
714
717
workspaceCount := len (workspaces )
715
718
require .Equal (t ,1 ,workspaceCount )
716
719
717
- // Advance clock toavoid backoff.
720
+ // Advance clock tobypass backoff mechanisms
718
721
clock .Advance (time .Second ).MustWait (ctx )
719
722
720
- // ReconcileAll won't have any effect because we hit hard limit.
721
-
722
- // ReconcileAll tries to create 1 desired instance, because previous build is failed.
723
- // But it maybe hard-limited depending on test configuration.
723
+ // Trigger reconciliation to attempt creating a new prebuild
724
+ // The outcome depends on whether the hard limit has been reached
724
725
require .NoError (t ,controller .ReconcileAll (ctx ))
725
726
727
+ // Verify the final state after reconciliation
726
728
workspaces ,err = db .GetWorkspacesByTemplateID (ctx ,template .ID )
727
729
require .NoError (t ,err )
728
730
729
731
if tc .isHardLimitHit {
730
- //We didn't try to create 2nd workspace, because we hit hard limit before.
732
+ //When hard limit is reached, no new workspace should be created
731
733
workspaceCount = len (workspaces )
732
734
require .Equal (t ,1 ,workspaceCount )
733
735
}else {
734
- //We created 2nd workspace, hard limit wasn't hit.
736
+ //When hard limit is not reached, a new workspace should be created
735
737
workspaceCount = len (workspaces )
736
738
require .Equal (t ,2 ,workspaceCount )
737
739
}
738
-
739
- workspacesInJSON ,err := json .Marshal (workspaces )
740
- fmt .Printf ("workspacesInJSON: %s\n " ,workspacesInJSON )
741
740
})
742
741
}
743
742
}