Debian Codegraph @

ansible_collections/netapp_eseries/santricity/tests/unit/modules/test_na_santricity_alerts.py

raw symbols SCIP
1 # (c) 2020, NetApp, Inc
2 # BSD-3 Clause (see COPYING or https://opensource.org/licenses/BSD-3-Clause)
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_alerts import NetAppESeriesAlerts
8 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
9 AnsibleFailJson, AnsibleExitJson, ModuleTestCase
10 )
11 from ansible_collections.community.internal_test_tools.tests.unit.compat import mock
12
13
14 class AlertsTest(ModuleTestCase):
15 REQUIRED_PARAMS = {
16 'api_username': 'rw',
17 'api_password': 'password',
18 'api_url': 'http://localhost',
19 'ssid': '1',
20 'state': 'disabled'
21 }
22 REQ_FUNC = 'ansible_collections.netapp_eseries.santricity.plugins.modules.na_santricity_alerts.NetAppESeriesAlerts.request'
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 _validate_args(self, **kwargs):
33 with self._set_args(**kwargs):
34 NetAppESeriesAlerts()
35
36 def test_validation_disable(self):
37 """Ensure a default configuration succeeds"""
38 self._validate_args()
39
40 def test_validation_enable(self):
41 """Ensure a typical, default configuration succeeds"""
42 self._validate_args(state='enabled', server='localhost', sender='[email protected]', recipients=['[email protected]'])
43
44 def test_validation_fail_required(self):
45 """Ensure we fail on missing configuration"""
46
47 # Missing recipients
48 with self.assertRaises(AnsibleFailJson):
49 self._validate_args(state='enabled', server='localhost', sender='[email protected]')
50 NetAppESeriesAlerts()
51
52 # Missing sender
53 with self.assertRaises(AnsibleFailJson):
54 self._validate_args(state='enabled', server='localhost', recipients=['[email protected]'])
55 NetAppESeriesAlerts()
56
57 # Missing server
58 with self.assertRaises(AnsibleFailJson):
59 self._validate_args(state='enabled', sender='[email protected]', recipients=['[email protected]'])
60
61 def test_validation_fail(self):
62 # Empty recipients
63 with self.assertRaises(AnsibleFailJson):
64 self._validate_args(state='enabled', server='localhost', sender='[email protected]', recipients=[])
65
66 # Bad sender
67 with self.assertRaises(AnsibleFailJson):
68 self._validate_args(state='enabled', server='localhost', sender='y.z', recipients=['[email protected]'])
69
70 def test_get_configuration(self):
71 """Validate retrieving the current configuration"""
72 with self._set_args(state='enabled', server='localhost', sender='[email protected]', recipients=['[email protected]']):
73 expected = 'result'
74 alerts = NetAppESeriesAlerts()
75 alerts.is_proxy = lambda: False
76 alerts.is_embedded_available = lambda: False
77
78 # Expecting an update
79 with mock.patch(self.REQ_FUNC, return_value=(200, expected)) as req:
80 actual = alerts.get_configuration()
81 self.assertEqual(expected, actual)
82 self.assertEqual(req.call_count, 1)
83
84 def test_update_configuration(self):
85 """Validate updating the configuration"""
86 initial = dict(alertingEnabled=True,
87 emailServerAddress='localhost',
88 sendAdditionalContactInformation=True,
89 additionalContactInformation='None',
90 emailSenderAddress='[email protected]',
91 recipientEmailAddresses=['[email protected]']
92 )
93
94 args = dict(state='enabled', server=initial['emailServerAddress'], sender=initial['emailSenderAddress'],
95 contact=initial['additionalContactInformation'], recipients=initial['recipientEmailAddresses'])
96
97 with self._set_args(**args):
98 alerts = NetAppESeriesAlerts()
99 alerts.is_proxy = lambda: False
100 alerts.is_embedded_available = lambda: False
101
102 # Ensure when trigger updates when each relevant field is changed
103 with mock.patch(self.REQ_FUNC, return_value=(200, None)) as req:
104 with mock.patch.object(alerts, 'get_configuration', return_value=initial):
105 update = alerts.update_configuration()
106 self.assertFalse(update)
107
108 alerts.sender = '[email protected]'
109 update = alerts.update_configuration()
110 self.assertTrue(update)
111 self._set_args(**args)
112
113 alerts.recipients = ['[email protected]']
114 update = alerts.update_configuration()
115 self.assertTrue(update)
116 self._set_args(**args)
117
118 alerts.contact = 'abc'
119 update = alerts.update_configuration()
120 self.assertTrue(update)
121 self._set_args(**args)
122
123 alerts.server = 'abc'
124 update = alerts.update_configuration()
125 self.assertTrue(update)
126
127 def test_send_test_email_check(self):
128 """Ensure we handle check_mode correctly"""
129 with self._set_args(test=True):
130 alerts = NetAppESeriesAlerts()
131 alerts.check_mode = True
132 with mock.patch(self.REQ_FUNC) as req:
133 with mock.patch.object(alerts, 'update_configuration', return_value=True):
134 alerts.send_test_email()
135 self.assertFalse(req.called)
136
137 def test_send_test_email(self):
138 """Ensure we send a test email if test=True"""
139 with self._set_args(test=True):
140 alerts = NetAppESeriesAlerts()
141 alerts.is_proxy = lambda: False
142 alerts.is_embedded_available = lambda: False
143
144 with mock.patch(self.REQ_FUNC, return_value=(200, dict(response='emailSentOK'))) as req:
145 alerts.send_test_email()
146 self.assertTrue(req.called)
147
148 def test_send_test_email_fail(self):
149 """Ensure we fail if the test returned a failure status"""
150 with self._set_args(test=True):
151 alerts = NetAppESeriesAlerts()
152 alerts.is_proxy = lambda: False
153 alerts.is_embedded_available = lambda: False
154
155 ret_msg = 'fail'
156 with self.assertRaisesRegex(AnsibleFailJson, ret_msg):
157 with mock.patch(self.REQ_FUNC, return_value=(200, dict(response=ret_msg))) as req:
158 alerts.send_test_email()
159 self.assertTrue(req.called)
160
161 def test_send_test_email_fail_connection(self):
162 """Ensure we fail cleanly if we hit a connection failure"""
163 with self._set_args(test=True):
164 alerts = NetAppESeriesAlerts()
165 alerts.is_proxy = lambda: False
166 alerts.is_embedded_available = lambda: False
167
168 with self.assertRaisesRegex(AnsibleFailJson, r"failed to send"):
169 with mock.patch(self.REQ_FUNC, side_effect=Exception) as req:
170 alerts.send_test_email()
171 self.assertTrue(req.called)
172
173 def test_update(self):
174 # Ensure that when test is enabled and alerting is enabled, we run the test
175 with self._set_args(state='enabled', server='localhost', sender='[email protected]', recipients=['[email protected]'], test=True):
176 alerts = NetAppESeriesAlerts()
177 with self.assertRaisesRegex(AnsibleExitJson, r"enabled"):
178 with mock.patch.object(alerts, 'update_configuration', return_value=True):
179 with mock.patch.object(alerts, 'send_test_email') as test:
180 alerts.update()
181 self.assertTrue(test.called)
182
183 # Ensure we don't run a test when changed=False
184 with self.assertRaisesRegex(AnsibleExitJson, r"enabled"):
185 with mock.patch.object(alerts, 'update_configuration', return_value=False):
186 with mock.patch.object(alerts, 'send_test_email') as test:
187 alerts.update()
188 self.assertFalse(test.called)
189
190 # Ensure that test is not called when we have alerting disabled
191 with self._set_args(state='disabled'):
192 alerts = NetAppESeriesAlerts()
193 with self.assertRaisesRegex(AnsibleExitJson, r"disabled"):
194 with mock.patch.object(alerts, 'update_configuration', return_value=True):
195 with mock.patch.object(alerts, 'send_test_email') as test:
196 alerts.update()
197 self.assertFalse(test.called)