Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e2e: Log errors #1683

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions e2e/exhaustive_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,19 @@ func generateWorkloads([]types.Workload) {
}
}

func Exhaustive(t *testing.T) {
// nolint: thelper
func Exhaustive(dt *testing.T) {
t := test.WithLog(dt, util.Ctx.Log)
t.Helper()
t.Parallel()

if err := util.EnsureChannel(); err != nil {
t.Fatalf("failed to ensure channel: %v", err)
t.Fatalf("Failed to ensure channel: %s", err)
}

t.Cleanup(func() {
if err := util.EnsureChannelDeleted(); err != nil {
t.Fatalf("failed to ensure channel deleted: %v", err)
t.Fatalf("Failed to ensure channel deleted: %s", err)
}
})

Expand All @@ -82,42 +84,43 @@ func Exhaustive(t *testing.T) {
for _, deployer := range Deployers {
for _, workload := range Workloads {
ctx := test.NewContext(workload, deployer, util.Ctx.Log)
t.Run(ctx.Name(), func(t *testing.T) {
t.Run(ctx.Name(), func(dt *testing.T) {
t := test.WithLog(dt, ctx.Logger())
t.Parallel()
runTestFlow(t, ctx)
})
}
}
}

func runTestFlow(t *testing.T, ctx test.Context) {
func runTestFlow(t *test.T, ctx test.Context) {
t.Helper()

if err := ctx.Validate(); err != nil {
t.Skipf("Skip test: %s", err)
}

if !t.Run("Deploy", ctx.Deploy) {
t.Fatal("Deploy failed")
t.FailNow()
}

if !t.Run("Enable", ctx.Enable) {
t.Fatal("Enable failed")
t.FailNow()
}

if !t.Run("Failover", ctx.Failover) {
t.Fatal("Failover failed")
t.FailNow()
}

if !t.Run("Relocate", ctx.Relocate) {
t.Fatal("Relocate failed")
t.FailNow()
}

if !t.Run("Disable", ctx.Disable) {
t.Fatal("Disable failed")
t.FailNow()
}

if !t.Run("Undeploy", ctx.Undeploy) {
t.Fatal("Undeploy failed")
t.FailNow()
}
}
8 changes: 5 additions & 3 deletions e2e/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"testing"

"github.com/ramendr/ramen/e2e/test"
"github.com/ramendr/ramen/e2e/util"
uberzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -53,11 +54,12 @@ var Suites = []testDef{
{"Exhaustive", Exhaustive},
}

func TestSuites(t *testing.T) {
util.Ctx.Log.Info(t.Name())
func TestSuites(dt *testing.T) {
t := test.WithLog(dt, util.Ctx.Log)
t.Log(t.Name())

if !t.Run("Validate", Validate) {
t.Fatal("failed to validate the test suite")
t.FailNow()
}

for _, suite := range Suites {
Expand Down
31 changes: 19 additions & 12 deletions e2e/test/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-FileCopyrightText: The RamenDR authors
// SPDX-License-Identifier: Apache-2.0

// nolint: thelper // for using dt *testing.T and keeping test code idiomatic.
package test

import (
Expand Down Expand Up @@ -65,50 +66,56 @@ func (c *Context) Validate() error {
return nil
}

func (c *Context) Deploy(t *testing.T) {
func (c *Context) Deploy(dt *testing.T) {
t := WithLog(dt, c.logger)
t.Helper()

if err := c.deployer.Deploy(c); err != nil {
t.Fatal(err)
t.Fatalf("Failed to deploy workload: %s", err)
}
}

func (c *Context) Undeploy(t *testing.T) {
func (c *Context) Undeploy(dt *testing.T) {
t := WithLog(dt, c.logger)
t.Helper()

if err := c.deployer.Undeploy(c); err != nil {
t.Fatal(err)
t.Fatalf("Failed to undeploy workload: %s", err)
}
}

func (c *Context) Enable(t *testing.T) {
func (c *Context) Enable(dt *testing.T) {
t := WithLog(dt, c.logger)
t.Helper()

if err := dractions.EnableProtection(c); err != nil {
t.Fatal(err)
t.Fatalf("Failed to enable protection for workload: %s", err)
}
}

func (c *Context) Disable(t *testing.T) {
func (c *Context) Disable(dt *testing.T) {
t := WithLog(dt, c.logger)
t.Helper()

if err := dractions.DisableProtection(c); err != nil {
t.Fatal(err)
t.Fatalf("Failed to disable protection for workload: %s", err)
}
}

func (c *Context) Failover(t *testing.T) {
func (c *Context) Failover(dt *testing.T) {
t := WithLog(dt, c.logger)
t.Helper()

if err := dractions.Failover(c); err != nil {
t.Fatal(err)
t.Fatalf("Failed to failover workload: %s", err)
}
}

func (c *Context) Relocate(t *testing.T) {
func (c *Context) Relocate(dt *testing.T) {
t := WithLog(dt, c.logger)
t.Helper()

if err := dractions.Relocate(c); err != nil {
t.Fatal(err)
t.Fatalf("Failed to relocate workload: %s", err)
}
}
69 changes: 69 additions & 0 deletions e2e/test/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-FileCopyrightText: The RamenDR authors
// SPDX-License-Identifier: Apache-2.0

package test

import (
"fmt"
"testing"

"github.com/go-logr/logr"
)

// T extends testing.T to use a custom logger.
type T struct {
*testing.T
log logr.Logger
}

// WithLog returns a t wrapped with a specified log.
// nolint: thelper
func WithLog(t *testing.T, log logr.Logger) *T {
return &T{T: t, log: log}
}

// Log writes a message to the log.
func (t *T) Log(msg string) {
t.log.Info(msg)
}

// Log writes a formatted message to the log.
func (t *T) Logf(format string, args ...any) {
t.log.Info(fmt.Sprintf(format, args...))
}

// Error writes an error message to the log and mark the test as failed.
func (t *T) Error(msg string) {
t.log.Error(nil, msg)
t.T.Fail()
}

// Errorf writes a formatted error message to the log and markd the test as failed.
func (t *T) Errorf(format string, args ...any) {
t.log.Error(nil, fmt.Sprintf(format, args...))
t.T.Fail()
}

// Fatal writes an error message to the log and fail the text immediately.
func (t *T) Fatal(msg string) {
t.log.Error(nil, msg)
t.T.FailNow()
}

// Fatalf writes a formatted error message to the log and fail the text immediately.
func (t *T) Fatalf(format string, args ...any) {
t.log.Error(nil, fmt.Sprintf(format, args...))
t.T.FailNow()
}

// Skip is equivalent to Log followed by SkipNow.
func (t *T) Skip(msg string) {
t.log.Info(msg)
t.T.SkipNow()
}

// Skipf is equivalent to Logf followed by SkipNow.
func (t *T) Skipf(format string, args ...any) {
t.log.Info(fmt.Sprintf(format, args...))
t.T.SkipNow()
}
22 changes: 15 additions & 7 deletions e2e/validation_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,35 @@ package e2e_test
import (
"testing"

"github.com/ramendr/ramen/e2e/test"
"github.com/ramendr/ramen/e2e/util"
)

func Validate(t *testing.T) {
func Validate(dt *testing.T) {
t := test.WithLog(dt, util.Ctx.Log)
t.Helper()
t.Run("hub", func(t *testing.T) {
t.Run("hub", func(dt *testing.T) {
t := test.WithLog(dt, util.Ctx.Log)

err := util.ValidateRamenHubOperator(util.Ctx.Hub.K8sClientSet)
if err != nil {
t.Fatal(err)
t.Fatalf("Failed to validated hub cluster: %s", err)
}
})
t.Run("c1", func(t *testing.T) {
t.Run("c1", func(dt *testing.T) {
t := test.WithLog(dt, util.Ctx.Log)

err := util.ValidateRamenDRClusterOperator(util.Ctx.C1.K8sClientSet, "c1")
if err != nil {
t.Fatal(err)
t.Fatalf("Failed to validated dr cluster c1: %s", err)
}
})
t.Run("c2", func(t *testing.T) {
t.Run("c2", func(dt *testing.T) {
t := test.WithLog(dt, util.Ctx.Log)

err := util.ValidateRamenDRClusterOperator(util.Ctx.C2.K8sClientSet, "c2")
if err != nil {
t.Fatal(err)
t.Fatalf("Failed to validated dr cluster c2: %s", err)
}
})
}
Loading