diff --git a/e2e/actions_test.go b/e2e/actions_test.go index 73bc986eb..98be53cf4 100644 --- a/e2e/actions_test.go +++ b/e2e/actions_test.go @@ -8,70 +8,71 @@ import ( "github.com/ramendr/ramen/e2e/dractions" "github.com/ramendr/ramen/e2e/testcontext" + "github.com/ramendr/ramen/e2e/util" ) func DeployAction(t *testing.T) { testCtx, err := testcontext.GetTestContext(t.Name()) if err != nil { - t.Error(err) + util.Fatal(t, "Deploy failed", err) } if err := testCtx.Deployer.Deploy(testCtx.Workload); err != nil { - t.Error(err) + util.Fatal(t, "Deploy failed", err) } } func EnableAction(t *testing.T) { testCtx, err := testcontext.GetTestContext(t.Name()) if err != nil { - t.Error(err) + util.Fatal(t, "Enable failed", err) } if err := dractions.EnableProtection(testCtx.Workload, testCtx.Deployer); err != nil { - t.Error(err) + util.Fatal(t, "Enable failed", err) } } func FailoverAction(t *testing.T) { testCtx, err := testcontext.GetTestContext(t.Name()) if err != nil { - t.Error(err) + util.Fatal(t, "Failover failed", err) } if err := dractions.Failover(testCtx.Workload, testCtx.Deployer); err != nil { - t.Error(err) + util.Fatal(t, "Failover failed", err) } } func RelocateAction(t *testing.T) { testCtx, err := testcontext.GetTestContext(t.Name()) if err != nil { - t.Error(err) + util.Fatal(t, "Relocate failed", err) } if err := dractions.Relocate(testCtx.Workload, testCtx.Deployer); err != nil { - t.Error(err) + util.Fatal(t, "Relocate failed", err) } } func DisableAction(t *testing.T) { testCtx, err := testcontext.GetTestContext(t.Name()) if err != nil { - t.Error(err) + util.Fatal(t, "Disable failed", err) } if err := dractions.DisableProtection(testCtx.Workload, testCtx.Deployer); err != nil { - t.Error(err) + util.Fatal(t, "Disable failed", err) } } func UndeployAction(t *testing.T) { testCtx, err := testcontext.GetTestContext(t.Name()) if err != nil { - t.Error(err) + util.Fatal(t, "Undeploy failed", err) } if err := testCtx.Deployer.Undeploy(testCtx.Workload); err != nil { - t.Error(err) + util.Fatal(t, "Undeploy failed", err) } } diff --git a/e2e/exhaustive_suite_test.go b/e2e/exhaustive_suite_test.go index e66cba4d1..296c39b2a 100644 --- a/e2e/exhaustive_suite_test.go +++ b/e2e/exhaustive_suite_test.go @@ -67,12 +67,12 @@ func Exhaustive(t *testing.T) { t.Parallel() if err := util.EnsureChannel(); err != nil { - t.Fatalf("failed to ensure channel: %v", err) + util.Fatal(t, "Failed to ensure channel", err) } t.Cleanup(func() { if err := util.EnsureChannelDeleted(); err != nil { - t.Fatalf("failed to ensure channel deleted: %v", err) + util.Fatal(t, "Failed to ensure channel deleted", err) } }) @@ -103,34 +103,34 @@ func runTestFlow(t *testing.T) { testCtx, err := testcontext.GetTestContext(t.Name()) if err != nil { - t.Fatal(err) + util.Fatal(t, "Failed to get test context", err) } if !testCtx.Deployer.IsWorkloadSupported(testCtx.Workload) { - t.Skipf("Workload %s not supported by deployer %s, skip test", testCtx.Workload.GetName(), testCtx.Deployer.GetName()) + util.Skipf(t, "Workload %s not supported by deployer %s", testCtx.Workload.GetName(), testCtx.Deployer.GetName()) } if !t.Run("Deploy", DeployAction) { - t.Fatal("Deploy failed") + util.FailNow(t) } if !t.Run("Enable", EnableAction) { - t.Fatal("Enable failed") + util.FailNow(t) } if !t.Run("Failover", FailoverAction) { - t.Fatal("Failover failed") + util.FailNow(t) } if !t.Run("Relocate", RelocateAction) { - t.Fatal("Relocate failed") + util.FailNow(t) } if !t.Run("Disable", DisableAction) { - t.Fatal("Disable failed") + util.FailNow(t) } if !t.Run("Undeploy", UndeployAction) { - t.Fatal("Undeploy failed") + util.FailNow(t) } } diff --git a/e2e/util/testing.go b/e2e/util/testing.go new file mode 100644 index 000000000..1f93bf15b --- /dev/null +++ b/e2e/util/testing.go @@ -0,0 +1,35 @@ +// SPDX-FileCopyrightText: The RamenDR authors +// SPDX-License-Identifier: Apache-2.0 + +package util + +import ( + "fmt" + "testing" +) + +// Fatal logs an error and fails the test. +func Fatal(t *testing.T, msg string, err error) { + t.Helper() + Ctx.Log.Error(err, msg) + t.FailNow() +} + +// FailNow fails the tests silently. Use for parent tests. +func FailNow(t *testing.T) { + t.Helper() + t.FailNow() +} + +// Skipf logs formmatted message the skips the test. +func Skipf(t *testing.T, format string, args ...any) { + t.Helper() + Skip(t, fmt.Sprintf(format, args...)) +} + +// Skip log msg and skips the test. +func Skip(t *testing.T, msg string) { + t.Helper() + Ctx.Log.Info(msg) + t.SkipNow() +} diff --git a/e2e/validation_suite_test.go b/e2e/validation_suite_test.go index 4ca531e4e..9b6e73e77 100644 --- a/e2e/validation_suite_test.go +++ b/e2e/validation_suite_test.go @@ -4,60 +4,65 @@ package e2e_test import ( + "errors" "testing" "github.com/ramendr/ramen/e2e/util" + "k8s.io/client-go/kubernetes" ) func Validate(t *testing.T) { t.Helper() - if !t.Run("CheckRamenHubOperatorStatus", CheckRamenHubOperatorStatus) { - t.Error("CheckRamenHubOperatorStatus failed") - } + // TODO: Use real cluster names from config - if !t.Run("CheckRamenSpokeOperatorStatus", CheckRamenSpokeOperatorStatus) { - t.Error("CheckRamenHubOperatorStatus failed") - } + t.Run("hub", func(t *testing.T) { + if err := validateHub(util.Ctx.Hub.K8sClientSet, "hub"); err != nil { + util.Fatal(t, "Validating cluster hub failed", err) + } + }) + t.Run("c1", func(t *testing.T) { + if err := validateCluster(util.Ctx.C1.K8sClientSet, "c1"); err != nil { + util.Fatal(t, "Validating cluster c2 failed", err) + } + }) + t.Run("c2", func(t *testing.T) { + if err := validateCluster(util.Ctx.C1.K8sClientSet, "c2"); err != nil { + util.Fatal(t, "Validating cluster c2 failed", err) + } + }) } -func CheckRamenHubOperatorStatus(t *testing.T) { - util.Ctx.Log.Info("enter CheckRamenHubOperatorStatus") +func validateHub(client *kubernetes.Clientset, name string) error { + util.Ctx.Log.Info("Validating hub cluster", "name", name) - isRunning, podName, err := util.CheckRamenHubPodRunningStatus(util.Ctx.Hub.K8sClientSet) + isRunning, podName, err := util.CheckRamenHubPodRunningStatus(client) if err != nil { - t.Error(err) + return err } - if isRunning { - util.Ctx.Log.Info("Ramen Hub Operator is running", "pod", podName) - } else { - t.Error("no running Ramen Hub Operator pod found") + if !isRunning { + return errors.New("no running ramen-hub-operator pod found") } + + util.Ctx.Log.Info("Ramen hub operator is running", "pod", podName) + + return nil } -func CheckRamenSpokeOperatorStatus(t *testing.T) { - util.Ctx.Log.Info("enter CheckRamenSpokeOperatorStatus") +func validateCluster(client *kubernetes.Clientset, name string) error { + util.Ctx.Log.Info("Validating managed cluster", "name", name) - isRunning, podName, err := util.CheckRamenSpokePodRunningStatus(util.Ctx.C1.K8sClientSet) + isRunning, podName, err := util.CheckRamenSpokePodRunningStatus(client) if err != nil { - t.Error(err) + return 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") + if !isRunning { + return errors.New("no running ramen-dr-cluster-operator pod found") } - isRunning, podName, err = util.CheckRamenSpokePodRunningStatus(util.Ctx.C2.K8sClientSet) - if err != nil { - t.Error(err) - } + util.Ctx.Log.Info("Ramen DR cluster operator is running", "pod", podName) - 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") - } + return nil }