3
3
import gzip
4
4
import unittest
5
5
from .helpers .ptrack_helpers import ProbackupTest ,ProbackupException ,GdbException
6
+ from .helpers .data_helpers import tail_file
6
7
from datetime import datetime ,timedelta
7
8
import subprocess
8
9
from sys import exit
@@ -383,26 +384,31 @@ def test_archive_push_file_exists(self):
383
384
self .switch_wal_segment (node )
384
385
sleep (1 )
385
386
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
+
388
391
self .assertIn (
389
392
'LOG: archive command failed with exit code 1' ,
390
- log_content )
393
+ log . content )
391
394
392
395
self .assertIn (
393
396
'DETAIL: The failed archive command was:' ,
394
- log_content )
397
+ log . content )
395
398
396
399
self .assertIn (
397
400
'pg_probackup archive-push WAL file' ,
398
- log_content )
401
+ log . content )
399
402
400
403
self .assertIn (
401
404
'WAL file already exists in archive with different checksum' ,
402
- log_content )
405
+ log . content )
403
406
404
407
self .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 )
406
412
407
413
if self .get_version (node )< 100000 :
408
414
wal_src = os .path .join (
@@ -419,19 +425,9 @@ def test_archive_push_file_exists(self):
419
425
shutil .copyfile (wal_src ,file )
420
426
421
427
self .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 )
433
428
434
- print (log_content )
429
+ log .stop_collect ()
430
+ log .wait (contains = 'pg_probackup archive-push completed successfully' )
435
431
436
432
# @unittest.skip("skip")
437
433
def test_archive_push_file_exists_overwrite (self ):
@@ -471,39 +467,35 @@ def test_archive_push_file_exists_overwrite(self):
471
467
self .switch_wal_segment (node )
472
468
sleep (1 )
473
469
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' )
476
472
477
473
self .assertIn (
478
- 'LOG: archive command failed with exit code 1' ,log_content )
474
+ 'LOG: archive command failed with exit code 1' ,log . content )
479
475
self .assertIn (
480
- 'DETAIL: The failed archive command was:' ,log_content )
476
+ 'DETAIL: The failed archive command was:' ,log . content )
481
477
self .assertIn (
482
- 'pg_probackup archive-push WAL file' ,log_content )
478
+ 'pg_probackup archive-push WAL file' ,log . content )
483
479
self .assertNotIn (
484
480
'WAL file already exists in archive with '
485
- 'different checksum, overwriting' ,log_content )
481
+ 'different checksum, overwriting' ,log . content )
486
482
self .assertIn (
487
483
'WAL file already exists in archive with '
488
- 'different checksum' ,log_content )
484
+ 'different checksum' ,log . content )
489
485
490
486
self .assertNotIn (
491
- 'pg_probackup archive-push completed successfully' ,log_content )
487
+ 'pg_probackup archive-push completed successfully' ,log . content )
492
488
493
489
self .set_archiving (backup_dir ,'node' ,node ,overwrite = True )
494
490
node .reload ()
495
491
self .switch_wal_segment (node )
496
- sleep (5 )
497
492
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' )
503
495
504
496
self .assertIn (
505
497
'WAL file already exists in archive with '
506
- 'different checksum, overwriting' ,log_content )
498
+ 'different checksum, overwriting' ,log . content )
507
499
508
500
# @unittest.skip("skip")
509
501
def test_archive_push_partial_file_exists (self ):
@@ -2049,14 +2041,22 @@ def test_archive_push_sanity(self):
2049
2041
replica .promote ()
2050
2042
replica .pgbench_init (scale = 10 )
2051
2043
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' )
2054
2050
2055
2051
# make sure that .partial file is not compressed
2056
- self .assertNotIn ('.partial.gz' ,replica_log_content )
2052
+ self .assertNotIn ('.partial.gz' ,log . content )
2057
2053
# 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 )
2060
2060
2061
2061
output = self .show_archive (
2062
2062
backup_dir ,'node' ,as_json = False ,as_text = True ,
@@ -2440,18 +2440,11 @@ def test_archive_get_prefetch_corruption(self):
2440
2440
os .remove (os .path .join (replica .logs_dir ,'postgresql.log' ))
2441
2441
replica .slow_start (replica = True )
2442
2442
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 )
2455
2448
2456
2449
# @unittest.skip("skip")
2457
2450
def test_archive_show_partial_files_handling (self ):