Debian Codegraph @

ansible_collections/netapp/ontap/tests/unit/plugins/module_utils/test_netapp_zapi.py

raw
1 # Copyright (c) 2018-2022 NetApp
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 ''' unit tests for module_utils netapp.py - ZAPI related features '''
5 from __future__ import (absolute_import, division, print_function)
6 __metaclass__ = type
7
8 import pytest
9 import sys
10
11 from ansible.module_utils import basic
12 from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch
13 # pylint: disable=unused-import
14 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import \
15 patch_ansible, create_module, expect_and_capture_ansible_exception, assert_warning_was_raised, print_warnings
16 from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import patch_request_and_invoke, register_responses, get_mock_record
17 from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_raw_xml_response, build_zapi_error, zapi_responses
18 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
19
20 if not netapp_utils.has_netapp_lib():
21 pytestmark = pytest.mark.skip("skipping as missing required netapp_lib")
22
23 if not hasattr(netapp_utils.ssl, 'create_default_context') or not hasattr(netapp_utils.ssl, 'SSLContext'):
24 pytestmark = pytest.mark.skip("skipping as missing required ssl package with SSLContext support")
25
26 ZRR = zapi_responses({
27 'error_no_vserver': build_zapi_error(12345, 'Vserver API missing vserver parameter.'),
28 'error_connection_error': build_zapi_error(12345, 'URLError'),
29 'error_other_error': build_zapi_error(12345, 'Some other error message.'),
30 })
31
32
33 DEFAULT_ARGS = {
34 'hostname': 'test',
35 'username': 'test_user',
36 'password': 'test_pass!',
37 'cert_filepath': None,
38 'key_filepath': None,
39 }
40
41 CERT_ARGS = {
42 'hostname': 'test',
43 'cert_filepath': 'test_pem.pem',
44 'key_filepath': 'test_key.key'
45 }
46
47
48 class MockONTAPModule:
49 def __init__(self):
50 self.module = basic.AnsibleModule(netapp_utils.na_ontap_host_argument_spec())
51
52
53 def create_ontap_module(default_args, module_args=None):
54 return create_module(MockONTAPModule, default_args, module_args).module
55
56
57 def create_ontapzapicx_object(default_args, module_args=None):
58 ontap_mock = create_module(MockONTAPModule, default_args, module_args)
59 my_args = {'module': ontap_mock.module}
60 for key in 'hostname', 'username', 'password', 'cert_filepath', 'key_filepath':
61 if key in ontap_mock.module.params:
62 my_args[key] = ontap_mock.module.params[key]
63 return netapp_utils.OntapZAPICx(**my_args)
64
65
66 def test_get_cserver():
67 ''' validate cluster vserser name is correctly retrieved '''
68 register_responses([
69 ('vserver-get-iter', ZRR['cserver']),
70 ])
71 server = netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(DEFAULT_ARGS))
72 cserver = netapp_utils.get_cserver(server)
73 assert cserver == 'cserver'
74
75
76 def test_get_cserver_none():
77 ''' validate cluster vserser name is correctly retrieved '''
78 register_responses([
79 ('vserver-get-iter', ZRR['empty']),
80 ])
81 server = netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(DEFAULT_ARGS))
82 cserver = netapp_utils.get_cserver(server)
83 assert cserver is None
84
85
86 def test_negative_get_cserver():
87 ''' validate NaApiError is correctly reported '''
88 register_responses([
89 ('vserver-get-iter', ZRR['error']),
90 ])
91 server = netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(DEFAULT_ARGS))
92 assert expect_and_capture_ansible_exception(netapp_utils.get_cserver, netapp_utils.zapi.NaApiError, server)
93
94
95 def test_negative_get_cserver_connection_error():
96 ''' validate NaApiError error is correctly ignore for connection or autorization issues '''
97 register_responses([
98 ('vserver-get-iter', ZRR['error_connection_error']),
99 ])
100 server = netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(DEFAULT_ARGS))
101 cserver = netapp_utils.get_cserver(server)
102 assert cserver is None
103
104
105 def test_setup_na_ontap_zapi_logging():
106 module_args = {'feature_flags': {'trace_apis': False}}
107 server = netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(DEFAULT_ARGS, module_args))
108 assert not server._trace
109 module_args = {'feature_flags': {'trace_apis': True}}
110 server = netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(DEFAULT_ARGS, module_args))
111 assert server._trace
112
113
114 def test_setup_na_ontap_zapi_auth_method_and_https():
115 module_args = {'feature_flags': {'trace_apis': False}}
116 server = netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(DEFAULT_ARGS, module_args))
117 assert server._auth_style == server.STYLE_LOGIN_PASSWORD
118 assert server.get_port() == '80'
119 module_args = {'feature_flags': {'trace_apis': True}}
120 server = netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(CERT_ARGS, module_args))
121 assert server._auth_style == server.STYLE_CERTIFICATE
122 assert server.get_port() == '443'
123
124
125 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.HAS_NETAPP_LIB', False)
126 def test_negative_setup_na_ontap_zapi():
127 error = 'Error: the python NetApp-Lib module is required. Import error: None'
128 assert expect_and_capture_ansible_exception(netapp_utils.setup_na_ontap_zapi, 'fail', create_ontap_module(DEFAULT_ARGS))['msg'] == error
129
130
131 def test_set_zapi_port_and_transport():
132 server = netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(DEFAULT_ARGS))
133 netapp_utils.set_zapi_port_and_transport(server, True, None, False)
134 assert server.get_port() == '443'
135 assert server.get_transport_type() == 'https'
136 netapp_utils.set_zapi_port_and_transport(server, False, None, False)
137 assert server.get_port() == '80'
138 assert server.get_transport_type() == 'http'
139
140
141 @patch('ssl.SSLContext.load_cert_chain')
142 def test_certificate_method_zapi(mock_ssl):
143 ''' should fail when trying to read the certificate file '''
144 zapi_cx = create_ontapzapicx_object(CERT_ARGS)
145 assert isinstance(zapi_cx._create_certificate_auth_handler(), netapp_utils.zapi.urllib.request.HTTPSHandler)
146 assert zapi_cx._get_url() == 'http://test:80/servlets/netapp.servlets.admin.XMLrequest_filer'
147
148
149 def test_certificate_method_zapi_missing_files():
150 ''' should fail when trying to read the certificate file '''
151 zapi_cx = create_ontapzapicx_object(CERT_ARGS)
152 msg1 = 'Cannot load SSL certificate, check files exist.'
153 # for python 2,6 :(
154 msg2 = 'SSL certificate authentication requires python 2.7 or later.'
155 assert expect_and_capture_ansible_exception(zapi_cx._create_certificate_auth_handler, 'fail')['msg'].startswith((msg1, msg2))
156 assert zapi_cx._get_url() == 'http://test:80/servlets/netapp.servlets.admin.XMLrequest_filer'
157
158
159 @patch('ssl.create_default_context')
160 def test_negative_certificate_method_zapi(mock_ssl):
161 ''' should fail when trying to set context '''
162 mock_ssl.side_effect = AttributeError('for test purpose')
163 zapi_cx = create_ontapzapicx_object(CERT_ARGS)
164 # AttributeError('for test purpose') with 3.x but AttributeError('for test purpose',) with 2.7
165 error = "SSL certificate authentication requires python 2.7 or later. More info: AttributeError('for test purpose'"
166 assert expect_and_capture_ansible_exception(zapi_cx._create_certificate_auth_handler, 'fail')['msg'].startswith(error)
167
168
169 def test_classify_zapi_exception_cluster_only():
170 ''' verify output matches expectations '''
171 code = 13005
172 message = 'Unable to find API: diagnosis-alert-get-iter on data vserver trident_svm'
173 zapi_exception = netapp_utils.zapi.NaApiError(code, message)
174 kind, new_message = netapp_utils.classify_zapi_exception(zapi_exception)
175 assert kind == 'missing_vserver_api_error'
176 assert new_message.endswith("%d:%s" % (code, message))
177
178
179 def test_classify_zapi_exception_rpc_error():
180 ''' verify output matches expectations '''
181 code = 13001
182 message = "RPC: Couldn't make connection [from mgwd on node \"laurentn-vsim1\" (VSID: -1) to mgwd at 172.32.78.223]"
183 error_message = 'NetApp API failed. Reason - %d:%s' % (code, message)
184 zapi_exception = netapp_utils.zapi.NaApiError(code, message)
185 kind, new_message = netapp_utils.classify_zapi_exception(zapi_exception)
186 assert kind == 'rpc_error'
187 assert new_message == error_message
188
189
190 def test_classify_zapi_exception_other_error():
191 ''' verify output matches expectations '''
192 code = 13008
193 message = 'whatever'
194 error_message = 'NetApp API failed. Reason - %d:%s' % (code, message)
195 zapi_exception = netapp_utils.zapi.NaApiError(code, message)
196 kind, new_message = netapp_utils.classify_zapi_exception(zapi_exception)
197 assert kind == 'other_error'
198 assert new_message == error_message
199
200
201 def test_classify_zapi_exception_attributeerror():
202 ''' verify output matches expectations '''
203 zapi_exception = 'invalid'
204 kind, new_message = netapp_utils.classify_zapi_exception(zapi_exception)
205 assert kind == 'other_error'
206 assert new_message == zapi_exception
207
208
209 def test_zapi_parse_response_sanitized():
210 ''' should not fail when trying to read invalid XML characters (\x08) '''
211 zapi_cx = create_ontapzapicx_object(DEFAULT_ARGS)
212 response = b"<?xml version='1.0' encoding='UTF-8' ?>\n<!DOCTYPE netapp SYSTEM 'file:/etc/netapp_gx.dtd'>\n"
213 response += b"<netapp version='1.180' xmlns='http://www.netapp.com/filer/admin'>\n<results status=\"passed\">"
214 response += b"<cli-output> (cluster log-forwarding create)\n\n"
215 response += b"Testing network connectivity to the destination host 10.10.10.10. \x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\n\n"
216 response += b"Error: command failed: Cannot contact destination host (10.10.10.10) from node\n"
217 response += b" &quot;laurentn-vsim1&quot;. Verify connectivity to desired host or skip the\n"
218 response += b" connectivity check with the &quot;-force&quot; parameter.</cli-output>"
219 response += b"<cli-result-value>0</cli-result-value></results></netapp>\n"
220 # Manually extract cli-output contents
221 cli_output = response.split(b'<cli-output>')[1]
222 cli_output = cli_output.split(b'</cli-output>')[0]
223 cli_output = cli_output.replace(b'&quot;', b'"')
224 # the XML parser would chole on \x08, zapi_cx._parse_response replaces them with '.'
225 cli_output = cli_output.replace(b'\x08', b'.')
226 # Use xml parser to extract cli-output contents
227 xml = zapi_cx._parse_response(response)
228 results = xml.get_child_by_name('results')
229 new_cli_output = results.get_child_content('cli-output')
230 assert cli_output.decode() == new_cli_output
231
232
233 def test_zapi_parse_response_unsanitized():
234 ''' should fail when trying to read invalid XML characters (\x08) '''
235 # use feature_flags to disable sanitization
236 module_args = {'feature_flags': {'sanitize_xml': False}}
237 zapi_cx = create_ontapzapicx_object(DEFAULT_ARGS, module_args)
238 response = b"<?xml version='1.0' encoding='UTF-8' ?>\n<!DOCTYPE netapp SYSTEM 'file:/etc/netapp_gx.dtd'>\n"
239 response += b"<netapp version='1.180' xmlns='http://www.netapp.com/filer/admin'>\n<results status=\"passed\">"
240 response += b"<cli-output> (cluster log-forwarding create)\n\n"
241 response += b"Testing network connectivity to the destination host 10.10.10.10. \x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\n\n"
242 response += b"Error: command failed: Cannot contact destination host (10.10.10.10) from node\n"
243 response += b" &quot;laurentn-vsim1&quot;. Verify connectivity to desired host or skip the\n"
244 response += b" connectivity check with the &quot;-force&quot; parameter.</cli-output>"
245 response += b"<cli-result-value>0</cli-result-value></results></netapp>\n"
246 with pytest.raises(netapp_utils.zapi.etree.XMLSyntaxError) as exc:
247 zapi_cx._parse_response(response)
248 msg = 'PCDATA invalid Char value 8'
249 assert exc.value.msg.startswith(msg)
250
251
252 def test_zapi_cx_add_auth_header():
253 ''' should add header '''
254 module = create_ontap_module(DEFAULT_ARGS)
255 zapi_cx = netapp_utils.setup_na_ontap_zapi(module)
256 assert isinstance(zapi_cx, netapp_utils.OntapZAPICx)
257 assert zapi_cx.base64_creds is not None
258 request, dummy = zapi_cx._create_request(netapp_utils.zapi.NaElement('dummy_tag'))
259 assert "Authorization" in [x[0] for x in request.header_items()]
260
261
262 def test_zapi_cx_add_auth_header_explicit():
263 ''' should add header '''
264 module_args = {'feature_flags': {'classic_basic_authorization': False}}
265 module = create_ontap_module(DEFAULT_ARGS, module_args)
266 zapi_cx = netapp_utils.setup_na_ontap_zapi(module)
267 assert isinstance(zapi_cx, netapp_utils.OntapZAPICx)
268 assert zapi_cx.base64_creds is not None
269 request, dummy = zapi_cx._create_request(netapp_utils.zapi.NaElement('dummy_tag'))
270 assert "Authorization" in [x[0] for x in request.header_items()]
271
272
273 def test_zapi_cx_no_auth_header():
274 ''' should add header '''
275 module_args = {'feature_flags': {'classic_basic_authorization': True, 'always_wrap_zapi': False}}
276 module = create_ontap_module(DEFAULT_ARGS, module_args)
277 zapi_cx = netapp_utils.setup_na_ontap_zapi(module)
278 assert not isinstance(zapi_cx, netapp_utils.OntapZAPICx)
279 request, dummy = zapi_cx._create_request(netapp_utils.zapi.NaElement('dummy_tag'))
280 assert "Authorization" not in [x[0] for x in request.header_items()]
281
282
283 def test_is_zapi_connection_error():
284 message = 'URLError'
285 assert netapp_utils.is_zapi_connection_error(message)
286 if sys.version_info >= (3, 5, 0):
287 # not defined in python 2.7
288 message = (ConnectionError(), '')
289 assert netapp_utils.is_zapi_connection_error(message)
290 message = []
291 assert not netapp_utils.is_zapi_connection_error(message)
292
293
294 def test_is_zapi_write_access_error():
295 message = 'Insufficient privileges: XXXXXXX does not have write access'
296 assert netapp_utils.is_zapi_write_access_error(message)
297 message = 'URLError'
298 assert not netapp_utils.is_zapi_write_access_error(message)
299 message = []
300 assert not netapp_utils.is_zapi_write_access_error(message)
301
302
303 def test_is_zapi_missing_vserver_error():
304 message = 'Vserver API missing vserver parameter.'
305 assert netapp_utils.is_zapi_missing_vserver_error(message)
306 message = 'URLError'
307 assert not netapp_utils.is_zapi_missing_vserver_error(message)
308 message = []
309 assert not netapp_utils.is_zapi_missing_vserver_error(message)
310
311
312 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.IMPORT_EXCEPTION', 'test_exc')
313 def test_netapp_lib_is_required():
314 msg = 'Error: the python NetApp-Lib module is required. Import error: %s' % 'test_exc'
315 assert netapp_utils.netapp_lib_is_required() == msg
316
317
318 def test_warn_when_rest_is_not_supported_http():
319 assert netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(DEFAULT_ARGS, {'use_rest': 'always'}))
320 print_warnings()
321 assert_warning_was_raised("Using ZAPI for basic.py, ignoring 'use_rest: always'. Note: https is set to false.")
322
323
324 def test_warn_when_rest_is_not_supported_https():
325 assert netapp_utils.setup_na_ontap_zapi(module=create_ontap_module(DEFAULT_ARGS, {'use_rest': 'always', 'https': True}))
326 print_warnings()
327 assert_warning_was_raised("Using ZAPI for basic.py, ignoring 'use_rest: always'.")
328
329
330 def test_sanitize_xml():
331 zapi_cx = create_ontapzapicx_object(CERT_ARGS)
332 xml = build_raw_xml_response({'test_key': 'test_Value'})
333 print('XML', xml)
334 assert zapi_cx.sanitize_xml(xml) == xml
335
336 # these tests require that 'V' is not used, and 3.x because of bytes
337 if sys.version_info > (3, 0):
338 test_xml = zapi_cx.sanitize_xml(xml.replace(b'V', bytes([8])))
339 sanitized_xml = xml.replace(b'V', b'.')
340 assert zapi_cx.sanitize_xml(test_xml) == sanitized_xml
341
342 with patch('builtins.bytes') as mock_bytes:
343 # forcing bytes to return some unexpected value to force the older paths
344 mock_bytes.return_value = 0
345 assert zapi_cx.sanitize_xml(test_xml) == sanitized_xml
346 with patch('builtins.chr') as mock_chr:
347 # forcing python 2.7 behavior
348 mock_chr.return_value = b'\x08'
349 assert zapi_cx.sanitize_xml(test_xml) == sanitized_xml
350
351
352 def test_parse_response_exceptions_single():
353 zapi_cx = create_ontapzapicx_object(CERT_ARGS)
354 exc = expect_and_capture_ansible_exception(zapi_cx._parse_response, netapp_utils.zapi.etree.XMLSyntaxError, b'response')
355 print(exc.value)
356 assert str(exc.value).startswith('Start tag expected')
357
358
359 @patch('netapp_lib.api.zapi.zapi.NaServer._parse_response')
360 def test_parse_response_exceptions_double(mock_parse_response):
361 xml_exc = netapp_utils.zapi.etree.XMLSyntaxError('UT', 'code', 101, 22, 'filename')
362 mock_parse_response.side_effect = [xml_exc, KeyError('second exception')]
363 zapi_cx = create_ontapzapicx_object(CERT_ARGS)
364 exc = expect_and_capture_ansible_exception(zapi_cx._parse_response, netapp_utils.zapi.etree.XMLSyntaxError, 'response')
365 print(exc)
366 assert str(exc.value) == 'UT. Received: response (filename, line 101)'
367
368 # force an exception while processing exception
369 delattr(xml_exc, 'msg')
370 mock_parse_response.side_effect = [xml_exc, KeyError('second exception')]
371 zapi_cx = create_ontapzapicx_object(CERT_ARGS)
372 exc = expect_and_capture_ansible_exception(zapi_cx._parse_response, netapp_utils.zapi.etree.XMLSyntaxError, 'response')
373 print(exc)
374 assert str(exc.value) == 'None (filename, line 101)'