Debian Codegraph @

ansible_collections/theforeman/foreman/tests/test_callback.py

raw symbols SCIP
1 import os
2 import re
3 import json
4
5 try:
6 from ansible.module_utils.compat.version import LooseVersion
7 except ImportError:
8 from distutils.version import LooseVersion
9
10 from .conftest import run_playbook, get_ansible_version, assert_no_warnings
11
12 ansible_version = get_ansible_version()
13
14
15 def run_playbook_callback(tmpdir, report_type):
16 extra_env = {}
17 if LooseVersion(ansible_version) < LooseVersion('2.11'):
18 extra_env['ANSIBLE_CALLBACK_WHITELIST'] = "theforeman.foreman.foreman"
19 extra_env['ANSIBLE_COMMAND_WARNINGS'] = "0"
20 else:
21 extra_env['ANSIBLE_CALLBACKS_ENABLED'] = "theforeman.foreman.foreman"
22 # No connection is actually performed during the test
23 extra_env['FOREMAN_REPORT_TYPE'] = report_type
24 extra_env['FOREMAN_URL'] = "http://localhost"
25 if report_type == "proxy":
26 extra_env['FOREMAN_PROXY_URL'] = "http://localhost"
27 extra_env['FOREMAN_SSL_CERT'] = "/dev/zero"
28 extra_env['FOREMAN_SSL_KEY'] = "/dev/zero"
29 extra_env['FOREMAN_DIR_STORE'] = tmpdir.strpath
30 extra_env['ANSIBLE_VAULT_PASSWORD_FILE'] = os.path.join(os.getcwd(), 'tests', 'callback', 'vault-pass')
31 playbook = os.path.join('..', 'callback', 'three_hosts')
32 inventory = os.path.join(os.getcwd(), 'tests', 'callback', 'three_hosts')
33 return run_playbook(playbook, inventory=inventory, extra_env=extra_env)
34
35
36 def drop_incompatible_items(d):
37 """
38 Recursively drop report items that vary on invocations
39 and versions and cannot be reasonably fixed
40 """
41 dd = {}
42 for k, v in d.items():
43 if k in ['msg', 'start', 'end', 'delta', 'uuid', 'timeout', '_ansible_no_log', 'warn', 'connection',
44 'extended_allitems', 'loop_control', 'expand_argument_vars', 'retries', 'parent', 'parent_type', 'finalized', 'squashed', 'no_log',
45 'listen', '_ansible_internal_redirect_list', 'exception', 'resolved_action', 'delay', '_resolved_action', 'is_handler', 'changed_when_result',
46 'ignore_errors', 'ignore_unreachable', 'failed']:
47 continue
48
49 if isinstance(v, dict):
50 dd[k] = drop_incompatible_items(v)
51 elif isinstance(v, (list, set, tuple)):
52 dd[k] = type(v)(drop_incompatible_items(vv) if isinstance(vv, dict) else vv
53 for vv in v)
54 else:
55 dd[k] = v
56
57 return dd
58
59
60 def run_callback(tmpdir, report_type, vcrmode):
61 run = run_playbook_callback(tmpdir, report_type)
62 assert run.rc == 0
63 assert len(tmpdir.listdir()) > 0, "Directory with results is empty"
64 fixture_directory = os.path.join(os.getcwd(), 'tests', 'fixtures', 'callback', 'dir_store', report_type)
65 assert len(tmpdir.listdir()) == len(os.listdir(fixture_directory)), "Fixture directory and output directory have a different number of files"
66 assert_no_warnings(run)
67 for real_file in tmpdir.listdir(sort=True):
68 contents = real_file.read()
69 contents = re.sub(r"\d+-\d+-\d+ \d+:\d+:\d+\+\d+:\d+", "2000-01-01 12:00:00+00:00", contents)
70 contents = re.sub(r"\d+-\d+-\d+[ T]\d+:\d+:\d+\.\d+", "2000-01-01 12:00:00.0000", contents)
71 contents = re.sub(r"\d+:\d+:\d+\.\d+", "12:00:00.0000", contents)
72 if report_type == "foreman":
73 # drop_incompatible_items cannot be used for the legacy format
74 contents = re.sub(r", \\\"msg\\\": \\\"\\\"", "", contents)
75 contents = re.sub(r"\\\"_ansible_no_log\\\": [^,]+, ", "", contents)
76 contents = re.sub(r", \\\"warn\\\": false", "", contents)
77 contents = re.sub(r", \\\"expand_argument_vars\\\": true", "", contents)
78 contents = re.sub(r", \\\"cmd\\\": null", "", contents)
79 contents = re.sub(r", \\\"exception\\\": [^,]+", "", contents)
80 contents = re.sub(r", \\\"changed_when_result\\\": [^,]+", "", contents)
81 contents = re.sub(r", \\\"failed\\\": false", "", contents)
82 real_contents = json.loads(contents)
83 if report_type == "foreman":
84 try:
85 real_contents['config_report']['metrics']['time']['total'] = 1
86 except KeyError:
87 pass
88 else:
89 real_contents['metrics']['time']['total'] = 1
90 real_contents = drop_incompatible_items(real_contents)
91 fixture_name = real_file.basename
92 fixture = os.path.join(fixture_directory, fixture_name)
93 if vcrmode == "record":
94 print("Writing: ", str(fixture))
95 with open(fixture, 'w') as f:
96 json.dump(real_contents, f, indent=2, sort_keys=True)
97 else:
98 with open(fixture, 'r') as f:
99 fixture_data = f.read()
100 if LooseVersion(ansible_version) >= LooseVersion('2.19'):
101 fixture_data = fixture_data.replace('ENCRYPTED_VAULT_VALUE_NOT_REPORTED', 'admin')
102 expected_contents = json.loads(fixture_data)
103 expected_contents = drop_incompatible_items(expected_contents)
104 real_contents = drop_incompatible_items(real_contents)
105 assert expected_contents == real_contents, "Fixture {fixture_name} differs, run with -vvvv to see the diff".format(fixture_name=fixture_name)
106
107
108 def test_callback_foreman(tmpdir, vcrmode):
109 run_callback(tmpdir, "foreman", vcrmode)
110
111
112 def test_callback_proxy(tmpdir, vcrmode):
113 run_callback(tmpdir, "proxy", vcrmode)