Skip to content

Commit

Permalink
Fix: race conditions during run (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber authored Jun 6, 2022
1 parent 3381f56 commit 44faf46
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
33 changes: 20 additions & 13 deletions internal/tfschema/tfschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"strings"
"sync"

"github.com/env0/terratag/internal/providers"
"github.com/env0/terratag/internal/tagging"
Expand All @@ -16,6 +17,7 @@ import (
)

var providerToClientMap = map[string]tfschema.Client{}
var providerToClientMapLock sync.Mutex

var customSupportedProviderNames = [...]string{"google-beta"}

Expand Down Expand Up @@ -83,6 +85,14 @@ func detectProviderName(resource hclwrite.Block) (string, error) {
}

func getClient(providerName string, dir string) (tfschema.Client, error) {
providerToClientMapLock.Lock()
defer providerToClientMapLock.Unlock()

client, exists := providerToClientMap[providerName]
if exists {
return client, nil
}

logger := hclog.New(&hclog.LoggerOptions{
Name: "plugin",
Level: hclog.Trace,
Expand All @@ -92,19 +102,16 @@ func getClient(providerName string, dir string) (tfschema.Client, error) {
// weird to need to bypass the issue by assigning the default
// output ¯\_(ツ)_/¯
})
client, exists := providerToClientMap[providerName]
if exists {
return client, nil
} else {
newClient, err := tfschema.NewClient(providerName, tfschema.Option{
RootDir: dir,
Logger: logger,
})
if err != nil {
return nil, err
}

providerToClientMap[providerName] = newClient
return newClient, nil
newClient, err := tfschema.NewClient(providerName, tfschema.Option{
RootDir: dir,
Logger: logger,
})
if err != nil {
return nil, err
}

providerToClientMap[providerName] = newClient

return newClient, nil
}
6 changes: 5 additions & 1 deletion terratag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"

"github.com/bmatcuk/doublestar"
Expand All @@ -28,6 +29,7 @@ var args = []string{
}
var testsDir = "test/tests"
var fixtureDir = "test/fixture"
var osArgsLock sync.Mutex

type TestCase struct {
suite string
Expand Down Expand Up @@ -233,17 +235,19 @@ func run_terratag(entryDir string, filter string) (err interface{}) {
err = innerErr
}
}()
osArgsLock.Lock()
if filter == "" {
os.Args = append(args, "-dir="+entryDir)
} else {
os.Args = append(args, "-dir="+entryDir, "-filter="+filter)
}
args, isMissingArg := cli.InitArgs()
os.Args = cleanArgs
osArgsLock.Unlock()
if isMissingArg {
return errors.New("Missing arg")
}
Terratag(args)
os.Args = cleanArgs

return nil
}
Expand Down

0 comments on commit 44faf46

Please sign in to comment.