1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2024-12-04 10:44:46 +02:00
pg_probackup/tests/replica.py

171 lines
8.6 KiB
Python
Raw Normal View History

2017-05-17 11:46:38 +02:00
import os
2017-06-27 11:43:45 +02:00
import unittest
2017-06-27 07:42:52 +02:00
from .helpers.ptrack_helpers import ProbackupTest, ProbackupException, idx_ptrack
2017-05-17 11:46:38 +02:00
from datetime import datetime, timedelta
import subprocess
2017-07-12 16:28:28 +02:00
from sys import exit
2017-05-17 11:46:38 +02:00
2017-07-12 16:28:28 +02:00
module_name = 'replica'
2017-05-17 11:46:38 +02:00
2017-07-12 16:28:28 +02:00
class ReplicaTest(ProbackupTest, unittest.TestCase):
2017-05-17 11:46:38 +02:00
# @unittest.skip("skip")
# @unittest.expectedFailure
2017-07-12 16:28:28 +02:00
def test_replica_stream_ptrack_backup(self):
2017-09-28 09:32:06 +02:00
"""make node, take full backup, restore it and make replica from it, take full stream backup from replica"""
2017-05-17 11:46:38 +02:00
fname = self.id().split('.')[3]
2017-07-12 16:28:28 +02:00
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
master = self.make_simple_node(base_dir="{0}/{1}/master".format(module_name, fname),
2017-05-17 11:46:38 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2017-07-12 16:28:28 +02:00
pg_options={'wal_level': 'replica', 'max_wal_senders': '2', 'checkpoint_timeout': '30s', 'ptrack_enable': 'on'}
2017-05-17 11:46:38 +02:00
)
2017-06-07 16:52:07 +02:00
master.start()
self.init_pb(backup_dir)
2017-06-20 12:57:23 +02:00
self.add_instance(backup_dir, 'master', master)
2017-06-07 16:52:07 +02:00
2017-07-12 16:28:28 +02:00
# CREATE TABLE
2017-05-17 11:46:38 +02:00
master.psql(
"postgres",
"create table t_heap as select i as id, md5(i::text) as text, md5(repeat(i::text,10))::tsvector as tsvector from generate_series(0,256) i")
2017-07-12 16:28:28 +02:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
2017-05-17 11:46:38 +02:00
2017-07-12 16:28:28 +02:00
# take full backup and restore it
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'master', master, options=['--stream'])
2017-09-28 09:32:06 +02:00
replica = self.make_simple_node(base_dir="{0}/{1}/replica".format(module_name, fname))
replica.cleanup()
2017-07-12 16:28:28 +02:00
self.restore_node(backup_dir, 'master', replica)
self.set_replica(master, replica)
2017-05-17 11:46:38 +02:00
2017-07-12 16:28:28 +02:00
# Check data correctness on replica
replica.start({"-t": "600"})
after = replica.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
2017-05-17 11:46:38 +02:00
2017-07-12 16:28:28 +02:00
# Change data on master, take FULL backup from replica, restore taken backup and check that restored data equal to original data
master.psql(
"postgres",
"insert into t_heap as select i as id, md5(i::text) as text, md5(repeat(i::text,10))::tsvector as tsvector from generate_series(256,512) i")
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
self.add_instance(backup_dir, 'replica', replica)
backup_id = self.backup_node(backup_dir, 'replica', replica, options=['--stream',
'--master-host=localhost', '--master-db=postgres','--master-port={0}'.format(master.port)])
self.validate_pb(backup_dir, 'replica')
self.assertEqual('OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
# RESTORE FULL BACKUP TAKEN FROM PREVIOUS STEP
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname))
node.cleanup()
self.restore_node(backup_dir, 'replica', data_dir=node.data_dir)
node.append_conf('postgresql.auto.conf', 'port = {0}'.format(node.port))
2017-09-28 09:32:06 +02:00
node.start()
2017-07-12 16:28:28 +02:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
2017-05-17 11:46:38 +02:00
self.assertEqual(before, after)
2017-07-12 16:28:28 +02:00
# Change data on master, take PTRACK backup from replica, restore taken backup and check that restored data equal to original data
master.psql(
"postgres",
"insert into t_heap as select i as id, md5(i::text) as text, md5(repeat(i::text,10))::tsvector as tsvector from generate_series(512,768) i")
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
backup_id = self.backup_node(backup_dir, 'replica', replica, backup_type='ptrack', options=['--stream',
'--master-host=localhost', '--master-db=postgres', '--master-port={0}'.format(master.port)])
self.validate_pb(backup_dir, 'replica')
self.assertEqual('OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
# RESTORE PTRACK BACKUP TAKEN FROM replica
node.cleanup()
self.restore_node(backup_dir, 'replica', data_dir=node.data_dir, backup_id=backup_id)
node.append_conf('postgresql.auto.conf', 'port = {0}'.format(node.port))
2017-09-28 09:32:06 +02:00
node.start()
2017-07-12 16:28:28 +02:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
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
2017-06-20 12:57:23 +02:00
# @unittest.skip("skip")
2017-07-12 16:28:28 +02:00
def test_replica_archive_page_backup(self):
2017-09-28 09:32:06 +02:00
"""make archive master, take full and page archive backups from master, set replica, make archive backup from replica"""
fname = self.id().split('.')[3]
2017-07-12 16:28:28 +02:00
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
master = self.make_simple_node(base_dir="{0}/{1}/master".format(module_name, fname),
2017-09-28 09:32:06 +02:00
set_replication=True,
initdb_params=['--data-checksums'],
2017-06-20 12:57:23 +02:00
pg_options={'wal_level': 'replica', 'max_wal_senders': '2', 'checkpoint_timeout': '30s'}
)
2017-06-20 12:57:23 +02:00
self.init_pb(backup_dir)
self.add_instance(backup_dir, 'master', master)
self.set_archiving(backup_dir, 'master', master)
# force more frequent wal switch
master.append_conf('postgresql.auto.conf', 'archive_timeout = 10')
master.start()
2017-07-12 16:28:28 +02:00
replica = self.make_simple_node(base_dir="{0}/{1}/replica".format(module_name, fname))
replica.cleanup()
2017-06-20 12:57:23 +02:00
self.backup_node(backup_dir, 'master', master)
master.psql(
"postgres",
"create table t_heap as select i as id, md5(i::text) as text, md5(repeat(i::text,10))::tsvector as tsvector from generate_series(0,256) i")
2017-07-12 16:28:28 +02:00
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
2017-06-20 12:57:23 +02:00
backup_id = self.backup_node(backup_dir, 'master', master, backup_type='page')
2017-07-12 16:28:28 +02:00
self.restore_node(backup_dir, 'master', replica)
# Settings for Replica
2017-07-12 16:28:28 +02:00
self.set_replica(master, replica)
self.set_archiving(backup_dir, 'replica', replica, replica=True)
replica.start({"-t": "600"})
# Check data correctness on replica
after = replica.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
2017-07-12 16:28:28 +02:00
# Change data on master, take FULL backup from replica, restore taken backup and check that restored data equal to original data
master.psql(
"postgres",
"insert into t_heap as select i as id, md5(i::text) as text, md5(repeat(i::text,10))::tsvector as tsvector from generate_series(256,512) i")
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
self.add_instance(backup_dir, 'replica', replica)
backup_id = self.backup_node(backup_dir, 'replica', replica, options=['--archive-timeout=300',
2017-06-20 12:57:23 +02:00
'--master-host=localhost', '--master-db=postgres','--master-port={0}'.format(master.port)])
2017-07-12 16:28:28 +02:00
self.validate_pb(backup_dir, 'replica')
self.assertEqual('OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
# RESTORE FULL BACKUP TAKEN FROM replica
node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname))
node.cleanup()
self.restore_node(backup_dir, 'replica', data_dir=node.data_dir)
node.append_conf('postgresql.auto.conf', 'port = {0}'.format(node.port))
2017-09-28 09:32:06 +02:00
node.start()
2017-07-12 16:28:28 +02:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
# Change data on master, make PAGE backup from replica, restore taken backup and check that restored data equal to original data
master.psql(
"postgres",
"insert into t_heap as select i as id, md5(i::text) as text, md5(repeat(i::text,10))::tsvector as tsvector from generate_series(512,768) i")
before = master.safe_psql("postgres", "SELECT * FROM t_heap")
backup_id = self.backup_node(backup_dir, 'replica', replica, backup_type='page', options=['--archive-timeout=300',
'--master-host=localhost', '--master-db=postgres','--master-port={0}'.format(master.port)])
self.validate_pb(backup_dir, 'replica')
self.assertEqual('OK', self.show_pb(backup_dir, 'replica', backup_id)['status'])
# RESTORE PAGE BACKUP TAKEN FROM replica
node.cleanup()
self.restore_node(backup_dir, 'replica', data_dir=node.data_dir, backup_id=backup_id)
node.append_conf('postgresql.auto.conf', 'port = {0}'.format(node.port))
2017-09-28 09:32:06 +02:00
node.start()
2017-07-12 16:28:28 +02:00
# CHECK DATA CORRECTNESS
after = node.safe_psql("postgres", "SELECT * FROM t_heap")
self.assertEqual(before, after)
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)