mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-10 04:27:54 +00:00
refactor test into a table
Signed-off-by: Olga Shestopalova <oshestopalova1@gmail.com>
This commit is contained in:
parent
4e1c3e5237
commit
2207f12bea
@ -2388,46 +2388,57 @@ func Test_calculateEffectiveGracePeriod(t *testing.T) {
|
|||||||
two := int64(2)
|
two := int64(2)
|
||||||
five := int64(5)
|
five := int64(5)
|
||||||
thirty := int64(30)
|
thirty := int64(30)
|
||||||
// no overrides, use what's on the spec
|
testCases := []struct {
|
||||||
pod := newNamedPod("1", "ns", "running-pod", false)
|
desc string
|
||||||
pod.Spec.TerminationGracePeriodSeconds = &thirty
|
podSpecTerminationGracePeriodSeconds *int64
|
||||||
gracePeriod, _ := calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{})
|
podDeletionGracePeriodSeconds *int64
|
||||||
expectedGracePeriod := int64(30)
|
gracePeriodOverride *int64
|
||||||
if gracePeriod != expectedGracePeriod {
|
expectedGracePeriod int64
|
||||||
t.Errorf("Expected a grace period of %v, but was %v", expectedGracePeriod, gracePeriod)
|
}{
|
||||||
|
{
|
||||||
|
desc: "use termination grace period from the spec when no overrides",
|
||||||
|
podSpecTerminationGracePeriodSeconds: &thirty,
|
||||||
|
expectedGracePeriod: thirty,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "use pod DeletionGracePeriodSeconds when set",
|
||||||
|
podSpecTerminationGracePeriodSeconds: &thirty,
|
||||||
|
podDeletionGracePeriodSeconds: &five,
|
||||||
|
expectedGracePeriod: five,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "use grace period override when set",
|
||||||
|
podSpecTerminationGracePeriodSeconds: &thirty,
|
||||||
|
podDeletionGracePeriodSeconds: &five,
|
||||||
|
gracePeriodOverride: &two,
|
||||||
|
expectedGracePeriod: two,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "use 1 when pod DeletionGracePeriodSeconds is zero",
|
||||||
|
podSpecTerminationGracePeriodSeconds: &thirty,
|
||||||
|
podDeletionGracePeriodSeconds: &zero,
|
||||||
|
expectedGracePeriod: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
desc: "use 1 when grace period override is zero",
|
||||||
|
podSpecTerminationGracePeriodSeconds: &thirty,
|
||||||
|
podDeletionGracePeriodSeconds: &five,
|
||||||
|
gracePeriodOverride: &zero,
|
||||||
|
expectedGracePeriod: 1,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// pod DeletionGracePeriodSeconds is set
|
for _, tc := range testCases {
|
||||||
pod.DeletionGracePeriodSeconds = &five
|
t.Run(tc.desc, func(t *testing.T) {
|
||||||
gracePeriod, _ = calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{})
|
pod := newNamedPod("1", "ns", "running-pod", false)
|
||||||
expectedGracePeriod = five
|
pod.Spec.TerminationGracePeriodSeconds = tc.podSpecTerminationGracePeriodSeconds
|
||||||
if gracePeriod != expectedGracePeriod {
|
pod.DeletionGracePeriodSeconds = tc.podDeletionGracePeriodSeconds
|
||||||
t.Errorf("Expected a grace period of %v, but was %v", expectedGracePeriod, gracePeriod)
|
gracePeriod, _ := calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{
|
||||||
}
|
PodTerminationGracePeriodSecondsOverride: tc.gracePeriodOverride,
|
||||||
|
})
|
||||||
// grace period override
|
if gracePeriod != tc.expectedGracePeriod {
|
||||||
gracePeriod, _ = calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{
|
t.Errorf("Expected a grace period of %v, but was %v", tc.expectedGracePeriod, gracePeriod)
|
||||||
PodTerminationGracePeriodSecondsOverride: &two,
|
}
|
||||||
})
|
})
|
||||||
expectedGracePeriod = two
|
|
||||||
if gracePeriod != expectedGracePeriod {
|
|
||||||
t.Errorf("Expected a grace period of %v, but was %v", expectedGracePeriod, gracePeriod)
|
|
||||||
}
|
|
||||||
|
|
||||||
// pod DeletionGracePeriodSeconds is zero
|
|
||||||
pod.DeletionGracePeriodSeconds = &zero
|
|
||||||
gracePeriod, _ = calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{})
|
|
||||||
expectedGracePeriod = int64(1)
|
|
||||||
if gracePeriod != expectedGracePeriod {
|
|
||||||
t.Errorf("Expected a grace period of %v, but was %v", expectedGracePeriod, gracePeriod)
|
|
||||||
}
|
|
||||||
|
|
||||||
// grace period override is zero
|
|
||||||
gracePeriod, _ = calculateEffectiveGracePeriod(&podSyncStatus{}, pod, &KillPodOptions{
|
|
||||||
PodTerminationGracePeriodSecondsOverride: &zero,
|
|
||||||
})
|
|
||||||
expectedGracePeriod = int64(1)
|
|
||||||
if gracePeriod != expectedGracePeriod {
|
|
||||||
t.Errorf("Expected a grace period of %v, but was %v", expectedGracePeriod, gracePeriod)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user