e2e: simplify test cleanup

ginkgo.DeferCleanup has multiple advantages:
- The cleanup operation can get registered if and only if needed.
- No need to return a cleanup function that the caller must invoke.
- Automatically determines whether a context is needed, which will
  simplify the introduction of context parameters.
- Ginkgo's timeline shows when it executes the cleanup operation.
This commit is contained in:
Patrick Ohly
2022-12-11 18:51:37 +01:00
parent 5c09ca57ff
commit d4729008ef
101 changed files with 716 additions and 992 deletions

View File

@@ -942,10 +942,10 @@ func RunLivenessTest(f *framework.Framework, pod *v1.Pod, expectNumRestarts int,
gomega.Expect(pod.Spec.Containers).NotTo(gomega.BeEmpty())
containerName := pod.Spec.Containers[0].Name
// At the end of the test, clean up by removing the pod.
defer func() {
ginkgo.DeferCleanup(func(ctx context.Context) error {
ginkgo.By("deleting the pod")
podClient.Delete(context.TODO(), pod.Name, *metav1.NewDeleteOptions(0))
}()
return podClient.Delete(ctx, pod.Name, *metav1.NewDeleteOptions(0))
})
ginkgo.By(fmt.Sprintf("Creating pod %s in namespace %s", pod.Name, ns))
podClient.Create(pod)
@@ -1002,10 +1002,10 @@ func runReadinessFailTest(f *framework.Framework, pod *v1.Pod, notReadyUntil tim
gomega.Expect(pod.Spec.Containers).NotTo(gomega.BeEmpty())
// At the end of the test, clean up by removing the pod.
defer func() {
ginkgo.DeferCleanup(func(ctx context.Context) error {
ginkgo.By("deleting the pod")
podClient.Delete(context.TODO(), pod.Name, *metav1.NewDeleteOptions(0))
}()
return podClient.Delete(ctx, pod.Name, *metav1.NewDeleteOptions(0))
})
ginkgo.By(fmt.Sprintf("Creating pod %s in namespace %s", pod.Name, ns))
podClient.Create(pod)

View File

@@ -376,9 +376,7 @@ func testPodFailSubpath(f *framework.Framework, pod *v1.Pod) {
podClient := e2epod.NewPodClient(f)
pod = podClient.Create(pod)
defer func() {
e2epod.DeletePodWithWait(f.ClientSet, pod)
}()
ginkgo.DeferCleanup(e2epod.DeletePodWithWait, f.ClientSet, pod)
err := e2epod.WaitForPodContainerToFail(f.ClientSet, pod.Namespace, pod.Name, 0, "CreateContainerConfigError", framework.PodStartShortTimeout)
framework.ExpectNoError(err, "while waiting for the pod container to fail")

View File

@@ -105,7 +105,7 @@ while true; do sleep 1; done
Volumes: testVolumes,
}
terminateContainer.Create()
defer terminateContainer.Delete()
ginkgo.DeferCleanup(framework.IgnoreNotFound(terminateContainer.Delete))
ginkgo.By(fmt.Sprintf("Container '%s': should get the expected 'RestartCount'", testContainer.Name))
gomega.Eventually(func() (int32, error) {
@@ -151,7 +151,7 @@ while true; do sleep 1; done
ginkgo.By("create the container")
c.Create()
defer c.Delete()
ginkgo.DeferCleanup(framework.IgnoreNotFound(c.Delete))
ginkgo.By(fmt.Sprintf("wait for the container to reach %s", expectedPhase))
gomega.Eventually(c.GetPhase, ContainerStatusRetryTimeout, ContainerStatusPollInterval).Should(gomega.Equal(expectedPhase))
@@ -303,7 +303,7 @@ while true; do sleep 1; done
ginkgo.By("create image pull secret")
_, err := f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Create(context.TODO(), secret, metav1.CreateOptions{})
framework.ExpectNoError(err)
defer f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Delete(context.TODO(), secret.Name, metav1.DeleteOptions{})
ginkgo.DeferCleanup(f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Delete, secret.Name, metav1.DeleteOptions{})
container.ImagePullSecrets = []string{secret.Name}
}
// checkContainerStatus checks whether the container status matches expectation.

View File

@@ -61,7 +61,7 @@ var _ = SIGDescribe("RuntimeClass", func() {
ginkgo.It("should reject a Pod requesting a RuntimeClass with an unconfigured handler [NodeFeature:RuntimeHandler]", func(ctx context.Context) {
handler := f.Namespace.Name + "-handler"
rcName := createRuntimeClass(f, "unconfigured-handler", handler, nil)
defer deleteRuntimeClass(f, rcName)
ginkgo.DeferCleanup(deleteRuntimeClass, f, rcName)
pod := e2epod.NewPodClient(f).Create(e2enode.NewRuntimeClassPod(rcName))
eventSelector := fields.Set{
"involvedObject.kind": "Pod",
@@ -88,7 +88,7 @@ var _ = SIGDescribe("RuntimeClass", func() {
e2eskipper.SkipUnlessProviderIs("gce")
rcName := createRuntimeClass(f, "preconfigured-handler", e2enode.PreconfiguredRuntimeClassHandler, nil)
defer deleteRuntimeClass(f, rcName)
ginkgo.DeferCleanup(deleteRuntimeClass, f, rcName)
pod := e2epod.NewPodClient(f).Create(e2enode.NewRuntimeClassPod(rcName))
expectPodSuccess(f, pod)
})
@@ -103,7 +103,7 @@ var _ = SIGDescribe("RuntimeClass", func() {
*/
framework.ConformanceIt("should schedule a Pod requesting a RuntimeClass without PodOverhead [NodeConformance]", func(ctx context.Context) {
rcName := createRuntimeClass(f, "preconfigured-handler", e2enode.PreconfiguredRuntimeClassHandler, nil)
defer deleteRuntimeClass(f, rcName)
ginkgo.DeferCleanup(deleteRuntimeClass, f, rcName)
pod := e2epod.NewPodClient(f).Create(e2enode.NewRuntimeClassPod(rcName))
// there is only one pod in the namespace
label := labels.SelectorFromSet(labels.Set(map[string]string{}))
@@ -133,7 +133,7 @@ var _ = SIGDescribe("RuntimeClass", func() {
v1.ResourceName(v1.ResourceMemory): resource.MustParse("1Mi"),
},
})
defer deleteRuntimeClass(f, rcName)
ginkgo.DeferCleanup(deleteRuntimeClass, f, rcName)
pod := e2epod.NewPodClient(f).Create(e2enode.NewRuntimeClassPod(rcName))
// there is only one pod in the namespace
label := labels.SelectorFromSet(labels.Set(map[string]string{}))

View File

@@ -78,11 +78,11 @@ var _ = SIGDescribe("Security Context", func() {
createdPod1 := podClient.Create(makePod(false))
createdPod2 := podClient.Create(makePod(false))
defer func() {
ginkgo.DeferCleanup(func(ctx context.Context) {
ginkgo.By("delete the pods")
podClient.DeleteSync(createdPod1.Name, metav1.DeleteOptions{}, e2epod.DefaultPodDeletionTimeout)
podClient.DeleteSync(createdPod2.Name, metav1.DeleteOptions{}, e2epod.DefaultPodDeletionTimeout)
}()
})
getLogs := func(pod *v1.Pod) (string, error) {
err := e2epod.WaitForPodSuccessInNamespaceTimeout(f.ClientSet, createdPod1.Name, f.Namespace.Name, f.Timeouts.PodStart)
if err != nil {