| 1 |
|
| 2 |
|
| 3 |
from __future__ import absolute_import, division, print_function |
| 4 |
__metaclass__ = type |
| 5 |
from contextlib import contextmanager |
| 6 |
from ansible.module_utils.testing import patch_module_args |
| 7 |
from ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_auditlog import NetAppESeriesAuditLog |
| 8 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 9 |
AnsibleFailJson, ModuleTestCase |
| 10 |
) |
| 11 |
from ansible_collections.community.internal_test_tools.tests.unit.compat import mock |
| 12 |
|
| 13 |
|
| 14 |
class NetAppESeriesAuditLogTests(ModuleTestCase): |
| 15 |
REQUIRED_PARAMS = {'api_username': 'rw', |
| 16 |
'api_password': 'password', |
| 17 |
'api_url': 'http://localhost', |
| 18 |
'ssid': '1'} |
| 19 |
REQ_FUNC = 'ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_auditlog.NetAppESeriesAuditLog.request' |
| 20 |
BASE_REQ_FUNC = 'ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity.request' |
| 21 |
MAX_RECORDS_MAXIMUM = 50000 |
| 22 |
MAX_RECORDS_MINIMUM = 100 |
| 23 |
|
| 24 |
@contextmanager |
| 25 |
def _set_args(self, **kwargs): |
| 26 |
module_args = self.REQUIRED_PARAMS.copy() |
| 27 |
if kwargs is not None: |
| 28 |
module_args.update(kwargs) |
| 29 |
with patch_module_args(module_args): |
| 30 |
yield |
| 31 |
|
| 32 |
def test_max_records_argument_pass(self): |
| 33 |
"""Verify NetAppESeriesAuditLog argument's max_records and threshold upper and lower boundaries.""" |
| 34 |
initial = {"max_records": 1000, |
| 35 |
"log_level": "writeOnly", |
| 36 |
"full_policy": "overWrite", |
| 37 |
"threshold": 90} |
| 38 |
max_records_set = (self.MAX_RECORDS_MINIMUM, 25000, self.MAX_RECORDS_MAXIMUM) |
| 39 |
|
| 40 |
for max_records in max_records_set: |
| 41 |
initial["max_records"] = max_records |
| 42 |
with self._set_args(**initial): |
| 43 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 44 |
audit_log = NetAppESeriesAuditLog() |
| 45 |
self.assertTrue(audit_log.max_records == max_records) |
| 46 |
|
| 47 |
def test_max_records_argument_fail(self): |
| 48 |
"""Verify NetAppESeriesAuditLog arument's max_records and threshold upper and lower boundaries.""" |
| 49 |
initial = {"max_records": 1000, |
| 50 |
"log_level": "writeOnly", |
| 51 |
"full_policy": "overWrite", |
| 52 |
"threshold": 90} |
| 53 |
max_records_set = (self.MAX_RECORDS_MINIMUM - 1, self.MAX_RECORDS_MAXIMUM + 1) |
| 54 |
|
| 55 |
for max_records in max_records_set: |
| 56 |
with self.assertRaisesRegex(AnsibleFailJson, r"Audit-log max_records count must be between 100 and 50000"): |
| 57 |
initial["max_records"] = max_records |
| 58 |
with self._set_args(**initial): |
| 59 |
NetAppESeriesAuditLog() |
| 60 |
|
| 61 |
def test_threshold_argument_pass(self): |
| 62 |
"""Verify NetAppESeriesAuditLog argument's max_records and threshold upper and lower boundaries.""" |
| 63 |
initial = {"max_records": 1000, |
| 64 |
"log_level": "writeOnly", |
| 65 |
"full_policy": "overWrite", |
| 66 |
"threshold": 90} |
| 67 |
threshold_set = (60, 75, 90) |
| 68 |
|
| 69 |
for threshold in threshold_set: |
| 70 |
initial["threshold"] = threshold |
| 71 |
with self._set_args(**initial): |
| 72 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 73 |
audit_log = NetAppESeriesAuditLog() |
| 74 |
self.assertTrue(audit_log.threshold == threshold) |
| 75 |
|
| 76 |
def test_threshold_argument_fail(self): |
| 77 |
"""Verify NetAppESeriesAuditLog arument's max_records and threshold upper and lower boundaries.""" |
| 78 |
initial = {"max_records": 1000, |
| 79 |
"log_level": "writeOnly", |
| 80 |
"full_policy": "overWrite", |
| 81 |
"threshold": 90} |
| 82 |
threshold_set = (59, 91) |
| 83 |
|
| 84 |
for threshold in threshold_set: |
| 85 |
with self.assertRaisesRegex(AnsibleFailJson, r"Audit-log percent threshold must be between 60 and 90"): |
| 86 |
initial["threshold"] = threshold |
| 87 |
with self._set_args(**initial): |
| 88 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 89 |
NetAppESeriesAuditLog() |
| 90 |
|
| 91 |
def test_get_configuration_pass(self): |
| 92 |
"""Validate get configuration does not throw exception when normal request is returned.""" |
| 93 |
initial = {"max_records": 1000, |
| 94 |
"log_level": "writeOnly", |
| 95 |
"full_policy": "overWrite", |
| 96 |
"threshold": 90} |
| 97 |
expected = {"auditLogMaxRecords": 1000, |
| 98 |
"auditLogLevel": "writeOnly", |
| 99 |
"auditLogFullPolicy": "overWrite", |
| 100 |
"auditLogWarningThresholdPct": 90} |
| 101 |
|
| 102 |
with self._set_args(**initial): |
| 103 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 104 |
audit_log = NetAppESeriesAuditLog() |
| 105 |
|
| 106 |
with mock.patch(self.REQ_FUNC, return_value=(200, expected)): |
| 107 |
body = audit_log.get_configuration() |
| 108 |
self.assertTrue(body == expected) |
| 109 |
|
| 110 |
def test_get_configuration_fail(self): |
| 111 |
"""Verify AnsibleJsonFail exception is thrown.""" |
| 112 |
initial = {"max_records": 1000, |
| 113 |
"log_level": "writeOnly", |
| 114 |
"full_policy": "overWrite", |
| 115 |
"threshold": 90} |
| 116 |
|
| 117 |
with self._set_args(**initial): |
| 118 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 119 |
audit_log = NetAppESeriesAuditLog() |
| 120 |
|
| 121 |
with self.assertRaisesRegex(AnsibleFailJson, r"Failed to retrieve the audit-log configuration!"): |
| 122 |
with mock.patch(self.REQ_FUNC, return_value=Exception()): |
| 123 |
audit_log.get_configuration() |
| 124 |
|
| 125 |
def test_build_configuration_pass(self): |
| 126 |
"""Validate configuration changes will force an update.""" |
| 127 |
response = {"auditLogMaxRecords": 1000, |
| 128 |
"auditLogLevel": "writeOnly", |
| 129 |
"auditLogFullPolicy": "overWrite", |
| 130 |
"auditLogWarningThresholdPct": 90} |
| 131 |
initial = {"max_records": 1000, |
| 132 |
"log_level": "writeOnly", |
| 133 |
"full_policy": "overWrite", |
| 134 |
"threshold": 90} |
| 135 |
changes = [{"max_records": 50000}, |
| 136 |
{"log_level": "all"}, |
| 137 |
{"full_policy": "preventSystemAccess"}, |
| 138 |
{"threshold": 75}] |
| 139 |
|
| 140 |
for change in changes: |
| 141 |
initial_with_changes = initial.copy() |
| 142 |
initial_with_changes.update(change) |
| 143 |
with self._set_args(**initial_with_changes): |
| 144 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 145 |
audit_log = NetAppESeriesAuditLog() |
| 146 |
|
| 147 |
with mock.patch(self.REQ_FUNC, return_value=(200, response)): |
| 148 |
update = audit_log.build_configuration() |
| 149 |
self.assertTrue(update) |
| 150 |
|
| 151 |
def test_delete_log_messages_fail(self): |
| 152 |
"""Verify AnsibleJsonFail exception is thrown.""" |
| 153 |
initial = {"max_records": 1000, |
| 154 |
"log_level": "writeOnly", |
| 155 |
"full_policy": "overWrite", |
| 156 |
"threshold": 90} |
| 157 |
|
| 158 |
with self._set_args(**initial): |
| 159 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 160 |
audit_log = NetAppESeriesAuditLog() |
| 161 |
|
| 162 |
with self.assertRaisesRegex(AnsibleFailJson, r"Failed to delete audit-log messages!"): |
| 163 |
with mock.patch(self.REQ_FUNC, return_value=Exception()): |
| 164 |
audit_log.delete_log_messages() |
| 165 |
|
| 166 |
def test_update_configuration_delete_pass(self): |
| 167 |
"""Verify 422 and force successfully returns True.""" |
| 168 |
body = {"auditLogMaxRecords": 1000, |
| 169 |
"auditLogLevel": "writeOnly", |
| 170 |
"auditLogFullPolicy": "overWrite", |
| 171 |
"auditLogWarningThresholdPct": 90} |
| 172 |
initial = {"max_records": 2000, |
| 173 |
"log_level": "writeOnly", |
| 174 |
"full_policy": "overWrite", |
| 175 |
"threshold": 90, |
| 176 |
"force": True} |
| 177 |
|
| 178 |
with self._set_args(**initial): |
| 179 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 180 |
audit_log = NetAppESeriesAuditLog() |
| 181 |
with mock.patch(self.REQ_FUNC, side_effect=[(200, body), |
| 182 |
(422, {u"invalidFieldsIfKnown": None, |
| 183 |
u"errorMessage": u"Configuration change...", |
| 184 |
u"localizedMessage": u"Configuration change...", |
| 185 |
u"retcode": u"auditLogImmediateFullCondition", |
| 186 |
u"codeType": u"devicemgrerror"}), |
| 187 |
(200, None), |
| 188 |
(200, None)]): |
| 189 |
self.assertTrue(audit_log.update_configuration()) |
| 190 |
|
| 191 |
def test_update_configuration_delete_skip_fail(self): |
| 192 |
"""Verify 422 and no force results in AnsibleJsonFail exception.""" |
| 193 |
body = {"auditLogMaxRecords": 1000, |
| 194 |
"auditLogLevel": "writeOnly", |
| 195 |
"auditLogFullPolicy": "overWrite", |
| 196 |
"auditLogWarningThresholdPct": 90} |
| 197 |
initial = {"max_records": 2000, |
| 198 |
"log_level": "writeOnly", |
| 199 |
"full_policy": "overWrite", |
| 200 |
"threshold": 90, |
| 201 |
"force": False} |
| 202 |
|
| 203 |
with self._set_args(**initial): |
| 204 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 205 |
audit_log = NetAppESeriesAuditLog() |
| 206 |
|
| 207 |
with self.assertRaisesRegex(AnsibleFailJson, r"Failed to update audit-log configuration!"): |
| 208 |
with mock.patch(self.REQ_FUNC, side_effect=[(200, body), Exception(422, {"errorMessage": "error"}), |
| 209 |
(200, None), (200, None)]): |
| 210 |
audit_log.update_configuration() |