11 from pathlib import Path
14 BCH_PATH = DIR / 'bcachefs'
16 VPAT = re.compile(r'ERROR SUMMARY: (\d+) errors from (\d+) contexts')
18 class ValgrindFailedError(Exception):
19 def __init__(self, log):
22 def check_valgrind(logfile):
23 log = logfile.read().decode('utf-8')
25 assert m is not None, 'Internal error: valgrind log did not match.'
27 errors = int(m.group(1))
29 raise ValgrindFailedError(log)
31 def run(cmd, *args, valgrind=False, check=False):
32 """Run an external program via subprocess, optionally with valgrind.
34 This subprocess wrapper will capture the stdout and stderr. If valgrind is
35 requested, it will be checked for errors and raise a
36 ValgrindFailedError if there's a problem.
38 cmds = [cmd] + list(args)
41 vout = tempfile.NamedTemporaryFile()
44 '--log-file={}'.format(vout.name)]
47 print("Running '{}'".format(cmds))
48 res = subprocess.run(cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
49 encoding='utf-8', check=check)
56 def run_bch(*args, **kwargs):
57 """Wrapper to run the bcachefs binary specifically."""
58 cmds = [BCH_PATH] + list(args)
59 return run(*cmds, **kwargs)
61 def sparse_file(lpath, size):
62 """Construct a sparse file of the specified size.
64 This is typically used to create device files for bcachefs.
67 f = path.touch(mode = 0o600, exist_ok = False)
68 os.truncate(path, size)
72 def device_1g(tmpdir):
73 """Default 1g sparse file for use with bcachefs."""
74 path = tmpdir / 'dev-1g'
75 return sparse_file(path, 1024**3)
77 def format_1g(tmpdir):
78 """Format a default filesystem on a 1g device."""
79 dev = device_1g(tmpdir)
80 run_bch('format', dev, check=True)
83 def mountpoint(tmpdir):
84 """Construct a mountpoint "mnt" for tests."""
85 path = Path(tmpdir) / 'mnt'
86 path.mkdir(mode = 0o700)
90 '''Context manager to assist in verifying timestamps.
92 Records the range of times which would be valid for an encoded operation to
95 FIXME: The kernel code is currently using CLOCK_REALTIME_COARSE, but python
96 didn't expose this time API (yet). Probably the kernel shouldn't be using
97 _COARSE anyway, but this might lead to occasional errors.
99 To make sure this doesn't happen, we sleep a fraction of a second in an
100 attempt to guarantee containment.
102 N.B. this might be better tested by overriding the clock used in bcachefs.
110 self.start = time.clock_gettime(time.CLOCK_REALTIME)
114 def __exit__(self, type, value, traceback):
116 self.end = time.clock_gettime(time.CLOCK_REALTIME)
118 def contains(self, test):
119 '''True iff the test time is within the range.'''
120 return self.start <= test <= self.end
122 class FuseError(Exception):
123 def __init__(self, msg):
126 class BFuse(threading.Thread):
127 '''bcachefs fuse runner.
129 This class runs bcachefs in fusemount mode, and waits until the mount has
130 reached a point suitable for testing the filesystem.
132 bcachefs is run under valgrind by default, and is checked for errors.
135 def __init__(self, dev, mnt):
136 threading.Thread.__init__(self)
139 self.ready = threading.Event()
141 self.returncode = None
147 """Background thread which runs "bcachefs fusemount" under valgrind"""
149 vout = tempfile.NamedTemporaryFile()
152 '--log-file={}'.format(vout.name),
154 'fusemount', '-f', self.dev, self.mnt]
156 print("Running {}".format(cmd))
158 err = tempfile.TemporaryFile()
159 self.proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=err,
162 out1 = self.expect(self.proc.stdout, r'^Fuse mount initialized.$')
165 print("Waiting for process.")
166 (out2, _) = self.proc.communicate()
167 print("Process exited.")
169 self.stdout = out1 + out2
170 self.stderr = err.read()
171 self.returncode = self.proc.returncode
174 def expect(self, pipe, regex):
175 """Wait for the child process to mount."""
177 c = re.compile(regex)
181 print('Expect line "{}"'.format(line.rstrip()))
187 raise FuseError('stdout did not contain regex "{}"'.format(regex))
190 print("Starting fuse thread.")
193 print("Fuse is mounted.")
195 def unmount(self, timeout=None):
196 print("Unmounting fuse.")
197 run("fusermount3", "-zu", self.mnt)
198 print("Waiting for thread to exit.")
205 check_valgrind(self.vout)
208 assert self.returncode == 0
209 assert len(self.stdout) > 0
210 assert len(self.stderr) == 0