From 6ccb98cad13296f382728ee2d6abb4bf08aceed1 Mon Sep 17 00:00:00 2001 From: Andrew Bays Date: Tue, 20 Feb 2024 15:08:32 +0000 Subject: [PATCH] Test helper func for simulating pod ready state --- modules/common/test/helpers/pod.go | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/modules/common/test/helpers/pod.go b/modules/common/test/helpers/pod.go index 1129fd57..5e50f465 100644 --- a/modules/common/test/helpers/pod.go +++ b/modules/common/test/helpers/pod.go @@ -47,3 +47,40 @@ func (tc *TestHelper) SimulatePodPhaseRunning(name types.NamespacedName) { }, tc.Timeout, tc.Interval).Should(gomega.Succeed()) tc.Logger.Info("Simulated pod running phase", "on", name) } + +// SimulatePodReady retrieves the Pod and simulates +// a Ready condition for the Pod in a Kubernetes cluster. +// +// example usage: +// +// th.SimulatePodReady(types.NamespacedName{Name: "test-pod", Namespace: "test-namespace"}) +func (tc *TestHelper) SimulatePodReady(name types.NamespacedName) { + gomega.Eventually(func(g gomega.Gomega) { + pod := tc.GetPod(name) + + found := false + + for index, condition := range pod.Status.Conditions { + if condition.Type == corev1.PodReady { + condition.Status = corev1.ConditionTrue + pod.Status.Conditions[index] = condition + found = true + break + } + } + + if !found { + pod.Status.Conditions = append( + pod.Status.Conditions, + corev1.PodCondition{ + Type: corev1.PodReady, + Status: corev1.ConditionTrue, + }, + ) + } + + g.Expect(tc.K8sClient.Status().Update(tc.Ctx, pod)).To(gomega.Succeed()) + + }, tc.Timeout, tc.Interval).Should(gomega.Succeed()) + tc.Logger.Info("Simulated pod ready state", "on", name) +}