Example #1
0
    def test_noopbackend(self, logger_mock):
        """Test _NoOpBackend().
        """
        # pylint: disable=protected-access

        alert_ = dict(type_='test',
                      summary='test summary',
                      instanceid='origin',
                      foo='bar')

        with tempfile.TemporaryDirectory() as alerts_dir:
            on_created = alert_monitor._get_on_create_handler(
                alert_monitor._load_alert_backend(None))

            alert.create(alerts_dir, **alert_)
            alert_file = os.listdir(alerts_dir)[0]

            on_created(os.path.join(alerts_dir, alert_file))

            logger_mock.critical.assert_called_once_with(
                mock.ANY, alert_['type_'], alert_['instanceid'],
                alert_['summary'], {
                    'foo': 'bar',
                    'epoch_ts': mock.ANY
                })

            # check that success callback is invoked and the alert is deleted
            with self.assertRaises(FileNotFoundError):
                alert.read(alert_file, alerts_dir)
Example #2
0
    def test_create_read(self, _):
        """Test alert.create() and alert.read().
        """
        # no epoch_ts defined
        alert_ = dict(type_='test',
                      summary='test summary',
                      instanceid='testorigin',
                      foo='bar')

        with tempfile.TemporaryDirectory() as alerts_dir, \
                mock.patch('time.time', return_value=987.654):
            alert.create(alerts_dir, **alert_)
            alert_file = os.path.join(
                alerts_dir,
                alert._to_filename(alert_['instanceid'], alert_['type_']))

            self.assertTrue(os.path.exists(alert_file))

            # epoch_ts was None
            alert_['epoch_ts'] = 987.654
            self.assertEqual(alert_, alert.read(alert_file))

            # check whether epoch_ts is preserved if passed to create()
            alert_['epoch_ts'] = 1.2
            alert.create(alerts_dir, **alert_)
            self.assertEqual(alert_, alert.read(alert_file))
Example #3
0
    def _on_created(alert_file):
        """Handler for newly created alerts."""

        def _delete_alert_file():
            fs.rm_safe(alert_file)

        alert_ = alert.read(alert_file)
        alert_backend.send_event(
            on_success_callback=_delete_alert_file, **alert_
        )
    def _on_created(alert_file):
        """Handler for newly created alerts."""

        # Avoid triggerring on temporary files
        if os.path.basename(alert_file)[0] == '.':
            return None

        def _delete_alert_file():
            fs.rm_safe(alert_file)

        alert_ = alert.read(alert_file)
        return alert_backend.send_event(
            on_success_callback=_delete_alert_file, **alert_
        )
    def _on_created(alert_file):
        """Handler for newly created alerts.
        """

        # Avoid triggerring on temporary files
        if os.path.basename(alert_file)[0] == '.':
            return None

        try:
            alert_ = alert.read(alert_file)
        except OSError as err:
            if err.errno == errno.ENOENT:
                # File already gone, nothing to do.
                return None
            else:
                raise

        return alert_backend.send_event(
            on_success_callback=lambda: fs.rm_safe(alert_file), **alert_)