|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +from .helpers.ptrack_helpers import ProbackupTest, ProbackupException |
| 4 | + |
| 5 | + |
| 6 | +module_name = 'exclude' |
| 7 | + |
| 8 | + |
| 9 | +class ExcludeTest(ProbackupTest, unittest.TestCase): |
| 10 | + |
| 11 | + # @unittest.skip("skip") |
| 12 | + # @unittest.expectedFailure |
| 13 | + def test_exclude_temp_tables(self): |
| 14 | + """make node without archiving, create temp table, take full backup, check that temp table not present in backup catalogue""" |
| 15 | + fname = self.id().split('.')[3] |
| 16 | + backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup') |
| 17 | + node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname), |
| 18 | + set_replication=True, |
| 19 | + initdb_params=['--data-checksums'], |
| 20 | + pg_options={'wal_level': 'replica', 'max_wal_senders': '2', 'shared_buffers': '1GB', |
| 21 | + "fsync": "off", 'ptrack_enable': 'on'} |
| 22 | + ) |
| 23 | + |
| 24 | + self.init_pb(backup_dir) |
| 25 | + self.add_instance(backup_dir, 'node', node) |
| 26 | + node.start() |
| 27 | + |
| 28 | + conn = node.connect() |
| 29 | + with node.connect("postgres") as conn: |
| 30 | + |
| 31 | + conn.execute("create temp table test as select generate_series(0,50050000)::text") |
| 32 | + conn.commit() |
| 33 | + |
| 34 | + temp_schema_name = conn.execute("SELECT nspname FROM pg_namespace WHERE oid = pg_my_temp_schema()")[0][0] |
| 35 | + conn.commit() |
| 36 | + |
| 37 | + temp_toast_schema_name = "pg_toast_" + temp_schema_name.replace("pg_", "") |
| 38 | + conn.commit() |
| 39 | + |
| 40 | + conn.execute("create index test_idx on test (generate_series)") |
| 41 | + conn.commit() |
| 42 | + |
| 43 | + heap_path = conn.execute("select pg_relation_filepath('test')")[0][0] |
| 44 | + conn.commit() |
| 45 | + |
| 46 | + index_path = conn.execute("select pg_relation_filepath('test_idx')")[0][0] |
| 47 | + conn.commit() |
| 48 | + |
| 49 | + heap_oid = conn.execute("select 'test'::regclass::oid")[0][0] |
| 50 | + conn.commit() |
| 51 | + |
| 52 | + toast_path = conn.execute("select pg_relation_filepath('{0}.{1}')".format(temp_toast_schema_name, "pg_toast_" + str(heap_oid)))[0][0] |
| 53 | + conn.commit() |
| 54 | + |
| 55 | + toast_idx_path = conn.execute("select pg_relation_filepath('{0}.{1}')".format(temp_toast_schema_name, "pg_toast_" + str(heap_oid) + "_index"))[0][0] |
| 56 | + conn.commit() |
| 57 | + |
| 58 | + temp_table_filename = os.path.basename(heap_path) |
| 59 | + temp_idx_filename = os.path.basename(index_path) |
| 60 | + temp_toast_filename = os.path.basename(toast_path) |
| 61 | + temp_idx_toast_filename = os.path.basename(toast_idx_path) |
| 62 | + |
| 63 | + self.backup_node(backup_dir, 'node', node, backup_type='full', options=['--stream']) |
| 64 | + |
| 65 | + for root, dirs, files in os.walk(backup_dir): |
| 66 | + for file in files: |
| 67 | + if file in [temp_table_filename, temp_table_filename + ".1", |
| 68 | + temp_idx_filename, |
| 69 | + temp_idx_filename + ".1", |
| 70 | + temp_toast_filename, |
| 71 | + temp_toast_filename + ".1", |
| 72 | + temp_idx_toast_filename, |
| 73 | + temp_idx_toast_filename + ".1"]: |
| 74 | + self.assertEqual(1, 0, "Found temp table file in backup catalogue.\n Filepath: {0}".format(file)) |
| 75 | + |
| 76 | + # Clean after yourself |
| 77 | + self.del_test_dir(module_name, fname) |
| 78 | + |
| 79 | + # @unittest.skip("skip") |
| 80 | + def test_exclude_unlogged_tables(self): |
| 81 | + """make node without archiving, create temp table, take full backup, check that temp table not present in backup catalogue""" |
| 82 | + fname = self.id().split('.')[3] |
| 83 | + backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup') |
| 84 | + node = self.make_simple_node(base_dir="{0}/{1}/node".format(module_name, fname), |
| 85 | + set_replication=True, |
| 86 | + initdb_params=['--data-checksums'], |
| 87 | + pg_options={'wal_level': 'replica', 'max_wal_senders': '2', "shared_buffers": "1GB", "fsync": "off", 'ptrack_enable': 'on'} |
| 88 | + ) |
| 89 | + |
| 90 | + self.init_pb(backup_dir) |
| 91 | + self.add_instance(backup_dir, 'node', node) |
| 92 | + node.start() |
| 93 | + |
| 94 | + conn = node.connect() |
| 95 | + with node.connect("postgres") as conn: |
| 96 | + |
| 97 | + conn.execute("create unlogged table test as select generate_series(0,50050000)::text") |
| 98 | + conn.commit() |
| 99 | + |
| 100 | + conn.execute("create index test_idx on test (generate_series)") |
| 101 | + conn.commit() |
| 102 | + |
| 103 | + heap_path = conn.execute("select pg_relation_filepath('test')")[0][0] |
| 104 | + conn.commit() |
| 105 | + |
| 106 | + index_path = conn.execute("select pg_relation_filepath('test_idx')")[0][0] |
| 107 | + conn.commit() |
| 108 | + index_init_path = index_path + "_init" |
| 109 | + |
| 110 | + heap_oid = conn.execute("select 'test'::regclass::oid")[0][0] |
| 111 | + conn.commit() |
| 112 | + |
| 113 | + toast_path = conn.execute("select pg_relation_filepath('{0}.{1}')".format("pg_toast", "pg_toast_" + str(heap_oid)))[0][0] |
| 114 | + conn.commit() |
| 115 | + toast_init_path = toast_path + "_init" |
| 116 | + |
| 117 | + toast_idx_path = conn.execute("select pg_relation_filepath('{0}.{1}')".format("pg_toast", "pg_toast_" + str(heap_oid) + "_index"))[0][0] |
| 118 | + conn.commit() |
| 119 | + toast_index_idx_path = toast_idx_path + "_init" |
| 120 | + |
| 121 | + unlogged_heap_filename = os.path.basename(heap_path) |
| 122 | + unlogged_heap_init_filename = unlogged_heap_filename + "_init" |
| 123 | + |
| 124 | + unlogged_idx_filename = os.path.basename(index_path) |
| 125 | + unlogged_idx_init_filename = unlogged_idx_filename + "_init" |
| 126 | + |
| 127 | + unlogged_toast_filename = os.path.basename(toast_path) |
| 128 | + unlogged_toast_init_filename = unlogged_toast_filename + "_init" |
| 129 | + |
| 130 | + unlogged_idx_toast_filename = os.path.basename(toast_idx_path) |
| 131 | + unlogged_idx_toast_init_filename = unlogged_idx_toast_filename + "_init" |
| 132 | + |
| 133 | + self.backup_node(backup_dir, 'node', node, backup_type='full', options=['--stream']) |
| 134 | + |
| 135 | + found_unlogged_heap_init = False |
| 136 | + found_unlogged_idx_init = False |
| 137 | + found_unlogged_toast = False |
| 138 | + found_unlogged_idx_toast_init = False |
| 139 | + for root, dirs, files in os.walk(backup_dir): |
| 140 | + for file in files: |
| 141 | + if file in [unlogged_heap_filename, unlogged_heap_filename + ".1", |
| 142 | + unlogged_idx_filename, |
| 143 | + unlogged_idx_filename + ".1", |
| 144 | + unlogged_toast_filename, |
| 145 | + unlogged_toast_filename + ".1", |
| 146 | + unlogged_idx_toast_filename, |
| 147 | + unlogged_idx_toast_filename + ".1"]: |
| 148 | + self.assertTrue(False, "Found unlogged table file in backup catalogue.\n Filepath: {0}".format(file)) |
| 149 | + |
| 150 | + if file == unlogged_heap_init_filename: |
| 151 | + found_unlogged_heap_init = True |
| 152 | + |
| 153 | + if file == unlogged_idx_init_filename: |
| 154 | + found_unlogged_idx_init = True |
| 155 | + |
| 156 | + if file == unlogged_toast_init_filename: |
| 157 | + found_unlogged_toast = True |
| 158 | + |
| 159 | + if file == unlogged_idx_toast_init_filename: |
| 160 | + found_unlogged_idx_toast_init = True |
| 161 | + |
| 162 | + self.assertTrue(found_unlogged_heap_init, "{0} is not found in backup catalogue".format(unlogged_heap_init_filename)); |
| 163 | + self.assertTrue(found_unlogged_idx_init, "{0} is not found in backup catalogue".format(unlogged_idx_init_filename)); |
| 164 | + self.assertTrue(found_unlogged_toast, "{0} is not found in backup catalogue".format(unlogged_toast_filename)); |
| 165 | + self.assertTrue(found_unlogged_idx_toast_init, "{0} is not found in backup catalogue".format(unlogged_idx_toast_init_filename)); |
| 166 | + |
| 167 | + # Clean after yourself |
| 168 | + self.del_test_dir(module_name, fname) |
0 commit comments