Skip to content

Commit

Permalink
Simplify test context management
Browse files Browse the repository at this point in the history
We had 2 places using a test context - test suites, and action helpers.
To work with both, we store the test context in a map and had
complicated search mechanism to locate the context in both the parent
and child sub-tests. This failed randomly since the map was not
protected with a mutex.

Simplify the design by implementing the actions (deploy, enable, ..) in
the test context. With this we can create a test context instance and
pass it to the code running a test flow, and we don't need to manage any
global state.

Fixes: RamenDR#1571
Signed-off-by: Nir Soffer <nsoffer@redhat.com>
  • Loading branch information
nirs committed Oct 22, 2024
1 parent cce2573 commit fbc3ece
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 120 deletions.
77 changes: 0 additions & 77 deletions e2e/actions_test.go

This file was deleted.

28 changes: 11 additions & 17 deletions e2e/exhaustive_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,48 +89,42 @@ func Exhaustive(t *testing.T) {
t.Parallel()
t.Run(d.GetName(), func(t *testing.T) {
t.Parallel()
testcontext.AddTestContext(t.Name(), w, d)
runTestFlow(t)
testcontext.DeleteTestContext(t.Name(), w, d)
ctx := testcontext.TestContext{Workload: w, Deployer: d}
runTestFlow(t, ctx)
})
})
}
}
}

func runTestFlow(t *testing.T) {
func runTestFlow(t *testing.T, ctx testcontext.TestContext) {
t.Helper()

testCtx, err := testcontext.GetTestContext(t.Name())
if err != nil {
t.Fatal(err)
if !ctx.Deployer.IsWorkloadSupported(ctx.Workload) {
t.Skipf("Workload %s not supported by deployer %s, skip test", ctx.Workload.GetName(), ctx.Deployer.GetName())
}

if !testCtx.Deployer.IsWorkloadSupported(testCtx.Workload) {
t.Skipf("Workload %s not supported by deployer %s, skip test", testCtx.Workload.GetName(), testCtx.Deployer.GetName())
}

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

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

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

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

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

if !t.Run("Undeploy", UndeployAction) {
if !t.Run("Undeploy", ctx.Undeploy) {
t.Fatal("Undeploy failed")
}
}
55 changes: 29 additions & 26 deletions e2e/testcontext/testcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package testcontext

import (
"fmt"
"strings"
"testing"

"github.com/ramendr/ramen/e2e/deployers"
"github.com/ramendr/ramen/e2e/dractions"
"github.com/ramendr/ramen/e2e/workloads"
)

Expand All @@ -16,35 +16,38 @@ type TestContext struct {
Deployer deployers.Deployer
}

var testContextMap = make(map[string]TestContext)
func (c *TestContext) Deploy(t *testing.T) {

Check failure on line 19 in e2e/testcontext/testcontext.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (e2e)

test helper function should start from t.Helper() (thelper)
if err := c.Deployer.Deploy(c.Workload); err != nil {
t.Fatal(err)
}
}

// Based on name passed, Init the deployer and Workload and stash in a map[string]TestContext
func AddTestContext(name string, w workloads.Workload, d deployers.Deployer) {
testContextMap[name] = TestContext{w, d}
func (c *TestContext) Enable(t *testing.T) {

Check failure on line 25 in e2e/testcontext/testcontext.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (e2e)

test helper function should start from t.Helper() (thelper)
if err := dractions.EnableProtection(c.Workload, c.Deployer); err != nil {
t.Fatal(err)
}
}

func DeleteTestContext(name string, w workloads.Workload, d deployers.Deployer) {
delete(testContextMap, name)
func (c *TestContext) Failover(t *testing.T) {

Check failure on line 31 in e2e/testcontext/testcontext.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (e2e)

test helper function should start from t.Helper() (thelper)
if err := dractions.Failover(c.Workload, c.Deployer); err != nil {
t.Fatal(err)
}
}

// Search name in map for a TestContext to return, if not found go backward
// - drop the last /<name> suffix form name and search
// - e.g If name passed is "TestSuites/Exhaustive/DaemonSet/Subscription/Undeploy"
// - Search for above name first (it will not be found as we create context at a point where we have a d+w)
// - Search for "TestSuites/Exhaustive/DaemonSet/Subscription" (should be found)
func GetTestContext(name string) (TestContext, error) {
testCtx, ok := testContextMap[name]
if !ok {
i := strings.LastIndex(name, "/")
if i < 1 {
return TestContext{}, fmt.Errorf("not a valid name in TestContext: %v", name)
}

testCtx, ok = testContextMap[name[0:i]]
if !ok {
return TestContext{}, fmt.Errorf("can not find testContext with name: %v", name)
}
func (c *TestContext) Relocate(t *testing.T) {
if err := dractions.Relocate(c.Workload, c.Deployer); err != nil {
t.Fatal(err)
}
}

return testCtx, nil
func (c *TestContext) Disable(t *testing.T) {
if err := dractions.DisableProtection(c.Workload, c.Deployer); err != nil {
t.Fatal(err)
}
}

func (c *TestContext) Undeploy(t *testing.T) {
if err := c.Deployer.Undeploy(c.Workload); err != nil {
t.Fatal(err)
}
}

0 comments on commit fbc3ece

Please sign in to comment.