From 8ef8cdf1793440522c888f218919aa0621476353 Mon Sep 17 00:00:00 2001 From: Matt Schuchard Date: Mon, 4 Mar 2024 10:47:55 -0500 Subject: [PATCH] prevent negative processes, and move test file arg to end --- provisioner/testinfra.go | 24 ++++++++++++++---------- provisioner/testinfra_command.go | 6 +++--- provisioner/testinfra_command_test.go | 2 +- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/provisioner/testinfra.go b/provisioner/testinfra.go index ac69da0..6496c16 100644 --- a/provisioner/testinfra.go +++ b/provisioner/testinfra.go @@ -71,12 +71,14 @@ func (provisioner *Provisioner) Prepare(raws ...interface{}) error { } if provisioner.config.Local { + // no validation of testinfra installation log.Print("test execution will occur on the temporary Packer instance used for building the machine image artifact") if len(provisioner.config.InstallCmd) > 0 { log.Printf("installation command on the temporary Packer instance prior to Testinfra test execution is: %s", strings.Join(provisioner.config.InstallCmd, " ")) } + // no validation of xdist installation if provisioner.config.Processes > 0 { log.Printf("number of Testinfra processes: %d", provisioner.config.Processes) } @@ -113,26 +115,28 @@ func (provisioner *Provisioner) Prepare(raws ...interface{}) error { } else { return fmt.Errorf("testinfra installation not found by specified Pytest installation") } + + if provisioner.config.Processes > 0 { + // check for xdist in pytest usage stdout + if strings.Contains(string(outSlurp), " -n ") { + log.Printf("number of Testinfra processes: %d", provisioner.config.Processes) + } else { + log.Printf("pytest-xdist is not installed, and processes parameter will be reset to default") + provisioner.config.Processes = 0 + } + } } else { // pytest returned no stdout return fmt.Errorf("pytest help command returned no stdout; this indicates an issue with the specified Pytest installation") } - - if provisioner.config.Processes > 0 { - // check for xdist in pytest usage stdout - if strings.Contains(string(outSlurp), " -n ") { - log.Printf("number of Testinfra processes: %d", provisioner.config.Processes) - } else { - log.Printf("pytest-xdist is not installed, and processes parameter will be reset to default") - provisioner.config.Processes = 0 - } - } } + // marker parameter if len(provisioner.config.Marker) > 0 { log.Printf("executing tests with marker expression: %s", provisioner.config.Marker) } + // sudo parameter if provisioner.config.Sudo { log.Print("testinfra will execute with sudo") } else { diff --git a/provisioner/testinfra_command.go b/provisioner/testinfra_command.go index 5437ebf..a6c7087 100644 --- a/provisioner/testinfra_command.go +++ b/provisioner/testinfra_command.go @@ -156,8 +156,6 @@ func (provisioner *Provisioner) determineExecCmd() (*exec.Cmd, *packer.RemoteCmd } // assign optional populated values - // testfiles - args = append(args, provisioner.config.TestFiles...) // keyword keyword, err := interpolate.Render(provisioner.config.Keyword, &provisioner.config.ctx) if err != nil { @@ -177,13 +175,15 @@ func (provisioner *Provisioner) determineExecCmd() (*exec.Cmd, *packer.RemoteCmd args = append(args, "-m", fmt.Sprintf("\"%s\"", marker)) } // processes - if provisioner.config.Processes != 0 { + if provisioner.config.Processes > 0 { args = append(args, "-n", strconv.Itoa(provisioner.config.Processes)) } // sudo if provisioner.config.Sudo { args = append(args, "--sudo") } + // testfiles + args = append(args, provisioner.config.TestFiles...) // return packer remote command for local testing on instance if localExec { diff --git a/provisioner/testinfra_command_test.go b/provisioner/testinfra_command_test.go index 9e238b9..c12990f 100644 --- a/provisioner/testinfra_command_test.go +++ b/provisioner/testinfra_command_test.go @@ -50,7 +50,7 @@ func TestProvisionerDetermineExecCmd(test *testing.T) { if err != nil { test.Errorf("determineExecCmd function failed to determine execution command for basic config with SSH communicator: %s", err) } - if execCmd.String() != fmt.Sprintf("%s -v --hosts=%s@%s:%d --ssh-identity-file=%s --ssh-extra-args=\"-o StrictHostKeyChecking=no\" %s -k \"%s\" -m \"%s\" -n %d --sudo", provisioner.config.PytestPath, generatedData["User"], generatedData["Host"], generatedData["Port"], generatedData["SSHPrivateKeyFile"], strings.Join(provisioner.config.TestFiles, ""), provisioner.config.Keyword, provisioner.config.Marker, provisioner.config.Processes) { + if execCmd.String() != fmt.Sprintf("%s -v --hosts=%s@%s:%d --ssh-identity-file=%s --ssh-extra-args=\"-o StrictHostKeyChecking=no\" -k \"%s\" -m \"%s\" -n %d --sudo %s", provisioner.config.PytestPath, generatedData["User"], generatedData["Host"], generatedData["Port"], generatedData["SSHPrivateKeyFile"], provisioner.config.Keyword, provisioner.config.Marker, provisioner.config.Processes, strings.Join(provisioner.config.TestFiles, "")) { test.Errorf("determineExecCmd function failed to properly determine remote execution command for basic config with SSH communicator: %s", execCmd.String()) } if localCmd != nil {