diff --git a/packer_test/common/check/gadgets.go b/packer_test/common/check/gadgets.go index 9eecc57cfeb..ec8e53fd3af 100644 --- a/packer_test/common/check/gadgets.go +++ b/packer_test/common/check/gadgets.go @@ -195,10 +195,24 @@ func (d dump) Check(stdout, stderr string, err error) error { return nil } +func DumpCommand(t *testing.T, command string) Checker { + return &dumpCommand{t, command} +} + +type dumpCommand struct { + t *testing.T + command string +} + +func (d dumpCommand) Check(_, _ string, _ error) error { + d.t.Logf("ran command %s", d.command) + return nil +} + type PanicCheck struct{} func (_ PanicCheck) Check(stdout, stderr string, _ error) error { - if strings.Contains(stdout, "= PACKER CRASH =") || strings.Contains(stderr, "= PACKER CRASH =") { + 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 diff --git a/packer_test/common/commands.go b/packer_test/common/commands.go index 869a6b27811..c9bfd311966 100644 --- a/packer_test/common/commands.go +++ b/packer_test/common/commands.go @@ -22,6 +22,7 @@ type packerCommand struct { err error t *testing.T fatalfAssert bool + dump bool } // PackerCommand creates a skeleton of packer command with the ability to execute gadgets on the outputs of the command. @@ -125,6 +126,26 @@ func (pc *packerCommand) SetAssertFatal() *packerCommand { return pc } +// Dump enables a verbose mode for the test, when Assert is called, the test +// proceeds normally, and the command-line that was invoked, along with the +// contents of stdout/stderr will also be output in addition to the test +// results. This is mostly useful for debugging a test. +func (pc *packerCommand) Dump() *packerCommand { + pc.dump = true + return pc +} + +func (pc *packerCommand) commandString() string { + buf := &strings.Builder{} + + fmt.Fprintf(buf, "%q", pc.packerPath) + for _, arg := range pc.args { + fmt.Fprintf(buf, " %q", arg) + } + + return buf.String() +} + // Run executes the packer command with the args/env requested and returns the // output streams (stdout, stderr) // @@ -182,6 +203,15 @@ func (pc *packerCommand) Output() (string, string, error) { } func (pc *packerCommand) Assert(checks ...check.Checker) { + if pc.dump { + tmpChecks := []check.Checker{ + check.DumpCommand(pc.t, pc.commandString()), + check.Dump(pc.t), + } + + checks = append(tmpChecks, checks...) + } + attempt := 0 for pc.runs > 0 { attempt++