From d024ed8c349daa4b6520e8276e63a9a46264dbed Mon Sep 17 00:00:00 2001 From: Matt Schuchard Date: Mon, 4 Mar 2024 10:36:18 -0500 Subject: [PATCH] only allow `processes` input parameter if `pytest-xdist` installed --- CHANGELOG.md | 1 + README.md | 2 +- provisioner/testinfra.go | 18 ++++++++++++++---- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c75f812..9e81162 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index e29b148..fadc7a4 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/provisioner/testinfra.go b/provisioner/testinfra.go index da23ea5..ac69da0 100644 --- a/provisioner/testinfra.go +++ b/provisioner/testinfra.go @@ -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 @@ -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 {