def test_check_misconfig(instance): """ Test bad config values """ instance['server'] = None check = Oracle(CHECK_NAME, {}, [instance]) with pytest.raises(ConfigurationError): check.validate_config()
def test_handle_query_error_when_connected_disconnects_and_resets_connection( instance): oracle_check = Oracle(CHECK_NAME, {}, [instance]) cached_connection = mock.Mock() oracle_check._cached_connection = cached_connection error = oracle_check.handle_query_error('foo') assert error == 'foo' assert oracle_check._cached_connection is None cached_connection.assert_has_calls([mock.call.close()])
def test_check_misconfig_invalid_protocol(dd_run_check, instance): """ Test invalid protocol """ instance['protocol'] = 'TCPP' check = Oracle(CHECK_NAME, {}, [instance]) with pytest.raises(Exception): dd_run_check(check)
def test_check_misconfig_null_server(dd_run_check, instance): """ Test null server """ instance['server'] = None check = Oracle(CHECK_NAME, {}, [instance]) with pytest.raises(Exception): dd_run_check(check)
def test__check_only_custom_queries(instance): """ Test the default metrics are not called when only_custom queries set to true """ instance['only_custom_queries'] = True check = Oracle(CHECK_NAME, {}, [instance]) assert check._query_manager.queries == []
def test__check_only_custom_queries_not_set(instance): """ Test the default metrics are called when only_custom queries is not set """ instance['only_custom_queries'] = False check = Oracle(CHECK_NAME, {}, [instance]) assert check._query_manager.queries != []
def test_if_only_custom_queries_default_queries_are_not_set( instance, only_custom_queries, expected_default_queries): """ Test the default metrics are not called or not depending on where or not only_custom_queries is set """ instance['only_custom_queries'] = only_custom_queries check = Oracle(CHECK_NAME, {}, [instance]) assert len(check._query_manager.queries) == expected_default_queries
def test_check_misconfig_invalid_truststore_type(dd_run_check, instance): """ Test truststore type is valid """ instance['jdbc_driver_path'] = '/path/to/jdbc/driver' instance['jdbc_truststore_path'] = '/path/to/jdbc/truststore' instance['jdbc_truststore_type'] = 'wrong_type' instance['protocol'] = 'TCPS' check = Oracle(CHECK_NAME, {}, [instance]) with pytest.raises(Exception): dd_run_check(check)
def test_check_misconfig_empty_truststore_and_type(dd_run_check, instance, jdbc_path, jdbc_type): """ Test if connecting via JDBC with TCPS, both `jdbc_truststore` and `jdbc_truststore_type` are non-empty """ instance['jdbc_driver_path'] = '/path/to/jdbc/driver' instance['jdbc_truststore_path'] = jdbc_path instance['jdbc_truststore_type'] = jdbc_type instance['protocol'] = 'TCPS' check = Oracle(CHECK_NAME, {}, [instance]) with pytest.raises(Exception): dd_run_check(check)
def test__get_connection_instant_client_server_incorrect_formatting( instance, dd_run_check, aggregator): """ Test the _get_connection method using the instant client when the server is formatted incorrectly """ con = mock.MagicMock() instance['server'] = 'localhost:1521a' check = Oracle(CHECK_NAME, {}, [instance]) check.use_oracle_client = True expected_tags = ['server:localhost:1521a', 'optional:tag1'] with mock.patch('datadog_checks.oracle.oracle.cx_Oracle') as cx: cx.connect.return_value = con dd_run_check(check) aggregator.assert_service_check("oracle.can_connect", check.CRITICAL, count=1, tags=expected_tags) aggregator.assert_service_check("oracle.can_query", check.CRITICAL, count=1, tags=expected_tags)
def test__get_connection_jdbc(instance, dd_run_check, aggregator, expected_tags, dsn, jdbc_connect_properties): """ Test the _get_connection method using the JDBC client """ check = Oracle(CHECK_NAME, {}, [instance]) check.use_oracle_client = False con = mock.MagicMock() cx = mock.MagicMock(DatabaseError=RuntimeError) cx.clientversion.side_effect = cx.DatabaseError() jdb = mock.MagicMock() jdb.connect.return_value = con jpype = mock.MagicMock(isJVMStarted=lambda: False) mocks = [ ('datadog_checks.oracle.oracle.cx_Oracle', cx), ('datadog_checks.oracle.oracle.jdb', jdb), ('datadog_checks.oracle.oracle.jpype', jpype), ('datadog_checks.oracle.oracle.JDBC_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._cached_connection == con jdb.connect.assert_called_with('oracle.jdbc.OracleDriver', "jdbc:oracle:thin:@" + dsn, jdbc_connect_properties, None) aggregator.assert_service_check("oracle.can_connect", check.OK, count=1, tags=expected_tags) aggregator.assert_service_check("oracle.can_query", check.OK, count=1, tags=expected_tags)
def test__get_config(check, instance): """ Test the _get_config method """ check = Oracle(CHECK_NAME, {}, [instance]) assert check._user == 'system' assert check._password == 'oracle' assert check._service == 'xe' assert check._jdbc_driver is None assert check._tags == ['optional:tag1'] assert check._service_check_tags == ['server:{}'.format(instance['server']), 'optional:tag1'] assert len(check._query_manager.queries) == 3
def test__get_connection_instant_client(instance, dd_run_check, aggregator, expected_tags): """ Test the _get_connection method using the instant client """ check = Oracle(CHECK_NAME, {}, [instance]) check.use_oracle_client = True con = mock.MagicMock() with mock.patch('datadog_checks.oracle.oracle.cx_Oracle') as cx: cx.connect.return_value = con dd_run_check(check) assert check._cached_connection == con cx.connect.assert_called_with(user='******', password='******', dsn=check._get_dsn()) aggregator.assert_service_check("oracle.can_connect", check.OK, count=1, tags=expected_tags) aggregator.assert_service_check("oracle.can_query", check.OK, count=1, tags=expected_tags)
def test_bad_connection_emits_critical_service_check(aggregator, dd_run_check, bad_instance): """ Test the right service check is sent upon _get_connection failures """ instance = copy.deepcopy(bad_instance) instance.update({'tags': ['optional:tag1']}) oracle_check = Oracle(CHECK_NAME, {}, [instance]) expected_tags = ['server:localhost:1521', 'optional:tag1'] dd_run_check(oracle_check) aggregator.assert_service_check("oracle.can_connect", Oracle.CRITICAL, count=1, tags=expected_tags) aggregator.assert_service_check("oracle.can_query", Oracle.CRITICAL, count=1, tags=expected_tags) assert oracle_check._cached_connection is None
def tcps_check(tcps_instance): return Oracle(CHECK_NAME, {}, [tcps_instance])
def test_handle_query_error_when_not_connected_does_no_fail(instance): oracle_check = Oracle(CHECK_NAME, {}, [instance]) error = oracle_check.handle_query_error('foo') assert error == 'foo'
def check(instance): return Oracle(CHECK_NAME, {}, [instance])
def check(): return Oracle(CHECK_NAME, {}, {})