| 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) 2021, 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 netapp_module.py |
| 30 |
|
| 31 |
Provides wrappers for storage/volumes REST APIs |
| 32 |
""" |
| 33 |
|
| 34 |
from __future__ import (absolute_import, division, print_function) |
| 35 |
__metaclass__ = type |
| 36 |
|
| 37 |
import copy |
| 38 |
import pytest |
| 39 |
import sys |
| 40 |
|
| 41 |
# from ansible_collections.netapp.ontap.tests.unit.compat import unittest |
| 42 |
from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch, call |
| 43 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_volume |
| 44 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 45 |
|
| 46 |
|
| 47 |
if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7): |
| 48 |
pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available') |
| 49 |
|
| 50 |
|
| 51 |
# REST API canned responses when mocking send_request |
| 52 |
SRR = { |
| 53 |
# common responses |
| 54 |
'is_rest': (200, dict(version=dict(generation=9, major=9, minor=0, full='dummy')), None), |
| 55 |
'is_rest_9_8': (200, dict(version=dict(generation=9, major=8, minor=0, full='dummy')), None), |
| 56 |
'is_zapi': (400, {}, "Unreachable"), |
| 57 |
'empty_good': (200, {}, None), |
| 58 |
'zero_record': (200, dict(records=[], num_records=0), None), |
| 59 |
'one_record_uuid': (200, dict(records=[dict(uuid='a1b2c3')], num_records=1), None), |
| 60 |
'end_of_sequence': (500, None, "Unexpected call to send_request"), |
| 61 |
'generic_error': (400, None, "Expected error"), |
| 62 |
'one_volume_record': (200, dict(records=[ |
| 63 |
dict(uuid='a1b2c3', |
| 64 |
name='test', |
| 65 |
svm=dict(name='vserver'), |
| 66 |
) |
| 67 |
], num_records=1), None), |
| 68 |
'three_volume_records': (200, dict(records=[ |
| 69 |
dict(uuid='a1b2c3_1', |
| 70 |
name='test1', |
| 71 |
svm=dict(name='vserver'), |
| 72 |
), |
| 73 |
dict(uuid='a1b2c3_2', |
| 74 |
name='test2', |
| 75 |
svm=dict(name='vserver'), |
| 76 |
), |
| 77 |
dict(uuid='a1b2c3_3', |
| 78 |
name='test3', |
| 79 |
svm=dict(name='vserver'), |
| 80 |
) |
| 81 |
], num_records=3), None), |
| 82 |
'job': (200, { |
| 83 |
'job': { |
| 84 |
'uuid': 'a1b2c3_job', |
| 85 |
'_links': {'self': {'href': 'api/some_link'}} |
| 86 |
}}, None), |
| 87 |
'job_bad_url': (200, { |
| 88 |
'job': { |
| 89 |
'uuid': 'a1b2c3_job', |
| 90 |
'_links': {'self': {'href': 'some_link'}} |
| 91 |
}}, None), |
| 92 |
'job_status_success': (200, { |
| 93 |
'state': 'success', |
| 94 |
'message': 'success_message', |
| 95 |
'job': { |
| 96 |
'uuid': 'a1b2c3_job', |
| 97 |
'_links': {'self': {'href': 'some_link'}} |
| 98 |
}}, None), |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
class AnsibleFailJson(Exception): |
| 103 |
"""Exception class to be raised by module.fail_json and caught by the test case""" |
| 104 |
|
| 105 |
|
| 106 |
class MockModule(object): |
| 107 |
''' rough mock for an Ansible module class ''' |
| 108 |
def __init__(self): |
| 109 |
self.params = dict( |
| 110 |
username='my_username', |
| 111 |
password='my_password', |
| 112 |
hostname='my_hostname', |
| 113 |
use_rest='my_use_rest', |
| 114 |
cert_filepath=None, |
| 115 |
key_filepath=None, |
| 116 |
validate_certs='my_validate_certs', |
| 117 |
http_port=None, |
| 118 |
feature_flags=None, |
| 119 |
) |
| 120 |
|
| 121 |
def fail_json(self, *args, **kwargs): # pylint: disable=unused-argument |
| 122 |
"""function to simulate fail_json: package return data into an exception""" |
| 123 |
kwargs['failed'] = True |
| 124 |
raise AnsibleFailJson(kwargs) |
| 125 |
|
| 126 |
|
| 127 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 128 |
def test_get_volumes_none(mock_request): |
| 129 |
module = MockModule() |
| 130 |
rest_api = netapp_utils.OntapRestAPI(module) |
| 131 |
mock_request.side_effect = [ |
| 132 |
SRR['zero_record'], |
| 133 |
SRR['end_of_sequence']] |
| 134 |
volumes, error = rest_volume.get_volumes(rest_api) |
| 135 |
assert error is None |
| 136 |
assert volumes is None |
| 137 |
|
| 138 |
|
| 139 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 140 |
def test_get_volumes_one(mock_request): |
| 141 |
module = MockModule() |
| 142 |
rest_api = netapp_utils.OntapRestAPI(module) |
| 143 |
mock_request.side_effect = [ |
| 144 |
SRR['one_volume_record'], |
| 145 |
SRR['end_of_sequence']] |
| 146 |
volumes, error = rest_volume.get_volumes(rest_api, 'vserver', 'name') |
| 147 |
assert error is None |
| 148 |
assert volumes == [SRR['one_volume_record'][1]['records'][0]] |
| 149 |
|
| 150 |
|
| 151 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 152 |
def test_get_volumes_three(mock_request): |
| 153 |
module = MockModule() |
| 154 |
rest_api = netapp_utils.OntapRestAPI(module) |
| 155 |
mock_request.side_effect = [ |
| 156 |
SRR['three_volume_records'], |
| 157 |
SRR['end_of_sequence']] |
| 158 |
volumes, error = rest_volume.get_volumes(rest_api) |
| 159 |
assert error is None |
| 160 |
assert volumes == [SRR['three_volume_records'][1]['records'][x] for x in (0, 1, 2)] |
| 161 |
|
| 162 |
|
| 163 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 164 |
def test_get_volume_not_found(mock_request): |
| 165 |
module = MockModule() |
| 166 |
rest_api = netapp_utils.OntapRestAPI(module) |
| 167 |
mock_request.side_effect = [ |
| 168 |
SRR['zero_record'], |
| 169 |
SRR['end_of_sequence']] |
| 170 |
volume, error = rest_volume.get_volume(rest_api, 'name', 'vserver') |
| 171 |
assert error is None |
| 172 |
assert volume is None |
| 173 |
|
| 174 |
|
| 175 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 176 |
def test_get_volume_found(mock_request): |
| 177 |
module = MockModule() |
| 178 |
rest_api = netapp_utils.OntapRestAPI(module) |
| 179 |
mock_request.side_effect = [ |
| 180 |
SRR['one_volume_record'], |
| 181 |
SRR['end_of_sequence']] |
| 182 |
volume, error = rest_volume.get_volume(rest_api, 'name', 'vserver') |
| 183 |
assert error is None |
| 184 |
assert volume == SRR['one_volume_record'][1]['records'][0] |
| 185 |
|
| 186 |
|
| 187 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 188 |
def test_get_volume_too_many(mock_request): |
| 189 |
module = MockModule() |
| 190 |
rest_api = netapp_utils.OntapRestAPI(module) |
| 191 |
mock_request.side_effect = [ |
| 192 |
SRR['three_volume_records'], |
| 193 |
SRR['end_of_sequence']] |
| 194 |
dummy, error = rest_volume.get_volume(rest_api, 'name', 'vserver') |
| 195 |
expected = "calling: storage/volumes: unexpected response" |
| 196 |
assert expected in error |
| 197 |
|
| 198 |
|
| 199 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 200 |
def test_patch_volume_async(mock_request): |
| 201 |
module = MockModule() |
| 202 |
rest_api = netapp_utils.OntapRestAPI(module) |
| 203 |
mock_request.side_effect = [ |
| 204 |
copy.deepcopy(SRR['job']), # deepcopy as job is modified in place! |
| 205 |
SRR['job_status_success'], |
| 206 |
SRR['end_of_sequence']] |
| 207 |
body = dict(a1=1, a2=True, a3='str') |
| 208 |
response, error = rest_volume.patch_volume(rest_api, 'uuid', body) |
| 209 |
job = dict(SRR['job'][1]) # deepcopy as job is modified in place! |
| 210 |
job['job_response'] = SRR['job_status_success'][1]['message'] |
| 211 |
assert error is None |
| 212 |
assert response == job |
| 213 |
expected = call('PATCH', 'storage/volumes/uuid', {'return_timeout': 30}, json=body, headers=None, files=None) |
| 214 |
assert expected in mock_request.mock_calls |
| 215 |
|
| 216 |
|
| 217 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 218 |
def test_patch_volume_async_with_query(mock_request): |
| 219 |
module = MockModule() |
| 220 |
rest_api = netapp_utils.OntapRestAPI(module) |
| 221 |
mock_request.side_effect = [ |
| 222 |
copy.deepcopy(SRR['job']), # deepcopy as job is modified in place! |
| 223 |
SRR['job_status_success'], |
| 224 |
SRR['end_of_sequence']] |
| 225 |
body = dict(a1=1, a2=True, a3='str') |
| 226 |
query = dict(return_timeout=20) |
| 227 |
response, error = rest_volume.patch_volume(rest_api, 'uuid', body, query) |
| 228 |
job = dict(SRR['job'][1]) # deepcopy as job is modified in place! |
| 229 |
job['job_response'] = SRR['job_status_success'][1]['message'] |
| 230 |
assert error is None |
| 231 |
assert response == job |
| 232 |
expected = call('PATCH', 'storage/volumes/uuid', {'return_timeout': 20}, json=body, headers=None, files=None) |
| 233 |
assert expected in mock_request.mock_calls |