| 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 |
import time |
| 8 |
from ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_asup import NetAppESeriesAsup |
| 9 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 10 |
AnsibleFailJson, ModuleTestCase |
| 11 |
) |
| 12 |
from ansible_collections.community.internal_test_tools.tests.unit.compat import mock |
| 13 |
|
| 14 |
|
| 15 |
class AsupTest(ModuleTestCase): |
| 16 |
REQUIRED_PARAMS = { |
| 17 |
"api_username": "rw", |
| 18 |
"api_password": "password", |
| 19 |
"api_url": "http://localhost", |
| 20 |
"ssid": "1", |
| 21 |
} |
| 22 |
|
| 23 |
REQ_FUNC = "ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_asup.NetAppESeriesAsup.request" |
| 24 |
BASE_REQ_FUNC = 'ansible_collections.netapp_eseries.santricity.plugins.module_utils.santricity.request' |
| 25 |
TIME_FUNC = "ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_asup.time.time" |
| 26 |
|
| 27 |
@contextmanager |
| 28 |
def _set_args(self, args=None): |
| 29 |
module_args = self.REQUIRED_PARAMS.copy() |
| 30 |
if args is not None: |
| 31 |
module_args.update(args) |
| 32 |
with patch_module_args(module_args): |
| 33 |
yield |
| 34 |
|
| 35 |
def test_valid_options_pass(self): |
| 36 |
"""Validate valid options.""" |
| 37 |
options_list = [ |
| 38 |
{"state": "disabled", "active": False}, |
| 39 |
{"state": "enabled", "active": False, "start": 20, "end": 24, "days": ["saturday", "sunday"], |
| 40 |
"method": "email", "email": {"server": "192.168.1.100", "sender": "[email protected]"}}, |
| 41 |
{"state": "enabled", "active": False, "start": 20, "end": 24, "days": ["saturday", "sunday"], |
| 42 |
"method": "https", "routing_type": "direct"}, |
| 43 |
{"state": "enabled", "active": False, "start": 20, "end": 24, "days": ["saturday", "sunday"], |
| 44 |
"method": "https", "routing_type": "proxy", "proxy": {"host": "192.168.1.100", "port": 1234}}, |
| 45 |
{"state": "enabled", "active": False, "start": 20, "end": 24, "days": ["saturday", "sunday"], |
| 46 |
"method": "https", "routing_type": "script", "proxy": {"script": "/path/to/proxy/script.sh"}}, |
| 47 |
{"state": "maintenance_enabled", "maintenance_duration": 24, "maintenance_emails": ["[email protected]", "[email protected]"]}, |
| 48 |
{"state": "maintenance_disabled"} |
| 49 |
] |
| 50 |
|
| 51 |
for options in options_list: |
| 52 |
with self._set_args(options): |
| 53 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 54 |
asup = NetAppESeriesAsup() |
| 55 |
for options in options_list: |
| 56 |
with self._set_args(options): |
| 57 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": True})]): |
| 58 |
asup = NetAppESeriesAsup() |
| 59 |
|
| 60 |
def test_invalid_options_fail(self): |
| 61 |
"""Verify invalid options throw expected exceptions.""" |
| 62 |
options_list = [ |
| 63 |
{"state": "enabled", "active": False, "start": 24, "end": 23, "days": ["saturday", "sunday"], |
| 64 |
"method": "email", "email": {"server": "192.168.1.100", "sender": "[email protected]"}}, |
| 65 |
{"state": "enabled", "active": False, "start": -1, "end": 23, "days": ["saturday", "sunday"], |
| 66 |
"method": "email", "email": {"server": "192.168.1.100", "sender": "[email protected]"}}, |
| 67 |
{"state": "enabled", "active": False, "start": 20, "end": 25, "days": ["saturday", "sunday"], |
| 68 |
"method": "email", "email": {"server": "192.168.1.100", "sender": "[email protected]"}}, |
| 69 |
{"state": "enabled", "active": False, "start": 20, "end": 24, "days": ["not_a_day", "sunday"], |
| 70 |
"method": "https", "routing_type": "direct"}, |
| 71 |
{"state": "maintenance_enabled", "maintenance_duration": 0, "maintenance_emails": ["[email protected]", "[email protected]"]}, |
| 72 |
{"state": "maintenance_enabled", "maintenance_duration": 73, "maintenance_emails": ["[email protected]", "[email protected]"]}, |
| 73 |
] |
| 74 |
|
| 75 |
for options in options_list: |
| 76 |
with self._set_args(options): |
| 77 |
with self.assertRaises(AnsibleFailJson): |
| 78 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 79 |
asup = NetAppESeriesAsup() |
| 80 |
|
| 81 |
def test_get_configuration_fail(self): |
| 82 |
"""Verify get_configuration method throws expected exceptions.""" |
| 83 |
with self._set_args({"state": "disabled", "active": False}): |
| 84 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 85 |
asup = NetAppESeriesAsup() |
| 86 |
with mock.patch(self.REQ_FUNC, return_value=Exception()): |
| 87 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to retrieve ASUP configuration!"): |
| 88 |
asup.get_configuration() |
| 89 |
with self._set_args({"state": "disabled", "active": False}): |
| 90 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 91 |
asup = NetAppESeriesAsup() |
| 92 |
with mock.patch(self.REQ_FUNC, return_value=(200, {"asupCapable": False, "onDemandCapable": True})): |
| 93 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to retrieve ASUP configuration!"): |
| 94 |
asup.get_configuration() |
| 95 |
with self._set_args({"state": "disabled", "active": False}): |
| 96 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 97 |
asup = NetAppESeriesAsup() |
| 98 |
with mock.patch(self.REQ_FUNC, return_value=(200, {"asupCapable": True, "onDemandCapable": False})): |
| 99 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to retrieve ASUP configuration!"): |
| 100 |
asup.get_configuration() |
| 101 |
with self._set_args({"state": "disabled", "active": False}): |
| 102 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 103 |
asup = NetAppESeriesAsup() |
| 104 |
with mock.patch(self.REQ_FUNC, return_value=(200, {"asupCapable": False, "onDemandCapable": False})): |
| 105 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to retrieve ASUP configuration!"): |
| 106 |
asup.get_configuration() |
| 107 |
|
| 108 |
def test_in_maintenance_mode_pass(self): |
| 109 |
"""Verify whether asup is in maintenance mode successful.""" |
| 110 |
with self._set_args({"state": "disabled", "active": False}): |
| 111 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 112 |
asup = NetAppESeriesAsup() |
| 113 |
with mock.patch(self.REQ_FUNC, return_value=(200, [{"key": "ansible_asup_maintenance_stop_time", "value": str(time.time() + 10000)}])): |
| 114 |
self.assertTrue(asup.in_maintenance_mode()) |
| 115 |
|
| 116 |
with self._set_args({"state": "disabled", "active": False}): |
| 117 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 118 |
asup = NetAppESeriesAsup() |
| 119 |
with mock.patch(self.REQ_FUNC, return_value=(200, [{"key": "ansible_asup_maintenance_email_list", "value": "[email protected],[email protected]"}, |
| 120 |
{"key": "ansible_asup_maintenance_stop_time", "value": str(time.time() - 1)}])): |
| 121 |
self.assertFalse(asup.in_maintenance_mode()) |
| 122 |
|
| 123 |
def test_in_maintenance_mode_fail(self): |
| 124 |
"""Verify that in_maintenance mode throws expected exceptions.""" |
| 125 |
with self._set_args({"state": "disabled", "active": False}): |
| 126 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 127 |
asup = NetAppESeriesAsup() |
| 128 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to retrieve maintenance windows information!"): |
| 129 |
with mock.patch(self.REQ_FUNC, return_value=Exception()): |
| 130 |
asup.in_maintenance_mode() |
| 131 |
|
| 132 |
def test_update_configuration_pass(self): |
| 133 |
"""Verify that update_configuration completes successfully.""" |
| 134 |
asup_config = [{"asupCapable": True, |
| 135 |
"onDemandCapable": True, |
| 136 |
"asupEnabled": True, |
| 137 |
"onDemandEnabled": True, |
| 138 |
"remoteDiagsEnabled": True, |
| 139 |
"delivery": {"method": "smtp", |
| 140 |
"routingType": "none", |
| 141 |
"proxyHost": None, |
| 142 |
"proxyPort": 0, |
| 143 |
"proxyUserName": None, |
| 144 |
"proxyPassword": None, |
| 145 |
"proxyScript": None, |
| 146 |
"mailRelayServer": "[email protected]", |
| 147 |
"mailSenderAddress": "[email protected]"}, |
| 148 |
"destinationAddress": "[email protected]", |
| 149 |
"schedule": {"dailyMinTime": 0, |
| 150 |
"dailyMaxTime": 1439, |
| 151 |
"weeklyMinTime": 0, |
| 152 |
"weeklyMaxTime": 1439, |
| 153 |
"daysOfWeek": ["sunday", "monday", "tuesday"]}}, |
| 154 |
{"asupCapable": True, |
| 155 |
"onDemandCapable": True, |
| 156 |
"asupEnabled": True, |
| 157 |
"onDemandEnabled": False, |
| 158 |
"remoteDiagsEnabled": False, |
| 159 |
"delivery": { |
| 160 |
"method": "https", |
| 161 |
"routingType": "proxyServer", |
| 162 |
"proxyHost": "192.168.1.100", |
| 163 |
"proxyPort": 1234, |
| 164 |
"proxyUserName": None, |
| 165 |
"proxyPassword": None, |
| 166 |
"proxyScript": None, |
| 167 |
"mailRelayServer": None, |
| 168 |
"mailSenderAddress": None |
| 169 |
}, |
| 170 |
"destinationAddress": "https://support.netapp.com/put/AsupPut/", |
| 171 |
"schedule": { |
| 172 |
"dailyMinTime": 1200, |
| 173 |
"dailyMaxTime": 1439, |
| 174 |
"weeklyMinTime": 0, |
| 175 |
"weeklyMaxTime": 1439, |
| 176 |
"daysOfWeek": ["sunday", "saturday"]}}, |
| 177 |
{"asupCapable": True, |
| 178 |
"onDemandCapable": True, |
| 179 |
"asupEnabled": True, |
| 180 |
"onDemandEnabled": False, |
| 181 |
"remoteDiagsEnabled": False, |
| 182 |
"delivery": { |
| 183 |
"method": "https", |
| 184 |
"routingType": "proxyScript", |
| 185 |
"proxyHost": None, |
| 186 |
"proxyPort": 0, |
| 187 |
"proxyUserName": None, |
| 188 |
"proxyPassword": None, |
| 189 |
"proxyScript": "/home/user/path/to/script.sh", |
| 190 |
"mailRelayServer": None, |
| 191 |
"mailSenderAddress": None |
| 192 |
}, |
| 193 |
"destinationAddress": "https://support.netapp.com/put/AsupPut/", |
| 194 |
"schedule": { |
| 195 |
"dailyMinTime": 0, |
| 196 |
"dailyMaxTime": 420, |
| 197 |
"weeklyMinTime": 0, |
| 198 |
"weeklyMaxTime": 1439, |
| 199 |
"daysOfWeek": ["monday", "tuesday", "wednesday", "thursday", "friday"]}}] |
| 200 |
options_list = [{"state": "disabled", "active": False}, |
| 201 |
{"state": "enabled", "active": False, "start": 20, "end": 24, "days": ["saturday"], |
| 202 |
"method": "email", "email": {"server": "192.168.1.100", "sender": "[email protected]"}}, |
| 203 |
{"state": "enabled", "active": False, "start": 20, "end": 24, "days": ["sunday"], |
| 204 |
"method": "https", "routing_type": "direct"}, |
| 205 |
{"state": "enabled", "active": False, "start": 20, "end": 24, "days": ["saturday", "sunday"], |
| 206 |
"method": "https", "routing_type": "proxy", "proxy": {"host": "192.168.1.100", "port": 1234}}, |
| 207 |
{"state": "enabled", "active": False, "start": 20, "end": 24, "days": ["saturday", "sunday"], |
| 208 |
"method": "https", "routing_type": "script", "proxy": {"script": "/path/to/proxy/script.sh"}}, |
| 209 |
{"state": "maintenance_enabled", "maintenance_duration": 24, "maintenance_emails": ["[email protected]", "[email protected]"]}, |
| 210 |
{"state": "maintenance_disabled"}] |
| 211 |
|
| 212 |
for index, options in enumerate(options_list): |
| 213 |
with self._set_args(options): |
| 214 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 215 |
asup = NetAppESeriesAsup() |
| 216 |
asup.get_configuration = lambda: asup_config[index % 3] |
| 217 |
asup.in_maintenance_mode = lambda: False |
| 218 |
|
| 219 |
with mock.patch(self.REQ_FUNC, return_value=(200, None)): |
| 220 |
asup.update_configuration() |
| 221 |
|
| 222 |
def test_update_configuration_fail(self): |
| 223 |
"""Verify that update_configuration throws expected exceptions.""" |
| 224 |
asup_config = {"asupCapable": True, |
| 225 |
"onDemandCapable": True, |
| 226 |
"asupEnabled": True, |
| 227 |
"onDemandEnabled": True, |
| 228 |
"remoteDiagsEnabled": True, |
| 229 |
"delivery": {"method": "smtp", |
| 230 |
"routingType": "none", |
| 231 |
"proxyHost": None, |
| 232 |
"proxyPort": 0, |
| 233 |
"proxyUserName": None, |
| 234 |
"proxyPassword": None, |
| 235 |
"proxyScript": None, |
| 236 |
"mailRelayServer": "[email protected]", |
| 237 |
"mailSenderAddress": "[email protected]"}, |
| 238 |
"destinationAddress": "[email protected]", |
| 239 |
"schedule": {"dailyMinTime": 0, |
| 240 |
"dailyMaxTime": 1439, |
| 241 |
"weeklyMinTime": 0, |
| 242 |
"weeklyMaxTime": 1439, |
| 243 |
"daysOfWeek": ["sunday", "monday", "tuesday"]}} |
| 244 |
|
| 245 |
|
| 246 |
with self._set_args({ |
| 247 |
"state": "enabled", "active": False, "start": 20, "end": 24, "days": ["saturday"], |
| 248 |
"method": "email", "email": {"server": "192.168.1.100", "sender": "[email protected]"} |
| 249 |
}): |
| 250 |
with mock.patch( |
| 251 |
self.BASE_REQ_FUNC, |
| 252 |
side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})] |
| 253 |
): |
| 254 |
asup = NetAppESeriesAsup() |
| 255 |
asup.get_configuration = lambda: asup_config |
| 256 |
asup.in_maintenance_mode = lambda: False |
| 257 |
asup.validate = lambda: True |
| 258 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to validate ASUP configuration!"): |
| 259 |
with mock.patch(self.REQ_FUNC, return_value=Exception()): |
| 260 |
asup.update_configuration() |
| 261 |
with self._set_args({"state": "disabled", "active": False}): |
| 262 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 263 |
asup = NetAppESeriesAsup() |
| 264 |
asup.get_configuration = lambda: asup_config |
| 265 |
asup.in_maintenance_mode = lambda: False |
| 266 |
asup.validate = lambda: False |
| 267 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to change ASUP configuration!"): |
| 268 |
with mock.patch(self.REQ_FUNC, return_value=Exception()): |
| 269 |
asup.update_configuration() |
| 270 |
|
| 271 |
|
| 272 |
with self._set_args({"state": "maintenance_enabled", "maintenance_duration": 24, "maintenance_emails": ["[email protected]", "[email protected]"]}): |
| 273 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 274 |
asup = NetAppESeriesAsup() |
| 275 |
asup.get_configuration = lambda: {"asupEnabled": False} |
| 276 |
asup.in_maintenance_mode = lambda: False |
| 277 |
with self.assertRaisesRegex(AnsibleFailJson, "AutoSupport must be enabled before enabling or disabling maintenance mode."): |
| 278 |
asup.update_configuration() |
| 279 |
with self._set_args({"state": "maintenance_enabled", "maintenance_duration": 24, "maintenance_emails": ["[email protected]", "[email protected]"]}): |
| 280 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 281 |
asup = NetAppESeriesAsup() |
| 282 |
asup.get_configuration = lambda: {"asupEnabled": True} |
| 283 |
asup.in_maintenance_mode = lambda: False |
| 284 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to enabled ASUP maintenance window."): |
| 285 |
with mock.patch(self.REQ_FUNC, return_value=Exception()): |
| 286 |
asup.update_configuration() |
| 287 |
with self._set_args({"state": "maintenance_enabled", "maintenance_duration": 24, "maintenance_emails": ["[email protected]", "[email protected]"]}): |
| 288 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 289 |
asup = NetAppESeriesAsup() |
| 290 |
asup.get_configuration = lambda: {"asupEnabled": True} |
| 291 |
asup.in_maintenance_mode = lambda: False |
| 292 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to store maintenance information."): |
| 293 |
with mock.patch(self.REQ_FUNC, side_effect=[(200, None), Exception()]): |
| 294 |
asup.update_configuration() |
| 295 |
with self._set_args({"state": "maintenance_enabled", "maintenance_duration": 24, "maintenance_emails": ["[email protected]", "[email protected]"]}): |
| 296 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 297 |
asup = NetAppESeriesAsup() |
| 298 |
asup.get_configuration = lambda: {"asupEnabled": True} |
| 299 |
asup.in_maintenance_mode = lambda: False |
| 300 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to store maintenance information."): |
| 301 |
with mock.patch(self.REQ_FUNC, side_effect=[(200, None), (200, None), Exception()]): |
| 302 |
asup.update_configuration() |
| 303 |
|
| 304 |
|
| 305 |
with self._set_args({"state": "maintenance_disabled"}): |
| 306 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 307 |
asup = NetAppESeriesAsup() |
| 308 |
asup.get_configuration = lambda: {"asupEnabled": True} |
| 309 |
asup.in_maintenance_mode = lambda: True |
| 310 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to disable ASUP maintenance window."): |
| 311 |
with mock.patch(self.REQ_FUNC, return_value=Exception()): |
| 312 |
asup.update_configuration() |
| 313 |
with self._set_args({"state": "maintenance_disabled"}): |
| 314 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 315 |
asup = NetAppESeriesAsup() |
| 316 |
asup.get_configuration = lambda: {"asupEnabled": True} |
| 317 |
asup.in_maintenance_mode = lambda: True |
| 318 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to store maintenance information."): |
| 319 |
with mock.patch(self.REQ_FUNC, side_effect=[(200, None), Exception()]): |
| 320 |
asup.update_configuration() |
| 321 |
with self._set_args({"state": "maintenance_disabled"}): |
| 322 |
with mock.patch(self.BASE_REQ_FUNC, side_effect=[(200, {"version": "04.00.00.00"}), (200, {"runningAsProxy": False})]): |
| 323 |
asup = NetAppESeriesAsup() |
| 324 |
asup.get_configuration = lambda: {"asupEnabled": True} |
| 325 |
asup.in_maintenance_mode = lambda: True |
| 326 |
with self.assertRaisesRegex(AnsibleFailJson, "Failed to store maintenance information."): |
| 327 |
with mock.patch(self.REQ_FUNC, side_effect=[(200, None), (200, None), Exception()]): |
| 328 |
asup.update_configuration() |