1111import signal
1212
1313from .helpers .ptrack_helpers import ProbackupTest , ProbackupException
14- from testgres import StartNodeException , configure_testgres
14+ from testgres import StartNodeException
1515
1616module_name = 'auth_test'
1717skip_test = False
@@ -77,7 +77,7 @@ def tearDownClass(cls):
7777
7878 @unittest .skipIf (skip_test , "Module pexpect isn't installed. You need to install it." )
7979 def setUp (self ):
80- self .cmd = [self . pb . probackup_path , 'backup' ,
80+ self .cmd = ['backup' ,
8181 '-B' , self .backup_dir ,
8282 '--instance' , self .node .name ,
8383 '-h' , '127.0.0.1' ,
@@ -94,7 +94,7 @@ def test_empty_password(self):
9494 try :
9595 self .assertIn ("ERROR: no password supplied" ,
9696 "" .join (map (lambda x : x .decode ("utf-8" ),
97- run_pb_with_auth (self .cmd , '\0 \r \n ' ))
97+ self . run_pb_with_auth (self .cmd , '\0 \r \n ' ))
9898 )
9999 )
100100 except (TIMEOUT , ExceptionPexpect ) as e :
@@ -105,7 +105,7 @@ def test_wrong_password(self):
105105 try :
106106 self .assertIn ("password authentication failed" ,
107107 "" .join (map (lambda x : x .decode ("utf-8" ),
108- run_pb_with_auth (self .cmd , 'wrong_password\r \n ' ))
108+ self . run_pb_with_auth (self .cmd , 'wrong_password\r \n ' ))
109109 )
110110 )
111111 except (TIMEOUT , ExceptionPexpect ) as e :
@@ -116,7 +116,7 @@ def test_right_password(self):
116116 try :
117117 self .assertIn ("completed" ,
118118 "" .join (map (lambda x : x .decode ("utf-8" ),
119- run_pb_with_auth (self .cmd , 'password\r \n ' ))
119+ self . run_pb_with_auth (self .cmd , 'password\r \n ' ))
120120 )
121121 )
122122 except (TIMEOUT , ExceptionPexpect ) as e :
@@ -125,7 +125,7 @@ def test_right_password(self):
125125 def test_ctrl_c_event (self ):
126126 """ Test case: PGPB_AUTH02 - send interrupt signal """
127127 try :
128- run_pb_with_auth (self .cmd , kill = True )
128+ self . run_pb_with_auth (self .cmd , kill = True )
129129 except TIMEOUT :
130130 self .fail ("Error: CTRL+C event ignored" )
131131
@@ -170,6 +170,21 @@ def test_pgpassword(self):
170170 except ProbackupException as e :
171171 self .fail (e )
172172
173+ def run_pb_with_auth (self , password = None , kill = False ):
174+ try :
175+ with spawn (" " .join (self .pb .probackup_path + self .cmd ), timeout = 10 ) as probackup :
176+ result = probackup .expect ("Password for user .*:" , 5 )
177+ if kill :
178+ probackup .kill (signal .SIGINT )
179+ elif result == 0 :
180+ probackup .sendline (password )
181+ return probackup .readlines ()
182+ else :
183+ raise TIMEOUT ("" )
184+ except TIMEOUT :
185+ raise TIMEOUT ("Timeout error." )
186+ except ExceptionPexpect :
187+ raise ExceptionPexpect ("Pexpect error." )
173188
174189def modify_pg_hba (node ):
175190 """
@@ -184,33 +199,6 @@ def modify_pg_hba(node):
184199 fio .seek (0 )
185200 fio .write ('host\t all\t postgres\t 127.0.0.1/0\t trust\n ' + data )
186201
187-
188- def run_pb_with_auth (cmd , password = None , kill = False ):
189- """
190- Description:
191- Runnig pg_probackup utility in interactive and send a password or the kill signal.
192- :param cmd:
193- :param password:
194- :param kill:
195- :return stdout:
196- :raises pexpect.TIMEOUT, pexpect.ExceptionPexpect:
197- """
198- try :
199- with spawn (" " .join (cmd ), timeout = 10 ) as probackup :
200- result = probackup .expect ("Password for user .*:" , 5 )
201- if kill :
202- probackup .kill (signal .SIGINT )
203- elif result == 0 :
204- probackup .sendline (password )
205- return probackup .readlines ()
206- else :
207- raise TIMEOUT ("" )
208- except TIMEOUT :
209- raise TIMEOUT ("Timeout error." )
210- except ExceptionPexpect :
211- raise ExceptionPexpect ("Pexpect error." )
212-
213-
214202def create_pgpass (path , line ):
215203 with open (path , 'w' ) as passfile :
216204 # host:port:db:username:password
0 commit comments