From dea32c5e1f4a9d97402c9306ac8157806b761fb4 Mon Sep 17 00:00:00 2001 From: Dantali0n Date: Mon, 20 May 2019 14:41:24 +0200 Subject: [PATCH] Improve Gnocchi and Monasca datasource tests As a follow up to the recent test improvements for Ceilometer this patch ensures that the same test pattern is used for Gnocchi and Monasca as well. This ensures that the mocked functions will be called with matching signatures. Change-Id: Ic14a4c087f3961a4b4f373e2e3d792aba71868f6 --- .../tests/datasources/test_gnocchi_helper.py | 156 +++++++++--------- .../tests/datasources/test_monasca_helper.py | 26 +-- 2 files changed, 91 insertions(+), 91 deletions(-) diff --git a/watcher/tests/datasources/test_gnocchi_helper.py b/watcher/tests/datasources/test_gnocchi_helper.py index 5d5030675..c0ccbfc4d 100644 --- a/watcher/tests/datasources/test_gnocchi_helper.py +++ b/watcher/tests/datasources/test_gnocchi_helper.py @@ -27,6 +27,16 @@ CONF = cfg.CONF @mock.patch.object(clients.OpenStackClients, 'gnocchi') class TestGnocchiHelper(base.BaseTestCase): + def setUp(self): + super(TestGnocchiHelper, self).setUp() + self.osc_mock = mock.Mock() + self.helper = gnocchi_helper.GnocchiHelper(osc=self.osc_mock) + stat_agg_patcher = mock.patch.object( + self.helper, 'statistic_aggregation', + spec=gnocchi_helper.GnocchiHelper.statistic_aggregation) + self.mock_aggregation = stat_agg_patcher.start() + self.addCleanup(stat_agg_patcher.stop) + def test_gnocchi_statistic_aggregation(self, mock_gnocchi): gnocchi = mock.MagicMock() expected_result = 5.5 @@ -48,91 +58,75 @@ class TestGnocchiHelper(base.BaseTestCase): ) self.assertEqual(expected_result, result) - @mock.patch.object(gnocchi_helper.GnocchiHelper, 'statistic_aggregation') - def test_get_host_cpu_usage(self, mock_aggregation, mock_gnocchi): - helper = gnocchi_helper.GnocchiHelper() - helper.get_host_cpu_usage('compute1', 600, 'mean', granularity=300) - mock_aggregation.assert_called_once_with( - 'compute1', helper.METRIC_MAP['host_cpu_usage'], 600, 300, - aggregation='mean') - - @mock.patch.object(gnocchi_helper.GnocchiHelper, 'statistic_aggregation') - def test_get_instance_cpu_usage(self, mock_aggregation, mock_gnocchi): - helper = gnocchi_helper.GnocchiHelper() - helper.get_instance_cpu_usage('compute1', 600, 'mean', granularity=300) - mock_aggregation.assert_called_once_with( - 'compute1', helper.METRIC_MAP['instance_cpu_usage'], 600, 300, - aggregation='mean') - - @mock.patch.object(gnocchi_helper.GnocchiHelper, 'statistic_aggregation') - def test_get_host_memory_usage(self, mock_aggregation, mock_gnocchi): - helper = gnocchi_helper.GnocchiHelper() - helper.get_host_memory_usage('compute1', 600, 'mean', granularity=300) - mock_aggregation.assert_called_once_with( - 'compute1', helper.METRIC_MAP['host_memory_usage'], 600, 300, - aggregation='mean') - - @mock.patch.object(gnocchi_helper.GnocchiHelper, 'statistic_aggregation') - def test_get_instance_memory_usage(self, mock_aggregation, mock_gnocchi): - helper = gnocchi_helper.GnocchiHelper() - helper.get_instance_ram_usage('compute1', 600, 'mean', - granularity=300) - mock_aggregation.assert_called_once_with( - 'compute1', helper.METRIC_MAP['instance_ram_usage'], 600, 300, - aggregation='mean') - - @mock.patch.object(gnocchi_helper.GnocchiHelper, 'statistic_aggregation') - def test_get_instance_ram_allocated(self, mock_aggregation, mock_gnocchi): - helper = gnocchi_helper.GnocchiHelper() - helper.get_instance_ram_allocated('compute1', 600, 'mean', - granularity=300) - mock_aggregation.assert_called_once_with( - 'compute1', helper.METRIC_MAP['instance_ram_allocated'], 600, 300, - aggregation='mean') - - @mock.patch.object(gnocchi_helper.GnocchiHelper, 'statistic_aggregation') - def test_get_instance_root_disk_allocated(self, mock_aggregation, - mock_gnocchi): - helper = gnocchi_helper.GnocchiHelper() - helper.get_instance_root_disk_size('compute1', 600, 'mean', - granularity=300) - mock_aggregation.assert_called_once_with( - 'compute1', helper.METRIC_MAP['instance_root_disk_size'], 600, + def test_get_host_cpu_usage(self, mock_gnocchi): + self.helper.get_host_cpu_usage('compute1', 600, 'mean', + granularity=300) + self.mock_aggregation.assert_called_once_with( + 'compute1', self.helper.METRIC_MAP['host_cpu_usage'], 600, 300, aggregation='mean') - @mock.patch.object(gnocchi_helper.GnocchiHelper, 'statistic_aggregation') - def test_get_host_outlet_temperature(self, mock_aggregation, mock_gnocchi): - helper = gnocchi_helper.GnocchiHelper() - helper.get_host_outlet_temp('compute1', 600, 'mean', - granularity=300) - mock_aggregation.assert_called_once_with( - 'compute1', helper.METRIC_MAP['host_outlet_temp'], 600, 300, - aggregation='mean') + def test_get_instance_cpu_usage(self, mock_gnocchi): + self.helper.get_instance_cpu_usage('compute1', 600, 'mean', + granularity=300) + self.mock_aggregation.assert_called_once_with( + 'compute1', self.helper.METRIC_MAP['instance_cpu_usage'], 600, + 300, aggregation='mean') - @mock.patch.object(gnocchi_helper.GnocchiHelper, 'statistic_aggregation') - def test_get_host_inlet_temperature(self, mock_aggregation, mock_gnocchi): - helper = gnocchi_helper.GnocchiHelper() - helper.get_host_inlet_temp('compute1', 600, 'mean', + def test_get_host_memory_usage(self, mock_gnocchi): + self.helper.get_host_memory_usage('compute1', 600, 'mean', + granularity=300) + self.mock_aggregation.assert_called_once_with( + 'compute1', self.helper.METRIC_MAP['host_memory_usage'], 600, + 300, aggregation='mean') + + def test_get_instance_memory_usage(self, mock_gnocchi): + self.helper.get_instance_ram_usage('compute1', 600, 'mean', + granularity=300) + self.mock_aggregation.assert_called_once_with( + 'compute1', self.helper.METRIC_MAP['instance_ram_usage'], 600, + 300, aggregation='mean') + + def test_get_instance_ram_allocated(self, mock_gnocchi): + self.helper.get_instance_ram_allocated('compute1', 600, 'mean', + granularity=300) + self.mock_aggregation.assert_called_once_with( + 'compute1', self.helper.METRIC_MAP['instance_ram_allocated'], 600, + 300, aggregation='mean') + + def test_get_instance_root_disk_allocated(self, mock_gnocchi): + self.helper.get_instance_root_disk_size('compute1', 600, 'mean', + granularity=300) + self.mock_aggregation.assert_called_once_with( + 'compute1', self.helper.METRIC_MAP['instance_root_disk_size'], 600, + 300, aggregation='mean') + + def test_get_host_outlet_temperature(self, mock_gnocchi): + self.helper.get_host_outlet_temp('compute1', 600, 'mean', + granularity=300) + self.mock_aggregation.assert_called_once_with( + 'compute1', self.helper.METRIC_MAP['host_outlet_temp'], 600, + 300, aggregation='mean') + + def test_get_host_inlet_temperature(self, mock_gnocchi): + self.helper.get_host_inlet_temp('compute1', 600, 'mean', + granularity=300) + self.mock_aggregation.assert_called_once_with( + 'compute1', self.helper.METRIC_MAP['host_inlet_temp'], 600, + 300, aggregation='mean') + + def test_get_host_airflow(self, mock_gnocchi): + self.helper.get_host_airflow('compute1', 600, 'mean', + granularity=300) + self.mock_aggregation.assert_called_once_with( + 'compute1', self.helper.METRIC_MAP['host_airflow'], 600, + 300, aggregation='mean') + + def test_get_host_power(self, mock_gnocchi): + self.helper.get_host_power('compute1', 600, 'mean', granularity=300) - mock_aggregation.assert_called_once_with( - 'compute1', helper.METRIC_MAP['host_inlet_temp'], 600, 300, - aggregation='mean') - - @mock.patch.object(gnocchi_helper.GnocchiHelper, 'statistic_aggregation') - def test_get_host_airflow(self, mock_aggregation, mock_gnocchi): - helper = gnocchi_helper.GnocchiHelper() - helper.get_host_airflow('compute1', 600, 'mean', granularity=300) - mock_aggregation.assert_called_once_with( - 'compute1', helper.METRIC_MAP['host_airflow'], 600, 300, - aggregation='mean') - - @mock.patch.object(gnocchi_helper.GnocchiHelper, 'statistic_aggregation') - def test_get_host_power(self, mock_aggregation, mock_gnocchi): - helper = gnocchi_helper.GnocchiHelper() - helper.get_host_power('compute1', 600, 'mean', granularity=300) - mock_aggregation.assert_called_once_with( - 'compute1', helper.METRIC_MAP['host_power'], 600, 300, - aggregation='mean') + self.mock_aggregation.assert_called_once_with( + 'compute1', self.helper.METRIC_MAP['host_power'], 600, + 300, aggregation='mean') def test_gnocchi_check_availability(self, mock_gnocchi): gnocchi = mock.MagicMock() diff --git a/watcher/tests/datasources/test_monasca_helper.py b/watcher/tests/datasources/test_monasca_helper.py index 840318d0e..0a1547e9f 100644 --- a/watcher/tests/datasources/test_monasca_helper.py +++ b/watcher/tests/datasources/test_monasca_helper.py @@ -27,6 +27,16 @@ CONF = cfg.CONF @mock.patch.object(clients.OpenStackClients, 'monasca') class TestMonascaHelper(base.BaseTestCase): + def setUp(self): + super(TestMonascaHelper, self).setUp() + self.osc_mock = mock.Mock() + self.helper = monasca_helper.MonascaHelper(osc=self.osc_mock) + stat_agg_patcher = mock.patch.object( + self.helper, 'statistic_aggregation', + spec=monasca_helper.MonascaHelper.statistic_aggregation) + self.mock_aggregation = stat_agg_patcher.start() + self.addCleanup(stat_agg_patcher.stop) + def test_monasca_statistic_aggregation(self, mock_monasca): monasca = mock.MagicMock() expected_stat = [{ @@ -113,17 +123,13 @@ class TestMonascaHelper(base.BaseTestCase): val = helper.statistics_list(meter_name="cpu.percent", dimensions={}) self.assertEqual(expected_result, val) - @mock.patch.object(monasca_helper.MonascaHelper, 'statistic_aggregation') - def test_get_host_cpu_usage(self, mock_aggregation, mock_monasca): + def test_get_host_cpu_usage(self, mock_monasca): node = "compute1_compute1" - mock_aggregation.return_value = 0.6 - helper = monasca_helper.MonascaHelper() - cpu_usage = helper.get_host_cpu_usage(node, 600, 'mean') + self.mock_aggregation.return_value = 0.6 + cpu_usage = self.helper.get_host_cpu_usage(node, 600, 'mean') self.assertEqual(0.6, cpu_usage) - @mock.patch.object(monasca_helper.MonascaHelper, 'statistic_aggregation') - def test_get_instance_cpu_usage(self, mock_aggregation, mock_monasca): - mock_aggregation.return_value = 0.6 - helper = monasca_helper.MonascaHelper() - cpu_usage = helper.get_instance_cpu_usage('vm1', 600, 'mean') + def test_get_instance_cpu_usage(self, mock_monasca): + self.mock_aggregation.return_value = 0.6 + cpu_usage = self.helper.get_instance_cpu_usage('vm1', 600, 'mean') self.assertEqual(0.6, cpu_usage)