Beispiel #1
0
 def test_get_timeout_exception(self):
     with mock.patch('dns.resolver.query') as query:
         query.side_effect = Timeout()
         with app.flask_app.test_client() as client:
             response = client.get('/mxrecords/example.com')
             json_data = response.get_json()
             self.assertEqual(json_data['exception'],
                              'Timeout -- mx record not found')
             self.assertEqual(response.status_code, 500)
             self.assertRaises(exceptions.InternalServerError)
Beispiel #2
0
class DNSCheckTest(AgentCheckTest):
    CHECK_NAME = 'dns_check'

    def tearDown(self):
        self.check.stop()

    def wait_for_async(self, method, attribute, count):
        """
        Loop on `self.check.method` until `self.check.attribute >= count`.

        Raise after
        """
        i = 0
        while i < RESULTS_TIMEOUT:
            self.check._process_results()
            if len(getattr(self.check, attribute)) >= count:
                return getattr(self.check, method)()
            time.sleep(1)
            i += 1
        raise Exception(
            "Didn't get the right count of service checks in time, {0}/{1} in {2}s: {3}"
            .format(len(getattr(self.check, attribute)), count, i,
                    getattr(self.check, attribute)))

    @mock.patch.object(Resolver, 'query', side_effect=success_query_mock)
    def test_success(self, mocked_query):
        self.run_check(CONFIG_SUCCESS)
        # Overrides self.service_checks attribute when values are available
        self.service_checks = self.wait_for_async('get_service_checks',
                                                  'service_checks', 1)
        tags = [
            'instance:success', 'resolved_hostname:www.example.org',
            'nameserver:127.0.0.1', 'record_type:A'
        ]
        self.assertServiceCheckOK(SERVICE_CHECK_NAME, tags=tags)
        tags = [
            'instance:cname', 'resolved_hostname:www.example.org',
            'nameserver:127.0.0.1', 'record_type:CNAME'
        ]
        self.assertServiceCheckOK(SERVICE_CHECK_NAME, tags=tags)
        self.coverage_report()

    @mock.patch.object(Resolver, 'query', side_effect=nxdomain_query_mock)
    def test_success_nxdomain(self, mocked_query):
        self.run_check(CONFIG_SUCCESS_NXDOMAIN)
        # Overrides self.service_checks attribute when values are available
        self.service_checks = self.wait_for_async('get_service_checks',
                                                  'service_checks', 1)
        self.assertServiceCheckOK(SERVICE_CHECK_NAME,
                                  tags=[
                                      'instance:nxdomain',
                                      'nameserver:127.0.0.1',
                                      'resolved_hostname:www.example.org',
                                      'record_type:NXDOMAIN'
                                  ])
        self.coverage_report()

    @mock.patch.object(Resolver, 'query', side_effect=Timeout())
    def test_default_timeout(self, mocked_query):
        self.run_check(CONFIG_DEFAULT_TIMEOUT)
        # Overrides self.service_checks attribute when values are available
        self.service_checks = self.wait_for_async('get_service_checks',
                                                  'service_checks', 1)
        tags = [
            'instance:default_timeout', 'resolved_hostname:www.example.org',
            'nameserver:127.0.0.1', 'record_type:A'
        ]
        self.assertServiceCheckCritical(SERVICE_CHECK_NAME, tags=tags)
        self.coverage_report()

    @mock.patch.object(Resolver, 'query', side_effect=Timeout())
    def test_instance_timeout(self, mocked_query):
        self.run_check(CONFIG_INSTANCE_TIMEOUT)
        # Overrides self.service_checks attribute when values are available
        self.service_checks = self.wait_for_async('get_service_checks',
                                                  'service_checks', 1)
        tags = [
            'instance:instance_timeout', 'resolved_hostname:www.example.org',
            'nameserver:127.0.0.1', 'record_type:A'
        ]
        self.assertServiceCheckCritical(SERVICE_CHECK_NAME, tags=tags)
        self.coverage_report()

    def test_invalid_config(self):
        for config, exception_class in CONFIG_INVALID:
            self.run_check(config)
            # Overrides self.service_checks attribute when values are available
            self.service_checks = self.wait_for_async('get_service_checks',
                                                      'service_checks', 1)
            self.assertRaises(exception_class)
            self.assertServiceCheckCritical(SERVICE_CHECK_NAME)
            self.coverage_report()
Beispiel #3
0
class DNSCheckTest(AgentCheckTest):
    CHECK_NAME = 'dns_check'

    def tearDown(self):
        self.check.stop()

    @mock.patch.object(Resolver, 'query', side_effect=success_query_mock)
    @mock.patch('time.time', side_effect=MockTime.time)
    def test_success(self, mocked_query, mocked_time):
        self.run_check(CONFIG_SUCCESS)
        # Overrides self.service_checks attribute when values are available
        self.service_checks = self.wait_for_async('get_service_checks',
                                                  'service_checks', 2,
                                                  RESULTS_TIMEOUT)
        self.metrics = self.check.get_metrics()

        tags = [
            'instance:success', 'resolved_hostname:www.example.org',
            'nameserver:127.0.0.1', 'record_type:A'
        ]
        self.assertServiceCheckOK(SERVICE_CHECK_NAME, tags=tags)
        self.assertMetric('dns.response_time', count=1, tags=tags)
        tags = [
            'instance:cname', 'resolved_hostname:www.example.org',
            'nameserver:127.0.0.1', 'record_type:CNAME'
        ]
        self.assertServiceCheckOK(SERVICE_CHECK_NAME, tags=tags)
        self.assertMetric('dns.response_time', count=1, tags=tags)

        self.coverage_report()

    @mock.patch.object(Resolver, 'query', side_effect=nxdomain_query_mock)
    @mock.patch('time.time', side_effect=MockTime.time)
    def test_success_nxdomain(self, mocked_query, mocked_time):
        self.run_check(CONFIG_SUCCESS_NXDOMAIN)
        # Overrides self.service_checks attribute when values are available
        self.service_checks = self.wait_for_async('get_service_checks',
                                                  'service_checks', 1,
                                                  RESULTS_TIMEOUT)
        self.metrics = self.check.get_metrics()

        tags = [
            'instance:nxdomain', 'nameserver:127.0.0.1',
            'resolved_hostname:www.example.org', 'record_type:NXDOMAIN'
        ]
        self.assertServiceCheckOK(SERVICE_CHECK_NAME, tags=tags)
        self.assertMetric('dns.response_time', count=1, tags=tags)

        self.coverage_report()

    @mock.patch.object(Resolver, 'query', side_effect=Timeout())
    @mock.patch('time.time', side_effect=MockTime.time)
    def test_default_timeout(self, mocked_query, mocked_time):
        self.run_check(CONFIG_DEFAULT_TIMEOUT)
        # Overrides self.service_checks attribute when values are available
        self.service_checks = self.wait_for_async('get_service_checks',
                                                  'service_checks', 1,
                                                  RESULTS_TIMEOUT)
        self.metrics = self.check.get_metrics()

        tags = [
            'instance:default_timeout', 'resolved_hostname:www.example.org',
            'nameserver:127.0.0.1', 'record_type:A'
        ]
        self.assertServiceCheckCritical(SERVICE_CHECK_NAME, tags=tags)

        self.coverage_report()

    @mock.patch.object(Resolver, 'query', side_effect=Timeout())
    @mock.patch('time.time', side_effect=MockTime.time)
    def test_instance_timeout(self, mocked_query, mocked_time):
        self.run_check(CONFIG_INSTANCE_TIMEOUT)
        # Overrides self.service_checks attribute when values are available
        self.service_checks = self.wait_for_async('get_service_checks',
                                                  'service_checks', 1,
                                                  RESULTS_TIMEOUT)
        self.metrics = self.check.get_metrics()

        tags = [
            'instance:instance_timeout', 'resolved_hostname:www.example.org',
            'nameserver:127.0.0.1', 'record_type:A'
        ]
        self.assertServiceCheckCritical(SERVICE_CHECK_NAME, tags=tags)

        self.coverage_report()

    def test_invalid_config(self):
        for config, exception_class in CONFIG_INVALID:
            self.run_check(config)
            # Overrides self.service_checks attribute when values are available
            self.service_checks = self.wait_for_async('get_service_checks',
                                                      'service_checks', 1,
                                                      RESULTS_TIMEOUT)
            self.metrics = self.check.get_metrics()

            self.assertRaises(exception_class)
            self.assertServiceCheckCritical(SERVICE_CHECK_NAME)
            self.coverage_report()
        'instance:nxdomain', 'nameserver:127.0.0.1',
        'resolved_hostname:www.example.org', 'record_type:NXDOMAIN'
    ]
    aggregator.assert_service_check(DNSCheck.SERVICE_CHECK_NAME,
                                    status=DNSCheck.OK,
                                    tags=tags,
                                    count=1)
    aggregator.assert_metric('dns.response_time', tags=tags, count=1, value=1)

    # Assert coverage for this check on this instance
    aggregator.assert_all_metrics_covered()


@mock.patch('datadog_checks.dns_check.dns_check.time_func',
            side_effect=MockTime.time)
@mock.patch.object(Resolver, 'query', side_effect=Timeout())
def test_default_timeout(mocked_query, mocked_time, aggregator):
    integration = DNSCheck('dns_check',
                           common.CONFIG_DEFAULT_TIMEOUT['init_config'],
                           common.CONFIG_DEFAULT_TIMEOUT['instances'])
    integration.check(common.CONFIG_DEFAULT_TIMEOUT['instances'][0])

    tags = [
        'instance:default_timeout', 'resolved_hostname:www.example.org',
        'nameserver:127.0.0.1', 'record_type:A'
    ]
    aggregator.assert_service_check(
        DNSCheck.SERVICE_CHECK_NAME,
        status=DNSCheck.CRITICAL,
        tags=tags,
        count=1,
Beispiel #5
0
class TestDns(AgentCheckTest):
    CHECK_NAME = 'dns_check'

    @mock.patch.object(Resolver, 'query', side_effect=success_query_mock)
    def test_success(self, mocked_query):
        config = {
            'instances': [{
                'hostname': 'www.example.org',
                'nameserver': '127.0.0.1'
            }]
        }
        self.run_check(config)
        self.assertMetric(
            'dns.response_time',
            count=1,
            tags=['nameserver:127.0.0.1', 'resolved_hostname:www.example.org'])
        self.assertServiceCheck(
            SERVICE_CHECK_NAME,
            status=AgentCheck.OK,
            tags=['resolved_hostname:www.example.org', 'nameserver:127.0.0.1'])
        self.coverage_report()

    @mock.patch.object(Resolver, 'query', side_effect=Timeout())
    def test_timeout(self, mocked_query):
        configs = [
            # short default timeout
            {
                'init_config': {
                    'default_timeout': 0.1
                },
                'instances': [{
                    'hostname': 'www.example.org',
                    'nameserver': '127.0.0.1'
                }]
            },
            # short timeout
            {
                'instances': [{
                    'hostname': 'www.example.org',
                    'timeout': 0.1,
                    'nameserver': '127.0.0.1'
                }]
            },
        ]
        for config in configs:
            self.assertRaises(Timeout, lambda: self.run_check(config))
            self.assertEquals(len(self.metrics), 0)
            self.assertServiceCheck(SERVICE_CHECK_NAME,
                                    status=AgentCheck.CRITICAL,
                                    tags=[
                                        'resolved_hostname:www.example.org',
                                        'nameserver:127.0.0.1'
                                    ])
            self.coverage_report()

    def test_invalid_config(self):
        configs = [
            # invalid hostname
            {
                'instances': [{
                    'hostname': 'example'
                }]
            },
            # invalid nameserver
            {
                'instances': [{
                    'hostname': 'www.example.org',
                    'nameserver': '0.0.0.0'
                }]
            }
        ]
        for config in configs:
            self.assertRaises(NXDOMAIN, lambda: self.run_check(config))
            self.assertEquals(len(self.metrics), 0)
            self.assertServiceCheck(SERVICE_CHECK_NAME,
                                    status=AgentCheck.CRITICAL)
            self.coverage_report()
Beispiel #6
0
def timeout_query_mock(d_name):
    raise Timeout()
Beispiel #7
0
_DNE_MX_RECORD = MX("IN", "MX", 10,
                    Name("does-not-exist.example.com".split(".")))
_MX_RECORDS = [_MX_RECORD_1]
_IP_SET_RECORDS = [RRset()]

_IPV4_RECORD_1 = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("192.0.2.1", 25))
_IPV4_RECORD_2 = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("192.0.2.2", 25))
_IPV6_RECORD = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("2001:db8::1", 25))
_BAD_IP_RECORD = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("BAD", 25))
_IP_RECORDS = [_IPV4_RECORD_1, _IPV6_RECORD]

_POSSIBLE_DNS_RESULTS = {
    "test3.com": [_MX_RECORD_2],
    "no-mx.example.com": NoAnswer(),
    "does-not-exist.example.com": NXDOMAIN(),
    "timeout.example.com": Timeout(),
    "no-ns-servers.example.com": NoNameservers(),
    "bad-response.example.com": [_BAD_MX_RECORD],
    "no-lookup.example.com": [_DNE_MX_RECORD],
    "no-answer.example.com": [_DNE_MX_RECORD],
    "dns-checks.com": [_MX_RECORD_2],
    "invalid-mx.com": [_MX_RECORD_3]
}

_POSSIBLE_DNS_RESULTS_NO_MX = {
    "does-not-exist.example.com": NXDOMAIN(),
    "mx2.example.com": NXDOMAIN(),
    "no-lookup.example.com": NXDOMAIN(),
    "no-answer.example.com": NoAnswer(),
    "bad-response.example.com": [RRsetInvalid()],
    "dns-checks.com": [