1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2024-12-11 11:41:33 +02:00
pg_probackup/tests/backup_test.py

281 lines
12 KiB
Python
Raw Normal View History

import unittest
2017-06-20 12:57:23 +02:00
import os
from time import sleep
2017-06-27 07:42:52 +02:00
from .helpers.ptrack_helpers import ProbackupTest, ProbackupException
2017-07-12 16:28:28 +02:00
module_name = 'backup'
2017-07-12 16:28:28 +02:00
class BackupTest(ProbackupTest, unittest.TestCase):
2017-05-03 13:14:48 +02:00
# @unittest.skip("skip")
# @unittest.expectedFailure
2017-06-07 16:52:07 +02:00
# PGPRO-707
2017-05-03 13:14:48 +02:00
def test_backup_modes_archive(self):
"""standart backup modes with ARCHIVE WAL method"""
fname = self.id().split('.')[3]
2017-07-12 16:28:28 +02:00
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname),
2017-05-03 13:14:48 +02:00
initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'}
)
2017-07-12 16:28:28 +02:00
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2017-06-20 12:57:23 +02:00
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node)
2017-05-03 13:14:48 +02:00
node.start()
# full backup mode
#with open(path.join(node.logs_dir, "backup_full.log"), "wb") as backup_log:
2017-06-20 12:57:23 +02:00
# backup_log.write(self.backup_node(node, options=["--verbose"]))
2017-06-20 12:57:23 +02:00
backup_id = self.backup_node(backup_dir, 'node', node)
show_backup = self.show_pb(backup_dir, 'node')[0]
2017-05-03 13:14:48 +02:00
2017-06-27 07:42:52 +02:00
self.assertEqual(show_backup['Status'], "OK")
self.assertEqual(show_backup['Mode'], "FULL")
2017-05-03 13:14:48 +02:00
# postmaster.pid and postmaster.opts shouldn't be copied
excluded = True
2017-06-20 12:57:23 +02:00
db_dir = os.path.join(backup_dir, "backups", 'node', backup_id, "database")
for f in os.listdir(db_dir):
if os.path.isfile(os.path.join(db_dir, f)) \
and (f == "postmaster.pid" or f == "postmaster.opts"):
2017-05-03 13:14:48 +02:00
excluded = False
self.assertEqual(excluded, True)
# page backup mode
2017-06-20 12:57:23 +02:00
page_backup_id = self.backup_node(backup_dir, 'node', node, backup_type="page")
2017-05-03 13:14:48 +02:00
2017-05-03 17:05:19 +02:00
# print self.show_pb(node)
2017-06-20 12:57:23 +02:00
show_backup = self.show_pb(backup_dir, 'node')[1]
2017-06-27 07:42:52 +02:00
self.assertEqual(show_backup['Status'], "OK")
self.assertEqual(show_backup['Mode'], "PAGE")
2017-05-03 13:14:48 +02:00
# Check parent backup
self.assertEqual(
2017-06-20 12:57:23 +02:00
backup_id,
self.show_pb(backup_dir, 'node', backup_id=show_backup['ID'])["parent-backup-id"])
2017-05-03 13:14:48 +02:00
# ptrack backup mode
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'node', node, backup_type="ptrack")
2017-05-03 13:14:48 +02:00
2017-06-20 12:57:23 +02:00
show_backup = self.show_pb(backup_dir, 'node')[2]
2017-06-27 07:42:52 +02:00
self.assertEqual(show_backup['Status'], "OK")
self.assertEqual(show_backup['Mode'], "PTRACK")
2017-05-03 13:14:48 +02:00
2017-06-20 12:57:23 +02:00
# Check parent backup
self.assertEqual(
page_backup_id,
self.show_pb(backup_dir, 'node', backup_id=show_backup['ID'])["parent-backup-id"])
2017-06-27 07:42:52 +02:00
# Clean after yourself
2017-07-12 16:28:28 +02:00
self.del_test_dir(module_name, fname)
2017-05-03 13:14:48 +02:00
2017-06-20 12:57:23 +02:00
# @unittest.skip("skip")
2017-05-03 13:14:48 +02:00
def test_smooth_checkpoint(self):
"""full backup with smooth checkpoint"""
fname = self.id().split('.')[3]
2017-07-12 16:28:28 +02:00
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname),
2017-05-03 13:14:48 +02:00
initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica'}
)
2017-07-12 16:28:28 +02:00
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2017-06-20 12:57:23 +02:00
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node)
node.start()
self.backup_node(backup_dir, 'node' ,node, options=["-C"])
2017-06-27 07:42:52 +02:00
self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK")
2017-06-20 12:57:23 +02:00
node.stop()
2017-06-27 07:42:52 +02:00
# Clean after yourself
2017-07-12 16:28:28 +02:00
self.del_test_dir(module_name, fname)
2017-06-27 07:42:52 +02:00
# @unittest.skip("skip")
2017-06-20 12:57:23 +02:00
def test_incremental_backup_without_full(self):
"""page-level backup without validated full backup"""
fname = self.id().split('.')[3]
2017-07-12 16:28:28 +02:00
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname),
2017-06-20 12:57:23 +02:00
initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'}
)
2017-07-12 16:28:28 +02:00
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2017-06-20 12:57:23 +02:00
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node)
2017-05-03 13:14:48 +02:00
node.start()
2017-06-20 12:57:23 +02:00
try:
self.backup_node(backup_dir, 'node', node, backup_type="page")
# we should die here because exception is what we expect to happen
self.assertEqual(1, 0, "Expecting Error because page backup should not be possible without valid full backup.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
2017-06-27 07:42:52 +02:00
except ProbackupException as e:
2017-12-13 10:15:42 +02:00
self.assertIn(
"ERROR: Valid backup on current timeline is not found. Create new FULL backup before an incremental one.",
e.message,
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd))
2017-05-03 13:14:48 +02:00
2017-06-20 12:57:23 +02:00
sleep(1)
2017-05-03 13:14:48 +02:00
2017-06-20 12:57:23 +02:00
try:
self.backup_node(backup_dir, 'node', node, backup_type="ptrack")
# we should die here because exception is what we expect to happen
self.assertEqual(1, 0, "Expecting Error because page backup should not be possible without valid full backup.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
2017-06-27 07:42:52 +02:00
except ProbackupException as e:
2017-12-13 10:15:42 +02:00
self.assertIn(
"ERROR: Valid backup on current timeline is not found. Create new FULL backup before an incremental one.",
e.message,
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd))
2017-06-20 12:57:23 +02:00
2017-06-27 07:42:52 +02:00
self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "ERROR")
# Clean after yourself
2017-07-12 16:28:28 +02:00
self.del_test_dir(module_name, fname)
2017-05-03 13:14:48 +02:00
# @unittest.skip("skip")
2017-06-20 12:57:23 +02:00
def test_incremental_backup_corrupt_full(self):
"""page-level backup with corrupted full backup"""
2017-05-03 13:14:48 +02:00
fname = self.id().split('.')[3]
2017-07-12 16:28:28 +02:00
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname),
2017-05-03 13:14:48 +02:00
initdb_params=['--data-checksums'],
2017-06-20 12:57:23 +02:00
pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'}
2017-05-03 13:14:48 +02:00
)
2017-07-12 16:28:28 +02:00
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2017-06-20 12:57:23 +02:00
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node)
2017-05-03 13:14:48 +02:00
node.start()
2017-06-20 12:57:23 +02:00
backup_id = self.backup_node(backup_dir, 'node', node)
2017-06-27 07:42:52 +02:00
file = os.path.join(backup_dir, "backups", "node", backup_id, "database", "postgresql.conf")
2017-06-20 12:57:23 +02:00
os.remove(file)
2017-05-03 13:14:48 +02:00
try:
2017-06-27 07:42:52 +02:00
self.validate_pb(backup_dir, 'node')
2017-06-20 12:57:23 +02:00
# we should die here because exception is what we expect to happen
2017-06-27 07:42:52 +02:00
self.assertEqual(1, 0, "Expecting Error because of validation of corrupted backup.\n Output: {0} \n CMD: {1}".format(
2017-06-20 12:57:23 +02:00
repr(self.output), self.cmd))
2017-06-27 07:42:52 +02:00
except ProbackupException as e:
self.assertTrue("INFO: Validate backups of the instance 'node'\n" in e.message
and 'WARNING: Backup file "{0}" is not found\n'.format(file) in e.message
and "WARNING: Backup {0} is corrupted\n".format(backup_id) in e.message
and "INFO: Some backups are not valid\n" in e.message,
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd))
2017-06-20 12:57:23 +02:00
2017-06-27 07:42:52 +02:00
try:
self.backup_node(backup_dir, 'node', node, backup_type="page")
# we should die here because exception is what we expect to happen
2017-06-20 12:57:23 +02:00
self.assertEqual(1, 0, "Expecting Error because page backup should not be possible without valid full backup.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
2017-06-27 07:42:52 +02:00
except ProbackupException as e:
2017-12-13 10:15:42 +02:00
self.assertIn(
"ERROR: Valid backup on current timeline is not found. Create new FULL backup before an incremental one.",
e.message,
2017-06-27 07:42:52 +02:00
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd))
2017-05-03 13:14:48 +02:00
2017-06-27 07:42:52 +02:00
# sleep(1)
self.assertEqual(self.show_pb(backup_dir, 'node', backup_id)['status'], "CORRUPT")
self.assertEqual(self.show_pb(backup_dir, 'node')[1]['Status'], "ERROR")
# Clean after yourself
2017-07-12 16:28:28 +02:00
self.del_test_dir(module_name, fname)
2017-05-03 13:14:48 +02:00
2017-06-20 12:57:23 +02:00
# @unittest.skip("skip")
2017-05-03 13:14:48 +02:00
def test_ptrack_threads(self):
"""ptrack multi thread backup mode"""
2017-05-15 01:43:05 +02:00
fname = self.id().split('.')[3]
2017-07-12 16:28:28 +02:00
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname),
2017-05-15 01:43:05 +02:00
initdb_params=['--data-checksums'],
2017-06-20 12:57:23 +02:00
pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'}
2017-05-15 01:43:05 +02:00
)
2017-07-12 16:28:28 +02:00
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2017-06-20 12:57:23 +02:00
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node)
2017-05-03 13:14:48 +02:00
node.start()
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'node', node, backup_type="full", options=["-j", "4"])
2017-06-27 07:42:52 +02:00
self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK")
2017-05-03 13:14:48 +02:00
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'node', node, backup_type="ptrack", options=["-j", "4"])
2017-06-27 07:42:52 +02:00
self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK")
2017-05-03 13:14:48 +02:00
2017-06-27 07:42:52 +02:00
# Clean after yourself
2017-07-12 16:28:28 +02:00
self.del_test_dir(module_name, fname)
2017-05-03 13:14:48 +02:00
2017-06-20 12:57:23 +02:00
# @unittest.skip("skip")
2017-05-03 13:14:48 +02:00
def test_ptrack_threads_stream(self):
"""ptrack multi thread backup mode and stream"""
fname = self.id().split('.')[3]
2017-07-12 16:28:28 +02:00
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname),
2017-05-03 13:14:48 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica', 'ptrack_enable': 'on', 'max_wal_senders': '2'}
)
2017-07-12 16:28:28 +02:00
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
2017-06-20 12:57:23 +02:00
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
2017-05-03 13:14:48 +02:00
node.start()
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'node', node, backup_type="full", options=["-j", "4", "--stream"])
2017-05-03 13:14:48 +02:00
2017-06-27 07:42:52 +02:00
self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK")
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'node', node, backup_type="ptrack", options=["-j", "4", "--stream"])
2017-06-27 07:42:52 +02:00
self.assertEqual(self.show_pb(backup_dir, 'node')[1]['Status'], "OK")
# Clean after yourself
2017-07-12 16:28:28 +02:00
self.del_test_dir(module_name, fname)
# @unittest.skip("skip")
def test_page_checksumm_fail(self):
"""make node, corrupt some page, check that backup failed"""
fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname),
set_replication=True,
initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica', 'max_wal_senders': '2'}
)
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node)
node.start()
self.backup_node(backup_dir, 'node', node, backup_type="full", options=["-j", "4", "--stream"])
node.safe_psql(
"postgres",
"create table t_heap as select 1 as id, md5(i::text) as text, md5(repeat(i::text,10))::tsvector as tsvector from generate_series(0,1000) i")
node.safe_psql(
"postgres",
"CHECKPOINT;")
heap_path = node.safe_psql("postgres", "select pg_relation_filepath('t_heap')").rstrip()
node.stop()
with open(os.path.join(node.data_dir,heap_path), "rb+", 0) as f:
f.seek(9000)
f.write(b"bla")
f.flush()
f.close
node.start()
try:
self.backup_node(backup_dir, 'node', node, backup_type="full", options=["-j", "4", "--stream"])
# we should die here because exception is what we expect to happen
self.assertEqual(1, 0, "Expecting Error because of page corruption in PostgreSQL instance.\n Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
except ProbackupException as e:
self.assertIn("ERROR: File", e.message,
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd))
self.assertIn("blknum", e.message,
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd))
self.assertIn("have wrong checksum", e.message,
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd))
# Clean after yourself
self.del_test_dir(module_name, fname)