1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2024-12-02 09:53:24 +02:00
pg_probackup/tests/retention_test.py

105 lines
4.1 KiB
Python
Raw Normal View History

import os
2017-06-27 11:43:45 +02:00
import unittest
from datetime import datetime, timedelta
2017-06-27 07:42:52 +02:00
from .helpers.ptrack_helpers import ProbackupTest
2017-07-12 16:28:28 +02:00
module_name = 'retention'
2017-07-12 16:28:28 +02:00
class RetentionTest(ProbackupTest, unittest.TestCase):
2017-05-05 15:21:49 +02:00
# @unittest.skip("skip")
# @unittest.expectedFailure
2017-05-05 15:21:49 +02:00
def test_retention_redundancy_1(self):
"""purge backups using redundancy-based retention policy"""
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-05 15:21:49 +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)
2017-05-05 15:21:49 +02:00
node.start()
2017-06-20 12:57:23 +02:00
with open(os.path.join(backup_dir, 'backups', 'node', "pg_probackup.conf"), "a") as conf:
2017-05-05 15:21:49 +02:00
conf.write("retention-redundancy = 1\n")
# Make backups to be purged
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'node', node)
self.backup_node(backup_dir, 'node', node, backup_type="page")
2017-05-05 15:21:49 +02:00
# Make backups to be keeped
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'node', node)
self.backup_node(backup_dir, 'node', node, backup_type="page")
2017-05-05 15:21:49 +02:00
2017-06-20 12:57:23 +02:00
self.assertEqual(len(self.show_pb(backup_dir, 'node')), 4)
2017-05-05 15:21:49 +02:00
# Purge backups
2017-06-20 12:57:23 +02:00
log = self.delete_expired(backup_dir, 'node')
self.assertEqual(len(self.show_pb(backup_dir, 'node')), 2)
2017-05-05 15:21:49 +02:00
# Check that WAL segments were deleted
min_wal = None
max_wal = None
for line in log.splitlines():
2017-06-27 07:42:52 +02:00
if line.startswith("INFO: removed min WAL segment"):
2017-05-05 15:21:49 +02:00
min_wal = line[31:-1]
2017-06-27 07:42:52 +02:00
elif line.startswith("INFO: removed max WAL segment"):
2017-05-05 15:21:49 +02:00
max_wal = line[31:-1]
2017-06-27 11:43:45 +02:00
for wal_name in os.listdir(os.path.join(backup_dir, 'wal', 'node')):
2017-05-05 15:21:49 +02:00
if not wal_name.endswith(".backup"):
2017-06-27 07:42:52 +02:00
#wal_name_b = wal_name.encode('ascii')
self.assertEqual(wal_name[8:] > min_wal[8:], True)
self.assertEqual(wal_name[8:] > max_wal[8:], True)
2017-05-05 15:21:49 +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-05 15:21:49 +02:00
# @unittest.skip("123")
def test_retention_window_2(self):
"""purge backups using window-based retention policy"""
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-05 15:21:49 +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)
2017-05-05 15:21:49 +02:00
node.start()
2017-06-20 12:57:23 +02:00
with open(os.path.join(backup_dir, 'backups', 'node', "pg_probackup.conf"), "a") as conf:
2017-05-05 15:21:49 +02:00
conf.write("retention-redundancy = 1\n")
conf.write("retention-window = 1\n")
# Make backups to be purged
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'node', node)
self.backup_node(backup_dir, 'node', node, backup_type="page")
2017-05-05 15:21:49 +02:00
# Make backup to be keeped
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'node', node)
2017-05-05 15:21:49 +02:00
2017-06-27 11:43:45 +02:00
backups = os.path.join(backup_dir, 'backups', 'node')
2017-05-05 15:21:49 +02:00
days_delta = 5
2017-06-27 11:43:45 +02:00
for backup in os.listdir(backups):
2017-06-20 12:57:23 +02:00
if backup == 'pg_probackup.conf':
continue
2017-06-27 11:43:45 +02:00
with open(os.path.join(backups, backup, "backup.control"), "a") as conf:
2017-05-05 15:21:49 +02:00
conf.write("recovery_time='{:%Y-%m-%d %H:%M:%S}'\n".format(
datetime.now() - timedelta(days=days_delta)))
days_delta -= 1
# Make backup to be keeped
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'node', node, backup_type="page")
2017-05-05 15:21:49 +02:00
2017-06-20 12:57:23 +02:00
self.assertEqual(len(self.show_pb(backup_dir, 'node')), 4)
2017-05-05 15:21:49 +02:00
# Purge backups
2017-06-20 12:57:23 +02:00
self.delete_expired(backup_dir, 'node')
self.assertEqual(len(self.show_pb(backup_dir, 'node')), 2)
2017-05-05 15:21:49 +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)