diff --git a/e2e/util/validation.go b/e2e/util/validation.go index 3c9be576b..e06a79afb 100644 --- a/e2e/util/validation.go +++ b/e2e/util/validation.go @@ -8,32 +8,57 @@ import ( "fmt" "strings" + v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" ) -func CheckRamenHubPodRunningStatus(k8sClient *kubernetes.Clientset) (bool, string, error) { +func ValidateRamenHubOperator(k8sClient *kubernetes.Clientset) error { labelSelector := "app=ramen-hub" podIdentifier := "ramen-hub-operator" ramenNameSpace, err := GetRamenNameSpace(k8sClient) if err != nil { - return false, "", err + return err } - return CheckPodRunningStatus(k8sClient, ramenNameSpace, labelSelector, podIdentifier) + pod, err := FindPod(k8sClient, ramenNameSpace, labelSelector, podIdentifier) + if err != nil { + return err + } + + if pod.Status.Phase != "Running" { + return fmt.Errorf("ramen hub operator pod %q not running (phase %q)", + pod.Name, pod.Status.Phase) + } + + Ctx.Log.Info("Ramen hub operator is running", "pod", pod.Name) + + return nil } -func CheckRamenSpokePodRunningStatus(k8sClient *kubernetes.Clientset) (bool, string, error) { +func ValidateRamenDRClusterOperatorPod(k8sClient *kubernetes.Clientset, clusterName string) error { labelSelector := "app=ramen-dr-cluster" podIdentifier := "ramen-dr-cluster-operator" ramenNameSpace, err := GetRamenNameSpace(k8sClient) if err != nil { - return false, "", err + return err } - return CheckPodRunningStatus(k8sClient, ramenNameSpace, labelSelector, podIdentifier) + pod, err := FindPod(k8sClient, ramenNameSpace, labelSelector, podIdentifier) + if err != nil { + return err + } + + if pod.Status.Phase != "Running" { + return fmt.Errorf("ramen dr cluster operator pod %q not running (phase %q)", + pod.Name, pod.Status.Phase) + } + + Ctx.Log.Info("Ramen dr cluster operator is running", "cluster", clusterName, "pod", pod.Name) + + return nil } func GetRamenNameSpace(k8sClient *kubernetes.Clientset) (string, error) { @@ -68,23 +93,25 @@ func IsOpenShiftCluster(k8sClient *kubernetes.Clientset) (bool, error) { return false, nil } -// CheckPodRunningStatus checks if there is at least one pod matching the labelSelector +// FindPod checks if there is at least one pod matching the labelSelector // in the given namespace that is in the "Running" phase and contains the podIdentifier in its name. -func CheckPodRunningStatus(client *kubernetes.Clientset, namespace, labelSelector, podIdentifier string) ( - bool, string, error, +func FindPod(client *kubernetes.Clientset, namespace, labelSelector, podIdentifier string) ( + *v1.Pod, error, ) { pods, err := client.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{ LabelSelector: labelSelector, }) if err != nil { - return false, "", fmt.Errorf("failed to list pods in namespace %s: %v", namespace, err) + return nil, fmt.Errorf("failed to list pods in namespace %s: %v", namespace, err) } - for _, pod := range pods.Items { - if strings.Contains(pod.Name, podIdentifier) && pod.Status.Phase == "Running" { - return true, pod.Name, nil + for i := range pods.Items { + pod := &pods.Items[i] + if strings.Contains(pod.Name, podIdentifier) { + return pod, nil } } - return false, "", nil + return nil, fmt.Errorf("no pod with labelSelctor %q and identifier %q in namespace %q", + labelSelector, podIdentifier, namespace) } diff --git a/e2e/validation_suite_test.go b/e2e/validation_suite_test.go index 4ca531e4e..a9291aff7 100644 --- a/e2e/validation_suite_test.go +++ b/e2e/validation_suite_test.go @@ -11,53 +11,22 @@ import ( func Validate(t *testing.T) { t.Helper() - - if !t.Run("CheckRamenHubOperatorStatus", CheckRamenHubOperatorStatus) { - t.Error("CheckRamenHubOperatorStatus failed") - } - - if !t.Run("CheckRamenSpokeOperatorStatus", CheckRamenSpokeOperatorStatus) { - t.Error("CheckRamenHubOperatorStatus failed") - } -} - -func CheckRamenHubOperatorStatus(t *testing.T) { - util.Ctx.Log.Info("enter CheckRamenHubOperatorStatus") - - isRunning, podName, err := util.CheckRamenHubPodRunningStatus(util.Ctx.Hub.K8sClientSet) - if err != nil { - t.Error(err) - } - - if isRunning { - util.Ctx.Log.Info("Ramen Hub Operator is running", "pod", podName) - } else { - t.Error("no running Ramen Hub Operator pod found") - } -} - -func CheckRamenSpokeOperatorStatus(t *testing.T) { - util.Ctx.Log.Info("enter CheckRamenSpokeOperatorStatus") - - isRunning, podName, err := util.CheckRamenSpokePodRunningStatus(util.Ctx.C1.K8sClientSet) - if err != nil { - t.Error(err) - } - - if isRunning { - util.Ctx.Log.Info("Ramen Spoke Operator is running on cluster 1", "pod", podName) - } else { - t.Error("no running Ramen Spoke Operator pod on cluster 1") - } - - isRunning, podName, err = util.CheckRamenSpokePodRunningStatus(util.Ctx.C2.K8sClientSet) - if err != nil { - t.Error(err) - } - - if isRunning { - util.Ctx.Log.Info("Ramen Spoke Operator is running on cluster 2", "pod", podName) - } else { - t.Error("no running Ramen Spoke Operator pod on cluster 2") - } + t.Run("hub", func(t *testing.T) { + err := util.ValidateRamenHubOperator(util.Ctx.Hub.K8sClientSet) + if err != nil { + t.Fatal(err) + } + }) + t.Run("c1", func(t *testing.T) { + err := util.ValidateRamenDRClusterOperatorPod(util.Ctx.C1.K8sClientSet, "c1") + if err != nil { + t.Fatal(err) + } + }) + t.Run("c2", func(t *testing.T) { + err := util.ValidateRamenDRClusterOperatorPod(util.Ctx.C2.K8sClientSet, "c2") + if err != nil { + t.Fatal(err) + } + }) }