Esempio n. 1
0
    def test_fetch_available_endpoints(self):

        # test with no responses
        with patch.object(ddf_exporter.DDFCollector,
                          '_make_request',
                          return_value={}) as mock_make_request:
            exp = ddf_exporter.DDFCollector()
            self.assertDictEqual(exp.fetch_available_endpoints(), {})

        # test with 1 response
        with patch.object(ddf_exporter.DDFCollector,
                          '_make_request',
                          return_value={'fakeItemA':
                                        'a'}) as mock_make_request:
            exp = ddf_exporter.DDFCollector()
            self.assertDictEqual(exp.fetch_available_endpoints(),
                                 {'fake_item_a': 'fakeItemA'})

        # Test with multiple responses
        with patch.object(ddf_exporter.DDFCollector,
                          '_make_request',
                          return_value={
                              'fakeItemA': 'a',
                              'fakeItemB': 'b'
                          }) as mock_make_request:
            exp = ddf_exporter.DDFCollector()
            self.assertDictEqual(exp.fetch_available_endpoints(), {
                'fake_item_a': 'fakeItemA',
                'fake_item_b': 'fakeItemB'
            })
Esempio n. 2
0
    def test__make_request_secure_no_file(self, mock_get, mock_os):

        os.environ['SECURE'] = "True"

        exp = ddf_exporter.DDFCollector()

        # test no cert response
        self.assertRaises(FileNotFoundError, exp._make_request,
                          'connection_error')
Esempio n. 3
0
    def test__make_request_secure(self, mock_get, mock_os):

        os.environ['SECURE'] = "True"

        exp = ddf_exporter.DDFCollector()

        # test_acquire_endpoints_request
        json_data = exp._make_request('', offset=None)
        self.assertEqual(json_data, {'title': 'categoryQueries'}, 200)

        # test_successful_request
        exp.metric_endpoints['test_metric'] = 'testMetric'
        json_data = exp._make_request('test_metric')
        self.assertEqual(json_data, {'value': 0.0}, 200)

        # test_site_not_found_request
        exp.metric_endpoints['not_found_metric'] = 'notFoundMetric'
        json_data = exp._make_request('not_found_metric')
        self.assertIsNone(json_data)

        # test_no_data_field_response
        exp.metric_endpoints['connection_error'] = 'connectionError'
        json_data = exp._make_request('connection_error')
        self.assertDictEqual(json_data, {})
Esempio n. 4
0
    def test_populate_and_fetch_metrics(self, mock_requests):

        exp = ddf_exporter.DDFCollector()

        # test empty dict
        metrics = exp.populate_and_fetch_metrics({}, self.metric_prefix)
        self.assertDictEqual(metrics, {})

        # test metric without labels
        with patch.object(prometheus_client.metrics_core.GaugeMetricFamily,
                          'add_metric') as mock_add_metric:
            exp.populate_and_fetch_metrics({'no_label': 'no_label'},
                                           self.metric_prefix)

        mock_add_metric.assert_called_with(labels=[], value=0.0)

        # test metric with labels
        with patch.object(prometheus_client.metrics_core.GaugeMetricFamily,
                          'add_metric') as mock_add_metric:
            exp.populate_and_fetch_metrics({'yes_label': 'yes_label'},
                                           self.metric_prefix,
                                           {'host_label': 'machine_yes_label'})

        mock_add_metric.assert_called_with(labels=['machine_yes_label'],
                                           value=1.0)

        # test 1 metric; 0 sample
        with patch.object(prometheus_client.metrics_core.GaugeMetricFamily,
                          'add_metric') as mock_add_metric:
            exp.populate_and_fetch_metrics({'1_0': '1_0'}, self.metric_prefix,
                                           {'host_label': 'machine_1_0'})

        mock_add_metric.assert_not_called()

        # test 1 metric; 1 sample
        with patch.object(prometheus_client.metrics_core.GaugeMetricFamily,
                          'add_metric') as mock_add_metric:
            exp.populate_and_fetch_metrics({'1_1': '1_1'}, self.metric_prefix,
                                           {'host_label': 'machine_1_1'})

        mock_add_metric.assert_called_with(labels=['machine_1_1'], value=1.0)

        # test 1 metric; multiple samples
        with patch.object(prometheus_client.metrics_core.GaugeMetricFamily,
                          'add_metric') as mock_add_metric:
            exp.populate_and_fetch_metrics({'1_2': '1_2'}, self.metric_prefix,
                                           {'host_label': 'machine_1_2'})

        calls = [
            call(labels=['machine_1_2'], value=1.0),
            call(labels=['machine_1_2'], value=2.0)
        ]
        mock_add_metric.assert_has_calls(calls)

        # test multiple metric; 0 sample
        with patch.object(prometheus_client.metrics_core.GaugeMetricFamily,
                          'add_metric') as mock_add_metric:
            exp.populate_and_fetch_metrics(
                {
                    '2_0_metric_1': '2_0_metric_1',
                    '2_0_metric_2': '2_0_metric_2'
                }, self.metric_prefix, {'host_label': 'machine_2_0'})

        mock_add_metric.assert_not_called()

        # test multiple metric; 1 sample
        with patch.object(prometheus_client.metrics_core.GaugeMetricFamily,
                          'add_metric') as mock_add_metric:
            exp.populate_and_fetch_metrics(
                {
                    '2_1_metric_1': '2_1_metric_1',
                    '2_1_metric_2': '2_1_metric_2'
                }, self.metric_prefix, {'host_label': 'machine_2_1'})

        calls = [
            call(labels=['machine_2_1'], value=1.0),
            call(labels=['machine_2_1'], value=2.0)
        ]
        mock_add_metric.assert_has_calls(calls)
        # Its hard to tell because we are patching the add_metric method, but the metric_results would end up looking
        # similar to:
        # {
        #  '2_1_metric_1':
        # 	Metric(test_case_2_1_metric_1, ...,
        # 		[Sample(name='test_case_2_1_metric_1',
        # 				labels={'host_label': 'machine_2_1'},
        # 				value=1.0)
        # 		]
        # 	),
        #  '2_1_metric_2':
        # 	Metric(test_case_2_1_metric_2, ...,
        # 		[Sample(name='test_case_2_1_metric_2',
        # 				labels={'host_label': 'machine_2_1'},
        # 				value=2.0)
        # 		]
        # 	)
        # }

        # test multiple metric; multiple samples
        with patch.object(prometheus_client.metrics_core.GaugeMetricFamily,
                          'add_metric') as mock_add_metric:
            metrics = exp.populate_and_fetch_metrics(
                {
                    '2_2_metric_1': '2_2_metric_1',
                    '2_2_metric_2': '2_2_metric_2'
                }, self.metric_prefix, {'host_label': 'machine_2_2'})

        calls = [
            call(labels=['machine_2_2'], value=1.0),
            call(labels=['machine_2_2'], value=2.0),
            call(labels=['machine_2_2'], value=1.0),
            call(labels=['machine_2_2'], value=2.0)
        ]
        mock_add_metric.assert_has_calls(calls)