1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2025-07-14 06:54:15 +02:00

validate rework, self.cmd and self.output added

This commit is contained in:
Grigory Smolkin
2017-05-25 12:53:33 +03:00
parent 777ab09f29
commit b1e849a588
14 changed files with 230 additions and 92 deletions

View File

@ -1,11 +1,13 @@
import unittest
import os
import six
from .ptrack_helpers import ProbackupTest, ProbackupException
from helpers.ptrack_helpers import ProbackupTest, ProbackupException
#from helpers.expected_errors import satisfying_full_backup_not_found, wal_corruption
from datetime import datetime, timedelta
from testgres import stop_all
import subprocess
from sys import exit
import re
class ValidateTest(ProbackupTest, unittest.TestCase):
@ -17,10 +19,14 @@ class ValidateTest(ProbackupTest, unittest.TestCase):
def tearDownClass(cls):
stop_all()
#@unittest.skip("skip")
#@unittest.expectedFailure
# @unittest.skip("skip")
# @unittest.expectedFailure
def test_validate_wal_unreal_values(self):
"""recovery to latest from full backup"""
"""
make node with archiving
make archive backup
validate to both real and unreal values
"""
fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="tmp_dirs/validate/{0}".format(fname),
set_archiving=True,
@ -35,7 +41,7 @@ class ValidateTest(ProbackupTest, unittest.TestCase):
con.execute("CREATE TABLE tbl0005 (a text)")
con.commit()
self.backup_pb(node)
backup_id = self.backup_pb(node)
node.pgbench_init(scale=2)
pgbench = node.pgbench(
@ -47,32 +53,32 @@ class ValidateTest(ProbackupTest, unittest.TestCase):
pgbench.wait()
pgbench.stdout.close()
backup_id = self.show_pb(node)[0]['ID']
target_time = self.show_pb(node)[0]['Recovery time']
after_backup_time = datetime.now()
target_time = self.show_pb(node, id=backup_id)['recovery-time']
after_backup_time = datetime.now().replace(second=0, microsecond=0)
# Validate to real time
self.assertIn(six.b("INFO: backup validation completed successfully on"),
self.validate_pb(node, options=["--time='{0}'".format(target_time)]))
# Validate to unreal time
unreal_time_1 = after_backup_time - timedelta(days=2)
try:
self.validate_pb(node, options=["--time='{:%Y-%m-%d %H:%M:%S}'".format(
after_backup_time - timedelta(days=2))])
# we should die here because exception is what we expect to happen
self.assertEqual(1, 0, "Error in validation is expected because of validation of unreal time")
self.validate_pb(node, options=["--time='{0}'".format(unreal_time_1)])
self.assertEqual(1, 0, "Expecting Error because of validation to unreal time.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
except ProbackupException, e:
self.assertEqual(
e.message,
'ERROR: Full backup satisfying target options is not found.\n')
self.assertEqual(e.message, 'ERROR: Full backup satisfying target options is not found.\n',
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(repr(e.message), self.cmd))
# Validate to unreal time #2
unreal_time_2 = after_backup_time + timedelta(days=2)
try:
self.validate_pb(node, options=["--time='{:%Y-%m-%d %H:%M:%S}'".format(
after_backup_time + timedelta(days=2))])
self.assertEqual(1, 0, "Error in validation is expected because of validation unreal time")
self.validate_pb(node, options=["--time='{0}'".format(unreal_time_2)])
self.assertEqual(1, 0, "Expecting Error because of validation to unreal time.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
except ProbackupException, e:
self.assertEqual(True, 'ERROR: not enough WAL records to time' in e.message)
self.assertTrue(re.match('WARNING: recovery can be done up to time [0-9-: ]+ and xid \d+\nERROR: not enough WAL records to time {0}\n\Z'.format(unreal_time_2), e.message),
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(repr(e.message), self.cmd))
# Validate to real xid
target_xid = None
@ -83,21 +89,31 @@ class ValidateTest(ProbackupTest, unittest.TestCase):
node.execute("postgres", "SELECT pg_switch_xlog()")
self.assertIn(six.b("INFO: backup validation completed successfully on"),
self.validate_pb(node, options=["--xid=%s" % target_xid]))
self.validate_pb(node, options=["--xid={0}".format(target_xid)]))
# Validate to unreal xid
unreal_xid = int(target_xid) + 1000
try:
self.validate_pb(node, options=["--xid=%d" % (int(target_xid) + 1000)])
self.assertEqual(1, 0, "Error in validation is expected because of validation of unreal xid")
self.validate_pb(node, options=["--xid={0}".format(unreal_xid)])
self.assertEqual(1, 0, "Expecting Error because of validation to unreal xid.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
except ProbackupException, e:
self.assertEqual(True, 'ERROR: not enough WAL records to xid' in e.message)
self.assertTrue(re.match('WARNING: recovery can be done up to time [0-9-: ]+ and xid \d+\nERROR: not enough WAL records to xid \d+\n\Z', e.message),
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(repr(e.message), self.cmd))
# Validate with backup ID
self.assertIn(six.b("INFO: backup validation completed successfully on"),
self.validate_pb(node, backup_id))
def test_validate_broken_wal_1(self):
"""recovery to latest from full backup"""
# @unittest.skip("skip")
def test_validate_corrupt_wal_1(self):
"""
make node with archiving
make archive backup
corrupt all wal files
run validate, expecting error because of wal corruption
make sure that backup status is 'CORRUPT'
"""
fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="tmp_dirs/validate/{0}".format(fname),
set_archiving=True,
@ -126,16 +142,25 @@ class ValidateTest(ProbackupTest, unittest.TestCase):
# Simple validate
try:
self.validate_pb(node)
self.assertEqual(1, 0, "Expecting Error because of wal corruption. THIS IS BAD")
self.assertEqual(1, 0, "Expecting Error because of wal segments corruption.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
except ProbackupException, e:
self.assertEqual(True, 'Possible WAL CORRUPTION' in e.message)
#TODO assert correct error message
self.assertTrue(re.match('Possible WAL CORRUPTION\Z', e.message),
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(repr(e.message), self.cmd))
self.assertEqual('CORRUPT', self.show_pb(node, id=backup_id)['status'], 'Backup STATUS should be "CORRUPT"')
node.stop()
def test_validate_broken_wal_2(self):
"""recovery to latest from full backup"""
# @unittest.skip("skip")
def test_validate_corrupt_wal_2(self):
"""
make node with archiving
make archive backup
corrupt all wal files
run validate to real xid, expecting error because of wal corruption
make sure that backup status is 'CORRUPT'
"""
fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="tmp_dirs/validate/{0}".format(fname),
set_archiving=True,
@ -167,19 +192,28 @@ class ValidateTest(ProbackupTest, unittest.TestCase):
f.write(six.b("blablabla"))
f.close
# Simple validate
# Validate to xid
try:
self.validate_pb(node, backup_id, options=['--xid=%s' % target_xid])
self.assertEqual(1, 0, "Expecting Error because of wal corruption. THIS IS BAD")
self.validate_pb(node, backup_id, options=['--xid={0}'.format(target_xid)])
self.assertEqual(1, 0, "Expecting Error because of wal segments corruption.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
except ProbackupException, e:
self.assertEqual(True, 'Possible WAL CORRUPTION' in e.message)
#TODO assert correct error message
self.assertTrue(re.match('Possible WAL CORRUPTION\Z', e.message),
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(repr(e.message), self.cmd))
self.assertEqual('CORRUPT', self.show_pb(node, id=backup_id)['status'], 'Backup STATUS should be "CORRUPT"')
node.stop()
@unittest.skip("skip")
# @unittest.skip("skip")
def test_validate_wal_lost_segment_1(self):
"""Loose segment which belong to some backup"""
"""
make node with archiving
make archive backup
delete from archive wal segment which belong to previous backup
run validate, expecting error because of missing wal segment
make sure that backup status is 'CORRUPT'
"""
fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="tmp_dirs/validate/{0}".format(fname),
set_archiving=True,
@ -202,18 +236,38 @@ class ValidateTest(ProbackupTest, unittest.TestCase):
# Delete wal segment
wals_dir = os.path.join(self.backup_dir(node), "wal")
wals = [f for f in os.listdir(wals_dir) if os.path.isfile(os.path.join(wals_dir, f)) and not f.endswith('.backup')]
os.remove(os.path.join(self.backup_dir(node), "wal", wals[1]))
file = os.path.join(self.backup_dir(node), "wal", wals[1])
os.remove(file)
try:
self.validate_pb(node)
self.assertEqual(1, 0, "Expecting Error because of wal segment disappearance")
self.assertEqual(1, 0, "Expecting Error because of wal segment disappearance.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
except ProbackupException, e:
self.assertEqual(True, 'is absent' in e.message)
self.assertTrue(re.match('WARNING: WAL segment "{0}" is absent\nERROR: there are not enough WAL records to restore from [0-9a-fA-F\/]+ to [0-9a-fA-F\/]+\n\Z'.format(
file), e.message),
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(repr(e.message), self.cmd))
self.assertEqual('CORRUPT', self.show_pb(node, id=backup_id)['status'], 'Backup STATUS should be "CORRUPT"')
self.assertEqual('CORRUPT', self.show_pb(node, id=backup_id)['status'], 'Backup {0} should have STATUS "CORRUPT"')
# Be paranoid and run validate again
try:
self.validate_pb(node)
self.assertEqual(1, 0, "Expecting Error because of backup corruption.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
except ProbackupException, e:
self.assertTrue(re.match('ERROR: Backup {0} has status: CORRUPT\n\Z'.format(backup_id), e.message),
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(repr(e.message), self.cmd))
node.stop()
# @unittest.skip("skip")
def test_validate_wal_lost_segment_2(self):
"""Loose segment located between backups"""
"""
make node with archiving
make archive backup
delete from archive wal segment which DO NOT belong to previous backup
run validate, expecting error because of missing wal segment
make sure that backup status is 'ERROR'
"""
fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="tmp_dirs/validate/{0}".format(fname),
set_archiving=True,
@ -239,12 +293,17 @@ class ValidateTest(ProbackupTest, unittest.TestCase):
wals_dir = os.path.join(self.backup_dir(node), "wal")
wals = [f for f in os.listdir(wals_dir) if os.path.isfile(os.path.join(wals_dir, f)) and not f.endswith('.backup')]
wals = map(int, wals)
os.remove(os.path.join(self.backup_dir(node), "wal", '0000000' + str(max(wals))))
file = os.path.join(self.backup_dir(node), "wal", '0000000' + str(max(wals)))
os.remove(file)
# Need more accurate error message about loosing wal segment between backups
try:
self.backup_pb(node, backup_type='page')
self.assertEqual(1, 0, "Expecting Error in PAGE backup because of wal segment disappearance")
backup_id = self.backup_pb(node, backup_type='page')
self.assertEqual(1, 0, "Expecting Error because of wal segment disappearance.\n Output: {0} \n CMD: {1}".format(
self.output, self.cmd))
except ProbackupException, e:
self.assertEqual(True, 'is absent' in e.message)
self.assertTrue(re.match('INFO: wait for LSN [0-9a-fA-F\/]+ in archived WAL segment .*\nWARNING: could not read WAL record at [0-9a-fA-F\/]+\nERROR: WAL segment "{0}" is absent\n\Z'.format(
file), e.message),
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(repr(e.message), self.cmd))
self.assertEqual('ERROR', self.show_pb(node)[1]['Status'], 'Backup {0} should have STATUS "ERROR"')
node.stop()