1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2024-12-03 09:59:53 +02:00

tests: minor fixes, backup_test PEP8

This commit is contained in:
Grigory Smolkin 2018-01-17 21:15:49 +03:00
parent d6a2d36403
commit fb1eb3cd20

View File

@ -15,9 +15,12 @@ class BackupTest(ProbackupTest, unittest.TestCase):
def test_backup_modes_archive(self): def test_backup_modes_archive(self):
"""standart backup modes with ARCHIVE WAL method""" """standart backup modes with ARCHIVE WAL method"""
fname = self.id().split('.')[3] fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname), node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
initdb_params=['--data-checksums'], initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'} pg_options={
'wal_level': 'replica',
'ptrack_enable': 'on'}
) )
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup') backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
self.init_pb(backup_dir) self.init_pb(backup_dir)
@ -26,7 +29,7 @@ class BackupTest(ProbackupTest, unittest.TestCase):
node.start() node.start()
# full backup mode # full backup mode
#with open(path.join(node.logs_dir, "backup_full.log"), "wb") as backup_log: # with open(path.join(node.logs_dir, "backup_full.log"), "wb") as backup_log:
# backup_log.write(self.backup_node(node, options=["--verbose"])) # backup_log.write(self.backup_node(node, options=["--verbose"]))
backup_id = self.backup_node(backup_dir, 'node', node) backup_id = self.backup_node(backup_dir, 'node', node)
@ -37,15 +40,23 @@ class BackupTest(ProbackupTest, unittest.TestCase):
# postmaster.pid and postmaster.opts shouldn't be copied # postmaster.pid and postmaster.opts shouldn't be copied
excluded = True excluded = True
db_dir = os.path.join(backup_dir, "backups", 'node', backup_id, "database") db_dir = os.path.join(
backup_dir, "backups", 'node', backup_id, "database")
for f in os.listdir(db_dir): for f in os.listdir(db_dir):
if os.path.isfile(os.path.join(db_dir, f)) \ if (
and (f == "postmaster.pid" or f == "postmaster.opts"): os.path.isfile(os.path.join(db_dir, f)) and
excluded = False (
self.assertEqual(excluded, True) f == "postmaster.pid" or
f == "postmaster.opts"
)
):
excluded = False
self.assertEqual(excluded, True)
# page backup mode # page backup mode
page_backup_id = self.backup_node(backup_dir, 'node', node, backup_type="page") page_backup_id = self.backup_node(
backup_dir, 'node', node, backup_type="page")
# print self.show_pb(node) # print self.show_pb(node)
show_backup = self.show_pb(backup_dir, 'node')[1] show_backup = self.show_pb(backup_dir, 'node')[1]
@ -55,7 +66,9 @@ class BackupTest(ProbackupTest, unittest.TestCase):
# Check parent backup # Check parent backup
self.assertEqual( self.assertEqual(
backup_id, backup_id,
self.show_pb(backup_dir, 'node', backup_id=show_backup['ID'])["parent-backup-id"]) self.show_pb(
backup_dir, 'node',
backup_id=show_backup['ID'])["parent-backup-id"])
# ptrack backup mode # ptrack backup mode
self.backup_node(backup_dir, 'node', node, backup_type="ptrack") self.backup_node(backup_dir, 'node', node, backup_type="ptrack")
@ -67,7 +80,9 @@ class BackupTest(ProbackupTest, unittest.TestCase):
# Check parent backup # Check parent backup
self.assertEqual( self.assertEqual(
page_backup_id, page_backup_id,
self.show_pb(backup_dir, 'node', backup_id=show_backup['ID'])["parent-backup-id"]) self.show_pb(
backup_dir, 'node',
backup_id=show_backup['ID'])["parent-backup-id"])
# Clean after yourself # Clean after yourself
self.del_test_dir(module_name, fname) self.del_test_dir(module_name, fname)
@ -76,17 +91,20 @@ class BackupTest(ProbackupTest, unittest.TestCase):
def test_smooth_checkpoint(self): def test_smooth_checkpoint(self):
"""full backup with smooth checkpoint""" """full backup with smooth checkpoint"""
fname = self.id().split('.')[3] fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname), node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
initdb_params=['--data-checksums'], initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica'} pg_options={'wal_level': 'replica'}
) )
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup') backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
self.init_pb(backup_dir) self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node) self.add_instance(backup_dir, 'node', node)
self.set_archiving(backup_dir, 'node', node) self.set_archiving(backup_dir, 'node', node)
node.start() node.start()
self.backup_node(backup_dir, 'node' ,node, options=["-C"]) self.backup_node(
backup_dir, 'node', node,
options=["-C"])
self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK") self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK")
node.stop() node.stop()
@ -97,7 +115,8 @@ class BackupTest(ProbackupTest, unittest.TestCase):
def test_incremental_backup_without_full(self): def test_incremental_backup_without_full(self):
"""page-level backup without validated full backup""" """page-level backup without validated full backup"""
fname = self.id().split('.')[3] fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname), node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
initdb_params=['--data-checksums'], initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'} pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'}
) )
@ -110,28 +129,40 @@ class BackupTest(ProbackupTest, unittest.TestCase):
try: try:
self.backup_node(backup_dir, 'node', node, backup_type="page") self.backup_node(backup_dir, 'node', node, backup_type="page")
# we should die here because exception is what we expect to happen # 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( self.assertEqual(
repr(self.output), self.cmd)) 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))
except ProbackupException as e: except ProbackupException as e:
self.assertIn( self.assertIn(
"ERROR: Valid backup on current timeline is not found. Create new FULL backup before an incremental one.", "ERROR: Valid backup on current timeline is not found. "
"Create new FULL backup before an incremental one.",
e.message, e.message,
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd)) "\n Unexpected Error Message: {0}\n CMD: {1}".format(
repr(e.message), self.cmd))
sleep(1) sleep(1)
try: try:
self.backup_node(backup_dir, 'node', node, backup_type="ptrack") self.backup_node(backup_dir, 'node', node, backup_type="ptrack")
# we should die here because exception is what we expect to happen # 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( self.assertEqual(
repr(self.output), self.cmd)) 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))
except ProbackupException as e: except ProbackupException as e:
self.assertIn( self.assertIn(
"ERROR: Valid backup on current timeline is not found. Create new FULL backup before an incremental one.", "ERROR: Valid backup on current timeline is not found. "
"Create new FULL backup before an incremental one.",
e.message, e.message,
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd)) "\n Unexpected Error Message: {0}\n CMD: {1}".format(
repr(e.message), self.cmd))
self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "ERROR") self.assertEqual(
self.show_pb(backup_dir, 'node')[0]['Status'],
"ERROR")
# Clean after yourself # Clean after yourself
self.del_test_dir(module_name, fname) self.del_test_dir(module_name, fname)
@ -140,7 +171,8 @@ class BackupTest(ProbackupTest, unittest.TestCase):
def test_incremental_backup_corrupt_full(self): def test_incremental_backup_corrupt_full(self):
"""page-level backup with corrupted full backup""" """page-level backup with corrupted full backup"""
fname = self.id().split('.')[3] fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname), node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
initdb_params=['--data-checksums'], initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'} pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'}
) )
@ -151,35 +183,50 @@ class BackupTest(ProbackupTest, unittest.TestCase):
node.start() node.start()
backup_id = self.backup_node(backup_dir, 'node', node) backup_id = self.backup_node(backup_dir, 'node', node)
file = os.path.join(backup_dir, "backups", "node", backup_id, "database", "postgresql.conf") file = os.path.join(
backup_dir, "backups", "node", backup_id,
"database", "postgresql.conf")
os.remove(file) os.remove(file)
try: try:
self.validate_pb(backup_dir, 'node') self.validate_pb(backup_dir, 'node')
# we should die here because exception is what we expect to happen # we should die here because exception is what we expect to happen
self.assertEqual(1, 0, "Expecting Error because of validation of corrupted backup.\n Output: {0} \n CMD: {1}".format( self.assertEqual(
repr(self.output), self.cmd)) 1, 0,
"Expecting Error because of validation of corrupted backup.\n"
" Output: {0} \n CMD: {1}".format(
repr(self.output), self.cmd))
except ProbackupException as e: except ProbackupException as e:
self.assertTrue("INFO: Validate backups of the instance 'node'\n" in e.message self.assertTrue(
and 'WARNING: Backup file "{0}" is not found\n'.format(file) in e.message "INFO: Validate backups of the instance 'node'\n" in e.message and
and "WARNING: Backup {0} data files are corrupted\n".format(backup_id) in e.message "WARNING: Backup file \"{0}\" is not found\n".format(
and "INFO: Some backups are not valid\n" in e.message, file) in e.message and
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd)) "WARNING: Backup {0} data files are 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))
try: try:
self.backup_node(backup_dir, 'node', node, backup_type="page") self.backup_node(backup_dir, 'node', node, backup_type="page")
# we should die here because exception is what we expect to happen # 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( self.assertEqual(
repr(self.output), self.cmd)) 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))
except ProbackupException as e: except ProbackupException as e:
self.assertIn( self.assertIn(
"ERROR: Valid backup on current timeline is not found. Create new FULL backup before an incremental one.", "ERROR: Valid backup on current timeline is not found. "
"Create new FULL backup before an incremental one.",
e.message, e.message,
"\n Unexpected Error Message: {0}\n CMD: {1}".format(repr(e.message), self.cmd)) "\n Unexpected Error Message: {0}\n CMD: {1}".format(
repr(e.message), self.cmd))
# sleep(1) self.assertEqual(
self.assertEqual(self.show_pb(backup_dir, 'node', backup_id)['status'], "CORRUPT") self.show_pb(backup_dir, 'node', backup_id)['status'], "CORRUPT")
self.assertEqual(self.show_pb(backup_dir, 'node')[1]['Status'], "ERROR") self.assertEqual(
self.show_pb(backup_dir, 'node')[1]['Status'], "ERROR")
# Clean after yourself # Clean after yourself
self.del_test_dir(module_name, fname) self.del_test_dir(module_name, fname)
@ -188,7 +235,8 @@ class BackupTest(ProbackupTest, unittest.TestCase):
def test_ptrack_threads(self): def test_ptrack_threads(self):
"""ptrack multi thread backup mode""" """ptrack multi thread backup mode"""
fname = self.id().split('.')[3] fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname), node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
initdb_params=['--data-checksums'], initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'} pg_options={'wal_level': 'replica', 'ptrack_enable': 'on'}
) )
@ -198,10 +246,14 @@ class BackupTest(ProbackupTest, unittest.TestCase):
self.set_archiving(backup_dir, 'node', node) self.set_archiving(backup_dir, 'node', node)
node.start() node.start()
self.backup_node(backup_dir, 'node', node, backup_type="full", options=["-j", "4"]) self.backup_node(
backup_dir, 'node', node,
backup_type="full", options=["-j", "4"])
self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK") self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK")
self.backup_node(backup_dir, 'node', node, backup_type="ptrack", options=["-j", "4"]) self.backup_node(
backup_dir, 'node', node,
backup_type="ptrack", options=["-j", "4"])
self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK") self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK")
# Clean after yourself # Clean after yourself
@ -211,20 +263,28 @@ class BackupTest(ProbackupTest, unittest.TestCase):
def test_ptrack_threads_stream(self): def test_ptrack_threads_stream(self):
"""ptrack multi thread backup mode and stream""" """ptrack multi thread backup mode and stream"""
fname = self.id().split('.')[3] fname = self.id().split('.')[3]
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname), node = self.make_simple_node(
base_dir="{0}/{1}/node".format(module_name, fname),
set_replication=True, set_replication=True,
initdb_params=['--data-checksums'], initdb_params=['--data-checksums'],
pg_options={'wal_level': 'replica', 'ptrack_enable': 'on', 'max_wal_senders': '2'} pg_options={
'wal_level': 'replica',
'ptrack_enable': 'on',
'max_wal_senders': '2'}
) )
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup') backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
self.init_pb(backup_dir) self.init_pb(backup_dir)
self.add_instance(backup_dir, 'node', node) self.add_instance(backup_dir, 'node', node)
node.start() node.start()
self.backup_node(backup_dir, 'node', node, backup_type="full", options=["-j", "4", "--stream"]) self.backup_node(
backup_dir, 'node', node, backup_type="full",
options=["-j", "4", "--stream"])
self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK") self.assertEqual(self.show_pb(backup_dir, 'node')[0]['Status'], "OK")
self.backup_node(backup_dir, 'node', node, backup_type="ptrack", options=["-j", "4", "--stream"]) self.backup_node(
backup_dir, 'node', node,
backup_type="ptrack", options=["-j", "4", "--stream"])
self.assertEqual(self.show_pb(backup_dir, 'node')[1]['Status'], "OK") self.assertEqual(self.show_pb(backup_dir, 'node')[1]['Status'], "OK")
# Clean after yourself # Clean after yourself