| 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_fdspt 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_fdspt \ |
| 18 |
import NetAppOntapFDSPT 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 |
'name': 'policy1', |
| 28 |
'vserver': 'vserver1', |
| 29 |
'hostname': '10.10.10.10', |
| 30 |
'username': 'username', |
| 31 |
'password': 'password', |
| 32 |
'use_rest': 'always', |
| 33 |
'ntfs_mode': 'ignore', |
| 34 |
'security_type': 'ntfs', |
| 35 |
'path': '/' |
| 36 |
} |
| 37 |
return args |
| 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_8': (200, dict(version=dict(generation=9, major=8, minor=0, full='dummy')), None), |
| 45 |
'is_zapi': (400, {}, "Unreachable"), |
| 46 |
'empty_good': (200, {}, None), |
| 47 |
'zero_record': (200, dict(records=[], num_records=0), None), |
| 48 |
'one_record_uuid': (200, dict(records=[dict(uuid='a1b2c3')], num_records=1), None), |
| 49 |
'end_of_sequence': (500, None, "Unexpected call to send_request"), |
| 50 |
'generic_error': (400, None, "Expected error"), |
| 51 |
'policy_task_record': ( |
| 52 |
200, { |
| 53 |
'records': [{ |
| 54 |
'vserver': 'vserver1', |
| 55 |
'policy_name': 'policy1', |
| 56 |
'index_num': 1, |
| 57 |
'path': '/', |
| 58 |
'security_type': 'ntfs', |
| 59 |
'ntfs_mode': 'ignore', |
| 60 |
'access_control': 'file_directory'}], |
| 61 |
'num_records': 1}, |
| 62 |
None), |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
def test_module_fail_when_required_args_missing(patch_ansible): |
| 67 |
''' required arguments are reported as errors ''' |
| 68 |
with pytest.raises(AnsibleFailJson) as exc: |
| 69 |
set_module_args({}) |
| 70 |
my_module() |
| 71 |
print('Info: %s' % exc.value.args[0]['msg']) |
| 72 |
|
| 73 |
|
| 74 |
def test_rest_missing_arguments(patch_ansible): # pylint: disable=redefined-outer-name,unused-argument |
| 75 |
''' test missing arguements ''' |
| 76 |
args = dict(default_args()) |
| 77 |
del args['hostname'] |
| 78 |
set_module_args(args) |
| 79 |
with pytest.raises(AnsibleFailJson) as exc: |
| 80 |
my_module() |
| 81 |
msg = 'missing required arguments: hostname' |
| 82 |
assert exc.value.args[0]['msg'] == msg |
| 83 |
|
| 84 |
|
| 85 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 86 |
def test_rest_create(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument |
| 87 |
''' Create security policies''' |
| 88 |
args = dict(default_args()) |
| 89 |
args['name'] = 'new_policy_task' |
| 90 |
print(args) |
| 91 |
set_module_args(args) |
| 92 |
mock_request.side_effect = [ |
| 93 |
SRR['is_rest'], |
| 94 |
SRR['zero_record'], |
| 95 |
SRR['empty_good'], # create |
| 96 |
SRR['end_of_sequence'] |
| 97 |
] |
| 98 |
my_obj = my_module() |
| 99 |
with pytest.raises(AnsibleExitJson) as exc: |
| 100 |
my_obj.apply() |
| 101 |
assert exc.value.args[0]['changed'] |
| 102 |
print(mock_request.mock_calls) |
| 103 |
assert len(mock_request.mock_calls) == 3 |
| 104 |
|
| 105 |
|
| 106 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 107 |
def test_rest_remove(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument |
| 108 |
''' remove Security policies ''' |
| 109 |
args = dict(default_args()) |
| 110 |
args['state'] = 'absent' |
| 111 |
set_module_args(args) |
| 112 |
mock_request.side_effect = [ |
| 113 |
SRR['is_rest'], |
| 114 |
SRR['policy_task_record'], |
| 115 |
SRR['empty_good'], # delete |
| 116 |
SRR['end_of_sequence'] |
| 117 |
] |
| 118 |
my_obj = my_module() |
| 119 |
with pytest.raises(AnsibleExitJson) as exc: |
| 120 |
my_obj.apply() |
| 121 |
assert exc.value.args[0]['changed'] is True |
| 122 |
print(mock_request.mock_calls) |
| 123 |
assert len(mock_request.mock_calls) == 3 |
| 124 |
|
| 125 |
|
| 126 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 127 |
def test_rest_modify(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument |
| 128 |
''' remove Security policies ''' |
| 129 |
args = dict(default_args()) |
| 130 |
args['state'] = 'present' |
| 131 |
args['name'] = 'policy1' |
| 132 |
args['ntfs_mode'] = 'replace' |
| 133 |
set_module_args(args) |
| 134 |
mock_request.side_effect = [ |
| 135 |
SRR['is_rest'], |
| 136 |
SRR['policy_task_record'], |
| 137 |
SRR['empty_good'], # delete |
| 138 |
SRR['empty_good'], # add |
| 139 |
SRR['end_of_sequence'] |
| 140 |
] |
| 141 |
my_obj = my_module() |
| 142 |
with pytest.raises(AnsibleExitJson) as exc: |
| 143 |
my_obj.apply() |
| 144 |
assert exc.value.args[0]['changed'] is True |
| 145 |
print(mock_request.mock_calls) |
| 146 |
assert len(mock_request.mock_calls) == 4 |
| 147 |
|
| 148 |
|
| 149 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request') |
| 150 |
def test_rest_no_action(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument |
| 151 |
''' Idempotent test ''' |
| 152 |
args = dict(default_args()) |
| 153 |
set_module_args(args) |
| 154 |
mock_request.side_effect = [ |
| 155 |
SRR['is_rest'], |
| 156 |
SRR['policy_task_record'], |
| 157 |
SRR['end_of_sequence'] |
| 158 |
] |
| 159 |
my_obj = my_module() |
| 160 |
with pytest.raises(AnsibleExitJson) as exc: |
| 161 |
my_obj.apply() |
| 162 |
assert exc.value.args[0]['changed'] is False |
| 163 |
print(mock_request.mock_calls) |
| 164 |
assert len(mock_request.mock_calls) == 2 |