From 96b4ae4767b359f92fbdf72f3fc2051d00c408be Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 5 Aug 2024 10:44:59 -0400 Subject: [PATCH] packer_test: move BuildSimplePlugin to plugin.go In terms of organisation, we keep functions that interact with plugins into its own file, therefore the function that build plugin versions should really be in plugin.go, not in suite.go. --- packer_test/lib/plugin.go | 43 +++++++++++++++++++++++++++++++++++++ packer_test/lib/suite.go | 45 --------------------------------------- 2 files changed, 43 insertions(+), 45 deletions(-) diff --git a/packer_test/lib/plugin.go b/packer_test/lib/plugin.go index 28afb1adf..a9530a5a0 100644 --- a/packer_test/lib/plugin.go +++ b/packer_test/lib/plugin.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "os/exec" "path/filepath" "runtime" "strings" @@ -104,6 +105,48 @@ func currentDir() (string, error) { return filepath.Dir(testDir), nil } +// BuildSimplePlugin creates a plugin that essentially does nothing. +// +// The plugin's code is contained in a subdirectory of this, and lets us +// change the attributes of the plugin binary itself, like the SDK version, +// the plugin's version, etc. +// +// The plugin is functional, and can be used to run builds with. +// There won't be anything substantial created though, its goal is only +// to validate the core functionality of Packer. +// +// The path to the plugin is returned, it won't be removed automatically +// 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) + if ok { + return path + } + + v := version.Must(version.NewSemver(versionString)) + + t.Logf("Building plugin in version %v", v) + + testDir, err := currentDir() + if err != nil { + t.Fatalf("failed to compile plugin binary: %s", err) + } + + testerPluginDir := filepath.Join(testDir, "plugin_tester") + outBin := filepath.Join(ts.pluginsDirectory, BinaryName(v)) + + compileCommand := exec.Command("go", "build", "-C", testerPluginDir, "-o", outBin, "-ldflags", LDFlags(v), ".") + logs, err := compileCommand.CombinedOutput() + if err != nil { + t.Fatalf("failed to compile plugin binary: %s\ncompiler logs: %s", err, logs) + } + + ts.StorePluginVersion(v.String(), outBin) + + return outBin +} + // MakePluginDir installs a list of plugins into a temporary directory and returns its path // // This can be set in the environment for a test through a function like t.SetEnv(), so diff --git a/packer_test/lib/suite.go b/packer_test/lib/suite.go index 2b8d10b7a..65cfdede8 100644 --- a/packer_test/lib/suite.go +++ b/packer_test/lib/suite.go @@ -3,12 +3,9 @@ package lib import ( "fmt" "os" - "os/exec" - "path/filepath" "sync" "testing" - "github.com/hashicorp/go-version" "github.com/stretchr/testify/suite" ) @@ -51,48 +48,6 @@ func (ts *PackerTestSuite) CompileTestPluginVersions(t *testing.T, versions ...s wg.Wait() } -// BuildSimplePlugin creates a plugin that essentially does nothing. -// -// The plugin's code is contained in a subdirectory of this, and lets us -// change the attributes of the plugin binary itself, like the SDK version, -// the plugin's version, etc. -// -// The plugin is functional, and can be used to run builds with. -// There won't be anything substantial created though, its goal is only -// to validate the core functionality of Packer. -// -// The path to the plugin is returned, it won't be removed automatically -// 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) - if ok { - return path - } - - v := version.Must(version.NewSemver(versionString)) - - t.Logf("Building plugin in version %v", v) - - testDir, err := currentDir() - if err != nil { - t.Fatalf("failed to compile plugin binary: %s", err) - } - - testerPluginDir := filepath.Join(testDir, "plugin_tester") - outBin := filepath.Join(ts.pluginsDirectory, BinaryName(v)) - - compileCommand := exec.Command("go", "build", "-C", testerPluginDir, "-o", outBin, "-ldflags", LDFlags(v), ".") - logs, err := compileCommand.CombinedOutput() - if err != nil { - t.Fatalf("failed to compile plugin binary: %s\ncompiler logs: %s", err, logs) - } - - ts.StorePluginVersion(v.String(), outBin) - - return outBin -} - // SkipNoAcc is a pre-condition that skips the test if the PACKER_ACC environment // variable is unset, or set to "0". //