Debian Codegraph @

ansible_collections/infoblox/nios_modules/tests/unit/plugins/modules/test_nios_vlan.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
18
19 from __future__ import (absolute_import, division, print_function)
20 __metaclass__ = type
21
22
23 from ansible_collections.infoblox.nios_modules.plugins.modules import nios_vlan
24 from ansible_collections.infoblox.nios_modules.plugins.module_utils import api
25 from ansible_collections.infoblox.nios_modules.tests.unit.compat.mock import patch, MagicMock, Mock
26 from .test_nios_module import TestNiosModule, load_fixture
27
28
29 class TestNiosVlanModule(TestNiosModule):
30
31 module = nios_vlan
32
33 def setUp(self):
34 super(TestNiosVlanModule, self).setUp()
35 self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_vlan.WapiModule')
36 self.module.check_mode = False
37 self.module.params = {'provider': None}
38 self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_vlan.WapiModule')
39 self.exec_command = self.mock_wapi.start()
40 self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_vlan.WapiModule.run')
41 self.load_config = self.mock_wapi_run.start()
42 self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict')
43 self.mock_check_type_dict_obj = self.mock_check_type_dict.start()
44
45 def tearDown(self):
46 super(TestNiosVlanModule, self).tearDown()
47 self.mock_wapi.stop()
48 self.mock_wapi_run.stop()
49 self.mock_check_type_dict.stop()
50
51 def _get_wapi(self, test_object):
52 wapi = api.WapiModule(self.module)
53 wapi.get_object = Mock(name='get_object', return_value=test_object)
54 wapi.create_object = Mock(name='create_object')
55 wapi.update_object = Mock(name='update_object')
56 wapi.delete_object = Mock(name='delete_object')
57 return wapi
58
59 def load_fixtures(self, commands=None):
60 self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None)
61 self.load_config.return_value = dict(diff=None, session='session')
62
63 def test_nios_vlan_create(self):
64 self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible_vlan',
65 'parent': 'default', 'id': '10', 'comment': None, 'extattrs': None}
66
67 test_object = None
68
69 test_spec = {
70 "name": {"ib_req": True},
71 "parent": {"ib_req": True},
72 "id": {"ib_req": True},
73 "comment": {},
74 "extattrs": {}
75 }
76
77 wapi = self._get_wapi(test_object)
78 print("WAPI: ", wapi.__dict__)
79 res = wapi.run('NIOS_VLAN', test_spec)
80
81 self.assertTrue(res['changed'])
82 wapi.create_object.assert_called_once_with(
83 'NIOS_VLAN',
84 {
85 'name': 'ansible_vlan',
86 'parent': 'default',
87 'id': '10'
88 }
89 )
90
91 def test_nios_vlan_update_comment(self):
92 self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible_vlan',
93 'parent': 'default', 'id': '10', 'comment': 'updated comment',
94 'contact': '[email protected]', 'department': 'IT', 'description': 'test',
95 'reserved': True, 'extattrs': None}
96
97 ref = "vlan/ZG5zLm5ldHdvcmtfdmlldyQw:ansible_vlan"
98 test_object = [
99 {
100 "comment": "test comment",
101 "_ref": ref,
102 "name": "ansible_vlan",
103 "parent": "default",
104 "id": "10",
105 "extattrs": {}
106 }
107 ]
108
109 test_spec = {
110 "name": {"ib_req": True},
111 "parent": {"ib_req": True},
112 "id": {"ib_req": True},
113 "comment": {},
114 "contact": {},
115 "department": {},
116 "description": {},
117 "reserved": {},
118 "extattrs": {}
119 }
120
121 wapi = self._get_wapi(test_object)
122 res = wapi.run('NIOS_VLAN', test_spec)
123 self.assertTrue(res['changed'])
124 wapi.update_object.assert_called_once_with(
125 ref, {'comment': 'updated comment', 'parent': 'default', 'id': '10', 'name': 'ansible_vlan',
126 'contact': '[email protected]', 'department': 'IT', 'description': 'test', 'reserved': True}
127 )
128
129 def test_nios_vlan_remove(self):
130 self.module.params = {'provider': None, 'state': 'absent', 'name': 'ansible_vlan',
131 'parent': 'default', 'id': '10', 'comment': None, 'extattrs': None}
132
133 ref = "vlan/ZG5zLm5ldHdvcmtfdmlldyQw:ansible_vlan"
134
135 test_object = [{
136 "comment": "test comment",
137 "_ref": ref,
138 "name": "ansible_vlan",
139 "parent": "default",
140 "id": "10",
141 "extattrs": {'Site': {'value': 'test'}}
142 }]
143
144 test_spec = {
145 "name": {"ib_req": True},
146 "parent": {"ib_req": True},
147 "id": {"ib_req": True},
148 "comment": {},
149 "extattrs": {}
150 }
151
152 wapi = self._get_wapi(test_object)
153 res = wapi.run('NIOS_VLAN', test_spec)
154
155 self.assertTrue(res['changed'])
156 wapi.delete_object.assert_called_once_with(ref)
157
158 def test_nios_vlan_update_record_name(self):
159 self.module.params = {'provider': None, 'state': 'present', 'name': {'new_name': 'ansible_new_vlan', 'old_name': 'ansible_vlan'},
160 'parent': 'default', 'id': '10', 'comment': 'comment', 'extattrs': None}
161
162 ref = "vlan/ZG5zLm5ldHdvcmtfdmlldyQw:ansible_new_vlan"
163 test_object = [
164 {
165 "comment": "test comment",
166 "_ref": ref,
167 "name": "ansible_vlan",
168 "extattrs": {}
169 }
170 ]
171
172 test_spec = {
173 "name": {"ib_req": True},
174 "parent": {"ib_req": True},
175 "id": {"ib_req": True},
176 "comment": {},
177 "extattrs": {}
178 }
179
180 wapi = self._get_wapi(test_object)
181 res = wapi.run('NIOS_VLAN', test_spec)
182
183 self.assertTrue(res['changed'])
184 wapi.update_object.assert_called_once_with(ref, {'name': 'ansible_new_vlan', 'parent': 'default', 'id': '10',
185 'comment': 'comment'})