diff --git a/lib/ansible/modules/windows/win_pester.ps1 b/lib/ansible/modules/windows/win_pester.ps1 index ae32d90dc50..5515d0ccb96 100644 --- a/lib/ansible/modules/windows/win_pester.ps1 +++ b/lib/ansible/modules/windows/win_pester.ps1 @@ -15,6 +15,7 @@ $diff_mode = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -d $path = Get-AnsibleParam -obj $params -name "path" -type "str" -failifempty $true $tags = Get-AnsibleParam -obj $params -name "tags" -type "list" +$test_parameters = Get-AnsibleParam -obj $params -name "test_parameters" -type "dict" $minimum_version = Get-AnsibleParam -obj $params -name "minimum_version" -type "str" -failifempty $false $result = @{ @@ -78,11 +79,19 @@ if($tags.count){ } # Run Pester tests If (Test-Path -LiteralPath $path -PathType Leaf) { + $test_parameters_check_mode_msg = '' + if ($test_parameters.keys.count) { + $Parameters.Script = @{Path = $Path ; Parameters = $test_parameters } + $test_parameters_check_mode_msg = " with $($test_parameters.keys -join ',') parameters" + } + else { + $Parameters.Script = $Path + } if ($check_mode) { - $result.output = "Run pester test in the file: $path" + $result.output = "Run pester test in the file: $path$test_parameters_check_mode_msg" } else { try { - $result.output = Invoke-Pester $path @Parameters + $result.output = Invoke-Pester @Parameters } catch { Fail-Json -obj $result -message $_.Exception } diff --git a/lib/ansible/modules/windows/win_pester.py b/lib/ansible/modules/windows/win_pester.py index 49f6c39d3ac..87b718c3d94 100644 --- a/lib/ansible/modules/windows/win_pester.py +++ b/lib/ansible/modules/windows/win_pester.py @@ -32,6 +32,11 @@ options: - Accepts multiple comma seperated tags. type: list version_added: '2.9' + test_parameters: + description: + - Allows to specify parameters to the test script. + type: dict + version_added: '2.9' version: description: - Minimum version of the pester module that has to be available on the remote host. @@ -64,6 +69,13 @@ EXAMPLES = r''' win_pester: path: C:\Pester\test01.test.ps1 version: 4.1.0 + +- name: Run the pester test present in a folder with given script parameters. + win_pester: + path: C:\Pester\test04.test.ps1 + test_parameters: + Process: lsass + Service: bits ''' RETURN = r''' diff --git a/test/integration/targets/win_pester/files/test04.test.ps1 b/test/integration/targets/win_pester/files/test04.test.ps1 new file mode 100644 index 00000000000..07d1bd43c5c --- /dev/null +++ b/test/integration/targets/win_pester/files/test04.test.ps1 @@ -0,0 +1,18 @@ +Param( + $Service, + $Process +) + +Describe "Process should exist" { + it "Process $Process should be running" -Skip:([String]::IsNullOrEmpty($Process)) { + Get-Process -Name $Process -ErrorAction SilentlyContinue | Should Not BeNullOrEmpty + } +} + + + +Describe "Service should exist" -tag Service { + it "Service $Service should exist" -Skip:([String]::IsNullOrEmpty($Service)) { + Get-Service -Name $Service -ErrorAction SilentlyContinue | Should Not BeNullOrEmpty + } +} diff --git a/test/integration/targets/win_pester/tasks/test.yml b/test/integration/targets/win_pester/tasks/test.yml index 0cd51f59264..26addb13d10 100644 --- a/test/integration/targets/win_pester/tasks/test.yml +++ b/test/integration/targets/win_pester/tasks/test.yml @@ -53,7 +53,7 @@ that: - dir_with_ending_slash.changed - not dir_with_ending_slash.failed - - dir_with_ending_slash.output.TotalCount == 4 + - dir_with_ending_slash.output.TotalCount == 6 - name: Run Pester test(s) located in a folder. Folder path does not end with '\' win_pester: @@ -65,7 +65,7 @@ that: - dir_without_ending_slash.changed - not dir_without_ending_slash.failed - - dir_without_ending_slash.output.TotalCount == 4 + - dir_without_ending_slash.output.TotalCount == 6 - name: Run Pester test(s) located in a folder and with a minimum mandatory Pester version win_pester: @@ -78,7 +78,7 @@ that: - dir_with_version.changed - not dir_with_version.failed - - dir_with_version.output.TotalCount == 4 + - dir_with_version.output.TotalCount == 6 - name: Run Pester test(s) specifying a test file without specifying tag win_pester: @@ -102,3 +102,18 @@ that: - test_with_tag.changed - test_with_tag.output.TotalCount == 1 + +- name: Run Pester test(s) specifying a test file with parameters + win_pester: + path: '{{test_win_pester_path}}\test04.test.ps1' + test_parameters: + Process: lsass + Service: bits + register: test_with_parameter + +- name: Run Pester test(s) specifying a test file with parameters + assert: + that: + - test_with_parameter.changed + - test_with_parameter.output.PassedCount == 2 + - test_with_parameter.output.TotalCount == 2