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.
pull/13147/head
Lucas Bajolet 2 years ago committed by Lucas Bajolet
parent 2fe11fb967
commit 96b4ae4767

@ -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

@ -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".
//

Loading…
Cancel
Save