Skip to content

Commit b11bd04

Browse files
committed
Merge bitcoin/bitcoin#26653: test, init: perturb file to ensure failure instead of only deleting them
c371cae test, init: perturb file to ensure failure instead of only deleting them (brunoerg) Pull request description: In `feature_init.py` there is a TODO about perturbing the files instead of only testing by deleting them. ```py # TODO: at some point, we should test perturbing the files instead of removing # them, e.g. # # contents = target_file.read_bytes() # tweaked_contents = bytearray(contents) # tweaked_contents[50:250] = b'1' * 200 # target_file.write_bytes(bytes(tweaked_contents)) # # At the moment I can't get this to work (bitcoind loads successfully?) so # investigate doing this later. ``` This PR adds it by writing into the file random bytes and checking whether it throws an error when starting. ACKs for top commit: MarcoFalke: lgtm ACK c371cae Tree-SHA512: d691eee60b91dd9d1b200588608f56b0a10dccd9761a75254b69e0ba5e5866cae14d2f90cb2bd7ec0f95b0617c2562cd33f20892ffd16355b6df770d3806a0ff
2 parents 6c7ebcc + c371cae commit b11bd04

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

test/functional/feature_init.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def sigterm_node():
4545
node.process.terminate()
4646
node.process.wait()
4747

48+
def start_expecting_error(err_fragment):
49+
node.assert_start_raises_init_error(
50+
extra_args=['-txindex=1', '-blockfilterindex=1', '-coinstatsindex=1'],
51+
expected_msg=err_fragment,
52+
match=ErrorMatch.PARTIAL_REGEX,
53+
)
54+
4855
def check_clean_start():
4956
"""Ensure that node restarts successfully after various interrupts."""
5057
node.start()
@@ -87,36 +94,27 @@ def check_clean_start():
8794

8895
self.log.info("Test startup errors after removing certain essential files")
8996

90-
files_to_disturb = {
97+
files_to_delete = {
9198
'blocks/index/*.ldb': 'Error opening block database.',
9299
'chainstate/*.ldb': 'Error opening block database.',
93100
'blocks/blk*.dat': 'Error loading block database.',
94101
}
95102

96-
for file_patt, err_fragment in files_to_disturb.items():
103+
files_to_perturb = {
104+
'blocks/index/*.ldb': 'Error opening block database.',
105+
'chainstate/*.ldb': 'Error opening block database.',
106+
'blocks/blk*.dat': 'Error opening block database.',
107+
}
108+
109+
for file_patt, err_fragment in files_to_delete.items():
97110
target_files = list(node.chain_path.glob(file_patt))
98111

99112
for target_file in target_files:
100-
self.log.info(f"Tweaking file to ensure failure {target_file}")
113+
self.log.info(f"Deleting file to ensure failure {target_file}")
101114
bak_path = str(target_file) + ".bak"
102115
target_file.rename(bak_path)
103116

104-
# TODO: at some point, we should test perturbing the files instead of removing
105-
# them, e.g.
106-
#
107-
# contents = target_file.read_bytes()
108-
# tweaked_contents = bytearray(contents)
109-
# tweaked_contents[50:250] = b'1' * 200
110-
# target_file.write_bytes(bytes(tweaked_contents))
111-
#
112-
# At the moment I can't get this to work (bitcoind loads successfully?) so
113-
# investigate doing this later.
114-
115-
node.assert_start_raises_init_error(
116-
extra_args=['-txindex=1', '-blockfilterindex=1', '-coinstatsindex=1'],
117-
expected_msg=err_fragment,
118-
match=ErrorMatch.PARTIAL_REGEX,
119-
)
117+
start_expecting_error(err_fragment)
120118

121119
for target_file in target_files:
122120
bak_path = str(target_file) + ".bak"
@@ -126,6 +124,18 @@ def check_clean_start():
126124
check_clean_start()
127125
self.stop_node(0)
128126

127+
for file_patt, err_fragment in files_to_perturb.items():
128+
target_files = list(node.chain_path.glob(file_patt))
129+
130+
for target_file in target_files:
131+
self.log.info(f"Perturbing file to ensure failure {target_file}")
132+
with open(target_file, "rb") as tf_read, open(target_file, "wb") as tf_write:
133+
contents = tf_read.read()
134+
tweaked_contents = bytearray(contents)
135+
tweaked_contents[50:250] = b'1' * 200
136+
tf_write.write(bytes(tweaked_contents))
137+
138+
start_expecting_error(err_fragment)
129139

130140
if __name__ == '__main__':
131141
InitStressTest().main()

0 commit comments

Comments
 (0)