Skip to content
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

WIP - Add unit testing with Pester for PowerShell modules #54965

Open
wants to merge 1 commit into
base: devel
from

Conversation

Projects
None yet
3 participants
@jborean93
Copy link
Contributor

jborean93 commented Apr 8, 2019

SUMMARY

Adds the ability to create unit tests for PowerShell modules and module utils in ansible-test. This is a basic POC to test out what we need to have in place for this to work. Currently it works by running the tests on the local PSCore container instead of on an actual Windows host. This means that calls to .NET functions that are unavailable on Windows will have to be mocked out but I'm hopeful we can still do proper unit tests this way without the overhead of managing and actual Windows host.

Items that need further discussion;

  • For this to work, the current proposal is to change our Windows modules to be in the shape
#!powershell

#AnsibleRequires -CSharpUtil Ansible.Basic

Function Invoke-AnsibleModule {
    param (
        [Parameter(Mandatory=$true)][AllowEmptyCollection()][System.Object[]]$Arguments
    )
    $spec = @{
        options = @{}
        supports_check_mode = $true
    }
    $module = [Ansible.Basic.AnsibleModule]::Create($Arguments, $spec)
    $module.ExitJson()
}

if ($MyInvocation.InvocationName -ne '.') {
    Invoke-AnsibleModule -Arguments $args
}
  • The above allows us to dot source the module without actually executing the module. This is critical so we can load the functions in the script in a unit test scenario
  • Unfortunately this means we need to change the way our modules are written but this can easily be part of a sanity check and shouldn't be too hard to write a once off script to convert our builtin modules
  • An alternative is to use an AST parser to get all defined functions in a script and then dot source them that way. This has an advantage because it works with our current modules but won't work with coverage collectors
  • We could still get coverage working with the above but it would involve creating temp module files with just the module functions in the correct line positions
  • The coverage output and test result output is different from the Python output. We still need a conversion script to align the output when using in Ansible
ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

ansible-test
windows

@ansibot

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.