class StatEventPublisherTest(TestCase): def setUp(self): logger = Mock() self.amqp_pub = Mock() self.event_publisher = StatEventPublisher(logger, self.amqp_pub) def test_no_events(self): self.event_publisher.send_events = False timestamp = 3 self.event_publisher.publish_statcounterinc_event( 1, 'counter_name', {}, {}) self.event_publisher.publish_statduration_event( timestamp, 'duration_name', 2, {}, {}) self.assertEqual(self.amqp_pub.canopsis_event.call_count, 0) def test_publish_statcounterinc_event(self): self.event_publisher.send_events = True self.event_publisher.publish_statcounterinc_event( 1, 'counter_name', {}, {}) self.assertEqual(self.amqp_pub.canopsis_event.call_count, 1) event = self.amqp_pub.canopsis_event.call_args[0][0] self.assertEqual(event[Event.EVENT_TYPE], StatEvents.statcounterinc) self.assertEqual(event[StatEventFields.stat_name], 'counter_name') self.assertEqual(event['timestamp'], 1) def test_publish_statduration_event(self): self.event_publisher.send_events = True timestamp = 3 self.event_publisher.publish_statduration_event( timestamp, 'duration_name', 2, {}, {}) self.assertEqual(self.amqp_pub.canopsis_event.call_count, 1) event = self.amqp_pub.canopsis_event.call_args[0][0] self.assertEqual(event[Event.EVENT_TYPE], StatEvents.statduration) self.assertEqual(event[StatEventFields.stat_name], 'duration_name') self.assertEqual(event['timestamp'], 3) self.assertEqual(event[StatEventFields.duration], 2)
class StatEventPublisherTest(TestCase): def setUp(self): logger = Mock() self.amqp_pub = Mock() self.event_publisher = StatEventPublisher(logger, self.amqp_pub) def test_no_events(self): self.event_publisher.send_events = False timestamp = 3 self.event_publisher.publish_statcounterinc_event( 1, 'counter_name', {}, {}) self.event_publisher.publish_statduration_event( timestamp, 'duration_name', 2, {}, {}) self.assertEqual(self.amqp_pub.canopsis_event.call_count, 0) def test_publish_statcounterinc_event(self): self.event_publisher.send_events = True self.event_publisher.publish_statcounterinc_event( 1, 'counter_name', {}, {}) self.assertEqual(self.amqp_pub.canopsis_event.call_count, 1) event = self.amqp_pub.canopsis_event.call_args[0][0] self.assertEqual(event[Event.EVENT_TYPE], StatEvents.statcounterinc) self.assertEqual(event[StatEventFields.counter_name], 'counter_name') self.assertEqual(event['timestamp'], 1) def test_publish_statduration_event(self): self.event_publisher.send_events = True timestamp = 3 self.event_publisher.publish_statduration_event( timestamp, 'duration_name', 2, {}, {}) self.assertEqual(self.amqp_pub.canopsis_event.call_count, 1) event = self.amqp_pub.canopsis_event.call_args[0][0] self.assertEqual(event[Event.EVENT_TYPE], StatEvents.statduration) self.assertEqual(event[StatEventFields.duration_name], 'duration_name') self.assertEqual(event['timestamp'], 3) self.assertEqual(event[StatEventFields.duration], 2)
def provide_default_basics(cls): """ Provide logger, config, storages... ! Do not use in tests ! :rtype: Union[canopsis.confng.simpleconf.Configuration logging.Logger, canopsis.storage.core.Storage, canopsis.common.ethereal_data.EtherealData, canopsis.storage.core.Storage, canopsis.context_graph.manager.ContextGraph, canopsis.watcher.manager.Watcher] """ config = Configuration.load(Alerts.CONF_PATH, Ini) conf_store = Configuration.load(MongoStore.CONF_PATH, Ini) mongo = MongoStore(config=conf_store) config_collection = MongoCollection( mongo.get_collection(name=cls.CONFIG_COLLECTION)) filter_ = {'crecord_type': 'statusmanagement'} config_data = EtherealData(collection=config_collection, filter_=filter_) logger = Logger.get('alerts', cls.LOG_PATH) alerts_storage = Middleware.get_middleware_by_uri( cls.ALERTS_STORAGE_URI ) filter_storage = Middleware.get_middleware_by_uri( cls.FILTER_STORAGE_URI ) context_manager = ContextGraph(logger) watcher_manager = Watcher() pbehavior_manager = PBehaviorManager(*PBehaviorManager.provide_default_basics()) amqp_pub = AmqpPublisher(get_default_amqp_conn(), logger) event_publisher = StatEventPublisher(logger, amqp_pub) return (config, logger, alerts_storage, config_data, filter_storage, context_manager, watcher_manager, event_publisher, pbehavior_manager)
def setUp(self): logger = Mock() self.amqp_pub = Mock() self.event_publisher = StatEventPublisher(logger, self.amqp_pub)