Example #1
0
    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)
Example #2
0
    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 = TemplateLoader().load(template_definition)
        entities = template_data.entities
        relationships = template_data.relationships
        scenarios = template_data.scenarios
        definitions = template_definition[TFields.DEFINITIONS]

        # 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 = definitions[TFields.RELATIONSHIPS]
        self._validate_relationships(relationships, relate_def, entities)
        self._validate_scenarios(scenarios, entities)

        expected_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_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'])
        }

        expected_scenario = Scenario(
            id='basic_template-scenario0',
            version=1,
            condition=[[
                ConditionVar(symbol_name='alarm_on_host', positive=True)
            ]],
            actions=[
                ActionSpecs(
                    id='basic_template-scenario0-action0',
                    type=ActionType.SET_STATE,
                    targets={'target': 'resource'},
                    properties={'state': OperationalResourceState.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)