Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packer_test/common/check/gadgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions packer_test/common/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
//
Expand Down Expand Up @@ -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++
Expand Down