Debian Codegraph @

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

raw
1 # This code is part of Ansible, but is an independent component.
2 # This particular file snippet, and this file snippet only, is BSD licensed.
3 # Modules you write using this snippet, which is embedded dynamically by Ansible
4 # still belong to the author of the module, and may assign their own license
5 # to the complete work.
6 #
7 # Copyright (c) 2022, Laurent Nicolas <[email protected]>
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without modification,
11 # are permitted provided that the following conditions are met:
12 #
13 # * Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # * Redistributions in binary form must reproduce the above copyright notice,
16 # this list of conditions and the following disclaimer in the documentation
17 # and/or other materials provided with the distribution.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 """ unit tests for module_utils rest_vserver.py
30
31 Provides wrappers for svm/svms REST APIs
32 """
33
34 from __future__ import (absolute_import, division, print_function)
35 __metaclass__ = type
36
37 import pytest
38 import sys
39
40 from ansible.module_utils import basic
41 # pylint: disable=unused-import
42 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import create_module, patch_ansible
43 from ansible_collections.netapp.ontap.tests.unit.framework.rest_factory import rest_error_message, rest_responses
44 from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import patch_request_and_invoke, register_responses
45 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
46
47 from ansible_collections.netapp.ontap.plugins.module_utils import rest_application
48
49
50 if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7):
51 pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available')
52
53
54 # REST API canned responses when mocking send_request
55 SRR = rest_responses({
56 'app_uuid': (200, {"records": [{"uuid": "test_uuid"}], "num_records": 1}, None),
57 'app_details': (200, {"details": "test_details"}, None),
58 'app_components': (200, {"records": [{"component": "test_component", "uuid": "component_uuid"}], "num_records": 1}, None),
59 'app_component_details': (200, {"component": "test_component", "uuid": "component_uuid", 'backing_storage': 'backing_storage'}, None),
60 'unexpected_argument': (200, None, 'Unexpected argument: exclude_aggregates'),
61 })
62
63
64 DEFAULT_ARGS = {
65 'hostname': 'test',
66 'username': 'test_user',
67 'password': 'test_pass!',
68 'cert_filepath': None,
69 'key_filepath': None,
70 }
71
72
73 class MockONTAPModule:
74 def __init__(self):
75 self.module = basic.AnsibleModule(netapp_utils.na_ontap_host_argument_spec())
76
77
78 def create_restapi_object(default_args, module_args=None):
79 module = create_module(MockONTAPModule, default_args, module_args)
80 return netapp_utils.OntapRestAPI(module.module)
81
82
83 def create_app(svm_name='vserver_name', app_name='application_name'):
84 rest_api = create_restapi_object(DEFAULT_ARGS)
85 return rest_application.RestApplication(rest_api, svm_name, app_name)
86
87
88 def test_successfully_create_object():
89 register_responses([
90 # ('GET', 'svm/svms', SRR['svm_uuid']),
91 # ('GET', 'svm/svms', SRR['zero_records']),
92 ])
93 assert create_app().svm_name == 'vserver_name'
94
95
96 def test_successfully_get_application_uuid():
97 register_responses([
98 ('GET', 'application/applications', SRR['zero_records']),
99 ('GET', 'application/applications', SRR['app_uuid']),
100 ])
101 my_app = create_app()
102 assert my_app.get_application_uuid() == (None, None)
103 assert my_app.get_application_uuid() == ('test_uuid', None)
104 # UUID is cached if not None, so no API call
105 assert my_app.get_application_uuid() == ('test_uuid', None)
106 assert my_app.get_application_uuid() == ('test_uuid', None)
107
108
109 def test_negative_get_application_uuid():
110 register_responses([
111 ('GET', 'application/applications', SRR['generic_error']),
112 ])
113 my_app = create_app()
114 assert my_app.get_application_uuid() == (None, rest_error_message('', 'application/applications'))
115
116
117 def test_successfully_get_application_details():
118 register_responses([
119 ('GET', 'application/applications', SRR['zero_records']),
120 ('GET', 'application/applications', SRR['app_uuid']),
121 ('GET', 'application/applications/test_uuid', SRR['app_details']),
122 ('GET', 'application/applications/test_uuid', SRR['app_details']),
123 ('GET', 'application/applications/test_uuid', SRR['app_details']),
124 ])
125 my_app = create_app()
126 assert my_app.get_application_details() == (None, None)
127 assert my_app.get_application_details() == (SRR['app_details'][1], None)
128 # UUID is cached if not None, so no API call
129 assert my_app.get_application_details(template='test') == (SRR['app_details'][1], None)
130 assert my_app.get_application_details() == (SRR['app_details'][1], None)
131
132
133 def test_negative_get_application_details():
134 register_responses([
135 ('GET', 'application/applications', SRR['generic_error']),
136 ('GET', 'application/applications', SRR['app_uuid']),
137 ('GET', 'application/applications/test_uuid', SRR['generic_error']),
138 ])
139 my_app = create_app()
140 assert my_app.get_application_details() == (None, rest_error_message('', 'application/applications'))
141 assert my_app.get_application_details() == (None, rest_error_message('', 'application/applications/test_uuid'))
142
143
144 def test_successfully_create_application():
145 register_responses([
146 ('POST', 'application/applications', SRR['success']),
147 ])
148 my_app = create_app()
149 assert my_app.create_application({'option': 'option'}) == ({}, None)
150
151
152 def test_negative_create_application():
153 register_responses([
154 ('POST', 'application/applications', SRR['generic_error']),
155 ('POST', 'application/applications', SRR['unexpected_argument']),
156 # third call, create fails if app already exists
157 ('GET', 'application/applications', SRR['app_uuid']),
158 ])
159 my_app = create_app()
160 assert my_app.create_application({'option': 'option'}) == (None, rest_error_message('', 'application/applications'))
161 assert my_app.create_application({'option': 'option'}) == (
162 None, 'calling: application/applications: got Unexpected argument: exclude_aggregates. "exclude_aggregates" requires ONTAP 9.9.1 GA or later.')
163
164 assert my_app.get_application_uuid() == ('test_uuid', None)
165 assert my_app.create_application({'option': 'option'}) ==\
166 (None, 'function create_application should not be called when application uuid is set: test_uuid.')
167
168
169 def test_successfully_patch_application():
170 register_responses([
171 ('GET', 'application/applications', SRR['app_uuid']),
172 ('PATCH', 'application/applications/test_uuid', SRR['success']),
173 ])
174 my_app = create_app()
175 assert my_app.get_application_uuid() == ('test_uuid', None)
176 assert my_app.patch_application({'option': 'option'}) == ({}, None)
177
178
179 def test_negative_patch_application():
180 register_responses([
181 # first call, patch fails if app does not exist
182 # second call
183 ('GET', 'application/applications', SRR['app_uuid']),
184 ('PATCH', 'application/applications/test_uuid', SRR['generic_error']),
185 ])
186 my_app = create_app()
187 assert my_app.patch_application({'option': 'option'}) == (None, 'function should not be called before application uuid is set.')
188
189 assert my_app.get_application_uuid() == ('test_uuid', None)
190 assert my_app.patch_application({'option': 'option'}) == (None, rest_error_message('', 'application/applications/test_uuid'))
191
192
193 def test_successfully_delete_application():
194 register_responses([
195 ('GET', 'application/applications', SRR['app_uuid']),
196 ('DELETE', 'application/applications/test_uuid', SRR['success']),
197 ])
198 my_app = create_app()
199 assert my_app.get_application_uuid() == ('test_uuid', None)
200 assert my_app.delete_application() == ({}, None)
201
202
203 def test_negative_delete_application():
204 register_responses([
205 # first call, delete fails if app does not exist
206 # second call
207 ('GET', 'application/applications', SRR['app_uuid']),
208 ('DELETE', 'application/applications/test_uuid', SRR['generic_error']),
209 ])
210 my_app = create_app()
211 assert my_app.delete_application() == (None, 'function should not be called before application uuid is set.')
212
213 assert my_app.get_application_uuid() == ('test_uuid', None)
214 assert my_app.delete_application() == (None, rest_error_message('', 'application/applications/test_uuid'))
215
216
217 def test_successfully_get_application_components():
218 register_responses([
219 ('GET', 'application/applications', SRR['app_uuid']),
220 ('GET', 'application/applications/test_uuid/components', SRR['zero_records']),
221 ('GET', 'application/applications/test_uuid/components', SRR['app_components']),
222 ('GET', 'application/applications/test_uuid/components', SRR['app_components']),
223 ])
224 my_app = create_app()
225 assert my_app.get_application_uuid() == ('test_uuid', None)
226 assert my_app.get_application_components() == (None, None)
227 assert my_app.get_application_components() == (SRR['app_components'][1]['records'], None)
228 assert my_app.get_application_components() == (SRR['app_components'][1]['records'], None)
229
230
231 def test_negative_get_application_components():
232 register_responses([
233 ('GET', 'application/applications', SRR['app_uuid']),
234 ('GET', 'application/applications/test_uuid/components', SRR['generic_error']),
235 ])
236 my_app = create_app()
237 assert my_app.get_application_components() == (None, 'function should not be called before application uuid is set.')
238 assert my_app.get_application_uuid() == ('test_uuid', None)
239 assert my_app.get_application_components() == (None, rest_error_message('', 'application/applications/test_uuid/components'))
240
241
242 def test_successfully_get_application_component_uuid():
243 register_responses([
244 ('GET', 'application/applications', SRR['app_uuid']),
245 ('GET', 'application/applications/test_uuid/components', SRR['zero_records']),
246 ('GET', 'application/applications/test_uuid/components', SRR['app_components']),
247 ('GET', 'application/applications/test_uuid/components', SRR['app_components']),
248 ])
249 my_app = create_app()
250 assert my_app.get_application_uuid() == ('test_uuid', None)
251 assert my_app.get_application_component_uuid() == (None, None)
252 assert my_app.get_application_component_uuid() == ('component_uuid', None)
253 assert my_app.get_application_component_uuid() == ('component_uuid', None)
254
255
256 def test_negative_get_application_component_uuid():
257 register_responses([
258 ('GET', 'application/applications', SRR['app_uuid']),
259 ('GET', 'application/applications/test_uuid/components', SRR['generic_error']),
260 ])
261 my_app = create_app()
262 assert my_app.get_application_component_uuid() == (None, 'function should not be called before application uuid is set.')
263 assert my_app.get_application_uuid() == ('test_uuid', None)
264 assert my_app.get_application_component_uuid() == (None, rest_error_message('', 'application/applications/test_uuid/components'))
265
266
267 def test_successfully_get_application_component_details():
268 register_responses([
269 ('GET', 'application/applications', SRR['app_uuid']),
270 ('GET', 'application/applications/test_uuid/components', SRR['app_components']),
271 ('GET', 'application/applications/test_uuid/components/component_uuid', SRR['app_components']),
272 ])
273 my_app = create_app()
274 assert my_app.get_application_uuid() == ('test_uuid', None)
275 assert my_app.get_application_component_details() == (SRR['app_components'][1]['records'][0], None)
276
277
278 def test_negative_get_application_component_details():
279 register_responses([
280 # first call, fail as UUID not set
281 # second call, fail to retrieve UUID
282 ('GET', 'application/applications', SRR['app_uuid']),
283 ('GET', 'application/applications/test_uuid/components', SRR['zero_records']),
284 # fail to retrieve UUID
285 ('GET', 'application/applications/test_uuid/components', SRR['generic_error']),
286 # fail to retrieve component_details
287 ('GET', 'application/applications/test_uuid/components', SRR['app_components']),
288 ('GET', 'application/applications/test_uuid/components/component_uuid', SRR['generic_error']),
289 ])
290 my_app = create_app()
291 assert my_app.get_application_component_details() == (None, 'function should not be called before application uuid is set.')
292 # second call, set UUI first
293 assert my_app.get_application_uuid() == ('test_uuid', None)
294 assert my_app.get_application_component_details() == (None, 'no component for application application_name')
295 # third call
296 assert my_app.get_application_component_details() == (None, rest_error_message('', 'application/applications/test_uuid/components'))
297 # fourth call
298 assert my_app.get_application_component_details() == (None, rest_error_message('', 'application/applications/test_uuid/components/component_uuid'))
299
300
301 def test_successfully_get_application_component_backing_storage():
302 register_responses([
303 ('GET', 'application/applications', SRR['app_uuid']),
304 ('GET', 'application/applications/test_uuid/components', SRR['app_components']),
305 ('GET', 'application/applications/test_uuid/components/component_uuid', SRR['app_component_details']),
306 ])
307 my_app = create_app()
308 assert my_app.get_application_uuid() == ('test_uuid', None)
309 assert my_app.get_application_component_backing_storage() == ('backing_storage', None)
310
311
312 def test_negative_get_application_component_backing_storage():
313 register_responses([
314 # first call, fail as UUID not set
315 # second call, fail to retrieve UUID
316 ('GET', 'application/applications', SRR['app_uuid']),
317 ('GET', 'application/applications/test_uuid/components', SRR['zero_records']),
318 # fail to retrieve UUID
319 ('GET', 'application/applications/test_uuid/components', SRR['generic_error']),
320 # fail to retrieve component_backing_storage
321 ('GET', 'application/applications/test_uuid/components', SRR['app_components']),
322 ('GET', 'application/applications/test_uuid/components/component_uuid', SRR['generic_error']),
323 ])
324 my_app = create_app()
325 assert my_app.get_application_component_backing_storage() == (None, 'function should not be called before application uuid is set.')
326 # second call, set UUI first
327 assert my_app.get_application_uuid() == ('test_uuid', None)
328 assert my_app.get_application_component_backing_storage() == (None, 'no component for application application_name')
329 # third call
330 assert my_app.get_application_component_backing_storage() == (None, rest_error_message('', 'application/applications/test_uuid/components'))
331 # fourth call
332 assert my_app.get_application_component_backing_storage() == (None, rest_error_message('', 'application/applications/test_uuid/components/component_uuid'))
333
334
335 def test_create_application_body():
336 my_app = create_app()
337 body = {
338 'name': my_app.app_name,
339 'svm': {'name': my_app.svm_name},
340 'smart_container': True,
341 'tname': 'tbody'
342 }
343 assert my_app.create_application_body('tname', 'tbody') == (body, None)
344 body['smart_container'] = False
345 assert my_app.create_application_body('tname', 'tbody', False) == (body, None)
346 assert my_app.create_application_body('tname', 'tbody', 'False') == (None, 'expecting bool value for smart_container, got: False')