Exemplo n.º 1
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))
Exemplo n.º 2
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)
Exemplo n.º 3
0
 def send_alert(instance, summary, **kwargs):
     """Send alert."""
     _LOGGER.debug('Sending alert for %s', instance)
     alert.create(alerts_dir,
                  type_='monitor.suspended',
                  instanceid='{}/{}'.format(cell, instance),
                  summary=summary,
                  **kwargs)