Debian Codegraph @

ansible_collections/infoblox/nios_modules/tests/unit/plugins/modules/test_nios_module.py

raw symbols SCIP
1 # (c) 2018 Red Hat Inc.
2 #
3 # This file is part of Ansible
4 #
5 # Ansible is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Ansible is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
17
18 # Make coding more python3-ish
19 from __future__ import (absolute_import, division, print_function)
20 __metaclass__ = type
21
22 import os
23 import json
24
25 from ansible_collections.infoblox.nios_modules.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase
26
27
28 fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures')
29 fixture_data = {}
30
31
32 def load_fixture(name):
33 path = os.path.join(fixture_path, name)
34
35 if path in fixture_data:
36 return fixture_data[path]
37
38 with open(path) as f:
39 data = f.read()
40
41 try:
42 data = json.loads(data)
43 except Exception:
44 pass
45
46 fixture_data[path] = data
47 return data
48
49
50 class TestNiosModule(ModuleTestCase):
51
52 def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False):
53
54 self.load_fixtures(commands)
55
56 if failed:
57 result = self.failed()
58 self.assertTrue(result['failed'], result)
59 else:
60 result = self.changed(changed)
61 self.assertEqual(result['changed'], changed, result)
62
63 if commands is not None:
64 if sort:
65 self.assertEqual(sorted(commands), sorted(result['commands']), result['commands'])
66 else:
67 self.assertEqual(commands, result['commands'], result['commands'])
68
69 return result
70
71 def failed(self):
72 with self.assertRaises(AnsibleFailJson) as exc:
73 self.module.main()
74
75 result = exc.exception.args[0]
76 self.assertTrue(result['failed'], result)
77 return result
78
79 def changed(self, changed=False):
80 with self.assertRaises(AnsibleExitJson) as exc:
81 self.module.main()
82
83 result = exc.exception.args[0]
84 self.assertEqual(result['changed'], changed, result)
85 return result
86
87 def load_fixtures(self, commands=None):
88 pass