-
Notifications
You must be signed in to change notification settings - Fork 23.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added module win_pester and relatives integration tests #37343
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6a8b1bc
Added module win_pester and relatives integration tests
equelin 1450eb4
Corrected issues as stated by ansible-test
equelin cf4531c
Added defaults variable in integration tests
equelin 5f338d8
Added task to install Pester if needed in the integration test
equelin ee174fb
Corrected error in win_psmodule task
equelin ff8e240
Added Pester installation with Chocolatey when Powershell version < 5
equelin 8dd0af7
Get facts...
equelin 105635c
Disabled invoke-pester output
equelin f563452
Added pester_result type
equelin 66c5c5b
Added jhawkesworth changes proposal
equelin 6e4b0f7
Corrected documentation linting
equelin bfa0cd8
Corrected linting
equelin 6cd4d35
Added dagwieers recommendations
equelin 24c5181
Added dagwieers recommendations
equelin bd75438
Corrected linting errors and task error in integration test
equelin 4cf0ff3
Corrected error in integration test
equelin dac24ed
Added dagwieers recommendations
equelin aa677ba
Corrected requirements in the DOCUMENTATION block
equelin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#!powershell | ||
# This file is part of Ansible | ||
|
||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
# Author: Erwan Quelin (mail:erwan.quelin@gmail.com - GitHub: @equelin - Twitter: @erwanquelin) | ||
|
||
#Requires -Module Ansible.ModuleUtils.Legacy | ||
|
||
$ErrorActionPreference = 'Stop' | ||
|
||
$params = Parse-Args -arguments $args -supports_check_mode $true | ||
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false | ||
$diff_mode = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false | ||
|
||
# Modules parameters | ||
|
||
$path = Get-AnsibleParam -obj $params -name "path" -type "string" -failifempty $true | ||
$minimum_version = Get-AnsibleParam -obj $params -name "minimum_version" -type "string" -failifempty $false | ||
|
||
$result = @{ | ||
changed = $false | ||
} | ||
|
||
if ($diff_mode) { | ||
$result.diff = @{} | ||
} | ||
|
||
# CODE | ||
# Test if parameter $version is valid | ||
Try { | ||
$minimum_version = [version]$minimum_version | ||
} | ||
Catch { | ||
Fail-Json -obj $result -message "Value '$minimum_version' for parameter 'minimum_version' is not a valid version format" | ||
} | ||
|
||
# Import Pester module if available | ||
$Module = 'Pester' | ||
|
||
If (-not (Get-Module -Name $Module -ErrorAction SilentlyContinue)) { | ||
If (Get-Module -Name $Module -ListAvailable -ErrorAction SilentlyContinue) { | ||
Import-Module $Module | ||
} else { | ||
Fail-Json -obj $result -message "Cannot find module: $Module. Check if pester is installed, and if it is not, install using win_psmodule or win_chocolatey." | ||
} | ||
} | ||
|
||
# Add actual pester's module version in the ansible's result variable | ||
$Pester_version = (Get-Module -Name $Module).Version.ToString() | ||
$result.pester_version = $Pester_version | ||
|
||
# Test if the Pester module is available with a version greater or equal than the one specified in the $version parameter | ||
If ((-not (Get-Module -Name $Module -ErrorAction SilentlyContinue | Where-Object {$_.Version -ge $minimum_version})) -and ($minimum_version)) { | ||
Fail-Json -obj $result -message "$Module version is not greater or equal to $minimum_version" | ||
} | ||
|
||
# Testing if test file or directory exist | ||
If (-not (Test-Path -LiteralPath $path)) { | ||
Fail-Json -obj $result -message "Cannot find file or directory: '$path' as it does not exist" | ||
} | ||
|
||
#Prepare Invoke-Pester parameters depending of the Pester's version. | ||
#Invoke-Pester output deactivation behave differently depending on the Pester's version | ||
If ($result.pester_version -ge "4.0.0") { | ||
$Parameters = @{ | ||
"show" = "none" | ||
"PassThru" = $True | ||
} | ||
} else { | ||
$Parameters = @{ | ||
"quiet" = $True | ||
"PassThru" = $True | ||
} | ||
} | ||
|
||
# Run Pester tests | ||
If (Test-Path -LiteralPath $path -PathType Leaf) { | ||
if ($check_mode) { | ||
$result.output = "Run pester test in the file: $path" | ||
} else { | ||
try { | ||
$result.output = Invoke-Pester $path @Parameters | ||
} catch { | ||
Fail-Json -obj $result -message $_.Exception | ||
} | ||
} | ||
} else { | ||
# Run Pester tests against all the .ps1 file in the local folder | ||
$files = Get-ChildItem -Path $path | Where-Object {$_.extension -eq ".ps1"} | ||
|
||
if ($check_mode) { | ||
$result.output = "Run pester test(s) who are in the folder: $path" | ||
} else { | ||
try { | ||
$result.output = Invoke-Pester $files.FullName @Parameters | ||
} catch { | ||
Fail-Json -obj $result -message $_.Exception | ||
} | ||
} | ||
} | ||
|
||
$result.changed = $true | ||
|
||
Exit-Json -obj $result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright: (c) 2018, Ansible Project | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
ANSIBLE_METADATA = {'metadata_version': '1.1', | ||
'status': ['preview'], | ||
'supported_by': 'community'} | ||
|
||
|
||
DOCUMENTATION = r''' | ||
--- | ||
module: win_pester | ||
short_description: Run Pester tests on Windows hosts | ||
version_added: "2.6" | ||
description: | ||
- Run Pester tests on Windows hosts. | ||
- Test files have to be available on the remote host. | ||
requirements: | ||
- Pester | ||
options: | ||
path: | ||
description: | ||
- Path to a pester test file or a folder where tests can be found. | ||
- If the path is a folder, the module will consider all ps1 files as Pester tests. | ||
required: true | ||
version: | ||
description: | ||
- Minimum version of the pester module that has to be available on the remote host. | ||
author: | ||
- Erwan Quelin (@erwanquelin) | ||
''' | ||
|
||
RETURN = r''' | ||
pester_version: | ||
description: Version of the pester module found on the remote host. | ||
returned: always | ||
type: string | ||
sample: 4.3.1 | ||
output: | ||
description: Results of the Pester tests. | ||
returned: success | ||
type: list | ||
sample: False | ||
''' | ||
|
||
EXAMPLES = r''' | ||
- name: Get facts | ||
setup: | ||
|
||
- name: Add Pester module | ||
action: | ||
module_name: "{{ 'win_psmodule' if ansible_powershell_version >= 5 else 'win_chocolatey' }}" | ||
name: Pester | ||
state: present | ||
|
||
- name: Run the pester test provided in the path parameter. | ||
win_pester: | ||
path: C:\Pester | ||
|
||
# Run pesters tests files that are present in the specified folder | ||
# ensure that the pester module version available is greater or equal to the version parameter. | ||
- name: Run the pester test present in a folder and check the Pester module version. | ||
win_pester: | ||
path: C:\Pester\test01.test.ps1 | ||
version: 4.1.0 | ||
''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
windows/ci/group2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
local_test_files: | ||
- files/test01.test.ps1 | ||
- files/test02.test.ps1 | ||
remote_test_files: | ||
- test01.test.ps1 | ||
- test02.test.ps1 | ||
fake_remote_test_file: C:\Pester\fake_test.test.ps1 | ||
fake_remote_folder: C:\Fake_Pester | ||
remote_test_folder: | ||
- C:\Pester\ | ||
- C:\Pester |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Describe -Name 'Test01' { | ||
It -name 'First Test' { | ||
{Get-Service} | Should Not Throw | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Describe -Name 'Test02' { | ||
It -name 'Second Test' { | ||
{Get-Service} | Should Throw | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
--- | ||
- name: Get facts | ||
setup: | ||
|
||
- name: Add Pester module | ||
win_psmodule: | ||
name: Pester | ||
state: present | ||
when: ansible_powershell_version >= 5 | ||
|
||
- name: Add Pester module with Chocolatey | ||
win_chocolatey: | ||
name: Pester | ||
state: present | ||
when: ansible_powershell_version < 5 | ||
|
||
- name: Copy test file(s) | ||
win_copy: | ||
src: "{{ item }}" | ||
dest: "{{ remote_test_folder[0] }}" | ||
with_items: "{{local_test_files}}" | ||
|
||
- name: Run Pester test(s) specifying a fake test file | ||
win_pester: | ||
path: "{{ fake_remote_test_file }}" | ||
register: result | ||
ignore_errors: yes | ||
|
||
- name: Assert result | ||
assert: | ||
that: | ||
- result.failed == true | ||
|
||
- name: Run Pester test(s) specifying a fake folder | ||
win_pester: | ||
path: "{{ fake_remote_folder }}" | ||
register: result | ||
ignore_errors: yes | ||
|
||
- name: Assert result | ||
assert: | ||
that: | ||
- result.failed == true | ||
|
||
- name: Run Pester test(s) specifying a test file and a higher pester version | ||
win_pester: | ||
path: "{{ remote_test_folder[0] }}{{ remote_test_files[0] }}" | ||
minimum_version: 6.0.0 | ||
register: result | ||
ignore_errors: yes | ||
|
||
- name: Assert result | ||
assert: | ||
that: | ||
- result.failed == true | ||
|
||
- name: Run Pester test(s) specifying a test file | ||
win_pester: | ||
path: "{{ remote_test_folder[0] }}{{ remote_test_files[0] }}" | ||
register: result | ||
|
||
- name: Assert result | ||
assert: | ||
that: | ||
- result.failed == false | ||
- result.output.TotalCount == 1 | ||
|
||
- name: Run Pester test(s) specifying a test file and with a minimum mandatory Pester version | ||
win_pester: | ||
path: "{{ remote_test_folder[0] }}{{ remote_test_files[0] }}" | ||
minimum_version: 3.0.0 | ||
register: result | ||
|
||
- name: Assert result | ||
assert: | ||
that: | ||
- result.failed == false | ||
- result.output.TotalCount == 1 | ||
|
||
- name: Run Pester test(s) located in a folder. Folder path end with '\' | ||
win_pester: | ||
path: "{{ remote_test_folder[0] }}" | ||
register: result | ||
|
||
- name: Assert result | ||
assert: | ||
that: | ||
- result.failed == false | ||
- result.output.TotalCount == 2 | ||
|
||
- name: Run Pester test(s) located in a folder. Folder path does not end with '\' | ||
win_pester: | ||
path: "{{ remote_test_folder[1] }}" | ||
register: result | ||
|
||
- name: Assert result | ||
assert: | ||
that: | ||
- result.failed == false | ||
- result.output.TotalCount == 2 | ||
|
||
- name: Run Pester test(s) located in a folder and with a minimum mandatory Pester version | ||
win_pester: | ||
path: "{{ remote_test_folder[0] }}" | ||
minimum_version: 3.0.0 | ||
register: result | ||
|
||
- name: Assert result | ||
assert: | ||
that: | ||
- result.failed == false | ||
- result.output.TotalCount == 2 | ||
|
||
- name: Delete test folder | ||
win_file: | ||
path: "{{ remote_test_folder[0] }}" | ||
state: absent |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would consider adding these steps to the examples so users can know how to ensure they have pre-requisites for running the module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done !