diff --git a/pgproto/test/auth_test.py b/pgproto/test/auth_test.py index f5ae037108408e5a44320cf81d574427fc12cd10..58fe9dbaf399e17a7222e83ccb8a194bd74ad178 100644 --- a/pgproto/test/auth_test.py +++ b/pgproto/test/auth_test.py @@ -1,43 +1,14 @@ import pytest -from conftest import Cluster import pg8000.dbapi as pg -import os +from conftest import Postgres -def start_pg_server(instance, host, service): - start_pg_server_lua_code = f""" - package.cpath="{os.environ['LUA_CPATH']}" - net_box = require('net.box') - box.schema.func.create('pgproto.server_start', {{language = 'C'}}) - box.schema.user.grant('guest', 'execute', 'function', 'pgproto.server_start') - - box.cfg{{listen=3301}} - caller = net_box:new(3301) - caller:call('pgproto.server_start', {{ '{host}', '{service}' }}) - """ - instance.eval(start_pg_server_lua_code) - -def stop_pg_server(instance): - stop_pg_server_lua_code = f""" - local net_box = require('net.box') - box.schema.func.create('pgproto.server_stop', {{language = 'C'}}) - box.schema.user.grant('guest', 'execute', 'function', 'pgproto.server_stop') - - box.cfg{{listen=3301}} - local caller = net_box:new(3301) - caller:call('pgproto.server_stop') - - box.schema.func.drop('pgproto.server_start') - box.schema.func.drop('pgproto.server_stop') - """ - instance.eval(stop_pg_server_lua_code) - -def test_auth(cluster: Cluster): - cluster.deploy(instance_count=1) - i1 = cluster.instances[0] +def test_auth(postgres: Postgres): host = '127.0.0.1' - service = '36118' - start_pg_server(i1, host, service) + port = 5432 + + postgres.start(host, port) + i1 = postgres.instance user = 'user' password = 'fANPIOUWEh79p12hdunqwADI' @@ -45,16 +16,16 @@ def test_auth(cluster: Cluster): i1.call("pico.create_user", user, password, dict(timeout=3)) # test successful authentication - conn = pg.Connection(user, password=password, host=host, port=int(service)) + conn = pg.Connection(user, password=password, host=host, port=port) conn.close() # test authentication with a wrong password with pytest.raises(pg.DatabaseError, match=f"md5 authentication failed for user '{user}'"): - pg.Connection(user, password='wrong password', host=host, port=int(service)) + pg.Connection(user, password='wrong password', host=host, port=port) # test authentication with an unknown user with pytest.raises(pg.DatabaseError, match=f"authentication failed for user 'unknown-user'"): - pg.Connection("unknown-user", password='aaa', host=host, port=int(service)) + pg.Connection("unknown-user", password='aaa', host=host, port=port) sha_user = 'chap-sha-enjoyer' sha_password = '231321fnijphui217h08' @@ -63,4 +34,4 @@ def test_auth(cluster: Cluster): # test authentication with an unsupported method with pytest.raises(pg.DatabaseError, match=f"authentication failed for user '{sha_user}'"): - pg.Connection(sha_user, password='aaa', host=host, port=int(service)) + pg.Connection(sha_user, password='aaa', host=host, port=port) diff --git a/pgproto/test/conftest.py b/pgproto/test/conftest.py index cf7ec94023c83b34066dd4c7740e7ae0d4e10bdb..a7b3c46f2ec89f2ee140dd3c443b95e7f0a76034 100644 --- a/pgproto/test/conftest.py +++ b/pgproto/test/conftest.py @@ -1259,3 +1259,30 @@ def pgrep_tree(pid): return [pid, *subps] except subprocess.SubprocessError: return [pid] + + +@dataclass +class Postgres: + instance: Instance + + def install(self): + code = f""" + package.cpath="{os.environ['LUA_CPATH']}" + + box.schema.func.create('pgproto.server_start', {{ language = 'C' }}) + box.schema.user.grant('guest', 'execute', 'function', 'pgproto.server_start') + """ + self.instance.eval(code) + return self + + def start(self, host, port): + code = f""" + box.func['pgproto.server_start']:call({{ '{host}', '{port}' }}) + """ + self.instance.eval(code) + return self + + +@pytest.fixture +def postgres(instance: Instance): + return Postgres(instance).install() diff --git a/pgproto/test/simple_query_test.py b/pgproto/test/simple_query_test.py index 5e8606afaa15e8e2262067e6d6670868e3aea00a..cd1561c201b17d697914eb68ea1e046eae009251 100644 --- a/pgproto/test/simple_query_test.py +++ b/pgproto/test/simple_query_test.py @@ -1,39 +1,15 @@ import pytest -from conftest import Cluster import pg8000.dbapi as pg import os +from conftest import Postgres -def start_pg_server(instance, host, service): - start_pg_server_lua_code = f""" - package.cpath="{os.environ['LUA_CPATH']}" - - box.schema.func.create('pgproto.server_start', {{ language = 'C' }}) - box.schema.user.grant('guest', 'execute', 'function', 'pgproto.server_start') - - box.func['pgproto.server_start']:call({{ '{host}', '{service}' }}) - """ - instance.eval(start_pg_server_lua_code) - -def stop_pg_server(instance): - stop_pg_server_lua_code = f""" - box.schema.func.create('pgproto.server_stop', {{language = 'C'}}) - box.schema.user.grant('guest', 'execute', 'function', 'pgproto.server_stop') - - box.func['pgproto.server_stop']:call() - - box.schema.func.drop('pgproto.server_start') - box.schema.func.drop('pgproto.server_stop') - """ - instance.eval(stop_pg_server_lua_code) - - -def test_simple_query_flow_errors(cluster: Cluster): - cluster.deploy(instance_count=1) - i1 = cluster.instances[0] +def test_simple_query_flow_errors(postgres: Postgres): host = '127.0.0.1' - service = '35776' - start_pg_server(i1, host, service) + port = 5432 + + postgres.start(host, port) + i1 = postgres.instance user = 'admin' password = 'fANPIOUWEh79p12hdunqwADI' @@ -41,10 +17,10 @@ def test_simple_query_flow_errors(cluster: Cluster): i1.eval(f"box.schema.user.passwd('{user}', '{password}')") with pytest.raises(pg.InterfaceError, match="Server refuses SSL"): - pg.Connection(user, password=password, host=host, port=int(service), ssl_context=True) + pg.Connection(user, password=password, host=host, port=port, ssl_context=True) os.environ['PGSSLMODE'] = 'disable' - conn = pg.Connection(user, password=password, host=host, port=int(service)) + conn = pg.Connection(user, password=password, host=host, port=port) conn.autocommit = True cur = conn.cursor() @@ -58,13 +34,13 @@ def test_simple_query_flow_errors(cluster: Cluster): INSERT INTO book VALUES (1, 2); """) -def test_simple_flow_session(cluster: Cluster): - cluster.deploy(instance_count=1) - i1 = cluster.instances[0] +def test_simple_flow_session(postgres: Postgres): host = '127.0.0.1' - service = '5432' - start_pg_server(i1, host, service) + port = 5432 + + postgres.start(host, port) + i1 = postgres.instance user = 'admin' password = 'password' @@ -72,7 +48,7 @@ def test_simple_flow_session(cluster: Cluster): i1.eval(f"box.schema.user.passwd('{user}', '{password}')") os.environ['PGSSLMODE'] = 'disable' - conn = pg.Connection(user, password=password, host=host, port=int(service)) + conn = pg.Connection(user, password=password, host=host, port=port) conn.autocommit = True cur = conn.cursor() @@ -108,15 +84,13 @@ def test_simple_flow_session(cluster: Cluster): DROP TABLE "tall"; """) - stop_pg_server(i1) - -def test_explain(cluster: Cluster): - cluster.deploy(instance_count=1) - i1 = cluster.instances[0] +def test_explain(postgres: Postgres): host = '127.0.0.1' - service = '54321' - start_pg_server(i1, host, service) + port = 5432 + + postgres.start(host, port) + i1 = postgres.instance user = 'admin' password = 'password' @@ -124,7 +98,7 @@ def test_explain(cluster: Cluster): i1.eval(f"box.schema.user.passwd('{user}', '{password}')") os.environ['PGSSLMODE'] = 'disable' - conn = pg.Connection(user, password=password, host=host, port=int(service)) + conn = pg.Connection(user, password=password, host=host, port=port) conn.autocommit = True cur = conn.cursor() @@ -175,18 +149,15 @@ def test_explain(cluster: Cluster): cur.execute('drop table "explain";') - stop_pg_server(i1) - # Aggregates return value type is decimal, which is currently not supported, # so an error is expected -def test_aggregate_error(cluster: Cluster): - cluster.deploy(instance_count=1) - i1 = cluster.instances[0] - +def test_aggregate_error(postgres: Postgres): host = '127.0.0.1' - service = '54321' - start_pg_server(i1, host, service) + port = 5432 + + postgres.start(host, port) + i1 = postgres.instance user = 'admin' password = 'password' @@ -194,7 +165,7 @@ def test_aggregate_error(cluster: Cluster): i1.eval(f"box.schema.user.passwd('{user}', '{password}')") os.environ['PGSSLMODE'] = 'disable' - conn = pg.Connection(user, password=password, host=host, port=int(service)) + conn = pg.Connection(user, password=password, host=host, port=port) conn.autocommit = True cur = conn.cursor() @@ -219,5 +190,3 @@ def test_aggregate_error(cluster: Cluster): cur.execute(""" SELECT SUM("id") FROM "tall"; """) - - stop_pg_server(i1)