def setUpClass(cls): cls.target_vertex = Vertex('RESOURCE:nova.host:test1') cls.targets = {TFields.TARGET: cls.target_vertex} cls.props = {TFields.ALARM_NAME: 'VM_CPU_SUBOPTIMAL_PERFORMANCE'} cls.action_spec = ActionSpecs(ActionType.SET_STATE, cls.targets, cls.props)
def test_scenario_0(self): observed_scenarios = self._load_scenarios('valid_actions.yaml') expected_scenario = Scenario( 'valid actions-scenario0', '3', None, [ ActionSpecs('valid actions-scenario0-action0', 'set_state', {'target': 'host'}, {'state': 'ERROR'}), ActionSpecs('valid actions-scenario0-action1', 'raise_alarm', {'target': 'host'}, { 'severity': 'WARNING', 'alarm_name': 'ddd' }), ActionSpecs('valid actions-scenario0-action2', 'mark_down', {'target': 'host'}, {}), ActionSpecs('valid actions-scenario0-action3', 'execute_mistral', {'target': 'host'}, { 'input': { 'farewell': 'get_attr(host, name) bla bla' }, 'workflow': 'wf_1234' }), ], [ NXGraph(vertices=[ Vertex( 'host_ssh_alarm', { 'rawtext': 'host ssh is down', 'vitrage_is_placeholder': False, 'vitrage_type': 'zabbix', 'vitrage_is_deleted': False, }), Vertex( 'host', { 'vitrage_is_placeholder': False, 'vitrage_type': 'nova.host', 'vitrage_is_deleted': False, }) ], edges=[ Edge( 'host_ssh_alarm', 'host', 'on', { 'vitrage_is_deleted': False, 'negative_condition': False }) ]) ], TemplateLoaderV3Test.expected_entities, TemplateLoaderV3Test.expected_relationships) self._assert_scenario_equal(expected_scenario, observed_scenarios[0])
def _get_action_spec(action_spec, match): targets = action_spec.targets real_items = { target: match[target_id] for target, target_id in targets.items() } return ActionSpecs(action_spec.id, action_spec.type, real_items, copy.deepcopy(action_spec.properties))
def setUpClass(cls): cls.target_vertex = Vertex('RESOURCE:nova.host:test1') targets = {TFields.TARGET: cls.target_vertex} cls.props = {TFields.STATE: 'SUBOPTIMAL'} cls.action_spec = ActionSpecs(ActionType.SET_STATE, targets, cls.props)
def test_execute_add_vertex(self): # Test Setup processor = self._create_processor_with_graph(self.conf) vertex_attrs = {VProps.TYPE: NOVA_HOST_DATASOURCE} host_vertices = processor.entity_graph.get_vertices( vertex_attr_filter=vertex_attrs) host = host_vertices[0] targets = {TFields.TARGET: host} props = { TFields.ALARM_NAME: 'VM_CPU_SUBOPTIMAL_PERFORMANCE', TFields.SEVERITY: 'CRITICAL', VProps.STATE: AlarmProps.ACTIVE_STATE } # Raise alarm action adds new vertex with type vitrage to the graph action_spec = ActionSpecs(ActionType.RAISE_ALARM, targets, props) alarm_vertex_attrs = {VProps.TYPE: VITRAGE_TYPE} before_alarms = processor.entity_graph.get_vertices( vertex_attr_filter=alarm_vertex_attrs) event_queue = queue.Queue() action_executor = ActionExecutor(event_queue) expected_alarm_id = 'ALARM:vitrage:%s:%s' % (props[TFields.ALARM_NAME], host.vertex_id) # Test Action action_executor.execute(action_spec, ActionMode.DO) processor.process_event(event_queue.get()) after_alarms = processor.entity_graph.get_vertices( vertex_attr_filter=alarm_vertex_attrs) # Assertions self.assertEqual(len(before_alarms) + 1, len(after_alarms)) self.assert_is_not_empty(after_alarms) alarms = [ alarm for alarm in after_alarms if alarm.vertex_id == expected_alarm_id ] # Expected exactly one alarm with expected id self.assertEqual(1, len(alarms)) alarm = alarms[0] self.assertEqual(alarm.properties[VProps.CATEGORY], EntityCategory.ALARM) self.assertEqual(alarm.properties[VProps.TYPE], VITRAGE_TYPE) self.assertEqual(alarm.properties[VProps.SEVERITY], props[TFields.SEVERITY]) self.assertEqual(alarm.properties[VProps.OPERATIONAL_SEVERITY], props[TFields.SEVERITY]) self.assertEqual(alarm.properties[VProps.STATE], AlarmProps.ACTIVE_STATE)
def test_execute_add_vertex(self): # Test Setup processor = self._create_processor_with_graph() vertex_attrs = {VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE} host_vertices = processor.entity_graph.get_vertices( vertex_attr_filter=vertex_attrs) host = host_vertices[0] targets = {TFields.TARGET: host} props = { TFields.ALARM_NAME: 'VM_CPU_SUBOPTIMAL_PERFORMANCE', TFields.SEVERITY: OperationalAlarmSeverity.CRITICAL, VProps.STATE: AlarmProps.ACTIVE_STATE, VProps.RESOURCE_ID: host[VProps.ID], VProps.VITRAGE_ID: 'DUMMY_ID' } # Raise alarm action adds new vertex with type vitrage to the graph action_spec = ActionSpecs(0, ActionType.RAISE_ALARM, targets, props) alarm_vertex_attrs = {VProps.VITRAGE_TYPE: VITRAGE_DATASOURCE} before_alarms = processor.entity_graph.get_vertices( vertex_attr_filter=alarm_vertex_attrs) event_queue, action_executor = self._init_executer() # Test Action action_executor.execute( [ActionInfo(action_spec, ActionMode.DO, None, None)]) self._consume_queue(event_queue, processor) after_alarms = processor.entity_graph.get_vertices( vertex_attr_filter=alarm_vertex_attrs) # Assertions self.assertEqual(len(before_alarms) + 1, len(after_alarms)) self.assert_is_not_empty(after_alarms) alarm = after_alarms[0] self.assertEqual(alarm.properties[VProps.VITRAGE_CATEGORY], EntityCategory.ALARM) self.assertEqual(alarm.properties[VProps.VITRAGE_TYPE], VITRAGE_DATASOURCE) self.assertEqual(alarm.properties[VProps.SEVERITY], props[TFields.SEVERITY]) self.assertEqual(alarm.properties[VProps.VITRAGE_OPERATIONAL_SEVERITY], props[TFields.SEVERITY]) self.assertEqual(alarm.properties[VProps.STATE], AlarmProps.ACTIVE_STATE) self.assertEqual( alarm.properties[VProps.VITRAGE_RESOURCE_ID], action_spec.targets[TTFields.TARGET][VProps.VITRAGE_ID]), self.assertEqual(alarm.properties[VProps.VITRAGE_RESOURCE_TYPE], NOVA_HOST_DATASOURCE)
def test_scenario_1(self): observed_scenarios = self._load_scenarios('valid_actions.yaml') expected_scenario = Scenario( 'valid actions-scenario1', '3', None, [ ActionSpecs( 'valid actions-scenario1-action0', 'add_causal_relationship', { 'target': 'host_ssh_alarm', 'source': 'host_network_alarm', }, {}), ], [ NXGraph( vertices=[ Vertex('host_ssh_alarm', { 'rawtext': 'host ssh is down', 'vitrage_is_placeholder': False, 'vitrage_type': 'zabbix', 'vitrage_is_deleted': False, }), Vertex('host_network_alarm', { 'rawtext': 'host network interface is down', 'vitrage_is_placeholder': False, 'vitrage_type': 'zabbix', 'vitrage_is_deleted': False, }), Vertex('host', { 'vitrage_is_placeholder': False, 'vitrage_type': 'nova.host', 'vitrage_is_deleted': False, }) ], edges=[ Edge('host_ssh_alarm', 'host', 'on', { 'vitrage_is_deleted': False, 'negative_condition': False }), Edge('host_network_alarm', 'host', 'on', { 'vitrage_is_deleted': False, 'negative_condition': False }) ]) ], TemplateLoaderV3Test.expected_entities, TemplateLoaderV3Test.expected_relationships) self._assert_scenario_equal( expected_scenario, observed_scenarios[1])
def setUpClass(cls): super(RaiseAlarmRecipeTest, cls).setUpClass() cls.props = {EXECUTION_ENGINE: MISTRAL, WORKFLOW: 'wf_4', 'host': 'host5', 'state': 'ok'} cls.action_spec = ActionSpecs(0, ActionType.EXECUTE_MISTRAL, {}, cls.props)
def setUpClass(cls): super(RaiseAlarmRecipeTest, cls).setUpClass() cls.target_props = {VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE} cls.target_vertex = Vertex('RESOURCE:nova.host:test1', cls.target_props) cls.targets = {TFields.TARGET: cls.target_vertex} cls.props = {TFields.ALARM_NAME: 'VM_CPU_SUBOPTIMAL_PERFORMANCE'} cls.action_spec = ActionSpecs( 0, ActionType.SET_STATE, cls.targets, cls.props)
def setUpClass(cls): cls.props = { EXECUTION_ENGINE: MISTRAL, WORKFLOW: 'wf_4', 'host': 'host5', 'state': 'ok' } cls.action_spec = ActionSpecs(ActionType.EXECUTE_MISTRAL, {}, cls.props)
def setUpClass(cls): super(SetStateRecipeTest, cls).setUpClass() cls.target_vertex = Vertex('RESOURCE:nova.host:test1') targets = {TFields.TARGET: cls.target_vertex} cls.props = {TFields.STATE: OperationalResourceState.SUBOPTIMAL} cls.action_spec = ActionSpecs(0, ActionType.SET_STATE, targets, cls.props)
def test_scenario_5(self): observed_scenarios = self._load_scenarios('valid_actions.yaml') expected_scenario = Scenario( 'valid actions-scenario5', '3', None, [ ActionSpecs( 'valid actions-scenario5-action0', 'mark_down', {'target': 'host'}, {}), ], [ NXGraph( vertices=[ Vertex('host_ssh_alarm', { 'rawtext': 'host ssh is down', 'vitrage_is_placeholder': False, 'vitrage_type': 'zabbix', 'vitrage_is_deleted': False, }), Vertex('instance', { 'vitrage_is_placeholder': False, 'vitrage_type': 'nova.instance', 'vitrage_is_deleted': False, }), Vertex('host', { 'vitrage_is_placeholder': False, 'vitrage_type': 'nova.host', 'vitrage_is_deleted': False, }), ], edges=[ Edge('host_ssh_alarm', 'host', 'on', { 'vitrage_is_deleted': True, 'negative_condition': True }), Edge('host', 'instance', 'contains', { 'vitrage_is_deleted': True, 'negative_condition': True }) ] ), ], TemplateLoaderV3Test.expected_entities, TemplateLoaderV3Test.expected_relationships) self._assert_scenario_equal( expected_scenario, observed_scenarios[5])
def _get_action_spec(action_spec, match): targets = action_spec.targets real_items = { target: match[target_id] for target, target_id in targets.items() } revised_spec = ActionSpecs(action_spec.type, real_items, action_spec.properties) # noinspection PyTypeChecker action_id = ScenarioEvaluator._generate_action_id(revised_spec) return revised_spec, action_id
def setUpClass(cls): super(AddCausalRelationshipTest, cls).setUpClass() cls.target_vertex = Vertex('RESOURCE:nova.host:test_target') cls.source_vertex = Vertex('RESOURCE:nova.host:test_source') targets = { TField.TARGET: cls.target_vertex, TField.SOURCE: cls.source_vertex } cls.action_spec = ActionSpecs(0, ActionType.ADD_CAUSAL_RELATIONSHIP, targets, {})
def setUpClass(cls): cls.target_vertex = Vertex('RESOURCE:nova.host:test_target') cls.source_vertex = Vertex('RESOURCE:nova.host:test_source') targets = { TField.TARGET: cls.target_vertex, TField.SOURCE: cls.source_vertex } cls.action_spec = ActionSpecs(ActionType.ADD_CAUSAL_RELATIONSHIP, targets, {})
def test_execute_set_state(self): # Test Setup processor = self._create_processor_with_graph() vertex_attrs = {VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE} host_vertices = processor.entity_graph.get_vertices( vertex_attr_filter=vertex_attrs) host_vertex_before = host_vertices[0] targets = {TFields.TARGET: host_vertex_before} props = {TFields.STATE: OperationalResourceState.SUBOPTIMAL} action_spec = ActionSpecs(0, ActionType.SET_STATE, targets, props) event_queue, action_executor = self._init_executer() # Test Action - do action_executor.execute( [ActionInfo(action_spec, ActionMode.DO, None, None)]) self._consume_queue(event_queue, processor) host_vertex_after = processor.entity_graph.get_vertex( host_vertex_before.vertex_id) # Test Assertions agg_state_before = \ host_vertex_before.get(VProps.VITRAGE_AGGREGATED_STATE) self.assertNotEqual(agg_state_before, OperationalResourceState.SUBOPTIMAL) self.assertNotIn(VProps.VITRAGE_STATE, host_vertex_before.properties) agg_state_after = \ host_vertex_after.get(VProps.VITRAGE_AGGREGATED_STATE) self.assertEqual(agg_state_after, OperationalResourceState.SUBOPTIMAL) v_state_after = host_vertex_after.get(VProps.VITRAGE_STATE) self.assertEqual(v_state_after, OperationalResourceState.SUBOPTIMAL) # Test Action - undo action_executor.execute( [ActionInfo(action_spec, ActionMode.UNDO, None, None)]) self._consume_queue(event_queue, processor) host_vertex_after_undo = processor.entity_graph.get_vertex( host_vertex_before.vertex_id) # Test Assertions agg_state_after_undo = \ host_vertex_before.get(VProps.VITRAGE_AGGREGATED_STATE) self.assertEqual(agg_state_after_undo, agg_state_before) self.assertNotIn(VProps.VITRAGE_STATE, host_vertex_after_undo.properties)
def _build_actions(self, actions_def, scenario_id): actions = [] for counter, action_def in enumerate(actions_def): action_id = '%s-action%s' % (scenario_id, str(counter)) action_dict = action_def[TFields.ACTION] action_type = action_dict[TFields.ACTION_TYPE] targets = action_dict.get(TFields.ACTION_TARGET, self.valid_target) properties = action_dict.get(TFields.PROPERTIES, {}) actions.append( ActionSpecs(action_id, action_type, targets, properties)) return actions
def load(self, action_id, default_target, action_dict, action_type): """V3 template action to ActionSpecs transformation :param action_id: Unique action identifier :param default_target: Is taken from the condition, it is used when the action doesn't define a target :param action_dict: Action section taken from the template. :param action_type: example: set_state/raise_alarm/etc.. :rtype: ActionSpecs """ target = action_dict.pop(TField.TARGET, default_target[TField.TARGET]) targets = {TField.TARGET: target} if action_dict.get(TField.SOURCE): targets[TField.SOURCE] = action_dict.pop(TField.SOURCE) return ActionSpecs(action_id, action_type, targets, action_dict)
def test_execute_add_edge(self): # Test Setup processor = self._create_processor_with_graph() vertex_attrs = {VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE} host_vertices = processor.entity_graph.get_vertices( vertex_attr_filter=vertex_attrs) host_1 = host_vertices[0] nagios_event1 = TestActionExecutor._get_nagios_event( host_1.get(VProps.ID), NOVA_HOST_DATASOURCE) processor.process_event(nagios_event1) host_2 = host_vertices[1] nagios_event2 = TestActionExecutor._get_nagios_event( host_2.get(VProps.ID), NOVA_HOST_DATASOURCE) processor.process_event(nagios_event2) alarms_attrs = {VProps.VITRAGE_TYPE: NAGIOS_DATASOURCE} alarms_vertices = processor.entity_graph.get_vertices( vertex_attr_filter=alarms_attrs) alarm1 = alarms_vertices[0] alarm2 = alarms_vertices[1] targets = { TFields.TARGET: alarm1, TFields.SOURCE: alarm2 } action_spec = ActionSpecs( 0, ActionType.ADD_CAUSAL_RELATIONSHIP, targets, {}) event_queue, action_executor = self._init_executer() before_edge = processor.entity_graph.get_edge(alarm2.vertex_id, alarm1.vertex_id, EdgeLabel.CAUSES) # Test Action - do action_executor.execute( [ActionInfo(action_spec, ActionMode.DO, None, None)]) self._consume_queue(event_queue, processor) new_edge = processor.entity_graph.get_edge(alarm2.vertex_id, alarm1.vertex_id, EdgeLabel.CAUSES) # Test Assertions self.assertIsNone(before_edge) self.assertIsNotNone(new_edge)
def test_execute_update_vertex(self): # Test Setup processor = self._create_processor_with_graph(self.conf) vertex_attrs = {VProps.TYPE: NOVA_HOST_DATASOURCE} host_vertices = processor.entity_graph.get_vertices( vertex_attr_filter=vertex_attrs) host_vertex_before = host_vertices[0] targets = {TFields.TARGET: host_vertex_before} props = {TFields.STATE: OperationalResourceState.SUBOPTIMAL} action_spec = ActionSpecs(ActionType.SET_STATE, targets, props) event_queue = queue.Queue() action_executor = ActionExecutor(event_queue) # Test Action - do action_executor.execute(action_spec, ActionMode.DO) processor.process_event(event_queue.get()) host_vertex_after = processor.entity_graph.get_vertex( host_vertex_before.vertex_id) # Test Assertions agg_state_before = host_vertex_before.get(VProps.AGGREGATED_STATE) self.assertTrue( agg_state_before != OperationalResourceState.SUBOPTIMAL) self.assertFalse(VProps.VITRAGE_STATE in host_vertex_before.properties) agg_state_after = host_vertex_after.get(VProps.AGGREGATED_STATE) self.assertEqual(agg_state_after, OperationalResourceState.SUBOPTIMAL) v_state_after = host_vertex_after.get(VProps.VITRAGE_STATE) self.assertEqual(v_state_after, OperationalResourceState.SUBOPTIMAL) # Test Action - undo action_executor.execute(action_spec, ActionMode.UNDO) processor.process_event(event_queue.get()) host_vertex_after_undo = processor.entity_graph.get_vertex( host_vertex_before.vertex_id) # Test Assertions agg_state_after_undo = host_vertex_before.get(VProps.AGGREGATED_STATE) self.assertEqual(agg_state_after_undo, agg_state_before) self.assertTrue( VProps.VITRAGE_STATE not in host_vertex_after_undo.properties)
def test_execute_add_and_remove_vertex(self): # Test Setup processor = self._create_processor_with_graph(self.conf) vertex_attrs = {VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE} host_vertices = processor.entity_graph.get_vertices( vertex_attr_filter=vertex_attrs) host = host_vertices[0] targets = {TFields.TARGET: host} props = { TFields.ALARM_NAME: 'VM_CPU_SUBOPTIMAL_PERFORMANCE', TFields.SEVERITY: OperationalAlarmSeverity.CRITICAL, VProps.STATE: AlarmProps.ACTIVE_STATE, VProps.RESOURCE_ID: host[VProps.ID] } action_spec = ActionSpecs(ActionType.RAISE_ALARM, targets, props) add_vertex_event = TestActionExecutor._get_vitrage_add_vertex_event( host, props[TFields.ALARM_NAME], props[TFields.SEVERITY]) processor.process_event(add_vertex_event) alarm_vertex_attrs = { VProps.VITRAGE_TYPE: VITRAGE_DATASOURCE, VProps.VITRAGE_IS_DELETED: False } before_alarms = processor.entity_graph.get_vertices( vertex_attr_filter=alarm_vertex_attrs) event_queue = queue.Queue() action_executor = ActionExecutor(self.conf, event_queue) # Test Action - undo action_executor.execute(action_spec, ActionMode.UNDO) event = event_queue.get() processor.process_event(event) after_alarms = processor.entity_graph.get_vertices( vertex_attr_filter=alarm_vertex_attrs) # Test Assertions self.assertEqual(len(before_alarms) - 1, len(after_alarms))
def test_scenario_4(self): observed_scenarios = self._load_scenarios('valid_actions.yaml') expected_scenario = Scenario( 'valid actions-scenario4', '3', None, [ ActionSpecs('valid actions-scenario4-action0', 'mark_down', {'target': 'host'}, {}), ], [ NXGraph(vertices=[ Vertex( 'host', { 'vitrage_is_placeholder': False, 'vitrage_type': 'nova.host', 'vitrage_is_deleted': False, }), ]), ], TemplateLoaderV3Test.expected_entities, TemplateLoaderV3Test.expected_relationships) self._assert_scenario_equal(expected_scenario, observed_scenarios[4])
def _db_action_to_action_info(self, db_action): target = self._entity_graph.get_vertex(db_action.target_vertex_id) targets = {TARGET: target} if db_action.source_vertex_id: source = self._entity_graph.get_vertex(db_action.source_vertex_id) targets[SOURCE] = source scenario_action = self._scenario_repo.actions.get(db_action.action_id) properties = copy.copy(scenario_action.properties) action_specs = ActionSpecs( id=db_action.action_id, type=db_action.action_type, targets=targets, properties=properties, ) action_info = ActionInfo( specs=action_specs, mode=ActionMode.DO, action_id=db_action.action_id, trigger_id=db_action.trigger, ) return action_info
def test_execute_mark_down(self): # Test Setup processor = self._create_processor_with_graph() vertex_attrs = {VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE} host_vertices = processor.entity_graph.get_vertices( vertex_attr_filter=vertex_attrs) host_vertex_before = host_vertices[0] targets = {TFields.TARGET: host_vertex_before} props = {} action_spec = ActionSpecs(0, ActionType.MARK_DOWN, targets, props) event_queue, action_executor = self._init_executer() # Test Action - do action_executor.execute( [ActionInfo(action_spec, ActionMode.DO, None, None)]) self._consume_queue(event_queue, processor) host_vertex_after = processor.entity_graph.get_vertex( host_vertex_before.vertex_id) # Test Assertions self.assertTrue(host_vertex_after.get(VProps.IS_MARKED_DOWN)) # Test Action - undo action_executor.execute( [ActionInfo(action_spec, ActionMode.UNDO, None, None)]) self._consume_queue(event_queue, processor) host_vertex_after_undo = processor.entity_graph.get_vertex( host_vertex_before.vertex_id) # Test Assertions self.assertFalse(host_vertex_after_undo.get(VProps.IS_MARKED_DOWN))
def test_execute_mark_down(self): # Test Setup processor = self._create_processor_with_graph(self.conf) vertex_attrs = {VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE} host_vertices = processor.entity_graph.get_vertices( vertex_attr_filter=vertex_attrs) host_vertex_before = host_vertices[0] targets = {TFields.TARGET: host_vertex_before} props = {} action_spec = ActionSpecs(ActionType.MARK_DOWN, targets, props) event_queue = queue.Queue() action_executor = ActionExecutor(self.conf, event_queue) # Test Action - do action_executor.execute(action_spec, ActionMode.DO) processor.process_event(event_queue.get()) host_vertex_after = processor.entity_graph.get_vertex( host_vertex_before.vertex_id) # Test Assertions self.assertTrue(host_vertex_after.get(VProps.IS_MARKED_DOWN)) # Test Action - undo action_executor.execute(action_spec, ActionMode.UNDO) processor.process_event(event_queue.get()) host_vertex_after_undo = processor.entity_graph.get_vertex( host_vertex_before.vertex_id) # Test Assertions self.assertFalse(host_vertex_after_undo.get(VProps.IS_MARKED_DOWN))
def setUpClass(cls): super(MarkDownRecipeTest, cls).setUpClass() cls.target_vertex = Vertex('RESOURCE:nova.host:test1') targets = {TFields.TARGET: cls.target_vertex} cls.action_spec = ActionSpecs(0, ActionType.MARK_DOWN, targets, None)
def setUpClass(cls): cls.target_vertex = Vertex('RESOURCE:nova.host:test1') targets = {TFields.TARGET: cls.target_vertex} cls.action_spec = ActionSpecs(ActionType.MARK_DOWN, targets, None)
def test_basic_template(self): # Test setup template_path = '%s/templates/general/%s' % (utils.get_resources_dir(), self.BASIC_TEMPLATE) template_definition = file_utils.load_yaml_file(template_path, True) template_data = TemplateData(template_definition) entities = template_data.entities relationships = template_data.relationships scenarios = template_data.scenarios definitions = template_definition[TFields.DEFINITIONS] # Assertions entities_definition = definitions[TFields.ENTITIES] self._validate_entities(entities, entities_definition) relate_def = definitions[TFields.RELATIONSHIPS] self._validate_relationships(relationships, relate_def, entities) self._validate_scenarios(scenarios, entities) expected_entities = { 'alarm': Vertex(vertex_id='alarm', properties={ 'category': 'ALARM', 'type': 'nagios', 'name': 'host_problem' }), 'resource': Vertex(vertex_id='resource', properties={ 'category': 'RESOURCE', 'type': 'nova.host' }) } expected_relationships = { 'alarm_on_host': EdgeDescription(edge=Edge(source_id='alarm', target_id='resource', label='on', properties={'relationship_type': 'on'}), source=expected_entities['alarm'], target=expected_entities['resource']) } expected_scenario = Scenario( id='basic_template-scenario0', condition=[[ ConditionVar(symbol_name='alarm_on_host', positive=True) ]], actions=[ ActionSpecs(type='set_state', targets={'target': 'resource'}, properties={'state': 'SUBOPTIMAL'}) ], # TODO(yujunz): verify the built subgraph is consistent with # scenario definition. For now the observed value is # assigned to make test passing subgraphs=template_data.scenarios[0].subgraphs, entities=expected_entities, relationships=expected_relationships) self._validate_strict_equal(template_data, expected_entities, expected_relationships, expected_scenario)
def load(self, action_id, default_target, action_def): action_dict = action_def[TFields.ACTION] action_type = action_dict[TFields.ACTION_TYPE] targets = action_dict.get(TFields.ACTION_TARGET, default_target) return ActionSpecs(action_id, action_type, targets, self._get_properties(action_dict))
def test_basic_template_with_include(self): # Test setup template_path = self.DEF_TEMPLATE_TESTS_DIR +\ '/templates/%s' % self.BASIC_TEMPLATE_WITH_INCLUDE template_definition = file_utils.load_yaml_file(template_path, True) def_templates_path = self.DEF_TEMPLATE_TESTS_DIR + \ '/definition_templates' def_demplates_list = file_utils.load_yaml_files(def_templates_path) def_templates_dict = utils.get_def_templates_dict_from_list( def_demplates_list) template_data = \ TemplateLoader().load(template_definition, def_templates_dict) entities = template_data.entities relationships = template_data.relationships scenarios = template_data.scenarios definitions = template_definition[TFields.DEFINITIONS] def_template = file_utils.load_yaml_file(def_templates_path + '/basic_def_template.yaml') def_template_entities = \ def_template[TFields.DEFINITIONS][TFields.ENTITIES] def_template_relationships = \ def_template[TFields.DEFINITIONS][TFields.RELATIONSHIPS] definitions[TFields.ENTITIES] += def_template_entities definitions[TFields.RELATIONSHIPS] = def_template_relationships # Assertions for definition in definitions[TFields.ENTITIES]: for key, value in definition['entity'].items(): new_key = TemplateLoader.PROPS_CONVERSION[key] if key in \ TemplateLoader.PROPS_CONVERSION else key del definition['entity'][key] definition['entity'][new_key] = value self._validate_entities(entities, definitions[TFields.ENTITIES]) relate_def = def_template_relationships self._validate_relationships(relationships, relate_def, entities) self._validate_scenarios(scenarios, entities) expected_entities = { 'alarm11': Vertex(vertex_id='alarm11', properties={ VProps.VITRAGE_CATEGORY: EntityCategory.ALARM, VProps.VITRAGE_TYPE: NAGIOS_DATASOURCE, VProps.NAME: 'host_problem' }), 'resource11': Vertex(vertex_id='resource11', properties={ VProps.VITRAGE_CATEGORY: EntityCategory.RESOURCE, VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE }), 'alarm': Vertex(vertex_id='alarm', properties={ VProps.VITRAGE_CATEGORY: EntityCategory.ALARM, VProps.VITRAGE_TYPE: NAGIOS_DATASOURCE, VProps.NAME: 'host_problem' }), 'resource': Vertex(vertex_id='resource', properties={ VProps.VITRAGE_CATEGORY: EntityCategory.RESOURCE, VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE }) } expected_relationships = { 'alarm_on_host': EdgeDescription(edge=Edge( source_id='alarm', target_id='resource', label=EdgeLabel.ON, properties={EdgeProperties.RELATIONSHIP_TYPE: EdgeLabel.ON}), source=expected_entities['alarm'], target=expected_entities['resource']), } scenario_entities = { 'alarm': Vertex(vertex_id='alarm', properties={ VProps.VITRAGE_CATEGORY: EntityCategory.ALARM, VProps.VITRAGE_TYPE: NAGIOS_DATASOURCE, VProps.NAME: 'host_problem' }), 'resource': Vertex(vertex_id='resource', properties={ VProps.VITRAGE_CATEGORY: EntityCategory.RESOURCE, VProps.VITRAGE_TYPE: NOVA_HOST_DATASOURCE }) } expected_scenario = Scenario( id='basic_template_with_include-scenario0', version=1, condition=[[ ConditionVar(symbol_name='alarm_on_host', positive=True) ]], actions=[ ActionSpecs( id='basic_template_with_include-scenario0-action0', type=ActionType.SET_STATE, targets={'target': 'resource'}, properties={'state': OperationalResourceState.SUBOPTIMAL}) ], subgraphs=template_data.scenarios[0].subgraphs, entities=scenario_entities, relationships=expected_relationships) self._validate_strict_equal(template_data, expected_entities, expected_relationships, expected_scenario)