This commit is contained in:
Lucas Bajolet 2026-04-28 16:28:04 -07:00 committed by GitHub
commit 25c77e851d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View file

@ -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

View file

@ -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++