Пример #1
0
def test_tls_config_ok(dd_run_check, instance, check_hostname):
    check = HTTPCheck(
        'http_check',
        {'ca_certs': mock_get_ca_certs_path()},
        [instance],
    )
    tls_context = check.get_tls_context()
    assert tls_context.check_hostname is check_hostname
def test_check_ssl_expire_error(aggregator, http_check):
    with mock.patch('ssl.SSLSocket.getpeercert', side_effect=Exception()):
        # Run the check for the one instance configured with days left
        http_check = HTTPCheck('', {}, [CONFIG_EXPIRED_SSL['instances'][0]])
        http_check.check(CONFIG_EXPIRED_SSL['instances'][0])

    expired_cert_tags = ['url:https://valid.mock', 'instance:expired_cert']
    aggregator.assert_service_check(HTTPCheck.SC_STATUS, status=HTTPCheck.OK, tags=expired_cert_tags, count=1)
    aggregator.assert_service_check(HTTPCheck.SC_SSL_CERT, status=HTTPCheck.CRITICAL, tags=expired_cert_tags, count=1)
Пример #3
0
def test__init__():
    # empty values should be ignored
    init_config = {'ca_certs': ''}
    # `get_ca_certs_path` needs to be mocked because it's used as fallback when
    # init_config doesn't contain `ca_certs`
    with mock.patch('datadog_checks.http_check.http_check.get_ca_certs_path',
                    return_value='bar'):
        http_check = HTTPCheck('http_check', init_config, [{}])
        assert http_check.ca_certs == 'bar'

    # normal case
    init_config = {'ca_certs': 'foo'}
    http_check = HTTPCheck('http_check', init_config, [{}])
    assert http_check.ca_certs == 'foo'
Пример #4
0
def test_message_when_content_is_disabled():
    content = "This is not part of the message"
    error_message = 'There has been an error.'
    message = HTTPCheck._include_content(False, error_message, content)

    assert message == error_message
    assert content not in message
Пример #5
0
def test_instance_auth_token(dd_run_check):
    token_path = os.path.join(HERE, 'fixtures', 'token.txt')
    with open(token_path, 'r') as t:
        data = t.read()
    auth_token = {
        "reader": {
            "type": "file",
            "path": token_path,
        },
        "writer": {"type": "header", "name": "Authorization"},
    }

    expected_headers = OrderedDict(
        [
            ('User-Agent', 'Datadog Agent/0.0.0'),
            ('Accept', '*/*'),
            ('Accept-Encoding', 'gzip, deflate'),
            ('Authorization', str(data)),
        ]
    )

    instance = {'url': 'https://valid.mock', 'name': 'UpService', "auth_token": auth_token}
    check = HTTPCheck('http_check', {'ca_certs': mock_get_ca_certs_path()}, [instance])
    dd_run_check(check)
    assert expected_headers == check.http.options['headers']
    dd_run_check(check)
    assert expected_headers == check.http.options['headers']
def test_unexisting_ca_cert_should_throw_error(aggregator):
    instance = {
        'name': 'Test Web VM HTTPS SSL',
        'url': 'https://foo.bar.net/',
        'method': 'get',
        'tls_ca_cert': '/tmp/unexisting.crt',
        'check_certificate_expiration': 'false',
        'collect_response_time': 'false',
        'disable_ssl_validation': 'false',
        'skip_proxy': 'false',
    }

    check = HTTPCheck('http_check', {'ca_certs': 'foo'}, [instance])

    check.check(instance)
    aggregator.assert_service_check(HTTPCheck.SC_STATUS, status=AgentCheck.CRITICAL)
    assert 'invalid path: /tmp/unexisting.crt' in aggregator._service_checks[HTTPCheck.SC_STATUS][0].message
Пример #7
0
def test_expected_headers(dd_run_check, instance, expected_headers):

    check = HTTPCheck('http_check', {'ca_certs': mock_get_ca_certs_path()}, [instance])
    dd_run_check(check)
    assert expected_headers == check.http.options['headers']

    dd_run_check(check)
    assert expected_headers == check.http.options['headers']
Пример #8
0
def test__init__():
    init_config = {
        'ca_certs': 'foo'
    }
    # `get_ca_certs_path` needs to be mocked because it's used as the default
    # value for `init_config.get('ca_certs')`
    with mock.patch('datadog_checks.http_check.http_check.get_ca_certs_path'):
        http_check = HTTPCheck('http_check', init_config, {})
        assert http_check.ca_certs == 'foo'
Пример #9
0
def http_check():
    from datadog_checks.http_check import HTTPCheck

    # Patch the function to return the certs located in the `tests/` folder
    patcher = patch('datadog_checks.http_check.http_check.get_ca_certs_path', new=mock_get_ca_certs_path)
    patcher.start()

    http_check = HTTPCheck('http_check', {}, {})
    yield http_check

    patcher.stop()
Пример #10
0
def test_check_tsl_ca_cert(aggregator):
    instance = {
        'name': 'good_cert',
        'url': 'https://valid.mock:443',
        'timeout': 1,
        'tls_ca_cert': os.path.join(HERE, 'fixtures', 'cacert.pem'),
        'check_certificate_expiration': 'false',
        'collect_response_time': 'false',
        'disable_ssl_validation': 'false',
        'skip_proxy': 'false',
    }

    with mock.patch(
        'datadog_checks.http_check.http_check.get_ca_certs_path',
        new=lambda: os.path.join(HERE, 'fixtures', 'emptycert.pem'),
    ):
        check = HTTPCheck('http_check', {}, [instance])

    check.check(instance)
    good_cert_tags = ['url:https://valid.mock:443', 'instance:good_cert']
    aggregator.assert_service_check(HTTPCheck.SC_STATUS, status=HTTPCheck.OK, tags=good_cert_tags, count=1)
Пример #11
0
def test_instances_do_not_share_data():
    http_check_1 = HTTPCheck('http_check', {'ca_certs': 'foo'}, [{}])
    http_check_1.HTTP_CONFIG_REMAPPER['ca_certs']['default'] = 'foo'
    http_check_2 = HTTPCheck('http_check', {'ca_certs': 'bar'}, [{}])
    http_check_2.HTTP_CONFIG_REMAPPER['ca_certs']['default'] = 'bar'

    assert http_check_1.HTTP_CONFIG_REMAPPER['ca_certs']['default'] == 'foo'
    assert http_check_2.HTTP_CONFIG_REMAPPER['ca_certs']['default'] == 'bar'
Пример #12
0
def test_message_lenght_when_content_is_too_long():
    max_lenght = http_check.MESSAGE_LENGTH
    try:
        http_check.MESSAGE_LENGTH = 25
        too_long_content = 'this message is too long'
        error_message = 'There has been an error.'
        message = HTTPCheck._include_content(True, error_message,
                                             too_long_content)
    finally:
        http_check.MESSAGE_LENGTH = max_lenght

    assert len(message) == 25
    assert error_message in message
    assert too_long_content not in message
Пример #13
0
def test_check_allow_redirects(aggregator):
    with mock.patch('datadog_checks.http_check.http_check.get_ca_certs_path', new=mock_get_ca_certs_path):
        http_check = HTTPCheck('http_check', {}, CONFIG_HTTP_NO_REDIRECTS["instances"])
        # Run the check for the one instance
        http_check.check(CONFIG_HTTP_NO_REDIRECTS['instances'][0])
        redirect_service_tags = ['url:https://valid.mock/301', 'instance:no_allow_redirect_service']
        aggregator.assert_service_check(HTTPCheck.SC_STATUS, status=HTTPCheck.OK, tags=redirect_service_tags, count=1)

        redirect_service_tags = ['url:https://valid.mock/301', 'instance:allow_redirect_service']
        http_check.check(CONFIG_HTTP_ALLOW_REDIRECTS['instances'][0])
        aggregator.assert_service_check(
            HTTPCheck.SC_STATUS, status=HTTPCheck.CRITICAL, tags=redirect_service_tags, count=1
        )
Пример #14
0
def test_message_lenght_when_content_is_ok():
    content = '''{
    "HikariPool-1.pool.ConnectivityCheck" : {
        "healthy" : true
    },
    "database" : {
        "healthy" : true,
        "message" : "Service located at jdbc ostgresql://pgbouncer-server.staging.net is alive. Version: 1.5"
    },
    "deadlocks" : {
        "healthy" : true
    }
    "gateway" : {
        "healthy" : true,
        "message" : "Service located at https://apis.staging.eu.people-doc.com is alive."
    }
}'''
    error_message = 'There has been an error.'
    message = HTTPCheck._include_content(True, error_message, content)

    assert len(message) < http_check.MESSAGE_LENGTH
    assert content in message
    assert error_message in message
Пример #15
0
def http_check():
    # Patch the function to return the certs located in the `tests/` folder
    with patch('datadog_checks.http_check.http_check.get_ca_certs_path',
               new=mock_get_ca_certs_path):
        yield HTTPCheck('http_check', {}, [{}])