]> git.sesse.net Git - bcachefs-tools-debian/blob - tests/test_fixture.py
Merge commit '780de81b36'
[bcachefs-tools-debian] / tests / test_fixture.py
1 #!/usr/bin/python3
2 #
3 # Tests of the functions in util.py
4
5 import pytest
6 import signal
7 import subprocess
8 import time
9
10 import util
11 from pathlib import Path
12
13 #helper = Path('.') / 'test_helper'
14 helper = './test_helper'
15
16 def test_sparse_file(tmpdir):
17     dev = util.sparse_file(tmpdir / '1k', 1024)
18     assert dev.stat().st_size == 1024
19
20 def test_device_1g(tmpdir):
21     dev = util.device_1g(tmpdir)
22     assert dev.stat().st_size == 1024**3
23
24 def test_abort():
25     ret = util.run(helper, 'abort')
26     assert ret.returncode == -signal.SIGABRT
27
28 def test_segfault():
29     ret = util.run(helper, 'segfault')
30     assert ret.returncode == -signal.SIGSEGV
31
32 @pytest.mark.skipif(not util.ENABLE_VALGRIND, reason="no valgrind")
33 def test_check():
34     with pytest.raises(subprocess.CalledProcessError):
35         ret = util.run(helper, 'abort', check=True)
36
37 @pytest.mark.skipif(not util.ENABLE_VALGRIND, reason="no valgrind")
38 def test_leak():
39     with pytest.raises(util.ValgrindFailedError):
40         ret = util.run(helper, 'leak', valgrind=True)
41
42 @pytest.mark.skipif(not util.ENABLE_VALGRIND, reason="no valgrind")
43 def test_undefined():
44     with pytest.raises(util.ValgrindFailedError):
45         ret = util.run(helper, 'undefined', valgrind=True)
46
47 @pytest.mark.skipif(not util.ENABLE_VALGRIND, reason="no valgrind")
48 def test_undefined_branch():
49     with pytest.raises(util.ValgrindFailedError):
50         ret = util.run(helper, 'undefined_branch', valgrind=True)
51
52 @pytest.mark.skipif(not util.ENABLE_VALGRIND, reason="no valgrind")
53 def test_read_after_free():
54     with pytest.raises(util.ValgrindFailedError):
55         ret = util.run(helper, 'read_after_free', valgrind=True)
56
57 @pytest.mark.skipif(not util.ENABLE_VALGRIND, reason="no valgrind")
58 def test_write_after_free():
59     with pytest.raises(util.ValgrindFailedError):
60         ret = util.run(helper, 'write_after_free', valgrind=True)
61
62 def test_mountpoint(tmpdir):
63     path = util.mountpoint(tmpdir)
64     assert str(path)[-4:] == '/mnt'
65     assert path.is_dir()
66
67 def test_timestamp():
68     t1 = time.clock_gettime(time.CLOCK_REALTIME)
69     with util.Timestamp() as ts:
70         t2 = time.clock_gettime(time.CLOCK_REALTIME)
71     t3 = time.clock_gettime(time.CLOCK_REALTIME)
72
73     assert not ts.contains(t1)
74     assert ts.contains(t2)
75     assert not ts.contains(t3)