Skip to content

Commit

Permalink
prevent negative processes, and move test file arg to end
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Mar 4, 2024
1 parent d024ed8 commit 8ef8cdf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
24 changes: 14 additions & 10 deletions provisioner/testinfra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions provisioner/testinfra_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion provisioner/testinfra_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8ef8cdf

Please sign in to comment.