12 from pathlib import Path
15 BCH_PATH = DIR / 'bcachefs'
17 VPAT = re.compile(r'ERROR SUMMARY: (\d+) errors from (\d+) contexts')
19 class ValgrindFailedError(Exception):
20 def __init__(self, log):
23 def check_valgrind(logfile):
24 log = logfile.read().decode('utf-8')
26 assert m is not None, 'Internal error: valgrind log did not match.'
28 errors = int(m.group(1))
30 raise ValgrindFailedError(log)
32 def run(cmd, *args, valgrind=False, check=False):
33 """Run an external program via subprocess, optionally with valgrind.
35 This subprocess wrapper will capture the stdout and stderr. If valgrind is
36 requested, it will be checked for errors and raise a
37 ValgrindFailedError if there's a problem.
39 cmds = [cmd] + list(args)
42 vout = tempfile.NamedTemporaryFile()
45 '--log-file={}'.format(vout.name)]
48 print("Running '{}'".format(cmds))
49 res = subprocess.run(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
50 encoding='utf-8', check=check)
57 def run_bch(*args, **kwargs):
58 """Wrapper to run the bcachefs binary specifically."""
59 cmds = [BCH_PATH] + list(args)
60 return run(*cmds, **kwargs)
62 def sparse_file(lpath, size):
63 """Construct a sparse file of the specified size.
65 This is typically used to create device files for bcachefs.
68 f = path.touch(mode = 0o600, exist_ok = False)
69 os.truncate(path, size)
73 def device_1g(tmpdir):
74 """Default 1g sparse file for use with bcachefs."""
75 path = tmpdir / 'dev-1g'
76 return sparse_file(path, 1024**3)
78 def format_1g(tmpdir):
79 """Format a default filesystem on a 1g device."""
80 dev = device_1g(tmpdir)
81 run_bch('format', dev, check=True)
84 def mountpoint(tmpdir):
85 """Construct a mountpoint "mnt" for tests."""
86 path = Path(tmpdir) / 'mnt'
87 path.mkdir(mode = 0o700)
91 '''Context manager to assist in verifying timestamps.
93 Records the range of times which would be valid for an encoded operation to
96 FIXME: The kernel code is currently using CLOCK_REALTIME_COARSE, but python
97 didn't expose this time API (yet). Probably the kernel shouldn't be using
98 _COARSE anyway, but this might lead to occasional errors.
100 To make sure this doesn't happen, we sleep a fraction of a second in an
101 attempt to guarantee containment.
103 N.B. this might be better tested by overriding the clock used in bcachefs.
111 self.start = time.clock_gettime(time.CLOCK_REALTIME)
115 def __exit__(self, type, value, traceback):
117 self.end = time.clock_gettime(time.CLOCK_REALTIME)
119 def contains(self, test):
120 '''True iff the test time is within the range.'''
121 return self.start <= test <= self.end
123 class FuseError(Exception):
124 def __init__(self, msg):
127 class BFuse(threading.Thread):
128 '''bcachefs fuse runner.
130 This class runs bcachefs in fusemount mode, and waits until the mount has
131 reached a point suitable for testing the filesystem.
133 bcachefs is run under valgrind by default, and is checked for errors.
136 def __init__(self, dev, mnt):
137 threading.Thread.__init__(self)
140 self.ready = threading.Event()
142 self.returncode = None
148 """Background thread which runs "bcachefs fusemount" under valgrind"""
150 vout = tempfile.NamedTemporaryFile()
153 '--log-file={}'.format(vout.name),
155 'fusemount', '-f', self.dev, self.mnt]
157 print("Running {}".format(cmd))
159 err = tempfile.TemporaryFile()
160 self.proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=err,
163 out1 = self.expect(self.proc.stdout, r'^Fuse mount initialized.$')
166 print("Waiting for process.")
167 (out2, _) = self.proc.communicate()
168 print("Process exited.")
170 self.stdout = out1 + out2
171 self.stderr = err.read()
172 self.returncode = self.proc.returncode
175 def expect(self, pipe, regex):
176 """Wait for the child process to mount."""
178 c = re.compile(regex)
182 print('Expect line "{}"'.format(line.rstrip()))
188 raise FuseError('stdout did not contain regex "{}"'.format(regex))
191 print("Starting fuse thread.")
194 print("Fuse is mounted.")
196 def unmount(self, timeout=None):
197 print("Unmounting fuse.")
198 run("fusermount3", "-zu", self.mnt)
199 print("Waiting for thread to exit.")
206 check_valgrind(self.vout)
209 assert self.returncode == 0
210 assert len(self.stdout) > 0
211 assert len(self.stderr) == 0
214 res = run(BCH_PATH, 'fusemount', valgrind=False)
215 return "Please supply a mountpoint." in res.stdout