| 1 |
# (c) 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 na_ontap_fdsg 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 |
| 16 |
|
| 17 |
from ansible_collections.netapp.ontap.plugins.modules.na_ontap_fdss \ |
| 18 |
import NetAppOntapFDSS as my_module # module under test |
| 19 |
|
| 20 |
|
| 21 |
if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7): |
| 22 |
pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available') |
| 23 |
|
| 24 |
|
| 25 |
def default_args(): |
| 26 |
args = { |
| 27 |
'hostname': '10.10.10.10', |
| 28 |
'username': 'username', |
| 29 |
'password': 'password', |
| 30 |
'vserver': 'vserver1', |
| 31 |
'name': 'policy1' |
| 32 |
} |
| 33 |
return args |
| 34 |
|
| 35 |
|
| 36 |
# REST API canned responses when mocking send_request |
| 37 |
SRR = { |
| 38 |
# common responses |
| 39 |
'is_rest': (200, dict(version=dict(generation=9, major=9, minor=0, full='dummy')), None), |
| 40 |
'is_rest_9_8': (200, dict(version=dict(generation=9, major=8, minor=0, full='dummy')), None), |
| 41 |
'is_zapi': (400, {}, "Unreachable"), |
| 42 |
'empty_good': (200, {}, None), |
| 43 |
'zero_record': (200, dict(records=[], num_records=0), None), |
| 44 |
'one_record_uuid': (200, dict(records=[dict(uuid='a1b2c3')], num_records=1), None), |
| 45 |
'end_of_sequence': (500, None, "Unexpected call to send_request"), |
| 46 |
'generic_error': (400, None, "Expected error"), |
| 47 |
'job_id_record': ( |
| 48 |
200, { |
| 49 |
'job': { |
| 50 |
'uuid': '94b6e6a7-d426-11eb-ac81-00505690980f', |
| 51 |
'_links': {'self': {'href': '/api/cluster/jobs/94b6e6a7-d426-11eb-ac81-00505690980f'}}}, |
| 52 |
'cli_output': ' Use the "job show -id 2379" command to view the status of this operation.'}, None), |
| 53 |
'job_response_record': ( |
| 54 |
200, { |
| 55 |
"uuid": "f03ccbb6-d8bb-11eb-ac81-00505690980f", |
| 56 |
"description": "File Directory Security Apply Job", |
| 57 |
"state": "success", |
| 58 |
"message": "Complete: Operation completed successfully. File ACLs modified using policy \"policy1\" on Vserver \"GBSMNAS80LD\". File count: 0. [0]", |
| 59 |
"code": 0, |
| 60 |
"start_time": "2021-06-29T05:25:26-04:00", |
| 61 |
"end_time": "2021-06-29T05:25:26-04:00" |
| 62 |
}, None |
| 63 |
) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
def test_module_fail_when_required_args_missing(patch_ansible): |
| 68 |
''' required arguments are reported as errors ''' |
| 69 |
with pytest.raises(AnsibleFailJson) as exc: |
| 70 |
set_module_args({}) |
| 71 |
my_module() |
| 72 |
print('Info: %s' % exc.value.args[0]['msg']) |
| 73 |
|
| 74 |
|
| 75 |
def test_rest_missing_arguments(patch_ansible): # pylint: disable=redefined-outer-name,unused-argument |
| 76 |
''' test missing arguements ''' |
| 77 |
args = dict(default_args()) |
| 78 |
del args['hostname'] |
| 79 |
set_module_args(args) |
| 80 |
with pytest.raises(AnsibleFailJson) as exc: |
| 81 |
my_module() |
| 82 |
msg = 'missing required arguments: hostname' |
| 83 |
assert exc.value.args[0]['msg'] == msg |
| 84 |
|
| 85 |
|
| 86 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 87 |
def test_rest_success(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument |
| 88 |
''' Create job to apply policy to directory ''' |
| 89 |
args = dict(default_args()) |
| 90 |
set_module_args(args) |
| 91 |
mock_request.side_effect = [ |
| 92 |
SRR['is_rest'], |
| 93 |
SRR['job_id_record'], |
| 94 |
SRR['job_response_record'], |
| 95 |
SRR['end_of_sequence'] |
| 96 |
] |
| 97 |
my_obj = my_module() |
| 98 |
with pytest.raises(AnsibleExitJson) as exc: |
| 99 |
my_obj.apply() |
| 100 |
assert exc.value.args[0]['changed'] is True |
| 101 |
print(mock_request.mock_calls) |
| 102 |
assert len(mock_request.mock_calls) == 3 |