| 1 |
# (c) 2018-2021, NetApp, Inc |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
''' unit test for ONTAP publickey Ansible module ''' |
| 5 |
|
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
import pytest |
| 9 |
import sys |
| 10 |
|
| 11 |
from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch |
| 12 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 13 |
# pylint: disable=unused-import |
| 14 |
from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import set_module_args, \ |
| 15 |
AnsibleFailJson, AnsibleExitJson, patch_ansible, assert_warning_was_raised, assert_no_warnings, print_warnings |
| 16 |
|
| 17 |
|
| 18 |
from ansible_collections.netapp.ontap.plugins.modules.na_ontap_publickey \ |
| 19 |
import NetAppOntapPublicKey as my_module, main as uut_main # module under test |
| 20 |
|
| 21 |
|
| 22 |
if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7): |
| 23 |
pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available') |
| 24 |
|
| 25 |
|
| 26 |
def default_args(): |
| 27 |
return { |
| 28 |
'state': 'present', |
| 29 |
'hostname': '10.10.10.10', |
| 30 |
'username': 'admin', |
| 31 |
'https': 'true', |
| 32 |
'validate_certs': 'false', |
| 33 |
'password': 'password', |
| 34 |
'account': 'user123', |
| 35 |
'public_key': '161245ASDF', |
| 36 |
'vserver': 'vserver', |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
# REST API canned responses when mocking send_request |
| 41 |
SRR = { |
| 42 |
# common responses |
| 43 |
'is_rest': (200, dict(version=dict(generation=9, major=9, minor=0, full='dummy')), None), |
| 44 |
'is_rest_9_6': (200, dict(version=dict(generation=9, major=6, minor=0, full='dummy')), None), |
| 45 |
'is_rest_9_8': (200, dict(version=dict(generation=9, major=8, minor=0, full='dummy')), None), |
| 46 |
'is_zapi': (400, {}, "Unreachable"), |
| 47 |
'empty_good': (200, {}, None), |
| 48 |
'zero_record': (200, dict(records=[], num_records=0), None), |
| 49 |
'one_record_uuid': (200, dict(records=[dict(uuid='a1b2c3')], num_records=1), None), |
| 50 |
'end_of_sequence': (500, None, "Unexpected call to send_request"), |
| 51 |
'generic_error': (400, None, "Expected error"), |
| 52 |
'one_pk_record': (200, { |
| 53 |
"records": [{ |
| 54 |
'account': dict(name='user123'), |
| 55 |
'owner': dict(uuid='98765'), |
| 56 |
'public_key': '161245ASDF', |
| 57 |
'index': 12, |
| 58 |
'comment': 'comment_123', |
| 59 |
}], |
| 60 |
'num_records': 1 |
| 61 |
}, None), |
| 62 |
'two_pk_records': (200, { |
| 63 |
"records": [{ |
| 64 |
'account': dict(name='user123'), |
| 65 |
'owner': dict(uuid='98765'), |
| 66 |
'public_key': '161245ASDF', |
| 67 |
'index': 12, |
| 68 |
'comment': 'comment_123', |
| 69 |
}, |
| 70 |
{ |
| 71 |
'account': dict(name='user123'), |
| 72 |
'owner': dict(uuid='98765'), |
| 73 |
'public_key': '161245ASDF', |
| 74 |
'index': 13, |
| 75 |
'comment': 'comment_123', |
| 76 |
}], |
| 77 |
'num_records': 2 |
| 78 |
}, None) |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
def test_module_fail_when_required_args_missing(patch_ansible): |
| 83 |
''' required arguments are reported as errors ''' |
| 84 |
with pytest.raises(AnsibleFailJson) as exc: |
| 85 |
set_module_args(dict(hostname='')) |
| 86 |
my_module() |
| 87 |
print('Info: %s' % exc.value.args[0]['msg']) |
| 88 |
msg = 'missing required arguments: account' |
| 89 |
assert msg == exc.value.args[0]['msg'] |
| 90 |
|
| 91 |
|
| 92 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 93 |
def test_ensure_get_called(mock_request, patch_ansible): |
| 94 |
''' test get''' |
| 95 |
args = dict(default_args()) |
| 96 |
args['index'] = 12 |
| 97 |
set_module_args(args) |
| 98 |
mock_request.side_effect = [ |
| 99 |
SRR['is_rest_9_8'], # get version |
| 100 |
SRR['one_pk_record'], # get |
| 101 |
SRR['end_of_sequence'] |
| 102 |
] |
| 103 |
my_obj = my_module() |
| 104 |
with pytest.raises(AnsibleExitJson) as exc: |
| 105 |
my_obj.apply() |
| 106 |
print('Info: %s' % exc.value.args[0]) |
| 107 |
assert exc.value.args[0]['changed'] is False |
| 108 |
assert_no_warnings() |
| 109 |
|
| 110 |
|
| 111 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 112 |
def test_ensure_create_called(mock_request, patch_ansible): |
| 113 |
''' test get''' |
| 114 |
args = dict(default_args()) |
| 115 |
args['use_rest'] = 'auto' |
| 116 |
args['index'] = 13 |
| 117 |
set_module_args(args) |
| 118 |
mock_request.side_effect = [ |
| 119 |
SRR['is_rest_9_8'], # get version |
| 120 |
SRR['zero_record'], # get |
| 121 |
SRR['empty_good'], # create |
| 122 |
SRR['end_of_sequence'] |
| 123 |
] |
| 124 |
my_obj = my_module() |
| 125 |
with pytest.raises(AnsibleExitJson) as exc: |
| 126 |
my_obj.apply() |
| 127 |
print('Info: %s' % exc.value.args[0]) |
| 128 |
assert exc.value.args[0]['changed'] is True |
| 129 |
assert_no_warnings() |
| 130 |
|
| 131 |
|
| 132 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 133 |
def test_ensure_create_idempotent(mock_request, patch_ansible): |
| 134 |
''' test get''' |
| 135 |
args = dict(default_args()) |
| 136 |
args['use_rest'] = 'always' |
| 137 |
args['index'] = 12 |
| 138 |
set_module_args(args) |
| 139 |
mock_request.side_effect = [ |
| 140 |
SRR['is_rest_9_8'], # get version |
| 141 |
SRR['one_pk_record'], # get |
| 142 |
SRR['end_of_sequence'] |
| 143 |
] |
| 144 |
my_obj = my_module() |
| 145 |
with pytest.raises(AnsibleExitJson) as exc: |
| 146 |
my_obj.apply() |
| 147 |
print('Info: %s' % exc.value.args[0]) |
| 148 |
assert exc.value.args[0]['changed'] is False |
| 149 |
assert_no_warnings() |
| 150 |
|
| 151 |
|
| 152 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 153 |
def test_ensure_create_always_called(mock_request, patch_ansible): |
| 154 |
''' test get''' |
| 155 |
args = dict(default_args()) |
| 156 |
set_module_args(args) |
| 157 |
mock_request.side_effect = [ |
| 158 |
SRR['is_rest_9_8'], # get version |
| 159 |
SRR['empty_good'], # create |
| 160 |
SRR['end_of_sequence'] |
| 161 |
] |
| 162 |
my_obj = my_module() |
| 163 |
with pytest.raises(AnsibleExitJson) as exc: |
| 164 |
my_obj.apply() |
| 165 |
print('Info: %s' % exc.value.args[0]) |
| 166 |
assert exc.value.args[0]['changed'] is True |
| 167 |
print_warnings() |
| 168 |
assert_warning_was_raised('Module is not idempotent if index is not provided with state=present.') |
| 169 |
|
| 170 |
|
| 171 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 172 |
def test_ensure_modify_called(mock_request, patch_ansible): |
| 173 |
''' test get''' |
| 174 |
args = dict(default_args()) |
| 175 |
args['index'] = 12 |
| 176 |
args['comment'] = 'new_comment' |
| 177 |
set_module_args(args) |
| 178 |
mock_request.side_effect = [ |
| 179 |
SRR['is_rest_9_8'], # get version |
| 180 |
SRR['one_pk_record'], # get |
| 181 |
SRR['empty_good'], # modify |
| 182 |
SRR['end_of_sequence'] |
| 183 |
] |
| 184 |
my_obj = my_module() |
| 185 |
with pytest.raises(AnsibleExitJson) as exc: |
| 186 |
my_obj.apply() |
| 187 |
print('Info: %s' % exc.value.args[0]) |
| 188 |
assert exc.value.args[0]['changed'] is True |
| 189 |
assert_no_warnings() |
| 190 |
|
| 191 |
|
| 192 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 193 |
def test_ensure_delete_called(mock_request, patch_ansible): |
| 194 |
''' test get''' |
| 195 |
args = dict(default_args()) |
| 196 |
args['use_rest'] = 'auto' |
| 197 |
args['index'] = 12 |
| 198 |
args['state'] = 'absent' |
| 199 |
set_module_args(args) |
| 200 |
mock_request.side_effect = [ |
| 201 |
SRR['is_rest_9_8'], # get version |
| 202 |
SRR['one_pk_record'], # get |
| 203 |
SRR['empty_good'], # delete |
| 204 |
SRR['end_of_sequence'] |
| 205 |
] |
| 206 |
my_obj = my_module() |
| 207 |
with pytest.raises(AnsibleExitJson) as exc: |
| 208 |
my_obj.apply() |
| 209 |
print('Info: %s' % exc.value.args[0]) |
| 210 |
assert exc.value.args[0]['changed'] is True |
| 211 |
assert_no_warnings() |
| 212 |
|
| 213 |
|
| 214 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 215 |
def test_ensure_delete_idempotent(mock_request, patch_ansible): |
| 216 |
''' test get''' |
| 217 |
args = dict(default_args()) |
| 218 |
args['use_rest'] = 'always' |
| 219 |
args['index'] = 12 |
| 220 |
args['state'] = 'absent' |
| 221 |
set_module_args(args) |
| 222 |
mock_request.side_effect = [ |
| 223 |
SRR['is_rest_9_8'], # get version |
| 224 |
SRR['zero_record'], # get |
| 225 |
SRR['end_of_sequence'] |
| 226 |
] |
| 227 |
my_obj = my_module() |
| 228 |
with pytest.raises(AnsibleExitJson) as exc: |
| 229 |
my_obj.apply() |
| 230 |
print('Info: %s' % exc.value.args[0]) |
| 231 |
assert exc.value.args[0]['changed'] is False |
| 232 |
assert_no_warnings() |
| 233 |
|
| 234 |
|
| 235 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 236 |
def test_ensure_delete_failed_N_records(mock_request, patch_ansible): |
| 237 |
''' test get''' |
| 238 |
args = dict(default_args()) |
| 239 |
args['use_rest'] = 'auto' |
| 240 |
args['state'] = 'absent' |
| 241 |
set_module_args(args) |
| 242 |
mock_request.side_effect = [ |
| 243 |
SRR['is_rest_9_8'], # get version |
| 244 |
SRR['two_pk_records'], # get |
| 245 |
SRR['end_of_sequence'] |
| 246 |
] |
| 247 |
my_obj = my_module() |
| 248 |
with pytest.raises(AnsibleFailJson) as exc: |
| 249 |
my_obj.apply() |
| 250 |
print('Info: %s' % exc.value.args[0]) |
| 251 |
msg = 'Error: index is required as more than one public_key exists for user account user123' |
| 252 |
assert msg in exc.value.args[0]['msg'] |
| 253 |
assert_no_warnings() |
| 254 |
|
| 255 |
|
| 256 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 257 |
def test_ensure_delete_succeeded_N_records(mock_request, patch_ansible): |
| 258 |
''' test get''' |
| 259 |
args = dict(default_args()) |
| 260 |
args['use_rest'] = 'auto' |
| 261 |
args['state'] = 'absent' |
| 262 |
args['delete_all'] = True |
| 263 |
set_module_args(args) |
| 264 |
mock_request.side_effect = [ |
| 265 |
SRR['is_rest_9_8'], # get version |
| 266 |
SRR['two_pk_records'], # get |
| 267 |
SRR['empty_good'], # delete |
| 268 |
SRR['empty_good'], # delete |
| 269 |
SRR['end_of_sequence'] |
| 270 |
] |
| 271 |
my_obj = my_module() |
| 272 |
with pytest.raises(AnsibleExitJson) as exc: |
| 273 |
my_obj.apply() |
| 274 |
print('Info: %s' % exc.value.args[0]) |
| 275 |
assert exc.value.args[0]['changed'] is True |
| 276 |
assert_no_warnings() |
| 277 |
|
| 278 |
|
| 279 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 280 |
def test_ensure_delete_succeeded_N_records_cluster(mock_request, patch_ansible): |
| 281 |
''' test get''' |
| 282 |
args = dict(default_args()) |
| 283 |
args['use_rest'] = 'auto' |
| 284 |
args['state'] = 'absent' |
| 285 |
args['delete_all'] = True |
| 286 |
args['vserver'] = None # cluster scope |
| 287 |
set_module_args(args) |
| 288 |
mock_request.side_effect = [ |
| 289 |
SRR['is_rest_9_8'], # get version |
| 290 |
SRR['two_pk_records'], # get |
| 291 |
SRR['empty_good'], # delete |
| 292 |
SRR['empty_good'], # delete |
| 293 |
SRR['end_of_sequence'] |
| 294 |
] |
| 295 |
with pytest.raises(AnsibleExitJson) as exc: |
| 296 |
uut_main() |
| 297 |
print('Info: %s' % exc.value.args[0]) |
| 298 |
assert exc.value.args[0]['changed'] is True |
| 299 |
assert_no_warnings() |
| 300 |
|
| 301 |
|
| 302 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 303 |
def test_negative_extra_record(mock_request, patch_ansible): |
| 304 |
''' test get''' |
| 305 |
args = dict(default_args()) |
| 306 |
args['use_rest'] = 'auto' |
| 307 |
args['state'] = 'present' |
| 308 |
args['index'] = 14 |
| 309 |
args['vserver'] = None # cluster scope |
| 310 |
set_module_args(args) |
| 311 |
mock_request.side_effect = [ |
| 312 |
SRR['is_rest_9_8'], # get version |
| 313 |
SRR['two_pk_records'], # get |
| 314 |
SRR['end_of_sequence'] |
| 315 |
] |
| 316 |
with pytest.raises(AnsibleFailJson) as exc: |
| 317 |
uut_main() |
| 318 |
print('Info: %s' % exc.value.args[0]) |
| 319 |
msg = 'Error in get_public_key: calling: security/authentication/publickeys: unexpected response' |
| 320 |
assert msg in exc.value.args[0]['msg'] |
| 321 |
assert_no_warnings() |
| 322 |
|
| 323 |
|
| 324 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 325 |
def test_negative_extra_arg_in_modify(mock_request, patch_ansible): |
| 326 |
''' test get''' |
| 327 |
args = dict(default_args()) |
| 328 |
args['use_rest'] = 'auto' |
| 329 |
args['state'] = 'present' |
| 330 |
args['index'] = 14 |
| 331 |
args['vserver'] = None # cluster scope |
| 332 |
set_module_args(args) |
| 333 |
mock_request.side_effect = [ |
| 334 |
SRR['is_rest_9_8'], # get version |
| 335 |
SRR['one_pk_record'], # get |
| 336 |
SRR['end_of_sequence'] |
| 337 |
] |
| 338 |
with pytest.raises(AnsibleFailJson) as exc: |
| 339 |
uut_main() |
| 340 |
print('Info: %s' % exc.value.args[0]) |
| 341 |
msg = "Error: attributes not supported in modify: {'index': 14}" |
| 342 |
assert msg in exc.value.args[0]['msg'] |
| 343 |
assert_no_warnings() |
| 344 |
|
| 345 |
|
| 346 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 347 |
def test_negative_empty_body_in_modify(mock_request, patch_ansible): |
| 348 |
''' test get''' |
| 349 |
args = dict(default_args()) |
| 350 |
set_module_args(args) |
| 351 |
mock_request.side_effect = [ |
| 352 |
SRR['is_rest_9_8'], # get version |
| 353 |
SRR['end_of_sequence'] |
| 354 |
] |
| 355 |
current = dict(owner=dict(uuid=''), account=dict(name=''), index=0) |
| 356 |
modify = {} |
| 357 |
my_obj = my_module() |
| 358 |
with pytest.raises(AnsibleFailJson) as exc: |
| 359 |
my_obj.modify_public_key(current, modify) |
| 360 |
print('Info: %s' % exc.value.args[0]) |
| 361 |
msg = 'Error: nothing to change - modify called with: {}' |
| 362 |
assert msg in exc.value.args[0]['msg'] |
| 363 |
assert_no_warnings() |
| 364 |
|
| 365 |
|
| 366 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 367 |
def test_negative_create_called(mock_request, patch_ansible): |
| 368 |
''' test get''' |
| 369 |
args = dict(default_args()) |
| 370 |
args['use_rest'] = 'auto' |
| 371 |
args['index'] = 13 |
| 372 |
set_module_args(args) |
| 373 |
mock_request.side_effect = [ |
| 374 |
SRR['is_rest_9_8'], # get version |
| 375 |
SRR['zero_record'], # get |
| 376 |
SRR['generic_error'], # create |
| 377 |
SRR['end_of_sequence'] |
| 378 |
] |
| 379 |
my_obj = my_module() |
| 380 |
with pytest.raises(AnsibleFailJson) as exc: |
| 381 |
my_obj.apply() |
| 382 |
print('Info: %s' % exc.value.args[0]) |
| 383 |
msg = 'Error in create_public_key: Expected error' |
| 384 |
assert msg in exc.value.args[0]['msg'] |
| 385 |
assert_no_warnings() |
| 386 |
|
| 387 |
|
| 388 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 389 |
def test_negative_delete_called(mock_request, patch_ansible): |
| 390 |
''' test get''' |
| 391 |
args = dict(default_args()) |
| 392 |
args['use_rest'] = 'auto' |
| 393 |
args['index'] = 12 |
| 394 |
args['state'] = 'absent' |
| 395 |
set_module_args(args) |
| 396 |
mock_request.side_effect = [ |
| 397 |
SRR['is_rest_9_8'], # get version |
| 398 |
SRR['one_pk_record'], # get |
| 399 |
SRR['generic_error'], # delete |
| 400 |
SRR['end_of_sequence'] |
| 401 |
] |
| 402 |
my_obj = my_module() |
| 403 |
with pytest.raises(AnsibleFailJson) as exc: |
| 404 |
my_obj.apply() |
| 405 |
print('Info: %s' % exc.value.args[0]) |
| 406 |
msg = 'Error in delete_public_key: Expected error' |
| 407 |
assert msg in exc.value.args[0]['msg'] |
| 408 |
assert_no_warnings() |
| 409 |
|
| 410 |
|
| 411 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 412 |
def test_negative_modify_called(mock_request, patch_ansible): |
| 413 |
''' test get''' |
| 414 |
args = dict(default_args()) |
| 415 |
args['use_rest'] = 'auto' |
| 416 |
args['index'] = 12 |
| 417 |
args['comment'] = 'change_me' |
| 418 |
set_module_args(args) |
| 419 |
mock_request.side_effect = [ |
| 420 |
SRR['is_rest_9_8'], # get version |
| 421 |
SRR['one_pk_record'], # get |
| 422 |
SRR['generic_error'], # modify |
| 423 |
SRR['end_of_sequence'] |
| 424 |
] |
| 425 |
my_obj = my_module() |
| 426 |
with pytest.raises(AnsibleFailJson) as exc: |
| 427 |
my_obj.apply() |
| 428 |
print('Info: %s' % exc.value.args[0]) |
| 429 |
msg = 'Error in modify_public_key: Expected error' |
| 430 |
assert msg in exc.value.args[0]['msg'] |
| 431 |
assert_no_warnings() |
| 432 |
|
| 433 |
|
| 434 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 435 |
def test_negative_older_version(mock_request, patch_ansible): |
| 436 |
''' test get''' |
| 437 |
args = dict(default_args()) |
| 438 |
args['use_rest'] = 'auto' |
| 439 |
args['index'] = 12 |
| 440 |
args['comment'] = 'change_me' |
| 441 |
set_module_args(args) |
| 442 |
mock_request.side_effect = [ |
| 443 |
SRR['is_rest_9_6'], # get version |
| 444 |
SRR['end_of_sequence'] |
| 445 |
] |
| 446 |
with pytest.raises(AnsibleFailJson) as exc: |
| 447 |
my_obj = my_module() |
| 448 |
print('Info: %s' % exc.value.args[0]) |
| 449 |
msg = 'Error: na_ontap_publickey only supports REST, and requires ONTAP 9.7.0 or later. Found: 9.6.0' |
| 450 |
assert msg in exc.value.args[0]['msg'] |
| 451 |
assert_no_warnings() |
| 452 |
|
| 453 |
|
| 454 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 455 |
def test_negative_zapi_only(mock_request, patch_ansible): |
| 456 |
''' test get''' |
| 457 |
args = dict(default_args()) |
| 458 |
args['use_rest'] = 'never' |
| 459 |
args['index'] = 12 |
| 460 |
args['comment'] = 'change_me' |
| 461 |
set_module_args(args) |
| 462 |
mock_request.side_effect = [ |
| 463 |
SRR['is_rest_9_6'], # get version |
| 464 |
SRR['end_of_sequence'] |
| 465 |
] |
| 466 |
with pytest.raises(AnsibleFailJson) as exc: |
| 467 |
my_obj = my_module() |
| 468 |
print('Info: %s' % exc.value.args[0]) |
| 469 |
msg = 'Error: REST is required for this module, found: "use_rest: never"' |
| 470 |
assert msg in exc.value.args[0]['msg'] |
| 471 |
assert_no_warnings() |