Debian Codegraph @

test/integration/targets/callback_results/callback_plugins/track_connections.py

raw
1 # Copyright: Contributors to the Ansible project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6 DOCUMENTATION = """
7 name: track_connections
8 short_description: Track connection plugins used for hosts
9 description:
10 - Track connection plugins used for hosts
11 type: aggregate
12 """
13
14 import functools
15 import inspect
16 import json
17
18 from collections import defaultdict
19
20 from ansible.plugins.callback import CallbackBase
21 from ansible.executor.task_result import CallbackTaskResult
22
23
24 class CallbackModule(CallbackBase):
25 CALLBACK_VERSION = 2.0
26 CALLBACK_TYPE = 'aggregate'
27 CALLBACK_NAME = 'track_connections'
28 CALLBACK_NEEDS_ENABLED = True
29
30 def __init__(self, *args, **kwargs):
31 super().__init__(*args, **kwargs)
32
33 self._conntrack = defaultdict(lambda : defaultdict(list))
34
35 # dynamically implement all v2 callback methods that accept `result`
36 for name, sig in ((cb, inspect.signature(getattr(self, cb))) for cb in dir(self) if cb.startswith('v2_')):
37 if 'result' in sig.parameters:
38 setattr(self, name, functools.partial(self._track, event_name=name))
39
40 def _track(self, result: CallbackTaskResult, *_args, event_name: str, **_kwargs):
41 host = result.host.get_name()
42 task = result.task
43
44 self._conntrack[host][task.connection].append(f'{event_name}: {task.name}')
45
46 def v2_playbook_on_stats(self, stats):
47 expected = {
48 "testhost": {
49 "ansible.builtin.local": [
50 "v2_runner_on_ok: execute a successful non-loop task with the local connection",
51 "v2_runner_on_failed: execute a failing non-loop task with the local connection",
52 "v2_runner_item_on_ok: execute a successful looped task with the local connection",
53 "v2_runner_on_ok: execute a successful looped task with the local connection",
54 "v2_runner_item_on_failed: execute a failing looped task with the local connection",
55 "v2_runner_on_failed: execute a failing looped task with the local connection",
56 "v2_runner_on_async_ok: execute a successful async task with the local connection",
57 "v2_runner_on_ok: execute a successful async task with the local connection",
58 "v2_runner_on_async_failed: execute a failing async task with the local connection",
59 "v2_runner_on_failed: execute a failing async task with the local connection"
60 ],
61 }
62 }
63
64 if self._conntrack == expected:
65 self._display.display('FOUND EXPECTED EVENTS')
66 return
67
68 # pragma: nocover
69 self._display.display(f'ACTUAL\n{json.dumps(self._conntrack, indent=4)}')
70 self._display.display(f'EXPECTED\n{json.dumps(expected, indent=4)}')