Skip to content

Commit 0f85328

Browse files
committed
tests: pgdata_compare block-level granularity
1 parent 41de50c commit 0f85328

File tree

2 files changed

+72
-26
lines changed

2 files changed

+72
-26
lines changed

tests/helpers/ptrack_helpers.py

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,10 @@ def run_pb(self, command, async=False, gdb=False):
476476
raise ProbackupException(e.output.decode("utf-8"), self.cmd)
477477

478478
def run_binary(self, command, async=False):
479+
if self.verbose:
480+
print([' '.join(map(str, command))])
479481
try:
480482
if async:
481-
if self.verbose:
482-
print(command)
483483
return subprocess.Popen(
484484
command,
485485
stdin=subprocess.PIPE,
@@ -488,8 +488,6 @@ def run_binary(self, command, async=False):
488488
env=self.test_env
489489
)
490490
else:
491-
if self.verbose:
492-
print(command)
493491
self.output = subprocess.check_output(
494492
command,
495493
stderr=subprocess.STDOUT,
@@ -859,46 +857,92 @@ def pgdata_content(self, directory):
859857
]
860858
suffixes_to_ignore = (
861859
'_ptrack', 'ptrack_control',
862-
'pg_control', 'ptrack_init'
860+
'pg_control', 'ptrack_init', 'backup_label'
863861
)
864862
directory_dict = {}
865863
directory_dict['pgdata'] = directory
866864
directory_dict['files'] = {}
867865
for root, dirs, files in os.walk(directory, followlinks=True):
868866
dirs[:] = [d for d in dirs if d not in dirs_to_ignore]
869867
for file in files:
870-
if file in files_to_ignore or file.endswith(
871-
suffixes_to_ignore
872-
):
873-
continue
874-
file = os.path.join(root, file)
875-
file_relpath = os.path.relpath(file, directory)
876-
directory_dict['files'][file_relpath] = hashlib.md5(
877-
open(file, 'rb').read()).hexdigest()
868+
if (
869+
file in files_to_ignore or
870+
file.endswith(suffixes_to_ignore)
871+
):
872+
continue
873+
874+
file_fullpath = os.path.join(root, file)
875+
file_relpath = os.path.relpath(file_fullpath, directory)
876+
directory_dict['files'][file_relpath] = {'is_datafile': False}
877+
directory_dict['files'][file_relpath]['md5'] = hashlib.md5(
878+
open(file_fullpath, 'rb').read()).hexdigest()
879+
880+
if file.isdigit():
881+
directory_dict['files'][file_relpath]['is_datafile'] = True
882+
size_in_pages = os.path.getsize(file_fullpath)/8192
883+
directory_dict['files'][file_relpath][
884+
'md5_per_page'] = self.get_md5_per_page_for_fork(
885+
file_fullpath, size_in_pages
886+
)
887+
878888
return directory_dict
879889

880890
def compare_pgdata(self, original_pgdata, restored_pgdata):
881891
""" return dict with directory content. DO IT BEFORE RECOVERY"""
882892
fail = False
883893
error_message = ''
894+
for file in restored_pgdata['files']:
895+
# File is present in RESTORED PGDATA
896+
# but not present in ORIGINAL
897+
if (
898+
file not in original_pgdata['files'] and
899+
not file.endswith('backup_label')
900+
):
901+
fail = True
902+
error_message += 'File is not present'
903+
error_message += ' in original PGDATA:\n {0}'.format(
904+
os.path.join(restored_pgdata['pgdata'], file)
905+
)
884906
for file in original_pgdata['files']:
885907
if file in restored_pgdata['files']:
908+
886909
if (
887-
original_pgdata['files'][file] !=
888-
restored_pgdata['files'][file]
910+
original_pgdata['files'][file]['md5'] !=
911+
restored_pgdata['files'][file]['md5']
889912
):
890-
error_message += '\nChecksumm mismatch.\n'
891-
' File_old: {0}\n Checksumm_old: {1}\n'
892-
' File_new: {2}\n Checksumm_new: {3}\n'.format(
913+
error_message += (
914+
'\nChecksumm mismatch.\n'
915+
' File_old: {0}\n Checksumm_old: {1}\n'
916+
' File_new: {2}\n Checksumm_new: {3}\n').format(
893917
os.path.join(original_pgdata['pgdata'], file),
894-
original_pgdata['files'][file],
918+
original_pgdata['files'][file]['md5'],
895919
os.path.join(restored_pgdata['pgdata'], file),
896-
restored_pgdata['files'][file]
897-
)
920+
restored_pgdata['files'][file]['md5']
921+
)
898922
fail = True
923+
if original_pgdata['files'][file]['is_datafile']:
924+
for page in original_pgdata['files'][
925+
file]['md5_per_page']:
926+
if original_pgdata['files'][file][
927+
'md5_per_page'][page] != restored_pgdata[
928+
'files'][file]['md5_per_page'][page]:
929+
error_message += (
930+
'PAGE: {0}\n'
931+
' PAGE Checksumm_old: {1}\n'
932+
' PAGE Checksumm_new: {2}\n'
933+
).format(
934+
page,
935+
original_pgdata['files'][file][
936+
'md5_per_page'][page],
937+
restored_pgdata['files'][file][
938+
'md5_per_page'][page])
939+
899940
else:
900-
error_message += '\nFile dissappearance.'
901-
' File: {0}/{1}'.format(restored_pgdata['pgdata'], file)
941+
error_message += (
942+
'\nFile dissappearance.'
943+
' File: {0}').format(
944+
os.path.join(restored_pgdata['pgdata'], file)
945+
)
902946
fail = True
903947
self.assertFalse(fail, error_message)
904948

tests/ptrack.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,6 @@ def test_ptrack_get_block(self):
175175
node.safe_psql(
176176
"postgres",
177177
"update t_heap set id = 100500")
178-
# print(node.safe_psql(
179-
# "postgres",
180-
# "select * from t_heap"))
181178

182179
if not gdb.continue_execution():
183180
self.assertTrue(
@@ -189,16 +186,21 @@ def test_ptrack_get_block(self):
189186
backup_dir, 'node', node,
190187
backup_type='ptrack', options=['--stream']
191188
)
189+
pgdata = self.pgdata_content(node.data_dir)
192190

193191
result = node.safe_psql("postgres", "SELECT * FROM t_heap")
194192
node.cleanup()
195193
self.restore_node(backup_dir, 'node', node, options=["-j", "4"])
194+
pgdata_restored = self.pgdata_content(node.data_dir)
196195

197196
node.start()
197+
# Logical comparison
198198
self.assertEqual(
199199
result,
200200
node.safe_psql("postgres", "SELECT * FROM t_heap")
201201
)
202+
# Physical comparison
203+
self.compare_pgdata(pgdata, pgdata_restored)
202204

203205
# Clean after yourself
204206
# self.del_test_dir(module_name, fname)

0 commit comments

Comments
 (0)