2016-12-12 18:21:15 +02:00
|
|
|
import unittest
|
2017-06-20 12:57:23 +02:00
|
|
|
import os
|
2017-06-27 07:42:52 +02:00
|
|
|
from .helpers.ptrack_helpers import ProbackupTest, ProbackupException
|
2016-12-12 18:21:15 +02:00
|
|
|
import subprocess
|
2017-07-12 16:28:28 +02:00
|
|
|
from sys import exit
|
2016-12-12 18:21:15 +02:00
|
|
|
|
|
|
|
|
2017-07-12 16:28:28 +02:00
|
|
|
module_name = 'delete'
|
|
|
|
|
2016-12-12 18:21:15 +02:00
|
|
|
|
2017-07-12 16:28:28 +02:00
|
|
|
class DeleteTest(ProbackupTest, unittest.TestCase):
|
2017-05-03 13:14:48 +02:00
|
|
|
|
2017-05-22 13:17:43 +02:00
|
|
|
# @unittest.skip("skip")
|
|
|
|
# @unittest.expectedFailure
|
2017-05-03 13:14:48 +02:00
|
|
|
def test_delete_full_backups(self):
|
|
|
|
"""delete full backups"""
|
|
|
|
fname = self.id().split('.')[3]
|
2018-05-03 13:12:19 +02:00
|
|
|
node = self.make_simple_node(
|
2018-12-26 21:59:13 +02:00
|
|
|
base_dir=os.path.join(module_name, fname, 'node'),
|
2017-05-03 13:14:48 +02:00
|
|
|
initdb_params=['--data-checksums'],
|
2018-12-26 21:59:13 +02:00
|
|
|
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)
|
2018-12-25 16:48:49 +02:00
|
|
|
node.slow_start()
|
2017-05-03 13:14:48 +02:00
|
|
|
|
2017-06-20 12:57:23 +02:00
|
|
|
# full backup
|
|
|
|
self.backup_node(backup_dir, 'node', node)
|
2017-05-03 13:14:48 +02:00
|
|
|
|
2018-05-03 13:12:19 +02:00
|
|
|
pgbench = node.pgbench(
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2017-05-03 13:14:48 +02:00
|
|
|
pgbench.wait()
|
|
|
|
pgbench.stdout.close()
|
|
|
|
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node)
|
2017-05-03 13:14:48 +02:00
|
|
|
|
2018-05-03 13:12:19 +02:00
|
|
|
pgbench = node.pgbench(
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2017-05-03 13:14:48 +02:00
|
|
|
pgbench.wait()
|
|
|
|
pgbench.stdout.close()
|
|
|
|
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node)
|
2017-05-03 13:14:48 +02:00
|
|
|
|
2017-06-20 12:57:23 +02:00
|
|
|
show_backups = self.show_pb(backup_dir, 'node')
|
2018-06-02 19:35:37 +02:00
|
|
|
id_1 = show_backups[0]['id']
|
|
|
|
id_2 = show_backups[1]['id']
|
|
|
|
id_3 = show_backups[2]['id']
|
2017-06-20 12:57:23 +02:00
|
|
|
self.delete_pb(backup_dir, 'node', id_2)
|
|
|
|
show_backups = self.show_pb(backup_dir, 'node')
|
2018-06-02 19:35:37 +02:00
|
|
|
self.assertEqual(show_backups[0]['id'], id_1)
|
|
|
|
self.assertEqual(show_backups[1]['id'], id_3)
|
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
|
|
|
|
2018-10-20 12:44:47 +02:00
|
|
|
# @unittest.skip("skip")
|
|
|
|
# @unittest.expectedFailure
|
|
|
|
def test_delete_archive_mix_compress_and_non_compressed_segments(self):
|
2018-10-24 20:08:00 +02:00
|
|
|
"""stub"""
|
2018-10-20 12:44:47 +02:00
|
|
|
|
2017-07-12 16:28:28 +02:00
|
|
|
# @unittest.skip("skip")
|
2017-06-20 12:57:23 +02:00
|
|
|
def test_delete_increment_page(self):
|
2017-05-03 13:14:48 +02:00
|
|
|
"""delete increment and all after him"""
|
|
|
|
fname = self.id().split('.')[3]
|
2018-12-26 21:59:13 +02:00
|
|
|
node = self.make_simple_node(
|
|
|
|
base_dir=os.path.join(module_name, fname, 'node'),
|
2017-05-03 13:14:48 +02:00
|
|
|
initdb_params=['--data-checksums'],
|
2018-12-26 21:59:13 +02:00
|
|
|
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)
|
2018-12-25 16:48:49 +02:00
|
|
|
node.slow_start()
|
2017-05-03 13:14:48 +02:00
|
|
|
|
|
|
|
# full backup mode
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node)
|
2017-05-03 13:14:48 +02:00
|
|
|
# page backup mode
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node, backup_type="page")
|
2017-05-03 13:14:48 +02:00
|
|
|
# page backup mode
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node, backup_type="page")
|
2017-05-03 13:14:48 +02:00
|
|
|
# full backup mode
|
2017-06-20 12:57:23 +02:00
|
|
|
self.backup_node(backup_dir, 'node', node)
|
|
|
|
|
|
|
|
show_backups = self.show_pb(backup_dir, 'node')
|
|
|
|
self.assertEqual(len(show_backups), 4)
|
|
|
|
|
|
|
|
# delete first page backup
|
2018-06-02 19:35:37 +02:00
|
|
|
self.delete_pb(backup_dir, 'node', show_backups[1]['id'])
|
2017-06-20 12:57:23 +02:00
|
|
|
|
|
|
|
show_backups = self.show_pb(backup_dir, 'node')
|
|
|
|
self.assertEqual(len(show_backups), 2)
|
2017-05-03 13:14:48 +02:00
|
|
|
|
2018-06-02 19:35:37 +02:00
|
|
|
self.assertEqual(show_backups[0]['backup-mode'], "FULL")
|
|
|
|
self.assertEqual(show_backups[0]['status'], "OK")
|
|
|
|
self.assertEqual(show_backups[1]['backup-mode'], "FULL")
|
|
|
|
self.assertEqual(show_backups[1]['status'], "OK")
|
2017-06-20 12:57:23 +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-06-20 12:57:23 +02:00
|
|
|
|
2017-07-12 16:28:28 +02:00
|
|
|
# @unittest.skip("skip")
|
2017-06-20 12:57:23 +02:00
|
|
|
def test_delete_increment_ptrack(self):
|
|
|
|
"""delete increment and all after him"""
|
|
|
|
fname = self.id().split('.')[3]
|
2018-12-26 21:59:13 +02:00
|
|
|
node = self.make_simple_node(
|
|
|
|
base_dir=os.path.join(module_name, fname, 'node'),
|
2017-06-20 12:57:23 +02:00
|
|
|
initdb_params=['--data-checksums'],
|
2018-12-26 21:59:13 +02:00
|
|
|
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)
|
2018-12-25 16:48:49 +02:00
|
|
|
node.slow_start()
|
2017-06-20 12:57:23 +02:00
|
|
|
|
|
|
|
# full backup mode
|
|
|
|
self.backup_node(backup_dir, 'node', node)
|
|
|
|
# page backup mode
|
|
|
|
self.backup_node(backup_dir, 'node', node, backup_type="ptrack")
|
|
|
|
# page backup mode
|
|
|
|
self.backup_node(backup_dir, 'node', node, backup_type="ptrack")
|
|
|
|
# full backup mode
|
|
|
|
self.backup_node(backup_dir, 'node', node)
|
2017-05-03 13:14:48 +02:00
|
|
|
|
2017-06-20 12:57:23 +02:00
|
|
|
show_backups = self.show_pb(backup_dir, 'node')
|
2017-05-03 13:14:48 +02:00
|
|
|
self.assertEqual(len(show_backups), 4)
|
|
|
|
|
|
|
|
# delete first page backup
|
2018-06-02 19:35:37 +02:00
|
|
|
self.delete_pb(backup_dir, 'node', show_backups[1]['id'])
|
2017-05-03 13:14:48 +02:00
|
|
|
|
2017-06-20 12:57:23 +02:00
|
|
|
show_backups = self.show_pb(backup_dir, 'node')
|
2017-05-03 13:14:48 +02:00
|
|
|
self.assertEqual(len(show_backups), 2)
|
|
|
|
|
2018-06-02 19:35:37 +02:00
|
|
|
self.assertEqual(show_backups[0]['backup-mode'], "FULL")
|
|
|
|
self.assertEqual(show_backups[0]['status'], "OK")
|
|
|
|
self.assertEqual(show_backups[1]['backup-mode'], "FULL")
|
|
|
|
self.assertEqual(show_backups[1]['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)
|
|
|
|
|
|
|
|
# @unittest.skip("skip")
|
|
|
|
def test_delete_orphaned_wal_segments(self):
|
|
|
|
"""make archive node, make three full backups, delete second backup without --wal option, then delete orphaned wals via --wal option"""
|
|
|
|
fname = self.id().split('.')[3]
|
2018-12-26 21:59:13 +02:00
|
|
|
node = self.make_simple_node(
|
|
|
|
base_dir=os.path.join(module_name, fname, 'node'),
|
2017-07-12 16:28:28 +02:00
|
|
|
initdb_params=['--data-checksums'],
|
2018-12-26 21:59:13 +02:00
|
|
|
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')
|
|
|
|
self.init_pb(backup_dir)
|
|
|
|
self.add_instance(backup_dir, 'node', node)
|
|
|
|
self.set_archiving(backup_dir, 'node', node)
|
2018-12-25 16:48:49 +02:00
|
|
|
node.slow_start()
|
2017-07-12 16:28:28 +02:00
|
|
|
|
|
|
|
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,10000) i")
|
|
|
|
# first full backup
|
|
|
|
backup_1_id = self.backup_node(backup_dir, 'node', node)
|
|
|
|
# second full backup
|
|
|
|
backup_2_id = self.backup_node(backup_dir, 'node', node)
|
|
|
|
# third full backup
|
|
|
|
backup_3_id = self.backup_node(backup_dir, 'node', node)
|
|
|
|
node.stop()
|
|
|
|
|
|
|
|
# Check wals
|
|
|
|
wals_dir = os.path.join(backup_dir, 'wal', 'node')
|
|
|
|
wals = [f for f in os.listdir(wals_dir) if os.path.isfile(os.path.join(wals_dir, f)) and not f.endswith('.backup')]
|
|
|
|
original_wal_quantity = len(wals)
|
|
|
|
|
|
|
|
# delete second full backup
|
|
|
|
self.delete_pb(backup_dir, 'node', backup_2_id)
|
|
|
|
# check wal quantity
|
|
|
|
self.validate_pb(backup_dir)
|
|
|
|
self.assertEqual(self.show_pb(backup_dir, 'node', backup_1_id)['status'], "OK")
|
|
|
|
self.assertEqual(self.show_pb(backup_dir, 'node', backup_3_id)['status'], "OK")
|
|
|
|
# try to delete wals for second backup
|
|
|
|
self.delete_pb(backup_dir, 'node', options=['--wal'])
|
|
|
|
# check wal quantity
|
|
|
|
self.validate_pb(backup_dir)
|
|
|
|
self.assertEqual(self.show_pb(backup_dir, 'node', backup_1_id)['status'], "OK")
|
|
|
|
self.assertEqual(self.show_pb(backup_dir, 'node', backup_3_id)['status'], "OK")
|
|
|
|
|
|
|
|
# delete first full backup
|
|
|
|
self.delete_pb(backup_dir, 'node', backup_1_id)
|
|
|
|
self.validate_pb(backup_dir)
|
|
|
|
self.assertEqual(self.show_pb(backup_dir, 'node', backup_3_id)['status'], "OK")
|
|
|
|
|
|
|
|
result = self.delete_pb(backup_dir, 'node', options=['--wal'])
|
|
|
|
# delete useless wals
|
|
|
|
self.assertTrue('INFO: removed min WAL segment' in result
|
|
|
|
and 'INFO: removed max WAL segment' in result)
|
|
|
|
self.validate_pb(backup_dir)
|
|
|
|
self.assertEqual(self.show_pb(backup_dir, 'node', backup_3_id)['status'], "OK")
|
|
|
|
|
|
|
|
# Check quantity, it should be lower than original
|
|
|
|
wals = [f for f in os.listdir(wals_dir) if os.path.isfile(os.path.join(wals_dir, f)) and not f.endswith('.backup')]
|
|
|
|
self.assertTrue(original_wal_quantity > len(wals), "Number of wals not changed after 'delete --wal' which is illegal")
|
|
|
|
|
|
|
|
# Delete last backup
|
|
|
|
self.delete_pb(backup_dir, 'node', backup_3_id, options=['--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')]
|
|
|
|
self.assertEqual (0, len(wals), "Number of wals should be equal to 0")
|
|
|
|
|
|
|
|
# Clean after yourself
|
|
|
|
self.del_test_dir(module_name, fname)
|