Skip to content

Commit

Permalink
WIP: Improve e2e logging
Browse files Browse the repository at this point in the history
- Use the logger for logging test errors and skips.
- Cleanup validation test suite

Signed-off-by: Nir Soffer <nsoffer@redhat.com>
  • Loading branch information
nirs committed Oct 22, 2024
1 parent cce2573 commit 52008ae
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 53 deletions.
25 changes: 13 additions & 12 deletions e2e/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
20 changes: 10 additions & 10 deletions e2e/exhaustive_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

Expand Down Expand Up @@ -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)
}
}
35 changes: 35 additions & 0 deletions e2e/util/testing.go
Original file line number Diff line number Diff line change
@@ -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()
}
67 changes: 36 additions & 31 deletions e2e/validation_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 52008ae

Please sign in to comment.