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