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:
parent
284dafe476
commit
a20afb5822
4 changed files with 59 additions and 5 deletions
|
@ -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
|
$path = Get-AnsibleParam -obj $params -name "path" -type "str" -failifempty $true
|
||||||
$tags = Get-AnsibleParam -obj $params -name "tags" -type "list"
|
$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
|
$minimum_version = Get-AnsibleParam -obj $params -name "minimum_version" -type "str" -failifempty $false
|
||||||
|
|
||||||
$result = @{
|
$result = @{
|
||||||
|
@ -78,11 +79,19 @@ if($tags.count){
|
||||||
}
|
}
|
||||||
# Run Pester tests
|
# Run Pester tests
|
||||||
If (Test-Path -LiteralPath $path -PathType Leaf) {
|
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) {
|
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 {
|
} else {
|
||||||
try {
|
try {
|
||||||
$result.output = Invoke-Pester $path @Parameters
|
$result.output = Invoke-Pester @Parameters
|
||||||
} catch {
|
} catch {
|
||||||
Fail-Json -obj $result -message $_.Exception
|
Fail-Json -obj $result -message $_.Exception
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,11 @@ options:
|
||||||
- Accepts multiple comma seperated tags.
|
- Accepts multiple comma seperated tags.
|
||||||
type: list
|
type: list
|
||||||
version_added: '2.9'
|
version_added: '2.9'
|
||||||
|
test_parameters:
|
||||||
|
description:
|
||||||
|
- Allows to specify parameters to the test script.
|
||||||
|
type: dict
|
||||||
|
version_added: '2.9'
|
||||||
version:
|
version:
|
||||||
description:
|
description:
|
||||||
- Minimum version of the pester module that has to be available on the remote host.
|
- Minimum version of the pester module that has to be available on the remote host.
|
||||||
|
@ -64,6 +69,13 @@ EXAMPLES = r'''
|
||||||
win_pester:
|
win_pester:
|
||||||
path: C:\Pester\test01.test.ps1
|
path: C:\Pester\test01.test.ps1
|
||||||
version: 4.1.0
|
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'''
|
RETURN = r'''
|
||||||
|
|
18
test/integration/targets/win_pester/files/test04.test.ps1
Normal file
18
test/integration/targets/win_pester/files/test04.test.ps1
Normal 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
|
||||||
|
}
|
||||||
|
}
|
|
@ -53,7 +53,7 @@
|
||||||
that:
|
that:
|
||||||
- dir_with_ending_slash.changed
|
- dir_with_ending_slash.changed
|
||||||
- not dir_with_ending_slash.failed
|
- 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 '\'
|
- name: Run Pester test(s) located in a folder. Folder path does not end with '\'
|
||||||
win_pester:
|
win_pester:
|
||||||
|
@ -65,7 +65,7 @@
|
||||||
that:
|
that:
|
||||||
- dir_without_ending_slash.changed
|
- dir_without_ending_slash.changed
|
||||||
- not dir_without_ending_slash.failed
|
- 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
|
- name: Run Pester test(s) located in a folder and with a minimum mandatory Pester version
|
||||||
win_pester:
|
win_pester:
|
||||||
|
@ -78,7 +78,7 @@
|
||||||
that:
|
that:
|
||||||
- dir_with_version.changed
|
- dir_with_version.changed
|
||||||
- not dir_with_version.failed
|
- 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
|
- name: Run Pester test(s) specifying a test file without specifying tag
|
||||||
win_pester:
|
win_pester:
|
||||||
|
@ -102,3 +102,18 @@
|
||||||
that:
|
that:
|
||||||
- test_with_tag.changed
|
- test_with_tag.changed
|
||||||
- test_with_tag.output.TotalCount == 1
|
- 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
|
||||||
|
|
Loading…
Reference in a new issue