diff --git a/test/e2e_node/mirror_pod_grace_period_test.go b/test/e2e_node/mirror_pod_grace_period_test.go index 9e8a1c63205..26710a1c1e1 100644 --- a/test/e2e_node/mirror_pod_grace_period_test.go +++ b/test/e2e_node/mirror_pod_grace_period_test.go @@ -26,8 +26,11 @@ import ( "github.com/onsi/gomega" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/uuid" + clientset "k8s.io/client-go/kubernetes" "k8s.io/kubernetes/test/e2e/framework" + imageutils "k8s.io/kubernetes/test/utils/image" ) var _ = SIGDescribe("MirrorPodWithGracePeriod", func() { @@ -53,10 +56,9 @@ var _ = SIGDescribe("MirrorPodWithGracePeriod", func() { ginkgo.It("mirror pod termination should satisfy grace period when static pod is deleted [NodeConformance]", func() { ginkgo.By("get mirror pod uid") - _, err := f.ClientSet.CoreV1().Pods(ns).Get(context.TODO(), mirrorPodName, metav1.GetOptions{}) + pod, err := f.ClientSet.CoreV1().Pods(ns).Get(context.TODO(), mirrorPodName, metav1.GetOptions{}) framework.ExpectNoError(err) - - start := time.Now() + uid := pod.UID ginkgo.By("delete the static pod") file := staticPodPath(podPath, staticPodName, ns) @@ -64,25 +66,51 @@ var _ = SIGDescribe("MirrorPodWithGracePeriod", func() { err = os.Remove(file) framework.ExpectNoError(err) - for { - if time.Now().Sub(start).Seconds() > 19 { - break - } - pod, err := f.ClientSet.CoreV1().Pods(ns).Get(context.TODO(), mirrorPodName, metav1.GetOptions{}) - framework.ExpectNoError(err) - if pod.Status.Phase != v1.PodRunning { - framework.Failf("expected the mirror pod %q to be running, got %q", mirrorPodName, pod.Status.Phase) - } - // have some pause in between the API server queries to avoid throttling - time.Sleep(time.Duration(200) * time.Millisecond) - } + ginkgo.By("wait for the mirror pod to be running for grace period") + gomega.Consistently(func() error { + return checkMirrorPodRunningWithUID(f.ClientSet, mirrorPodName, ns, uid) + }, 19*time.Second, 200*time.Millisecond).Should(gomega.BeNil()) + }) + + ginkgo.It("mirror pod termination should satisfy grace period when static pod is updated [NodeConformance]", func() { + ginkgo.By("get mirror pod uid") + pod, err := f.ClientSet.CoreV1().Pods(ns).Get(context.TODO(), mirrorPodName, metav1.GetOptions{}) + framework.ExpectNoError(err) + uid := pod.UID + + ginkgo.By("update the static pod container image") + image := imageutils.GetPauseImageName() + err = createStaticPod(podPath, staticPodName, ns, image, v1.RestartPolicyAlways) + framework.ExpectNoError(err) + + ginkgo.By("wait for the mirror pod to be running for grace period") + gomega.Consistently(func() error { + return checkMirrorPodRunningWithUID(f.ClientSet, mirrorPodName, ns, uid) + }, 19*time.Second, 200*time.Millisecond).Should(gomega.BeNil()) + + ginkgo.By("wait for the mirror pod to be updated") + gomega.Eventually(func() error { + return checkMirrorPodRecreatedAndRunning(f.ClientSet, mirrorPodName, ns, uid) + }, 2*time.Minute, time.Second*4).Should(gomega.BeNil()) + + ginkgo.By("check the mirror pod container image is updated") + pod, err = f.ClientSet.CoreV1().Pods(ns).Get(context.TODO(), mirrorPodName, metav1.GetOptions{}) + framework.ExpectNoError(err) + framework.ExpectEqual(len(pod.Spec.Containers), 1) + framework.ExpectEqual(pod.Spec.Containers[0].Image, image) }) ginkgo.AfterEach(func() { + ginkgo.By("delete the static pod") + err := deleteStaticPod(podPath, staticPodName, ns) + if !os.IsNotExist(err) { + framework.ExpectNoError(err) + } + ginkgo.By("wait for the mirror pod to disappear") gomega.Eventually(func() error { return checkMirrorPodDisappear(f.ClientSet, mirrorPodName, ns) - }, time.Second*19, time.Second).Should(gomega.BeNil()) + }, 2*time.Minute, time.Second*4).Should(gomega.BeNil()) }) }) }) @@ -124,3 +152,17 @@ spec: framework.Logf("has written %v", file) return err } + +func checkMirrorPodRunningWithUID(cl clientset.Interface, name, namespace string, oUID types.UID) error { + pod, err := cl.CoreV1().Pods(namespace).Get(context.TODO(), name, metav1.GetOptions{}) + if err != nil { + return fmt.Errorf("expected the mirror pod %q to appear: %v", name, err) + } + if pod.UID != oUID { + return fmt.Errorf("expected the uid of mirror pod %q to be same, got %q", name, pod.UID) + } + if pod.Status.Phase != v1.PodRunning { + return fmt.Errorf("expected the mirror pod %q to be running, got %q", name, pod.Status.Phase) + } + return nil +}