Debian Codegraph @

ansible_collections/community/routeros/tests/unit/plugins/modules/routeros_module.py

raw
1 # Copyright (c) 2016 Red Hat Inc.
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Make coding more python3-ish
6 from __future__ import (absolute_import, division, print_function)
7 __metaclass__ = type
8
9 import os
10 import json
11
12 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase
13
14
15 fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
16 fixture_data = {}
17
18
19 def load_fixture(name):
20 path = os.path.join(fixture_path, name)
21
22 if path in fixture_data:
23 return fixture_data[path]
24
25 with open(path) as f:
26 data = f.read()
27
28 try:
29 data = json.loads(data)
30 except Exception:
31 pass
32
33 fixture_data[path] = data
34 return data
35
36
37 class TestRouterosModule(ModuleTestCase):
38
39 def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
40
41 self.load_fixtures(commands)
42
43 if failed:
44 result = self.failed()
45 self.assertTrue(result['failed'], result)
46 else:
47 result = self.changed(changed)
48 self.assertEqual(result['changed'], changed, result)
49
50 if commands is not None:
51 if sort:
52 self.assertEqual(sorted(commands), sorted(result['commands']), result['commands'])
53 else:
54 self.assertEqual(commands, result['commands'], result['commands'])
55
56 return result
57
58 def failed(self):
59 with self.assertRaises(AnsibleFailJson) as exc:
60 self.module.main()
61
62 result = exc.exception.args[0]
63 self.assertTrue(result['failed'], result)
64 return result
65
66 def changed(self, changed=False):
67 with self.assertRaises(AnsibleExitJson) as exc:
68 self.module.main()
69
70 result = exc.exception.args[0]
71 self.assertEqual(result['changed'], changed, result)
72 return result
73
74 def load_fixtures(self, commands=None):
75 pass