Skip to content

Commit ab61087

Browse files
committed
Merge bitcoin#28660: test: enable reindex readonly test on *BSD
5a0688a test: enable reindex readonly test on *BSD and macOS as root (Matthew Zipkin) Pull request description: see bitcoin#27850 (comment) OpenBSD and FreeBSD don't have `chattr` but they do have `chflags`, use that method to make the block file immutable for the reindex_readonly test. Written and tested on a VPS running FreeBSD: ``` FreeBSD freebsd-13-1 13.2-RELEASE-p4 FreeBSD 13.2-RELEASE-p4 GENERIC amd64 ``` ACKs for top commit: maflcko: re-cr-lgtm-ACK 5a0688a jonatack: ACK 5a0688a tested on macOS only theStack: ACK 5a0688a Tree-SHA512: 8c88d282d09c00355d22c4c504b779f60e420327a5e07bcf80fa77b97fefcb04952af9ceaf439d9033a0a2448cb26a02663fe6bddcd4a74792857cfbaf1c5162
2 parents d724bb5 + 5a0688a commit ab61087

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

test/functional/feature_reindex_readonly.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""
88

99
import os
10-
import platform
1110
import stat
1211
import subprocess
1312
from test_framework.test_framework import BitcoinTestFramework
@@ -34,11 +33,13 @@ def reindex_readonly(self):
3433
filename = self.nodes[0].chain_path / "blocks" / "blk00000.dat"
3534
filename.chmod(stat.S_IREAD)
3635

37-
used_chattr = False
38-
if platform.system() == "Linux":
36+
undo_immutable = lambda: None
37+
# Linux
38+
try:
39+
subprocess.run(['chattr'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
3940
try:
4041
subprocess.run(['chattr', '+i', filename], capture_output=True, check=True)
41-
used_chattr = True
42+
undo_immutable = lambda: subprocess.check_call(['chattr', '-i', filename])
4243
self.log.info("Made file immutable with chattr")
4344
except subprocess.CalledProcessError as e:
4445
self.log.warning(str(e))
@@ -50,15 +51,33 @@ def reindex_readonly(self):
5051
self.log.warning("Return early on Linux under root, because chattr failed.")
5152
self.log.warning("This should only happen due to missing capabilities in a container.")
5253
self.log.warning("Make sure to --cap-add LINUX_IMMUTABLE if you want to run this test.")
53-
return
54-
55-
self.log.debug("Attempt to restart and reindex the node with the unwritable block file")
56-
with self.nodes[0].assert_debug_log(expected_msgs=['FlushStateToDisk', 'failed to open file'], unexpected_msgs=[]):
57-
self.nodes[0].assert_start_raises_init_error(extra_args=['-reindex', '-fastprune'],
58-
expected_msg="Error: A fatal internal error occurred, see debug.log for details")
54+
undo_immutable = False
55+
except Exception:
56+
# macOS, and *BSD
57+
try:
58+
subprocess.run(['chflags'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
59+
try:
60+
subprocess.run(['chflags', 'uchg', filename], capture_output=True, check=True)
61+
undo_immutable = lambda: subprocess.check_call(['chflags', 'nouchg', filename])
62+
self.log.info("Made file immutable with chflags")
63+
except subprocess.CalledProcessError as e:
64+
self.log.warning(str(e))
65+
if e.stdout:
66+
self.log.warning(f"stdout: {e.stdout}")
67+
if e.stderr:
68+
self.log.warning(f"stderr: {e.stderr}")
69+
if os.getuid() == 0:
70+
self.log.warning("Return early on BSD under root, because chflags failed.")
71+
undo_immutable = False
72+
except Exception:
73+
pass
5974

60-
if used_chattr:
61-
subprocess.check_call(['chattr', '-i', filename])
75+
if undo_immutable:
76+
self.log.info("Attempt to restart and reindex the node with the unwritable block file")
77+
with self.nodes[0].assert_debug_log(expected_msgs=['FlushStateToDisk', 'failed to open file'], unexpected_msgs=[]):
78+
self.nodes[0].assert_start_raises_init_error(extra_args=['-reindex', '-fastprune'],
79+
expected_msg="Error: A fatal internal error occurred, see debug.log for details")
80+
undo_immutable()
6281

6382
filename.chmod(0o777)
6483

0 commit comments

Comments
 (0)