test: make PanicCheck an implicit Assert gadget

When running "Assert" on a packer command, we run a series of checkers
on the command's output/error code, which provoke test failures if they
fail.

Panic checking used to be part of Run, but in the end this would make
more sense to have that as a regular checker if asserting the results of
a packer command, so that's the approach we adopt with this commit.
pull/13032/head
Lucas Bajolet 2 years ago committed by Lucas Bajolet
parent 1e7f29f7c7
commit fde79cc667

@ -53,13 +53,9 @@ func (pc *packerCommand) AddEnv(key, val string) *packerCommand {
//
// Note: "Run" will only execute the command once, and return the streams and
// error from the only execution for every subsequent call
func (pc *packerCommand) Run(t *testing.T) (string, string, error) {
func (pc *packerCommand) Run() (string, string, error) {
pc.once.Do(pc.doRun)
if strings.Contains(pc.stdout.String(), "PACKER CRASH") || strings.Contains(pc.stderr.String(), "PACKER CRASH") {
t.Fatalf("Packer has crashed while running the following command: packer %#v", pc.args)
}
return pc.stdout.String(), pc.stderr.String(), pc.err
}
@ -75,7 +71,9 @@ func (pc *packerCommand) doRun() {
}
func (pc *packerCommand) Assert(t *testing.T, checks ...Checker) {
stdout, stderr, err := pc.Run(t)
stdout, stderr, err := pc.Run()
checks = append(checks, PanicCheck{})
for _, check := range checks {
checkErr := check.Check(stdout, stderr, err)

@ -4,6 +4,7 @@ import (
"fmt"
"reflect"
"regexp"
"strings"
"testing"
)
@ -117,3 +118,12 @@ func (d Dump) Check(stdout, stderr string, err error) error {
d.t.Logf("stderr: %s", stderr)
return nil
}
type PanicCheck struct{}
func (_ PanicCheck) Check(stdout, stderr string, _ error) error {
if strings.Contains(stdout, "= PACKER CRASH =") || strings.Contains(stderr, "= PACKER CRASH =") {
return fmt.Errorf("packer has crashed: this is never normal and should be investigated")
}
return nil
}

@ -161,7 +161,7 @@ func (ts *PackerTestSuite) MakePluginDir(pluginVersions ...string) (pluginTempDi
path, _ := LoadPluginVersion(pluginVersion)
cmd := ts.PackerCommand().SetArgs("plugins", "install", "--path", path, "github.com/hashicorp/tester").AddEnv("PACKER_PLUGIN_PATH", pluginTempDir)
cmd.Assert(t, MustSucceed{})
out, stderr, cmdErr := cmd.Run(t)
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

Loading…
Cancel
Save