Beispiel #1
0
def test_config(dd_run_check, aggregator, test_instance, err_msg, is_valid):
    check = TeradataCheck(CHECK_NAME, {}, [test_instance])
    conn = mock.MagicMock()
    cursor = conn.cursor()
    cursor.rowcount = float('+inf')

    teradatasql = mock.MagicMock()
    teradatasql.connect.return_value = conn

    mocks = [
        ('datadog_checks.teradata.check.teradatasql', teradatasql),
        ('datadog_checks.teradata.check.TERADATASQL_IMPORT_ERROR', None),
    ]

    with ExitStack() as stack:
        for mock_call in mocks:
            stack.enter_context(mock.patch(*mock_call))

        if not is_valid:
            with pytest.raises(
                    Exception,
                    match=err_msg,
            ):
                dd_run_check(check)
        else:
            try:
                dd_run_check(check)
                aggregator.assert_service_check(SERVICE_CHECK_CONNECT,
                                                ServiceCheck.OK,
                                                count=1)
                aggregator.assert_service_check(SERVICE_CHECK_QUERY,
                                                ServiceCheck.OK,
                                                count=1)
            except AssertionError as e:
                raise AssertionError(e)
Beispiel #2
0
def test_connection_errors(cursor_factory, dd_run_check, aggregator, instance, caplog):
    with cursor_factory(exception=True):
        check = TeradataCheck(CHECK_NAME, {}, [instance])
        with pytest.raises(Exception, match="Exception: Unable to connect to Teradata."):
            dd_run_check(check)
        aggregator.assert_service_check(SERVICE_CHECK_CONNECT, ServiceCheck.CRITICAL, tags=EXPECTED_TAGS)
        aggregator.assert_service_check(SERVICE_CHECK_QUERY, count=0)
def test_timestamp_validator(caplog, instance, row, expected, msg):
    check = TeradataCheck(CHECK_NAME, {}, [instance])
    result = timestamp_validator(check, row)
    assert result == expected
    if msg:
        assert msg in caplog.text
    else:
        assert not caplog.text
def test_submit_version(instance, caplog, row, expected_log):
    caplog.clear()
    check = TeradataCheck(CHECK_NAME, {}, [instance])
    submit_version(check, row)

    if expected_log:
        assert expected_log in caplog.text
    else:
        assert caplog.text == ''
Beispiel #5
0
def test_tables_filter(cursor_factory, config, expected, instance, dd_run_check, aggregator):
    instance['tables'] = config
    check = TeradataCheck(CHECK_NAME, {}, [instance])
    with cursor_factory():
        dd_run_check(check)
    tag_template = 'td_table:{}'
    for metric in TABLE_DISK_METRICS:
        if isinstance(config, list):
            if not config:
                aggregator.assert_metric_has_tag_prefix(metric, 'td_table', at_least=1)
            for include_table in config:
                aggregator.assert_metric_has_tag(metric, tag_template.format(include_table), at_least=1)
                aggregator.assert_metric_has_tag(metric, tag_template.format("DimOrganization"), count=0)
        else:
            for include_table in expected[0]:
                aggregator.assert_metric_has_tag(metric, tag_template.format(include_table), at_least=1)
            for exclude_table in expected[1]:
                aggregator.assert_metric_has_tag(metric, tag_template.format(exclude_table), count=0)
            aggregator.assert_metric_has_tag(metric, tag_template.format("DimOrganization"), count=0)

    assert check._tables_filter == expected
Beispiel #6
0
def test_query_errors(dd_run_check, aggregator, instance):
    check = TeradataCheck(CHECK_NAME, {}, [instance])
    query_error = mock.Mock(side_effect=Exception("teradatasql.Error"))
    check._query_manager.executor = mock.MagicMock()
    check._query_manager.executor.side_effect = query_error

    teradatasql = mock.MagicMock()

    mocks = [
        ('datadog_checks.teradata.check.teradatasql', teradatasql),
        ('datadog_checks.teradata.check.TERADATASQL_IMPORT_ERROR', None),
    ]

    with ExitStack() as stack:
        for mock_call in mocks:
            stack.enter_context(mock.patch(*mock_call))
        dd_run_check(check)

    assert check._query_errors > 0

    aggregator.assert_service_check(SERVICE_CHECK_CONNECT, ServiceCheck.OK, tags=EXPECTED_TAGS)
    aggregator.assert_service_check(SERVICE_CHECK_QUERY, ServiceCheck.CRITICAL, tags=EXPECTED_TAGS)
Beispiel #7
0
def test_connect(test_instance, dd_run_check, aggregator, expected_tags, conn_params):
    check = TeradataCheck(CHECK_NAME, {}, [test_instance])
    conn = mock.MagicMock()
    cursor = conn.cursor()
    cursor.rowcount = float('+inf')

    teradatasql = mock.MagicMock()
    teradatasql.connect.return_value = conn

    mocks = [
        ('datadog_checks.teradata.check.teradatasql', teradatasql),
        ('datadog_checks.teradata.check.TERADATASQL_IMPORT_ERROR', None),
    ]

    with ExitStack() as stack:
        for mock_call in mocks:
            stack.enter_context(mock.patch(*mock_call))
        dd_run_check(check)
        assert check._connection == conn

    teradatasql.connect.assert_called_with(json.dumps(conn_params))
    aggregator.assert_service_check(SERVICE_CHECK_CONNECT, ServiceCheck.OK, tags=expected_tags)
    aggregator.assert_service_check(SERVICE_CHECK_QUERY, ServiceCheck.OK, tags=expected_tags)
Beispiel #8
0
def test_no_rows_returned(dd_run_check, aggregator, instance, caplog):
    caplog.clear()
    caplog.set_level(logging.WARNING)
    check = TeradataCheck(CHECK_NAME, {}, [instance])
    conn = mock.MagicMock()
    cursor = conn.cursor()
    cursor.rowcount = 0

    teradatasql = mock.MagicMock()
    teradatasql.connect.return_value = conn

    mocks = [
        ('datadog_checks.teradata.check.teradatasql', teradatasql),
        ('datadog_checks.teradata.check.TERADATASQL_IMPORT_ERROR', None),
    ]

    with ExitStack() as stack:
        for mock_call in mocks:
            stack.enter_context(mock.patch(*mock_call))
        dd_run_check(check)

    assert "Failed to fetch records from query:" in caplog.text
    aggregator.assert_service_check(SERVICE_CHECK_CONNECT, ServiceCheck.OK, tags=EXPECTED_TAGS)
    aggregator.assert_service_check(SERVICE_CHECK_QUERY, ServiceCheck.CRITICAL, tags=EXPECTED_TAGS)