def test_update_context_case5_re_none(self): conn_id = "conn_id" conn_name = "conn_name" comp_id = "comp_id" ids = process.gen_ids(create_event(conn_id, conn_name, comp_id)) event = create_event(conn_id, conn_name, comp_id) expected_conn = ContextGraph.create_entity_dict(ids["conn_id"], conn_name, "connector", impact=[ids["comp_id"]]) expected_comp = ContextGraph.create_entity_dict(ids["comp_id"], comp_id, "component", impact=[], depends=[ids["conn_id"]]) comp = ContextGraph.create_entity_dict(comp_id, comp_id, "component", impact=[], depends=[]) res = process.update_context_case5(ids, [comp], event) result_conn, result_comp, result_re = prepare_test_update_context(res) self.assertDictEqual(expected_conn, result_conn) self.assertDictEqual(expected_comp, result_comp)
def update_context_case2(ids, in_db, event): """Case 2 update entities. A component exist in the context but no connector nor resource. :param ids: the tuple of ids. :param type: a tuple :param in_db: a list of the rest of entity in the event. :type in_db: a tuple :return: a list of entities (as a dict)""" LOGGER.debug("Case 2") comp = ContextGraph.create_entity_dict( ids['comp_id'], event['component'], 'component', depends=[ids['re_id']], impact=[]) re = ContextGraph.create_entity_dict( ids['re_id'], event['resource'], 'resource', depends=[], impact=[ids['comp_id']]) update_links_conn_res(in_db[0], re) update_links_conn_comp(in_db[0], comp) return [comp, re, in_db[0]]
def test_info_field(self): conn_id = "conn_id" conn_name = "conn_name" comp_id = "comp_id" re_id = "re_id" event = create_event(conn_id, conn_name, comp_id, re_id) ids = process.gen_ids(event) expected_conn = ContextGraph.create_entity_dict(ids["conn_id"], conn_name, "connector", impact=sorted([ids["comp_id"], ids["re_id"]]), infos={}) expected_comp = ContextGraph.create_entity_dict(ids["comp_id"], comp_id, "component", depends=sorted([ids["conn_id"], ids["re_id"]]), infos={}) expected_re = ContextGraph.create_entity_dict(ids["re_id"], re_id, "resource", impact=[ids["comp_id"]], depends=[ids["conn_id"]], infos={}) process.update_context((False, False, False), ids, [], event) result_re = self.gctx_man.get_entities_by_id(ids["re_id"])[0] result_conn = self.gctx_man.get_entities_by_id(ids["conn_id"])[0] result_comp = self.gctx_man.get_entities_by_id(ids["comp_id"])[0] # A fields links is added in the entity returned by the # get_entities_by_id methods. It is not relievent to this test and his # value fluctuate with specific configuration. So we delete it in the # entity returned by get_entities_by_id del result_comp["links"] del result_conn["links"] del result_re["links"] self.assertEqualEntities(expected_re, result_re) self.assertEqualEntities(expected_conn, result_conn) self.assertEqualEntities(expected_comp, result_comp)
def test__get_disable_entity(self): event = { 'connector': '03-K64_Firefly', 'connector_name': 'serenity', 'component': 'Malcolm_Reynolds', 'output': 'the big red recall button', 'timestamp': int(time.time()) - 100, "source_type": "component" } alarm_id = '/strawberry' alarm = self.manager.make_alarm( alarm_id, event ) context_manager = ContextGraph(logger=LoggerMock()) ent_id = context_manager.get_id(event) entity = context_manager.create_entity_dict(ent_id, "inara", "component") entity["enabled"] = False context_manager._put_entities(entity) alarms = self.reader.get(opened=True) print(alarms) self.assertEqual(len(alarms["alarms"]), 0)
def update_context_case6(ids, in_db, event): """Case 6 update entities. A connector and a resource exist in the context but no component. :param ids: the tuple of ids. :param type: a tuple :param in_db: a list of the rest of entity in the event. :type in_db: a tuple :return: a list of entities (as a dict)""" LOGGER.debug("Update context case 6.") resource = None for entity in in_db: if entity["type"] == "resource": resource = entity elif entity["type"] == "component": component = entity connector = ContextGraph.create_entity_dict( ids["conn_id"], event["connector_name"], "connector", impact=[], depends=[]) update_links_conn_comp(connector, component) if resource is not None: update_links_conn_res(connector, resource) return [connector, component, resource] return [connector, component]
def create_ent_metric(event): """Create a metric entity from an event. :param event: the event to use to create the metric entity :type event: a dict. :return: an event :return type: a dict""" result = [] for perf in event["perf_data_array"]: id_ = "/metric/{0}/{1}/{2}/{3}".format( event["connector"], event["connector_name"], event["component"], perf["metric"]) ent_metric = ContextGraph.create_entity_dict( id=id_, name=perf["metric"], etype="metric", depends=[], impact=[event["resource"]], measurements={}, infos={}, resource=event["resource"], component=event["component"], connector=event["connector"], connector_name=event["connector_name"]) result.append(ent_metric) return result
def create_ent_metric(event): """Create a metric entity from an event. :param event: the event to use to create the metric entity :type event: a dict. :return: an event :return type: a dict""" result = [] for perf in event["perf_data_array"]: id_ = "/metric/{0}/{1}/{2}/{3}".format(event["connector"], event["connector_name"], event["component"], perf["metric"]) ent_metric = ContextGraph.create_entity_dict( id=id_, name=perf["metric"], etype="metric", depends=[], impact=[event["resource"]], measurements={}, infos={}, resource=event["resource"], component=event["component"], connector=event["connector"], connector_name=event["connector_name"]) result.append(ent_metric) return result
def update_context_case3(ids, in_db, event): """Case 3 update entities. A component and connector exist in the context but no resource. :param ids: the tuple of ids. :param type: a tuple :param in_db: a list of the rest of entity in the event. :type in_db: a tuple :return: a list of entities (as a dict)""" LOGGER.debug("Case 3") comp = {} conn = {} for i in in_db: if i['type'] == 'connector': conn = i elif i['type'] == 'component': comp = i re = ContextGraph.create_entity_dict(ids['re_id'], event['resource'], 'resource', depends=[], impact=[]) update_links_res_comp(re, comp) update_links_conn_res(conn, re) return [comp, re, conn]
def update_context_case6(ids, in_db, event): """Case 6 update entities. A connector and a resource exist in the context but no component. :param ids: the tuple of ids. :param type: a tuple :param in_db: a list of the rest of entity in the event. :type in_db: a tuple :return: a list of entities (as a dict)""" LOGGER.debug("Update context case 6.") resource = None for entity in in_db: if entity["type"] == "resource": resource = entity elif entity["type"] == "component": component = entity connector = ContextGraph.create_entity_dict(ids["conn_id"], event["connector_name"], "connector", impact=[], depends=[]) update_links_conn_comp(connector, component) if resource is not None: update_links_conn_res(connector, resource) return [connector, component, resource] return [connector, component]
def test_update_context_case3(self): conn_id = "conn_id" conn_name = "conn_name" comp_id = "comp_id" re_id = "re_id" event = create_event(conn_id, conn_name, comp_id, re_id) ids = process.gen_ids(create_event(conn_id, conn_name, comp_id, re_id)) expected_conn = ContextGraph.create_entity_dict(ids["conn_id"], conn_name, "connector", impact=sorted([ids["comp_id"], ids["re_id"]])) expected_comp = ContextGraph.create_entity_dict(ids["comp_id"], comp_id, "component", depends=sorted([ids["conn_id"], ids["re_id"]])) expected_re = ContextGraph.create_entity_dict(ids["re_id"], re_id, "resource", impact=[ids["comp_id"]], depends=[ids["conn_id"]]) conn = ContextGraph.create_entity_dict("{0}/{1}".format(conn_id, conn_name), conn_name, "connector", impact=[comp_id], depends=[]) comp = ContextGraph.create_entity_dict(comp_id, comp_id, "component", impact=[], depends=["{0}/{1}".format(conn_id, conn_name)]) res = process.update_context_case3(ids, [conn, comp], event) result_conn, result_comp, result_re = prepare_test_update_context(res) self.assertDictEqual(expected_conn, result_conn) self.assertDictEqual(expected_comp, result_comp) self.assertDictEqual(expected_re, result_re)
def test_get_watcher(self): self.assertIsNone(self.manager.get_watcher('watcher-one')) watcher_entity = ContextGraph.create_entity_dict( 'watcher-one', 'one', 'watcher' ) self.context_graph_manager.create_entity(watcher_entity)
def update_context_case1_re_none(ids, event): """Case 1 update entities. No component or connector exist in the context and no resource are define in the event. :param ids: the tuple of ids. :type ids: a tuple :return: a list of entities (as a dict) """ LOGGER.debug("Case 1 re none.") comp = ContextGraph.create_entity_dict(ids['comp_id'], event['component'], 'component', depends=[ids['conn_id']], impact=[]) conn = ContextGraph.create_entity_dict(ids['conn_id'], event['connector_name'], 'connector', depends=[], impact=[ids['comp_id']]) return [comp, conn]
def update_context_case1(ids, event): """Case 1 update entities. No resource, component or connector exist in the context. :param ids: the tuple of ids. :type ids: a tuple :param event: the event to process :type event: a dict. :return: a list of entities (as a dict) """ result = [] LOGGER.debug("Case 1.") comp = ContextGraph.create_entity_dict( ids['comp_id'], event['component'], 'component', depends=[ids['conn_id'], ids['re_id']], impact=[]) conn = ContextGraph.create_entity_dict( ids['conn_id'], event['connector_name'], 'connector', depends=[], impact=[ids['re_id'], ids['comp_id']]) result.append(comp) result.append(conn) if event["event_type"] == "perf": result += create_ent_metric(event) re = ContextGraph.create_entity_dict( ids['re_id'], event['resource'], 'resource', depends=[ids['conn_id']], impact=[ids['comp_id']]) result.append(re) return result
def update_context_case1(ids, event): """Case 1 update entities. No resource, component or connector exist in the context. :param ids: the tuple of ids. :type ids: a tuple :param event: the event to process :type event: a dict. :return: a list of entities (as a dict) """ result = [] LOGGER.debug("Case 1.") comp = ContextGraph.create_entity_dict( ids['comp_id'], event['component'], 'component', depends=[ids['conn_id'], ids['re_id']], impact=[]) conn = ContextGraph.create_entity_dict( ids['conn_id'], event['connector_name'], 'connector', depends=[], impact=[ids['re_id'], ids['comp_id']]) result.append(comp) result.append(conn) if event["event_type"] == "perf": result += create_ent_metric(event) re = ContextGraph.create_entity_dict(ids['re_id'], event['resource'], 'resource', depends=[ids['conn_id']], impact=[ids['comp_id']]) result.append(re) return result
def update_context_case1_re_none(ids, event): """Case 1 update entities. No component or connector exist in the context and no resource are define in the event. :param ids: the tuple of ids. :type ids: a tuple :return: a list of entities (as a dict) """ LOGGER.debug("Case 1 re none.") comp = ContextGraph.create_entity_dict( ids['comp_id'], event['component'], 'component', depends=[ids['conn_id']], impact=[]) conn = ContextGraph.create_entity_dict( ids['conn_id'], event['connector_name'], 'connector', depends=[], impact=[ids['comp_id']]) return [comp, conn]
def test_update_context_case1(self): conn_id = "conn_id" conn_name = "conn_name" comp_id = "comp_id" re_id = "re_id" event = create_event(conn_id, conn_name, comp_id, re_id) ids = process.gen_ids(event) impact = sorted([ids["comp_id"], ids["re_id"]]) expected_conn = ContextGraph.create_entity_dict(ids["conn_id"], conn_name, "connector", impact=impact) depends = sorted([ids["conn_id"], ids["re_id"]]) expected_comp = ContextGraph.create_entity_dict(ids["comp_id"], comp_id, "component", depends=depends) impact = [ids["comp_id"]] depends = [ids["conn_id"]] expected_re = ContextGraph.create_entity_dict(ids["re_id"], re_id, "resource", impact=impact, depends=depends) res = process.update_context_case1(ids, event) result_conn, result_comp, result_re = prepare_test_update_context(res) self.assertDictEqual(expected_comp, result_comp) self.assertDictEqual(expected_conn, result_conn) self.assertDictEqual(expected_re, result_re)
def update_context_case2(ids, in_db, event): """Case 2 update entities. A component exist in the context but no connector nor resource. :param ids: the tuple of ids. :param type: a tuple :param in_db: a list of the rest of entity in the event. :type in_db: a tuple :return: a list of entities (as a dict)""" LOGGER.debug("Case 2") comp = ContextGraph.create_entity_dict(ids['comp_id'], event['component'], 'component', depends=[ids['re_id']], impact=[]) re = ContextGraph.create_entity_dict(ids['re_id'], event['resource'], 'resource', depends=[], impact=[ids['comp_id']]) update_links_conn_res(in_db[0], re) update_links_conn_comp(in_db[0], comp) return [comp, re, in_db[0]]
def create_watcher(self, body): """ Create watcher entity in context and link to entities. :param dict body: watcher conf """ watcher_id = body['_id'] try: watcher_finder = json.loads(body['mfilter']) except ValueError: self.logger.error('can t decode mfilter') return None except KeyError: self.logger.error('no filter') return None depends_list = self.context_graph.get_entities( query=watcher_finder, projection={'_id': 1} ) self.watcher_storage.put_element(body) depend_list = [] for entity_id in depends_list: depend_list.append(entity_id['_id']) entity = ContextGraph.create_entity_dict( id=watcher_id, name=body['display_name'], etype='watcher', impact=[], depends=depend_list ) # adding the fields specific to the Watcher entities entity['mfilter'] = body['mfilter'] entity['state'] = 0 try: self.context_graph.create_entity(entity) except ValueError: self.context_graph.update_entity(entity) self.compute_state(watcher_id) return True # TODO: return really something
def test_create_component(self): id_ = "id_1" name = "name_1" etype = "component" depends = ["id_2", "id_3", "id_4", "id_5"] impacts = ["id_6", "id_7", "id_8", "id_9"] measurements = {"tag_1": "data_1", "tag_2": "data_2"} infos = {"info_1": "foo_1", "info_2": "bar_2"} ent = ContextGraph.create_entity_dict(id_, name, etype, depends, impacts, measurements, infos) self.assertEqual(id_, ent["_id"]) self.assertEqual(name, ent["name"]) self.assertEqual(etype, ent["type"]) self.assertEqual(depends, ent["depends"]) self.assertEqual(impacts, ent["impact"]) self.assertNotIn("measurements", ent.keys()) self.assertEqual(infos, ent["infos"])
def update_context_case3(ids, in_db, event): """Case 3 update entities. A component and connector exist in the context but no resource. :param ids: the tuple of ids. :param type: a tuple :param in_db: a list of the rest of entity in the event. :type in_db: a tuple :return: a list of entities (as a dict)""" LOGGER.debug("Case 3") comp = {} conn = {} for i in in_db: if i['type'] == 'connector': conn = i elif i['type'] == 'component': comp = i re = ContextGraph.create_entity_dict( ids['re_id'], event['resource'], 'resource', depends=[], impact=[]) update_links_res_comp(re, comp) update_links_conn_res(conn, re) return [comp, re, conn]
def setUp(self): super(ComputeState, self).setUp() pbehavior_storage = Middleware.get_middleware_by_uri( 'storage-default-testpbehavior://') filter_storage = Middleware.get_middleware_by_uri( 'storage-default-testalarmfilter://') config_storage = Middleware.get_middleware_by_uri( 'storage-default-testconfig://') config_storage.put_element(element={ '_id': 'test_config', 'crecord_type': 'statusmanagement', 'bagot_time': 3600, 'bagot_freq': 10, 'stealthy_time': 300, 'restore_event': True, 'auto_snooze': False, 'snooze_default_time': 300, }, _id='test_config') logger = Logger.get('test_pb', None, output_cls=OutputNull) config = Configuration.load(PBehaviorManager.CONF_PATH, Ini) self.pbm = PBehaviorManager(config=config, logger=logger, pb_storage=pbehavior_storage) self.pbm.context = self.context_graph_manager self.manager.pbehavior_manager = self.pbm conf = Configuration.load(Alerts.CONF_PATH, Ini) filter_ = {'crecord_type': 'statusmanagement'} config_data = EtherealData(collection=config_storage._backend, filter_=filter_) event_publisher = Mock(spec=StatEventPublisher) self.alert_manager = Alerts(config=conf, logger=logger, alerts_storage=self.alerts_storage, config_data=config_data, filter_storage=filter_storage, context_graph=self.context_graph_manager, watcher=self.manager, event_publisher=event_publisher) # Creating entity self.type_ = 'resource' self.name = 'morticia' entity = ContextGraph.create_entity_dict(id=self.name, etype=self.type_, name=self.name) self.context_graph_manager.create_entity(entity) # Creating coresponding alarm event = { 'connector': self.type_, 'connector_name': 'connector_name', 'component': self.name, 'output': 'tadaTaDA tic tic', 'timestamp': 0 } alarm = self.alert_manager.make_alarm(self.name, event) self.state = 2 alarm = self.alert_manager.update_state(alarm, self.state, event) new_value = alarm[self.alert_manager.alerts_storage.VALUE] self.alert_manager.update_current_alarm(alarm, new_value)
def setUp(self): super(ComputeState, self).setUp() pbehavior_storage = Middleware.get_middleware_by_uri( 'storage-default-testpbehavior://' ) filter_storage = Middleware.get_middleware_by_uri( 'storage-default-testalarmfilter://' ) config_storage = Middleware.get_middleware_by_uri( 'storage-default-testconfig://' ) config_storage.put_element( element={ '_id': 'test_config', 'crecord_type': 'statusmanagement', 'bagot_time': 3600, 'bagot_freq': 10, 'stealthy_time': 300, 'restore_event': True, 'auto_snooze': False, 'snooze_default_time': 300, }, _id='test_config' ) logger = Logger.get('test_pb', None, output_cls=OutputNull) config = Configuration.load(PBehaviorManager.CONF_PATH, Ini) self.pbm = PBehaviorManager(config=config, logger=logger, pb_storage=pbehavior_storage) self.pbm.context = self.context_graph_manager self.manager.pbehavior_manager = self.pbm conf = Configuration.load(Alerts.CONF_PATH, Ini) filter_ = {'crecord_type': 'statusmanagement'} config_data = EtherealData(collection=config_storage._backend, filter_=filter_) event_publisher = Mock(spec=StatEventPublisher) self.alert_manager = Alerts(config=conf, logger=logger, alerts_storage=self.alerts_storage, config_data=config_data, filter_storage=filter_storage, context_graph=self.context_graph_manager, watcher=self.manager, event_publisher=event_publisher) # Creating entity self.type_ = 'resource' self.name = 'morticia' entity = ContextGraph.create_entity_dict( id=self.name, etype=self.type_, name=self.name ) self.context_graph_manager.create_entity(entity) # Creating coresponding alarm event = { 'connector': self.type_, 'connector_name': 'connector_name', 'component': self.name, 'output': 'tadaTaDA tic tic', 'timestamp': 0 } alarm = self.alert_manager.make_alarm(self.name, event) self.state = 2 alarm = self.alert_manager.update_state(alarm, self.state, event) new_value = alarm[self.alert_manager.alerts_storage.VALUE] self.alert_manager.update_current_alarm(alarm, new_value)