Skip to content

Commit

Permalink
Fix testcontext map thread safety
Browse files Browse the repository at this point in the history
We modify the textcontext map form multiple tests running in parallel,
so we must synchronize access to the map.

Fixes RamenDR#1571

Signed-off-by: Nir Soffer <nsoffer@redhat.com>
  • Loading branch information
nirs committed Oct 21, 2024
1 parent 8c3a92b commit dbe428e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions e2e/testcontext/testcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package testcontext
import (
"fmt"
"strings"
"sync"

"github.com/ramendr/ramen/e2e/deployers"
"github.com/ramendr/ramen/e2e/workloads"
Expand All @@ -16,14 +17,22 @@ type TestContext struct {
Deployer deployers.Deployer
}

var mutex sync.Mutex

var contexts = make(map[string]TestContext)

// 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) {
mutex.Lock()
defer mutex.Unlock()

contexts[name] = TestContext{w, d}
}

func DeleteTestContext(name string) {
mutex.Lock()
defer mutex.Unlock()

delete(contexts, name)
}

Expand All @@ -33,6 +42,9 @@ func DeleteTestContext(name string) {
// - 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) {
mutex.Lock()
defer mutex.Unlock()

testCtx, ok := contexts[name]
if !ok {
i := strings.LastIndex(name, "/")
Expand Down

0 comments on commit dbe428e

Please sign in to comment.