def _validate_template_without_params(self, template_filename): # Setup apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) files_content = self._load_template_content(template_filename) # Action results = apis.validate_template(ctx=None, templates=files_content, template_type=None, params=None) # Test assertions self._assert_validate_template_result( self.VALIDATION_OK, 0, 'Template validation is OK', results)
def _validate_template_with_missing_param_def(self, template_filename): # Setup apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) files_content = self._load_template_content(template_filename) params = {'alarm_type': 'zabbix', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action results = apis.validate_template(ctx=None, templates=files_content, template_type=None, params=params) # Test assertions self._assert_validate_template_result( self.VALIDATION_FAILED, 161, 'get_param called for a parameter ' 'that is not defined in the \'parameters\' block', results)
def _validate_template_with_actual_params(self, template_filename): # Setup apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) files_content = self._load_template_content(template_filename) params = {'template_name': 'template_with_params_2', 'alarm_type': 'zabbix', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action results = apis.validate_template(ctx=None, templates=files_content, template_type=None, params=params) # Test assertions self._assert_validate_template_result( self.VALIDATION_OK, 0, 'Template validation is OK', results)
def start(self): LOG.info("Vitrage Api Handler Service - Starting...") super(VitrageApiHandlerService, self).start() transport = messaging.get_rpc_transport(self.conf) rabbit_hosts = self.conf.oslo_messaging_rabbit.rabbit_hosts target = oslo_messaging.Target(topic=self.conf.rpc_topic, server=rabbit_hosts) endpoints = [ TopologyApis(self.entity_graph, self.conf), AlarmApis(self.entity_graph, self.conf), RcaApis(self.entity_graph, self.conf), TemplateApis(self.scenario_repo.templates, self.scenario_repo.def_templates), EventApis(self.conf), ResourceApis(self.entity_graph, self.conf) ] server = vitrage_rpc.get_server(target, endpoints, transport) server.start() LOG.info("Vitrage Api Handler Service - Started!")
def _init_instance(self): conf = self._conf LOG.info("Vitrage Api Handler Service - Starting...") notifier = messaging.VitrageNotifier(conf, "vitrage.api", [EVALUATOR_TOPIC]) db = storage.get_connection_from_config(conf) transport = messaging.get_rpc_transport(conf) rabbit_hosts = conf.oslo_messaging_rabbit.rabbit_hosts target = oslo_messaging.Target(topic=conf.rpc_topic, server=rabbit_hosts) endpoints = [ TopologyApis(self._entity_graph, conf), AlarmApis(self._entity_graph, conf, db), RcaApis(self._entity_graph, conf, db), TemplateApis(notifier, db), EventApis(conf), ResourceApis(self._entity_graph, conf), WebhookApis(conf) ] server = vitrage_rpc.get_server(target, endpoints, transport) server.start() LOG.info("Vitrage Api Handler Service - Started!")
def _validate_template_with_missing_param(self, template_filename): # Setup apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) files_content = self._load_template_content(template_filename) params = {'template_name': 'template_with_params_1', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action results = apis.validate_template(ctx=None, templates=files_content, template_type=None, params=params) # Test assertions self._assert_validate_template_result( self.VALIDATION_FAILED, 163, 'Failed to resolve parameter', results)
def _start(self): LOG.info("Vitrage Api Handler Service - Starting...") transport = messaging.get_rpc_transport(self.conf) rabbit_hosts = self.conf.oslo_messaging_rabbit.rabbit_hosts target = oslo_messaging.Target(topic=self.conf.rpc_topic, server=rabbit_hosts) endpoints = [TopologyApis(self.entity_graph, self.conf), AlarmApis(self.entity_graph, self.conf), RcaApis(self.entity_graph, self.conf), TemplateApis(self.notifier, self.db), EventApis(self.conf), ResourceApis(self.entity_graph, self.conf), WebhookApis(self.conf)] server = vitrage_rpc.get_server(target, endpoints, transport) server.start() LOG.info("Vitrage Api Handler Service - Started!")
def _init_instance(self): notifier = messaging.VitrageNotifier("vitrage.api", [EVALUATOR_TOPIC]) db = storage.get_connection_from_config() transport = messaging.get_rpc_transport() target = oslo_messaging.Target(topic=CONF.rpc_topic, server=uuidutils.generate_uuid()) self.api_lock = threading.RLock() endpoints = [ TopologyApis(self._entity_graph, self.api_lock), AlarmApis(self._entity_graph, self.api_lock, db), RcaApis(self._entity_graph, self.api_lock, db), ResourceApis(self._entity_graph, self.api_lock), TemplateApis(notifier, db), EventApis(), WebhookApis(db), OperationalApis(self._entity_graph), ] server = vitrage_rpc.get_server(target, endpoints, transport) server.start()
def setUp(self): super(TemplateValidatorV3Test, self).setUp() self.add_db() self.template_apis = TemplateApis(db=self._db)
class TemplateValidatorV3Test(base.BaseTest, TestConfiguration): def setUp(self): super(TemplateValidatorV3Test, self).setUp() self.add_db() self.template_apis = TemplateApis(db=self._db) def _test_validation(self, file=None, content=None, expected_code=0): if file and not content: content = self._get_yaml(file) self._call_validate_api('/tmp/tmp', content, expected_code) @staticmethod def _get_yaml(filename): path = '%s/templates/v3_templates/%s' % (get_resources_dir(), filename) return file_utils.load_yaml_file(path) def _call_validate_api(self, path, content, expected_error_code): templates = [[ path, content, ]] results = self.template_apis.validate_template(None, templates, None) result = json.loads(results)['results'][0] self.assertEqual(expected_error_code, result['status code'], message='GOT ' + result['message']) def test_actions(self): template = self._get_yaml('valid_actions.yaml') self._test_validation(content=template, expected_code=0) del template[TF.SCENARIOS][0][TF.CONDITION] self._test_validation(content=template, expected_code=83) del template[TF.SCENARIOS][0] del template[TF.SCENARIOS][0][TF.ACTIONS] self._test_validation(content=template, expected_code=84) def test_set_state(self): template = self._get_yaml('valid_actions.yaml') valid_action = { 'set_state': { TF.TARGET: 'host', TF.STATE: 'BAD', } } template[TF.SCENARIOS][0][TF.ACTIONS].append(valid_action) self._test_validation(content=template, expected_code=0) valid_action['set_state'] = { TF.TARGET: 'host_incorrect_key', TF.STATE: 'BAD', } self._test_validation(content=template, expected_code=10101) valid_action['set_state'] = { TF.STATE: 'BAD', } self._test_validation(content=template, expected_code=10100) valid_action['set_state'] = { TF.TARGET: 'host', TF.STATE: 'BAD', 'kuku': 'kuku', } self._test_validation(content=template, expected_code=4) valid_action['set_state'] = { TF.TARGET: 'host', } self._test_validation(content=template, expected_code=128) def test_mark_down(self): template = self._get_yaml('valid_actions.yaml') valid_action = { 'mark_down': { TF.TARGET: 'host', } } template[TF.SCENARIOS][0][TF.ACTIONS].append(valid_action) self._test_validation(content=template, expected_code=0) valid_action['mark_down'] = { TF.TARGET: 'host_incorrect_key', } self._test_validation(content=template, expected_code=10101) valid_action['mark_down'] = {} self._test_validation(content=template, expected_code=10100) valid_action['mark_down'] = { TF.TARGET: 'host', 'kuku': 'kuku', } self._test_validation(content=template, expected_code=4) def test_raise_alarm(self): self._test_validation(file='valid_actions.yaml', expected_code=0) template = self._get_yaml('valid_actions.yaml') valid_action = { 'raise_alarm': { TF.TARGET: 'host', TF.ALARM_NAME: 'BAD', TF.SEVERITY: 'BAD', } } template[TF.SCENARIOS][0][TF.ACTIONS].append(valid_action) self._test_validation(content=template, expected_code=0) valid_action['raise_alarm'] = { TF.TARGET: 'host_incorrect_key', TF.ALARM_NAME: 'BAD', TF.SEVERITY: 'BAD', } self._test_validation(content=template, expected_code=10101) valid_action['raise_alarm'] = { TF.TARGET: 'host', TF.ALARM_NAME: 'BAD', TF.SEVERITY: 'BAD', 'kuku': 'kuku', } self._test_validation(content=template, expected_code=4) valid_action['raise_alarm'] = { TF.ALARM_NAME: 'BAD', TF.SEVERITY: 'BAD', } self._test_validation(content=template, expected_code=10100) valid_action['raise_alarm'] = { TF.TARGET: 'host', TF.SEVERITY: 'BAD', } self._test_validation(content=template, expected_code=10104) valid_action['raise_alarm'] = { TF.TARGET: 'host', TF.ALARM_NAME: 'BAD', } self._test_validation(content=template, expected_code=126) valid_action['raise_alarm'] = { TF.TARGET: 'host', TF.ALARM_NAME: 'BAD', TF.SEVERITY: 'BAD', TF.CAUSING_ALARM: 'host_ssh_alarm' } self._test_validation(content=template, expected_code=0) valid_action['raise_alarm'] = { TF.TARGET: 'host', TF.ALARM_NAME: 'BAD', TF.SEVERITY: 'BAD', TF.CAUSING_ALARM: 'host_ssh_alarm_incorrect_key' } self._test_validation(content=template, expected_code=10107) def test_add_causal_relationship(self): self._test_validation(file='valid_actions.yaml', expected_code=0) template = self._get_yaml('valid_actions.yaml') valid_action = { 'add_causal_relationship': { TF.TARGET: 'host_ssh_alarm', TF.SOURCE: 'host_network_alarm', } } template[TF.SCENARIOS][0][TF.ACTIONS].append(valid_action) self._test_validation(content=template, expected_code=0) valid_action['add_causal_relationship'] = { TF.TARGET: 'host_ssh_alarm_incorrect_key', TF.SOURCE: 'host_network_alarm', } self._test_validation(content=template, expected_code=10101) valid_action['add_causal_relationship'] = { TF.TARGET: 'host_ssh_alarm', TF.SOURCE: 'host_network_alarm_incorrect_key', } self._test_validation(content=template, expected_code=10103) valid_action['add_causal_relationship'] = { TF.SOURCE: 'host_network_alarm', } self._test_validation(content=template, expected_code=10100) valid_action['add_causal_relationship'] = { TF.TARGET: 'host_ssh_alarm', } self._test_validation(content=template, expected_code=10102) def test_execute_mistral(self): template = self._get_yaml('valid_actions.yaml') valid_action = { 'execute_mistral': { execute_mistral.WORKFLOW: 'kuku', execute_mistral.INPUT: {}, } } template[TF.SCENARIOS][0][TF.ACTIONS].append(valid_action) self._test_validation(content=template, expected_code=0) valid_action['execute_mistral'] = { execute_mistral.WORKFLOW: {}, execute_mistral.INPUT: {}, } self._test_validation(content=template, expected_code=4) valid_action['execute_mistral'] = {execute_mistral.INPUT: {}} self._test_validation(content=template, expected_code=10105) valid_action['execute_mistral'] = {execute_mistral.WORKFLOW: 'kuku'} self._test_validation(content=template, expected_code=0) valid_action['execute_mistral'] = { execute_mistral.WORKFLOW: 'kuku', execute_mistral.INPUT: { 'kuku': 'get_attr(' } } self._test_validation(content=template, expected_code=138) valid_action['execute_mistral'] = { execute_mistral.WORKFLOW: 'kuku', execute_mistral.INPUT: { 'kuku': 'get_attr(host, name)' }, } self._test_validation(content=template, expected_code=0) def test_conditions(self): self._test_validation(file='valid_conditions.yaml', expected_code=0) template = self._get_yaml('valid_conditions.yaml') template[TF.SCENARIOS][0][TF.CONDITION] = \ 'host AND NOT host host [contains] instance' self._test_validation(content=template, expected_code=85) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'host AND instance host [contains] instance' self._test_validation(content=template, expected_code=85) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'host AND NOT host [host contains] instance' self._test_validation(content=template, expected_code=85) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'host AND instance host [contains] instance' self._test_validation(content=template, expected_code=85) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'NOT host [contains] instance' self._test_validation(content=template, expected_code=134) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'NOT host [contains] instance AND NOT host [contains] instance' self._test_validation(content=template, expected_code=134) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'NOT (host [ contains ] instance or host [ contains ] instance)' self._test_validation(content=template, expected_code=134) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'NOT host_incorrect_key' self._test_validation(content=template, expected_code=10200) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'host_incorrect_key' self._test_validation(content=template, expected_code=10200) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'host [contains] instance_incorrect_key' self._test_validation(content=template, expected_code=10200) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'host_incorrect_key [contains] instance' self._test_validation(content=template, expected_code=10200) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'NOT host' self._test_validation(content=template, expected_code=86) template[TF.SCENARIOS][0][TF.CONDITION] = \ '(host_incorrect_key)' self._test_validation(content=template, expected_code=10200) template[TF.SCENARIOS][0][TF.CONDITION] = \ '( host OR host [contains] instance ) AND' \ ' (instance OR host [contains] instance' # missing parentheses self._test_validation(content=template, expected_code=85) template[TF.SCENARIOS][0][TF.CONDITION] = 'host OR instance' self._test_validation(content=template, expected_code=135) template[TF.SCENARIOS][0][TF.CONDITION] = \ 'host OR NOT host [contains] instance' self._test_validation(content=template, expected_code=135) def test_regex(self): template = self._get_yaml('valid_actions.yaml') template[TF.ENTITIES]['entity_regex'] = {'name.regex': 'bad.regex('} self._test_validation(content=template, expected_code=47) def test_basic(self): template = self._get_yaml('valid_conditions.yaml') del template[TF.SCENARIOS] self._test_validation(content=template, expected_code=80) template = self._get_yaml('valid_conditions.yaml') del template[TF.ENTITIES] self._test_validation(content=template, expected_code=10101) template = self._get_yaml('valid_conditions.yaml') template['kuku'] = {} self._test_validation(content=template, expected_code=4)
def setUpClass(cls): super(TemplateValidatorV3Test, cls).setUpClass() cls.conf = cfg.ConfigOpts() cls.add_db(cls.conf) cls.template_apis = TemplateApis(db=cls._db)
def setUp(self): super(TestTemplates, self).setUp() self.add_db() self.apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) self.added_template = None
class TestTemplates(TestEntityGraphUnitBase, TestConfiguration): VALIDATION_FAILED = 'validation failed' VALIDATION_OK = 'validation OK' class MockNotifier(object): def notify(self, event_type, data): pass def setUp(self): super(TestTemplates, self).setUp() self.add_db() self.apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) self.added_template = None def tearDown(self): super(TestTemplates, self).tearDown() self._delete_templates() def _load_template_content(self, template_filename): template_path = '%s/templates/parameters/%s' % ( utils.get_resources_dir(), template_filename) return [(template_path, self._load_yaml_file(template_path))] def _validate_template_with_no_params(self, template_filename): files_content = self._load_template_content(template_filename) # Action results = self.apis.validate_template( ctx=None, templates=files_content, template_type=None, params=None) # Test assertions self._assert_validate_template_result( self.VALIDATION_FAILED, 163, 'Failed to resolve parameter', results) def _validate_template_with_missing_param(self, template_filename): # Setup apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) files_content = self._load_template_content(template_filename) params = {'template_name': 'template_with_params_1', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action results = apis.validate_template(ctx=None, templates=files_content, template_type=None, params=params) # Test assertions self._assert_validate_template_result( self.VALIDATION_FAILED, 163, 'Failed to resolve parameter', results) def _validate_template_with_actual_params(self, template_filename): # Setup apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) files_content = self._load_template_content(template_filename) params = {'template_name': 'template_with_params_2', 'alarm_type': 'zabbix', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action results = apis.validate_template(ctx=None, templates=files_content, template_type=None, params=params) # Test assertions self._assert_validate_template_result( self.VALIDATION_OK, 0, 'Template validation is OK', results) def _validate_template_with_missing_param_def(self, template_filename): # Setup apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) files_content = self._load_template_content(template_filename) params = {'alarm_type': 'zabbix', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action results = apis.validate_template(ctx=None, templates=files_content, template_type=None, params=params) # Test assertions self._assert_validate_template_result( self.VALIDATION_FAILED, 161, 'get_param called for a parameter ' 'that is not defined in the \'parameters\' block', results) def _validate_template_without_params(self, template_filename): # Setup apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) files_content = self._load_template_content(template_filename) # Action results = apis.validate_template(ctx=None, templates=files_content, template_type=None, params=None) # Test assertions self._assert_validate_template_result( self.VALIDATION_OK, 0, 'Template validation is OK', results) def _validate_template_with_extra_actual_param(self, template_filename): # Setup apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) files_content = self._load_template_content(template_filename) params = {'template_name': 'template_with_params_2', 'alarm_type': 'zabbix', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL', 'non_existing_param': 'some value'} # Action results = apis.validate_template(ctx=None, templates=files_content, template_type=None, params=params) # Test assertions self._assert_validate_template_result( self.VALIDATION_OK, 0, 'Template validation is OK', results) def _validate_template_with_extra_param_def(self, template_filename): # Setup apis = TemplateApis(notifier=self.MockNotifier(), db=self._db) files_content = self._load_template_content(template_filename) params = {'template_name': 'template_with_params_2', 'alarm_type': 'zabbix', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action results = apis.validate_template(ctx=None, templates=files_content, template_type=None, params=params) # Test assertions self._assert_validate_template_result( self.VALIDATION_OK, 0, 'Template validation is OK', results) def _add_template_with_no_params(self, template_filename): # Setup files_content = self._load_template_content(template_filename) # Action. added_templates = \ self.apis.add_template(ctx=None, templates=files_content, template_type=None, params=None) self.added_template = added_templates[0]['uuid'] # Test assertions self.assertThat(added_templates, matchers.HasLength(1)) self.assertEqual('ERROR', added_templates[0]['status']) self.assert_starts_with('Failed to resolve parameter', added_templates[0]['status details']) def _add_template_with_missing_param(self, template_filename): # Setup files_content = self._load_template_content(template_filename) params = {'template_name': 'template_with_params_3', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action added_templates = \ self.apis.add_template(ctx=None, templates=files_content, template_type=None, params=params) self.added_template = added_templates[0]['uuid'] # Test assertions self.assertThat(added_templates, matchers.HasLength(1)) self.assertEqual('ERROR', added_templates[0]['status']) self.assert_starts_with('Failed to resolve parameter', added_templates[0]['status details']) def _add_template_with_actual_params(self, template_filename): # Setup files_content = self._load_template_content(template_filename) params = {'template_name': 'template_with_params_4', 'alarm_type': 'zabbix', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action added_templates = \ self.apis.add_template(ctx=None, templates=files_content, template_type=None, params=params) self.added_template = added_templates[0]['uuid'] # Test assertions self.assertThat(added_templates, matchers.HasLength(1)) self.assertEqual('LOADING', added_templates[0]['status']) def _add_template_with_missing_param_def(self, template_filename): # Setup files_content = self._load_template_content(template_filename) params = {'alarm_type': 'zabbix', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action added_templates = \ self.apis.add_template(ctx=None, templates=files_content, template_type=None, params=params) self.added_template = added_templates[0]['uuid'] # Test assertions self.assertEqual('ERROR', added_templates[0]['status']) self.assert_starts_with('get_param called for a parameter that is not ' 'defined in the \'parameters\' block', added_templates[0]['status details']) def _add_template_without_params(self, template_filename): # Setup files_content = self._load_template_content(template_filename) # Action added_templates = \ self.apis.add_template(ctx=None, templates=files_content, template_type=None, params=None) self.added_template = added_templates[0]['uuid'] # Test assertions self.assertThat(added_templates, matchers.HasLength(1)) self.assertEqual('LOADING', added_templates[0]['status']) def _add_template_with_extra_actual_param(self, template_filename): # Setup files_content = self._load_template_content(template_filename) params = {'template_name': 'template_with_extra_actual_param', 'alarm_type': 'zabbix', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL', 'non_existing_param': 'some value'} # Action added_templates = \ self.apis.add_template(ctx=None, templates=files_content, template_type=None, params=params) self.added_template = added_templates[0]['uuid'] # Test assertions self.assertThat(added_templates, matchers.HasLength(1)) self.assertEqual('LOADING', added_templates[0]['status']) def _add_template_with_extra_param_def(self, template_filename): # Setup files_content = self._load_template_content(template_filename) params = {'template_name': 'template_with_extra_param_def', 'alarm_type': 'zabbix', 'alarm_name': 'My alarm', 'new_state': 'SUBOPTIMAL'} # Action added_templates = \ self.apis.add_template(ctx=None, templates=files_content, template_type=None, params=params) self.added_template = added_templates[0]['uuid'] # Test assertions self.assertThat(added_templates, matchers.HasLength(1)) self.assertEqual('LOADING', added_templates[0]['status']) def _assert_validate_template_result(self, expected_status, expected_status_code, expected_message, results): self.assertIsNotNone(results) results = json.loads(results) results = results['results'] self.assertIsNotNone(results) self.assertThat(results, matchers.HasLength(1)) self.assertEqual(expected_status, results[0]['status']) self.assertEqual(expected_status_code, results[0]['status code']) self.assert_starts_with(expected_message, results[0]['message']) def _delete_templates(self): if self.added_template: self.apis.delete_template(ctx=None, uuids=[self.added_template])
def setUpClass(cls): super(TestTemplates, cls).setUpClass() cls.conf = cfg.ConfigOpts() cls.add_db(cls.conf) cls.apis = TemplateApis(notifier=cls.MockNotifier(), db=cls._db) cls.added_template = None
def test_get_versions(self): with mock.patch('pecan.request') as request: versions = TemplateApis().template_versions(mock.Mock()) request.client.call.return_value = versions resp = self.get_json('/template/versions/') self.assert_list_equal(VERSIONS, resp)