33import gzip
44import unittest
55from .helpers .ptrack_helpers import ProbackupTest ,ProbackupException ,GdbException
6+ from .helpers .data_helpers import tail_file
67from datetime import datetime ,timedelta
78import subprocess
89from sys import exit
@@ -383,26 +384,31 @@ def test_archive_push_file_exists(self):
383384self .switch_wal_segment (node )
384385sleep (1 )
385386
386- with open (log_file ,'r' )as f :
387- log_content = f .read ()
387+ log = tail_file (log_file ,linetimeout = 30 ,totaltimeout = 120 ,
388+ collect = True )
389+ log .wait (contains = 'The failed archive command was' )
390+
388391self .assertIn (
389392'LOG: archive command failed with exit code 1' ,
390- log_content )
393+ log . content )
391394
392395self .assertIn (
393396'DETAIL: The failed archive command was:' ,
394- log_content )
397+ log . content )
395398
396399self .assertIn (
397400'pg_probackup archive-push WAL file' ,
398- log_content )
401+ log . content )
399402
400403self .assertIn (
401404'WAL file already exists in archive with different checksum' ,
402- log_content )
405+ log . content )
403406
404407self .assertNotIn (
405- 'pg_probackup archive-push completed successfully' ,log_content )
408+ 'pg_probackup archive-push completed successfully' ,log .content )
409+
410+ # btw check that console coloring codes are not slipped into log file
411+ self .assertNotIn ('[0m' ,log .content )
406412
407413if self .get_version (node )< 100000 :
408414wal_src = os .path .join (
@@ -419,19 +425,9 @@ def test_archive_push_file_exists(self):
419425shutil .copyfile (wal_src ,file )
420426
421427self .switch_wal_segment (node )
422- sleep (5 )
423-
424- with open (log_file ,'r' )as f :
425- log_content = f .read ()
426-
427- self .assertIn (
428- 'pg_probackup archive-push completed successfully' ,
429- log_content )
430-
431- # btw check that console coloring codes are not slipped into log file
432- self .assertNotIn ('[0m' ,log_content )
433428
434- print (log_content )
429+ log .stop_collect ()
430+ log .wait (contains = 'pg_probackup archive-push completed successfully' )
435431
436432# @unittest.skip("skip")
437433def test_archive_push_file_exists_overwrite (self ):
@@ -471,39 +467,35 @@ def test_archive_push_file_exists_overwrite(self):
471467self .switch_wal_segment (node )
472468sleep (1 )
473469
474- with open (log_file ,'r' ) as f :
475- log_content = f . read ( )
470+ log = tail_file (log_file ,linetimeout = 30 , collect = True )
471+ log . wait ( contains = 'The failed archive command was' )
476472
477473self .assertIn (
478- 'LOG: archive command failed with exit code 1' ,log_content )
474+ 'LOG: archive command failed with exit code 1' ,log . content )
479475self .assertIn (
480- 'DETAIL: The failed archive command was:' ,log_content )
476+ 'DETAIL: The failed archive command was:' ,log . content )
481477self .assertIn (
482- 'pg_probackup archive-push WAL file' ,log_content )
478+ 'pg_probackup archive-push WAL file' ,log . content )
483479self .assertNotIn (
484480'WAL file already exists in archive with '
485- 'different checksum, overwriting' ,log_content )
481+ 'different checksum, overwriting' ,log . content )
486482self .assertIn (
487483'WAL file already exists in archive with '
488- 'different checksum' ,log_content )
484+ 'different checksum' ,log . content )
489485
490486self .assertNotIn (
491- 'pg_probackup archive-push completed successfully' ,log_content )
487+ 'pg_probackup archive-push completed successfully' ,log . content )
492488
493489self .set_archiving (backup_dir ,'node' ,node ,overwrite = True )
494490node .reload ()
495491self .switch_wal_segment (node )
496- sleep (5 )
497492
498- with open (log_file ,'r' )as f :
499- log_content = f .read ()
500- self .assertTrue (
501- 'pg_probackup archive-push completed successfully' in log_content ,
502- 'Expecting messages about successfull execution archive_command' )
493+ log .drop_content ()
494+ log .wait (contains = 'pg_probackup archive-push completed successfully' )
503495
504496self .assertIn (
505497'WAL file already exists in archive with '
506- 'different checksum, overwriting' ,log_content )
498+ 'different checksum, overwriting' ,log . content )
507499
508500# @unittest.skip("skip")
509501def test_archive_push_partial_file_exists (self ):
@@ -2049,14 +2041,22 @@ def test_archive_push_sanity(self):
20492041replica .promote ()
20502042replica .pgbench_init (scale = 10 )
20512043
2052- with open (os .path .join (replica .logs_dir ,'postgresql.log' ),'r' )as f :
2053- replica_log_content = f .read ()
2044+ log = tail_file (os .path .join (replica .logs_dir ,'postgresql.log' ),
2045+ collect = True )
2046+ log .wait (regex = r"pushing file.*history" )
2047+ log .wait (contains = 'archive-push completed successfully' )
2048+ log .wait (regex = r"pushing file.*partial" )
2049+ log .wait (contains = 'archive-push completed successfully' )
20542050
20552051# make sure that .partial file is not compressed
2056- self .assertNotIn ('.partial.gz' ,replica_log_content )
2052+ self .assertNotIn ('.partial.gz' ,log . content )
20572053# make sure that .history file is not compressed
2058- self .assertNotIn ('.history.gz' ,replica_log_content )
2059- self .assertNotIn ('WARNING' ,replica_log_content )
2054+ self .assertNotIn ('.history.gz' ,log .content )
2055+
2056+ replica .stop ()
2057+ log .wait_shutdown ()
2058+
2059+ self .assertNotIn ('WARNING' ,log .content )
20602060
20612061output = self .show_archive (
20622062backup_dir ,'node' ,as_json = False ,as_text = True ,
@@ -2440,18 +2440,11 @@ def test_archive_get_prefetch_corruption(self):
24402440os .remove (os .path .join (replica .logs_dir ,'postgresql.log' ))
24412441replica .slow_start (replica = True )
24422442
2443- sleep (60 )
2444-
2445- with open (os .path .join (replica .logs_dir ,'postgresql.log' ),'r' )as f :
2446- postgres_log_content = f .read ()
2447-
2448- self .assertIn (
2449- 'Prefetched WAL segment {0} is invalid, cannot use it' .format (filename ),
2450- postgres_log_content )
2451-
2452- self .assertIn (
2453- 'LOG: restored log file "{0}" from archive' .format (filename ),
2454- postgres_log_content )
2443+ prefetch_line = 'Prefetched WAL segment {0} is invalid, cannot use it' .format (filename )
2444+ restored_line = 'LOG: restored log file "{0}" from archive' .format (filename )
2445+ tailer = tail_file (os .path .join (replica .logs_dir ,'postgresql.log' ))
2446+ tailer .wait (contains = prefetch_line )
2447+ tailer .wait (contains = restored_line )
24552448
24562449# @unittest.skip("skip")
24572450def test_archive_show_partial_files_handling (self ):