def test_gnocchi_check_availability(self, mock_gnocchi):
     gnocchi = mock.MagicMock()
     gnocchi.status.get.return_value = True
     mock_gnocchi.return_value = gnocchi
     helper = gnocchi_helper.GnocchiHelper()
     result = helper.check_availability()
     self.assertEqual('available', result)
 def test_gnocchi_list_metrics_with_failure(self, mock_gnocchi):
     cfg.CONF.set_override("query_max_retries",
                           1,
                           group='watcher_datasources')
     gnocchi = mock.MagicMock()
     gnocchi.metric.list.side_effect = Exception()
     mock_gnocchi.return_value = gnocchi
     helper = gnocchi_helper.GnocchiHelper()
     self.assertFalse(helper.list_metrics())
 def test_gnocchi_list_metrics(self, mock_gnocchi):
     gnocchi = mock.MagicMock()
     metrics = [{"name": "metric1"}, {"name": "metric2"}]
     expected_metrics = set(["metric1", "metric2"])
     gnocchi.metric.list.return_value = metrics
     mock_gnocchi.return_value = gnocchi
     helper = gnocchi_helper.GnocchiHelper()
     result = helper.list_metrics()
     self.assertEqual(expected_metrics, result)
 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_check_availability_with_failure(self, mock_gnocchi):
        cfg.CONF.set_override("query_max_retries",
                              1,
                              group='watcher_datasources')
        gnocchi = mock.MagicMock()
        gnocchi.status.get.side_effect = Exception()
        mock_gnocchi.return_value = gnocchi
        helper = gnocchi_helper.GnocchiHelper()

        self.assertEqual('not available', helper.check_availability())
    def test_gnocchi_statistic_aggregation(self, mock_gnocchi):
        gnocchi = mock.MagicMock()
        expected_result = 5.5

        expected_measures = [["2017-02-02T09:00:00.000000", 360, 5.5]]

        gnocchi.metric.get_measures.return_value = expected_measures
        mock_gnocchi.return_value = gnocchi

        helper = gnocchi_helper.GnocchiHelper()
        result = 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',
        )
        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)
Beispiel #8
0
 def gnocchi(self):
     if self._gnocchi is None:
         self._gnocchi = gnoc.GnocchiHelper(osc=self.osc)
     return self._gnocchi