diff --git a/packer_test/local_eval_test.go b/packer_test/core_tests/local_eval_test.go similarity index 58% rename from packer_test/local_eval_test.go rename to packer_test/core_tests/local_eval_test.go index dec5b1fdb49..dd450d75095 100644 --- a/packer_test/local_eval_test.go +++ b/packer_test/core_tests/local_eval_test.go @@ -1,8 +1,12 @@ -package packer_test +package core_test -import "fmt" +import ( + "fmt" -func (ts *PackerTestSuite) TestEvalLocalsOrder() { + "github.com/hashicorp/packer/packer_test/lib" +) + +func (ts *PackerCoreTestSuite) TestEvalLocalsOrder() { ts.SkipNoAcc() pluginDir, cleanup := ts.MakePluginDir() @@ -12,10 +16,11 @@ func (ts *PackerTestSuite) TestEvalLocalsOrder() { Runs(10). Stdin("local.test_local\n"). SetArgs("console", "./templates/locals_no_order.pkr.hcl"). - Assert(MustSucceed(), Grep("\\[\\]", grepStdout, grepInvert)) + Assert(lib.MustSucceed(), + lib.Grep("\\[\\]", lib.GrepStdout, lib.GrepInvert)) } -func (ts *PackerTestSuite) TestLocalDuplicates() { +func (ts *PackerCoreTestSuite) TestLocalDuplicates() { pluginDir, cleanup := ts.MakePluginDir() defer cleanup() @@ -23,9 +28,9 @@ func (ts *PackerTestSuite) TestLocalDuplicates() { ts.Run(fmt.Sprintf("duplicate local detection with %s command - expect error", cmd), func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs(cmd, "./templates/locals_duplicate.pkr.hcl"). - Assert(MustFail(), - Grep("Duplicate local definition"), - Grep("Local variable \"test\" is defined twice")) + Assert(lib.MustFail(), + lib.Grep("Duplicate local definition"), + lib.Grep("Local variable \"test\" is defined twice")) }) } } diff --git a/packer_test/core_tests/suite_test.go b/packer_test/core_tests/suite_test.go new file mode 100644 index 00000000000..0d2e39367f3 --- /dev/null +++ b/packer_test/core_tests/suite_test.go @@ -0,0 +1,23 @@ +package core_test + +import ( + "testing" + + "github.com/hashicorp/packer/packer_test/lib" + "github.com/stretchr/testify/suite" +) + +type PackerCoreTestSuite struct { + *lib.PackerTestSuite +} + +func Test_PackerPluginSuite(t *testing.T) { + baseSuite, cleanup := lib.PackerCoreSuite(t) + defer cleanup() + + ts := &PackerCoreTestSuite{ + baseSuite, + } + + suite.Run(t, ts) +} diff --git a/packer_test/templates/locals_duplicate.pkr.hcl b/packer_test/core_tests/templates/locals_duplicate.pkr.hcl similarity index 100% rename from packer_test/templates/locals_duplicate.pkr.hcl rename to packer_test/core_tests/templates/locals_duplicate.pkr.hcl diff --git a/packer_test/templates/locals_no_order.pkr.hcl b/packer_test/core_tests/templates/locals_no_order.pkr.hcl similarity index 100% rename from packer_test/templates/locals_no_order.pkr.hcl rename to packer_test/core_tests/templates/locals_no_order.pkr.hcl diff --git a/packer_test/base_test.go b/packer_test/lib/base.go similarity index 92% rename from packer_test/base_test.go rename to packer_test/lib/base.go index 4fe675d3158..a54c43a1c48 100644 --- a/packer_test/base_test.go +++ b/packer_test/lib/base.go @@ -1,4 +1,4 @@ -package packer_test +package lib import ( "fmt" @@ -19,7 +19,7 @@ func BuildTestPacker(t *testing.T) (string, error) { return "", fmt.Errorf("failed to compile packer binary: %s", err) } - packerCoreDir := filepath.Dir(testDir) + packerCoreDir := filepath.Dir(filepath.Dir(testDir)) outBin := filepath.Join(os.TempDir(), fmt.Sprintf("packer_core-%d", rand.Int())) if runtime.GOOS == "windows" { diff --git a/packer_test/commands_test.go b/packer_test/lib/commands.go similarity index 99% rename from packer_test/commands_test.go rename to packer_test/lib/commands.go index b8d7bc5849b..6e90bd62580 100644 --- a/packer_test/commands_test.go +++ b/packer_test/lib/commands.go @@ -1,4 +1,4 @@ -package packer_test +package lib import ( "fmt" diff --git a/packer_test/lib/fs.go b/packer_test/lib/fs.go new file mode 100644 index 00000000000..ce497601d6f --- /dev/null +++ b/packer_test/lib/fs.go @@ -0,0 +1,127 @@ +package lib + +import ( + "crypto/sha256" + "fmt" + "io" + "os" + "path/filepath" + "runtime" + "testing" +) + +// CopyFile essentially replicates the `cp` command, for a file only. +// +// # Permissions are copied over from the source to destination +// +// The function detects if destination is a directory or a file (existent or not). +// +// If this is the former, we append the source file's basename to the +// directory and create the file from that inferred path. +func CopyFile(t *testing.T, dest, src string) { + st, err := os.Stat(src) + if err != nil { + t.Fatalf("failed to stat origin file %q: %s", src, err) + } + + // If the stat call fails, we assume dest is the destination file. + dstStat, err := os.Stat(dest) + if err == nil && dstStat.IsDir() { + dest = filepath.Join(dest, filepath.Base(src)) + } + + destFD, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, st.Mode().Perm()) + if err != nil { + t.Fatalf("failed to create cp destination file %q: %s", dest, err) + } + defer destFD.Close() + + srcFD, err := os.Open(src) + if err != nil { + t.Fatalf("failed to open source file to copy: %s", err) + } + defer srcFD.Close() + + _, err = io.Copy(destFD, srcFD) + if err != nil { + t.Fatalf("failed to copy from %q -> %q: %s", src, dest, err) + } +} + +// WriteFile writes `content` to a file `dest` +// +// The default permissions of that file is 0644 +func WriteFile(t *testing.T, dest string, content string) { + outFile, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) + if err != nil { + t.Fatalf("failed to open/create %q: %s", dest, err) + } + defer outFile.Close() + + _, err = fmt.Fprintf(outFile, content) + if err != nil { + t.Fatalf("failed to write to file %q: %s", dest, err) + } +} + +// TempWorkdir creates a working directory for a Packer test with the list of files +// given as input. +// +// The files should either have a path relative to the test that invokes it, or should +// be absolute. +// Each file will be copied to the root of the workdir being created. +// +// If any file cannot be found, this function will fail +func TempWorkdir(t *testing.T, files ...string) (string, func()) { + var err error + tempDir, err := os.MkdirTemp("", "packer-test-workdir-") + if err != nil { + t.Fatalf("failed to create temporary working directory: %s", err) + } + + defer func() { + if err != nil { + os.RemoveAll(tempDir) + t.Errorf("failed to create temporary workdir: %s", err) + } + }() + + for _, file := range files { + CopyFile(t, tempDir, file) + } + + return tempDir, func() { + err := os.RemoveAll(tempDir) + if err != nil { + t.Logf("failed to remove temporary workdir %q: %s. This will need manual action.", tempDir, err) + } + } +} + +// SHA256Sum computes the SHA256 digest for an input file +// +// The digest is returned as a hexstring +func SHA256Sum(t *testing.T, file string) string { + fl, err := os.ReadFile(file) + if err != nil { + t.Fatalf("failed to compute sha256sum for %q: %s", file, err) + } + sha := sha256.New() + sha.Write(fl) + return fmt.Sprintf("%x", sha.Sum([]byte{})) +} + +// currentDir returns the directory in which the current file is located. +// +// Since we're in tests it's reliable as they're supposed to run on the same +// machine the binary's compiled from, but goes to say it's not meant for use +// in distributed binaries. +func currentDir() (string, error) { + // pc uintptr, file string, line int, ok bool + _, testDir, _, ok := runtime.Caller(0) + if !ok { + return "", fmt.Errorf("couldn't get the location of the test suite file") + } + + return filepath.Dir(testDir), nil +} diff --git a/packer_test/gadgets_test.go b/packer_test/lib/gadgets.go similarity index 70% rename from packer_test/gadgets_test.go rename to packer_test/lib/gadgets.go index 3ddd29e55a3..d1b16d254ab 100644 --- a/packer_test/gadgets_test.go +++ b/packer_test/lib/gadgets.go @@ -1,10 +1,13 @@ -package packer_test +package lib import ( "fmt" "reflect" "strings" "testing" + + "github.com/hashicorp/go-multierror" + "github.com/hashicorp/go-version" ) type Stream int @@ -86,21 +89,21 @@ func (_ mustFail) Check(stdout, stderr string, err error) error { return nil } -type grepOpts int +type GrepOpts int const ( // Invert the check, i.e. by default an empty grep fails, if this is set, a non-empty grep fails - grepInvert grepOpts = iota + GrepInvert GrepOpts = iota // Only grep stderr - grepStderr + GrepStderr // Only grep stdout - grepStdout + GrepStdout ) // Grep returns a checker that performs a regexp match on the command's output and returns an error if it failed // // Note: by default both streams will be checked by the grep -func Grep(expression string, opts ...grepOpts) Checker { +func Grep(expression string, opts ...GrepOpts) Checker { pc := PipeChecker{ name: fmt.Sprintf("command | grep -E %q", expression), stream: BothStreams, @@ -111,22 +114,77 @@ func Grep(expression string, opts ...grepOpts) Checker { } for _, opt := range opts { switch opt { - case grepInvert: + case GrepInvert: pc.check = ExpectEmptyInput() - case grepStderr: + case GrepStderr: pc.stream = OnlyStderr - case grepStdout: + case GrepStdout: pc.stream = OnlyStdout } } return pc } -type Dump struct { +type PluginVersionTuple struct { + Source string + Version *version.Version +} + +func NewPluginVersionTuple(src, pluginVersion string) PluginVersionTuple { + ver := version.Must(version.NewVersion(pluginVersion)) + return PluginVersionTuple{ + Source: src, + Version: ver, + } +} + +type pluginsUsed struct { + invert bool + plugins []PluginVersionTuple +} + +func (pu pluginsUsed) Check(stdout, stderr string, _ error) error { + var opts []GrepOpts + if !pu.invert { + opts = append(opts, GrepInvert) + } + + var retErr error + + for _, pvt := range pu.plugins { + // `error` is ignored for Grep, so we can pass in nil + err := Grep( + fmt.Sprintf("%s_v%s[^:]+\\\\s*plugin process exited", pvt.Source, pvt.Version.Core()), + opts..., + ).Check(stdout, stderr, nil) + if err != nil { + retErr = multierror.Append(retErr, err) + } + } + + return retErr +} + +// PluginsUsed is a glorified `Grep` checker that looks for a bunch of plugins +// used from the logs of a packer build or packer validate. +// +// Each tuple passed as parameter is looked for in the logs using Grep +func PluginsUsed(invert bool, plugins ...PluginVersionTuple) Checker { + return pluginsUsed{ + invert: invert, + plugins: plugins, + } +} + +func Dump(t *testing.T) Checker { + return &dump{t} +} + +type dump struct { t *testing.T } -func (d Dump) Check(stdout, stderr string, err error) error { +func (d dump) Check(stdout, stderr string, err error) error { d.t.Logf("Dumping command result.") d.t.Logf("Stdout: %s", stdout) d.t.Logf("stderr: %s", stderr) @@ -165,6 +223,6 @@ func (c CustomCheck) Name() string { // returned pipe checker. func LineCountCheck(lines int) *PipeChecker { return MkPipeCheck(fmt.Sprintf("line count (%d)", lines), LineCount()). - SetTester(IntCompare(eq, lines)). + SetTester(IntCompare(Eq, lines)). SetStream(OnlyStdout) } diff --git a/packer_test/pipe_checker_test.go b/packer_test/lib/pipe_checker.go similarity index 95% rename from packer_test/pipe_checker_test.go rename to packer_test/lib/pipe_checker.go index b3b415349b9..1279b5b9ca6 100644 --- a/packer_test/pipe_checker_test.go +++ b/packer_test/lib/pipe_checker.go @@ -1,4 +1,4 @@ -package packer_test +package lib import ( "fmt" @@ -101,30 +101,30 @@ func ExpectEmptyInput() Tester { }) } -type op int +type Op int const ( - eq op = iota - ne - gt - ge - lt - le + Eq Op = iota + Ne + Gt + Ge + Lt + Le ) -func (op op) String() string { +func (op Op) String() string { switch op { - case eq: + case Eq: return "==" - case ne: + case Ne: return "!=" - case gt: + case Gt: return ">" - case ge: + case Ge: return ">=" - case lt: + case Lt: return "<" - case le: + case Le: return "<=" } @@ -134,7 +134,7 @@ func (op op) String() string { // IntCompare reads the input from the pipeline and compares it to a value using `op` // // If the input is not an int, this fails. -func IntCompare(op op, value int) Tester { +func IntCompare(op Op, value int) Tester { return CustomTester(func(in string) error { n, err := strconv.Atoi(strings.TrimSpace(in)) if err != nil { @@ -143,17 +143,17 @@ func IntCompare(op op, value int) Tester { var result bool switch op { - case eq: + case Eq: result = n == value - case ne: + case Ne: result = n != value - case gt: + case Gt: result = n > value - case ge: + case Ge: result = n >= value - case lt: + case Lt: result = n < value - case le: + case Le: result = n <= value default: panic(fmt.Sprintf("Unsupported operator %d, make sure the operation is implemented for IntCompare", op)) diff --git a/packer_test/plugin_test.go b/packer_test/lib/plugin.go similarity index 52% rename from packer_test/plugin_test.go rename to packer_test/lib/plugin.go index a2e1b4aff24..85ec51c39b4 100644 --- a/packer_test/plugin_test.go +++ b/packer_test/lib/plugin.go @@ -1,9 +1,7 @@ -package packer_test +package lib import ( - "crypto/sha256" "fmt" - "io" "os" "os/exec" "path/filepath" @@ -16,44 +14,30 @@ import ( "github.com/hashicorp/packer-plugin-sdk/plugin" ) -var compiledPlugins = struct { +type compiledPlugins struct { pluginVersions map[string]string - RWMutex sync.RWMutex -}{ - pluginVersions: map[string]string{}, + mutex sync.Mutex } -func StorePluginVersion(pluginVersion, path string) { - compiledPlugins.RWMutex.Lock() - defer compiledPlugins.RWMutex.Unlock() - compiledPlugins.pluginVersions[pluginVersion] = path -} - -func LoadPluginVersion(pluginVersion string) (string, bool) { - compiledPlugins.RWMutex.RLock() - defer compiledPlugins.RWMutex.RUnlock() +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{} + } - path, ok := compiledPlugins.pluginVersions[pluginVersion] - return path, ok + ts.compiledPlugins.pluginVersions[pluginVersion] = path } -var tempPluginBinaryPath = struct { - path string - once sync.Once -}{} - -// PluginBinaryDir returns the path to the directory where temporary binaries will be compiled -func PluginBinaryDir() string { - tempPluginBinaryPath.once.Do(func() { - tempDir, err := os.MkdirTemp("", "packer-core-acc-test-") - if err != nil { - panic(fmt.Sprintf("failed to create temporary directory for compiled plugins: %s", err)) - } - - tempPluginBinaryPath.path = tempDir - }) +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{} + } - return tempPluginBinaryPath.path + path, ok := ts.compiledPlugins.pluginVersions[pluginVersion] + return path, ok } // LDFlags compiles the ldflags for the plugin to compile based on the information provided. @@ -116,9 +100,9 @@ func ExpectedInstalledName(versionStr string) string { // // The path to the plugin is returned, it won't be removed automatically // though, deletion is the caller's responsibility. -func BuildSimplePlugin(versionString string, t *testing.T) string { +func (ts *PackerTestSuite) BuildSimplePlugin(versionString string, t *testing.T) string { // Only build plugin binary if not already done beforehand - path, ok := LoadPluginVersion(versionString) + path, ok := ts.LoadPluginVersion(versionString) if ok { return path } @@ -133,7 +117,7 @@ func BuildSimplePlugin(versionString string, t *testing.T) string { } testerPluginDir := filepath.Join(testDir, "plugin_tester") - outBin := filepath.Join(PluginBinaryDir(), BinaryName(v)) + outBin := filepath.Join(ts.pluginsDirectory, BinaryName(v)) compileCommand := exec.Command("go", "build", "-C", testerPluginDir, "-o", outBin, "-ldflags", LDFlags(v), ".") logs, err := compileCommand.CombinedOutput() @@ -141,26 +125,11 @@ func BuildSimplePlugin(versionString string, t *testing.T) string { t.Fatalf("failed to compile plugin binary: %s\ncompiler logs: %s", err, logs) } - StorePluginVersion(v.String(), outBin) + ts.StorePluginVersion(v.String(), outBin) return outBin } -// currentDir returns the directory in which the current file is located. -// -// Since we're in tests it's reliable as they're supposed to run on the same -// machine the binary's compiled from, but goes to say it's not meant for use -// in distributed binaries. -func currentDir() (string, error) { - // pc uintptr, file string, line int, ok bool - _, testDir, _, ok := runtime.Caller(0) - if !ok { - return "", fmt.Errorf("couldn't get the location of the test suite file") - } - - return filepath.Dir(testDir), nil -} - // 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 @@ -171,7 +140,7 @@ func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDi t := ts.T() for _, ver := range pluginVersions { - BuildSimplePlugin(ver, t) + ts.BuildSimplePlugin(ver, t) } var err error @@ -191,7 +160,7 @@ func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDi } for _, pluginVersion := range pluginVersions { - path, ok := LoadPluginVersion(pluginVersion) + path, ok := ts.LoadPluginVersion(pluginVersion) if !ok { err = fmt.Errorf("failed to get path to version %q, was it compiled?", pluginVersion) } @@ -212,107 +181,6 @@ func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDi } } -// CopyFile essentially replicates the `cp` command, for a file only. -// -// # Permissions are copied over from the source to destination -// -// The function detects if destination is a directory or a file (existent or not). -// -// If this is the former, we append the source file's basename to the -// directory and create the file from that inferred path. -func CopyFile(t *testing.T, dest, src string) { - st, err := os.Stat(src) - if err != nil { - t.Fatalf("failed to stat origin file %q: %s", src, err) - } - - // If the stat call fails, we assume dest is the destination file. - dstStat, err := os.Stat(dest) - if err == nil && dstStat.IsDir() { - dest = filepath.Join(dest, filepath.Base(src)) - } - - destFD, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, st.Mode().Perm()) - if err != nil { - t.Fatalf("failed to create cp destination file %q: %s", dest, err) - } - defer destFD.Close() - - srcFD, err := os.Open(src) - if err != nil { - t.Fatalf("failed to open source file to copy: %s", err) - } - defer srcFD.Close() - - _, err = io.Copy(destFD, srcFD) - if err != nil { - t.Fatalf("failed to copy from %q -> %q: %s", src, dest, err) - } -} - -// WriteFile writes `content` to a file `dest` -// -// The default permissions of that file is 0644 -func WriteFile(t *testing.T, dest string, content string) { - outFile, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) - if err != nil { - t.Fatalf("failed to open/create %q: %s", dest, err) - } - defer outFile.Close() - - _, err = fmt.Fprintf(outFile, content) - if err != nil { - t.Fatalf("failed to write to file %q: %s", dest, err) - } -} - -// TempWorkdir creates a working directory for a Packer test with the list of files -// given as input. -// -// The files should either have a path relative to the test that invokes it, or should -// be absolute. -// Each file will be copied to the root of the workdir being created. -// -// If any file cannot be found, this function will fail -func TempWorkdir(t *testing.T, files ...string) (string, func()) { - var err error - tempDir, err := os.MkdirTemp("", "packer-test-workdir-") - if err != nil { - t.Fatalf("failed to create temporary working directory: %s", err) - } - - defer func() { - if err != nil { - os.RemoveAll(tempDir) - t.Errorf("failed to create temporary workdir: %s", err) - } - }() - - for _, file := range files { - CopyFile(t, tempDir, file) - } - - return tempDir, func() { - err := os.RemoveAll(tempDir) - if err != nil { - t.Logf("failed to remove temporary workdir %q: %s. This will need manual action.", tempDir, err) - } - } -} - -// SHA256Sum computes the SHA256 digest for an input file -// -// The digest is returned as a hexstring -func SHA256Sum(t *testing.T, file string) string { - fl, err := os.ReadFile(file) - if err != nil { - t.Fatalf("failed to compute sha256sum for %q: %s", file, err) - } - sha := sha256.New() - sha.Write(fl) - return fmt.Sprintf("%x", sha.Sum([]byte{})) -} - // ManualPluginInstall emulates how Packer installs plugins with `packer plugins install` // // This is used for some tests if we want to install a plugin that cannot be installed diff --git a/packer_test/plugin_tester/LICENSE b/packer_test/lib/plugin_tester/LICENSE similarity index 100% rename from packer_test/plugin_tester/LICENSE rename to packer_test/lib/plugin_tester/LICENSE diff --git a/packer_test/plugin_tester/README.md b/packer_test/lib/plugin_tester/README.md similarity index 100% rename from packer_test/plugin_tester/README.md rename to packer_test/lib/plugin_tester/README.md diff --git a/packer_test/plugin_tester/builder/dynamic/artifact.go b/packer_test/lib/plugin_tester/builder/dynamic/artifact.go similarity index 100% rename from packer_test/plugin_tester/builder/dynamic/artifact.go rename to packer_test/lib/plugin_tester/builder/dynamic/artifact.go diff --git a/packer_test/plugin_tester/builder/dynamic/builder.go b/packer_test/lib/plugin_tester/builder/dynamic/builder.go similarity index 100% rename from packer_test/plugin_tester/builder/dynamic/builder.go rename to packer_test/lib/plugin_tester/builder/dynamic/builder.go diff --git a/packer_test/plugin_tester/builder/dynamic/builder.hcl2spec.go b/packer_test/lib/plugin_tester/builder/dynamic/builder.hcl2spec.go similarity index 100% rename from packer_test/plugin_tester/builder/dynamic/builder.hcl2spec.go rename to packer_test/lib/plugin_tester/builder/dynamic/builder.hcl2spec.go diff --git a/packer_test/plugin_tester/builder/dynamic/builder_acc_test.go b/packer_test/lib/plugin_tester/builder/dynamic/builder_acc_test.go similarity index 100% rename from packer_test/plugin_tester/builder/dynamic/builder_acc_test.go rename to packer_test/lib/plugin_tester/builder/dynamic/builder_acc_test.go diff --git a/packer_test/plugin_tester/builder/dynamic/step_say_config.go b/packer_test/lib/plugin_tester/builder/dynamic/step_say_config.go similarity index 100% rename from packer_test/plugin_tester/builder/dynamic/step_say_config.go rename to packer_test/lib/plugin_tester/builder/dynamic/step_say_config.go diff --git a/packer_test/plugin_tester/builder/dynamic/test-fixtures/template.pkr.hcl b/packer_test/lib/plugin_tester/builder/dynamic/test-fixtures/template.pkr.hcl similarity index 100% rename from packer_test/plugin_tester/builder/dynamic/test-fixtures/template.pkr.hcl rename to packer_test/lib/plugin_tester/builder/dynamic/test-fixtures/template.pkr.hcl diff --git a/packer_test/plugin_tester/datasource/dynamic/data.go b/packer_test/lib/plugin_tester/datasource/dynamic/data.go similarity index 100% rename from packer_test/plugin_tester/datasource/dynamic/data.go rename to packer_test/lib/plugin_tester/datasource/dynamic/data.go diff --git a/packer_test/plugin_tester/datasource/dynamic/data.hcl2spec.go b/packer_test/lib/plugin_tester/datasource/dynamic/data.hcl2spec.go similarity index 100% rename from packer_test/plugin_tester/datasource/dynamic/data.hcl2spec.go rename to packer_test/lib/plugin_tester/datasource/dynamic/data.hcl2spec.go diff --git a/packer_test/plugin_tester/datasource/dynamic/data_acc_test.go b/packer_test/lib/plugin_tester/datasource/dynamic/data_acc_test.go similarity index 100% rename from packer_test/plugin_tester/datasource/dynamic/data_acc_test.go rename to packer_test/lib/plugin_tester/datasource/dynamic/data_acc_test.go diff --git a/packer_test/plugin_tester/datasource/dynamic/test-fixtures/template.pkr.hcl b/packer_test/lib/plugin_tester/datasource/dynamic/test-fixtures/template.pkr.hcl similarity index 100% rename from packer_test/plugin_tester/datasource/dynamic/test-fixtures/template.pkr.hcl rename to packer_test/lib/plugin_tester/datasource/dynamic/test-fixtures/template.pkr.hcl diff --git a/packer_test/plugin_tester/datasource/parrot/data.go b/packer_test/lib/plugin_tester/datasource/parrot/data.go similarity index 100% rename from packer_test/plugin_tester/datasource/parrot/data.go rename to packer_test/lib/plugin_tester/datasource/parrot/data.go diff --git a/packer_test/plugin_tester/datasource/parrot/data.hcl2spec.go b/packer_test/lib/plugin_tester/datasource/parrot/data.hcl2spec.go similarity index 100% rename from packer_test/plugin_tester/datasource/parrot/data.hcl2spec.go rename to packer_test/lib/plugin_tester/datasource/parrot/data.hcl2spec.go diff --git a/packer_test/plugin_tester/datasource/sleeper/data.go b/packer_test/lib/plugin_tester/datasource/sleeper/data.go similarity index 100% rename from packer_test/plugin_tester/datasource/sleeper/data.go rename to packer_test/lib/plugin_tester/datasource/sleeper/data.go diff --git a/packer_test/plugin_tester/datasource/sleeper/data.hcl2spec.go b/packer_test/lib/plugin_tester/datasource/sleeper/data.hcl2spec.go similarity index 100% rename from packer_test/plugin_tester/datasource/sleeper/data.hcl2spec.go rename to packer_test/lib/plugin_tester/datasource/sleeper/data.hcl2spec.go diff --git a/packer_test/plugin_tester/docs/README.md b/packer_test/lib/plugin_tester/docs/README.md similarity index 100% rename from packer_test/plugin_tester/docs/README.md rename to packer_test/lib/plugin_tester/docs/README.md diff --git a/packer_test/plugin_tester/docs/builders/builder-name.mdx b/packer_test/lib/plugin_tester/docs/builders/builder-name.mdx similarity index 100% rename from packer_test/plugin_tester/docs/builders/builder-name.mdx rename to packer_test/lib/plugin_tester/docs/builders/builder-name.mdx diff --git a/packer_test/plugin_tester/docs/datasources/datasource-name.mdx b/packer_test/lib/plugin_tester/docs/datasources/datasource-name.mdx similarity index 100% rename from packer_test/plugin_tester/docs/datasources/datasource-name.mdx rename to packer_test/lib/plugin_tester/docs/datasources/datasource-name.mdx diff --git a/packer_test/plugin_tester/docs/post-processors/postprocessor-name.mdx b/packer_test/lib/plugin_tester/docs/post-processors/postprocessor-name.mdx similarity index 100% rename from packer_test/plugin_tester/docs/post-processors/postprocessor-name.mdx rename to packer_test/lib/plugin_tester/docs/post-processors/postprocessor-name.mdx diff --git a/packer_test/plugin_tester/docs/provisioners/provisioner-name.mdx b/packer_test/lib/plugin_tester/docs/provisioners/provisioner-name.mdx similarity index 100% rename from packer_test/plugin_tester/docs/provisioners/provisioner-name.mdx rename to packer_test/lib/plugin_tester/docs/provisioners/provisioner-name.mdx diff --git a/packer_test/plugin_tester/example/README.md b/packer_test/lib/plugin_tester/example/README.md similarity index 100% rename from packer_test/plugin_tester/example/README.md rename to packer_test/lib/plugin_tester/example/README.md diff --git a/packer_test/plugin_tester/example/build.pkr.hcl b/packer_test/lib/plugin_tester/example/build.pkr.hcl similarity index 100% rename from packer_test/plugin_tester/example/build.pkr.hcl rename to packer_test/lib/plugin_tester/example/build.pkr.hcl diff --git a/packer_test/plugin_tester/example/data.pkr.hcl b/packer_test/lib/plugin_tester/example/data.pkr.hcl similarity index 100% rename from packer_test/plugin_tester/example/data.pkr.hcl rename to packer_test/lib/plugin_tester/example/data.pkr.hcl diff --git a/packer_test/plugin_tester/example/variables.pkr.hcl b/packer_test/lib/plugin_tester/example/variables.pkr.hcl similarity index 100% rename from packer_test/plugin_tester/example/variables.pkr.hcl rename to packer_test/lib/plugin_tester/example/variables.pkr.hcl diff --git a/packer_test/plugin_tester/go.mod b/packer_test/lib/plugin_tester/go.mod similarity index 100% rename from packer_test/plugin_tester/go.mod rename to packer_test/lib/plugin_tester/go.mod diff --git a/packer_test/plugin_tester/go.sum b/packer_test/lib/plugin_tester/go.sum similarity index 100% rename from packer_test/plugin_tester/go.sum rename to packer_test/lib/plugin_tester/go.sum diff --git a/packer_test/plugin_tester/main.go b/packer_test/lib/plugin_tester/main.go similarity index 100% rename from packer_test/plugin_tester/main.go rename to packer_test/lib/plugin_tester/main.go diff --git a/packer_test/plugin_tester/post-processor/dynamic/post-processor.go b/packer_test/lib/plugin_tester/post-processor/dynamic/post-processor.go similarity index 100% rename from packer_test/plugin_tester/post-processor/dynamic/post-processor.go rename to packer_test/lib/plugin_tester/post-processor/dynamic/post-processor.go diff --git a/packer_test/plugin_tester/post-processor/dynamic/post-processor.hcl2spec.go b/packer_test/lib/plugin_tester/post-processor/dynamic/post-processor.hcl2spec.go similarity index 100% rename from packer_test/plugin_tester/post-processor/dynamic/post-processor.hcl2spec.go rename to packer_test/lib/plugin_tester/post-processor/dynamic/post-processor.hcl2spec.go diff --git a/packer_test/plugin_tester/post-processor/dynamic/post-processor_acc_test.go b/packer_test/lib/plugin_tester/post-processor/dynamic/post-processor_acc_test.go similarity index 100% rename from packer_test/plugin_tester/post-processor/dynamic/post-processor_acc_test.go rename to packer_test/lib/plugin_tester/post-processor/dynamic/post-processor_acc_test.go diff --git a/packer_test/plugin_tester/post-processor/dynamic/test-fixtures/template.pkr.hcl b/packer_test/lib/plugin_tester/post-processor/dynamic/test-fixtures/template.pkr.hcl similarity index 100% rename from packer_test/plugin_tester/post-processor/dynamic/test-fixtures/template.pkr.hcl rename to packer_test/lib/plugin_tester/post-processor/dynamic/test-fixtures/template.pkr.hcl diff --git a/packer_test/plugin_tester/provisioner/dynamic/provisioner.go b/packer_test/lib/plugin_tester/provisioner/dynamic/provisioner.go similarity index 100% rename from packer_test/plugin_tester/provisioner/dynamic/provisioner.go rename to packer_test/lib/plugin_tester/provisioner/dynamic/provisioner.go diff --git a/packer_test/plugin_tester/provisioner/dynamic/provisioner.hcl2spec.go b/packer_test/lib/plugin_tester/provisioner/dynamic/provisioner.hcl2spec.go similarity index 100% rename from packer_test/plugin_tester/provisioner/dynamic/provisioner.hcl2spec.go rename to packer_test/lib/plugin_tester/provisioner/dynamic/provisioner.hcl2spec.go diff --git a/packer_test/plugin_tester/provisioner/dynamic/provisioner_acc_test.go b/packer_test/lib/plugin_tester/provisioner/dynamic/provisioner_acc_test.go similarity index 100% rename from packer_test/plugin_tester/provisioner/dynamic/provisioner_acc_test.go rename to packer_test/lib/plugin_tester/provisioner/dynamic/provisioner_acc_test.go diff --git a/packer_test/plugin_tester/provisioner/dynamic/test-fixtures/template.pkr.hcl b/packer_test/lib/plugin_tester/provisioner/dynamic/test-fixtures/template.pkr.hcl similarity index 100% rename from packer_test/plugin_tester/provisioner/dynamic/test-fixtures/template.pkr.hcl rename to packer_test/lib/plugin_tester/provisioner/dynamic/test-fixtures/template.pkr.hcl diff --git a/packer_test/plugin_tester/version/version.go b/packer_test/lib/plugin_tester/version/version.go similarity index 100% rename from packer_test/plugin_tester/version/version.go rename to packer_test/lib/plugin_tester/version/version.go diff --git a/packer_test/suite_test.go b/packer_test/lib/suite.go similarity index 61% rename from packer_test/suite_test.go rename to packer_test/lib/suite.go index ad76281c6cf..65cfdede817 100644 --- a/packer_test/suite_test.go +++ b/packer_test/lib/suite.go @@ -1,6 +1,7 @@ -package packer_test +package lib import ( + "fmt" "os" "sync" "testing" @@ -20,24 +21,29 @@ type PackerTestSuite struct { // Since we don't necessarily want to manually compile Packer beforehand, // we compile it on demand, and use this executable for the tests. packerPath string + // compiledPlugins is the map of each compiled plugin to its path. + // + // This used to be global, but should be linked to the suite instead, as + // 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 } -func buildPluginVersion(waitgroup *sync.WaitGroup, versionString string, t *testing.T) { +func (ts *PackerTestSuite) buildPluginVersion(waitgroup *sync.WaitGroup, versionString string, t *testing.T) { waitgroup.Add(1) go func() { defer waitgroup.Done() - BuildSimplePlugin(versionString, t) + ts.BuildSimplePlugin(versionString, t) }() } -func (ts *PackerTestSuite) buildPluginBinaries(t *testing.T) { +func (ts *PackerTestSuite) CompileTestPluginVersions(t *testing.T, versions ...string) { wg := &sync.WaitGroup{} - buildPluginVersion(wg, "1.0.0", t) - buildPluginVersion(wg, "1.0.0+metadata", t) - buildPluginVersion(wg, "1.0.1-alpha1", t) - buildPluginVersion(wg, "1.0.9", t) - buildPluginVersion(wg, "1.0.10", t) + for _, ver := range versions { + ts.buildPluginVersion(wg, ver, t) + } wg.Wait() } @@ -56,20 +62,18 @@ func (ts *PackerTestSuite) SkipNoAcc() { } } -func Test_PackerCoreSuite(t *testing.T) { +func PackerCoreSuite(t *testing.T) (*PackerTestSuite, func()) { ts := &PackerTestSuite{} - pluginsDirectory := PluginBinaryDir() + tempDir, err := os.MkdirTemp("", "packer-core-acc-test-") + if err != nil { + panic(fmt.Sprintf("failed to create temporary directory for compiled plugins: %s", err)) + } + ts.pluginsDirectory = tempDir + defer func() { - err := os.RemoveAll(pluginsDirectory) - if err != nil { - t.Logf("failed to cleanup directory %q: %s. This will need manual action", pluginsDirectory, err) - } }() - ts.pluginsDirectory = pluginsDirectory - ts.buildPluginBinaries(t) - packerPath := os.Getenv("PACKER_CUSTOM_PATH") if packerPath == "" { var err error @@ -82,16 +86,19 @@ func Test_PackerCoreSuite(t *testing.T) { ts.packerPath = packerPath t.Logf("Done") - defer func() { + return ts, func() { + err := os.RemoveAll(ts.pluginsDirectory) + if err != nil { + t.Logf("failed to cleanup directory %q: %s. This will need manual action", ts.pluginsDirectory, err) + } + if os.Getenv("PACKER_CUSTOM_PATH") != "" { return } - err := os.Remove(ts.packerPath) + err = os.Remove(ts.packerPath) if err != nil { t.Logf("failed to cleanup compiled packer binary %q: %s. This will need manual action", packerPath, err) } - }() - - suite.Run(t, ts) + } } diff --git a/packer_test/init_test.go b/packer_test/plugin_tests/init_test.go similarity index 57% rename from packer_test/init_test.go rename to packer_test/plugin_tests/init_test.go index 38939ec252a..e5c3fa46488 100644 --- a/packer_test/init_test.go +++ b/packer_test/plugin_tests/init_test.go @@ -1,6 +1,8 @@ -package packer_test +package plugin_tests -func (ts *PackerTestSuite) TestPackerInitForce() { +import "github.com/hashicorp/packer/packer_test/lib" + +func (ts *PackerPluginTestSuite) TestPackerInitForce() { ts.SkipNoAcc() pluginPath, cleanup := ts.MakePluginDir() @@ -9,17 +11,17 @@ func (ts *PackerTestSuite) TestPackerInitForce() { ts.Run("installs any missing plugins", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("init", "--force", "./templates/init/hashicups.pkr.hcl"). - Assert(MustSucceed(), Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", grepStdout)) + Assert(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", lib.GrepStdout)) }) ts.Run("reinstalls plugins matching version constraints", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("init", "--force", "./templates/init/hashicups.pkr.hcl"). - Assert(MustSucceed(), Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", grepStdout)) + Assert(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", lib.GrepStdout)) }) } -func (ts *PackerTestSuite) TestPackerInitUpgrade() { +func (ts *PackerPluginTestSuite) TestPackerInitUpgrade() { ts.SkipNoAcc() pluginPath, cleanup := ts.MakePluginDir() @@ -27,7 +29,7 @@ func (ts *PackerTestSuite) TestPackerInitUpgrade() { cmd := ts.PackerCommand().UsePluginDir(pluginPath) cmd.SetArgs("plugins", "install", "github.com/hashicorp/hashicups", "1.0.1") - cmd.Assert(MustSucceed(), Grep("Installed plugin github.com/hashicorp/hashicups v1.0.1", grepStdout)) + cmd.Assert(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.1", lib.GrepStdout)) _, _, err := cmd.Run() if err != nil { @@ -37,35 +39,35 @@ func (ts *PackerTestSuite) TestPackerInitUpgrade() { ts.Run("upgrades a plugin to the latest matching version constraints", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("init", "--upgrade", "./templates/init/hashicups.pkr.hcl"). - Assert(MustSucceed(), Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", grepStdout)) + Assert(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", lib.GrepStdout)) }) } -func (ts *PackerTestSuite) TestPackerInitWithNonGithubSource() { +func (ts *PackerPluginTestSuite) TestPackerInitWithNonGithubSource() { pluginPath, cleanup := ts.MakePluginDir() defer cleanup() ts.Run("try installing from a non-github source, should fail", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("init", "./templates/init/non_gh.pkr.hcl"). - Assert(MustFail(), Grep(`doesn't appear to be a valid "github.com" source address`, grepStdout)) + Assert(lib.MustFail(), lib.Grep(`doesn't appear to be a valid "github.com" source address`, lib.GrepStdout)) }) ts.Run("manually install plugin to the expected source", func() { ts.PackerCommand().UsePluginDir(pluginPath). - SetArgs("plugins", "install", "--path", BuildSimplePlugin("1.0.10", ts.T()), "hubgit.com/hashicorp/tester"). - Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.10", grepStdout)) + SetArgs("plugins", "install", "--path", ts.BuildSimplePlugin("1.0.10", ts.T()), "hubgit.com/hashicorp/tester"). + Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1.0.10", lib.GrepStdout)) }) ts.Run("re-run packer init on same template, should succeed silently", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("init", "./templates/init/non_gh.pkr.hcl"). - Assert(MustSucceed(), - MkPipeCheck("no output in stdout").SetTester(ExpectEmptyInput()).SetStream(OnlyStdout)) + Assert(lib.MustSucceed(), + lib.MkPipeCheck("no output in stdout").SetTester(lib.ExpectEmptyInput()).SetStream(lib.OnlyStdout)) }) } -func (ts *PackerTestSuite) TestPackerInitWithMixedVersions() { +func (ts *PackerPluginTestSuite) TestPackerInitWithMixedVersions() { ts.SkipNoAcc() pluginPath, cleanup := ts.MakePluginDir() @@ -74,7 +76,7 @@ func (ts *PackerTestSuite) TestPackerInitWithMixedVersions() { ts.Run("skips the plugin installation with mixed versions before exiting with an error", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("init", "./templates/init/mixed_versions.pkr.hcl"). - Assert(MustFail(), - Grep("binary reported a pre-release version of 10.7.3-dev", grepStdout)) + Assert(lib.MustFail(), + lib.Grep("binary reported a pre-release version of 10.7.3-dev", lib.GrepStdout)) }) } diff --git a/packer_test/install_test.go b/packer_test/plugin_tests/install_test.go similarity index 52% rename from packer_test/install_test.go rename to packer_test/plugin_tests/install_test.go index 3472c9af3c3..a80369d36a5 100644 --- a/packer_test/install_test.go +++ b/packer_test/plugin_tests/install_test.go @@ -1,62 +1,64 @@ -package packer_test +package plugin_tests -func (ts *PackerTestSuite) TestInstallPluginWithMetadata() { +import "github.com/hashicorp/packer/packer_test/lib" + +func (ts *PackerPluginTestSuite) TestInstallPluginWithMetadata() { tempPluginDir, cleanup := ts.MakePluginDir() defer cleanup() ts.Run("install plugin with metadata in version", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). - SetArgs("plugins", "install", "--path", BuildSimplePlugin("1.0.0+metadata", ts.T()), "github.com/hashicorp/tester"). - Assert(MustSucceed(), Grep("Successfully installed plugin", grepStdout)) + SetArgs("plugins", "install", "--path", ts.BuildSimplePlugin("1.0.0+metadata", ts.T()), "github.com/hashicorp/tester"). + Assert(lib.MustSucceed(), lib.Grep("Successfully installed plugin", lib.GrepStdout)) }) ts.Run("metadata plugin installed must not have metadata in its path", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). SetArgs("plugins", "installed"). - Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.0[^+]", grepStdout)) + Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1.0.0[^+]", lib.GrepStdout)) }) ts.Run("plugin with metadata should work with validate", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). SetArgs("validate", "./templates/simple.pkr.hcl"). - Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", grepStderr)) + Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", lib.GrepStderr)) }) ts.Run("plugin with metadata should work with build", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). SetArgs("build", "./templates/simple.pkr.hcl"). - Assert(MustSucceed(), Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", grepStderr)) + Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", lib.GrepStderr)) }) } -func (ts *PackerTestSuite) TestInstallPluginWithPath() { +func (ts *PackerPluginTestSuite) TestInstallPluginWithPath() { tempPluginDir, cleanup := ts.MakePluginDir() defer cleanup() ts.Run("install plugin with pre-release only", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). - SetArgs("plugins", "install", "--path", BuildSimplePlugin("1.0.0-dev", ts.T()), "github.com/hashicorp/tester"). - Assert(MustSucceed(), Grep("Successfully installed plugin", grepStdout)) + SetArgs("plugins", "install", "--path", ts.BuildSimplePlugin("1.0.0-dev", ts.T()), "github.com/hashicorp/tester"). + Assert(lib.MustSucceed(), lib.Grep("Successfully installed plugin", lib.GrepStdout)) }) ts.Run("install same plugin with pre-release + metadata", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). - SetArgs("plugins", "install", "--path", BuildSimplePlugin("1.0.0-dev+metadata", ts.T()), "github.com/hashicorp/tester"). - Assert(MustSucceed(), Grep("Successfully installed plugin", grepStdout)) + SetArgs("plugins", "install", "--path", ts.BuildSimplePlugin("1.0.0-dev+metadata", ts.T()), "github.com/hashicorp/tester"). + Assert(lib.MustSucceed(), lib.Grep("Successfully installed plugin", lib.GrepStdout)) }) ts.Run("list plugins, should only report one plugin", func() { ts.PackerCommand().UsePluginDir(tempPluginDir). SetArgs("plugins", "installed"). - Assert(MustSucceed(), - Grep("plugin-tester_v1.0.0-dev[^+]", grepStdout), - Grep("plugin-tester_v1.0.0-dev\\+", grepStdout, grepInvert), - LineCountCheck(1)) + Assert(lib.MustSucceed(), + lib.Grep("plugin-tester_v1.0.0-dev[^+]", lib.GrepStdout), + lib.Grep("plugin-tester_v1.0.0-dev\\+", lib.GrepStdout, lib.GrepInvert), + lib.LineCountCheck(1)) }) } -func (ts *PackerTestSuite) TestInstallPluginPrerelease() { - pluginPath := BuildSimplePlugin("1.0.1-alpha1", ts.T()) +func (ts *PackerPluginTestSuite) TestInstallPluginPrerelease() { + pluginPath := ts.BuildSimplePlugin("1.0.1-alpha1", ts.T()) pluginDir, cleanup := ts.MakePluginDir() defer cleanup() @@ -64,11 +66,11 @@ func (ts *PackerTestSuite) TestInstallPluginPrerelease() { ts.Run("try install plugin with alpha1 prerelease - should fail", func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("plugins", "install", "--path", pluginPath, "github.com/hashicorp/tester"). - Assert(MustFail(), Grep("Packer can only install plugin releases with this command", grepStdout)) + Assert(lib.MustFail(), lib.Grep("Packer can only install plugin releases with this command", lib.GrepStdout)) }) } -func (ts *PackerTestSuite) TestRemoteInstallWithPluginsInstall() { +func (ts *PackerPluginTestSuite) TestRemoteInstallWithPluginsInstall() { ts.SkipNoAcc() pluginPath, cleanup := ts.MakePluginDir() @@ -77,11 +79,11 @@ func (ts *PackerTestSuite) TestRemoteInstallWithPluginsInstall() { ts.Run("install latest version of a remote plugin with packer plugins install", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "install", "github.com/hashicorp/hashicups"). - Assert(MustSucceed()) + Assert(lib.MustSucceed()) }) } -func (ts *PackerTestSuite) TestRemoteInstallOfPreReleasePlugin() { +func (ts *PackerPluginTestSuite) TestRemoteInstallOfPreReleasePlugin() { ts.SkipNoAcc() pluginPath, cleanup := ts.MakePluginDir() @@ -90,15 +92,15 @@ func (ts *PackerTestSuite) TestRemoteInstallOfPreReleasePlugin() { ts.Run("try to init with a pre-release constraint - should fail", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("init", "templates/pre-release_constraint.pkr.hcl"). - Assert(MustFail(), - Grep("Invalid version constraint", grepStdout), - Grep("Unsupported prerelease for constraint", grepStdout)) + Assert(lib.MustFail(), + lib.Grep("Invalid version constraint", lib.GrepStdout), + lib.Grep("Unsupported prerelease for constraint", lib.GrepStdout)) }) ts.Run("try to plugins install with a pre-release version - should fail", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugin", "install", "github.com/hashicorp/hashicups", "1.0.2-dev"). - Assert(MustFail(), - Grep("Unsupported prerelease for constraint", grepStdout)) + Assert(lib.MustFail(), + lib.Grep("Unsupported prerelease for constraint", lib.GrepStdout)) }) } diff --git a/packer_test/loading_test.go b/packer_test/plugin_tests/loading_test.go similarity index 55% rename from packer_test/loading_test.go rename to packer_test/plugin_tests/loading_test.go index 1f1d8ceeef2..4b14ba8060f 100644 --- a/packer_test/loading_test.go +++ b/packer_test/plugin_tests/loading_test.go @@ -1,13 +1,15 @@ -package packer_test +package plugin_tests import ( "crypto/sha256" "fmt" "os" "path/filepath" + + "github.com/hashicorp/packer/packer_test/lib" ) -func (ts *PackerTestSuite) TestLoadingOrder() { +func (ts *PackerPluginTestSuite) TestLoadingOrder() { pluginDir, cleanup := ts.MakePluginDir("1.0.9", "1.0.10") defer cleanup() @@ -39,100 +41,100 @@ func (ts *PackerTestSuite) TestLoadingOrder() { ts.PackerCommand(). SetArgs(command, tt.templatePath). UsePluginDir(pluginDir). - Assert(MustSucceed(), Grep(tt.grepStr)) + Assert(lib.MustSucceed(), lib.Grep(tt.grepStr)) }) } } } -func (ts *PackerTestSuite) TestLoadWithLegacyPluginName() { +func (ts *PackerPluginTestSuite) TestLoadWithLegacyPluginName() { pluginDir, cleanup := ts.MakePluginDir() defer cleanup() - plugin := BuildSimplePlugin("1.0.10", ts.T()) + plugin := ts.BuildSimplePlugin("1.0.10", ts.T()) - CopyFile(ts.T(), filepath.Join(pluginDir, "packer-plugin-tester"), plugin) + lib.CopyFile(ts.T(), filepath.Join(pluginDir, "packer-plugin-tester"), plugin) ts.Run("only legacy plugins installed: expect build to fail", func() { ts.Run("with required_plugins - expect prompt for packer init", func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("build", "templates/simple.pkr.hcl"). - Assert(MustFail(), - Grep("Did you run packer init for this project", grepStdout), - Grep("following plugins are required", grepStdout)) + Assert(lib.MustFail(), + lib.Grep("Did you run packer init for this project", lib.GrepStdout), + lib.Grep("following plugins are required", lib.GrepStdout)) }) ts.Run("JSON template, without required_plugins: should say the component is unknown", func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("build", "templates/simple.json"). - Assert(MustFail(), - Grep("The builder tester-dynamic is unknown by Packer", grepStdout)) + Assert(lib.MustFail(), + lib.Grep("The builder tester-dynamic is unknown by Packer", lib.GrepStdout)) }) }) pluginDir, cleanup = ts.MakePluginDir("1.0.0") defer cleanup() - CopyFile(ts.T(), filepath.Join(pluginDir, "packer-plugin-tester"), plugin) + lib.CopyFile(ts.T(), filepath.Join(pluginDir, "packer-plugin-tester"), plugin) ts.Run("multiple plugins installed: one with no version in path, one with qualified name. Should pick-up the qualified one only.", func() { ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("build", "templates/simple.pkr.hcl"). - Assert(MustSucceed(), Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", grepStderr)) + Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", lib.GrepStderr)) }) - wd, cleanup := TempWorkdir(ts.T(), "./templates/simple.pkr.hcl") + wd, cleanup := lib.TempWorkdir(ts.T(), "./templates/simple.pkr.hcl") defer cleanup() - CopyFile(ts.T(), filepath.Join(wd, "packer-plugin-tester"), plugin) + lib.CopyFile(ts.T(), filepath.Join(wd, "packer-plugin-tester"), plugin) ts.Run("multiple plugins installed: 1.0.0 in plugin dir with sum, one in workdir (no version). Should load 1.0.0", func() { ts.PackerCommand().UsePluginDir(pluginDir).SetWD(wd). SetArgs("build", "simple.pkr.hcl"). - Assert(MustSucceed(), Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", grepStderr)) + Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", lib.GrepStderr)) }) } -func (ts *PackerTestSuite) TestLoadWithSHAMismatches() { - plugin := BuildSimplePlugin("1.0.10", ts.T()) +func (ts *PackerPluginTestSuite) TestLoadWithSHAMismatches() { + plugin := ts.BuildSimplePlugin("1.0.10", ts.T()) ts.Run("move plugin with right name, but no SHA256SUM, should reject", func() { pluginDir, cleanup := ts.MakePluginDir("1.0.9") defer cleanup() - pluginDestName := ExpectedInstalledName("1.0.10") + pluginDestName := lib.ExpectedInstalledName("1.0.10") - CopyFile(ts.T(), filepath.Join(pluginDir, "github.com", "hashicorp", "tester", pluginDestName), plugin) + lib.CopyFile(ts.T(), filepath.Join(pluginDir, "github.com", "hashicorp", "tester", pluginDestName), plugin) ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("plugins", "installed"). - Assert(MustSucceed(), - Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", grepStdout), - Grep("packer-plugin-tester_v1.0.10", grepStdout, grepInvert), - Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", grepStderr)) + Assert(lib.MustSucceed(), + lib.Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", lib.GrepStdout), + lib.Grep("packer-plugin-tester_v1.0.10", lib.GrepStdout, lib.GrepInvert), + lib.Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", lib.GrepStderr)) }) ts.Run("move plugin with right name, invalid SHA256SUM, should reject", func() { pluginDir, cleanup := ts.MakePluginDir("1.0.9") defer cleanup() - pluginDestName := ExpectedInstalledName("1.0.10") - CopyFile(ts.T(), filepath.Join(pluginDir, "github.com", "hashicorp", "tester", pluginDestName), plugin) - WriteFile(ts.T(), + pluginDestName := lib.ExpectedInstalledName("1.0.10") + lib.CopyFile(ts.T(), filepath.Join(pluginDir, "github.com", "hashicorp", "tester", pluginDestName), plugin) + lib.WriteFile(ts.T(), filepath.Join(pluginDir, "github.com", "hashicorp", "tester", fmt.Sprintf("%s_SHA256SUM", pluginDestName)), fmt.Sprintf("%x", sha256.New().Sum([]byte("Not the plugin's contents for sure.")))) ts.PackerCommand().UsePluginDir(pluginDir). SetArgs("plugins", "installed"). - Assert(MustSucceed(), - Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", grepStdout), - Grep("packer-plugin-tester_v1.0.10", grepInvert, grepStdout), - Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", grepStderr), - Grep(`Checksums \(\*sha256\.digest\) did not match.`, grepStderr)) + Assert(lib.MustSucceed(), + lib.Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", lib.GrepStdout), + lib.Grep("packer-plugin-tester_v1.0.10", lib.GrepInvert, lib.GrepStdout), + lib.Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", lib.GrepStderr), + lib.Grep(`Checksums \(\*sha256\.digest\) did not match.`, lib.GrepStderr)) }) } -func (ts *PackerTestSuite) TestPluginPathEnvvarWithMultiplePaths() { +func (ts *PackerPluginTestSuite) TestPluginPathEnvvarWithMultiplePaths() { pluginDirOne, cleanup := ts.MakePluginDir("1.0.10") defer cleanup() @@ -143,53 +145,53 @@ func (ts *PackerTestSuite) TestPluginPathEnvvarWithMultiplePaths() { ts.Run("load plugin with two dirs - not supported anymore, should error", func() { ts.PackerCommand().UsePluginDir(pluginDirVal). SetArgs("plugins", "installed"). - Assert(MustFail(), - Grep("Multiple paths are no longer supported for PACKER_PLUGIN_PATH"), - MkPipeCheck("All envvars are suggested", - PipeGrep(`\* PACKER_PLUGIN_PATH=`), - LineCount()). - SetStream(OnlyStderr). - SetTester(IntCompare(eq, 2))) + Assert(lib.MustFail(), + lib.Grep("Multiple paths are no longer supported for PACKER_PLUGIN_PATH"), + lib.MkPipeCheck("All envvars are suggested", + lib.PipeGrep(`\* PACKER_PLUGIN_PATH=`), + lib.LineCount()). + SetStream(lib.OnlyStderr). + SetTester(lib.IntCompare(lib.Eq, 2))) }) } -func (ts *PackerTestSuite) TestInstallNonCanonicalPluginVersion() { +func (ts *PackerPluginTestSuite) TestInstallNonCanonicalPluginVersion() { pluginPath, cleanup := ts.MakePluginDir() defer cleanup() - ManualPluginInstall(ts.T(), + lib.ManualPluginInstall(ts.T(), filepath.Join(pluginPath, "github.com", "hashicorp", "tester"), - BuildSimplePlugin("1.0.10", ts.T()), + ts.BuildSimplePlugin("1.0.10", ts.T()), "001.00.010") ts.Run("try listing plugins with non-canonical version installed - report none", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "installed"). - Assert(MustSucceed(), - Grep(`version .* in path is non canonical`, grepStderr), - MkPipeCheck("no output in stdout").SetTester(ExpectEmptyInput()).SetStream(OnlyStdout)) + Assert(lib.MustSucceed(), + lib.Grep(`version .* in path is non canonical`, lib.GrepStderr), + lib.MkPipeCheck("no output in stdout").SetTester(lib.ExpectEmptyInput()).SetStream(lib.OnlyStdout)) }) } -func (ts *PackerTestSuite) TestLoadPluginWithMetadataInName() { +func (ts *PackerPluginTestSuite) TestLoadPluginWithMetadataInName() { pluginPath, cleanup := ts.MakePluginDir() defer cleanup() - ManualPluginInstall(ts.T(), + lib.ManualPluginInstall(ts.T(), filepath.Join(pluginPath, "github.com", "hashicorp", "tester"), - BuildSimplePlugin("1.0.10+metadata", ts.T()), + ts.BuildSimplePlugin("1.0.10+metadata", ts.T()), "1.0.10+metadata") ts.Run("try listing plugins with metadata in name - report none", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "installed"). - Assert(MustSucceed(), - Grep("found version .* with metadata in the name", grepStderr), - MkPipeCheck("no output in stdout").SetTester(ExpectEmptyInput()).SetStream(OnlyStdout)) + Assert(lib.MustSucceed(), + lib.Grep("found version .* with metadata in the name", lib.GrepStderr), + lib.MkPipeCheck("no output in stdout").SetTester(lib.ExpectEmptyInput()).SetStream(lib.OnlyStdout)) }) } -func (ts *PackerTestSuite) TestLoadWithOnlyReleaseFlag() { +func (ts *PackerPluginTestSuite) TestLoadWithOnlyReleaseFlag() { pluginPath, cleanup := ts.MakePluginDir("1.0.0", "1.0.1-dev") defer cleanup() @@ -197,24 +199,24 @@ func (ts *PackerTestSuite) TestLoadWithOnlyReleaseFlag() { ts.Run(fmt.Sprintf("run %s without --ignore-prerelease flag - pick 1.0.1-dev by default", cmd), func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs(cmd, "./templates/simple.pkr.hcl"). - Assert(MustSucceed(), - Grep("packer-plugin-tester_v1.0.1-dev.*: plugin process exited", grepStderr)) + Assert(lib.MustSucceed(), + lib.Grep("packer-plugin-tester_v1.0.1-dev.*: plugin process exited", lib.GrepStderr)) }) ts.Run(fmt.Sprintf("run %s with --ignore-prerelease flag - pick 1.0.0", cmd), func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs(cmd, "--ignore-prerelease-plugins", "./templates/simple.pkr.hcl"). - Assert(MustSucceed(), - Grep("packer-plugin-tester_v1.0.0.*: plugin process exited", grepStderr)) + Assert(lib.MustSucceed(), + lib.Grep("packer-plugin-tester_v1.0.0.*: plugin process exited", lib.GrepStderr)) }) } } -func (ts *PackerTestSuite) TestWithLegacyConfigAndComponents() { +func (ts *PackerPluginTestSuite) TestWithLegacyConfigAndComponents() { pluginDir, cleanup := ts.MakePluginDir("1.0.0") defer cleanup() - workdir, cleanup := TempWorkdir(ts.T(), "./sample_config.json", "./templates/simple.json", "./templates/simple.pkr.hcl") + workdir, cleanup := lib.TempWorkdir(ts.T(), "./sample_config.json", "./templates/simple.json", "./templates/simple.pkr.hcl") defer cleanup() for _, cmd := range []string{"validate", "build"} { @@ -222,18 +224,18 @@ func (ts *PackerTestSuite) TestWithLegacyConfigAndComponents() { ts.PackerCommand().UsePluginDir(pluginDir).SetWD(workdir). SetArgs(cmd, "simple.json"). AddEnv("PACKER_CONFIG", filepath.Join(workdir, "sample_config.json")). - Assert(MustFail(), - Grep("Your configuration file describes some legacy components", grepStderr), - Grep("packer-provisioner-super-shell", grepStderr)) + Assert(lib.MustFail(), + lib.Grep("Your configuration file describes some legacy components", lib.GrepStderr), + lib.Grep("packer-provisioner-super-shell", lib.GrepStderr)) }) ts.Run(fmt.Sprintf("%s simple HCL2 template with config.json and components defined", cmd), func() { ts.PackerCommand().UsePluginDir(pluginDir).SetWD(workdir). SetArgs(cmd, "simple.pkr.hcl"). AddEnv("PACKER_CONFIG", filepath.Join(workdir, "sample_config.json")). - Assert(MustFail(), - Grep("Your configuration file describes some legacy components", grepStderr), - Grep("packer-provisioner-super-shell", grepStderr)) + Assert(lib.MustFail(), + lib.Grep("Your configuration file describes some legacy components", lib.GrepStderr), + lib.Grep("packer-provisioner-super-shell", lib.GrepStderr)) }) } } diff --git a/packer_test/plugins_remove_test.go b/packer_test/plugin_tests/plugins_remove_test.go similarity index 70% rename from packer_test/plugins_remove_test.go rename to packer_test/plugin_tests/plugins_remove_test.go index cc10b611376..3bdfed37981 100644 --- a/packer_test/plugins_remove_test.go +++ b/packer_test/plugin_tests/plugins_remove_test.go @@ -1,11 +1,13 @@ -package packer_test +package plugin_tests import ( "path/filepath" "strings" + + "github.com/hashicorp/packer/packer_test/lib" ) -func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddress() { +func (ts *PackerPluginTestSuite) TestPluginsRemoveWithSourceAddress() { pluginPath, cleanup := ts.MakePluginDir("1.0.9", "1.0.10", "2.0.0") defer cleanup() @@ -17,10 +19,10 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddress() { ts.Run("plugins remove with source address removes all installed plugin versions", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "remove", "github.com/hashicorp/tester"). - Assert(MustSucceed(), - Grep("packer-plugin-tester_v1.0.9", grepStdout), - Grep("packer-plugin-tester_v1.0.10", grepStdout), - Grep("packer-plugin-tester_v2.0.0", grepStdout), + Assert(lib.MustSucceed(), + lib.Grep("packer-plugin-tester_v1.0.9", lib.GrepStdout), + lib.Grep("packer-plugin-tester_v1.0.10", lib.GrepStdout), + lib.Grep("packer-plugin-tester_v2.0.0", lib.GrepStdout), ) }) @@ -33,8 +35,8 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddress() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "remove", "github.com/hashicorp/testerONE"). Assert( - MustFail(), - Grep("No installed plugin found matching the plugin constraints github.com/hashicorp/testerONE"), + lib.MustFail(), + lib.Grep("No installed plugin found matching the plugin constraints github.com/hashicorp/testerONE"), ) }) @@ -42,13 +44,13 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddress() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "remove", "github.com/hashicorp/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/tester"). Assert( - MustFail(), - Grep("The source URL must have at most 16 components"), + lib.MustFail(), + lib.Grep("The source URL must have at most 16 components"), ) }) } -func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddressAndVersion() { +func (ts *PackerPluginTestSuite) TestPluginsRemoveWithSourceAddressAndVersion() { pluginPath, cleanup := ts.MakePluginDir("1.0.9", "1.0.10", "2.0.0") defer cleanup() @@ -60,17 +62,17 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddressAndVersion() { ts.Run("plugins remove with source address and version removes only the versioned plugin", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "remove", "github.com/hashicorp/tester", ">= 2.0.0"). - Assert(MustSucceed(), Grep("packer-plugin-tester_v2.0.0", grepStdout)) + Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v2.0.0", lib.GrepStdout)) }) ts.Run("plugins installed after single plugins remove outputs remaining installed plugins", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "installed"). Assert( - MustSucceed(), - Grep("packer-plugin-tester_v1.0.9", grepStdout), - Grep("packer-plugin-tester_v1.0.10", grepStdout), - Grep("packer-plugin-tester_v2.0.0", grepInvert, grepStdout), + lib.MustSucceed(), + lib.Grep("packer-plugin-tester_v1.0.9", lib.GrepStdout), + lib.Grep("packer-plugin-tester_v1.0.10", lib.GrepStdout), + lib.Grep("packer-plugin-tester_v2.0.0", lib.GrepInvert, lib.GrepStdout), ) }) @@ -80,7 +82,7 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithSourceAddressAndVersion() { } } -func (ts *PackerTestSuite) TestPluginsRemoveWithLocalPath() { +func (ts *PackerPluginTestSuite) TestPluginsRemoveWithLocalPath() { pluginPath, cleanup := ts.MakePluginDir("1.0.9", "1.0.10") defer cleanup() @@ -94,18 +96,18 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithLocalPath() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "remove", plugins[0]). Assert( - MustSucceed(), - Grep("packer-plugin-tester_v1.0.9", grepStdout), - Grep("packer-plugin-tester_v1.0.10", grepInvert, grepStdout), + lib.MustSucceed(), + lib.Grep("packer-plugin-tester_v1.0.9", lib.GrepStdout), + lib.Grep("packer-plugin-tester_v1.0.10", lib.GrepInvert, lib.GrepStdout), ) }) ts.Run("plugins installed after calling plugins remove outputs remaining installed plugins", func() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "installed"). Assert( - MustSucceed(), - Grep("packer-plugin-tester_v1.0.10", grepStdout), - Grep("packer-plugin-tester_v1.0.9", grepInvert, grepStdout), + lib.MustSucceed(), + lib.Grep("packer-plugin-tester_v1.0.10", lib.GrepStdout), + lib.Grep("packer-plugin-tester_v1.0.9", lib.GrepInvert, lib.GrepStdout), ) }) @@ -118,8 +120,8 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithLocalPath() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "remove", filepath.Base(plugins[0])). Assert( - MustFail(), - Grep("A source URL must at least contain a host and a path with 2 components", grepStdout), + lib.MustFail(), + lib.Grep("A source URL must at least contain a host and a path with 2 components", lib.GrepStdout), ) }) @@ -127,13 +129,13 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithLocalPath() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "remove", ts.T().TempDir()). Assert( - MustFail(), - Grep("is not under the plugin directory inferred by Packer", grepStdout), + lib.MustFail(), + lib.Grep("is not under the plugin directory inferred by Packer", lib.GrepStdout), ) }) } -func (ts *PackerTestSuite) TestPluginsRemoveWithNoArguments() { +func (ts *PackerPluginTestSuite) TestPluginsRemoveWithNoArguments() { pluginPath, cleanup := ts.MakePluginDir("1.0.9") defer cleanup() @@ -146,8 +148,8 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithNoArguments() { ts.PackerCommand().UsePluginDir(pluginPath). SetArgs("plugins", "remove"). Assert( - MustFail(), - Grep("Usage: packer plugins remove ", grepStdout), + lib.MustFail(), + lib.Grep("Usage: packer plugins remove ", lib.GrepStdout), ) }) @@ -158,12 +160,12 @@ func (ts *PackerTestSuite) TestPluginsRemoveWithNoArguments() { } -func InstalledPlugins(ts *PackerTestSuite, dir string) []string { +func InstalledPlugins(ts *PackerPluginTestSuite, dir string) []string { ts.T().Helper() cmd := ts.PackerCommand().UsePluginDir(dir). SetArgs("plugins", "installed") - cmd.Assert(MustSucceed()) + cmd.Assert(lib.MustSucceed()) if ts.T().Failed() { ts.T().Fatalf("Failed to execute plugin installed for %q", dir) } diff --git a/packer_test/sample_config.json b/packer_test/plugin_tests/sample_config.json similarity index 100% rename from packer_test/sample_config.json rename to packer_test/plugin_tests/sample_config.json diff --git a/packer_test/plugin_tests/suite_test.go b/packer_test/plugin_tests/suite_test.go new file mode 100644 index 00000000000..dac2e3b72d1 --- /dev/null +++ b/packer_test/plugin_tests/suite_test.go @@ -0,0 +1,36 @@ +package plugin_tests + +import ( + "testing" + + "github.com/hashicorp/packer/packer_test/lib" + "github.com/stretchr/testify/suite" +) + +type PackerPluginTestSuite struct { + *lib.PackerTestSuite +} + +func Test_PackerPluginSuite(t *testing.T) { + baseSuite, cleanup := lib.PackerCoreSuite(t) + defer cleanup() + + ts := &PackerPluginTestSuite{ + baseSuite, + } + ts.PackerTestSuite.CompileTestPluginVersions(t, + "1.0.0", + "1.0.0", + "1.0.0+metadata", + "1.0.1-alpha1", + "1.0.9", + "1.0.10", + "1.0.0-dev", + "1.0.0-dev+metadata", + "1.0.10+metadata", + "1.0.1-dev", + "2.0.0", + ) + + suite.Run(t, ts) +} diff --git a/packer_test/templates/init/hashicups.pkr.hcl b/packer_test/plugin_tests/templates/init/hashicups.pkr.hcl similarity index 100% rename from packer_test/templates/init/hashicups.pkr.hcl rename to packer_test/plugin_tests/templates/init/hashicups.pkr.hcl diff --git a/packer_test/templates/init/mixed_versions.pkr.hcl b/packer_test/plugin_tests/templates/init/mixed_versions.pkr.hcl similarity index 100% rename from packer_test/templates/init/mixed_versions.pkr.hcl rename to packer_test/plugin_tests/templates/init/mixed_versions.pkr.hcl diff --git a/packer_test/templates/init/non_gh.pkr.hcl b/packer_test/plugin_tests/templates/init/non_gh.pkr.hcl similarity index 100% rename from packer_test/templates/init/non_gh.pkr.hcl rename to packer_test/plugin_tests/templates/init/non_gh.pkr.hcl diff --git a/packer_test/templates/pin_1.0.9.pkr.hcl b/packer_test/plugin_tests/templates/pin_1.0.9.pkr.hcl similarity index 100% rename from packer_test/templates/pin_1.0.9.pkr.hcl rename to packer_test/plugin_tests/templates/pin_1.0.9.pkr.hcl diff --git a/packer_test/templates/pre-release_constraint.pkr.hcl b/packer_test/plugin_tests/templates/pre-release_constraint.pkr.hcl similarity index 100% rename from packer_test/templates/pre-release_constraint.pkr.hcl rename to packer_test/plugin_tests/templates/pre-release_constraint.pkr.hcl diff --git a/packer_test/templates/simple.json b/packer_test/plugin_tests/templates/simple.json similarity index 100% rename from packer_test/templates/simple.json rename to packer_test/plugin_tests/templates/simple.json diff --git a/packer_test/templates/simple.pkr.hcl b/packer_test/plugin_tests/templates/simple.pkr.hcl similarity index 100% rename from packer_test/templates/simple.pkr.hcl rename to packer_test/plugin_tests/templates/simple.pkr.hcl