packer_test: hide run and introduce Output

When using a PackerCommand, the Run function was made public as a way to
access the contents of an execution.

This was clumsy as it had too many responsabilities, and was not needed
strictly as Assert was performing the executions, as many times as
required.

This could introduce cases in which one run as spent by the caller, then
the remainder were executed through Assert.

Therefore, we change this convention.

Now, run is private to the type, and only through Assert can a command
be executed.
If a test needs access to a command's output, stderr, or error, it can
do so through the Output function, which requires Assert to be called
first.
pull/13163/head
Lucas Bajolet 2 years ago committed by Lucas Bajolet
parent 71169416db
commit 31b6109430

@ -125,7 +125,7 @@ func (pc *packerCommand) SetAssertFatal() *packerCommand {
// Note: while originally "Run" was designed to be idempotent, with the
// introduction of multiple runs for a command, this is not the case anymore
// and the function should not be considered thread-safe anymore.
func (pc *packerCommand) Run() (string, string, error) {
func (pc *packerCommand) run() (string, string, error) {
if pc.runs <= 0 {
return pc.stdout.String(), pc.stderr.String(), pc.err
}
@ -160,11 +160,26 @@ func (pc *packerCommand) Run() (string, string, error) {
return pc.stdout.String(), pc.stderr.String(), pc.err
}
// Output returns the results of the latest Run that was executed.
//
// In general there is only one run of the command, but as it can be changed
// through the Runs function, only the latest run will be returned.
//
// If the command was not run (through Assert), this will make the test fail
// immediately.
func (pc *packerCommand) Output() (string, string, error) {
if pc.runs > 0 {
pc.t.Fatalf("command was not run, invoke Assert first, then Output.")
}
return pc.stdout.String(), pc.stderr.String(), pc.err
}
func (pc *packerCommand) Assert(checks ...check.Checker) {
attempt := 0
for pc.runs > 0 {
attempt++
stdout, stderr, err := pc.Run()
stdout, stderr, err := pc.run()
for _, checker := range checks {
checkErr := checker.Check(stdout, stderr, err)

@ -131,6 +131,9 @@ func (ts *PackerTestSuite) CompilePlugin(t *testing.T, versionString string) {
// packer will be able to use that directory for running its functions.
//
// Deletion of the directory is the caller's responsibility.
//
// Note: all of the plugin versions specified to be installed in this plugin directory
// must have been compiled beforehand.
func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDir string, cleanup func()) {
t := ts.T()
@ -158,7 +161,7 @@ func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDi
path := ts.GetPluginPath(t, pluginVersion)
cmd := ts.PackerCommand().SetArgs("plugins", "install", "--path", path, "github.com/hashicorp/tester").AddEnv("PACKER_PLUGIN_PATH", pluginTempDir)
cmd.Assert(check.MustSucceed())
out, stderr, cmdErr := cmd.Run()
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)
return

@ -168,7 +168,7 @@ func InstalledPlugins(ts *PackerPluginTestSuite, dir string) []string {
SetAssertFatal()
cmd.Assert(check.MustSucceed())
out, _, _ := cmd.Run()
out, _, _ := cmd.Output()
// Output will be split on '\n' after trimming all other white space
out = strings.TrimSpace(out)
plugins := strings.Fields(out)

Loading…
Cancel
Save