1- #coding: utf-8
1+ # coding: utf-8
22"""
33testgres.py
44 Postgres testing utility
4949
5050
5151class ClusterException (Exception ):
52+
5253 """
5354 Predefined exceptions
5455 """
5556 pass
5657
5758
5859class QueryException (Exception ):
60+
5961 """
6062 Predefined exceptions
6163 """
6264 pass
6365
6466
6567class NodeConnection (object ):
68+
6669 """
6770 Transaction wrapper returned by Node
6871 """
72+
6973 def __init__ (self , parent_node , dbname ):
7074 self .parent_node = parent_node
7175
@@ -93,7 +97,7 @@ def begin(self, isolation_level=0):
9397 print (type (isolation_level ))
9498 # Check if level is int [0..3]
9599 if (isinstance (isolation_level , int ) and
96- isolation_level in range (0 , 4 )):
100+ isolation_level in range (0 , 4 )):
97101
98102 # Replace index with isolation level type
99103 isolation_level = levels [isolation_level ]
@@ -130,6 +134,7 @@ def close(self):
130134
131135
132136class PostgresNode (object ):
137+
133138 def __init__ (self , name , port ):
134139 self .name = name
135140 self .host = '127.0.0.1'
@@ -140,19 +145,19 @@ def __init__(self, name, port):
140145
141146 @property
142147 def data_dir (self ):
143- return "%s/data" % self .base_dir
148+ return os . path . join ( self .base_dir , "data" )
144149
145150 @property
146151 def logs_dir (self ):
147- return "%s/logs" % self .base_dir
152+ return os . path . join ( self .base_dir , "logs" )
148153
149154 @property
150155 def output_filename (self ):
151- return "%s/ stdout.log" % self . logs_dir
156+ return os . path . join ( self . logs_dir , " stdout.log")
152157
153158 @property
154159 def error_filename (self ):
155- return "%s/ stderr.log" % self . logs_dir
160+ return os . path . join ( self . logs_dir , " stderr.log")
156161
157162 @property
158163 def connstr (self ):
@@ -165,7 +170,7 @@ def get_bin_path(self, filename):
165170 if "BINDIR" not in pg_config :
166171 return filename
167172 else :
168- return "%s/%s" % (pg_config .get ("BINDIR" ), filename )
173+ return os . path . join (pg_config .get ("BINDIR" ), filename )
169174
170175 def init (self , allows_streaming = False ):
171176 """ Performs initdb """
@@ -174,7 +179,7 @@ def init(self, allows_streaming=False):
174179 os .makedirs (self .data_dir )
175180 initdb = self .get_bin_path ("initdb" )
176181 with open (self .output_filename , "a" ) as file_out , \
177- open (self .error_filename , "a" ) as file_err :
182+ open (self .error_filename , "a" ) as file_err :
178183 ret = subprocess .call (
179184 [initdb , self .data_dir , "-N" ],
180185 stdout = file_out ,
@@ -184,8 +189,8 @@ def init(self, allows_streaming=False):
184189 raise ClusterException ("Cluster initialization failed" )
185190
186191 # add parameters to config file
187- config_name = "%s/ postgresql.conf" % self . data_dir
188- with open (config_name , "a" ) as conf :
192+ postgres_conf = os . path . join ( self . data_dir , " postgresql.conf")
193+ with open (postgres_conf , "a" ) as conf :
189194 conf .write (
190195 "fsync = off\n "
191196 "log_statement = all\n "
@@ -218,7 +223,7 @@ def init_from_backup(self, root_node, backup_name, has_streaming=False, hba_perm
218223 """Initializes cluster from backup, made by another node"""
219224
220225 # Copy data from backup
221- backup_path = "%s/%s" % (root_node .base_dir , backup_name )
226+ backup_path = os . path . join (root_node .base_dir , backup_name )
222227 shutil .copytree (backup_path , self .data_dir )
223228 os .chmod (self .data_dir , 0o0700 )
224229
@@ -234,14 +239,14 @@ def init_from_backup(self, root_node, backup_name, has_streaming=False, hba_perm
234239 self .enable_streaming (root_node )
235240
236241 def set_replication_conf (self ):
237- hba_conf = "%s/ pg_hba.conf" % self . data_dir
242+ hba_conf = os . path . join ( self . data_dir , " pg_hba.conf")
238243 with open (hba_conf , "a" ) as conf :
239244 conf .write ("local replication all trust\n " )
240245 # conf.write("host replication all 127.0.0.1/32 trust\n")
241246
242247 def enable_streaming (self , root_node ):
243- config_name = "%s/ recovery.conf" % self . data_dir
244- with open (config_name , "a" ) as conf :
248+ recovery_conf = os . path . join ( self . data_dir , " recovery.conf")
249+ with open (recovery_conf , "a" ) as conf :
245250 conf .write (
246251 "primary_conninfo='%s application_name=%s'\n "
247252 "standby_mode=on\n "
@@ -273,7 +278,7 @@ def pg_ctl(self, command, params):
273278 arguments .append (value )
274279
275280 with open (self .output_filename , "a" ) as file_out , \
276- open (self .error_filename , "a" ) as file_err :
281+ open (self .error_filename , "a" ) as file_err :
277282
278283 res = subprocess .call (
279284 arguments + [command ],
@@ -287,7 +292,7 @@ def pg_ctl(self, command, params):
287292
288293 def start (self ):
289294 """ Starts cluster """
290- logfile = self .logs_dir + "/ postgresql.log"
295+ logfile = os . path . join ( self .logs_dir , " postgresql.log")
291296 params = {
292297 "-D" : self .data_dir ,
293298 "-w" : None ,
@@ -381,7 +386,7 @@ def safe_psql(self, dbname, query):
381386
382387 def dump (self , dbname , filename ):
383388 """Invoke pg_dump and exports database to a file as an sql script"""
384- path = self .base_dir + "/" + filename
389+ path = os . path . join ( self .base_dir , filename )
385390 params = [
386391 self .get_bin_path ("pg_dump" ),
387392 "-p %s" % self .port ,
@@ -405,7 +410,7 @@ def restore(self, dbname, filename, node=None):
405410 if not node :
406411 node = self
407412
408- path = node .base_dir + "/" + filename
413+ path = os . path . join ( node .base_dir , filename )
409414 self .psql (dbname , filename = path )
410415
411416 def poll_query_until (self , dbname , query ):
@@ -431,11 +436,11 @@ def execute(self, dbname, query):
431436 def backup (self , name ):
432437 """Performs pg_basebackup"""
433438 pg_basebackup = self .get_bin_path ("pg_basebackup" )
434- backup_path = self .base_dir + "/" + name
439+ backup_path = os . path . join ( self .base_dir , name )
435440 os .makedirs (backup_path )
436441 params = [pg_basebackup , "-D" , backup_path , "-p %s" % self .port , "-x" ]
437442 with open (self .output_filename , "a" ) as file_out , \
438- open (self .error_filename , "a" ) as file_err :
443+ open (self .error_filename , "a" ) as file_err :
439444 ret = subprocess .call (
440445 params ,
441446 stdout = file_out ,
@@ -462,7 +467,8 @@ def get_config():
462467 pg_config_cmd = os .environ .get ("PG_CONFIG" ) \
463468 if "PG_CONFIG" in os .environ else "pg_config"
464469
465- out = six .StringIO (subprocess .check_output ([pg_config_cmd ], universal_newlines = True ))
470+ out = six .StringIO (
471+ subprocess .check_output ([pg_config_cmd ], universal_newlines = True ))
466472 for line in out :
467473 if line and "=" in line :
468474 key , value = line .split ("=" , 1 )
0 commit comments