ansible-test - fix ps argspec check inside cmdlet (#79699)

* ansible-test - fix ps argspec check inside cmdlet

* Added error condition test

* Fix sanity problem
pull/79712/head
Jordan Borean 1 year ago committed by GitHub
parent 602824db1d
commit ee33be9484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- ansible-test - Fix validate-modules error when retrieving PowerShell argspec when retrieved inside a Cmdlet

@ -0,0 +1,3 @@
README
------
This is a simple collection used to test failures with ``ansible-test sanity --test validate-modules``.

@ -0,0 +1,6 @@
namespace: ns
name: failure
version: 1.0.0
readme: README.rst
authors:
- Ansible

@ -0,0 +1,16 @@
#!powershell
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#AnsibleRequires -CSharpUtil Ansible.Basic
throw "test inner error message"
$module = [Ansible.Basic.AnsibleModule]::Create($args, @{
options = @{
test = @{ type = 'str'; choices = @('foo', 'bar'); default = 'foo' }
}
})
$module.Result.test = 'abc'
$module.ExitJson()

@ -0,0 +1,31 @@
# Copyright (c) 2022 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION:
module: failure_ps
short_description: Short description for failure_ps module
description:
- Description for failure_ps module
options:
test:
description:
- Description for test module option
type: str
choices:
- foo
- bar
default: foo
author:
- Ansible Core Team
EXAMPLES: |
- name: example for failure_ps
ns.col.failure_ps:
test: bar
RETURN:
test:
description: The test return value
returned: always
type: str
sample: abc

@ -0,0 +1,19 @@
#AnsibleRequires -CSharpUtil Ansible.Basic
Function Invoke-AnsibleModule {
<#
.SYNOPSIS
validate
#>
[CmdletBinding()]
param ()
$module = [Ansible.Basic.AnsibleModule]::Create(@(), @{
options = @{
test = @{ type = 'str' }
}
})
$module.ExitJson()
}
Export-ModuleMember -Function Invoke-AnsibleModule

@ -0,0 +1,7 @@
#!powershell
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#AnsibleRequires -CSharpUtil Ansible.Basic
#AnsibleRequires -PowerShell ..module_utils.share_module
Invoke-AnsibleModule

@ -0,0 +1,25 @@
# Copyright (c) 2022 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
DOCUMENTATION:
module: in_function
short_description: Short description for in_function module
description:
- Description for in_function module
options:
test:
description: Description for test
type: str
author:
- Ansible Core Team
EXAMPLES: |
- name: example for sidecar
ns.col.in_function:
RETURN:
test:
description: The test return value
returned: always
type: str
sample: abc

@ -17,3 +17,18 @@ fi
# Use a PowerShell-only collection to verify that validate-modules does not load the collection loader multiple times.
ansible-test sanity --test validate-modules --color --truncate 0 "${@}"
cd ../failure
if ansible-test sanity --test validate-modules --color --truncate 0 "${@}" 1> ansible-stdout.txt 2> ansible-stderr.txt; then
echo "ansible-test sanity for failure should cause failure"
exit 1
fi
cat ansible-stdout.txt
grep -q "ERROR: plugins/modules/failure_ps.ps1:0:0: import-error: Exception attempting to import module for argument_spec introspection" < ansible-stdout.txt
grep -q "test inner error message" < ansible-stdout.txt
cat ansible-stderr.txt
grep -q "FATAL: The 1 sanity test(s) listed below (out of 1) failed" < ansible-stderr.txt
grep -q "validate-modules" < ansible-stderr.txt

@ -101,13 +101,21 @@ Add-CSharpType -References @(Get-Content -LiteralPath $manifest.ansible_basic -R
$powershell.AddScript($module_code) > $null
$powershell.Invoke() > $null
$arg_spec = $powershell.Runspace.SessionStateProxy.GetVariable('ansibleTestArgSpec')
if (-not $arg_spec) {
$err = $powershell.Streams.Error
if ($err) {
$err
}
else {
"Unknown error trying to get PowerShell arg spec"
}
if ($powershell.HadErrors) {
$powershell.Streams.Error
exit 1
}
$arg_spec = $powershell.Runspace.SessionStateProxy.GetVariable('ansibleTestArgSpec')
Resolve-CircularReference -Hash $arg_spec
ConvertTo-Json -InputObject $arg_spec -Compress -Depth 99

Loading…
Cancel
Save