def test_with_no_match(self): """Test to check that filters with no match do not send event """ try: # Add webhook TempestClients.vitrage().webhook.add( url=self.URL_PROPS, regex_filter=FILTER_NO_MATCH, ) # Raise alarm self._trigger_do_action(TRIGGER_ALARM_1) # Check event not received self.assertThat(self.mock_server.requests, IsEmpty(), 'event should not have passed filter') # Raise another alarm self._trigger_do_action(TRIGGER_ALARM_2) # Check second event not received self.assertThat(self.mock_server.requests, IsEmpty(), 'event should not have passed filter') finally: self._trigger_undo_action(TRIGGER_ALARM_1) self._trigger_undo_action(TRIGGER_ALARM_2)
def test_payload_format(self): try: self.vitrage_client.webhook.add(url=self.URL_PROPS, headers=HEADERS_PROPS) # Raise the trigger alarm self._trigger_do_action(TRIGGER_ALARM_1) # pre check that correct amount of notifications sent self.assertThat(self.mock_server.requests, matchers.HasLength(1), 'Wrong number of notifications for alarm') self.assertThat(messages, matchers.HasLength(1), 'Wrong number of messages for alarm') alarm = ast.literal_eval(messages[0]) # check that only specified fields are sent for the alarm, # payload and resource passed_filter = utils.filter_data(alarm, MAIN_FILTER, match_filter=False) self.assertThat(passed_filter, IsEmpty(), "Wrong main fields sent") payload = alarm.get(PAYLOAD) if payload: passed_filter = utils.filter_data(payload, DOCTOR_ALARM_FILTER, match_filter=False) self.assertThat(passed_filter, IsEmpty(), "Wrong alarm fields sent") sent_fields = utils.filter_data(payload, DOCTOR_ALARM_FILTER) self.assert_set_equal(DOCTOR_ALARM_FILTER, sent_fields, "Some alarm fields not sent") resource = payload.get(VProps.RESOURCE) if resource: passed_filter = utils.filter_data(resource, RESOURCE_FILTER, match_filter=False) self.assertThat(passed_filter, IsEmpty(), "Wrong resource fields sent") sent_fields = utils.filter_data(resource, RESOURCE_FILTER) self.assert_set_equal(RESOURCE_FILTER, sent_fields, "Some resource fields not sent") finally: self._trigger_undo_action(TRIGGER_ALARM_1)
def _test_send_doctor_event(self, details): try: # post an event to the message bus event_time = datetime.now() event_type = 'compute.host.down' self._post_event(event_time, event_type, details) api_alarms = wait_for_answer(2, 0.5, self._check_alarms) # expect to get a 'host down alarm', generated by Doctor datasource self.assertThat(api_alarms, matchers.HasLength(1), 'Expected host down alarm') alarm = api_alarms[0] event_time_tz = six.u(event_time.strftime('%Y-%m-%dT%H:%M:%SZ')) self._check_alarm(alarm, event_time_tz, event_type, details) event_time = datetime.now() details['status'] = 'up' self._post_event(event_time, event_type, details) api_alarms = wait_for_answer(2, 0.5, self._check_alarms) self.assertIsNotNone(api_alarms) self.assertThat(api_alarms, IsEmpty(), 'Expected no alarms on ' 'host') except Exception as e: LOG.exception(e) raise
def _test_send_prometheus_alert(self, details): try: # post an event to the message bus event_time = datetime.now() event_type = 'prometheus.alarm' self._post_event(event_time, event_type, details) api_alarms = wait_for_answer(2, 0.5, self._check_alarms) # expect to get a 'prometheus.alarm', generated by Prometheus # datasource self.assertThat(api_alarms, matchers.HasLength(1), 'Expected Prometheus alarm on host') alarm = api_alarms[0] self._check_alarm(alarm, details) event_time = datetime.now() details['status'] = RESOLVED self._post_event(event_time, event_type, details) api_alarms = wait_for_answer(2, 0.5, self._check_alarms) self.assertIsNotNone(api_alarms, ) self.assertThat(api_alarms, IsEmpty(), 'Expected no alarms on ' 'host') except Exception as e: LOG.exception(e) raise
def test_graph_with_no_match_query(self): """graph_with_no_match_query This test validate correctness of topology graph with no match query """ # Action self._create_entities(num_instances=self.NUM_INSTANCE, num_volumes=self.NUM_VOLUME) # Calculate expected results api_graph = self.vitrage_client.topology.get( query=self._graph_no_match_query(), all_tenants=True) # Test Assertions self.assertThat(api_graph['nodes'], IsEmpty(), 'num of vertex node') self.assertThat(api_graph['links'], IsEmpty(), 'num of edges')
def test_resource_list_with_no_existing_type(self): """resource list with no existing type""" try: instances = nova_utils.create_instances(num_instances=1, set_public_network=True) self.assertThat(instances, IsNotEmpty(), 'The instances list is empty') resources = self.vitrage_client.resource.list( resource_type=CINDER_VOLUME_DATASOURCE, all_tenants=True) self.assertThat(resources, IsEmpty()) except Exception as e: self._handle_exception(e) raise finally: nova_utils.delete_all_instances()
def _test_send_prometheus_alert(self, details): # post an event to the message bus event_time = datetime.now() event_type = 'prometheus.alarm' self._post_event(event_time, event_type, details) api_alarms = wait_for_answer(2, 0.5, self._check_alarms) # expect to get a 'prometheus.alarm', generated by Prometheus # datasource self.assertThat(api_alarms, matchers.HasLength(1), 'Expected Prometheus alarm') alarm = api_alarms[0] self._check_alarm(alarm, details) event_time = datetime.now() self._resolve_alerts(details) self._post_event(event_time, event_type, details) api_alarms = wait_for_answer(2, 0.5, self._check_alarms) self.assertIsNotNone(api_alarms) self.assertThat(api_alarms, IsEmpty(), 'Expected no alarms')
def test_resource_list_with_no_existing_type(self): """resource list with no existing type""" resources = self.vitrage_client.resource.list( resource_type=CINDER_VOLUME_DATASOURCE, all_tenants=True) self.assertThat(resources, IsEmpty())
def test_overlapping_action_add_causal_relationship(self): try: # ---- Do first & second ---- self._trigger_do_action(TRIGGER_ALARM_1) self._trigger_do_action(TRIGGER_ALARM_2) alarms = self.vitrage_client.alarm.list( vitrage_id=self.orig_host.get(VProps.VITRAGE_ID), all_tenants=True) deduced = g_utils.first_match(alarms, **DEDUCED_PROPS) trigger1 = g_utils.first_match(alarms, **TRIGGER_ALARM_1_PROPS) trigger2 = g_utils.first_match(alarms, **TRIGGER_ALARM_2_PROPS) # Get Rca for the deduced rca = self.vitrage_client.rca.get(deduced[VProps.VITRAGE_ID], all_tenants=True) self._check_rca(rca, [deduced, trigger1, trigger2], DEDUCED_PROPS) # Get Rca for trigger 1 rca = self.vitrage_client.rca.get(trigger1[VProps.VITRAGE_ID], all_tenants=True) self._check_rca(rca, [deduced, trigger1], TRIGGER_ALARM_1_PROPS) # Get Rca for trigger 2 rca = self.vitrage_client.rca.get(trigger2[VProps.VITRAGE_ID], all_tenants=True) self._check_rca(rca, [deduced, trigger2], TRIGGER_ALARM_2_PROPS) # ---- Undo - first ---- self._trigger_undo_action(TRIGGER_ALARM_1) alarms = self.vitrage_client.alarm.list( vitrage_id=self.orig_host.get(VProps.VITRAGE_ID), all_tenants=True) deduced = g_utils.first_match(alarms, **DEDUCED_PROPS) trigger2 = g_utils.first_match(alarms, **TRIGGER_ALARM_2_PROPS) # Get Rca for the deduced rca = self.vitrage_client.rca.get(deduced[VProps.VITRAGE_ID], all_tenants=True) self._check_rca(rca, [deduced, trigger2], DEDUCED_PROPS) # Get Rca for trigger 2 rca = self.vitrage_client.rca.get(trigger2[VProps.VITRAGE_ID], all_tenants=True) self._check_rca(rca, [deduced, trigger2], TRIGGER_ALARM_2_PROPS) # ---- Undo - second ---- self._trigger_undo_action(TRIGGER_ALARM_2) alarms = self.vitrage_client.alarm.list( vitrage_id=self.orig_host.get(VProps.VITRAGE_ID), all_tenants=True) self.assertThat( g_utils.all_matches(alarms, **TRIGGER_ALARM_1_PROPS), IsEmpty(), 'trigger alarm 1 should have been removed') self.assertThat( g_utils.all_matches(alarms, **TRIGGER_ALARM_2_PROPS), IsEmpty(), 'trigger alarm 2 should have been removed') self.assertThat( g_utils.all_matches(alarms, **DEDUCED_PROPS), IsEmpty(), 'deduced alarm should have been removed') finally: self._trigger_undo_action(TRIGGER_ALARM_1) self._trigger_undo_action(TRIGGER_ALARM_2)