| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import collections.abc as c |
| 4 |
import functools |
| 5 |
|
| 6 |
from unittest.mock import MagicMock |
| 7 |
|
| 8 |
from ansible.executor.task_result import CallbackTaskResult |
| 9 |
from ansible.plugins.callback import CallbackBase |
| 10 |
|
| 11 |
|
| 12 |
class CallbackModule(CallbackBase): |
| 13 |
CALLBACK_NEEDS_ENABLED = True |
| 14 |
seen_tr = [] # track taskresult instances to ensure every call sees a unique instance |
| 15 |
|
| 16 |
expects_task_result = { |
| 17 |
'v2_runner_on_failed', 'v2_runner_on_ok', 'v2_runner_on_skipped', 'v2_runner_on_unreachable', 'v2_runner_on_async_poll', 'v2_runner_on_async_ok', |
| 18 |
'v2_runner_on_async_failed,', 'v2_on_file_diff', 'v2_runner_item_on_ok', |
| 19 |
'v2_runner_item_on_failed', 'v2_runner_item_on_skipped', 'v2_runner_retry', |
| 20 |
} |
| 21 |
|
| 22 |
expects_no_task_result = { |
| 23 |
'v2_playbook_on_start', 'v2_playbook_on_notify', 'v2_playbook_on_no_hosts_matched', 'v2_playbook_on_no_hosts_remaining', 'v2_playbook_on_task_start', |
| 24 |
'v2_playbook_on_handler_task_start', 'v2_playbook_on_vars_prompt', 'v2_playbook_on_play_start', |
| 25 |
'v2_playbook_on_include', 'v2_runner_on_start', |
| 26 |
} |
| 27 |
|
| 28 |
# we're abusing runtime assertions to signify failure in this integration test component; ensure they're not disabled by optimizations |
| 29 |
try: |
| 30 |
assert False |
| 31 |
except AssertionError: |
| 32 |
pass |
| 33 |
else: |
| 34 |
raise BaseException("this test does not function when running Python with optimization") |
| 35 |
|
| 36 |
def __init__(self, *args, **kwargs) -> None: |
| 37 |
super().__init__(*args, **kwargs) |
| 38 |
self._display = MagicMock() |
| 39 |
|
| 40 |
@staticmethod |
| 41 |
def get_first_task_result(args: c.Sequence) -> CallbackTaskResult | None: |
| 42 |
"""Find the first CallbackTaskResult in posargs, since the signatures are dynamic and we didn't want to use inspect signature binding.""" |
| 43 |
return next((arg for arg in args if isinstance(arg, CallbackTaskResult)), None) |
| 44 |
|
| 45 |
def v2_on_any(self, *args, **kwargs) -> None: |
| 46 |
"""Standard behavioral test for the v2_on_any callback method.""" |
| 47 |
print(f'hello from v2_on_any {args=} {kwargs=}') |
| 48 |
if result := self.get_first_task_result(args): |
| 49 |
assert isinstance(result, CallbackTaskResult) |
| 50 |
|
| 51 |
assert result is self._current_task_result |
| 52 |
|
| 53 |
assert result not in self.seen_tr |
| 54 |
|
| 55 |
self.seen_tr.append(result) |
| 56 |
else: |
| 57 |
assert self._current_task_result is None |
| 58 |
|
| 59 |
def v2_method_expects_task_result(self, *args, method_name: str, **_kwargs) -> None: |
| 60 |
"""Standard behavioral tests for callback methods accepting a task result; wired dynamically.""" |
| 61 |
print(f'hello from {method_name}') |
| 62 |
result = self.get_first_task_result(args) |
| 63 |
|
| 64 |
assert result is self._current_task_result |
| 65 |
|
| 66 |
assert isinstance(result, CallbackTaskResult) |
| 67 |
|
| 68 |
assert result not in self.seen_tr |
| 69 |
|
| 70 |
self.seen_tr.append(result) |
| 71 |
|
| 72 |
has_exception = bool(result.exception) |
| 73 |
has_warnings = bool(result.warnings) |
| 74 |
has_deprecations = bool(result.deprecations) |
| 75 |
|
| 76 |
self._display.reset_mock() |
| 77 |
|
| 78 |
self._handle_exception(result.result) # pops exception from transformed dict |
| 79 |
|
| 80 |
if has_exception: |
| 81 |
assert 'exception' not in result.result |
| 82 |
self._display._error.assert_called() |
| 83 |
|
| 84 |
self._display.reset_mock() |
| 85 |
|
| 86 |
self._handle_warnings(result.result) # pops warnings/deprecations from transformed dict |
| 87 |
|
| 88 |
if has_warnings: |
| 89 |
assert 'warnings' not in result.result |
| 90 |
self._display._warning.assert_called() |
| 91 |
|
| 92 |
if has_deprecations: |
| 93 |
assert 'deprecations' not in result.result |
| 94 |
self._display._deprecated.assert_called() |
| 95 |
|
| 96 |
def v2_method_expects_no_task_result(self, *args, method_name: str, **_kwargs) -> None: |
| 97 |
"""Standard behavioral tests for non-task result callback methods; wired dynamically.""" |
| 98 |
print(f'hello from {method_name}') |
| 99 |
|
| 100 |
assert self.get_first_task_result(args) is None |
| 101 |
|
| 102 |
assert self._current_task_result is None |
| 103 |
|
| 104 |
def v2_playbook_on_stats(self, *args, **kwargs) -> None: |
| 105 |
print('hello from v2_playbook_on_stats') |
| 106 |
assert self.get_first_task_result(args) is None |
| 107 |
|
| 108 |
print('legacy warning display callback test PASS') |
| 109 |
|
| 110 |
def __getattribute__(self, item: str) -> object: |
| 111 |
if item in CallbackModule.expects_task_result: |
| 112 |
return functools.partial(CallbackModule.v2_method_expects_task_result, self, method_name=item) |
| 113 |
elif item in CallbackModule.expects_no_task_result: |
| 114 |
return functools.partial(CallbackModule.v2_method_expects_no_task_result, self, method_name=item) |
| 115 |
else: |
| 116 |
return object.__getattribute__(self, item) |