Skip to content

Commit

Permalink
packer_test: check for a panic during execution
Browse files Browse the repository at this point in the history
When a command is run, it is the expectation that no test should make
Packer panic. If it did, something is wrong and Packer should be fixed
so it doesn't panic anymore in that situation.

The way we did the check before was adding a PanicCheck after the
command ran, so we could make sure of that during `Assert`.

However, since we introduced the possibility to have multiple runs,
having this addition as part of the run loop meant that the PanicCheck
would be run as many times as there were runs.

While this worked, this implied that we'd do the same check multiple
times on a single command output, which is not optimal.

Instead, this commit moves the check to within the `Run` function, this
way for each run of the command we do the check once, and then we can
assert the results of the command on what output it produced.
  • Loading branch information
lbajolet-hashicorp committed Jun 17, 2024
1 parent 8c134ba commit 7823d15
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packer_test/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ func (pc *packerCommand) Run() (string, string, error) {

pc.err = cmd.Run()

// Check that the command didn't panic, and if it did, we can immediately error
panicErr := PanicCheck{}.Check(pc.stdout.String(), pc.stderr.String(), pc.err)
if panicErr != nil {
pc.t.Fatalf("Packer panicked during execution: %s", panicErr)
}

return pc.stdout.String(), pc.stderr.String(), pc.err
}

Expand All @@ -147,8 +153,6 @@ func (pc *packerCommand) Assert(checks ...Checker) {
attempt++
stdout, stderr, err := pc.Run()

checks = append(checks, PanicCheck{})

for _, check := range checks {
checkErr := check.Check(stdout, stderr, err)
if checkErr != nil {
Expand Down

0 comments on commit 7823d15

Please sign in to comment.