diff options
author | Jordan Borean <jborean93@gmail.com> | 2023-08-24 21:24:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-24 21:24:02 +0200 |
commit | 7865b198d26e3948777e1e3ee89e699075da2223 (patch) | |
tree | 0d62752ce7549556a3f170414e4229524424a93d /test/integration/targets/win_exec_wrapper | |
parent | PowerShell - remove uneeded dotnet code for future compatibility (#81472) (diff) | |
download | ansible-7865b198d26e3948777e1e3ee89e699075da2223.tar.xz ansible-7865b198d26e3948777e1e3ee89e699075da2223.zip |
PowerShell - Improve error checking (#80984)
Improves the error checking when running PowerShell modules using
Ansible.ModuleUtils.Legacy. It will only return an rc of 1 if both the
PowerShell module runner signalled an error occurred and those error
records were present in the output. This should reduce some false
positive errors when using the older module style.
Diffstat (limited to 'test/integration/targets/win_exec_wrapper')
3 files changed, 61 insertions, 0 deletions
diff --git a/test/integration/targets/win_exec_wrapper/action_plugins/test_rc_1.py b/test/integration/targets/win_exec_wrapper/action_plugins/test_rc_1.py new file mode 100644 index 0000000000..60cffde96b --- /dev/null +++ b/test/integration/targets/win_exec_wrapper/action_plugins/test_rc_1.py @@ -0,0 +1,35 @@ +# Copyright: (c) 2023, Ansible Project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +import json + +from ansible.plugins.action import ActionBase + + +class ActionModule(ActionBase): + + def run(self, tmp=None, task_vars=None): + super().run(tmp, task_vars) + del tmp + + exec_command = self._connection.exec_command + + def patched_exec_command(*args, **kwargs): + rc, stdout, stderr = exec_command(*args, **kwargs) + + new_stdout = json.dumps({ + "rc": rc, + "stdout": stdout.decode(), + "stderr": stderr.decode(), + "failed": False, + "changed": False, + }).encode() + + return (0, new_stdout, b"") + + try: + # This is done to capture the raw rc/stdio from the module exec + self._connection.exec_command = patched_exec_command + return self._execute_module(task_vars=task_vars) + finally: + self._connection.exec_command = exec_command diff --git a/test/integration/targets/win_exec_wrapper/library/test_rc_1.ps1 b/test/integration/targets/win_exec_wrapper/library/test_rc_1.ps1 new file mode 100644 index 0000000000..a9879548fa --- /dev/null +++ b/test/integration/targets/win_exec_wrapper/library/test_rc_1.ps1 @@ -0,0 +1,17 @@ +#!powershell + +# This scenario needs to use Legacy, the same HadErrors won't be set if using +# Ansible.Basic +#Requires -Module Ansible.ModuleUtils.Legacy + +# This will set `$ps.HadErrors` in the running pipeline but with no error +# record written. We are testing that it won't set the rc to 1 for this +# scenario. +try { + Write-Error -Message err -ErrorAction Stop +} +catch { + Exit-Json @{} +} + +Fail-Json @{} "This should not be reached" diff --git a/test/integration/targets/win_exec_wrapper/tasks/main.yml b/test/integration/targets/win_exec_wrapper/tasks/main.yml index 8fc54f7ca8..f1342c480b 100644 --- a/test/integration/targets/win_exec_wrapper/tasks/main.yml +++ b/test/integration/targets/win_exec_wrapper/tasks/main.yml @@ -272,3 +272,12 @@ assert: that: - ps_log_count.stdout | int == 0 + +- name: test module that sets HadErrors with no error records + test_rc_1: + register: module_had_errors + +- name: assert test module that sets HadErrors with no error records + assert: + that: + - module_had_errors.rc == 0 |