Skip to content

Commit

Permalink
add lock on CopyTerraformFolderToTemp method to avoid concurrent erro…
Browse files Browse the repository at this point in the history
…r (One goroutine might try to copy a temp tfvars file generated by another gorouteine, then found it as been deleted by another goroutine)
  • Loading branch information
lonegunmanb committed Mar 2, 2023
1 parent 15811e9 commit c1ac612
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions e2etest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"path/filepath"
"sync"
"testing"
"time"

Expand All @@ -16,7 +15,7 @@ import (
"github.com/stretchr/testify/require"
)

var initLock = new(sync.Mutex)
var copyLock = &KeyedMutex{}

type TerraformOutput = map[string]interface{}

Expand Down Expand Up @@ -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()
Expand All @@ -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)
}

Expand Down

0 comments on commit c1ac612

Please sign in to comment.