Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0c26f77

Browse files
author
v.shepard
committed
PBCKP-588 fix failed tests - psql, set_auto_conf
1 parent190d084 commit0c26f77

File tree

5 files changed

+82
-70
lines changed

5 files changed

+82
-70
lines changed

‎testgres/node.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
importos
44
importrandom
55
importsignal
6+
importsubprocess
67
importthreading
78
fromqueueimportQueue
89

@@ -714,14 +715,13 @@ def start(self, params=[], wait=True):
714715
exit_status,out,error=execute_utility(_params,self.utils_log_file,verbose=True)
715716
if'does not exist'inerror:
716717
raiseException
717-
if'server started'inout:
718-
self.is_started=True
719718
exceptExceptionase:
720719
msg='Cannot start node'
721720
files=self._collect_special_files()
722721
raise_from(StartNodeException(msg,files),e)
723722

724723
self._maybe_start_logger()
724+
self.is_started=True
725725
returnself
726726

727727
defstop(self,params=[],wait=True):
@@ -958,19 +958,31 @@ def psql(self,
958958

959959
# select query source
960960
ifquery:
961-
psql_params.extend(("-c",'"{}"'.format(query)))
961+
ifself.os_ops.remote:
962+
psql_params.extend(("-c",'"{}"'.format(query)))
963+
else:
964+
psql_params.extend(("-c",query))
962965
eliffilename:
963966
psql_params.extend(("-f",filename))
964967
else:
965968
raiseQueryException('Query or filename must be provided')
966969

967970
# should be the last one
968971
psql_params.append(dbname)
972+
ifnotself.os_ops.remote:
973+
# start psql process
974+
process=subprocess.Popen(psql_params,
975+
stdin=subprocess.PIPE,
976+
stdout=subprocess.PIPE,
977+
stderr=subprocess.PIPE)
978+
979+
# wait until it finishes and get stdout and stderr
980+
out,err=process.communicate(input=input)
981+
returnprocess.returncode,out,err
982+
else:
983+
status_code,out,err=self.os_ops.exec_command(psql_params,verbose=True,input=input)
969984

970-
# start psql process
971-
status_code,out,err=self.os_ops.exec_command(psql_params,verbose=True,input=input)
972-
973-
returnstatus_code,out,err
985+
returnstatus_code,out,err
974986

975987
@method_decorator(positional_args_hack(['dbname','query']))
976988
defsafe_psql(self,query=None,expect_error=False,**kwargs):
@@ -1002,9 +1014,9 @@ def safe_psql(self, query=None, expect_error=False, **kwargs):
10021014
err=e.message
10031015
ifret:
10041016
ifexpect_error:
1005-
out=errorb''
1017+
out=(errorb'').decode('utf-8')
10061018
else:
1007-
raiseQueryException(errorb'',query)
1019+
raiseQueryException((errorb'').decode('utf-8'),query)
10081020
elifexpect_error:
10091021
assertFalse,"Exception was expected, but query finished successfully: `{}` ".format(query)
10101022

@@ -1529,18 +1541,18 @@ def set_auto_conf(self, options, config='postgresql.auto.conf', rm_options={}):
15291541
Defaults to an empty set.
15301542
"""
15311543
# parse postgresql.auto.conf
1532-
auto_conf_file=os.path.join(self.data_dir,config)
1533-
raw_content=self.os_ops.read(auto_conf_file)
1544+
path=os.path.join(self.data_dir,config)
15341545

1546+
lines=self.os_ops.readlines(path)
15351547
current_options= {}
15361548
current_directives= []
1537-
forlineinraw_content.splitlines():
1549+
forlineinlines:
15381550

15391551
# ignore comments
15401552
ifline.startswith('#'):
15411553
continue
15421554

1543-
ifline=='':
1555+
ifline.strip()=='':
15441556
continue
15451557

15461558
ifline.startswith('include'):
@@ -1570,7 +1582,7 @@ def set_auto_conf(self, options, config='postgresql.auto.conf', rm_options={}):
15701582
fordirectiveincurrent_directives:
15711583
auto_conf+=directive+"\n"
15721584

1573-
self.os_ops.write(auto_conf_file,auto_conf)
1585+
self.os_ops.write(path,auto_conf,truncate=True)
15741586

15751587

15761588
classNodeApp:

‎testgres/operations/local_ops.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
importgetpass
22
importos
33
importshutil
4+
importstat
45
importsubprocess
56
importtempfile
67
fromshutilimportrmtree
78

89
importpsutil
910

1011
from ..exceptionsimportExecUtilException
11-
12-
from .os_opsimportOsOperations,ConnectionParams
12+
from .os_opsimportConnectionParams,OsOperations
1313
from .os_opsimportpglib
1414

1515
try:
@@ -18,20 +18,24 @@
1818
fromdistutils.spawnimportfind_executable
1919

2020
CMD_TIMEOUT_SEC=60
21+
error_markers= [b'error',b'Permission denied',b'fatal']
2122

2223

2324
classLocalOperations(OsOperations):
24-
def__init__(self,conn_params:ConnectionParams=ConnectionParams()):
25-
super().__init__(conn_params.username)
25+
def__init__(self,conn_params=None):
26+
ifconn_paramsisNone:
27+
conn_params=ConnectionParams()
28+
super(LocalOperations,self).__init__(conn_params.username)
2629
self.conn_params=conn_params
2730
self.host=conn_params.host
2831
self.ssh_key=None
32+
self.remote=False
2933
self.username=conn_params.usernameorself.get_user()
3034

3135
# Command execution
3236
defexec_command(self,cmd,wait_exit=False,verbose=False,
33-
expect_error=False,encoding=None,shell=True,text=False,
34-
input=None,stdout=subprocess.PIPE,stderr=subprocess.PIPE,proc=None):
37+
expect_error=False,encoding=None,shell=False,text=False,
38+
input=None,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,proc=None):
3539
"""
3640
Execute a command in a subprocess.
3741
@@ -49,9 +53,6 @@ def exec_command(self, cmd, wait_exit=False, verbose=False,
4953
- proc: The process to use for subprocess creation.
5054
:return: The output of the subprocess.
5155
"""
52-
ifisinstance(cmd,list):
53-
cmd=' '.join(item.decode('utf-8')ifisinstance(item,bytes)elseitemforitemincmd)
54-
5556
ifos.name=='nt':
5657
withtempfile.NamedTemporaryFile()asbuf:
5758
process=subprocess.Popen(cmd,stdout=buf,stderr=subprocess.STDOUT)
@@ -71,7 +72,7 @@ def exec_command(self, cmd, wait_exit=False, verbose=False,
7172
result,error=process.communicate(input)
7273
exit_status=process.returncode
7374

74-
found_error="error"inerror.decode(encodingor'utf-8').lower()
75+
error_found=exit_status!=0orany(markerinerrorformarkerinerror_markers)
7576

7677
ifencoding:
7778
result=result.decode(encoding)
@@ -80,7 +81,7 @@ def exec_command(self, cmd, wait_exit=False, verbose=False,
8081
ifexpect_error:
8182
raiseException(result,error)
8283

83-
ifexit_status!=0orfound_error:
84+
ifexit_status!=0orerror_found:
8485
ifexit_status==0:
8586
exit_status=1
8687
raiseExecUtilException(message='Utility exited with non-zero code. Error `{}`'.format(error),
@@ -101,7 +102,7 @@ def find_executable(self, executable):
101102

102103
defis_executable(self,file):
103104
# Check if the file is executable
104-
returnos.access(file,os.X_OK)
105+
returnos.stat(file).st_mode&stat.S_IXUSR
105106

106107
defset_env(self,var_name,var_val):
107108
# Check if the directory is already in PATH
@@ -116,9 +117,12 @@ def get_name(self):
116117

117118
# Work with dirs
118119
defmakedirs(self,path,remove_existing=False):
119-
ifremove_existingandos.path.exists(path):
120-
shutil.rmtree(path)
121-
os.makedirs(path,exist_ok=True)
120+
ifremove_existing:
121+
shutil.rmtree(path,ignore_errors=True)
122+
try:
123+
os.makedirs(path)
124+
exceptFileExistsError:
125+
pass
122126

123127
defrmdirs(self,path,ignore_errors=True):
124128
returnrmtree(path,ignore_errors=ignore_errors)
@@ -141,7 +145,7 @@ def pathsep(self):
141145
returnpathsep
142146

143147
defmkdtemp(self,prefix=None):
144-
returntempfile.mkdtemp(prefix=prefix)
148+
returntempfile.mkdtemp(prefix='{}'.format(prefix))
145149

146150
defmkstemp(self,prefix=None):
147151
fd,filename=tempfile.mkstemp(prefix=prefix)

‎testgres/operations/remote_ops.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
sshtunnel.TUNNEL_TIMEOUT=5.0
1818

1919

20-
error_markers= [b'error',b'Permission denied']
20+
error_markers= [b'error',b'Permission denied',b'fatal']
2121

2222

2323
classPsUtilProcessProxy:
@@ -43,6 +43,7 @@ def __init__(self, conn_params: ConnectionParams):
4343
self.host=conn_params.host
4444
self.ssh_key=conn_params.ssh_key
4545
self.ssh=self.ssh_connect()
46+
self.remote=True
4647
self.username=conn_params.usernameorself.get_user()
4748
self.tunnel=None
4849

@@ -89,7 +90,7 @@ def _read_ssh_key(self):
8990
ExecUtilException(message="An error occurred while reading the ssh key: {}".format(e))
9091

9192
defexec_command(self,cmd:str,wait_exit=False,verbose=False,expect_error=False,
92-
encoding=None,shell=True,text=False,input=None,stdout=None,
93+
encoding=None,shell=True,text=False,input=None,stdin=None,stdout=None,
9394
stderr=None,proc=None):
9495
"""
9596
Execute a command in the SSH session.
@@ -131,7 +132,11 @@ def exec_command(self, cmd: str, wait_exit=False, verbose=False, expect_error=Fa
131132
iferror_found:
132133
ifexit_status==0:
133134
exit_status=1
134-
raiseExecUtilException(message="Utility exited with non-zero code. Error: {}".format(error.decode(encodingor'utf-8')),
135+
ifencoding:
136+
message="Utility exited with non-zero code. Error: {}".format(error.decode(encoding))
137+
else:
138+
message=b"Utility exited with non-zero code. Error: "+error
139+
raiseExecUtilException(message=message,
135140
command=cmd,
136141
exit_code=exit_status,
137142
out=result)
@@ -429,7 +434,7 @@ def db_connect(self, dbname, user, password=None, host="127.0.0.1", port=5432, s
429434
conn=pglib.connect(
430435
host=host,# change to 'localhost' because we're connecting through a local ssh tunnel
431436
port=self.tunnel.local_bind_port,# use the local bind port set up by the tunnel
432-
dbname=dbname,
437+
database=dbname,
433438
user=userorself.username,
434439
password=password
435440
)

‎tests/test_remote.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
importos
2+
13
importpytest
24

35
fromtestgresimportExecUtilException
@@ -9,9 +11,10 @@ class TestRemoteOperations:
911

1012
@pytest.fixture(scope="function",autouse=True)
1113
defsetup(self):
12-
conn_params=ConnectionParams(host="172.18.0.3",
13-
username="dev",
14-
ssh_key='../../container_files/postgres/ssh/id_ed25519')
14+
conn_params=ConnectionParams(host=os.getenv('RDBMS_TESTPOOL1_HOST')or'172.18.0.3',
15+
username='dev',
16+
ssh_key=os.getenv(
17+
'RDBMS_TESTPOOL_SSHKEY')or'../../container_files/postgres/ssh/id_ed25519')
1518
self.operations=RemoteOperations(conn_params)
1619

1720
yield
@@ -35,7 +38,7 @@ def test_exec_command_failure(self):
3538
exit_status,result,error=self.operations.exec_command(cmd,verbose=True,wait_exit=True)
3639
exceptExecUtilExceptionase:
3740
error=e.message
38-
asserterror=='Utility exited with non-zero code. Error: bash: line 1: nonexistent_command: command not found\n'
41+
asserterror==b'Utility exited with non-zero code. Error: bash: line 1: nonexistent_command: command not found\n'
3942

4043
deftest_is_executable_true(self):
4144
"""
@@ -62,7 +65,7 @@ def test_makedirs_and_rmdirs_success(self):
6265
cmd="pwd"
6366
pwd=self.operations.exec_command(cmd,wait_exit=True,encoding='utf-8').strip()
6467

65-
path=f"{pwd}/test_dir"
68+
path="{}/test_dir".format(pwd)
6669

6770
# Test makedirs
6871
self.operations.makedirs(path)
@@ -88,7 +91,7 @@ def test_makedirs_and_rmdirs_failure(self):
8891
exit_status,result,error=self.operations.rmdirs(path,verbose=True)
8992
exceptExecUtilExceptionase:
9093
error=e.message
91-
asserterror=="Utility exited with non-zero code. Error: rm: cannot remove '/root/test_dir': Permission denied\n"
94+
asserterror==b"Utility exited with non-zero code. Error: rm: cannot remove '/root/test_dir': Permission denied\n"
9295

9396
deftest_listdir(self):
9497
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp