]> git.sesse.net Git - bcachefs-tools-debian/blob - tests/test_fuse.py
Make fuse3 support optional and document.
[bcachefs-tools-debian] / tests / test_fuse.py
1 #!/usr/bin/python3
2 #
3 # Tests of the fuse mount functionality.
4
5 import pytest
6 import os
7 import util
8
9 pytestmark = pytest.mark.skipif(
10     not util.have_fuse(), reason="bcachefs not built with fuse support.")
11
12 def test_mount(bfuse):
13     bfuse.mount()
14     bfuse.unmount()
15     bfuse.verify()
16
17 def test_lostfound(bfuse):
18     bfuse.mount()
19
20     lf = bfuse.mnt / "lost+found"
21     assert lf.is_dir()
22
23     st = lf.stat()
24     assert st.st_mode == 0o40700
25
26     bfuse.unmount()
27     bfuse.verify()
28
29 def test_create(bfuse):
30     bfuse.mount()
31
32     path = bfuse.mnt / "file"
33
34     with util.Timestamp() as ts:
35         fd = os.open(path, os.O_CREAT, 0o700)
36
37     assert fd >= 0
38
39     os.close(fd)
40     assert path.is_file()
41
42     # Verify file.
43     st = path.stat()
44     assert st.st_mode == 0o100700
45     assert st.st_mtime == st.st_ctime
46     assert st.st_mtime == st.st_atime
47     assert ts.contains(st.st_mtime)
48
49     # Verify dir.
50     dst = bfuse.mnt.stat()
51     assert dst.st_mtime == dst.st_ctime
52     assert ts.contains(dst.st_mtime)
53
54     bfuse.unmount()
55     bfuse.verify()
56
57 def test_mkdir(bfuse):
58     bfuse.mount()
59
60     path = bfuse.mnt / "dir"
61
62     with util.Timestamp() as ts:
63         os.mkdir(path, 0o700)
64
65     assert path.is_dir()
66
67     # Verify child.
68     st = path.stat()
69     assert st.st_mode == 0o40700
70     assert st.st_mtime == st.st_ctime
71     assert st.st_mtime == st.st_atime
72     assert ts.contains(st.st_mtime)
73
74     # Verify parent.
75     dst = bfuse.mnt.stat()
76     assert dst.st_mtime == dst.st_ctime
77     assert ts.contains(dst.st_mtime)
78
79     bfuse.unmount()
80     bfuse.verify()
81
82 def test_unlink(bfuse):
83     bfuse.mount()
84
85     path = bfuse.mnt / "file"
86     path.touch(mode=0o600, exist_ok=False)
87
88     with util.Timestamp() as ts:
89         os.unlink(path)
90
91     assert not path.exists()
92
93     # Verify dir.
94     dst = bfuse.mnt.stat()
95     assert dst.st_mtime == dst.st_ctime
96     assert ts.contains(dst.st_mtime)
97
98     bfuse.unmount()
99     bfuse.verify()
100
101 def test_rmdir(bfuse):
102     bfuse.mount()
103
104     path = bfuse.mnt / "dir"
105     path.mkdir(mode=0o700, exist_ok=False)
106
107     with util.Timestamp() as ts:
108         os.rmdir(path)
109
110     assert not path.exists()
111
112     # Verify dir.
113     dst = bfuse.mnt.stat()
114     assert dst.st_mtime == dst.st_ctime
115     assert ts.contains(dst.st_mtime)
116
117     bfuse.unmount()
118     bfuse.verify()
119
120 def test_rename(bfuse):
121     bfuse.mount()
122
123     srcdir = bfuse.mnt
124
125     path = srcdir / "file"
126     path.touch(mode=0o600, exist_ok=False)
127
128     destdir = srcdir / "dir"
129     destdir.mkdir(mode=0o700, exist_ok=False)
130
131     destpath = destdir / "file"
132
133     path_pre_st = path.stat()
134
135     with util.Timestamp() as ts:
136         os.rename(path, destpath)
137
138     assert not path.exists()
139     assert destpath.is_file()
140
141     # Verify dirs.
142     src_st = srcdir.stat()
143     assert src_st.st_mtime == src_st.st_ctime
144     assert ts.contains(src_st.st_mtime)
145
146     dest_st = destdir.stat()
147     assert dest_st.st_mtime == dest_st.st_ctime
148     assert ts.contains(dest_st.st_mtime)
149
150     # Verify file.
151     path_post_st = destpath.stat()
152     assert path_post_st.st_mtime == path_pre_st.st_mtime
153     assert path_post_st.st_atime == path_pre_st.st_atime
154     assert ts.contains(path_post_st.st_ctime)
155
156     bfuse.unmount()
157     bfuse.verify()
158
159 def test_link(bfuse):
160     bfuse.mount()
161
162     srcdir = bfuse.mnt
163
164     path = srcdir / "file"
165     path.touch(mode=0o600, exist_ok=False)
166
167     destdir = srcdir / "dir"
168     destdir.mkdir(mode=0o700, exist_ok=False)
169
170     destpath = destdir / "file"
171
172     path_pre_st = path.stat()
173     srcdir_pre_st = srcdir.stat()
174
175     with util.Timestamp() as ts:
176         os.link(path, destpath)
177
178     assert path.exists()
179     assert destpath.is_file()
180
181     # Verify source dir is unchanged.
182     srcdir_post_st = srcdir.stat()
183     assert srcdir_pre_st == srcdir_post_st
184
185     # Verify dest dir.
186     destdir_st = destdir.stat()
187     assert destdir_st.st_mtime == destdir_st.st_ctime
188     assert ts.contains(destdir_st.st_mtime)
189
190     # Verify file.
191     path_post_st = path.stat()
192     destpath_post_st = destpath.stat()
193     assert path_post_st == destpath_post_st
194
195     assert path_post_st.st_mtime == path_pre_st.st_mtime
196     assert path_post_st.st_atime == path_pre_st.st_atime
197     assert ts.contains(path_post_st.st_ctime)
198
199     bfuse.unmount()
200     bfuse.verify()
201
202 def test_write(bfuse):
203     bfuse.mount()
204
205     path = bfuse.mnt / "file"
206     path.touch(mode=0o600, exist_ok=False)
207
208     pre_st = path.stat()
209
210     fd = os.open(path, os.O_WRONLY)
211     assert fd >= 0
212
213     with util.Timestamp() as ts:
214         written = os.write(fd, b'test')
215
216     os.close(fd)
217
218     assert written == 4
219
220     post_st = path.stat()
221     assert post_st.st_atime == pre_st.st_atime
222     assert post_st.st_mtime == post_st.st_ctime
223     assert ts.contains(post_st.st_mtime)
224
225     assert path.read_bytes() == b'test'