@@ -37,6 +37,17 @@ const (
3737 CELKeyConfigMapBackup
3838)
3939
40+ type faultQuarantineConfig struct {
41+ LabelPrefix string `toml:"label-prefix"`
42+ CircuitBreaker circuitBreakerConfig `toml:"circuitBreaker"`
43+ RuleSets []map [string ]any `toml:"rule-sets"`
44+ }
45+
46+ type circuitBreakerConfig struct {
47+ Percentage int `toml:"percentage"`
48+ Duration string `toml:"duration"`
49+ }
50+
4051type QuarantineTestContext struct {
4152 NodeName string
4253 ConfigMapBackup []byte
@@ -94,16 +105,11 @@ func SetupQuarantineTest(ctx context.Context, t *testing.T, c *envconf.Config, c
94105
95106// QuarantineSetupOptions provides options for setting up quarantine tests.
96107type QuarantineSetupOptions struct {
97- // CircuitBreakerPercentage sets the CB percentage threshold (0 to skip)
98108 CircuitBreakerPercentage int
99- // CircuitBreakerDuration sets the CB duration (empty to skip)
100- CircuitBreakerDuration string
101- // CircuitBreakerState sets the initial CB state (empty to skip)
102- CircuitBreakerState string
103- // DryRun sets dry-run mode (nil to skip, otherwise pointer to bool value)
104- DryRun * bool
105- // SkipRestart skips the deployment restart (useful when chaining operations)
106- SkipRestart bool
109+ CircuitBreakerDuration string
110+ CircuitBreakerState string
111+ DryRun * bool
112+ SkipRestart bool
107113}
108114
109115// SetupQuarantineTestWithOptions sets up a quarantine test with additional configuration options.
@@ -118,36 +124,31 @@ func SetupQuarantineTestWithOptions(ctx context.Context, t *testing.T, c *envcon
118124 testCtx := & QuarantineTestContext {}
119125 var originalDeployment * appsv1.Deployment
120126
121- t .Log ("Backing up current fault-quarantine configmap" )
122127 backupData , err := BackupConfigMap (ctx , client , "fault-quarantine" , NVSentinelNamespace )
123128 require .NoError (t , err )
124- t .Log ("Backup created in memory" )
125129 testCtx .ConfigMapBackup = backupData
126130
127131 t .Logf ("Applying test configmap: %s" , configMapPath )
128132 err = createConfigMapFromFilePath (ctx , client , configMapPath , "fault-quarantine" , NVSentinelNamespace )
129133 require .NoError (t , err )
130134
131- argUpdates := make (map [string ]string )
132135 if opts != nil {
133136 if opts .CircuitBreakerPercentage > 0 {
134- t .Logf ("Will set circuit breaker threshold : %d%%, duration: %s" ,
137+ t .Logf ("Updating circuit breaker in ConfigMap : %d%%, duration: %s" ,
135138 opts .CircuitBreakerPercentage , opts .CircuitBreakerDuration )
136- argUpdates [ "--circuit-breaker-percentage=" ] = fmt . Sprintf ( "--circuit-breaker-percentage=%d" , opts .CircuitBreakerPercentage )
137- argUpdates [ "--circuit-breaker-duration=" ] = fmt . Sprintf ( "--circuit-breaker-duration=%s" , opts . CircuitBreakerDuration )
139+ err = updateCircuitBreakerConfigInConfigMap ( ctx , t , client , opts .CircuitBreakerPercentage , opts . CircuitBreakerDuration )
140+ require . NoError ( t , err )
138141 }
139142 if opts .DryRun != nil {
140143 t .Logf ("Will set dry-run mode to: %v" , * opts .DryRun )
141- argUpdates ["--dry-run=" ] = fmt .Sprintf ("--dry-run=%v" , * opts .DryRun )
144+ argUpdates := map [string ]string {
145+ "--dry-run=" : fmt .Sprintf ("--dry-run=%v" , * opts .DryRun ),
146+ }
147+ originalDeployment = modifyFaultQuarantineDeploymentArgs (ctx , t , client , argUpdates )
148+ }
149+ if opts .CircuitBreakerState != "" {
150+ updateCircuitBreakerStateConfigMap (ctx , t , client , opts .CircuitBreakerState )
142151 }
143- }
144-
145- if len (argUpdates ) > 0 {
146- originalDeployment = modifyFaultQuarantineDeploymentArgs (ctx , t , client , argUpdates )
147- }
148-
149- if opts != nil && opts .CircuitBreakerState != "" {
150- updateCircuitBreakerStateConfigMap (ctx , t , client , opts .CircuitBreakerState )
151152 }
152153
153154 if opts == nil || ! opts .SkipRestart {
@@ -479,3 +480,22 @@ func updateCircuitBreakerStateConfigMap(ctx context.Context, t *testing.T, clien
479480 err := client .Resources ().Create (ctx , cm )
480481 require .NoError (t , err , "failed to create CB state configmap" )
481482}
483+
484+ // updateCircuitBreakerConfigInConfigMap updates the circuit breaker percentage and duration in the fault-quarantine ConfigMap.
485+ func updateCircuitBreakerConfigInConfigMap (ctx context.Context , t * testing.T , client klient.Client , percentage int , duration string ) error {
486+ t .Helper ()
487+
488+ err := UpdateConfigMapTOMLField (ctx , client , "fault-quarantine" , NVSentinelNamespace , "config.toml" ,
489+ func (cfg * faultQuarantineConfig ) error {
490+ cfg .CircuitBreaker .Percentage = percentage
491+ cfg .CircuitBreaker .Duration = duration
492+ return nil
493+ })
494+
495+ if err != nil {
496+ return fmt .Errorf ("failed to update circuit breaker config: %w" , err )
497+ }
498+
499+ t .Logf ("Updated circuit breaker config in ConfigMap: percentage=%d, duration=%s" , percentage , duration )
500+ return nil
501+ }
0 commit comments