Esempio n. 1
0
    def test_update(self):
        # Ensure that when test is enabled and alerting is enabled, we run the test
        self._set_args(state='enabled',
                       server='localhost',
                       sender='[email protected]',
                       recipients=['[email protected]'],
                       test=True)
        alerts = Alerts()
        with self.assertRaisesRegexp(AnsibleExitJson, r"enabled"):
            with mock.patch.object(alerts,
                                   'update_configuration',
                                   return_value=True):
                with mock.patch.object(alerts, 'send_test_email') as test:
                    alerts.update()
                    self.assertTrue(test.called)

        # Ensure we don't run a test when changed=False
        with self.assertRaisesRegexp(AnsibleExitJson, r"enabled"):
            with mock.patch.object(alerts,
                                   'update_configuration',
                                   return_value=False):
                with mock.patch.object(alerts, 'send_test_email') as test:
                    alerts.update()
                    self.assertFalse(test.called)

        # Ensure that test is not called when we have alerting disabled
        self._set_args(state='disabled')
        alerts = Alerts()
        with self.assertRaisesRegexp(AnsibleExitJson, r"disabled"):
            with mock.patch.object(alerts,
                                   'update_configuration',
                                   return_value=True):
                with mock.patch.object(alerts, 'send_test_email') as test:
                    alerts.update()
                    self.assertFalse(test.called)
Esempio n. 2
0
    def test_send_test_email(self):
        """Ensure we send a test email if test=True"""
        self._set_args(test=True)
        alerts = Alerts()

        with mock.patch(self.REQ_FUNC, return_value=(200, dict(response='emailSentOK'))) as req:
            alerts.send_test_email()
            self.assertTrue(req.called)
Esempio n. 3
0
    def test_send_test_email_fail_connection(self):
        """Ensure we fail cleanly if we hit a connection failure"""
        self._set_args(test=True)
        alerts = Alerts()

        with self.assertRaisesRegexp(AnsibleFailJson, r"failed to send"):
            with mock.patch(self.REQ_FUNC, side_effect=Exception) as req:
                alerts.send_test_email()
                self.assertTrue(req.called)
Esempio n. 4
0
 def test_send_test_email_check(self):
     """Ensure we handle check_mode correctly"""
     self._set_args(test=True)
     alerts = Alerts()
     alerts.check_mode = True
     with mock.patch(self.REQ_FUNC) as req:
         with mock.patch.object(alerts, 'update_configuration', return_value=True):
             alerts.send_test_email()
             self.assertFalse(req.called)
Esempio n. 5
0
    def test_send_test_email_fail(self):
        """Ensure we fail if the test returned a failure status"""
        self._set_args(test=True)
        alerts = Alerts()

        ret_msg = 'fail'
        with self.assertRaisesRegexp(AnsibleFailJson, ret_msg):
            with mock.patch(self.REQ_FUNC, return_value=(200, dict(response=ret_msg))) as req:
                alerts.send_test_email()
                self.assertTrue(req.called)
Esempio n. 6
0
    def test_get_configuration(self):
        """Validate retrieving the current configuration"""
        self._set_args(state='enabled', server='localhost', sender='[email protected]', recipients=['[email protected]'])

        expected = 'result'
        alerts = Alerts()
        # Expecting an update
        with mock.patch(self.REQ_FUNC, return_value=(200, expected)) as req:
            actual = alerts.get_configuration()
            self.assertEquals(expected, actual)
            self.assertEquals(req.call_count, 1)
Esempio n. 7
0
    def test_validation_fail_required(self):
        """Ensure we fail on missing configuration"""

        # Missing recipients
        with self.assertRaises(AnsibleFailJson):
            self._validate_args(state='enabled', server='localhost', sender='[email protected]')
            Alerts()

        # Missing sender
        with self.assertRaises(AnsibleFailJson):
            self._validate_args(state='enabled', server='localhost', recipients=['[email protected]'])
            Alerts()

        # Missing server
        with self.assertRaises(AnsibleFailJson):
            self._validate_args(state='enabled', sender='[email protected]', recipients=['[email protected]'])
Esempio n. 8
0
    def test_update_configuration(self):
        """Validate updating the configuration"""
        initial = dict(alertingEnabled=True,
                       emailServerAddress='localhost',
                       sendAdditionalContactInformation=True,
                       additionalContactInformation='None',
                       emailSenderAddress='[email protected]',
                       recipientEmailAddresses=['[email protected]'])

        args = dict(state='enabled',
                    server=initial['emailServerAddress'],
                    sender=initial['emailSenderAddress'],
                    contact=initial['additionalContactInformation'],
                    recipients=initial['recipientEmailAddresses'])

        self._set_args(**args)

        alerts = Alerts()

        # Ensure when trigger updates when each relevant field is changed
        with mock.patch(self.REQ_FUNC, return_value=(200, None)) as req:
            with mock.patch.object(alerts,
                                   'get_configuration',
                                   return_value=initial):
                update = alerts.update_configuration()
                self.assertFalse(update)

                alerts.sender = '[email protected]'
                update = alerts.update_configuration()
                self.assertTrue(update)
                self._set_args(**args)

                alerts.recipients = ['[email protected]']
                update = alerts.update_configuration()
                self.assertTrue(update)
                self._set_args(**args)

                alerts.contact = 'abc'
                update = alerts.update_configuration()
                self.assertTrue(update)
                self._set_args(**args)

                alerts.server = 'abc'
                update = alerts.update_configuration()
                self.assertTrue(update)
Esempio n. 9
0
 def _validate_args(self, **kwargs):
     self._set_args(**kwargs)
     Alerts()