From aca285b10c8e9246b8762f56b6661784ee449aff Mon Sep 17 00:00:00 2001 From: Muhamad Najjar Date: Tue, 15 Jan 2019 12:58:29 +0000 Subject: [PATCH] Updated prometheus datasource tempest to support working with config file Change-Id: I35fa08e9723a94e9611678efdcbbdb3a2826ff68 --- devstack/pre_test_hook.sh | 4 ++ .../tests/datasources/test_prometheus.py | 69 ++++++++++++++++--- .../resources/prometheus/prometheus_conf.yaml | 12 ++++ 3 files changed, 76 insertions(+), 9 deletions(-) create mode 100644 vitrage_tempest_plugin/tests/resources/prometheus/prometheus_conf.yaml diff --git a/devstack/pre_test_hook.sh b/devstack/pre_test_hook.sh index 933f99f..a0ac80d 100644 --- a/devstack/pre_test_hook.sh +++ b/devstack/pre_test_hook.sh @@ -15,4 +15,8 @@ DEVSTACK_PATH="$BASE/new" +# Copy prometheus_conf.yaml to /etc/vitrage directory +ETC_VITRAGE=/etc/vitrage/ +PROMETHES_CONF_PATH=vitrage-tempest-plugin/vitrage_tempest_plugin/tests/resources/prometheus/prometheus_conf.yaml +sudo mkdir -p $ETC_VITRAGE && sudo cp $DEVSTACK_PATH/$PROMETHES_CONF_PATH $ETC_VITRAGE diff --git a/vitrage_tempest_plugin/tests/datasources/test_prometheus.py b/vitrage_tempest_plugin/tests/datasources/test_prometheus.py index 253cd60..b72fe1c 100644 --- a/vitrage_tempest_plugin/tests/datasources/test_prometheus.py +++ b/vitrage_tempest_plugin/tests/datasources/test_prometheus.py @@ -31,15 +31,17 @@ RESOLVED = 'resolved' class TestPrometheus(BaseTestEvents): """Test class for Prometheus datasource""" - def test_send_prometheus_alert_without_resource_id(self): + def test_send_prometheus_alert_on_host_without_resource_id(self): """Sending an alert in Prometheus format should result in an alarm""" self._test_send_prometheus_alert( - self._create_prometheus_alert_details('host123', FIRING)) + self._create_prometheus_alert_on_host_details('host123', FIRING)) - def test_send_prometheus_alert_without_resource_id_v2(self): + def test_send_prometheus_alert_on_instance_without_resource_id(self): """Sending an alert in Prometheus format should result in an alarm""" self._test_send_prometheus_alert( - self._create_prometheus_alert_details('host457', FIRING)) + self._create_prometheus_alert_on_instance_details('host457', + 'instance457', + FIRING)) def _test_send_prometheus_alert(self, details): try: @@ -52,19 +54,18 @@ class TestPrometheus(BaseTestEvents): # expect to get a 'prometheus.alarm', generated by Prometheus # datasource self.assertThat(api_alarms, matchers.HasLength(1), - 'Expected Prometheus alarm on host') + 'Expected Prometheus alarm') alarm = api_alarms[0] self._check_alarm(alarm, details) event_time = datetime.now() - details['status'] = RESOLVED + 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 on ' - 'host') + self.assertThat(api_alarms, IsEmpty(), 'Expected no alarms') except Exception as e: LOG.exception(e) @@ -77,7 +78,13 @@ class TestPrometheus(BaseTestEvents): self.assertFalse(alarm[VProps.VITRAGE_IS_PLACEHOLDER]) @staticmethod - def _create_prometheus_alert_details(hostname, status): + def _resolve_alerts(details): + details['status'] = RESOLVED + for alert in details['alerts']: + alert['status'] = RESOLVED + + @staticmethod + def _create_prometheus_alert_on_host_details(hostname, status): return { "status": status, "groupLabels": { @@ -130,3 +137,47 @@ class TestPrometheus(BaseTestEvents): "mountpoint": "/" } } + + @staticmethod + def _create_prometheus_alert_on_instance_details(hostname, + instance, + status): + return { + 'status': status, + 'groupLabels': { + 'alertname': 'HighCpuOnVmAlert' + }, + 'groupKey': '{}:{alertname="HighCpuOnVmAlert"}', + 'commonAnnotations': { + 'description': 'Test alert to test libvirt exporter.\n', + 'title': 'High cpu usage on vm' + }, + 'alerts': [ + { + 'status': status, + 'labels': { + 'instance': hostname, + 'domain': instance, + 'job': 'libvirt', + 'alertname': 'HighCpuOnVmAlert', + 'severity': 'critical' + }, + 'endsAt': '2019-01-15T12:29:50.91446215Z', + 'generatorURL': 'http://devstack-rocky-release-4:9090', + 'startsAt': '2019-01-15T12:26:50.91446215Z', + 'annotations': { + 'description': 'Test alert to test libvirt exporter', + 'title': 'High cpu usage on vm' + } + } + ], + 'version': '4', + 'receiver': 'vitrage', + 'externalURL': 'http://devstack-rocky-release-4:9093', + 'commonLabels': { + 'instance': hostname, + 'job': 'libvirt', + 'severity': 'critical', + 'alertname': 'HighCpuOnVmAlert' + } + } diff --git a/vitrage_tempest_plugin/tests/resources/prometheus/prometheus_conf.yaml b/vitrage_tempest_plugin/tests/resources/prometheus/prometheus_conf.yaml new file mode 100644 index 0000000..325f244 --- /dev/null +++ b/vitrage_tempest_plugin/tests/resources/prometheus/prometheus_conf.yaml @@ -0,0 +1,12 @@ +alerts: + - key: + alertname: HighCpuOnVmAlert + job: libvirt + resource: + instance_name: domain + host_id: instance + - key: + alertname: HighInodeUsage + job: node + resource: + id: instance