Add support for test script parameters in win_pester (#58790)

* add parameter list argument

* add tests

* fix test and add doc

* correct test file

* fix typo

* fix tests

* fix typo in file name

* correct file name reverting the previous commit

* correct property name

* add checkmode message

* changes as per review comments

* variable casing and other review comment changes

* define $test_parameters_check_mode_msg variable
This commit is contained in:
PRASOON KARUNAN V 2019-07-23 01:16:26 +05:30 committed by Jordan Borean
parent 284dafe476
commit a20afb5822
4 changed files with 59 additions and 5 deletions

View file

@ -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
}

View file

@ -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'''

View file

@ -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
}
}

View file

@ -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