2
2
from os import path ,listdir
3
3
import six
4
4
from .pb_lib import ProbackupTest
5
+ from datetime import datetime ,timedelta
5
6
from testgres import stop_all
6
7
import subprocess
7
8
@@ -18,9 +19,9 @@ def tearDownClass(cls):
18
19
except :
19
20
pass
20
21
21
- def test_validate_broke_wal_1 (self ):
22
+ def test_validate_wal_1 (self ):
22
23
"""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 " )
24
25
node .start ()
25
26
self .assertEqual (self .init_pb (node ),six .b ("" ))
26
27
node .pgbench_init (scale = 2 )
@@ -40,6 +41,9 @@ def test_validate_broke_wal_1(self):
40
41
pgbench .wait ()
41
42
pgbench .stdout .close ()
42
43
44
+ # Save time to validate
45
+ target_time = datetime .now ()
46
+
43
47
target_xid = None
44
48
with node .connect ("postgres" )as con :
45
49
res = con .execute ("INSERT INTO tbl0005 VALUES ('inserted') RETURNING (xmin)" )
@@ -49,13 +53,42 @@ def test_validate_broke_wal_1(self):
49
53
node .execute ("postgres" ,"SELECT pg_switch_xlog()" )
50
54
node .stop ({"-m" :"immediate" })
51
55
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
52
86
wals_dir = path .join (self .backup_dir (node ),"wal" )
53
87
wals = [f for f in listdir (wals_dir )if path .isfile (path .join (wals_dir ,f ))]
54
88
wals .sort ()
55
89
with open (path .join (wals_dir ,wals [- 3 ]),"rb+" )as f :
56
90
f .seek (256 )
57
91
f .write (six .b ("blablabla" ))
58
92
59
- id_backup = self .show_pb (node )[0 ].id
60
93
res = 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 )