From 393f2e925bd7e20d62353b74abbb4ce1856c32b5 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Wed, 14 Aug 2024 16:04:21 -0400 Subject: [PATCH] packer_test: replace compiledPlugins by a sync.Map The compiledPlugins map with a mutex was essentially a glorified sync.Map, and therefore did not need to be defined as such. Instead, this commit replaces its uses by a normal sync.Map, and keeps it in the base test suite. --- packer_test/lib/plugin.go | 37 +++++-------------------------------- packer_test/lib/suite.go | 6 ++++-- 2 files changed, 9 insertions(+), 34 deletions(-) diff --git a/packer_test/lib/plugin.go b/packer_test/lib/plugin.go index 85ec51c39..bdc6a7ac3 100644 --- a/packer_test/lib/plugin.go +++ b/packer_test/lib/plugin.go @@ -7,39 +7,12 @@ import ( "path/filepath" "runtime" "strings" - "sync" "testing" "github.com/hashicorp/go-version" "github.com/hashicorp/packer-plugin-sdk/plugin" ) -type compiledPlugins struct { - pluginVersions map[string]string - mutex sync.Mutex -} - -func (ts *PackerTestSuite) StorePluginVersion(pluginVersion, path string) { - ts.compiledPlugins.mutex.Lock() - defer ts.compiledPlugins.mutex.Unlock() - if ts.compiledPlugins.pluginVersions == nil { - ts.compiledPlugins.pluginVersions = map[string]string{} - } - - ts.compiledPlugins.pluginVersions[pluginVersion] = path -} - -func (ts *PackerTestSuite) LoadPluginVersion(pluginVersion string) (string, bool) { - ts.compiledPlugins.mutex.Lock() - defer ts.compiledPlugins.mutex.Unlock() - if ts.compiledPlugins.pluginVersions == nil { - ts.compiledPlugins.pluginVersions = map[string]string{} - } - - path, ok := ts.compiledPlugins.pluginVersions[pluginVersion] - return path, ok -} - // LDFlags compiles the ldflags for the plugin to compile based on the information provided. func LDFlags(version *version.Version) string { pluginPackage := "github.com/hashicorp/packer-plugin-tester" @@ -102,9 +75,9 @@ func ExpectedInstalledName(versionStr string) string { // though, deletion is the caller's responsibility. func (ts *PackerTestSuite) BuildSimplePlugin(versionString string, t *testing.T) string { // Only build plugin binary if not already done beforehand - path, ok := ts.LoadPluginVersion(versionString) + path, ok := ts.compiledPlugins.Load(versionString) if ok { - return path + return path.(string) } v := version.Must(version.NewSemver(versionString)) @@ -125,7 +98,7 @@ func (ts *PackerTestSuite) BuildSimplePlugin(versionString string, t *testing.T) t.Fatalf("failed to compile plugin binary: %s\ncompiler logs: %s", err, logs) } - ts.StorePluginVersion(v.String(), outBin) + ts.compiledPlugins.Store(v.String(), outBin) return outBin } @@ -160,11 +133,11 @@ func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDi } for _, pluginVersion := range pluginVersions { - path, ok := ts.LoadPluginVersion(pluginVersion) + path, ok := ts.compiledPlugins.Load(pluginVersion) if !ok { err = fmt.Errorf("failed to get path to version %q, was it compiled?", pluginVersion) } - cmd := ts.PackerCommand().SetArgs("plugins", "install", "--path", path, "github.com/hashicorp/tester").AddEnv("PACKER_PLUGIN_PATH", pluginTempDir) + cmd := ts.PackerCommand().SetArgs("plugins", "install", "--path", path.(string), "github.com/hashicorp/tester").AddEnv("PACKER_PLUGIN_PATH", pluginTempDir) cmd.Assert(MustSucceed()) out, stderr, cmdErr := cmd.Run() if cmdErr != nil { diff --git a/packer_test/lib/suite.go b/packer_test/lib/suite.go index 952a638e6..ed2d10969 100644 --- a/packer_test/lib/suite.go +++ b/packer_test/lib/suite.go @@ -27,7 +27,7 @@ type PackerTestSuite struct { // we may have multiple suites that exist, each with its own repo of // plugins compiled for the purposes of the test, so as they all run // within the same process space, they should be separate instances. - compiledPlugins compiledPlugins + compiledPlugins sync.Map } func (ts *PackerTestSuite) buildPluginVersion(waitgroup *sync.WaitGroup, versionString string, t *testing.T) { @@ -63,7 +63,9 @@ func (ts *PackerTestSuite) SkipNoAcc() { } func InitBaseSuite(t *testing.T) (*PackerTestSuite, func()) { - ts := &PackerTestSuite{} + ts := &PackerTestSuite{ + compiledPlugins: sync.Map{}, + } tempDir, err := os.MkdirTemp("", "packer-core-acc-test-") if err != nil {