Пример #1
0
def test_metadata_unit(datadog_agent):
    version = common._get_pdns_version()
    if version == 3:
        instance = common.CONFIG
    elif version == 4:
        instance = common.CONFIG_V4
    check = PowerDNSRecursorCheck("powerdns_recursor", {}, [instance])
    check.check_id = 'test:123'
    check.log = mock.MagicMock()

    config_obj, tags = check._get_config(instance)

    with mock.patch('requests.get', side_effect=requests.exceptions.Timeout()):
        check._collect_metadata(config_obj)
        datadog_agent.assert_metadata_count(0)
        check.log.debug.assert_called_with(
            'Error collecting PowerDNS Recursor version: %s', '')

    datadog_agent.reset()
    with mock.patch('requests.get', return_value=MockResponse()):
        check._collect_metadata(config_obj)
        datadog_agent.assert_metadata_count(0)
        check.log.debug.assert_called_with(
            "Couldn't find the PowerDNS Recursor Server version header")

    datadog_agent.reset()
    with mock.patch(
            'requests.get',
            return_value=MockResponse(headers={'Server': 'wrong_stuff'})):
        check._collect_metadata(config_obj)
        datadog_agent.assert_metadata_count(0)
        check.log.debug.assert_called_with(
            'Error while decoding PowerDNS Recursor version: %s',
            'list index out of range')
Пример #2
0
def test_tags(aggregator, spin_up_powerdns):
    version = _get_pdns_version()

    pdns_check = PowerDNSRecursorCheck(CHECK_NAME, {}, {})
    tags = ['foo:bar']
    if version == 3:
        config = common.CONFIG.copy()
        config['tags'] = ['foo:bar']
        pdns_check.check(config)

        # Assert metrics v3
        for metric in metrics.GAUGE_METRICS:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric), tags=tags, count=1)

        for metric in metrics.RATE_METRICS:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric), tags=tags, count=1)

    elif version == 4:
        config = common.CONFIG_V4.copy()
        config['tags'] = ['foo:bar']
        pdns_check.check(config)

        # Assert metrics v3
        for metric in metrics.GAUGE_METRICS + metrics.GAUGE_METRICS_V4:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric), tags=tags, count=1)

        for metric in metrics.RATE_METRICS + metrics.RATE_METRICS_V4:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric), tags=tags, count=1)

    service_check_tags = common._config_sc_tags(common.CONFIG)
    aggregator.assert_service_check('powerdns.recursor.can_connect',
                                    status=PowerDNSRecursorCheck.OK,
                                    tags=service_check_tags+tags)

    aggregator.assert_all_metrics_covered()
Пример #3
0
def test_very_bad_config(aggregator):
    pdns_check = PowerDNSRecursorCheck(CHECK_NAME, {}, {})
    for config in [{}, {"host": "localhost"}, {"port": 1000}, {"host": "localhost", "port": 1000}]:
        with pytest.raises(Exception):
            pdns_check.check(common.BAD_API_KEY_CONFIG)

    assert len(aggregator._metrics) == 0
def test_check(aggregator):
    # get version and test v3 first.
    version = common._get_pdns_version()
    if version == 3:
        check = PowerDNSRecursorCheck("powerdns_recursor", {}, [common.CONFIG])
        service_check_tags = common._config_sc_tags(common.CONFIG)
        check.check(common.CONFIG)

        # Assert metrics
        for metric in metrics.GAUGE_METRICS:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=[],
                                     count=1)

        for metric in metrics.RATE_METRICS:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=[],
                                     count=1)

        aggregator.assert_service_check('powerdns.recursor.can_connect',
                                        status=check.OK,
                                        tags=service_check_tags)
        aggregator.assert_all_metrics_covered()

    elif version == 4:
        check = PowerDNSRecursorCheck("powerdns_recursor", {},
                                      [common.CONFIG_V4])
        service_check_tags = common._config_sc_tags(common.CONFIG_V4)
        check.check(common.CONFIG_V4)

        # Assert metrics
        for metric in metrics.GAUGE_METRICS + metrics.GAUGE_METRICS_V4:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=[],
                                     count=1)

        for metric in metrics.RATE_METRICS + metrics.RATE_METRICS_V4:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=[],
                                     count=1)

        aggregator.assert_service_check('powerdns.recursor.can_connect',
                                        status=check.OK,
                                        tags=service_check_tags)
        aggregator.assert_all_metrics_covered()
    else:
        raise Exception("Version not supported")
def test_tags(aggregator):
    version = common._get_pdns_version()

    tags = ['foo:bar']
    if version == 3:
        config = common.CONFIG.copy()
        config['tags'] = ['foo:bar']
        check = PowerDNSRecursorCheck("powerdns_recursor", {}, [config])
        check.check(config)

        # Assert metrics v3
        for metric in metrics.GAUGE_METRICS:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=tags,
                                     count=1)

        for metric in metrics.RATE_METRICS:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=tags,
                                     count=1)

    elif version == 4:
        config = common.CONFIG_V4.copy()
        config['tags'] = ['foo:bar']
        check = PowerDNSRecursorCheck("powerdns_recursor", {}, [config])
        check.check(config)

        # Assert metrics v3
        for metric in metrics.GAUGE_METRICS + metrics.GAUGE_METRICS_V4:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=tags,
                                     count=1)

        for metric in metrics.RATE_METRICS + metrics.RATE_METRICS_V4:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=tags,
                                     count=1)
    else:
        raise Exception("Version not supported")

    service_check_tags = common._config_sc_tags(common.CONFIG)
    aggregator.assert_service_check('powerdns.recursor.can_connect',
                                    status=check.OK,
                                    tags=service_check_tags + tags)

    aggregator.assert_all_metrics_covered()
Пример #6
0
def test_api_key_headers():
    instance = deepcopy(common.CONFIG)
    instance.update({'api_key': 'API_KEY', 'headers': {'foo': 'bar'}})
    expected_headers = {'X-API-Key': 'API_KEY', 'foo': 'bar'}

    check = PowerDNSRecursorCheck('powerdns_recursor', {},
                                  instances=[instance])
    assert expected_headers == check._http.options['headers']
Пример #7
0
def test_bad_api_key(aggregator, spin_up_powerdns):
    pdns_check = PowerDNSRecursorCheck(CHECK_NAME, {}, {})
    with pytest.raises(Exception):
        pdns_check.check(common.BAD_API_KEY_CONFIG)

    service_check_tags = common._config_sc_tags(common.BAD_API_KEY_CONFIG)
    aggregator.assert_service_check('powerdns.recursor.can_connect',
                                    status=PowerDNSRecursorCheck.CRITICAL,
                                    tags=service_check_tags)
    assert len(aggregator._metrics) == 0
Пример #8
0
def test_bad_config(aggregator):
    check = PowerDNSRecursorCheck("powerdns_recursor", {}, [common.BAD_CONFIG])
    with pytest.raises(Exception):
        check.check(common.BAD_CONFIG)

    service_check_tags = common._config_sc_tags(common.BAD_CONFIG)
    aggregator.assert_service_check('powerdns.recursor.can_connect',
                                    status=check.CRITICAL,
                                    tags=service_check_tags)
    assert len(aggregator._metrics) == 0
Пример #9
0
def test_metadata_integration(aggregator, datadog_agent):
    version = common._get_pdns_version()
    if version == 3:
        check = PowerDNSRecursorCheck("powerdns_recursor", {}, [common.CONFIG])
        check.check_id = 'test:123'
        check.check(common.CONFIG)
    elif version == 4:
        check = PowerDNSRecursorCheck("powerdns_recursor", {}, [common.CONFIG_V4])
        check.check_id = 'test:123'
        check.check(common.CONFIG_V4)

    major, minor, patch = common.POWERDNS_RECURSOR_VERSION.split('.')
    version_metadata = {
        'version.scheme': 'semver',
        'version.major': major,
        'version.minor': minor,
        'version.patch': patch,
        'version.raw': common.POWERDNS_RECURSOR_VERSION,
    }

    datadog_agent.assert_metadata('test:123', version_metadata)
    datadog_agent.assert_metadata_count(len(version_metadata))
Пример #10
0
def test_very_bad_config(aggregator):
    for config in [{}, {
            "host": "localhost"
    }, {
            "port": 1000
    }, {
            "host": "localhost",
            "port": 1000
    }]:
        check = PowerDNSRecursorCheck("powerdns_recursor", {}, [config])
        with pytest.raises(Exception):
            check.check(config)

    assert len(aggregator._metrics) == 0
Пример #11
0
def test_check(aggregator, spin_up_powerdns):
    service_check_tags = common._config_sc_tags(common.CONFIG)

    # get version and test v3 first.
    version = _get_pdns_version()
    pdns_check = PowerDNSRecursorCheck(CHECK_NAME, {}, {})
    if version == 3:
        pdns_check.check(common.CONFIG)

        # Assert metrics
        for metric in metrics.GAUGE_METRICS:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=[],
                                     count=1)

        for metric in metrics.RATE_METRICS:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=[],
                                     count=1)

        aggregator.assert_service_check('powerdns.recursor.can_connect',
                                        status=PowerDNSRecursorCheck.OK,
                                        tags=service_check_tags)
        assert aggregator.metrics_asserted_pct == 100.0

    elif version == 4:
        pdns_check.check(common.CONFIG_V4)

        # Assert metrics
        for metric in metrics.GAUGE_METRICS + metrics.GAUGE_METRICS_V4:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=[],
                                     count=1)

        for metric in metrics.RATE_METRICS + metrics.RATE_METRICS_V4:
            aggregator.assert_metric(metrics.METRIC_FORMAT.format(metric),
                                     tags=[],
                                     count=1)

        aggregator.assert_service_check('powerdns.recursor.can_connect',
                                        status=PowerDNSRecursorCheck.OK,
                                        tags=service_check_tags)
        assert aggregator.metrics_asserted_pct == 100.0
    else:
        print("powerdns_recursor unknown version.")
        aggregator.assert_service_check('powerdns.recursor.can_connect',
                                        status=PowerDNSRecursorCheck.CRITICAL,
                                        tags=service_check_tags)
Пример #12
0
def check():
    return PowerDNSRecursorCheck("powerdns_recursor", {}, {})