Skip to content

Commit

Permalink
only allow processes input parameter if pytest-xdist installed
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Mar 4, 2024
1 parent 0689b25 commit d024ed8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 1.2.3 (Next)
- Improve communication backend formatting.
- Only allow `processes` input parameter if `pytest-xdist` installed.

### 1.2.2
- Logging updates, code optimization, go version increments, and dependency updates.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ build {
| **keyword** | PyTest keyword substring expression for selective test execution. | string | "" | no |
| **local** | Execute Testinfra tests locally on the instance used for building the machine image artifact. Most plugin validation is skipped with this option. | bool | false | no |
| **marker** | PyTest marker expression for selective test execution. | string | "" | no |
| **processes** | The number of parallel processes for Testinfra test execution. | number | 0 | no |
| **processes** | The number of parallel processes for Testinfra test execution. This parameter requires installation of the [pytest-xdist](https://pypi.org/project/pytest-xdist/) plugin. | number | 0 | no |
| **pytest_path** | The path to the installed `py.test` executable for initiating the Testinfra tests. | string | "py.test" | no |
| **sudo** | Whether or not to execute the tests with `sudo` elevated permissions. | bool | false | no |
| **test_files** | The paths to the files containing the Testinfra tests for execution and validation of the machine image artifact. The default empty value will execute default PyTest behavior of all test files prefixed with `test_` recursively discovered from the current working directory. | list(string) | [] | no |
Expand Down
18 changes: 14 additions & 4 deletions provisioner/testinfra.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func (provisioner *Provisioner) Prepare(raws ...interface{}) error {
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, " "))
}

if provisioner.config.Processes > 0 {
log.Printf("number of Testinfra processes: %d", provisioner.config.Processes)
}
} else { // verify testinfra installed
log.Print("beginning Testinfra installation verification")
// initialize testinfra -h command
Expand Down Expand Up @@ -113,16 +117,22 @@ func (provisioner *Provisioner) Prepare(raws ...interface{}) error {
// 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
}
}
}

if len(provisioner.config.Marker) > 0 {
log.Printf("executing tests with marker expression: %s", provisioner.config.Marker)
}

if provisioner.config.Processes > 0 {
log.Printf("number of Testinfra processes: %d", provisioner.config.Processes)
}

if provisioner.config.Sudo {
log.Print("testinfra will execute with sudo")
} else {
Expand Down

0 comments on commit d024ed8

Please sign in to comment.