Ejemplo n.º 1
0
def test_response_time(aggregator):
    """
    Test the response time from a server expected to be up
    """
    instance = deepcopy(common.INSTANCE)
    instance['collect_response_time'] = True
    instance['name'] = 'instance:response_time'
    check = TCPCheck(common.CHECK_NAME, {}, [instance])
    check.check(instance)

    # service check
    expected_tags = [
        'foo:bar', 'target_host:datadoghq.com', 'port:80',
        'instance:instance:response_time'
    ]
    aggregator.assert_service_check('tcp.can_connect',
                                    status=check.OK,
                                    tags=expected_tags)
    aggregator.assert_metric('network.tcp.can_connect',
                             value=1,
                             tags=expected_tags)

    # response time metric
    expected_tags = [
        'url:datadoghq.com:80', 'instance:instance:response_time', 'foo:bar'
    ]
    aggregator.assert_metric('network.tcp.response_time', tags=expected_tags)
    aggregator.assert_all_metrics_covered()
    assert len(aggregator.service_checks('tcp.can_connect')) == 1
Ejemplo n.º 2
0
def test_down(aggregator):
    """
    Service expected to be down
    """
    instance = deepcopy(common.INSTANCE_KO)
    check = TCPCheck(common.CHECK_NAME, {}, [instance])
    check.check(instance)
    expected_tags = ["instance:DownService", "target_host:127.0.0.1", "port:65530", "foo:bar"]
    aggregator.assert_service_check('tcp.can_connect', status=check.CRITICAL, tags=expected_tags)
    aggregator.assert_metric('network.tcp.can_connect', value=0, tags=expected_tags)
    aggregator.assert_all_metrics_covered()
    assert len(aggregator.service_checks('tcp.can_connect')) == 1
Ejemplo n.º 3
0
def test_reattempt_resolution():
    instance = deepcopy(common.INSTANCE)
    check = TCPCheck(common.CHECK_NAME, {}, [instance])

    def failed_connection(self):
        raise Exception()

    check.connect = failed_connection

    with mock.patch.object(check, 'resolve_ip',
                           wraps=check.resolve_ip) as resolve_ip:
        check.check(instance)
        assert resolve_ip.called
Ejemplo n.º 4
0
def test_multiple(aggregator):
    """
    Test when a domain is attached to 3 IPs [UP, DOWN, UP]
    """
    instance = deepcopy(common.INSTANCE_MULTIPLE)
    instance['name'] = 'multiple'
    instance['ip_cache_duration'] = 0
    check = TCPCheck(common.CHECK_NAME, {}, [instance])

    with mock.patch(
            'socket.getaddrinfo',
            return_value=[
                (socket.AF_INET, socket.SOCK_STREAM, 6, '', ('ip1', 80)),
                (socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('ip2', 80, 0,
                                                              0)),
                (socket.AF_INET6, socket.SOCK_STREAM, 6, '', ('ip3', 80, 0,
                                                              0)),
            ],
    ), mock.patch.object(check, 'connect', wraps=check.connect) as connect:
        connect.side_effect = [None, Exception(), None] * 2
        expected_tags = [
            'foo:bar', 'target_host:datadoghq.com', 'port:80',
            'instance:multiple'
        ]

        # Running the check twice
        check.check(None)
        check.check(None)

        aggregator.assert_metric('network.tcp.can_connect',
                                 value=1,
                                 tags=expected_tags + ['address:ip1'],
                                 count=2)
        aggregator.assert_metric('network.tcp.can_connect',
                                 value=0,
                                 tags=expected_tags + ['address:ip2'],
                                 count=2)
        aggregator.assert_metric('network.tcp.can_connect',
                                 value=1,
                                 tags=expected_tags + ['address:ip3'],
                                 count=2)
        aggregator.assert_service_check('tcp.can_connect',
                                        status=check.OK,
                                        count=4)
        aggregator.assert_service_check('tcp.can_connect',
                                        status=check.CRITICAL,
                                        count=2)

    aggregator.assert_all_metrics_covered()
    assert len(aggregator.service_checks('tcp.can_connect')) == 6
Ejemplo n.º 5
0
def test_reattempt_resolution_on_duration():
    instance = deepcopy(common.INSTANCE)
    instance['ip_cache_duration'] = 0
    check = TCPCheck(common.CHECK_NAME, {}, [instance])
    check.check(instance)

    assert check.ip_cache_duration is not None

    # ip_cache_duration = 0 means IP is re-resolved every check run
    with mock.patch.object(check, 'resolve_ips',
                           wraps=check.resolve_ips) as resolve_ips:
        check.check(instance)
        assert resolve_ips.called
        check.check(instance)
        assert resolve_ips.called
        check.check(instance)
        assert resolve_ips.called
Ejemplo n.º 6
0
def test_ipv6(aggregator, check):
    """
    Service expected to be up
    """
    instance = deepcopy(common.INSTANCE_IPV6)
    check = TCPCheck(common.CHECK_NAME, {}, [instance])
    check.check(instance)

    nb_ipv4, nb_ipv6 = 0, 0
    for addr in check.addrs:
        expected_tags = [
            "instance:UpService", "target_host:app.datad0g.com", "port:80",
            "foo:bar"
        ]
        expected_tags.append("address:{}".format(addr))
        if re.match(r'^[0-9a-f:]+$', addr):
            nb_ipv6 += 1
            if has_ipv6_connectivity():
                aggregator.assert_service_check('tcp.can_connect',
                                                status=check.OK,
                                                tags=expected_tags)
                aggregator.assert_metric('network.tcp.can_connect',
                                         value=1,
                                         tags=expected_tags)
            else:
                aggregator.assert_service_check('tcp.can_connect',
                                                status=check.CRITICAL,
                                                tags=expected_tags)
                aggregator.assert_metric('network.tcp.can_connect',
                                         value=0,
                                         tags=expected_tags)
        else:
            nb_ipv4 += 1
            aggregator.assert_service_check('tcp.can_connect',
                                            status=check.OK,
                                            tags=expected_tags)
            aggregator.assert_metric('network.tcp.can_connect',
                                     value=1,
                                     tags=expected_tags)
    assert nb_ipv4 == 3
    # The Windows CI machine doesn't return IPv6
    assert nb_ipv6 == 3 or platform.system() == 'Windows' and nb_ipv6 == 0
    aggregator.assert_all_metrics_covered()
    assert len(
        aggregator.service_checks('tcp.can_connect')) == nb_ipv4 + nb_ipv6
Ejemplo n.º 7
0
def test_down(aggregator):
    """
    Service expected to be down
    """
    instance = deepcopy(common.INSTANCE_KO)
    instance['collect_response_time'] = True
    check = TCPCheck(common.CHECK_NAME, {}, [instance])
    check.check(instance)
    expected_tags = [
        "instance:DownService", "target_host:127.0.0.1", "port:65530",
        "foo:bar"
    ]
    aggregator.assert_service_check('tcp.can_connect',
                                    status=check.CRITICAL,
                                    tags=expected_tags)
    aggregator.assert_metric('network.tcp.can_connect',
                             value=0,
                             tags=expected_tags)
    aggregator.assert_metric(
        'network.tcp.response_time',
        count=0)  # should not submit response time metric on failure
    aggregator.assert_all_metrics_covered()
    assert len(aggregator.service_checks('tcp.can_connect')) == 1
Ejemplo n.º 8
0
def test_reattempt_resolution_on_error():
    instance = deepcopy(common.INSTANCE)
    check = TCPCheck(common.CHECK_NAME, {}, [instance])
    check.check(instance)

    assert check.ip_cache_duration is None

    # IP is not normally re-resolved after the first check run
    with mock.patch.object(check, 'resolve_ips',
                           wraps=check.resolve_ips) as resolve_ips:
        check.check(instance)
        assert not resolve_ips.called

    # Upon connection failure, cached resolved IP is cleared
    with mock.patch.object(check, 'connect', wraps=check.connect) as connect:
        connect.side_effect = lambda self, addr: Exception()
        check.check(instance)
        assert check._addrs is None

    # On next check run IP is re-resolved
    with mock.patch.object(check, 'resolve_ips',
                           wraps=check.resolve_ips) as resolve_ips:
        check.check(instance)
        assert resolve_ips.called
Ejemplo n.º 9
0
def check():
    return TCPCheck(common.CHECK_NAME, {}, {})
Ejemplo n.º 10
0
def test_check_multiple(aggregator):
    check = TCPCheck(common.CHECK_NAME, {}, [common.INSTANCE_MULTIPLE])
    check.check(None)
    common._test_check(aggregator, check._addrs)
    assert len(check._addrs) == 4
def check():
    return TCPCheck(common.CHECK_NAME, {}, [common.INSTANCE])
Ejemplo n.º 12
0
def check():
    return TCPCheck(CHECK_NAME, {}, {})