From 15754a14ddd8bda6d1777d5c32d1e2570afaf325 Mon Sep 17 00:00:00 2001 From: Dantali0n Date: Wed, 12 Jun 2019 10:25:43 +0200 Subject: [PATCH] Replace removed exceptions and prevent regression Replaces the NoSuchMetric exception that was replaced. The exception is replaced with MetricNotAvailable and test cases are added to prevent regression. The changes in the exceptions were introduced in: https://review.opendev.org/#/c/658127/ Change-Id: Id0f872e916aaa5dec59ed1ae6c0f653553b3fe46 --- watcher/datasources/ceilometer.py | 2 +- watcher/datasources/gnocchi.py | 2 +- watcher/datasources/monasca.py | 2 +- .../datasources/test_ceilometer_helper.py | 24 +++++++++++++++++++ .../tests/datasources/test_gnocchi_helper.py | 23 ++++++++++++++++++ .../tests/datasources/test_monasca_helper.py | 23 ++++++++++++++++++ 6 files changed, 73 insertions(+), 3 deletions(-) diff --git a/watcher/datasources/ceilometer.py b/watcher/datasources/ceilometer.py index b5497aecc..2f4955c5c 100644 --- a/watcher/datasources/ceilometer.py +++ b/watcher/datasources/ceilometer.py @@ -169,7 +169,7 @@ class CeilometerHelper(base.DataSourceBase): meter = self.METRIC_MAP.get(meter_name) if meter is None: - raise exception.NoSuchMetric() + raise exception.MetricNotAvailable(metric=meter_name) if aggregate == 'mean': aggregate = 'avg' diff --git a/watcher/datasources/gnocchi.py b/watcher/datasources/gnocchi.py index c4ee110bf..5fe09f1d7 100644 --- a/watcher/datasources/gnocchi.py +++ b/watcher/datasources/gnocchi.py @@ -86,7 +86,7 @@ class GnocchiHelper(base.DataSourceBase): meter = self.METRIC_MAP.get(meter_name) if meter is None: - raise exception.NoSuchMetric() + raise exception.MetricNotAvailable(metric=meter_name) if aggregate == 'count': aggregate = 'mean' diff --git a/watcher/datasources/monasca.py b/watcher/datasources/monasca.py index 5dbee611e..0d7af11a8 100644 --- a/watcher/datasources/monasca.py +++ b/watcher/datasources/monasca.py @@ -97,7 +97,7 @@ class MonascaHelper(base.DataSourceBase): meter = self.METRIC_MAP.get(meter_name) if meter is None: - raise exception.NoSuchMetric() + raise exception.MetricNotAvailable(metric=meter_name) if aggregate == 'mean': aggregate = 'avg' diff --git a/watcher/tests/datasources/test_ceilometer_helper.py b/watcher/tests/datasources/test_ceilometer_helper.py index 75cbc1b41..9bebbebf8 100644 --- a/watcher/tests/datasources/test_ceilometer_helper.py +++ b/watcher/tests/datasources/test_ceilometer_helper.py @@ -20,6 +20,7 @@ from __future__ import unicode_literals import mock from watcher.common import clients +from watcher.common import exception from watcher.datasources import ceilometer as ceilometer_helper from watcher.tests import base @@ -70,6 +71,29 @@ class TestCeilometerHelper(base.BaseTestCase): ) self.assertEqual(expected_result, val) + def test_statistic_aggregation_metric_unavailable(self, mock_ceilometer): + helper = ceilometer_helper.CeilometerHelper() + + # invalidate instance_cpu_usage in metric map + original_metric_value = helper.METRIC_MAP.get('instance_cpu_usage') + helper.METRIC_MAP.update( + instance_cpu_usage=None + ) + + self.assertRaises( + exception.MetricNotAvailable, + helper.statistic_aggregation, resource=mock.Mock(id="INSTANCE_ID"), + resource_type='instance', meter_name="instance_cpu_usage", + period="7300", + granularity=None + ) + + # restore the metric map as it is a static attribute that does not get + # restored between unit tests! + helper.METRIC_MAP.update( + instance_cpu_usage=original_metric_value + ) + def test_get_host_cpu_usage(self, mock_ceilometer): self.helper.get_host_cpu_usage('compute1', 600, 'mean') self.mock_aggregation.assert_called_once_with( diff --git a/watcher/tests/datasources/test_gnocchi_helper.py b/watcher/tests/datasources/test_gnocchi_helper.py index 370a1cbf3..5ccb0cb13 100644 --- a/watcher/tests/datasources/test_gnocchi_helper.py +++ b/watcher/tests/datasources/test_gnocchi_helper.py @@ -18,6 +18,7 @@ import mock from oslo_config import cfg from watcher.common import clients +from watcher.common import exception from watcher.datasources import gnocchi as gnocchi_helper from watcher.tests import base @@ -57,6 +58,28 @@ class TestGnocchiHelper(base.BaseTestCase): ) self.assertEqual(expected_result, result) + def test_statistic_aggregation_metric_unavailable(self, mock_gnocchi): + helper = gnocchi_helper.GnocchiHelper() + + # invalidate instance_cpu_usage in metric map + original_metric_value = helper.METRIC_MAP.get('instance_cpu_usage') + helper.METRIC_MAP.update( + instance_cpu_usage=None + ) + + self.assertRaises( + exception.MetricNotAvailable, helper.statistic_aggregation, + resource=mock.Mock(id='16a86790-327a-45f9-bc82-45839f062fdc'), + resource_type='instance', meter_name='instance_cpu_usage', + period=300, granularity=360, aggregate='mean', + ) + + # restore the metric map as it is a static attribute that does not get + # restored between unit tests! + helper.METRIC_MAP.update( + instance_cpu_usage=original_metric_value + ) + def test_get_host_cpu_usage(self, mock_gnocchi): self.helper.get_host_cpu_usage('compute1', 600, 'mean', 300) self.mock_aggregation.assert_called_once_with( diff --git a/watcher/tests/datasources/test_monasca_helper.py b/watcher/tests/datasources/test_monasca_helper.py index c10f5db74..77f08db11 100644 --- a/watcher/tests/datasources/test_monasca_helper.py +++ b/watcher/tests/datasources/test_monasca_helper.py @@ -18,6 +18,7 @@ import mock from oslo_config import cfg from watcher.common import clients +from watcher.common import exception from watcher.datasources import monasca as monasca_helper from watcher.tests import base @@ -65,6 +66,28 @@ class TestMonascaHelper(base.BaseTestCase): ) self.assertEqual(0.6, result) + def test_statistic_aggregation_metric_unavailable(self, mock_monasca): + helper = monasca_helper.MonascaHelper() + + # invalidate host_cpu_usage in metric map + original_metric_value = helper.METRIC_MAP.get('host_cpu_usage') + helper.METRIC_MAP.update( + host_cpu_usage=None + ) + + self.assertRaises( + exception.MetricNotAvailable, helper.statistic_aggregation, + resource=mock.Mock(id='NODE_UUID'), resource_type='compute_node', + meter_name='host_cpu_usage', period=7200, granularity=300, + aggregate='mean', + ) + + # restore the metric map as it is a static attribute that does not get + # restored between unit tests! + helper.METRIC_MAP.update( + instance_cpu_usage=original_metric_value + ) + def test_check_availability(self, mock_monasca): monasca = mock.MagicMock() monasca.metrics.list.return_value = True