Debian Codegraph @

tests/ansible/lib/action/assert_equal.py

raw
1 #
2 # Print data structure diff on assertion failure.
3 #
4 # assert_equal: left=some.result right={1:2}
5 #
6
7 __metaclass__ = type
8
9 import inspect
10 import unittest
11
12 import ansible.template
13
14 from ansible.plugins.action import ActionBase
15
16
17 TEMPLATE_KWARGS = {}
18
19 try:
20 # inspect.getfullargspec() Added: 3.0
21 _argspec = inspect.getfullargspec(ansible.template.Templar.template)
22 except AttributeError:
23 # inspect.getargspec() Added: 2.1 Deprecated: 3.0 Removed: 3.11
24 _argspec = inspect.getargspec(ansible.template.Templar.template)
25 if 'bare_deprecated' in _argspec.args:
26 TEMPLATE_KWARGS['bare_deprecated'] = False
27
28
29 class TestCase(unittest.TestCase):
30 def runTest(self):
31 pass
32
33
34 def text_diff(a, b):
35 tc = TestCase()
36 tc.maxDiff = None
37 try:
38 tc.assertEqual(a, b)
39 return None
40 except AssertionError as e:
41 return str(e)
42
43
44 class ActionModule(ActionBase):
45 ''' Fail with custom message '''
46
47 TRANSFERS_FILES = False
48 _VALID_ARGS = frozenset(('left', 'right'))
49
50 def template(self, obj):
51 return self._templar.template(
52 obj,
53 convert_bare=True,
54 **TEMPLATE_KWARGS
55 )
56
57 def run(self, tmp=None, task_vars=None):
58 result = super(ActionModule, self).run(tmp, task_vars or {})
59 left = self.template(self._task.args['left'])
60 right = self.template(self._task.args['right'])
61
62 diff = text_diff(left, right)
63 if diff is None:
64 return {
65 'changed': False
66 }
67
68 return {
69 'changed': False,
70 'failed': True,
71 'msg': diff,
72 '_ansible_verbose_always': True,
73 }