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