From c1ac612533b561f603b7debb695b47721129dbf9 Mon Sep 17 00:00:00 2001 From: hezijie Date: Thu, 2 Mar 2023 16:44:06 +0800 Subject: [PATCH] add lock on CopyTerraformFolderToTemp method to avoid concurrent error (One goroutine might try to copy a temp tfvars file generated by another gorouteine, then found it as been deleted by another goroutine) --- e2etest.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/e2etest.go b/e2etest.go index f982223..2e38297 100644 --- a/e2etest.go +++ b/e2etest.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "path/filepath" - "sync" "testing" "time" @@ -16,7 +15,7 @@ import ( "github.com/stretchr/testify/require" ) -var initLock = new(sync.Mutex) +var copyLock = &KeyedMutex{} type TerraformOutput = map[string]interface{} @@ -52,7 +51,7 @@ func initAndApplyAndIdempotentTest(t *testing.T, moduleRootPath string, exampleR testDir := filepath.Join(moduleRootPath, exampleRelativePath) logger.Log(t, fmt.Sprintf("===> Starting test for %s, since we're running tests in parallel, the test log will be buffered and output to stdout after the test was finished.", testDir)) - tmpDir := test_structure.CopyTerraformFolderToTemp(t, moduleRootPath, exampleRelativePath) + tmpDir := copyTerraformFolderToTemp(t, moduleRootPath, exampleRelativePath) option.TerraformDir = tmpDir l := executor.Logger() @@ -75,14 +74,19 @@ func initAndApplyAndIdempotentTest(t *testing.T, moduleRootPath string, exampleR } } +func copyTerraformFolderToTemp(t *testing.T, moduleRootPath string, exampleRelativePath string) string { + unlock := copyLock.Lock(exampleRelativePath) + defer unlock() + tmpDir := test_structure.CopyTerraformFolderToTemp(t, moduleRootPath, exampleRelativePath) + return tmpDir +} + func initAndApply(t terratest.TestingT, options *terraform.Options) string { tfInit(t, options) return terraform.Apply(t, options) } func tfInit(t terratest.TestingT, options *terraform.Options) { - initLock.Lock() - defer initLock.Unlock() terraform.Init(t, options) }