def notify_usage_exists(instance_ref, current_period=False): """ Generates 'exists' notification for an instance for usage auditing purposes. Generates usage for last completed period, unless 'current_period' is True.""" admin_context = context.get_admin_context() begin, end = utils.current_audit_period() bw = {} if current_period: audit_start = end audit_end = utils.utcnow() else: audit_start = begin audit_end = end for b in db.bw_usage_get_by_instance(admin_context, instance_ref['id'], audit_start): bw[b.network_label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) usage_info = utils.usage_from_instance( instance_ref, audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw) notifier_api.notify('compute.%s' % FLAGS.host, 'compute.instance.exists', notifier_api.INFO, usage_info)
def notify_usage_exists(instance_ref, current_period=False): """ Generates 'exists' notification for an instance for usage auditing purposes. Generates usage for last completed period, unless 'current_period' is True.""" admin_context = context.get_admin_context() begin, end = utils.current_audit_period() bw = {} if current_period: audit_start = end audit_end = utils.utcnow() else: audit_start = begin audit_end = end for b in db.bw_usage_get_by_instance(admin_context, instance_ref['id'], audit_start): bw[b.network_label] = dict(bw_in=b.bw_in, bw_out=b.bw_out) usage_info = utils.usage_from_instance(instance_ref, audit_period_beginning=str(audit_start), audit_period_ending=str(audit_end), bandwidth=bw) notifier_api.notify('compute.%s' % FLAGS.host, 'compute.instance.exists', notifier_api.INFO, usage_info)
def test_when_driver_fails_to_import(self): self.flags(notification_driver='engine.notifier.list_notifier', list_notifier_drivers=['engine.notifier.no_op_notifier', 'engine.notifier.logo_notifier', 'fdsjgsdfhjkhgsfkj']) notify('publisher_id', 'event_type', engine.notifier.api.WARN, dict(a=3)) self.assertEqual(self.exception_count, 2) self.assertEqual(self.notify_count, 1)
def test_send_notifications_with_errors(self): self.flags(notification_driver='engine.notifier.list_notifier', list_notifier_drivers=['engine.notifier.no_op_notifier', 'engine.notifier.log_notifier']) notify('publisher_id', 'event_type', engine.notifier.api.WARN, dict(a=3)) self.assertEqual(self.notify_count, 1) self.assertEqual(self.exception_count, 1)
def test_send_notification(self): self.notify_called = False def mock_notify(cls, *args): self.notify_called = True self.stubs.Set(engine.notifier.no_op_notifier, 'notify', mock_notify) notify('publisher_id', 'event_type', engine.notifier.api.WARN, dict(a=3)) self.assertEqual(self.notify_called, True)
def test_rabbit_priority_queue(self): self.stubs.Set(engine.flags.FLAGS, 'notification_driver', 'engine.notifier.rabbit_notifier') self.stubs.Set(engine.flags.FLAGS, 'notification_topic', 'testnotify') self.test_topic = None def mock_notify(context, topic, msg): self.test_topic = topic self.stubs.Set(engine.rpc, 'notify', mock_notify) notify('publisher_id', 'event_type', 'DEBUG', dict(a=3)) self.assertEqual(self.test_topic, 'testnotify.debug')
def test_send_rabbit_notification(self): self.stubs.Set(engine.flags.FLAGS, 'notification_driver', 'engine.notifier.rabbit_notifier') self.mock_notify = False def mock_notify(cls, *args): self.mock_notify = True self.stubs.Set(engine.rpc, 'notify', mock_notify) notify('publisher_id', 'event_type', engine.notifier.api.WARN, dict(a=3)) self.assertEqual(self.mock_notify, True)
def test_verify_message_format(self): """A test to ensure changing the message format is prohibitively annoying""" def message_assert(message): fields = [('publisher_id', 'publisher_id'), ('event_type', 'event_type'), ('priority', 'WARN'), ('payload', dict(a=3))] for k, v in fields: self.assertEqual(message[k], v) self.assertTrue(len(message['message_id']) > 0) self.assertTrue(len(message['timestamp']) > 0) self.stubs.Set(engine.notifier.no_op_notifier, 'notify', message_assert) notify('publisher_id', 'event_type', engine.notifier.api.WARN, dict(a=3))