From 48a0172670990bc883134181a9e44f19ef5d8ec5 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 8 Dec 2020 09:59:04 -0800 Subject: [PATCH] better error handling and messaging in provisioner acc test framework --- .../acctest/provisioneracc/provisioners.go | 15 +++++++++++---- provisioner/shell-local/provisioner_acc_test.go | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packer-plugin-sdk/acctest/provisioneracc/provisioners.go b/packer-plugin-sdk/acctest/provisioneracc/provisioners.go index 39c7aa073..5a6292ae4 100644 --- a/packer-plugin-sdk/acctest/provisioneracc/provisioners.go +++ b/packer-plugin-sdk/acctest/provisioneracc/provisioners.go @@ -206,8 +206,13 @@ func TestProvisionersAgainstBuilders(testCase *ProvisionerTestCase, t *testing.T writeJsonTemplate(out, templatePath, t) logfile := fmt.Sprintf("packer_log_%s_%s.txt", builderType, testCase.Type) + // Make sure packer is installed: + packerbin, err := exec.LookPath("packer") + if err != nil { + t.Fatalf("Couldn't find packer binary installed on system: %s", err.Error()) + } // Run build - buildCommand := exec.Command("packer", "build", "--machine-readable", templatePath) + buildCommand := exec.Command(packerbin, "build", "--machine-readable", templatePath) buildCommand.Env = append(buildCommand.Env, os.Environ()...) buildCommand.Env = append(buildCommand.Env, "PACKER_LOG=1", fmt.Sprintf("PACKER_LOG_PATH=%s", logfile)) @@ -234,10 +239,12 @@ func TestProvisionersAgainstBuilders(testCase *ProvisionerTestCase, t *testing.T // Fail test if check failed. if checkErr != nil { - t.Fatalf(fmt.Sprint("Error running provisioner acceptance"+ - " tests: %s\nLogs can be found at %s and the "+ + cwd, _ := os.Getwd() + t.Fatalf(fmt.Sprintf("Error running provisioner acceptance"+ + " tests: %s\nLogs can be found at %s\nand the "+ "acceptance test template can be found at %s", - checkErr.Error(), logfile, templatePath)) + checkErr.Error(), filepath.Join(cwd, logfile), + filepath.Join(cwd, templatePath))) } else { os.Remove(templatePath) os.Remove(logfile) diff --git a/provisioner/shell-local/provisioner_acc_test.go b/provisioner/shell-local/provisioner_acc_test.go index 1ecf47c09..c3998694e 100644 --- a/provisioner/shell-local/provisioner_acc_test.go +++ b/provisioner/shell-local/provisioner_acc_test.go @@ -58,7 +58,7 @@ func TestAccShellProvisioner_basic(t *testing.T) { Check: func(buildcommand *exec.Cmd, logfile string) error { if buildcommand.ProcessState != nil { if buildcommand.ProcessState.ExitCode() != 0 { - return fmt.Errorf("Bad exit code. Logfile: %s", logfile) + return fmt.Errorf("Bad exit code. Logfile: %s\n", logfile) } } filecontents, err := loadFile("file.txt")