11 from pathlib import Path
13 BASE_PATH= os.path.dirname(__file__)
14 BCH_PATH = os.path.abspath(os.path.join(BASE_PATH, '../rust-src/target/release', 'bcachefs'))
15 VALGRIND_PATH= os.path.abspath(os.path.join(BASE_PATH,
16 'valgrind-suppressions.txt'))
18 VPAT = re.compile(r'ERROR SUMMARY: (\d+) errors from (\d+) contexts')
20 ENABLE_VALGRIND = os.getenv('BCACHEFS_TEST_USE_VALGRIND', 'no') == 'yes'
22 class ValgrindFailedError(Exception):
23 def __init__(self, log):
26 def check_valgrind(log):
29 print('Internal error: valgrind log did not match.')
30 print('-- valgrind log:')
32 print('-- end log --')
35 errors = int(m.group(1))
37 raise ValgrindFailedError(log)
39 def run(cmd, *args, valgrind=False, check=False):
40 """Run an external program via subprocess, optionally with valgrind.
42 This subprocess wrapper will capture the stdout and stderr. If valgrind is
43 requested, it will be checked for errors and raise a
44 ValgrindFailedError if there's a problem.
46 cmds = [cmd] + list(args)
47 valgrind = valgrind and ENABLE_VALGRIND
49 print("Running '{}'".format(cmds))
51 vout = tempfile.NamedTemporaryFile()
54 '--gen-suppressions=all',
55 '--suppressions={}'.format(VALGRIND_PATH),
56 '--log-file={}'.format(vout.name)]
59 res = subprocess.run(cmds, stdout=subprocess.PIPE,
60 stderr=subprocess.PIPE, encoding='utf-8', check=check)
61 check_valgrind(vout.read().decode('utf-8'))
63 res = subprocess.run(cmds, stdout=subprocess.PIPE,
64 stderr=subprocess.PIPE, encoding='utf-8', check=check)
68 def run_bch(*args, **kwargs):
69 """Wrapper to run the bcachefs binary specifically."""
70 cmds = [BCH_PATH] + list(args)
71 return run(*cmds, **kwargs)
73 def sparse_file(lpath, size):
74 """Construct a sparse file of the specified size.
76 This is typically used to create device files for bcachefs.
79 path.touch(mode = 0o600, exist_ok = False)
80 os.truncate(path, size)
84 def device_1g(tmpdir):
85 """Default 1g sparse file for use with bcachefs."""
86 path = tmpdir / 'dev-1g'
87 return sparse_file(path, 1024**3)
89 def format_1g(tmpdir):
90 """Format a default filesystem on a 1g device."""
91 dev = device_1g(tmpdir)
92 run_bch('format', dev, check=True)
95 def mountpoint(tmpdir):
96 """Construct a mountpoint "mnt" for tests."""
97 path = Path(tmpdir) / 'mnt'
98 path.mkdir(mode = 0o700)
102 '''Context manager to assist in verifying timestamps.
104 Records the range of times which would be valid for an encoded operation to
107 FIXME: The kernel code is currently using CLOCK_REALTIME_COARSE, but python
108 didn't expose this time API (yet). Probably the kernel shouldn't be using
109 _COARSE anyway, but this might lead to occasional errors.
111 To make sure this doesn't happen, we sleep a fraction of a second in an
112 attempt to guarantee containment.
114 N.B. this might be better tested by overriding the clock used in bcachefs.
122 self.start = time.clock_gettime(time.CLOCK_REALTIME)
126 def __exit__(self, type, value, traceback):
128 self.end = time.clock_gettime(time.CLOCK_REALTIME)
130 def contains(self, test):
131 '''True iff the test time is within the range.'''
132 return self.start <= test <= self.end
134 class FuseError(Exception):
135 def __init__(self, msg):
139 '''bcachefs fuse runner.
141 This class runs bcachefs in fusemount mode, and waits until the mount has
142 reached a point suitable for testing the filesystem.
144 bcachefs is run under valgrind by default, and is checked for errors.
147 def __init__(self, dev, mnt):
151 self.ready = threading.Event()
153 self.returncode = None
159 """Background thread which runs "bcachefs fusemount" under valgrind"""
165 vlog = tempfile.NamedTemporaryFile()
168 '--gen-suppressions=all',
169 '--suppressions=valgrind-suppressions.txt',
170 '--log-file={}'.format(vlog.name) ]
173 'fusemount', '-f', self.dev, self.mnt]
175 print("Running {}".format(cmd))
177 err = tempfile.TemporaryFile()
178 self.proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=err,
181 out1 = self.expect(self.proc.stdout, r'^Fuse mount initialized.$')
184 print("Waiting for process.")
185 (out2, _) = self.proc.communicate()
186 print("Process exited.")
188 self.returncode = self.proc.returncode
189 if self.returncode == 0:
190 errors = [ 'btree iterators leaked!',
191 'emergency read only!' ]
194 print('Debug error found in output: "{}"'.format(e))
195 self.returncode = errno.ENOMSG
197 self.stdout = out1 + out2
198 self.stderr = err.read()
200 self.vout = vlog.read().decode('utf-8')
202 def expect(self, pipe, regex):
203 """Wait for the child process to mount."""
205 c = re.compile(regex)
209 print('Expect line "{}"'.format(line.rstrip()))
215 raise FuseError('stdout did not contain regex "{}"'.format(regex))
218 print("Starting fuse thread.")
220 assert not self.thread
221 self.thread = threading.Thread(target=self.run)
225 print("Fuse is mounted.")
227 def unmount(self, timeout=None):
228 print("Unmounting fuse.")
229 run("fusermount3", "-zu", self.mnt)
232 print("Waiting for thread to exit.")
233 self.thread.join(timeout)
234 if self.thread.is_alive():
239 print("Thread was already done.")
245 check_valgrind(self.vout)
248 # avoid throwing exception in assertion
249 assert self.stdout is not None
250 assert self.stderr is not None
251 assert self.returncode == 0
252 assert len(self.stdout) > 0
253 assert len(self.stderr) == 0
256 res = run(BCH_PATH, 'fusemount', valgrind=False)
257 return "Please supply a mountpoint." in res.stdout