Debian Codegraph @

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

raw symbols SCIP
1 # This file is part of Ansible
2 #
3 # Ansible is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # Ansible is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
15
16 # Make coding more python3-ish
17 from __future__ import (absolute_import, division, print_function)
18 __metaclass__ = type
19
20
21 from ansible_collections.infoblox.nios_modules.plugins.modules import nios_nsgroup
22 from ansible_collections.infoblox.nios_modules.plugins.module_utils import api
23 from ansible_collections.infoblox.nios_modules.tests.unit.compat.mock import patch, MagicMock, Mock
24 from .test_nios_module import TestNiosModule, load_fixture
25
26
27 class TestNiosNSGroupModule(TestNiosModule):
28
29 module = nios_nsgroup
30
31 def setUp(self):
32
33 super(TestNiosNSGroupModule, self).setUp()
34 self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_nsgroup.WapiModule')
35 self.module.check_mode = False
36 self.module.params = {'provider': None}
37
38 self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_nsgroup.WapiModule')
39 self.exec_command = self.mock_wapi.start()
40 self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_nsgroup.WapiModule.run')
41
42 self.load_config = self.mock_wapi_run.start()
43
44 self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
45 self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
46
47 def tearDown(self):
48 super(TestNiosNSGroupModule, self).tearDown()
49 self.mock_wapi.stop()
50 self.mock_check_type_dict.stop()
51
52 def _get_wapi(self, test_object):
53 wapi = api.WapiModule(self.module)
54 wapi.get_object = Mock(name='get_object', return_value=test_object)
55 wapi.create_object = Mock(name='create_object')
56 wapi.update_object = Mock(name='update_object')
57 wapi.delete_object = Mock(name='delete_object')
58 return wapi
59
60 def load_fixtures(self, commands=None):
61 self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None)
62 self.load_config.return_value = dict(diff=None, session='session')
63
64 def test_nios_nsgroup_create(self):
65 self.module.params = {'provider': None, 'state': 'present', 'name': 'my-simple-group',
66 'comment': None, 'grid_primary': None}
67
68 test_object = None
69 test_spec = {
70 "name": {"ib_req": True},
71 "comment": {},
72 "grid_primary": {}
73 }
74
75 wapi = self._get_wapi(test_object)
76 res = wapi.run('testobject', test_spec)
77
78 self.assertTrue(res['changed'])
79 wapi.create_object.assert_called_once_with('testobject', {'name': 'my-simple-group'})
80
81 def test_nios_nsgroup_remove(self):
82 self.module.params = {'provider': None, 'state': 'absent', 'name': 'my-simple-group',
83 'comment': None, 'grid_primary': None}
84
85 ref = "nsgroup/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false"
86
87 test_object = [{
88 "comment": "test comment",
89 "_ref": ref,
90 "name": "my-simple-group",
91 "grid_primary": {'name': 'infoblox-test.example.com'}
92 }]
93
94 test_spec = {
95 "name": {"ib_req": True},
96 "comment": {},
97 "grid_primary": {}
98 }
99
100 wapi = self._get_wapi(test_object)
101 res = wapi.run('testobject', test_spec)
102 self.assertTrue(res['changed'])
103 wapi.delete_object.assert_called_once_with(ref)
104
105 def test_nios_nsgroup_update_comment(self):
106 self.module.params = {'provider': None, 'state': 'present', 'name': 'default',
107 'comment': 'updated comment', 'grid_primary': None}
108
109 ref = "nsgroup/ZG5zLm5ldHdvcmtfdmlldyQw:default/true"
110 test_object = [
111 {
112 "comment": "test comment",
113 "_ref": ref,
114 "name": "default",
115 "grid_primary": {}
116 }
117 ]
118
119 test_spec = {
120 "name": {"ib_req": True},
121 "comment": {},
122 "grid_primary": {}
123 }
124
125 wapi = self._get_wapi(test_object)
126 res = wapi.run('testobject', test_spec)
127
128 self.assertTrue(res['changed'])
129 wapi.update_object.assert_called_once_with(
130 ref, {'comment': 'updated comment', 'name': 'default'}
131 )