]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/pybind/test_cephfs.py
Import ceph 15.2.8
[ceph.git] / ceph / src / test / pybind / test_cephfs.py
1 # vim: expandtab smarttab shiftwidth=4 softtabstop=4
2 from nose.tools import assert_raises, assert_equal, assert_greater, with_setup
3 import cephfs as libcephfs
4 import fcntl
5 import os
6 import time
7 from datetime import datetime
8
9 cephfs = None
10
11 def setup_module():
12 global cephfs
13 cephfs = libcephfs.LibCephFS(conffile='')
14 cephfs.mount()
15
16 def teardown_module():
17 global cephfs
18 cephfs.shutdown()
19
20 def setup_test():
21 d = cephfs.opendir(b"/")
22 dent = cephfs.readdir(d)
23 while dent:
24 if (dent.d_name not in [b".", b".."]):
25 if dent.is_dir():
26 cephfs.rmdir(b"/" + dent.d_name)
27 else:
28 cephfs.unlink(b"/" + dent.d_name)
29
30 dent = cephfs.readdir(d)
31
32 cephfs.closedir(d)
33
34 cephfs.chdir(b"/")
35
36 @with_setup(setup_test)
37 def test_conf_get():
38 fsid = cephfs.conf_get("fsid")
39 assert(len(fsid) > 0)
40
41 @with_setup(setup_test)
42 def test_version():
43 cephfs.version()
44
45 @with_setup(setup_test)
46 def test_fstat():
47 fd = cephfs.open(b'file-1', 'w', 0o755)
48 stat = cephfs.fstat(fd)
49 assert(len(stat) == 13)
50 cephfs.close(fd)
51
52 @with_setup(setup_test)
53 def test_statfs():
54 stat = cephfs.statfs(b'/')
55 assert(len(stat) == 11)
56
57 @with_setup(setup_test)
58 def test_statx():
59 stat = cephfs.statx(b'/', libcephfs.CEPH_STATX_MODE, 0)
60 assert('mode' in stat.keys())
61 stat = cephfs.statx(b'/', libcephfs.CEPH_STATX_BTIME, 0)
62 assert('btime' in stat.keys())
63
64 fd = cephfs.open(b'file-1', 'w', 0o755)
65 cephfs.write(fd, b"1111", 0)
66 cephfs.close(fd)
67 cephfs.symlink(b'file-1', b'file-2')
68 stat = cephfs.statx(b'file-2', libcephfs.CEPH_STATX_MODE | libcephfs.CEPH_STATX_BTIME, libcephfs.AT_SYMLINK_NOFOLLOW)
69 assert('mode' in stat.keys())
70 assert('btime' in stat.keys())
71 cephfs.unlink(b'file-2')
72 cephfs.unlink(b'file-1')
73
74 @with_setup(setup_test)
75 def test_syncfs():
76 stat = cephfs.sync_fs()
77
78 @with_setup(setup_test)
79 def test_fsync():
80 fd = cephfs.open(b'file-1', 'w', 0o755)
81 cephfs.write(fd, b"asdf", 0)
82 stat = cephfs.fsync(fd, 0)
83 cephfs.write(fd, b"qwer", 0)
84 stat = cephfs.fsync(fd, 1)
85 cephfs.close(fd)
86 #sync on non-existing fd (assume fd 12345 is not exists)
87 assert_raises(libcephfs.Error, cephfs.fsync, 12345, 0)
88
89 @with_setup(setup_test)
90 def test_directory():
91 cephfs.mkdir(b"/temp-directory", 0o755)
92 cephfs.mkdirs(b"/temp-directory/foo/bar", 0o755)
93 cephfs.chdir(b"/temp-directory")
94 assert_equal(cephfs.getcwd(), b"/temp-directory")
95 cephfs.rmdir(b"/temp-directory/foo/bar")
96 cephfs.rmdir(b"/temp-directory/foo")
97 cephfs.rmdir(b"/temp-directory")
98 assert_raises(libcephfs.ObjectNotFound, cephfs.chdir, b"/temp-directory")
99
100 @with_setup(setup_test)
101 def test_walk_dir():
102 cephfs.chdir(b"/")
103 dirs = [b"dir-1", b"dir-2", b"dir-3"]
104 for i in dirs:
105 cephfs.mkdir(i, 0o755)
106 handler = cephfs.opendir(b"/")
107 d = cephfs.readdir(handler)
108 dirs += [b".", b".."]
109 while d:
110 assert(d.d_name in dirs)
111 dirs.remove(d.d_name)
112 d = cephfs.readdir(handler)
113 assert(len(dirs) == 0)
114 dirs = [b"/dir-1", b"/dir-2", b"/dir-3"]
115 for i in dirs:
116 cephfs.rmdir(i)
117 cephfs.closedir(handler)
118
119 @with_setup(setup_test)
120 def test_xattr():
121 assert_raises(libcephfs.OperationNotSupported, cephfs.setxattr, "/", "key", b"value", 0)
122 cephfs.setxattr("/", "user.key", b"value", 0)
123 assert_equal(b"value", cephfs.getxattr("/", "user.key"))
124
125 cephfs.setxattr("/", "user.big", b"x" * 300, 0)
126
127 # Default size is 255, get ERANGE
128 assert_raises(libcephfs.OutOfRange, cephfs.getxattr, "/", "user.big")
129
130 # Pass explicit size, and we'll get the value
131 assert_equal(300, len(cephfs.getxattr("/", "user.big", 300)))
132
133 cephfs.removexattr("/", "user.key")
134 # user.key is already removed
135 assert_raises(libcephfs.NoData, cephfs.getxattr, "/", "user.key")
136
137 # user.big is only listed
138 ret_val, ret_buff = cephfs.listxattr("/")
139 assert_equal(9, ret_val)
140 assert_equal("user.big\x00", ret_buff.decode('utf-8'))
141
142 @with_setup(setup_test)
143 def test_rename():
144 cephfs.mkdir(b"/a", 0o755)
145 cephfs.mkdir(b"/a/b", 0o755)
146 cephfs.rename(b"/a", b"/b")
147 cephfs.stat(b"/b/b")
148 cephfs.rmdir(b"/b/b")
149 cephfs.rmdir(b"/b")
150
151 @with_setup(setup_test)
152 def test_open():
153 assert_raises(libcephfs.ObjectNotFound, cephfs.open, b'file-1', 'r')
154 assert_raises(libcephfs.ObjectNotFound, cephfs.open, b'file-1', 'r+')
155 fd = cephfs.open(b'file-1', 'w', 0o755)
156 cephfs.write(fd, b"asdf", 0)
157 cephfs.close(fd)
158 fd = cephfs.open(b'file-1', 'r', 0o755)
159 assert_equal(cephfs.read(fd, 0, 4), b"asdf")
160 cephfs.close(fd)
161 fd = cephfs.open(b'file-1', 'r+', 0o755)
162 cephfs.write(fd, b"zxcv", 4)
163 assert_equal(cephfs.read(fd, 4, 8), b"zxcv")
164 cephfs.close(fd)
165 fd = cephfs.open(b'file-1', 'w+', 0o755)
166 assert_equal(cephfs.read(fd, 0, 4), b"")
167 cephfs.write(fd, b"zxcv", 4)
168 assert_equal(cephfs.read(fd, 4, 8), b"zxcv")
169 cephfs.close(fd)
170 fd = cephfs.open(b'file-1', os.O_RDWR, 0o755)
171 cephfs.write(fd, b"asdf", 0)
172 assert_equal(cephfs.read(fd, 0, 4), b"asdf")
173 cephfs.close(fd)
174 assert_raises(libcephfs.OperationNotSupported, cephfs.open, b'file-1', 'a')
175 cephfs.unlink(b'file-1')
176
177 @with_setup(setup_test)
178 def test_link():
179 fd = cephfs.open(b'file-1', 'w', 0o755)
180 cephfs.write(fd, b"1111", 0)
181 cephfs.close(fd)
182 cephfs.link(b'file-1', b'file-2')
183 fd = cephfs.open(b'file-2', 'r', 0o755)
184 assert_equal(cephfs.read(fd, 0, 4), b"1111")
185 cephfs.close(fd)
186 fd = cephfs.open(b'file-2', 'r+', 0o755)
187 cephfs.write(fd, b"2222", 4)
188 cephfs.close(fd)
189 fd = cephfs.open(b'file-1', 'r', 0o755)
190 assert_equal(cephfs.read(fd, 0, 8), b"11112222")
191 cephfs.close(fd)
192 cephfs.unlink(b'file-2')
193
194 @with_setup(setup_test)
195 def test_symlink():
196 fd = cephfs.open(b'file-1', 'w', 0o755)
197 cephfs.write(fd, b"1111", 0)
198 cephfs.close(fd)
199 cephfs.symlink(b'file-1', b'file-2')
200 fd = cephfs.open(b'file-2', 'r', 0o755)
201 assert_equal(cephfs.read(fd, 0, 4), b"1111")
202 cephfs.close(fd)
203 fd = cephfs.open(b'file-2', 'r+', 0o755)
204 cephfs.write(fd, b"2222", 4)
205 cephfs.close(fd)
206 fd = cephfs.open(b'file-1', 'r', 0o755)
207 assert_equal(cephfs.read(fd, 0, 8), b"11112222")
208 cephfs.close(fd)
209 cephfs.unlink(b'file-2')
210
211 @with_setup(setup_test)
212 def test_readlink():
213 fd = cephfs.open(b'/file-1', 'w', 0o755)
214 cephfs.write(fd, b"1111", 0)
215 cephfs.close(fd)
216 cephfs.symlink(b'/file-1', b'/file-2')
217 d = cephfs.readlink(b"/file-2",100)
218 assert_equal(d, b"/file-1")
219 cephfs.unlink(b'/file-2')
220 cephfs.unlink(b'/file-1')
221
222 @with_setup(setup_test)
223 def test_delete_cwd():
224 assert_equal(b"/", cephfs.getcwd())
225
226 cephfs.mkdir(b"/temp-directory", 0o755)
227 cephfs.chdir(b"/temp-directory")
228 cephfs.rmdir(b"/temp-directory")
229
230 # getcwd gives you something stale here: it remembers the path string
231 # even when things are unlinked. It's up to the caller to find out
232 # whether it really still exists
233 assert_equal(b"/temp-directory", cephfs.getcwd())
234
235 @with_setup(setup_test)
236 def test_flock():
237 fd = cephfs.open(b'file-1', 'w', 0o755)
238
239 cephfs.flock(fd, fcntl.LOCK_EX, 123);
240 fd2 = cephfs.open(b'file-1', 'w', 0o755)
241
242 assert_raises(libcephfs.WouldBlock, cephfs.flock, fd2,
243 fcntl.LOCK_EX | fcntl.LOCK_NB, 456);
244 cephfs.close(fd2)
245
246 cephfs.close(fd)
247
248 @with_setup(setup_test)
249 def test_mount_unmount():
250 test_directory()
251 cephfs.unmount()
252 cephfs.mount()
253 test_open()
254
255 @with_setup(setup_test)
256 def test_mount_root():
257 cephfs.mkdir(b"/mount-directory", 0o755)
258 cephfs.unmount()
259 cephfs.mount(mount_root = b"/mount-directory")
260
261 assert_raises(libcephfs.Error, cephfs.mount, mount_root = b"/nowhere")
262 cephfs.unmount()
263 cephfs.mount()
264
265 @with_setup(setup_test)
266 def test_utime():
267 fd = cephfs.open(b'/file-1', 'w', 0o755)
268 cephfs.write(fd, b'0000', 0)
269 cephfs.close(fd)
270
271 stx_pre = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
272
273 time.sleep(1)
274 cephfs.utime(b'/file-1')
275
276 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
277
278 assert_greater(stx_post['atime'], stx_pre['atime'])
279 assert_greater(stx_post['mtime'], stx_pre['mtime'])
280
281 atime_pre = int(time.mktime(stx_pre['atime'].timetuple()))
282 mtime_pre = int(time.mktime(stx_pre['mtime'].timetuple()))
283
284 cephfs.utime(b'/file-1', (atime_pre, mtime_pre))
285 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
286
287 assert_equal(stx_post['atime'], stx_pre['atime'])
288 assert_equal(stx_post['mtime'], stx_pre['mtime'])
289
290 cephfs.unlink(b'/file-1')
291
292 @with_setup(setup_test)
293 def test_futime():
294 fd = cephfs.open(b'/file-1', 'w', 0o755)
295 cephfs.write(fd, b'0000', 0)
296
297 stx_pre = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
298
299 time.sleep(1)
300 cephfs.futime(fd)
301
302 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
303
304 assert_greater(stx_post['atime'], stx_pre['atime'])
305 assert_greater(stx_post['mtime'], stx_pre['mtime'])
306
307 atime_pre = int(time.mktime(stx_pre['atime'].timetuple()))
308 mtime_pre = int(time.mktime(stx_pre['mtime'].timetuple()))
309
310 cephfs.futime(fd, (atime_pre, mtime_pre))
311 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
312
313 assert_equal(stx_post['atime'], stx_pre['atime'])
314 assert_equal(stx_post['mtime'], stx_pre['mtime'])
315
316 cephfs.close(fd)
317 cephfs.unlink(b'/file-1')
318
319 @with_setup(setup_test)
320 def test_utimes():
321 fd = cephfs.open(b'/file-1', 'w', 0o755)
322 cephfs.write(fd, b'0000', 0)
323 cephfs.close(fd)
324
325 stx_pre = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
326
327 time.sleep(1)
328 cephfs.utimes(b'/file-1')
329
330 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
331
332 assert_greater(stx_post['atime'], stx_pre['atime'])
333 assert_greater(stx_post['mtime'], stx_pre['mtime'])
334
335 atime_pre = time.mktime(stx_pre['atime'].timetuple())
336 mtime_pre = time.mktime(stx_pre['mtime'].timetuple())
337
338 cephfs.utimes(b'/file-1', (atime_pre, mtime_pre))
339 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
340
341 assert_equal(stx_post['atime'], stx_pre['atime'])
342 assert_equal(stx_post['mtime'], stx_pre['mtime'])
343
344 cephfs.unlink(b'/file-1')
345
346 @with_setup(setup_test)
347 def test_lutimes():
348 fd = cephfs.open(b'/file-1', 'w', 0o755)
349 cephfs.write(fd, b'0000', 0)
350 cephfs.close(fd)
351
352 cephfs.symlink(b'/file-1', b'/file-2')
353
354 stx_pre_t = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
355 stx_pre_s = cephfs.statx(b'/file-2', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, libcephfs.AT_SYMLINK_NOFOLLOW)
356
357 time.sleep(1)
358 cephfs.lutimes(b'/file-2')
359
360 stx_post_t = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
361 stx_post_s = cephfs.statx(b'/file-2', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, libcephfs.AT_SYMLINK_NOFOLLOW)
362
363 assert_equal(stx_post_t['atime'], stx_pre_t['atime'])
364 assert_equal(stx_post_t['mtime'], stx_pre_t['mtime'])
365
366 assert_greater(stx_post_s['atime'], stx_pre_s['atime'])
367 assert_greater(stx_post_s['mtime'], stx_pre_s['mtime'])
368
369 atime_pre = time.mktime(stx_pre_s['atime'].timetuple())
370 mtime_pre = time.mktime(stx_pre_s['mtime'].timetuple())
371
372 cephfs.lutimes(b'/file-2', (atime_pre, mtime_pre))
373 stx_post_s = cephfs.statx(b'/file-2', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, libcephfs.AT_SYMLINK_NOFOLLOW)
374
375 assert_equal(stx_post_s['atime'], stx_pre_s['atime'])
376 assert_equal(stx_post_s['mtime'], stx_pre_s['mtime'])
377
378 cephfs.unlink(b'/file-2')
379 cephfs.unlink(b'/file-1')
380
381 @with_setup(setup_test)
382 def test_futimes():
383 fd = cephfs.open(b'/file-1', 'w', 0o755)
384 cephfs.write(fd, b'0000', 0)
385
386 stx_pre = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
387
388 time.sleep(1)
389 cephfs.futimes(fd)
390
391 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
392
393 assert_greater(stx_post['atime'], stx_pre['atime'])
394 assert_greater(stx_post['mtime'], stx_pre['mtime'])
395
396 atime_pre = time.mktime(stx_pre['atime'].timetuple())
397 mtime_pre = time.mktime(stx_pre['mtime'].timetuple())
398
399 cephfs.futimes(fd, (atime_pre, mtime_pre))
400 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
401
402 assert_equal(stx_post['atime'], stx_pre['atime'])
403 assert_equal(stx_post['mtime'], stx_pre['mtime'])
404
405 cephfs.close(fd)
406 cephfs.unlink(b'/file-1')
407
408 @with_setup(setup_test)
409 def test_futimens():
410 fd = cephfs.open(b'/file-1', 'w', 0o755)
411 cephfs.write(fd, b'0000', 0)
412
413 stx_pre = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
414
415 time.sleep(1)
416 cephfs.futimens(fd)
417
418 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
419
420 assert_greater(stx_post['atime'], stx_pre['atime'])
421 assert_greater(stx_post['mtime'], stx_pre['mtime'])
422
423 atime_pre = time.mktime(stx_pre['atime'].timetuple())
424 mtime_pre = time.mktime(stx_pre['mtime'].timetuple())
425
426 cephfs.futimens(fd, (atime_pre, mtime_pre))
427 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
428
429 assert_equal(stx_post['atime'], stx_pre['atime'])
430 assert_equal(stx_post['mtime'], stx_pre['mtime'])
431
432 cephfs.close(fd)
433 cephfs.unlink(b'/file-1')
434
435 @with_setup(setup_test)
436 def test_disk_quota_exceeeded_error():
437 cephfs.mkdir("/dir-1", 0o755)
438 cephfs.setxattr("/dir-1", "ceph.quota.max_bytes", b"5", 0)
439 fd = cephfs.open(b'/dir-1/file-1', 'w', 0o755)
440 assert_raises(libcephfs.DiskQuotaExceeded, cephfs.write, fd, b"abcdeghiklmnopqrstuvwxyz", 0)
441 cephfs.close(fd)
442 cephfs.unlink(b"/dir-1/file-1")