Debian Codegraph @

ansible_collections/netapp_eseries/santricity/tests/unit/modules/test_na_santricity_host.py

raw symbols SCIP
1 # (c) 2020, NetApp, Inc
2 # BSD-3 Clause (see COPYING or https://opensource.org/licenses/BSD-3-Clause)
3 from __future__ import absolute_import, division, print_function
4 __metaclass__ = type
5
6 import unittest
7 from contextlib import contextmanager
8 from ansible.module_utils.testing import patch_module_args
9 from ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_host import NetAppESeriesHost
10 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
11 AnsibleFailJson, AnsibleExitJson, ModuleTestCase
12 )
13 from ansible_collections.community.internal_test_tools.tests.unit.compat import mock
14
15
16 class HostTest(ModuleTestCase):
17 REQUIRED_PARAMS = {
18 'api_username': 'rw',
19 'api_password': 'password',
20 'api_url': 'http://localhost',
21 'ssid': '1',
22 'name': '1',
23 }
24 HOST = {
25 'name': '1',
26 'hostRef': '123',
27 'label': '1',
28 'id': '0' * 30,
29 'clusterRef': 40 * '0',
30 'hostTypeIndex': 28,
31 'hostSidePorts': [],
32 'initiators': [],
33 'ports': [],
34 }
35 HOST_ALT = {
36 'name': '2',
37 'label': '2',
38 'id': '1' * 30,
39 'clusterRef': '1',
40 'hostSidePorts': [],
41 'initiators': [],
42 'ports': [],
43 }
44 EXISTING_HOSTS = [
45 {"hostRef": "84000000600A098000A4B28D00303D065D430118", "clusterRef": "0000000000000000000000000000000000000000", "label": "Beegfs_storage1",
46 "hostTypeIndex": 28, "ports": [], "initiators": [{"initiatorRef": "89000000600A098000A4B28D00303CF55D4300E3",
47 "nodeName": {"ioInterfaceType": "iscsi",
48 "iscsiNodeName": "iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818",
49 "remoteNodeWWN": None, "nvmeNodeName": None},
50 "alias": {"ioInterfaceType": "iscsi", "iscsiAlias": ""}, "label": "beegfs_storage1_iscsi_0",
51 "hostRef": "84000000600A098000A4B28D00303D065D430118",
52 "id": "89000000600A098000A4B28D00303CF55D4300E3"}],
53 "hostSidePorts": [{"type": "iscsi", "address": "iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818", "label": "beegfs_storage1_iscsi_0"}],
54 "id": "84000000600A098000A4B28D00303D065D430118", "name": "beegfs_storage1"},
55 {"hostRef": "84000000600A098000A4B9D10030370B5D430109", "clusterRef": "0000000000000000000000000000000000000000", "label": "beegfs_metadata1",
56 "hostTypeIndex": 28, "ports": [], "initiators": [{"initiatorRef": "89000000600A098000A4B28D00303CFC5D4300F7",
57 "nodeName": {"ioInterfaceType": "iscsi",
58 "iscsiNodeName": "iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8",
59 "remoteNodeWWN": None, "nvmeNodeName": None},
60 "alias": {"ioInterfaceType": "iscsi", "iscsiAlias": ""}, "label": "beegfs_metadata1_iscsi_0",
61 "hostRef": "84000000600A098000A4B9D10030370B5D430109",
62 "id": "89000000600A098000A4B28D00303CFC5D4300F7"}],
63 "hostSidePorts": [{"type": "iscsi", "address": "iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8", "label": "beegfs_metadata1_iscsi_0"}],
64 "id": "84000000600A098000A4B9D10030370B5D430109", "name": "beegfs_metadata1"},
65 {"hostRef": "84000000600A098000A4B9D10030370B5D430109", "clusterRef": "85000000600A098000A4B9D1003637135D483DEB", "label": "beegfs_metadata2",
66 "hostTypeIndex": 28, "ports": [], "initiators": [{"initiatorRef": "89000000600A098000A4B28D00303CFC5D4300F7",
67 "nodeName": {"ioInterfaceType": "iscsi",
68 "iscsiNodeName": "iqn.used_elsewhere",
69 "remoteNodeWWN": None, "nvmeNodeName": None},
70 "alias": {"ioInterfaceType": "iscsi", "iscsiAlias": ""}, "label": "beegfs_metadata2_iscsi_0",
71 "hostRef": "84000000600A098000A4B9D10030370B5D430109",
72 "id": "89000000600A098000A4B28D00303CFC5D4300F7"}],
73 "hostSidePorts": [{"type": "iscsi", "address": "iqn.used_elsewhere", "label": "beegfs_metadata2_iscsi_0"}],
74 "id": "84000000600A098000A4B9D10030370B5D430120", "name": "beegfs_metadata2"}]
75 HOST_GROUPS = [{"clusterRef": "85000000600A098000A4B9D1003637135D483DEB", "label": "test_group", "isSAControlled": False,
76 "confirmLUNMappingCreation": False, "protectionInformationCapableAccessMethod": True, "isLun0Restricted": False,
77 "id": "85000000600A098000A4B9D1003637135D483DEB", "name": "test_group"}]
78 HOST_TYPES = [{"name": "FactoryDefault", "index": 0, "code": "FactoryDefault"},
79 {"name": "Windows 2000/Server 2003/Server 2008 Non-Clustered", "index": 1, "code": "W2KNETNCL"},
80 {"name": "Solaris", "index": 2, "code": "SOL"},
81 {"name": "Linux", "index": 6, "code": "LNX"},
82 {"name": "LnxALUA", "index": 7, "code": "LnxALUA"},
83 {"name": "Windows 2000/Server 2003/Server 2008 Clustered", "index": 8, "code": "W2KNETCL"},
84 {"name": "LnxTPGSALUA_SF", "index": 27, "code": "LnxTPGSALUA_SF"},
85 {"name": "LnxDHALUA", "index": 28, "code": "LnxDHALUA"}]
86 REQ_FUNC = 'ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_host.NetAppESeriesHost.request'
87
88 @contextmanager
89 def _set_args(self, args):
90 module_args = self.REQUIRED_PARAMS.copy()
91 module_args.update(args)
92 with patch_module_args(module_args):
93 yield
94
95 def test_host_exists_pass(self):
96 """Verify host_exists produces expected results."""
97 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
98 with self._set_args({'state': 'present', 'name': 'new_host', 'host_type': 'linux dm-mp', 'force_port': False,
99 'ports': [{'label': 'new_host_port_1', 'type': 'fc', 'port': '0x08ef08ef08ef08ef'}]}):
100 host = NetAppESeriesHost()
101 self.assertFalse(host.host_exists)
102
103 with self._set_args({'state': 'present', 'name': 'does_not_exist', 'host_type': 'linux dm-mp',
104 'ports': [{'label': 'beegfs_storage1_iscsi_0', 'type': 'iscsi',
105 'port': 'iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818'}]}):
106 host = NetAppESeriesHost()
107 self.assertFalse(host.host_exists)
108
109 with self._set_args({'state': 'present', 'name': 'beegfs_storage1', 'host_type': 'linux dm-mp',
110 'ports': [{'label': 'beegfs_storage1_iscsi_0', 'type': 'iscsi', 'port': 'iqn.differentiqn.org'}]}):
111 host = NetAppESeriesHost()
112 self.assertTrue(host.host_exists)
113
114 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
115 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': True,
116 'ports': [{'label': 'beegfs_metadata1_iscsi_0', 'type': 'iscsi',
117 'port': 'iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818'}]}):
118 host = NetAppESeriesHost()
119 self.assertTrue(host.host_exists)
120
121 def test_host_exists_fail(self):
122 """Verify host_exists produces expected exceptions."""
123 with self._set_args({'state': 'present', 'host_type': 'linux dm-mp', 'ports': [{'label': 'abc', 'type': 'iscsi', 'port': 'iqn:0'}]}):
124 host = NetAppESeriesHost()
125 with self.assertRaisesRegex(AnsibleFailJson, "Failed to determine host existence."):
126 with mock.patch(self.REQ_FUNC, return_value=Exception()):
127 exists = host.host_exists
128
129 def test_needs_update_pass(self):
130 """Verify needs_update produces expected results."""
131 # No changes
132 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
133 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp',
134 'ports': [{'label': 'beegfs_metadata1_iscsi_0', 'type': 'iscsi',
135 'port': 'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'}]}):
136 host = NetAppESeriesHost()
137 exists = host.host_exists
138 self.assertFalse(host.needs_update)
139
140 # Change host type
141 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
142 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'windows', 'force_port': False,
143 'ports': [{'label': 'beegfs_metadata1_iscsi_1', 'type': 'iscsi', 'port': 'iqn.not_used'}]}):
144 host = NetAppESeriesHost()
145 exists = host.host_exists
146 self.assertTrue(host.needs_update)
147
148 # Add port to host
149 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
150 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': False,
151 'ports': [{'label': 'beegfs_metadata1_iscsi_1', 'type': 'iscsi', 'port': 'iqn.not_used'}]}):
152 host = NetAppESeriesHost()
153 exists = host.host_exists
154 self.assertTrue(host.needs_update)
155
156 # Change port name
157 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
158 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': False,
159 'ports': [{'label': 'beegfs_metadata1_iscsi_2', 'type': 'iscsi',
160 'port': 'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'}]}):
161 host = NetAppESeriesHost()
162 exists = host.host_exists
163 self.assertTrue(host.needs_update)
164
165 # take port from another host by force
166 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
167 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': True,
168 'ports': [{'label': 'beegfs_metadata2_iscsi_0', 'type': 'iscsi',
169 'port': 'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'}]}):
170 host = NetAppESeriesHost()
171 exists = host.host_exists
172 self.assertTrue(host.needs_update)
173
174 def test_needs_update_fail(self):
175 """Verify needs_update produces expected exceptions."""
176 with self.assertRaisesRegex(AnsibleFailJson, "is associated with a different host."):
177 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
178 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': False,
179 'ports': [{'label': 'beegfs_metadata2_iscsi_0', 'type': 'iscsi',
180 'port': 'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'}]}):
181 host = NetAppESeriesHost()
182 exists = host.host_exists
183 host.needs_update
184
185 def test_valid_host_type_pass(self):
186 """Validate the available host types."""
187 with mock.patch(self.REQ_FUNC, return_value=(200, self.HOST_TYPES)):
188 with self._set_args({'state': 'present', 'host_type': '0'}):
189 host = NetAppESeriesHost()
190 self.assertTrue(host.valid_host_type)
191 with self._set_args({'state': 'present', 'host_type': '28'}):
192 host = NetAppESeriesHost()
193 self.assertTrue(host.valid_host_type)
194 with self._set_args({'state': 'present', 'host_type': 'windows'}):
195 host = NetAppESeriesHost()
196 self.assertTrue(host.valid_host_type)
197 with self._set_args({'state': 'present', 'host_type': 'linux dm-mp'}):
198 host = NetAppESeriesHost()
199 self.assertTrue(host.valid_host_type)
200
201 def test_valid_host_type_fail(self):
202 """Validate the available host types."""
203 with self.assertRaisesRegex(AnsibleFailJson, "host_type must be either a host type name or host type index found integer the documentation"):
204 with self._set_args({'state': 'present', 'host_type': 'non-host-type'}):
205 host = NetAppESeriesHost()
206
207 with mock.patch(self.REQ_FUNC, return_value=(200, self.HOST_TYPES)):
208 with self.assertRaisesRegex(AnsibleFailJson, "There is no host type with index"):
209 with self._set_args({'state': 'present', 'host_type': '4'}):
210 host = NetAppESeriesHost()
211 valid = host.valid_host_type
212
213 with mock.patch(self.REQ_FUNC, return_value=Exception()):
214 with self.assertRaisesRegex(AnsibleFailJson, "Failed to get host types."):
215 with self._set_args({'state': 'present', 'host_type': '4'}):
216 host = NetAppESeriesHost()
217 valid = host.valid_host_type
218
219 @unittest.skip("Test needs to be reworked.")
220 def test_assigned_host_ports_pass(self):
221 """Verify assigned_host_ports gives expected results."""
222
223 # Add an unused port to host
224 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
225 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': False,
226 'ports': [{'label': 'beegfs_metadata1_iscsi_1', 'type': 'iscsi', 'port': 'iqn.not_used'}]}):
227 host = NetAppESeriesHost()
228 exists = host.host_exists
229 self.assertTrue(host.needs_update)
230 self.assertEqual(host.assigned_host_ports(), {})
231
232 # Change port name (force)
233 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
234 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': True,
235 'ports': [{'label': 'beegfs_metadata1_iscsi_2', 'type': 'iscsi',
236 'port': 'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'}]}):
237 host = NetAppESeriesHost()
238 exists = host.host_exists
239 self.assertTrue(host.needs_update)
240 self.assertEqual(host.assigned_host_ports(), {'84000000600A098000A4B9D10030370B5D430109': ['89000000600A098000A4B28D00303CFC5D4300F7']})
241
242 # Change port type
243 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
244 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': True,
245 'ports': [{'label': 'beegfs_metadata1_iscsi_1', 'type': 'fc', 'port': '08:ef:7e:24:52:a0'}]}):
246 host = NetAppESeriesHost()
247 exists = host.host_exists
248 self.assertTrue(host.needs_update)
249 self.assertEqual(host.assigned_host_ports(), {})
250
251 # take port from another host by force
252 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
253 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': True,
254 'ports': [{'label': 'beegfs_metadata2_iscsi_0', 'type': 'iscsi', 'port': 'iqn.used_elsewhere'}]}):
255 host = NetAppESeriesHost()
256 exists = host.host_exists
257 self.assertTrue(host.needs_update)
258 self.assertEqual(host.assigned_host_ports(), {'84000000600A098000A4B9D10030370B5D430109': ['89000000600A098000A4B28D00303CFC5D4300F7']})
259
260 # take port from another host by force
261 with mock.patch(self.REQ_FUNC, side_effect=[(200, self.EXISTING_HOSTS), (200, {})]):
262 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': True,
263 'ports': [{'label': 'beegfs_metadata2_iscsi_0', 'type': 'iscsi', 'port': 'iqn.used_elsewhere'}]}):
264 host = NetAppESeriesHost()
265 exists = host.host_exists
266 self.assertTrue(host.needs_update)
267 self.assertEqual(host.assigned_host_ports(apply_unassigning=True),
268 {'84000000600A098000A4B9D10030370B5D430109': ['89000000600A098000A4B28D00303CFC5D4300F7']})
269
270 @unittest.skip("Test needs to be reworked.")
271 def test_assigned_host_ports_fail(self):
272 """Verify assigned_host_ports gives expected exceptions."""
273 # take port from another
274 with self.assertRaisesRegex(AnsibleFailJson, "There are no host ports available OR there are not enough unassigned host ports"):
275 with mock.patch(self.REQ_FUNC, side_effect=[(200, self.EXISTING_HOSTS)]):
276 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': False,
277 'ports': [{'label': 'beegfs_metadata1_iscsi_2', 'type': 'iscsi',
278 'port': 'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'}]}):
279 host = NetAppESeriesHost()
280 exists = host.host_exists
281 self.assertTrue(host.needs_update)
282 host.assigned_host_ports(apply_unassigning=True)
283
284 # take port from another host and fail because force == False
285 with self.assertRaisesRegex(AnsibleFailJson, "There are no host ports available OR there are not enough unassigned host ports"):
286 with mock.patch(self.REQ_FUNC, side_effect=[(200, self.EXISTING_HOSTS)]):
287 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': False,
288 'ports': [{'label': 'beegfs_metadata2_iscsi_0', 'type': 'iscsi', 'port': 'iqn.used_elsewhere'}]}):
289 host = NetAppESeriesHost()
290 exists = host.host_exists
291 self.assertTrue(host.needs_update)
292 host.assigned_host_ports(apply_unassigning=True)
293
294 # take port from another host and fail because force == False
295 with self.assertRaisesRegex(AnsibleFailJson, "There are no host ports available OR there are not enough unassigned host ports"):
296 with mock.patch(self.REQ_FUNC, side_effect=[(200, self.EXISTING_HOSTS)]):
297 with self._set_args({'state': 'present', 'name': 'beegfs_metadata3', 'host_type': 'linux dm-mp', 'force_port': False,
298 'ports': [{'label': 'beegfs_metadata2_iscsi_0', 'type': 'iscsi', 'port': 'iqn.used_elsewhere'}]}):
299 host = NetAppESeriesHost()
300 exists = host.host_exists
301 host.assigned_host_ports(apply_unassigning=True)
302
303 with self.assertRaisesRegex(AnsibleFailJson, "Failed to unassign host port."):
304 with mock.patch(self.REQ_FUNC, side_effect=[(200, self.EXISTING_HOSTS), Exception()]):
305 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': True,
306 'ports': [{'label': 'beegfs_metadata2_iscsi_0', 'type': 'iscsi', 'port': 'iqn.used_elsewhere'}]}):
307 host = NetAppESeriesHost()
308 exists = host.host_exists
309 self.assertTrue(host.needs_update)
310 host.assigned_host_ports(apply_unassigning=True)
311
312 def test_update_host_pass(self):
313 """Verify update_host produces expected results."""
314 # Change host type
315 with self.assertRaises(AnsibleExitJson):
316 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
317 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'windows', 'force_port': True,
318 'ports': [{'label': 'beegfs_metadata1_iscsi_1', 'type': 'iscsi',
319 'port': 'iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818'}]}):
320 host = NetAppESeriesHost()
321 host.build_success_payload = lambda x: {}
322 exists = host.host_exists
323 self.assertTrue(host.needs_update)
324 host.update_host()
325
326 # Change port iqn
327 with self.assertRaises(AnsibleExitJson):
328 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
329 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': False,
330 'ports': [{'label': 'beegfs_metadata1_iscsi_1', 'type': 'iscsi', 'port': 'iqn.not_used'}]}):
331 host = NetAppESeriesHost()
332 host.build_success_payload = lambda x: {}
333 exists = host.host_exists
334 self.assertTrue(host.needs_update)
335 host.update_host()
336
337 # Change port type to fc
338 with self.assertRaises(AnsibleExitJson):
339 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
340 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': False,
341 'ports': [{'label': 'beegfs_metadata1_iscsi_1', 'type': 'fc', 'port': '0x08ef08ef08ef08ef'}]}):
342 host = NetAppESeriesHost()
343 host.build_success_payload = lambda x: {}
344 exists = host.host_exists
345 self.assertTrue(host.needs_update)
346 host.update_host()
347
348 # Change port name
349 with self.assertRaises(AnsibleExitJson):
350 with mock.patch(self.REQ_FUNC, return_value=(200, self.EXISTING_HOSTS)):
351 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'windows', 'force_port': True,
352 'ports': [{'label': 'beegfs_metadata1_iscsi_12', 'type': 'iscsi',
353 'port': 'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'}]}):
354 host = NetAppESeriesHost()
355 host.build_success_payload = lambda x: {}
356 exists = host.host_exists
357 self.assertTrue(host.needs_update)
358 host.update_host()
359
360 def test_update_host_fail(self):
361 """Verify update_host produces expected exceptions."""
362 with self.assertRaisesRegex(AnsibleFailJson, "Failed to update host."):
363 with mock.patch(self.REQ_FUNC, side_effect=[(200, self.EXISTING_HOSTS), Exception()]):
364 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'windows', 'force_port': False,
365 'ports': [{'label': 'beegfs_metadata1_iscsi_0', 'type': 'iscsi',
366 'port': 'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'}]}):
367 host = NetAppESeriesHost()
368 host.build_success_payload = lambda x: {}
369 exists = host.host_exists
370 self.assertTrue(host.needs_update)
371 host.update_host()
372
373 def test_create_host_pass(self):
374 """Verify create_host produces expected results."""
375 def _assigned_host_ports(apply_unassigning=False):
376 return None
377
378 with self.assertRaises(AnsibleExitJson):
379 with mock.patch(self.REQ_FUNC, return_value=(200, {'id': '84000000600A098000A4B9D10030370B5D430109'})):
380 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'windows', 'force_port': True,
381 'ports': [{'label': 'beegfs_metadata1_iscsi_1', 'type': 'iscsi',
382 'port': 'iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818'}]}):
383 host = NetAppESeriesHost()
384 with mock.patch(self.REQ_FUNC, return_value=(200, [])):
385 host.assigned_host_ports = _assigned_host_ports
386 host.build_success_payload = lambda x: {}
387 host.create_host()
388
389 def test_create_host_fail(self):
390 """Verify create_host produces expected exceptions."""
391 def _assigned_host_ports(apply_unassigning=False):
392 return None
393
394 with self.assertRaisesRegex(AnsibleFailJson, "Failed to create host."):
395 with mock.patch(self.REQ_FUNC, side_effect=[(200, []), Exception()]):
396 with self._set_args({'state': 'present', 'name': 'beegfs_metadata1', 'host_type': 'windows', 'force_port': True,
397 'ports': [{'label': 'beegfs_metadata1_iscsi_1', 'type': 'iscsi',
398 'port': 'iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818'}]}):
399 host = NetAppESeriesHost()
400 host.assigned_host_ports = _assigned_host_ports
401 host.build_success_payload = lambda x: {}
402 host.create_host()
403
404 with self.assertRaisesRegex(AnsibleExitJson, "Host already exists."):
405 with mock.patch(self.REQ_FUNC, side_effect=[(200, self.EXISTING_HOSTS)]):
406 with self._set_args({'state': 'present', 'name': 'beegfs_storage1', 'host_type': 'linux dm-mp', 'force_port': True,
407 'ports': [{'label': 'beegfs_storage1_iscsi_0', 'type': 'iscsi',
408 'port': 'iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818'}]}):
409 host = NetAppESeriesHost()
410 host.assigned_host_ports = _assigned_host_ports
411 host.build_success_payload = lambda x: {}
412 host.create_host()
413
414 def test_remove_host_pass(self):
415 """Verify remove_host produces expected results."""
416 with mock.patch(self.REQ_FUNC, return_value=(200, None)):
417 with self._set_args({'state': 'absent', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': False,
418 'ports': [{'label': 'beegfs_metadata1_iscsi_0', 'type': 'iscsi',
419 'port': 'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'}]}):
420 host = NetAppESeriesHost()
421 host.host_obj = {"id": "84000000600A098000A4B9D10030370B5D430109"}
422 host.remove_host()
423
424 def test_remove_host_fail(self):
425 """Verify remove_host produces expected exceptions."""
426 with self.assertRaisesRegex(AnsibleFailJson, "Failed to remove host."):
427 with mock.patch(self.REQ_FUNC, return_value=Exception()):
428 with self._set_args({'state': 'absent', 'name': 'beegfs_metadata1', 'host_type': 'linux dm-mp', 'force_port': False,
429 'ports': [{'label': 'beegfs_metadata1_iscsi_0', 'type': 'iscsi',
430 'port': 'iqn.1993-08.org.debian.beegfs-metadata:01:69e4efdf30b8'}]}):
431 host = NetAppESeriesHost()
432 host.host_obj = {"id": "84000000600A098000A4B9D10030370B5D430109"}
433 host.remove_host()
434
435 def test_build_success_payload(self):
436 """Validate success payload."""
437 def _assigned_host_ports(apply_unassigning=False):
438 return None
439
440 with self._set_args({
441 'state': 'present',
442 'name': 'beegfs_metadata1',
443 'host_type': 'windows',
444 'force_port': True,
445 'ports': [{
446 'label': 'beegfs_metadata1_iscsi_1',
447 'type': 'iscsi',
448 'port': 'iqn.1993-08.org.debian.beegfs-storage1:01:b0621126818'
449 }]
450 }):
451 host = NetAppESeriesHost()
452 self.assertEqual(host.build_success_payload(), {'api_url': 'http://localhost/', 'ssid': '1'})