diff --git a/watcher/datasources/ceilometer.py b/watcher/datasources/ceilometer.py index 913b4aff2..b04608bc9 100644 --- a/watcher/datasources/ceilometer.py +++ b/watcher/datasources/ceilometer.py @@ -164,7 +164,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 89615403d..02a7998a6 100644 --- a/watcher/datasources/gnocchi.py +++ b/watcher/datasources/gnocchi.py @@ -75,7 +75,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 c4818d8c7..a2f5b8917 100644 --- a/watcher/datasources/monasca.py +++ b/watcher/datasources/monasca.py @@ -92,7 +92,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 8e0949299..ea8ea91ac 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