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 30, 2024
1 parent b83b03a commit 1664ba6
Show file tree
Hide file tree
Showing 3 changed files with 52 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")
}
}
67 changes: 41 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,50 @@ type TestContext struct {
Deployer deployers.Deployer
}

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

// 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}
if err := c.Deployer.Deploy(c.Workload); err != nil {
t.Fatal(err)
}
}

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

if err := dractions.EnableProtection(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) Failover(t *testing.T) {
t.Helper()

if err := dractions.Failover(c.Workload, c.Deployer); err != nil {
t.Fatal(err)
}
}

func (c *TestContext) Relocate(t *testing.T) {
t.Helper()

return testCtx, nil
if err := dractions.Relocate(c.Workload, c.Deployer); err != nil {
t.Fatal(err)
}
}

func (c *TestContext) Disable(t *testing.T) {
t.Helper()

if err := dractions.DisableProtection(c.Workload, c.Deployer); err != nil {
t.Fatal(err)
}
}

func (c *TestContext) Undeploy(t *testing.T) {
t.Helper()

if err := c.Deployer.Undeploy(c.Workload); err != nil {
t.Fatal(err)
}
}

0 comments on commit 1664ba6

Please sign in to comment.