Skip to content

Commit 0528541

Browse files
author
v.shepard
committed
PBCKP-588 fixes after review - add ConnectionParams
1 parent 2e916df commit 0528541

File tree

9 files changed

+53
-59
lines changed

9 files changed

+53
-59
lines changed

testgres/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
First, \
4747
Any
4848

49-
from .operations.os_ops import OsOperations
49+
from .operations.os_ops import OsOperations, ConnectionParams
5050
from .operations.local_ops import LocalOperations
5151
from .operations.remote_ops import RemoteOperations
5252

@@ -60,5 +60,5 @@
6060
"PostgresNode", "NodeApp",
6161
"reserve_port", "release_port", "bound_ports", "get_bin_path", "get_pg_config", "get_pg_version",
6262
"First", "Any",
63-
"OsOperations", "LocalOperations", "RemoteOperations"
63+
"OsOperations", "LocalOperations", "RemoteOperations", "ConnectionParams"
6464
]

testgres/api.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ def get_new_node(name=None, base_dir=None, **kwargs):
3737
"""
3838
Simply a wrapper around :class:`.PostgresNode` constructor.
3939
See :meth:`.PostgresNode.__init__` for details.
40-
For remote connection you can add next parameters:
41-
host='127.0.0.1',
42-
hostname='localhost',
43-
ssh_key=None,
44-
username=default_username()
40+
For remote connection you can add the next parameter:
41+
conn_params = ConnectionParams(host='127.0.0.1',
42+
ssh_key=None,
43+
username=default_username())
4544
"""
4645
# NOTE: leave explicit 'name' and 'base_dir' for compatibility
4746
return PostgresNode(name=name, base_dir=base_dir, **kwargs)

testgres/backup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def spawn_primary(self, name=None, destroy=True):
139139

140140
# Build a new PostgresNode
141141
NodeClass = self.original_node.__class__
142-
with clean_on_error(NodeClass(name=name, base_dir=base_dir, os_ops=self.original_node.os_ops)) as node:
142+
with clean_on_error(NodeClass(name=name, base_dir=base_dir, conn_params=self.original_node.os_ops.conn_params)) as node:
143143

144144
# New nodes should always remove dir tree
145145
node._should_rm_dirs = True

testgres/node.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494

9595
from .backup import NodeBackup
9696

97+
from .operations.os_ops import ConnectionParams
9798
from .operations.local_ops import LocalOperations
9899
from .operations.remote_ops import RemoteOperations
99100

@@ -125,8 +126,7 @@ def __repr__(self):
125126

126127

127128
class PostgresNode(object):
128-
def __init__(self, name=None, port=None, base_dir=None,
129-
host='127.0.0.1', hostname='localhost', ssh_key=None, username=default_username(), os_ops=None):
129+
def __init__(self, name=None, port=None, base_dir=None, conn_params: ConnectionParams = ConnectionParams()):
130130
"""
131131
PostgresNode constructor.
132132
@@ -146,17 +146,14 @@ def __init__(self, name=None, port=None, base_dir=None,
146146
# basic
147147
self.name = name or generate_app_name()
148148

149-
if os_ops:
150-
self.os_ops = os_ops
151-
elif ssh_key:
152-
self.os_ops = RemoteOperations(host=host, hostname=hostname, ssh_key=ssh_key, username=username)
149+
if conn_params.ssh_key:
150+
self.os_ops = RemoteOperations(conn_params)
153151
else:
154-
self.os_ops = LocalOperations(host=host, hostname=hostname, username=username)
152+
self.os_ops = LocalOperations(conn_params)
155153

156-
self.port = self.os_ops.port or reserve_port()
154+
self.port = port or reserve_port()
157155

158156
self.host = self.os_ops.host
159-
self.hostname = self.os_ops.hostname
160157
self.ssh_key = self.os_ops.ssh_key
161158

162159
testgres_config.os_ops = self.os_ops
@@ -628,7 +625,7 @@ def status(self):
628625
status_code, out, err = execute_utility(_params, self.utils_log_file, verbose=True)
629626
if 'does not exist' in err:
630627
return NodeStatus.Uninitialized
631-
elif'no server running' in out:
628+
elif 'no server running' in out:
632629
return NodeStatus.Stopped
633630
return NodeStatus.Running
634631

testgres/operations/local_ops.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import psutil
99

10-
from testgres.exceptions import ExecUtilException
10+
from ..exceptions import ExecUtilException
1111

12-
from .os_ops import OsOperations
12+
from .os_ops import OsOperations, ConnectionParams
1313
from .os_ops import pglib
1414

1515
try:
@@ -21,13 +21,12 @@
2121

2222

2323
class LocalOperations(OsOperations):
24-
def __init__(self, host='127.0.0.1', hostname='localhost', port=None, username=None):
25-
super().__init__(username)
26-
self.host = host
27-
self.hostname = hostname
28-
self.port = port
24+
def __init__(self, conn_params: ConnectionParams = ConnectionParams()):
25+
super().__init__(conn_params.username)
26+
self.conn_params = conn_params
27+
self.host = conn_params.host
2928
self.ssh_key = None
30-
self.username = username or self.get_user()
29+
self.username = conn_params.username or self.get_user()
3130

3231
# Command execution
3332
def exec_command(self, cmd, wait_exit=False, verbose=False,

testgres/operations/os_ops.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
raise ImportError("You must have psycopg2 or pg8000 modules installed")
88

99

10+
class ConnectionParams:
11+
def __init__(self, host='127.0.0.1', ssh_key=None, username=None):
12+
self.host = host
13+
self.ssh_key = ssh_key
14+
self.username = username
15+
16+
1017
class OsOperations:
1118
def __init__(self, username=None):
12-
self.hostname = "localhost"
13-
self.remote = False
14-
self.ssh = None
19+
self.ssh_key = None
1520
self.username = username
1621

1722
# Command execution

testgres/operations/remote_ops.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import paramiko
99
from paramiko import SSHClient
1010

11-
from testgres.exceptions import ExecUtilException
11+
from ..exceptions import ExecUtilException
1212

13-
from .os_ops import OsOperations
13+
from .os_ops import OsOperations, ConnectionParams
1414
from .os_ops import pglib
1515

1616
sshtunnel.SSH_TIMEOUT = 5.0
@@ -37,15 +37,13 @@ def cmdline(self):
3737

3838

3939
class RemoteOperations(OsOperations):
40-
def __init__(self, host="127.0.0.1", hostname='localhost', port=None, ssh_key=None, username=None):
41-
super().__init__(username)
42-
self.host = host
43-
self.hostname = hostname
44-
self.port = port
45-
self.ssh_key = ssh_key
46-
self.remote = True
40+
def __init__(self, conn_params: ConnectionParams):
41+
super().__init__(conn_params.username)
42+
self.conn_params = conn_params
43+
self.host = conn_params.host
44+
self.ssh_key = conn_params.ssh_key
4745
self.ssh = self.ssh_connect()
48-
self.username = username or self.get_user()
46+
self.username = conn_params.username or self.get_user()
4947
self.tunnel = None
5048

5149
def __enter__(self):
@@ -70,14 +68,11 @@ def close_tunnel(self):
7068
time.sleep(0.5)
7169

7270
def ssh_connect(self) -> Optional[SSHClient]:
73-
if not self.remote:
74-
return None
75-
else:
76-
key = self._read_ssh_key()
77-
ssh = paramiko.SSHClient()
78-
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
79-
ssh.connect(self.host, username=self.username, pkey=key)
80-
return ssh
71+
key = self._read_ssh_key()
72+
ssh = paramiko.SSHClient()
73+
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
74+
ssh.connect(self.host, username=self.username, pkey=key)
75+
return ssh
8176

8277
def _read_ssh_key(self):
8378
try:

tests/test_remote.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22

33
from testgres import ExecUtilException
44
from testgres import RemoteOperations
5+
from testgres import ConnectionParams
56

67

78
class TestRemoteOperations:
89

910
@pytest.fixture(scope="function", autouse=True)
1011
def setup(self):
11-
self.operations = RemoteOperations(
12-
host="172.18.0.3",
13-
username="dev",
14-
ssh_key='../../container_files/postgres/ssh/id_ed25519'
15-
)
12+
conn_params = ConnectionParams(host="172.18.0.3",
13+
username="dev",
14+
ssh_key='../../container_files/postgres/ssh/id_ed25519')
15+
self.operations = RemoteOperations(conn_params)
1616

1717
yield
18-
1918
self.operations.__del__()
2019

2120
def test_exec_command_success(self):

tests/test_simple_remote.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050
# NOTE: those are ugly imports
5151
from testgres import bound_ports
5252
from testgres.utils import PgVer
53-
from testgres.node import ProcessProxy
53+
from testgres.node import ProcessProxy, ConnectionParams
5454

55-
56-
os_ops = RemoteOperations(host='172.18.0.3',
57-
username='dev',
58-
ssh_key='../../container_files/postgres/ssh/id_ed25519')
55+
conn_params = ConnectionParams(host="172.18.0.3",
56+
username="dev",
57+
ssh_key='../../container_files/postgres/ssh/id_ed25519')
58+
os_ops = RemoteOperations(conn_params)
5959
testgres_config.set_os_ops(os_ops=os_ops)
6060

6161

@@ -94,7 +94,7 @@ def removing(f):
9494

9595

9696
def get_remote_node(name=None):
97-
return get_new_node(name=name, host=os_ops.host, username=os_ops.username, ssh_key=os_ops.ssh_key)
97+
return get_new_node(name=name, conn_params=conn_params)
9898

9999

100100
class TestgresRemoteTests(unittest.TestCase):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy