Skip to content

Commit 83bb5d5

Browse files
authored
deflake restart count assertions in in-place resize tests (kubernetes#131055)
1 parent 047e4c8 commit 83bb5d5

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

test/e2e/common/node/pod_resize.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,13 +1200,14 @@ func doPodResizeTests() {
12001200
patchedPod, pErr = f.ClientSet.CoreV1().Pods(newPod.Namespace).Patch(ctx, newPod.Name,
12011201
types.StrategicMergePatchType, []byte(patchString), metav1.PatchOptions{}, "resize")
12021202
framework.ExpectNoError(pErr, fmt.Sprintf("failed to patch pod for %s", opStr))
1203+
expected := e2epod.UpdateExpectedContainerRestarts(ctx, patchedPod, expectedContainers)
12031204

12041205
ginkgo.By(fmt.Sprintf("verifying pod patched for %s", opStr))
1205-
e2epod.VerifyPodResources(patchedPod, expectedContainers)
1206+
e2epod.VerifyPodResources(patchedPod, expected)
12061207

12071208
ginkgo.By(fmt.Sprintf("waiting for %s to be actuated", opStr))
1208-
resizedPod := e2epod.WaitForPodResizeActuation(ctx, f, podClient, newPod, expectedContainers)
1209-
e2epod.ExpectPodResized(ctx, f, resizedPod, expectedContainers)
1209+
resizedPod := e2epod.WaitForPodResizeActuation(ctx, f, podClient, newPod, expected)
1210+
e2epod.ExpectPodResized(ctx, f, resizedPod, expected)
12101211
}
12111212

12121213
patchAndVerify(tc.patchString, tc.expected, "resize")
@@ -1219,7 +1220,7 @@ func doPodResizeTests() {
12191220
gomega.Expect(c.Name).To(gomega.Equal(tc.expected[i].Name),
12201221
"test case containers & expectations should be in the same order")
12211222
// Resizes that trigger a restart should trigger a second restart when rolling back.
1222-
rollbackContainers[i].RestartCount = tc.expected[i].RestartCount * 2
1223+
rollbackContainers[i].RestartCount = tc.expected[i].RestartCount
12231224
}
12241225

12251226
rbPatchStr, err := e2epod.ResizeContainerPatch(tc.containers)

test/e2e/framework/pod/resize.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,24 @@ func ResizeContainerPatch(containers []ResizableContainerInfo) (string, error) {
505505
return string(patchBytes), nil
506506
}
507507

508+
// UpdateExpectedContainerRestarts updates the RestartCounts in expectedContainers by
509+
// adding them to the existing RestartCounts in the containerStatuses of the provided pod.
510+
// This reduces the flakiness of the RestartCount assertions by grabbing the current
511+
// restart count right before the resize operation, and verify the expected increment (0 or 1)
512+
// rather than the absolute count.
513+
func UpdateExpectedContainerRestarts(ctx context.Context, pod *v1.Pod, expectedContainers []ResizableContainerInfo) []ResizableContainerInfo {
514+
initialRestarts := make(map[string]int32)
515+
newExpectedContainers := []ResizableContainerInfo{}
516+
for _, ctr := range pod.Status.ContainerStatuses {
517+
initialRestarts[ctr.Name] = ctr.RestartCount
518+
}
519+
for i, ctr := range expectedContainers {
520+
newExpectedContainers = append(newExpectedContainers, expectedContainers[i])
521+
newExpectedContainers[i].RestartCount += initialRestarts[ctr.Name]
522+
}
523+
return newExpectedContainers
524+
}
525+
508526
func formatErrors(err error) error {
509527
// Put each error on a new line for readability.
510528
var agg utilerrors.Aggregate

test/e2e/node/pod_resize.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ func doPodResizeAdmissionPluginsTests() {
155155
patchedPod, pErr := f.ClientSet.CoreV1().Pods(newPods[0].Namespace).Patch(ctx, newPods[0].Name,
156156
types.StrategicMergePatchType, []byte(patchString), metav1.PatchOptions{}, "resize")
157157
framework.ExpectNoError(pErr, "failed to patch pod for resize")
158+
expected = e2epod.UpdateExpectedContainerRestarts(ctx, patchedPod, expected)
158159

159160
ginkgo.By("verifying pod patched for resize within resource quota")
160161
e2epod.VerifyPodResources(patchedPod, expected)
@@ -405,8 +406,9 @@ func doPodResizeSchedulerTests(f *framework.Framework) {
405406
ginkgo.By(fmt.Sprintf("TEST3: Verify pod '%s' is resized successfully after pod deletion '%s' and '%s", testPod1.Name, testPod2.Name, testPod3.Name))
406407
expected := []e2epod.ResizableContainerInfo{
407408
{
408-
Name: "c1",
409-
Resources: &e2epod.ContainerResources{CPUReq: testPod1CPUQuantity.String(), CPULim: testPod1CPUQuantity.String()},
409+
Name: "c1",
410+
Resources: &e2epod.ContainerResources{CPUReq: testPod1CPUQuantity.String(), CPULim: testPod1CPUQuantity.String()},
411+
RestartCount: testPod1.Status.ContainerStatuses[0].RestartCount,
410412
},
411413
}
412414
resizedPod := e2epod.WaitForPodResizeActuation(ctx, f, podClient, testPod1, expected)

0 commit comments

Comments
 (0)