예제 #1
0
    def test_configured_error(self):
        """Butcher the required configuration and test if configured is false

        """

        self.m_conf.grafana_client.base_url = ""
        t_grafana = grafana.GrafanaHelper(osc=mock.Mock())
        self.assertFalse(t_grafana.configured)
예제 #2
0
    def test_no_metric_raise_error(self):
        """Test raising error when specified meter does not exist"""

        t_grafana = grafana.GrafanaHelper(osc=mock.Mock())

        self.assertRaises(exception.MetricNotAvailable,
                          t_grafana.statistic_aggregation, self.m_compute_node,
                          'none existing meter', 60)
예제 #3
0
    def test_metric_builder(self):
        """Creates valid and invalid sets of configuration for metrics

        Ensures that a valid metric entry can be configured even if multiple
        invalid configurations exist for other metrics.
        """

        self.m_conf.grafana_client.project_id_map = {
            'host_cpu_usage': 7221,
            'host_ram_usage': 7221,
            'instance_ram_allocated': 7221,
        }
        self.m_conf.grafana_client.database_map = {
            'host_cpu_usage': 'mock_db',
            'instance_cpu_usage': 'mock_db',
            'instance_ram_allocated': 'mock_db',
        }
        self.m_conf.grafana_client.attribute_map = {
            'host_cpu_usage': 'hostname',
            'host_power': 'hostname',
            'instance_ram_allocated': 'human_id',
        }
        self.m_conf.grafana_client.translator_map = {
            'host_cpu_usage': 'influxdb',
            'host_inlet_temp': 'influxdb',
            # validate that invalid entries don't get added
            'instance_ram_usage': 'dummy',
            'instance_ram_allocated': 'influxdb',
        }
        self.m_conf.grafana_client.query_map = {
            'host_cpu_usage': 'SHOW SERIES',
            'instance_ram_usage': 'SHOW SERIES',
            'instance_ram_allocated': 'SHOW SERIES',
        }

        expected_result = {
            'host_cpu_usage': {
                'db': 'mock_db',
                'project': 7221,
                'attribute': 'hostname',
                'translator': 'influxdb',
                'query': 'SHOW SERIES'
            },
            'instance_ram_allocated': {
                'db': 'mock_db',
                'project': 7221,
                'attribute': 'human_id',
                'translator': 'influxdb',
                'query': 'SHOW SERIES'
            },
        }

        t_grafana = grafana.GrafanaHelper(osc=mock.Mock())
        self.assertEqual(t_grafana.METRIC_MAP, expected_result)
예제 #4
0
    def test_statistic_aggregation(self, m_request):
        m_request.return_value.content = "{ \"results\": [{ \"series\": [{ " \
                                         "\"columns\": [\"time\",\"mean\"]," \
                                         "\"values\": [[1552500855000, " \
                                         "67.3550078657577]]}]}]}"

        t_grafana = grafana.GrafanaHelper(osc=mock.Mock())

        result = t_grafana.statistic_aggregation(self.m_compute_node,
                                                 'compute_node',
                                                 'host_cpu_usage', 60)
        self.assertEqual(result, 67.3550078657577)
예제 #5
0
    def test_get_metric_raise_error(self, m_request):
        """Test raising error when endpoint unable to deliver data for metric

        """

        m_request.return_value.content = "{}"

        t_grafana = grafana.GrafanaHelper(osc=mock.Mock())

        self.assertRaises(exception.NoSuchMetricForHost,
                          t_grafana.get_host_cpu_usage, self.m_compute_node,
                          60)
예제 #6
0
    def test_request_raise_error(self, m_request):
        """Test raising error when status code of request indicates problem

        Assure that the _request method raises errors if the response indicates
        problems.
        """

        m_request.return_value = mock.Mock(status_code=404)

        t_grafana = grafana.GrafanaHelper(osc=mock.Mock())

        self.assertIsNone(t_grafana.get_host_cpu_usage(self.m_compute_node))
예제 #7
0
    def test_configured_raise_error(self):
        """Test raising error when using improperly configured GrafanHelper

        Assure that the _get_metric method raises errors if the metric is
        missing from the map
        """

        # Clear the METRIC_MAP of Grafana since it is a static variable that
        # other tests might have set before this test runs.
        grafana.GrafanaHelper.METRIC_MAP = {}

        self.m_conf.grafana_client.base_url = ""

        t_grafana = grafana.GrafanaHelper(osc=mock.Mock())

        self.assertFalse(t_grafana.configured)
        self.assertEqual({}, t_grafana.METRIC_MAP)
        self.assertRaises(exception.MetricNotAvailable,
                          t_grafana.get_host_cpu_usage, self.m_compute_node)
예제 #8
0
    def setUp(self):
        super(TestGrafana, self).setUp()

        self.p_conf = mock.patch.object(grafana,
                                        'CONF',
                                        new_callable=mock.PropertyMock)
        self.m_conf = self.p_conf.start()
        self.addCleanup(self.p_conf.stop)

        self.m_conf.grafana_client.token = \
            "eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk=="
        self.m_conf.grafana_client.base_url = "https://grafana.proxy/api/"
        self.m_conf.grafana_client.project_id_map = {'host_cpu_usage': 7221}
        self.m_conf.grafana_client.database_map = \
            {'host_cpu_usage': 'mock_db'}
        self.m_conf.grafana_client.attribute_map = \
            {'host_cpu_usage': 'hostname'}
        self.m_conf.grafana_client.translator_map = \
            {'host_cpu_usage': 'influxdb'}
        self.m_conf.grafana_client.query_map = \
            {'host_cpu_usage': 'SELECT 100-{0}("{0}_value") FROM {3}.'
                               'cpu_percent WHERE ("host" =~ /^{1}$/ AND '
                               '"type_instance" =~/^idle$/ AND time > '
                               '(now()-{2}m)'}

        self.m_grafana = grafana.GrafanaHelper(osc=mock.Mock())
        stat_agg_patcher = mock.patch.object(
            self.m_grafana,
            'statistic_aggregation',
            spec=grafana.GrafanaHelper.statistic_aggregation)
        self.mock_aggregation = stat_agg_patcher.start()
        self.addCleanup(stat_agg_patcher.stop)

        self.m_compute_node = mock.Mock(
            id='16a86790-327a-45f9-bc82-45839f062fdc',
            hostname='example.hostname.ch')
        self.m_instance = mock.Mock(id='73b1ff78-aca7-404f-ac43-3ed16c1fa555',
                                    human_id='example.hostname')
예제 #9
0
 def grafana(self):
     if self._grafana is None:
         self._grafana = graf.GrafanaHelper(osc=self.osc)
     return self._grafana
예제 #10
0
    def test_configured(self):
        """Initialize GrafanaHelper and check if configured is true"""

        t_grafana = grafana.GrafanaHelper(osc=mock.Mock())
        self.assertTrue(t_grafana.configured)