22from os import path ,listdir
33import six
44from .pb_lib import ProbackupTest
5+ from datetime import datetime ,timedelta
56from testgres import stop_all
67import subprocess
78
@@ -18,9 +19,9 @@ def tearDownClass(cls):
1819except :
1920pass
2021
21- def test_validate_broke_wal_1 (self ):
22+ def test_validate_wal_1 (self ):
2223"""recovery to latest from full backup"""
23- node = self .make_bnode ('test_validate_broke_wal_1 ' ,base_dir = "tmp_dirs/validate/broke_wal_1 " )
24+ node = self .make_bnode ('test_validate_wal_1 ' ,base_dir = "tmp_dirs/validate/wal_1 " )
2425node .start ()
2526self .assertEqual (self .init_pb (node ),six .b ("" ))
2627node .pgbench_init (scale = 2 )
@@ -40,6 +41,9 @@ def test_validate_broke_wal_1(self):
4041pgbench .wait ()
4142pgbench .stdout .close ()
4243
44+ # Save time to validate
45+ target_time = datetime .now ()
46+
4347target_xid = None
4448with node .connect ("postgres" )as con :
4549res = con .execute ("INSERT INTO tbl0005 VALUES ('inserted') RETURNING (xmin)" )
@@ -49,13 +53,42 @@ def test_validate_broke_wal_1(self):
4953node .execute ("postgres" ,"SELECT pg_switch_xlog()" )
5054node .stop ({"-m" :"immediate" })
5155
56+ id_backup = self .show_pb (node )[0 ].id
57+
58+ # Validate to real time
59+ self .assertIn (six .b ("INFO: Backup validation stopped on" ),
60+ self .validate_pb (node ,options = ["--time='{:%Y-%m-%d %H:%M:%S}'" .format (
61+ target_time )]))
62+
63+ # Validate to unreal time
64+ self .assertIn (six .b ("ERROR: no full backup found, cannot validate." ),
65+ self .validate_pb (node ,options = ["--time='{:%Y-%m-%d %H:%M:%S}'" .format (
66+ target_time - timedelta (days = 2 ))]))
67+
68+ # Validate to unreal time #2
69+ self .assertIn (six .b ("ERROR: there are no WAL records to time" ),
70+ self .validate_pb (node ,options = ["--time='{:%Y-%m-%d %H:%M:%S}'" .format (
71+ target_time + timedelta (days = 2 ))]))
72+
73+ # Validate to real xid
74+ self .assertIn (six .b ("INFO: Backup validation stopped on" ),
75+ self .validate_pb (node ,options = ["--xid=%s" % target_xid ]))
76+
77+ # Validate to unreal xid
78+ self .assertIn (six .b ("ERROR: there are no WAL records to xid" ),
79+ self .validate_pb (node ,options = ["--xid=%d" % (int (target_xid )+ 1000 )]))
80+
81+ # Validate with backup ID
82+ self .assertIn (six .b ("INFO: Backup validation stopped on" ),
83+ self .validate_pb (node ,id_backup ))
84+
85+ # Validate broken WAL
5286wals_dir = path .join (self .backup_dir (node ),"wal" )
5387wals = [f for f in listdir (wals_dir )if path .isfile (path .join (wals_dir ,f ))]
5488wals .sort ()
5589with open (path .join (wals_dir ,wals [- 3 ]),"rb+" )as f :
5690f .seek (256 )
5791f .write (six .b ("blablabla" ))
5892
59- id_backup = self .show_pb (node )[0 ].id
6093res = self .validate_pb (node ,id_backup ,options = ['--xid=%s' % target_xid ])
61- self .assertIn (six .b ("there arenot WAL records to xid" ),res )
94+ self .assertIn (six .b ("there areno WAL records to xid" ),res )