コード例 #1
0
def test_client_logging_enabled_debug_if_agent_uses_debug_or_trace(
        aggregator, instance, agent_log_level, expected_vertica_log_level):
    """
    Improve collection of debug flares by automatically enabling client DEBUG logs when the Agent uses DEBUG logs.
    """
    instance.pop('client_lib_log_level', None)
    root_logger = logging.getLogger()
    root_logger.setLevel(agent_log_level)

    check = VerticaCheck('vertica', {}, [instance])

    with mock.patch('datadog_checks.vertica.vertica.vertica') as vertica:
        check.check(instance)

        vertica.connect.assert_called_with(
            database=mock.ANY,
            host=mock.ANY,
            port=mock.ANY,
            user=mock.ANY,
            password=mock.ANY,
            backup_server_node=mock.ANY,
            connection_load_balance=mock.ANY,
            connection_timeout=mock.ANY,
            log_level=expected_vertica_log_level,
            log_path='',
        )
コード例 #2
0
def test_check(aggregator, instance):
    check = VerticaCheck('vertica', {}, [instance])
    check.check(instance)

    for metric in ALL_METRICS:
        aggregator.assert_metric_has_tag(metric, 'db:datadog')
        aggregator.assert_metric_has_tag(metric, 'foo:bar')

    aggregator.assert_all_metrics_covered()
コード例 #3
0
def test_vertica_log_file_not_created(aggregator, instance):
    instance['client_lib_log_level'] = 'DEBUG'

    vertica_default_log = os.path.join(os.path.dirname(common.HERE), 'vertica_python.log')
    if os.path.exists(vertica_default_log):
        os.remove(vertica_default_log)

    check = VerticaCheck('vertica', {}, [instance])
    check.check(instance)

    assert not os.path.exists(vertica_default_log)
コード例 #4
0
def test_check_connection_load_balance(instance):
    instance['connection_load_balance'] = True
    check = VerticaCheck('vertica', {}, [instance])

    def mock_reset_connection():
        raise Exception('reset_connection was called')

    with mock.patch('vertica_python.vertica.connection.Connection.reset_connection', side_effect=mock_reset_connection):
        check.check(instance)

        with pytest.raises(Exception, match='reset_connection was called'):
            check.check(instance)
コード例 #5
0
def test_check(aggregator, datadog_agent, instance):
    check = VerticaCheck('vertica', {}, [instance])
    check.check_id = 'test:123'
    check.check(instance)

    for metric in ALL_METRICS:
        aggregator.assert_metric_has_tag(metric, 'db:datadog')
        aggregator.assert_metric_has_tag(metric, 'foo:bar')

    aggregator.assert_all_metrics_covered()

    version_metadata = {'version.scheme': 'semver', 'version.major': os.environ['VERTICA_VERSION'][0]}
    datadog_agent.assert_metadata('test:123', version_metadata)
    datadog_agent.assert_metadata_count(len(version_metadata) + 4)
コード例 #6
0
def test_custom_queries(aggregator, instance):
    instance['custom_queries'] = [
        {
            'tags': ['test:vertica'],
            'query': 'SELECT force_outer, table_name FROM v_catalog.tables',
            'columns': [{'name': 'table.force_outer', 'type': 'gauge'}, {'name': 'table_name', 'type': 'tag'}],
        }
    ]

    check = VerticaCheck('vertica', {}, [instance])
    check.check(instance)

    aggregator.assert_metric(
        'vertica.table.force_outer', metric_type=0, tags=['db:datadog', 'foo:bar', 'test:vertica', 'table_name:datadog']
    )
コード例 #7
0
def test_client_logging_disabled(aggregator, instance):
    check = VerticaCheck('vertica', {}, [instance])

    with mock.patch('datadog_checks.vertica.vertica.vertica') as vertica:
        check.check(instance)

        vertica.connect.assert_called_with(
            database=mock.ANY,
            host=mock.ANY,
            port=mock.ANY,
            user=mock.ANY,
            password=mock.ANY,
            backup_server_node=mock.ANY,
            connection_load_balance=mock.ANY,
            connection_timeout=mock.ANY,
        )
コード例 #8
0
def test_client_logging_enabled(aggregator, instance):
    instance['client_lib_log_level'] = 'DEBUG'

    check = VerticaCheck('vertica', {}, [instance])

    with mock.patch('datadog_checks.vertica.vertica.vertica') as vertica:
        check.check(instance)

        vertica.connect.assert_called_with(
            database=mock.ANY,
            host=mock.ANY,
            port=mock.ANY,
            user=mock.ANY,
            password=mock.ANY,
            backup_server_node=mock.ANY,
            connection_load_balance=mock.ANY,
            connection_timeout=mock.ANY,
            log_level='DEBUG',
            log_path=os.devnull,
        )
コード例 #9
0
def test_ssl_config_ok(aggregator):
    cert = os.path.join(CERTIFICATE_DIR, 'cert.cert')
    private_key = os.path.join(CERTIFICATE_DIR, 'server.pem')
    instance = {
        'db': 'abc',
        'server': 'localhost',
        'port': '999',
        'username': '******',
        'password': '******',
        'timeout': 10,
        'tags': ['foo:bar'],
        'tls_verify': True,
        'validate_hostname': True,
        'cert': cert,
        'private_key': private_key,
        'ca_cert': CERTIFICATE_DIR,
    }

    check = VerticaCheck('vertica', {}, [instance])

    with mock.patch('datadog_checks.vertica.vertica.vertica') as vertica:
        with mock.patch('datadog_checks.vertica.vertica.ssl') as ssl:
            vertica.connect.return_value = mock.MagicMock()
            tls_context = mock.MagicMock()
            ssl.SSLContext.return_value = tls_context

            check.check(instance)

            assert tls_context.verify_mode == ssl.CERT_REQUIRED
            assert tls_context.check_hostname is True
            tls_context.load_verify_locations.assert_called_with(
                None, CERTIFICATE_DIR, None)
            tls_context.load_cert_chain.assert_called_with(cert,
                                                           keyfile=private_key)

            assert check._connection is not None

    aggregator.assert_service_check("vertica.can_connect",
                                    status=AgentCheck.OK,
                                    tags=['db:abc', 'foo:bar'])
コード例 #10
0
def test_ssl_config_ok(aggregator, tls_instance):
    with mock.patch('datadog_checks.vertica.vertica.vertica') as vertica:
        with mock.patch('datadog_checks.base.utils.tls.ssl') as ssl:
            vertica.connect.return_value = mock.MagicMock()
            tls_context = mock.MagicMock()
            ssl.SSLContext.return_value = tls_context
            check = VerticaCheck('vertica', {}, [tls_instance])
            check.check(tls_instance)

            assert tls_context.verify_mode == ssl.CERT_REQUIRED
            assert tls_context.check_hostname is True
            tls_context.load_verify_locations.assert_called_with(
                cadata=None, cafile=None, capath=CERTIFICATE_DIR)
            tls_context.load_cert_chain.assert_called_with(cert,
                                                           keyfile=private_key,
                                                           password=None)

            assert check._connection is not None

    aggregator.assert_service_check("vertica.can_connect",
                                    status=AgentCheck.OK,
                                    tags=['db:abc', 'foo:bar'])
コード例 #11
0
def test_client_logging_disabled_if_agent_uses_info(aggregator, instance):
    """
    Library logs should be disabled by default, in particular under normal Agent operation (INFO level).
    """
    instance.pop('client_lib_log_level', None)
    root_logger = logging.getLogger()
    root_logger.setLevel(logging.INFO)

    check = VerticaCheck('vertica', {}, [instance])

    with mock.patch('datadog_checks.vertica.vertica.vertica') as vertica:
        check.check(instance)

        vertica.connect.assert_called_with(
            database=mock.ANY,
            host=mock.ANY,
            port=mock.ANY,
            user=mock.ANY,
            password=mock.ANY,
            backup_server_node=mock.ANY,
            connection_load_balance=mock.ANY,
            connection_timeout=mock.ANY,
        )