packer_test: rename/split lib into common/check

The lib name for the common components for writing packer_test suites
was not clear, and did not follow the convention established in Packer
core and plugins.
Therefore this commit does two things: first the lib is renamed into
common as to follow this convention, and clearly document which
components are common to all tests.
Also checkers are placed in a subpackage of common, common/check, so
that it is clearer what is meant to be used as checks for a command's
execution status after it's been run, as part of Assert.
pull/13163/head
Lucas Bajolet 2 years ago committed by Lucas Bajolet
parent 7f7ae8454a
commit 35b2317ef3

@ -1,4 +1,4 @@
package lib
package common
import (
"fmt"

@ -1,4 +1,4 @@
package lib
package check
import (
"fmt"

@ -1,4 +1,4 @@
package lib
package common
import (
"fmt"
@ -6,6 +6,8 @@ import (
"os/exec"
"strings"
"testing"
"github.com/hashicorp/packer/packer_test/common/check"
)
type packerCommand struct {
@ -139,7 +141,7 @@ func (pc *packerCommand) Run() (string, string, error) {
pc.err = cmd.Run()
// Check that the command didn't panic, and if it did, we can immediately error
panicErr := PanicCheck{}.Check(pc.stdout.String(), pc.stderr.String(), pc.err)
panicErr := check.PanicCheck{}.Check(pc.stdout.String(), pc.stderr.String(), pc.err)
if panicErr != nil {
pc.t.Fatalf("Packer panicked during execution: %s", panicErr)
}
@ -147,16 +149,16 @@ func (pc *packerCommand) Run() (string, string, error) {
return pc.stdout.String(), pc.stderr.String(), pc.err
}
func (pc *packerCommand) Assert(checks ...Checker) {
func (pc *packerCommand) Assert(checks ...check.Checker) {
attempt := 0
for pc.runs > 0 {
attempt++
stdout, stderr, err := pc.Run()
for _, check := range checks {
checkErr := check.Check(stdout, stderr, err)
for _, checker := range checks {
checkErr := checker.Check(stdout, stderr, err)
if checkErr != nil {
checkerName := InferName(check)
checkerName := check.InferName(checker)
pc.t.Errorf("check %q failed: %s", checkerName, checkErr)
}
}

@ -1,4 +1,4 @@
package lib
package common
import (
"crypto/sha256"

@ -1,4 +1,4 @@
package lib
package common
import (
"fmt"
@ -11,6 +11,7 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/packer-plugin-sdk/plugin"
"github.com/hashicorp/packer/packer_test/common/check"
)
// LDFlags compiles the ldflags for the plugin to compile based on the information provided.
@ -156,7 +157,7 @@ func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDi
for _, pluginVersion := range pluginVersions {
path := ts.GetPluginPath(t, pluginVersion)
cmd := ts.PackerCommand().SetArgs("plugins", "install", "--path", path, "github.com/hashicorp/tester").AddEnv("PACKER_PLUGIN_PATH", pluginTempDir)
cmd.Assert(MustSucceed())
cmd.Assert(check.MustSucceed())
out, stderr, cmdErr := cmd.Run()
if cmdErr != nil {
err = fmt.Errorf("failed to install tester plugin version %q: %s\nCommand stdout: %s\nCommand stderr: %s", pluginVersion, err, out, stderr)

@ -1,4 +1,4 @@
package lib
package common
import (
"fmt"

@ -3,7 +3,7 @@ package core_test
import (
"fmt"
"github.com/hashicorp/packer/packer_test/lib"
"github.com/hashicorp/packer/packer_test/common/check"
)
func (ts *PackerCoreTestSuite) TestEvalLocalsOrder() {
@ -16,8 +16,8 @@ func (ts *PackerCoreTestSuite) TestEvalLocalsOrder() {
Runs(10).
Stdin("local.test_local\n").
SetArgs("console", "./templates/locals_no_order.pkr.hcl").
Assert(lib.MustSucceed(),
lib.GrepInverted("\\[\\]", lib.GrepStdout))
Assert(check.MustSucceed(),
check.GrepInverted("\\[\\]", check.GrepStdout))
}
func (ts *PackerCoreTestSuite) TestLocalDuplicates() {
@ -28,9 +28,9 @@ func (ts *PackerCoreTestSuite) 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(lib.MustFail(),
lib.Grep("Duplicate local definition"),
lib.Grep("Local variable \"test\" is defined twice"))
Assert(check.MustFail(),
check.Grep("Duplicate local definition"),
check.Grep("Local variable \"test\" is defined twice"))
})
}
}

@ -3,16 +3,16 @@ package core_test
import (
"testing"
"github.com/hashicorp/packer/packer_test/lib"
"github.com/hashicorp/packer/packer_test/common"
"github.com/stretchr/testify/suite"
)
type PackerCoreTestSuite struct {
*lib.PackerTestSuite
*common.PackerTestSuite
}
func Test_PackerCoreSuite(t *testing.T) {
baseSuite, cleanup := lib.InitBaseSuite(t)
baseSuite, cleanup := common.InitBaseSuite(t)
defer cleanup()
ts := &PackerCoreTestSuite{

@ -1,6 +1,6 @@
package plugin_tests
import "github.com/hashicorp/packer/packer_test/lib"
import "github.com/hashicorp/packer/packer_test/common/check"
func (ts *PackerPluginTestSuite) TestPackerInitForce() {
ts.SkipNoAcc()
@ -11,13 +11,13 @@ func (ts *PackerPluginTestSuite) TestPackerInitForce() {
ts.Run("installs any missing plugins", func() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("init", "--force", "./templates/init/hashicups.pkr.hcl").
Assert(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", lib.GrepStdout))
Assert(check.MustSucceed(), check.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", check.GrepStdout))
})
ts.Run("reinstalls plugins matching version constraints", func() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("init", "--force", "./templates/init/hashicups.pkr.hcl").
Assert(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", lib.GrepStdout))
Assert(check.MustSucceed(), check.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", check.GrepStdout))
})
}
@ -29,7 +29,7 @@ func (ts *PackerPluginTestSuite) TestPackerInitUpgrade() {
cmd := ts.PackerCommand().UsePluginDir(pluginPath)
cmd.SetArgs("plugins", "install", "github.com/hashicorp/hashicups", "1.0.1")
cmd.Assert(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.1", lib.GrepStdout))
cmd.Assert(check.MustSucceed(), check.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.1", check.GrepStdout))
_, _, err := cmd.Run()
if err != nil {
@ -39,7 +39,7 @@ func (ts *PackerPluginTestSuite) 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(lib.MustSucceed(), lib.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", lib.GrepStdout))
Assert(check.MustSucceed(), check.Grep("Installed plugin github.com/hashicorp/hashicups v1.0.2", check.GrepStdout))
})
}
@ -50,20 +50,20 @@ func (ts *PackerPluginTestSuite) TestPackerInitWithNonGithubSource() {
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(lib.MustFail(), lib.Grep(`doesn't appear to be a valid "github.com" source address`, lib.GrepStdout))
Assert(check.MustFail(), check.Grep(`doesn't appear to be a valid "github.com" source address`, check.GrepStdout))
})
ts.Run("manually install plugin to the expected source", func() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.10"), "hubgit.com/hashicorp/tester").
Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1.0.10", lib.GrepStdout))
Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v1.0.10", check.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(lib.MustSucceed(),
lib.MkPipeCheck("no output in stdout").SetTester(lib.ExpectEmptyInput()).SetStream(lib.OnlyStdout))
Assert(check.MustSucceed(),
check.MkPipeCheck("no output in stdout").SetTester(check.ExpectEmptyInput()).SetStream(check.OnlyStdout))
})
}
@ -76,7 +76,7 @@ func (ts *PackerPluginTestSuite) 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(lib.MustFail(),
lib.Grep("binary reported a pre-release version of 10.7.3-dev", lib.GrepStdout))
Assert(check.MustFail(),
check.Grep("binary reported a pre-release version of 10.7.3-dev", check.GrepStdout))
})
}

@ -1,6 +1,6 @@
package plugin_tests
import "github.com/hashicorp/packer/packer_test/lib"
import "github.com/hashicorp/packer/packer_test/common/check"
func (ts *PackerPluginTestSuite) TestInstallPluginWithMetadata() {
tempPluginDir, cleanup := ts.MakePluginDir()
@ -9,25 +9,25 @@ func (ts *PackerPluginTestSuite) TestInstallPluginWithMetadata() {
ts.Run("install plugin with metadata in version", func() {
ts.PackerCommand().UsePluginDir(tempPluginDir).
SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.0+metadata"), "github.com/hashicorp/tester").
Assert(lib.MustSucceed(), lib.Grep("Successfully installed plugin", lib.GrepStdout))
Assert(check.MustSucceed(), check.Grep("Successfully installed plugin", check.GrepStdout))
})
ts.Run("metadata plugin installed must not have metadata in its path", func() {
ts.PackerCommand().UsePluginDir(tempPluginDir).
SetArgs("plugins", "installed").
Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1.0.0[^+]", lib.GrepStdout))
Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v1.0.0[^+]", check.GrepStdout))
})
ts.Run("plugin with metadata should work with validate", func() {
ts.PackerCommand().UsePluginDir(tempPluginDir).
SetArgs("validate", "./templates/simple.pkr.hcl").
Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", lib.GrepStderr))
Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", check.GrepStderr))
})
ts.Run("plugin with metadata should work with build", func() {
ts.PackerCommand().UsePluginDir(tempPluginDir).
SetArgs("build", "./templates/simple.pkr.hcl").
Assert(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", lib.GrepStderr))
Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v1.0.0[^+][^\\n]+plugin:", check.GrepStderr))
})
}
@ -38,22 +38,22 @@ func (ts *PackerPluginTestSuite) TestInstallPluginWithPath() {
ts.Run("install plugin with pre-release only", func() {
ts.PackerCommand().UsePluginDir(tempPluginDir).
SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.0-dev"), "github.com/hashicorp/tester").
Assert(lib.MustSucceed(), lib.Grep("Successfully installed plugin", lib.GrepStdout))
Assert(check.MustSucceed(), check.Grep("Successfully installed plugin", check.GrepStdout))
})
ts.Run("install same plugin with pre-release + metadata", func() {
ts.PackerCommand().UsePluginDir(tempPluginDir).
SetArgs("plugins", "install", "--path", ts.GetPluginPath(ts.T(), "1.0.0-dev+metadata"), "github.com/hashicorp/tester").
Assert(lib.MustSucceed(), lib.Grep("Successfully installed plugin", lib.GrepStdout))
Assert(check.MustSucceed(), check.Grep("Successfully installed plugin", check.GrepStdout))
})
ts.Run("list plugins, should only report one plugin", func() {
ts.PackerCommand().UsePluginDir(tempPluginDir).
SetArgs("plugins", "installed").
Assert(lib.MustSucceed(),
lib.Grep("plugin-tester_v1.0.0-dev[^+]", lib.GrepStdout),
lib.GrepInverted("plugin-tester_v1.0.0-dev\\+", lib.GrepStdout),
lib.LineCountCheck(1))
Assert(check.MustSucceed(),
check.Grep("plugin-tester_v1.0.0-dev[^+]", check.GrepStdout),
check.GrepInverted("plugin-tester_v1.0.0-dev\\+", check.GrepStdout),
check.LineCountCheck(1))
})
}
@ -66,7 +66,7 @@ func (ts *PackerPluginTestSuite) 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(lib.MustFail(), lib.Grep("Packer can only install plugin releases with this command", lib.GrepStdout))
Assert(check.MustFail(), check.Grep("Packer can only install plugin releases with this command", check.GrepStdout))
})
}
@ -79,7 +79,7 @@ func (ts *PackerPluginTestSuite) 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(lib.MustSucceed())
Assert(check.MustSucceed())
})
}
@ -92,15 +92,15 @@ func (ts *PackerPluginTestSuite) 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(lib.MustFail(),
lib.Grep("Invalid version constraint", lib.GrepStdout),
lib.Grep("Unsupported prerelease for constraint", lib.GrepStdout))
Assert(check.MustFail(),
check.Grep("Invalid version constraint", check.GrepStdout),
check.Grep("Unsupported prerelease for constraint", check.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(lib.MustFail(),
lib.Grep("Unsupported prerelease for constraint", lib.GrepStdout))
Assert(check.MustFail(),
check.Grep("Unsupported prerelease for constraint", check.GrepStdout))
})
}

@ -6,7 +6,8 @@ import (
"os"
"path/filepath"
"github.com/hashicorp/packer/packer_test/lib"
"github.com/hashicorp/packer/packer_test/common"
"github.com/hashicorp/packer/packer_test/common/check"
)
func (ts *PackerPluginTestSuite) TestLoadingOrder() {
@ -41,7 +42,7 @@ func (ts *PackerPluginTestSuite) TestLoadingOrder() {
ts.PackerCommand().
SetArgs(command, tt.templatePath).
UsePluginDir(pluginDir).
Assert(lib.MustSucceed(), lib.Grep(tt.grepStr))
Assert(check.MustSucceed(), check.Grep(tt.grepStr))
})
}
}
@ -53,45 +54,45 @@ func (ts *PackerPluginTestSuite) TestLoadWithLegacyPluginName() {
plugin := ts.GetPluginPath(ts.T(), "1.0.10")
lib.CopyFile(ts.T(), filepath.Join(pluginDir, "packer-plugin-tester"), plugin)
common.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(lib.MustFail(),
lib.Grep("Did you run packer init for this project", lib.GrepStdout),
lib.Grep("following plugins are required", lib.GrepStdout))
Assert(check.MustFail(),
check.Grep("Did you run packer init for this project", check.GrepStdout),
check.Grep("following plugins are required", check.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(lib.MustFail(),
lib.Grep("The builder tester-dynamic is unknown by Packer", lib.GrepStdout))
Assert(check.MustFail(),
check.Grep("The builder tester-dynamic is unknown by Packer", check.GrepStdout))
})
})
pluginDir, cleanup = ts.MakePluginDir("1.0.0")
defer cleanup()
lib.CopyFile(ts.T(), filepath.Join(pluginDir, "packer-plugin-tester"), plugin)
common.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(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", lib.GrepStderr))
Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", check.GrepStderr))
})
wd, cleanup := lib.TempWorkdir(ts.T(), "./templates/simple.pkr.hcl")
wd, cleanup := common.TempWorkdir(ts.T(), "./templates/simple.pkr.hcl")
defer cleanup()
lib.CopyFile(ts.T(), filepath.Join(wd, "packer-plugin-tester"), plugin)
common.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(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", lib.GrepStderr))
Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v1\\.0\\.0[^\\n]+ plugin:", check.GrepStderr))
})
}
@ -102,35 +103,35 @@ func (ts *PackerPluginTestSuite) TestLoadWithSHAMismatches() {
pluginDir, cleanup := ts.MakePluginDir("1.0.9")
defer cleanup()
pluginDestName := lib.ExpectedInstalledName("1.0.10")
pluginDestName := common.ExpectedInstalledName("1.0.10")
lib.CopyFile(ts.T(), filepath.Join(pluginDir, "github.com", "hashicorp", "tester", pluginDestName), plugin)
common.CopyFile(ts.T(), filepath.Join(pluginDir, "github.com", "hashicorp", "tester", pluginDestName), plugin)
ts.PackerCommand().UsePluginDir(pluginDir).
SetArgs("plugins", "installed").
Assert(lib.MustSucceed(),
lib.Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", lib.GrepStdout),
lib.GrepInverted("packer-plugin-tester_v1.0.10", lib.GrepStdout),
lib.Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", lib.GrepStderr))
Assert(check.MustSucceed(),
check.Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", check.GrepStdout),
check.GrepInverted("packer-plugin-tester_v1.0.10", check.GrepStdout),
check.Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", check.GrepStderr))
})
ts.Run("move plugin with right name, invalid SHA256SUM, should reject", func() {
pluginDir, cleanup := ts.MakePluginDir("1.0.9")
defer cleanup()
pluginDestName := lib.ExpectedInstalledName("1.0.10")
lib.CopyFile(ts.T(), filepath.Join(pluginDir, "github.com", "hashicorp", "tester", pluginDestName), plugin)
lib.WriteFile(ts.T(),
pluginDestName := common.ExpectedInstalledName("1.0.10")
common.CopyFile(ts.T(), filepath.Join(pluginDir, "github.com", "hashicorp", "tester", pluginDestName), plugin)
common.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(lib.MustSucceed(),
lib.Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", lib.GrepStdout),
lib.GrepInverted("packer-plugin-tester_v1.0.10", lib.GrepStdout),
lib.Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", lib.GrepStderr),
lib.Grep(`Checksums \(\*sha256\.digest\) did not match.`, lib.GrepStderr))
Assert(check.MustSucceed(),
check.Grep("packer-plugin-tester_v1\\.0\\.9[^\\n]+", check.GrepStdout),
check.GrepInverted("packer-plugin-tester_v1.0.10", check.GrepStdout),
check.Grep("v1.0.10[^\\n]+ignoring possibly unsafe binary", check.GrepStderr),
check.Grep(`Checksums \(\*sha256\.digest\) did not match.`, check.GrepStderr))
})
}
@ -145,13 +146,13 @@ func (ts *PackerPluginTestSuite) TestPluginPathEnvvarWithMultiplePaths() {
ts.Run("load plugin with two dirs - not supported anymore, should error", func() {
ts.PackerCommand().UsePluginDir(pluginDirVal).
SetArgs("plugins", "installed").
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)))
Assert(check.MustFail(),
check.Grep("Multiple paths are no longer supported for PACKER_PLUGIN_PATH"),
check.MkPipeCheck("All envvars are suggested",
check.PipeGrep(`\* PACKER_PLUGIN_PATH=`),
check.LineCount()).
SetStream(check.OnlyStderr).
SetTester(check.IntCompare(check.Eq, 2)))
})
}
@ -159,7 +160,7 @@ func (ts *PackerPluginTestSuite) TestInstallNonCanonicalPluginVersion() {
pluginPath, cleanup := ts.MakePluginDir()
defer cleanup()
lib.ManualPluginInstall(ts.T(),
common.ManualPluginInstall(ts.T(),
filepath.Join(pluginPath, "github.com", "hashicorp", "tester"),
ts.GetPluginPath(ts.T(), "1.0.10"),
"001.00.010")
@ -167,9 +168,9 @@ func (ts *PackerPluginTestSuite) TestInstallNonCanonicalPluginVersion() {
ts.Run("try listing plugins with non-canonical version installed - report none", func() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "installed").
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))
Assert(check.MustSucceed(),
check.Grep(`version .* in path is non canonical`, check.GrepStderr),
check.MkPipeCheck("no output in stdout").SetTester(check.ExpectEmptyInput()).SetStream(check.OnlyStdout))
})
}
@ -177,7 +178,7 @@ func (ts *PackerPluginTestSuite) TestLoadPluginWithMetadataInName() {
pluginPath, cleanup := ts.MakePluginDir()
defer cleanup()
lib.ManualPluginInstall(ts.T(),
common.ManualPluginInstall(ts.T(),
filepath.Join(pluginPath, "github.com", "hashicorp", "tester"),
ts.GetPluginPath(ts.T(), "1.0.10+metadata"),
"1.0.10+metadata")
@ -185,9 +186,9 @@ func (ts *PackerPluginTestSuite) TestLoadPluginWithMetadataInName() {
ts.Run("try listing plugins with metadata in name - report none", func() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "installed").
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))
Assert(check.MustSucceed(),
check.Grep("found version .* with metadata in the name", check.GrepStderr),
check.MkPipeCheck("no output in stdout").SetTester(check.ExpectEmptyInput()).SetStream(check.OnlyStdout))
})
}
@ -199,15 +200,15 @@ func (ts *PackerPluginTestSuite) 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(lib.MustSucceed(),
lib.Grep("packer-plugin-tester_v1.0.1-dev.*: plugin process exited", lib.GrepStderr))
Assert(check.MustSucceed(),
check.Grep("packer-plugin-tester_v1.0.1-dev.*: plugin process exited", check.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(lib.MustSucceed(),
lib.Grep("packer-plugin-tester_v1.0.0.*: plugin process exited", lib.GrepStderr))
Assert(check.MustSucceed(),
check.Grep("packer-plugin-tester_v1.0.0.*: plugin process exited", check.GrepStderr))
})
}
}
@ -216,7 +217,7 @@ func (ts *PackerPluginTestSuite) TestWithLegacyConfigAndComponents() {
pluginDir, cleanup := ts.MakePluginDir("1.0.0")
defer cleanup()
workdir, cleanup := lib.TempWorkdir(ts.T(), "./sample_config.json", "./templates/simple.json", "./templates/simple.pkr.hcl")
workdir, cleanup := common.TempWorkdir(ts.T(), "./sample_config.json", "./templates/simple.json", "./templates/simple.pkr.hcl")
defer cleanup()
for _, cmd := range []string{"validate", "build"} {
@ -224,18 +225,18 @@ func (ts *PackerPluginTestSuite) TestWithLegacyConfigAndComponents() {
ts.PackerCommand().UsePluginDir(pluginDir).SetWD(workdir).
SetArgs(cmd, "simple.json").
AddEnv("PACKER_CONFIG", filepath.Join(workdir, "sample_config.json")).
Assert(lib.MustFail(),
lib.Grep("Your configuration file describes some legacy components", lib.GrepStderr),
lib.Grep("packer-provisioner-super-shell", lib.GrepStderr))
Assert(check.MustFail(),
check.Grep("Your configuration file describes some legacy components", check.GrepStderr),
check.Grep("packer-provisioner-super-shell", check.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(lib.MustFail(),
lib.Grep("Your configuration file describes some legacy components", lib.GrepStderr),
lib.Grep("packer-provisioner-super-shell", lib.GrepStderr))
Assert(check.MustFail(),
check.Grep("Your configuration file describes some legacy components", check.GrepStderr),
check.Grep("packer-provisioner-super-shell", check.GrepStderr))
})
}
}

@ -4,7 +4,7 @@ import (
"path/filepath"
"strings"
"github.com/hashicorp/packer/packer_test/lib"
"github.com/hashicorp/packer/packer_test/common/check"
)
func (ts *PackerPluginTestSuite) TestPluginsRemoveWithSourceAddress() {
@ -19,10 +19,10 @@ func (ts *PackerPluginTestSuite) 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(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),
Assert(check.MustSucceed(),
check.Grep("packer-plugin-tester_v1.0.9", check.GrepStdout),
check.Grep("packer-plugin-tester_v1.0.10", check.GrepStdout),
check.Grep("packer-plugin-tester_v2.0.0", check.GrepStdout),
)
})
@ -35,8 +35,8 @@ func (ts *PackerPluginTestSuite) TestPluginsRemoveWithSourceAddress() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "remove", "github.com/hashicorp/testerONE").
Assert(
lib.MustFail(),
lib.Grep("No installed plugin found matching the plugin constraints github.com/hashicorp/testerONE"),
check.MustFail(),
check.Grep("No installed plugin found matching the plugin constraints github.com/hashicorp/testerONE"),
)
})
@ -44,8 +44,8 @@ func (ts *PackerPluginTestSuite) 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(
lib.MustFail(),
lib.Grep("The source URL must have at most 16 components"),
check.MustFail(),
check.Grep("The source URL must have at most 16 components"),
)
})
}
@ -62,17 +62,17 @@ func (ts *PackerPluginTestSuite) 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(lib.MustSucceed(), lib.Grep("packer-plugin-tester_v2.0.0", lib.GrepStdout))
Assert(check.MustSucceed(), check.Grep("packer-plugin-tester_v2.0.0", check.GrepStdout))
})
ts.Run("plugins installed after single plugins remove outputs remaining installed plugins", func() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "installed").
Assert(
lib.MustSucceed(),
lib.Grep("packer-plugin-tester_v1.0.9", lib.GrepStdout),
lib.Grep("packer-plugin-tester_v1.0.10", lib.GrepStdout),
lib.GrepInverted("packer-plugin-tester_v2.0.0", lib.GrepStdout),
check.MustSucceed(),
check.Grep("packer-plugin-tester_v1.0.9", check.GrepStdout),
check.Grep("packer-plugin-tester_v1.0.10", check.GrepStdout),
check.GrepInverted("packer-plugin-tester_v2.0.0", check.GrepStdout),
)
})
@ -96,18 +96,18 @@ func (ts *PackerPluginTestSuite) TestPluginsRemoveWithLocalPath() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "remove", plugins[0]).
Assert(
lib.MustSucceed(),
lib.Grep("packer-plugin-tester_v1.0.9", lib.GrepStdout),
lib.GrepInverted("packer-plugin-tester_v1.0.10", lib.GrepStdout),
check.MustSucceed(),
check.Grep("packer-plugin-tester_v1.0.9", check.GrepStdout),
check.GrepInverted("packer-plugin-tester_v1.0.10", check.GrepStdout),
)
})
ts.Run("plugins installed after calling plugins remove outputs remaining installed plugins", func() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "installed").
Assert(
lib.MustSucceed(),
lib.Grep("packer-plugin-tester_v1.0.10", lib.GrepStdout),
lib.GrepInverted("packer-plugin-tester_v1.0.9", lib.GrepStdout),
check.MustSucceed(),
check.Grep("packer-plugin-tester_v1.0.10", check.GrepStdout),
check.GrepInverted("packer-plugin-tester_v1.0.9", check.GrepStdout),
)
})
@ -120,8 +120,8 @@ func (ts *PackerPluginTestSuite) TestPluginsRemoveWithLocalPath() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "remove", filepath.Base(plugins[0])).
Assert(
lib.MustFail(),
lib.Grep("A source URL must at least contain a host and a path with 2 components", lib.GrepStdout),
check.MustFail(),
check.Grep("A source URL must at least contain a host and a path with 2 components", check.GrepStdout),
)
})
@ -129,8 +129,8 @@ func (ts *PackerPluginTestSuite) TestPluginsRemoveWithLocalPath() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "remove", ts.T().TempDir()).
Assert(
lib.MustFail(),
lib.Grep("is not under the plugin directory inferred by Packer", lib.GrepStdout),
check.MustFail(),
check.Grep("is not under the plugin directory inferred by Packer", check.GrepStdout),
)
})
}
@ -148,8 +148,8 @@ func (ts *PackerPluginTestSuite) TestPluginsRemoveWithNoArguments() {
ts.PackerCommand().UsePluginDir(pluginPath).
SetArgs("plugins", "remove").
Assert(
lib.MustFail(),
lib.Grep("Usage: packer plugins remove <plugin>", lib.GrepStdout),
check.MustFail(),
check.Grep("Usage: packer plugins remove <plugin>", check.GrepStdout),
)
})
@ -165,7 +165,7 @@ func InstalledPlugins(ts *PackerPluginTestSuite, dir string) []string {
cmd := ts.PackerCommand().UsePluginDir(dir).
SetArgs("plugins", "installed")
cmd.Assert(lib.MustSucceed())
cmd.Assert(check.MustSucceed())
if ts.T().Failed() {
ts.T().Fatalf("Failed to execute plugin installed for %q", dir)
}

@ -3,16 +3,16 @@ package plugin_tests
import (
"testing"
"github.com/hashicorp/packer/packer_test/lib"
"github.com/hashicorp/packer/packer_test/common"
"github.com/stretchr/testify/suite"
)
type PackerPluginTestSuite struct {
*lib.PackerTestSuite
*common.PackerTestSuite
}
func Test_PackerPluginSuite(t *testing.T) {
baseSuite, cleanup := lib.InitBaseSuite(t)
baseSuite, cleanup := common.InitBaseSuite(t)
defer cleanup()
baseSuite.CompileTestPluginVersions(t,
"1.0.0",

Loading…
Cancel
Save