From dbe428e29d2d6cfd80197991e860d57c27b6acf0 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Mon, 21 Oct 2024 18:36:26 +0300 Subject: [PATCH] Fix testcontext map thread safety We modify the textcontext map form multiple tests running in parallel, so we must synchronize access to the map. Fixes #1571 Signed-off-by: Nir Soffer --- e2e/testcontext/testcontext.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/e2e/testcontext/testcontext.go b/e2e/testcontext/testcontext.go index 312303c22..37bcad52b 100644 --- a/e2e/testcontext/testcontext.go +++ b/e2e/testcontext/testcontext.go @@ -6,6 +6,7 @@ package testcontext import ( "fmt" "strings" + "sync" "github.com/ramendr/ramen/e2e/deployers" "github.com/ramendr/ramen/e2e/workloads" @@ -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) } @@ -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, "/")