Debian Codegraph @

ansible_collections/netapp/ontap/tests/unit/plugins/modules/test_na_ontap_zapit.py

raw
1 # (c) 2022, NetApp, Inc
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 ''' unit tests ONTAP Ansible module: na_ontap_zapit '''
5
6 from __future__ import (absolute_import, division, print_function)
7 __metaclass__ = type
8
9 import pytest
10 import sys
11
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 expect_and_capture_ansible_exception, call_main, create_module, create_and_apply, patch_ansible
16 from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import\
17 patch_request_and_invoke, register_responses
18 from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_zapi_response, zapi_responses
19
20 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
21 from ansible_collections.netapp.ontap.plugins.modules.na_ontap_zapit \
22 import NetAppONTAPZapi as my_module, main as my_main # module under test
23
24 if not netapp_utils.has_netapp_lib():
25 pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
26
27
28 def cluster_image_info():
29 version = 'Fattire__9.3.0'
30 return {
31 'num-records': 1,
32 # composite response, attributes-list for cluster-image-get-iter and attributes for cluster-image-get
33 'attributes-list': [
34 {'cluster-image-info': {
35 'node-id': 'node4test',
36 'current-version': version}},
37 {'cluster-image-info': {
38 'node-id': 'node4test',
39 'current-version': version}},
40 ],
41 'attributes': {
42 'cluster-image-info': {
43 'node-id': 'node4test',
44 'current-version': version
45 }},
46 }
47
48
49 def build_zapi_error_custom(errno, reason, results='results'):
50 ''' build an XML response
51 errno as int
52 reason as str
53 '''
54 if not netapp_utils.has_netapp_lib():
55 return 'build_zapi_error: netapp-lib is missing', 'invalid'
56 if results != 'results':
57 return (netapp_utils.zapi.NaElement(results), 'valid')
58 xml = {}
59 if errno is not None:
60 xml['errorno'] = errno
61 if reason is not None:
62 xml['reason'] = reason
63 response = netapp_utils.zapi.NaElement('results')
64 if xml:
65 response.translate_struct(xml)
66 return (response, 'valid')
67
68
69 ZRR = zapi_responses({
70 'cluster_image_info': build_zapi_response(cluster_image_info()),
71 'error_no_errno': build_zapi_error_custom(None, 'some reason'),
72 'error_no_reason': build_zapi_error_custom(18408, None),
73 'error_no_results': build_zapi_error_custom(None, None, 'no_results')
74 })
75
76
77 DEFAULT_ARGS = {
78 'hostname': 'hostname',
79 'username': 'username',
80 'password': 'password',
81 'zapi': {'cluster-image-get-iter': None}
82 }
83
84
85 def test_ensure_zapi_called_cluster():
86 register_responses([
87
88 ('ZAPI', 'cluster-image-get-iter', ZRR['cluster_image_info']),
89 ])
90 module_args = {
91 "use_rest": "never",
92 }
93 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
94
95
96 def test_ensure_zapi_called_vserver():
97 register_responses([
98 ('ZAPI', 'cluster-image-get-iter', ZRR['cluster_image_info']),
99 ])
100 module_args = {
101 "use_rest": "never",
102 "vserver": "vserver",
103 "zapi": {'cluster-image-get-iter': {'attributes': None}}
104 }
105 assert call_main(my_main, DEFAULT_ARGS, module_args)['changed']
106
107
108 def test_negative_zapi_called_attributes():
109 register_responses([
110 ('ZAPI', 'cluster-image-get-iter', ZRR['error']),
111 ])
112 module_args = {
113 "use_rest": "never",
114 }
115 exception = create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)
116 assert exception['msg'] == 'ZAPI failure: check errno and reason.'
117 assert exception['errno'] == '12345'
118 assert exception['reason'] == 'synthetic error for UT purpose'
119
120
121 def test_negative_zapi_called_element_no_errno():
122 register_responses([
123 ('ZAPI', 'cluster-image-get-iter', ZRR['error_no_errno']),
124 ])
125 module_args = {
126 "use_rest": "never",
127 }
128 exception = create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)
129 assert exception['msg'] == 'ZAPI failure: check errno and reason.'
130 assert exception['errno'] == 'ESTATUSFAILED'
131 assert exception['reason'] == 'some reason'
132
133
134 def test_negative_zapi_called_element_no_reason():
135 register_responses([
136 ('ZAPI', 'cluster-image-get-iter', ZRR['error_no_reason']),
137 ])
138 module_args = {
139 "use_rest": "never",
140 }
141 exception = create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)
142 assert exception['msg'] == 'ZAPI failure: check errno and reason.'
143 assert exception['errno'] == '18408'
144 assert exception['reason'] == 'Execution failure with unknown reason.'
145
146
147 def test_negative_zapi_unexpected_error():
148 register_responses([
149 ('ZAPI', 'cluster-image-get-iter', (netapp_utils.zapi.NaApiError(), 'valid')),
150 ])
151 module_args = {
152 "use_rest": "never",
153 }
154 exception = create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)
155 assert exception['msg'] == "Error running zapi cluster-image-get-iter: NetApp API failed. Reason - unknown:unknown"
156
157
158 def test_negative_two_zapis():
159 register_responses([
160 ])
161 module_args = {
162 "use_rest": "never",
163 "zapi": {"1": 1, "2": 2}
164 }
165 exception = create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)
166 assert 'A single ZAPI can be called at a time, received: ' in exception['msg']
167
168
169 def test_negative_bad_zapi_type():
170 register_responses([
171 ])
172 module_args = {
173 "use_rest": "never",
174 }
175 obj = create_module(my_module, DEFAULT_ARGS, module_args)
176 obj.zapi = "1"
177 error = 'A directory entry is expected, eg: system-get-version: , received: 1'
178 assert expect_and_capture_ansible_exception(obj.run_zapi, 'fail')['msg'] == error
179 obj.zapi = [3, 1]
180 error = 'A directory entry is expected, eg: system-get-version: , received: [3, 1]'
181 assert expect_and_capture_ansible_exception(obj.run_zapi, 'fail')['msg'] == error
182
183
184 # python 2.7 does not have bytes but str
185 BYTES_MARKER_BEGIN = "b'" if sys.version_info >= (3, 0) else ''
186 BYTES_MARKER_END = "'" if sys.version_info >= (3, 0) else ''
187 BYTES_TYPE = 'bytes' if sys.version_info >= (3, 0) else 'str'
188
189
190 def test_negative_zapi_called_element_no_results():
191 register_responses([
192 ('ZAPI', 'cluster-image-get-iter', ZRR['error_no_results']),
193 ])
194 module_args = {
195 "use_rest": "never",
196 }
197 error = "Error running zapi, no results field: %s<no_results/>" % BYTES_MARKER_BEGIN
198 assert error in create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
199
200
201 def test_negative_bad_zapi_response_to_string():
202 module_args = {
203 "use_rest": "never",
204 }
205 obj = create_module(my_module, DEFAULT_ARGS, module_args)
206 error = "Error running zapi in to_string: '%s' object has no attribute 'to_string'" % BYTES_TYPE
207 assert expect_and_capture_ansible_exception(obj.jsonify_and_parse_output, 'fail', b'<bad_xml')['msg'] == error
208
209
210 class xml_mock:
211 def __init__(self, data, action=None):
212 self.data = data
213 self.action = action
214
215 def to_string(self):
216 if self.action == 'raise_on_to_string':
217 raise ValueError(self.data)
218 return self.data
219
220
221 def test_negative_bad_zapi_response_bad_xml():
222 module_args = {
223 "use_rest": "never",
224 }
225 obj = create_module(my_module, DEFAULT_ARGS, module_args)
226 xml = xml_mock(b'<bad_xml')
227 error = "Error running zapi in xmltodict: %s<bad_xml%s: unclosed token" % (BYTES_MARKER_BEGIN, BYTES_MARKER_END)
228 assert error in expect_and_capture_ansible_exception(obj.jsonify_and_parse_output, 'fail', xml)['msg']
229
230
231 def test_negative_bad_zapi_response_bad_json():
232 # TODO: need to find some valid XML that cannot be converted in JSON. Is it possible?
233 module_args = {
234 "use_rest": "never",
235 }
236 obj = create_module(my_module, DEFAULT_ARGS, module_args)
237 xml = xml_mock(b'<bad_json><elemX-1>elem_value</elemX-1><elem-2>elem_value</elem-2></bad_json>')
238 error = "Error running zapi, no results field"
239 assert error in expect_and_capture_ansible_exception(obj.jsonify_and_parse_output, 'fail', xml)['msg']
240
241
242 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.has_netapp_lib')
243 def test_fail_netapp_lib_error(mock_has_netapp_lib):
244 mock_has_netapp_lib.return_value = False
245 assert 'Error: the python NetApp-Lib module is required. Import error: None' == call_main(my_main, DEFAULT_ARGS, fail=True)['msg']
246
247
248 @patch('ansible_collections.netapp.ontap.plugins.modules.na_ontap_zapit.HAS_JSON', False)
249 def test_fail_netapp_lib_error():
250 assert 'the python json module is required' == call_main(my_main, DEFAULT_ARGS, fail=True)['msg']
251
252
253 @patch('ansible_collections.netapp.ontap.plugins.modules.na_ontap_zapit.HAS_XMLTODICT', False)
254 def test_fail_netapp_lib_error():
255 assert 'the python xmltodict module is required' == call_main(my_main, DEFAULT_ARGS, fail=True)['msg']