ClickHouse/tests/integration/test_postgresql_protocol/test.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

194 lines
5.5 KiB
Python
Raw Normal View History

2020-05-30 17:04:02 +00:00
# -*- coding: utf-8 -*-
import datetime
import decimal
import os
import uuid
2023-01-03 19:30:14 +00:00
import logging
2020-05-30 17:04:02 +00:00
import psycopg2 as py_psql
import psycopg2.extras
import pytest
2021-01-22 14:27:23 +00:00
from helpers.cluster import ClickHouseCluster, get_docker_compose_path, run_and_check
2020-05-30 17:04:02 +00:00
psycopg2.extras.register_uuid()
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
DOCKER_COMPOSE_PATH = get_docker_compose_path()
2020-05-30 17:04:02 +00:00
cluster = ClickHouseCluster(__file__)
2023-01-03 19:30:14 +00:00
cluster.add_instance(
"node",
main_configs=[
"configs/postresql.xml",
"configs/log.xml",
"configs/ssl_conf.xml",
"configs/dhparam.pem",
"configs/server.crt",
"configs/server.key",
],
user_configs=["configs/default_passwd.xml"],
2023-01-03 19:30:14 +00:00
with_postgres=True,
with_postgresql_java_client=True,
env_variables={"UBSAN_OPTIONS": "print_stacktrace=1"},
)
2020-05-30 17:04:02 +00:00
server_port = 5433
2023-01-03 21:28:54 +00:00
2020-05-30 17:04:02 +00:00
@pytest.fixture(scope="module")
2023-01-03 19:30:14 +00:00
def started_cluster():
2020-05-30 17:04:02 +00:00
try:
2023-01-03 19:30:14 +00:00
cluster.start()
yield cluster
except Exception as ex:
logging.exception(ex)
raise ex
2020-05-30 17:04:02 +00:00
finally:
cluster.shutdown()
2023-01-03 21:28:54 +00:00
2023-01-03 19:30:14 +00:00
def test_psql_client(started_cluster):
node = cluster.instances["node"]
2020-05-30 17:04:02 +00:00
2023-01-03 21:28:54 +00:00
for query_file in ["query1.sql", "query2.sql", "query3.sql", "query4.sql"]:
started_cluster.copy_file_to_container(
started_cluster.postgres_id,
os.path.join(SCRIPT_DIR, "queries", query_file),
f"/{query_file}",
)
cmd_prefix = [
"/usr/bin/psql",
f"sslmode=require host={node.hostname} port={server_port} user=default dbname=default password=123",
]
2023-01-03 19:30:14 +00:00
cmd_prefix += ["--no-align", "--field-separator=' '"]
2020-05-30 17:04:02 +00:00
2023-01-03 21:28:54 +00:00
res = started_cluster.exec_in_container(
started_cluster.postgres_id, cmd_prefix + ["-f", "/query1.sql"], shell=True
)
2023-01-03 19:30:14 +00:00
logging.debug(res)
assert res == "\n".join(["a", "1", "(1 row)", ""])
2020-05-30 17:04:02 +00:00
2023-01-03 21:28:54 +00:00
res = started_cluster.exec_in_container(
started_cluster.postgres_id, cmd_prefix + ["-f", "/query2.sql"], shell=True
)
2023-01-03 19:30:14 +00:00
logging.debug(res)
assert res == "\n".join(["a", "колонка", "(1 row)", ""])
2020-05-30 17:04:02 +00:00
2023-01-03 21:28:54 +00:00
res = started_cluster.exec_in_container(
started_cluster.postgres_id, cmd_prefix + ["-f", "/query3.sql"], shell=True
)
2023-01-03 19:30:14 +00:00
logging.debug(res)
assert res == "\n".join(
2023-01-03 21:28:54 +00:00
[
"SELECT 0",
"SELECT 0",
"SELECT 0",
"INSERT 0 0",
"INSERT 0 0",
"column",
"0",
"0",
"1",
"1",
"5",
"5",
"(6 rows)",
"SELECT 0\n",
]
2020-05-30 17:04:02 +00:00
)
2023-01-03 21:28:54 +00:00
res = started_cluster.exec_in_container(
started_cluster.postgres_id, cmd_prefix + ["-f", "/query4.sql"], shell=True
2020-05-30 17:04:02 +00:00
)
2023-01-03 19:30:14 +00:00
logging.debug(res)
2023-01-03 21:28:54 +00:00
assert res == "\n".join(
["SELECT 0", "INSERT 0 0", "tmp_column", "0", "1", "(2 rows)", "SELECT 0\n"]
)
2020-05-30 17:04:02 +00:00
2023-01-03 19:30:14 +00:00
def test_python_client(started_cluster):
node = cluster.instances["node"]
2020-05-30 17:04:02 +00:00
with pytest.raises(py_psql.InternalError) as exc_info:
ch = py_psql.connect(
2023-01-03 19:30:14 +00:00
host=node.ip_address,
port=server_port,
user="default",
password="123",
database="",
)
2020-05-30 17:04:02 +00:00
cur = ch.cursor()
cur.execute("select name from tables;")
2020-05-30 17:04:02 +00:00
assert exc_info.value.args == (
"Query execution failed.\nDB::Exception: Table default.tables doesn't exist\nSSL connection has been closed unexpectedly\n",
)
2020-05-30 17:04:02 +00:00
ch = py_psql.connect(
2023-01-03 19:30:14 +00:00
host=node.ip_address,
port=server_port,
user="default",
password="123",
database="",
)
2020-05-30 17:04:02 +00:00
cur = ch.cursor()
cur.execute("select 1 as a, 2 as b")
assert (cur.description[0].name, cur.description[1].name) == ("a", "b")
2020-05-30 17:04:02 +00:00
assert cur.fetchall() == [(1, 2)]
cur.execute("CREATE DATABASE x")
cur.execute("USE x")
cur.execute(
"CREATE TEMPORARY TABLE tmp2 (ch Int8, i64 Int64, f64 Float64, str String, date Date, dec Decimal(19, 10), uuid UUID) ENGINE = Memory"
)
cur.execute(
"insert into tmp2 (ch, i64, f64, str, date, dec, uuid) values (44, 534324234, 0.32423423, 'hello', '2019-01-23', 0.333333, '61f0c404-5cb3-11e7-907b-a6006ad3dba0')"
)
cur.execute("select * from tmp2")
assert cur.fetchall()[0] == (
"44",
534324234,
0.32423423,
"hello",
datetime.date(2019, 1, 23),
decimal.Decimal("0.3333330000"),
uuid.UUID("61f0c404-5cb3-11e7-907b-a6006ad3dba0"),
)
2023-01-03 21:28:54 +00:00
cur.execute("DROP DATABASE x")
2020-05-30 17:04:02 +00:00
2023-01-03 19:30:14 +00:00
def test_java_client(started_cluster):
node = cluster.instances["node"]
with open(os.path.join(SCRIPT_DIR, "java.reference")) as fp:
2020-05-30 17:04:02 +00:00
reference = fp.read()
# database not exists exception.
2023-01-03 19:30:14 +00:00
with pytest.raises(Exception) as exc:
2023-01-03 21:28:54 +00:00
res = started_cluster.exec_in_container(
started_cluster.postgresql_java_client_docker_id,
[
"bash",
"-c",
f"java JavaConnectorTest --host {node.hostname} --port {server_port} --user default --database abc",
],
)
assert (
"org.postgresql.util.PSQLException: ERROR: Invalid user or password"
in str(exc.value)
)
2020-05-30 17:04:02 +00:00
# non-empty password passed.
2023-01-03 21:28:54 +00:00
res = started_cluster.exec_in_container(
started_cluster.postgresql_java_client_docker_id,
[
"bash",
"-c",
f"java JavaConnectorTest --host {node.hostname} --port {server_port} --user default --password 123 --database default",
],
)
2023-01-03 19:30:14 +00:00
assert res == reference