Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 94d7572

Browse files
[#240] Using of node.psql with other host and port (#242)
* [FIX] Tests include testgres by right way through import When we do not have root __init__.py tests must to import testgres through "import testgres" not through "from <relative_path> import testgres" * [#240] Using of node.psql with other host and port This patch adds the support of using other host and port in the following methods: - PostgresNode.psql (explicit new args: host and port) - PostgresNode.safe_psql (indirectly through **kwargs) It allows to run psql utility from one PostgreSQL instance to work with another one. If explicit host and port are not defined (are None), PostgresNode will use own ones. This patch closes #240.
1 parent 1a662f1 commit 94d7572

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

testgres/node.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,8 @@ def psql(self,
13721372
dbname=None,
13731373
username=None,
13741374
input=None,
1375+
host: typing.Optional[str] = None,
1376+
port: typing.Optional[int] = None,
13751377
**variables):
13761378
"""
13771379
Execute a query using psql.
@@ -1382,6 +1384,8 @@ def psql(self,
13821384
dbname: database name to connect to.
13831385
username: database user name.
13841386
input: raw input to be passed.
1387+
host: an explicit host of server.
1388+
port: an explicit port of server.
13851389
**variables: vars to be set before execution.
13861390
13871391
Returns:
@@ -1393,13 +1397,19 @@ def psql(self,
13931397
>>> psql(query='select 3', ON_ERROR_STOP=1)
13941398
"""
13951399

1400+
assert host is None or type(host) == str # noqa: E721
1401+
assert port is None or type(port) == int # noqa: E721
1402+
assert type(variables) == dict # noqa: E721
1403+
13961404
return self._psql(
13971405
ignore_errors=True,
13981406
query=query,
13991407
filename=filename,
14001408
dbname=dbname,
14011409
username=username,
14021410
input=input,
1411+
host=host,
1412+
port=port,
14031413
**variables
14041414
)
14051415

@@ -1411,7 +1421,11 @@ def _psql(
14111421
dbname=None,
14121422
username=None,
14131423
input=None,
1424+
host: typing.Optional[str] = None,
1425+
port: typing.Optional[int] = None,
14141426
**variables):
1427+
assert host is None or type(host) == str # noqa: E721
1428+
assert port is None or type(port) == int # noqa: E721
14151429
assert type(variables) == dict # noqa: E721
14161430

14171431
#
@@ -1424,10 +1438,21 @@ def _psql(
14241438
else:
14251439
raise Exception("Input data must be None or bytes.")
14261440

1441+
if host is None:
1442+
host = self.host
1443+
1444+
if port is None:
1445+
port = self.port
1446+
1447+
assert host is not None
1448+
assert port is not None
1449+
assert type(host) == str # noqa: E721
1450+
assert type(port) == int # noqa: E721
1451+
14271452
psql_params = [
14281453
self._get_bin_path("psql"),
1429-
"-p", str(self.port),
1430-
"-h", self.host,
1454+
"-p", str(port),
1455+
"-h", host,
14311456
"-U", username or self.os_ops.username,
14321457
"-d", dbname or default_dbname(),
14331458
"-X", # no .psqlrc

tests/test_testgres_common.py

+83
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,89 @@ def test_psql(self, node_svc: PostgresNodeService):
678678
r = node.safe_psql('select 1') # raises!
679679
logging.error("node.safe_psql returns [{}]".format(r))
680680

681+
def test_psql__another_port(self, node_svc: PostgresNodeService):
682+
assert isinstance(node_svc, PostgresNodeService)
683+
with __class__.helper__get_node(node_svc).init() as node1:
684+
with __class__.helper__get_node(node_svc).init() as node2:
685+
node1.start()
686+
node2.start()
687+
assert node1.port != node2.port
688+
assert node1.host == node2.host
689+
690+
node1.stop()
691+
692+
logging.info("test table in node2 is creating ...")
693+
node2.safe_psql(
694+
dbname="postgres",
695+
query="create table test (id integer);"
696+
)
697+
698+
logging.info("try to find test table through node1.psql ...")
699+
res = node1.psql(
700+
dbname="postgres",
701+
query="select count(*) from pg_class where relname='test'",
702+
host=node2.host,
703+
port=node2.port,
704+
)
705+
assert (__class__.helper__rm_carriage_returns(res) == (0, b'1\n', b''))
706+
707+
def test_psql__another_bad_host(self, node_svc: PostgresNodeService):
708+
assert isinstance(node_svc, PostgresNodeService)
709+
with __class__.helper__get_node(node_svc).init() as node:
710+
logging.info("try to execute node1.psql ...")
711+
res = node.psql(
712+
dbname="postgres",
713+
query="select count(*) from pg_class where relname='test'",
714+
host="DUMMY_HOST_NAME",
715+
port=node.port,
716+
)
717+
718+
res2 = __class__.helper__rm_carriage_returns(res)
719+
720+
assert res2[0] != 0
721+
assert b"DUMMY_HOST_NAME" in res[2]
722+
723+
def test_safe_psql__another_port(self, node_svc: PostgresNodeService):
724+
assert isinstance(node_svc, PostgresNodeService)
725+
with __class__.helper__get_node(node_svc).init() as node1:
726+
with __class__.helper__get_node(node_svc).init() as node2:
727+
node1.start()
728+
node2.start()
729+
assert node1.port != node2.port
730+
assert node1.host == node2.host
731+
732+
node1.stop()
733+
734+
logging.info("test table in node2 is creating ...")
735+
node2.safe_psql(
736+
dbname="postgres",
737+
query="create table test (id integer);"
738+
)
739+
740+
logging.info("try to find test table through node1.psql ...")
741+
res = node1.safe_psql(
742+
dbname="postgres",
743+
query="select count(*) from pg_class where relname='test'",
744+
host=node2.host,
745+
port=node2.port,
746+
)
747+
assert (__class__.helper__rm_carriage_returns(res) == b'1\n')
748+
749+
def test_safe_psql__another_bad_host(self, node_svc: PostgresNodeService):
750+
assert isinstance(node_svc, PostgresNodeService)
751+
with __class__.helper__get_node(node_svc).init() as node:
752+
logging.info("try to execute node1.psql ...")
753+
754+
with pytest.raises(expected_exception=Exception) as x:
755+
node.safe_psql(
756+
dbname="postgres",
757+
query="select count(*) from pg_class where relname='test'",
758+
host="DUMMY_HOST_NAME",
759+
port=node.port,
760+
)
761+
762+
assert "DUMMY_HOST_NAME" in str(x.value)
763+
681764
def test_safe_psql__expect_error(self, node_svc: PostgresNodeService):
682765
assert isinstance(node_svc, PostgresNodeService)
683766
with __class__.helper__get_node(node_svc).init().start() as node:

0 commit comments

Comments
 (0)