@@ -2388,46 +2388,57 @@ func Test_calculateEffectiveGracePeriod(t *testing.T) {
2388
2388
two := int64 (2 )
2389
2389
five := int64 (5 )
2390
2390
thirty := int64 (30 )
2391
- // no overrides, use what's on the spec
2392
- pod := newNamedPod ("1" , "ns" , "running-pod" , false )
2393
- pod .Spec .TerminationGracePeriodSeconds = & thirty
2394
- gracePeriod , _ := calculateEffectiveGracePeriod (& podSyncStatus {}, pod , & KillPodOptions {})
2395
- expectedGracePeriod := int64 (30 )
2396
- if gracePeriod != expectedGracePeriod {
2397
- t .Errorf ("Expected a grace period of %v, but was %v" , expectedGracePeriod , gracePeriod )
2398
- }
2399
-
2400
- // pod DeletionGracePeriodSeconds is set
2401
- pod .DeletionGracePeriodSeconds = & five
2402
- gracePeriod , _ = calculateEffectiveGracePeriod (& podSyncStatus {}, pod , & KillPodOptions {})
2403
- expectedGracePeriod = five
2404
- if gracePeriod != expectedGracePeriod {
2405
- t .Errorf ("Expected a grace period of %v, but was %v" , expectedGracePeriod , gracePeriod )
2406
- }
2407
-
2408
- // grace period override
2409
- gracePeriod , _ = calculateEffectiveGracePeriod (& podSyncStatus {}, pod , & KillPodOptions {
2410
- PodTerminationGracePeriodSecondsOverride : & two ,
2411
- })
2412
- expectedGracePeriod = two
2413
- if gracePeriod != expectedGracePeriod {
2414
- t .Errorf ("Expected a grace period of %v, but was %v" , expectedGracePeriod , gracePeriod )
2415
- }
2416
-
2417
- // pod DeletionGracePeriodSeconds is zero
2418
- pod .DeletionGracePeriodSeconds = & zero
2419
- gracePeriod , _ = calculateEffectiveGracePeriod (& podSyncStatus {}, pod , & KillPodOptions {})
2420
- expectedGracePeriod = int64 (1 )
2421
- if gracePeriod != expectedGracePeriod {
2422
- t .Errorf ("Expected a grace period of %v, but was %v" , expectedGracePeriod , gracePeriod )
2391
+ testCases := []struct {
2392
+ desc string
2393
+ podSpecTerminationGracePeriodSeconds * int64
2394
+ podDeletionGracePeriodSeconds * int64
2395
+ gracePeriodOverride * int64
2396
+ expectedGracePeriod int64
2397
+ }{
2398
+ {
2399
+ desc : "use termination grace period from the spec when no overrides" ,
2400
+ podSpecTerminationGracePeriodSeconds : & thirty ,
2401
+ expectedGracePeriod : thirty ,
2402
+ },
2403
+ {
2404
+ desc : "use pod DeletionGracePeriodSeconds when set" ,
2405
+ podSpecTerminationGracePeriodSeconds : & thirty ,
2406
+ podDeletionGracePeriodSeconds : & five ,
2407
+ expectedGracePeriod : five ,
2408
+ },
2409
+ {
2410
+ desc : "use grace period override when set" ,
2411
+ podSpecTerminationGracePeriodSeconds : & thirty ,
2412
+ podDeletionGracePeriodSeconds : & five ,
2413
+ gracePeriodOverride : & two ,
2414
+ expectedGracePeriod : two ,
2415
+ },
2416
+ {
2417
+ desc : "use 1 when pod DeletionGracePeriodSeconds is zero" ,
2418
+ podSpecTerminationGracePeriodSeconds : & thirty ,
2419
+ podDeletionGracePeriodSeconds : & zero ,
2420
+ expectedGracePeriod : 1 ,
2421
+ },
2422
+ {
2423
+ desc : "use 1 when grace period override is zero" ,
2424
+ podSpecTerminationGracePeriodSeconds : & thirty ,
2425
+ podDeletionGracePeriodSeconds : & five ,
2426
+ gracePeriodOverride : & zero ,
2427
+ expectedGracePeriod : 1 ,
2428
+ },
2423
2429
}
2424
2430
2425
- // grace period override is zero
2426
- gracePeriod , _ = calculateEffectiveGracePeriod (& podSyncStatus {}, pod , & KillPodOptions {
2427
- PodTerminationGracePeriodSecondsOverride : & zero ,
2428
- })
2429
- expectedGracePeriod = int64 (1 )
2430
- if gracePeriod != expectedGracePeriod {
2431
- t .Errorf ("Expected a grace period of %v, but was %v" , expectedGracePeriod , gracePeriod )
2431
+ for _ , tc := range testCases {
2432
+ t .Run (tc .desc , func (t * testing.T ) {
2433
+ pod := newNamedPod ("1" , "ns" , "running-pod" , false )
2434
+ pod .Spec .TerminationGracePeriodSeconds = tc .podSpecTerminationGracePeriodSeconds
2435
+ pod .DeletionGracePeriodSeconds = tc .podDeletionGracePeriodSeconds
2436
+ gracePeriod , _ := calculateEffectiveGracePeriod (& podSyncStatus {}, pod , & KillPodOptions {
2437
+ PodTerminationGracePeriodSecondsOverride : tc .gracePeriodOverride ,
2438
+ })
2439
+ if gracePeriod != tc .expectedGracePeriod {
2440
+ t .Errorf ("Expected a grace period of %v, but was %v" , tc .expectedGracePeriod , gracePeriod )
2441
+ }
2442
+ })
2432
2443
}
2433
2444
}
0 commit comments