Debian Codegraph @

ansible_collections/infoblox/nios_modules/tests/unit/plugins/modules/test_nios_member.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_member
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 TestNiosMemberModule(TestNiosModule):
28
29 module = nios_member
30
31 def setUp(self):
32 super(TestNiosMemberModule, self).setUp()
33 self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_member.WapiModule')
34 self.module.check_mode = False
35 self.module.params = {'provider': None}
36 self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_member.WapiModule')
37 self.exec_command = self.mock_wapi.start()
38 self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_member.WapiModule.run')
39 self.load_config = self.mock_wapi_run.start()
40
41 def tearDown(self):
42 super(TestNiosMemberModule, self).tearDown()
43 self.mock_wapi.stop()
44 self.mock_wapi_run.stop()
45
46 def load_fixtures(self, commands=None):
47 self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None)
48 self.load_config.return_value = dict(diff=None, session='session')
49
50 def _get_wapi(self, test_object):
51 wapi = api.WapiModule(self.module)
52 wapi.get_object = Mock(name='get_object', return_value=test_object)
53 wapi.create_object = Mock(name='create_object')
54 wapi.update_object = Mock(name='update_object')
55 wapi.delete_object = Mock(name='delete_object')
56 return wapi
57
58 def test_nios_member_create(self):
59 self.module.params = {'provider': None, 'state': 'present', 'host_name': 'test_member',
60 'vip_setting': {'address': '192.168.1.110', 'subnet_mask': '255.255.255.0', 'gateway': '192.168.1.1'},
61 'config_addr_type': 'IPV4', 'platform': 'VNIOS', 'comment': None, 'extattrs': None}
62
63 test_object = None
64 test_spec = {
65 "host_name": {"ib_req": True},
66 "vip_setting": {},
67 "config_addr_type": {},
68 "platform": {},
69 "comment": {},
70 "extattrs": {}
71 }
72
73 wapi = self._get_wapi(test_object)
74 res = wapi.run('testobject', test_spec)
75
76 self.assertTrue(res['changed'])
77 wapi.create_object.assert_called_once_with('testobject', {'host_name': 'test_member',
78 'vip_setting': {'address': '192.168.1.110', 'subnet_mask': '255.255.255.0',
79 'gateway': '192.168.1.1'},
80 'config_addr_type': 'IPV4', 'platform': 'VNIOS'})
81
82 def test_nios_member_update(self):
83 self.module.params = {'provider': None, 'state': 'present', 'host_name': 'test_member',
84 'vip_setting': {'address': '192.168.1.110', 'subnet_mask': '255.255.255.0', 'gateway': '192.168.1.1'},
85 'config_addr_type': 'IPV4', 'platform': 'VNIOS', 'comment': 'updated comment', 'extattrs': None}
86 ref = "member/b25lLnZpcnR1YWxfbm9kZSQ3:member01.ansible-dev.com"
87 test_object = [
88 {
89 "comment": "Created with Ansible",
90 "_ref": ref,
91 "config_addr_type": "IPV4",
92 "host_name": "member01.ansible-dev.com",
93 "platform": "VNIOS",
94 "service_type_configuration": "ALL_V4",
95 "vip_setting":
96 {
97 "address": "192.168.1.100",
98 "dscp": 0,
99 "gateway": "192.168.1.1",
100 "primary": True,
101 "subnet_mask": "255.255.255.0",
102 "use_dscp": False
103 }
104 }
105 ]
106
107 test_spec = {
108 "host_name": {"ib_req": True},
109 "vip_setting": {},
110 "config_addr_type": {},
111 "platform": {},
112 "comment": {},
113 "extattrs": {}
114 }
115
116 wapi = self._get_wapi(test_object)
117 res = wapi.run('testobject', test_spec)
118
119 self.assertTrue(res['changed'])
120 wapi.update_object.assert_called_once_with(
121 ref,
122 {'comment': 'updated comment', 'host_name': 'test_member',
123 'vip_setting': {'address': '192.168.1.110', 'subnet_mask': '255.255.255.0', 'gateway': '192.168.1.1'},
124 'config_addr_type': 'IPV4', 'platform': 'VNIOS'}
125 )
126
127 def test_nios_member_remove(self):
128 self.module.params = {'provider': None, 'state': 'absent', 'host_name': 'test_member',
129 'vip_setting': {'address': '192.168.1.110', 'subnet_mask': '255.255.255.0', 'gateway': '192.168.1.1'},
130 'config_addr_type': 'IPV4', 'platform': 'VNIOS', 'comment': 'updated comment', 'extattrs': None}
131
132 ref = "member/b25lLnZpcnR1YWxfbm9kZSQ3:member01.ansible-dev.com"
133
134 test_object = [
135 {
136 "comment": "Created with Ansible",
137 "_ref": "member/b25lLnZpcnR1YWxfbm9kZSQ3:member01.ansible-dev.com",
138 "config_addr_type": "IPV4",
139 "host_name": "member01.ansible-dev.com",
140 "platform": "VNIOS",
141 "service_type_configuration": "ALL_V4",
142 "vip_setting":
143 {
144 "address": "192.168.1.100",
145 "dscp": 0,
146 "gateway": "192.168.1.1",
147 "primary": True,
148 "subnet_mask": "255.255.255.0",
149 "use_dscp": False
150 }
151 }
152 ]
153
154 test_spec = {
155 "host_name": {"ib_req": True},
156 "vip_setting": {},
157 "config_addr_type": {},
158 "platform": {},
159 "comment": {},
160 "extattrs": {}
161 }
162
163 wapi = self._get_wapi(test_object)
164 res = wapi.run('testobject', test_spec)
165
166 self.assertTrue(res['changed'])
167 wapi.delete_object.assert_called_once_with(ref)