From fde79cc66797d3e87b98e913616798b0c7c32a5e Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 3 May 2024 15:28:19 -0400 Subject: [PATCH] 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. --- test/commands_test.go | 10 ++++------ test/gadgets_test.go | 10 ++++++++++ test/plugin_test.go | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/test/commands_test.go b/test/commands_test.go index d35dcafc0..24561f21e 100644 --- a/test/commands_test.go +++ b/test/commands_test.go @@ -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) diff --git a/test/gadgets_test.go b/test/gadgets_test.go index 33412b9ae..abb2ead3b 100644 --- a/test/gadgets_test.go +++ b/test/gadgets_test.go @@ -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 +} diff --git a/test/plugin_test.go b/test/plugin_test.go index 778941a0e..b5aa3990d 100644 --- a/test/plugin_test.go +++ b/test/plugin_test.go @@ -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