From e13f9c1092c1513f29787fe2f7b9975c019032ef Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Fri, 1 Dec 2023 17:28:36 +0100 Subject: [PATCH] [PR #7630/0589c841 backport][stable-6] Fix and re-enable cmd_runner tests (#7651) Fix and re-enable cmd_runner tests (#7630) Fix cmd_runner tests. (cherry picked from commit 0589c841765e45bd59853a4d151c13c55a218d00) Co-authored-by: Felix Fontein --- .../action_plugins/_unsafe_assert.py | 56 +++++++++++++++++++ tests/integration/targets/cmd_runner/aliases | 1 - .../cmd_runner/tasks/test_cmd_echo.yml | 2 +- 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 tests/integration/targets/cmd_runner/action_plugins/_unsafe_assert.py diff --git a/tests/integration/targets/cmd_runner/action_plugins/_unsafe_assert.py b/tests/integration/targets/cmd_runner/action_plugins/_unsafe_assert.py new file mode 100644 index 0000000000..498e8258d0 --- /dev/null +++ b/tests/integration/targets/cmd_runner/action_plugins/_unsafe_assert.py @@ -0,0 +1,56 @@ +# Copyright 2012, Dag Wieers +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +from ansible.errors import AnsibleError +from ansible.playbook.conditional import Conditional +from ansible.plugins.action import ActionBase + + +class ActionModule(ActionBase): + ''' Fail with custom message ''' + + _requires_connection = False + + _VALID_ARGS = frozenset(('msg', 'that')) + + def _make_safe(self, text): + # A simple str(text) won't do it since AnsibleUnsafeText is clever :-) + return ''.join(chr(ord(x)) for x in text) + + def run(self, tmp=None, task_vars=None): + if task_vars is None: + task_vars = dict() + + result = super(ActionModule, self).run(tmp, task_vars) + del tmp # tmp no longer has any effect + + if 'that' not in self._task.args: + raise AnsibleError('conditional required in "that" string') + + fail_msg = 'Assertion failed' + success_msg = 'All assertions passed' + + thats = self._task.args['that'] + + cond = Conditional(loader=self._loader) + result['_ansible_verbose_always'] = True + + for that in thats: + cond.when = [str(self._make_safe(that))] + test_result = cond.evaluate_conditional(templar=self._templar, all_vars=task_vars) + if not test_result: + result['failed'] = True + result['evaluated_to'] = test_result + result['assertion'] = that + + result['msg'] = fail_msg + + return result + + result['changed'] = False + result['msg'] = success_msg + return result diff --git a/tests/integration/targets/cmd_runner/aliases b/tests/integration/targets/cmd_runner/aliases index 0c3b04c1d4..12d1d6617e 100644 --- a/tests/integration/targets/cmd_runner/aliases +++ b/tests/integration/targets/cmd_runner/aliases @@ -3,4 +3,3 @@ # SPDX-License-Identifier: GPL-3.0-or-later azp/posix/2 -disabled # TODO diff --git a/tests/integration/targets/cmd_runner/tasks/test_cmd_echo.yml b/tests/integration/targets/cmd_runner/tasks/test_cmd_echo.yml index dd56e9e762..e2e6c4b20e 100644 --- a/tests/integration/targets/cmd_runner/tasks/test_cmd_echo.yml +++ b/tests/integration/targets/cmd_runner/tasks/test_cmd_echo.yml @@ -24,5 +24,5 @@ ignore_errors: "{{ item.expect_error | default(omit) }}" - name: check results ({{ item.name }}) - assert: + _unsafe_assert: that: "{{ item.assertions }}"