def test_standard_template(self):
        template_path = '%s/templates/version/v2/%s' % \
                        (utils.get_resources_dir(), self.STANDARD_TEMPLATE)
        template_definition = file_utils.load_yaml_file(template_path, True)

        template_data = get_template_data(template_definition)
        self.assertIsNotNone(template_data)

        template_type = template_data.template_type
        self.assertIsNotNone(template_type, 'v2 template must include a type')
        self.assertEqual(TemplateTypes.STANDARD, template_type,
                         'template_type should be ' + TemplateTypes.STANDARD)
示例#2
0
 def _get_template_single_action(self, template_file):
     template_path = '%s/templates/version/%s' % (utils.get_resources_dir(),
                                                  template_file)
     template_definition = file_utils.load_yaml_file(template_path, True)
     template_data = get_template_data(template_definition)
     scenarios = template_data.scenarios
     self.assertIsNotNone(scenarios, 'Template should include a scenario')
     self.assertThat(scenarios, matchers.HasLength(1),
                     'Template should include a single scenario')
     actions = scenarios[0].actions
     self.assertIsNotNone(actions, 'Scenario should include an action')
     self.assertThat(actions, matchers.HasLength(1),
                     'Scenario should include a single action')
     return actions[0]
示例#3
0
    def _check_get_condition_common_targets(self, template_name,
                                            valid_targets):
        template_path = CONDITION_TEMPLATES_DIR % (utils.get_resources_dir(),
                                                   template_name)
        template_definition = file_utils.load_yaml_file(template_path, True)

        template_data = get_template_data(template_definition)
        definitions_index = template_data.entities.copy()
        definitions_index.update(template_data.relationships)

        common_targets = get_condition_common_targets(
            template_data.scenarios[0].condition, definitions_index,
            self.ConditionSymbolResolver())

        self.assertIsNotNone(common_targets)
        self.assertTrue(common_targets == set(valid_targets))
示例#4
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_templates_list = file_utils.load_yaml_files(
            def_templates_path)
        def_templates_dict = utils.get_def_templates_dict_from_list(
            def_templates_list)
        template_data = get_template_data(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 = PropsConverter.PROPS_CONVERSION[key] \
                    if key in PropsConverter.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)
示例#5
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 = get_template_data(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 = PropsConverter.PROPS_CONVERSION[key] \
                    if key in PropsConverter.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)
 def _load_scenarios(self, file=None, content=None):
     if file and not content:
         content = self._get_yaml(file)
     return get_template_data(content).scenarios