]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/pybind/test_cephfs.py
a3b1d494e6a952b7a5cb375eed2e35f059a22039
[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_not_equal, assert_greater, with_setup
3 import cephfs as libcephfs
4 import fcntl
5 import os
6 import random
7 import time
8 import stat
9 import uuid
10 from datetime import datetime
11
12 cephfs = None
13
14 def setup_module():
15 global cephfs
16 cephfs = libcephfs.LibCephFS(conffile='')
17 cephfs.mount()
18
19 def teardown_module():
20 global cephfs
21 cephfs.shutdown()
22
23 def setup_test():
24 d = cephfs.opendir(b"/")
25 dent = cephfs.readdir(d)
26 while dent:
27 if (dent.d_name not in [b".", b".."]):
28 if dent.is_dir():
29 cephfs.rmdir(b"/" + dent.d_name)
30 else:
31 cephfs.unlink(b"/" + dent.d_name)
32
33 dent = cephfs.readdir(d)
34
35 cephfs.closedir(d)
36
37 cephfs.chdir(b"/")
38 _, ret_buf = cephfs.listxattr("/")
39 print(f'ret_buf={ret_buf}')
40 xattrs = ret_buf.decode('utf-8').split('\x00')
41 for xattr in xattrs[:-1]:
42 cephfs.removexattr("/", xattr)
43
44 @with_setup(setup_test)
45 def test_conf_get():
46 fsid = cephfs.conf_get("fsid")
47 assert(len(fsid) > 0)
48
49 @with_setup(setup_test)
50 def test_version():
51 cephfs.version()
52
53 @with_setup(setup_test)
54 def test_fstat():
55 fd = cephfs.open(b'file-1', 'w', 0o755)
56 stat = cephfs.fstat(fd)
57 assert(len(stat) == 13)
58 cephfs.close(fd)
59
60 @with_setup(setup_test)
61 def test_statfs():
62 stat = cephfs.statfs(b'/')
63 assert(len(stat) == 11)
64
65 @with_setup(setup_test)
66 def test_statx():
67 stat = cephfs.statx(b'/', libcephfs.CEPH_STATX_MODE, 0)
68 assert('mode' in stat.keys())
69 stat = cephfs.statx(b'/', libcephfs.CEPH_STATX_BTIME, 0)
70 assert('btime' in stat.keys())
71
72 fd = cephfs.open(b'file-1', 'w', 0o755)
73 cephfs.write(fd, b"1111", 0)
74 cephfs.close(fd)
75 cephfs.symlink(b'file-1', b'file-2')
76 stat = cephfs.statx(b'file-2', libcephfs.CEPH_STATX_MODE | libcephfs.CEPH_STATX_BTIME, libcephfs.AT_SYMLINK_NOFOLLOW)
77 assert('mode' in stat.keys())
78 assert('btime' in stat.keys())
79 cephfs.unlink(b'file-2')
80 cephfs.unlink(b'file-1')
81
82 @with_setup(setup_test)
83 def test_syncfs():
84 stat = cephfs.sync_fs()
85
86 @with_setup(setup_test)
87 def test_fsync():
88 fd = cephfs.open(b'file-1', 'w', 0o755)
89 cephfs.write(fd, b"asdf", 0)
90 stat = cephfs.fsync(fd, 0)
91 cephfs.write(fd, b"qwer", 0)
92 stat = cephfs.fsync(fd, 1)
93 cephfs.close(fd)
94 #sync on non-existing fd (assume fd 12345 is not exists)
95 assert_raises(libcephfs.Error, cephfs.fsync, 12345, 0)
96
97 @with_setup(setup_test)
98 def test_directory():
99 cephfs.mkdir(b"/temp-directory", 0o755)
100 cephfs.mkdirs(b"/temp-directory/foo/bar", 0o755)
101 cephfs.chdir(b"/temp-directory")
102 assert_equal(cephfs.getcwd(), b"/temp-directory")
103 cephfs.rmdir(b"/temp-directory/foo/bar")
104 cephfs.rmdir(b"/temp-directory/foo")
105 cephfs.rmdir(b"/temp-directory")
106 assert_raises(libcephfs.ObjectNotFound, cephfs.chdir, b"/temp-directory")
107
108 @with_setup(setup_test)
109 def test_walk_dir():
110 cephfs.chdir(b"/")
111 dirs = [b"dir-1", b"dir-2", b"dir-3"]
112 for i in dirs:
113 cephfs.mkdir(i, 0o755)
114 handler = cephfs.opendir(b"/")
115 d = cephfs.readdir(handler)
116 dirs += [b".", b".."]
117 while d:
118 assert(d.d_name in dirs)
119 dirs.remove(d.d_name)
120 d = cephfs.readdir(handler)
121 assert(len(dirs) == 0)
122 dirs = [b"/dir-1", b"/dir-2", b"/dir-3"]
123 for i in dirs:
124 cephfs.rmdir(i)
125 cephfs.closedir(handler)
126
127 @with_setup(setup_test)
128 def test_xattr():
129 assert_raises(libcephfs.OperationNotSupported, cephfs.setxattr, "/", "key", b"value", 0)
130 cephfs.setxattr("/", "user.key", b"value", 0)
131 assert_equal(b"value", cephfs.getxattr("/", "user.key"))
132
133 cephfs.setxattr("/", "user.big", b"x" * 300, 0)
134
135 # Default size is 255, get ERANGE
136 assert_raises(libcephfs.OutOfRange, cephfs.getxattr, "/", "user.big")
137
138 # Pass explicit size, and we'll get the value
139 assert_equal(300, len(cephfs.getxattr("/", "user.big", 300)))
140
141 cephfs.removexattr("/", "user.key")
142 # user.key is already removed
143 assert_raises(libcephfs.NoData, cephfs.getxattr, "/", "user.key")
144
145 # user.big is only listed
146 ret_val, ret_buff = cephfs.listxattr("/")
147 assert_equal(9, ret_val)
148 assert_equal("user.big\x00", ret_buff.decode('utf-8'))
149
150 @with_setup(setup_test)
151 def test_ceph_mirror_xattr():
152 def gen_mirror_xattr():
153 cluster_id = str(uuid.uuid4())
154 fs_id = random.randint(1, 10)
155 mirror_xattr = f'cluster_id={cluster_id} fs_id={fs_id}'
156 return mirror_xattr.encode('utf-8')
157
158 mirror_xattr_enc_1 = gen_mirror_xattr()
159
160 # mirror xattr is only allowed on root
161 cephfs.mkdir('/d0', 0o755)
162 assert_raises(libcephfs.InvalidValue, cephfs.setxattr,
163 '/d0', 'ceph.mirror.info', mirror_xattr_enc_1, os.XATTR_CREATE)
164 cephfs.rmdir('/d0')
165
166 cephfs.setxattr('/', 'ceph.mirror.info', mirror_xattr_enc_1, os.XATTR_CREATE)
167 assert_equal(mirror_xattr_enc_1, cephfs.getxattr('/', 'ceph.mirror.info'))
168
169 # setting again with XATTR_CREATE should fail
170 assert_raises(libcephfs.ObjectExists, cephfs.setxattr,
171 '/', 'ceph.mirror.info', mirror_xattr_enc_1, os.XATTR_CREATE)
172
173 # ceph.mirror.info should not show up in listing
174 ret_val, _ = cephfs.listxattr("/")
175 assert_equal(0, ret_val)
176
177 mirror_xattr_enc_2 = gen_mirror_xattr()
178
179 cephfs.setxattr('/', 'ceph.mirror.info', mirror_xattr_enc_2, os.XATTR_REPLACE)
180 assert_equal(mirror_xattr_enc_2, cephfs.getxattr('/', 'ceph.mirror.info'))
181
182 cephfs.removexattr('/', 'ceph.mirror.info')
183 # ceph.mirror.info is already removed
184 assert_raises(libcephfs.NoData, cephfs.getxattr, '/', 'ceph.mirror.info')
185 # removing again should throw error
186 assert_raises(libcephfs.NoData, cephfs.removexattr, "/", "ceph.mirror.info")
187
188 # check mirror info xattr format
189 assert_raises(libcephfs.InvalidValue, cephfs.setxattr, '/', 'ceph.mirror.info', b"unknown", 0)
190
191 @with_setup(setup_test)
192 def test_fxattr():
193 fd = cephfs.open(b'/file-fxattr', 'w', 0o755)
194 assert_raises(libcephfs.OperationNotSupported, cephfs.fsetxattr, fd, "key", b"value", 0)
195 assert_raises(TypeError, cephfs.fsetxattr, "fd", "user.key", b"value", 0)
196 assert_raises(TypeError, cephfs.fsetxattr, fd, "user.key", "value", 0)
197 assert_raises(TypeError, cephfs.fsetxattr, fd, "user.key", b"value", "0")
198 cephfs.fsetxattr(fd, "user.key", b"value", 0)
199 assert_equal(b"value", cephfs.fgetxattr(fd, "user.key"))
200
201 cephfs.fsetxattr(fd, "user.big", b"x" * 300, 0)
202
203 # Default size is 255, get ERANGE
204 assert_raises(libcephfs.OutOfRange, cephfs.fgetxattr, fd, "user.big")
205
206 # Pass explicit size, and we'll get the value
207 assert_equal(300, len(cephfs.fgetxattr(fd, "user.big", 300)))
208
209 cephfs.fremovexattr(fd, "user.key")
210 # user.key is already removed
211 assert_raises(libcephfs.NoData, cephfs.fgetxattr, fd, "user.key")
212
213 # user.big is only listed
214 ret_val, ret_buff = cephfs.flistxattr(fd)
215 assert_equal(9, ret_val)
216 assert_equal("user.big\x00", ret_buff.decode('utf-8'))
217 cephfs.close(fd)
218 cephfs.unlink(b'/file-fxattr')
219
220 @with_setup(setup_test)
221 def test_rename():
222 cephfs.mkdir(b"/a", 0o755)
223 cephfs.mkdir(b"/a/b", 0o755)
224 cephfs.rename(b"/a", b"/b")
225 cephfs.stat(b"/b/b")
226 cephfs.rmdir(b"/b/b")
227 cephfs.rmdir(b"/b")
228
229 @with_setup(setup_test)
230 def test_open():
231 assert_raises(libcephfs.ObjectNotFound, cephfs.open, b'file-1', 'r')
232 assert_raises(libcephfs.ObjectNotFound, cephfs.open, b'file-1', 'r+')
233 fd = cephfs.open(b'file-1', 'w', 0o755)
234 cephfs.write(fd, b"asdf", 0)
235 cephfs.close(fd)
236 fd = cephfs.open(b'file-1', 'r', 0o755)
237 assert_equal(cephfs.read(fd, 0, 4), b"asdf")
238 cephfs.close(fd)
239 fd = cephfs.open(b'file-1', 'r+', 0o755)
240 cephfs.write(fd, b"zxcv", 4)
241 assert_equal(cephfs.read(fd, 4, 8), b"zxcv")
242 cephfs.close(fd)
243 fd = cephfs.open(b'file-1', 'w+', 0o755)
244 assert_equal(cephfs.read(fd, 0, 4), b"")
245 cephfs.write(fd, b"zxcv", 4)
246 assert_equal(cephfs.read(fd, 4, 8), b"zxcv")
247 cephfs.close(fd)
248 fd = cephfs.open(b'file-1', os.O_RDWR, 0o755)
249 cephfs.write(fd, b"asdf", 0)
250 assert_equal(cephfs.read(fd, 0, 4), b"asdf")
251 cephfs.close(fd)
252 assert_raises(libcephfs.OperationNotSupported, cephfs.open, b'file-1', 'a')
253 cephfs.unlink(b'file-1')
254
255 @with_setup(setup_test)
256 def test_link():
257 fd = cephfs.open(b'file-1', 'w', 0o755)
258 cephfs.write(fd, b"1111", 0)
259 cephfs.close(fd)
260 cephfs.link(b'file-1', b'file-2')
261 fd = cephfs.open(b'file-2', 'r', 0o755)
262 assert_equal(cephfs.read(fd, 0, 4), b"1111")
263 cephfs.close(fd)
264 fd = cephfs.open(b'file-2', 'r+', 0o755)
265 cephfs.write(fd, b"2222", 4)
266 cephfs.close(fd)
267 fd = cephfs.open(b'file-1', 'r', 0o755)
268 assert_equal(cephfs.read(fd, 0, 8), b"11112222")
269 cephfs.close(fd)
270 cephfs.unlink(b'file-2')
271
272 @with_setup(setup_test)
273 def test_symlink():
274 fd = cephfs.open(b'file-1', 'w', 0o755)
275 cephfs.write(fd, b"1111", 0)
276 cephfs.close(fd)
277 cephfs.symlink(b'file-1', b'file-2')
278 fd = cephfs.open(b'file-2', 'r', 0o755)
279 assert_equal(cephfs.read(fd, 0, 4), b"1111")
280 cephfs.close(fd)
281 fd = cephfs.open(b'file-2', 'r+', 0o755)
282 cephfs.write(fd, b"2222", 4)
283 cephfs.close(fd)
284 fd = cephfs.open(b'file-1', 'r', 0o755)
285 assert_equal(cephfs.read(fd, 0, 8), b"11112222")
286 cephfs.close(fd)
287 cephfs.unlink(b'file-2')
288
289 @with_setup(setup_test)
290 def test_readlink():
291 fd = cephfs.open(b'/file-1', 'w', 0o755)
292 cephfs.write(fd, b"1111", 0)
293 cephfs.close(fd)
294 cephfs.symlink(b'/file-1', b'/file-2')
295 d = cephfs.readlink(b"/file-2",100)
296 assert_equal(d, b"/file-1")
297 cephfs.unlink(b'/file-2')
298 cephfs.unlink(b'/file-1')
299
300 @with_setup(setup_test)
301 def test_delete_cwd():
302 assert_equal(b"/", cephfs.getcwd())
303
304 cephfs.mkdir(b"/temp-directory", 0o755)
305 cephfs.chdir(b"/temp-directory")
306 cephfs.rmdir(b"/temp-directory")
307
308 # getcwd gives you something stale here: it remembers the path string
309 # even when things are unlinked. It's up to the caller to find out
310 # whether it really still exists
311 assert_equal(b"/temp-directory", cephfs.getcwd())
312
313 @with_setup(setup_test)
314 def test_flock():
315 fd = cephfs.open(b'file-1', 'w', 0o755)
316
317 cephfs.flock(fd, fcntl.LOCK_EX, 123);
318 fd2 = cephfs.open(b'file-1', 'w', 0o755)
319
320 assert_raises(libcephfs.WouldBlock, cephfs.flock, fd2,
321 fcntl.LOCK_EX | fcntl.LOCK_NB, 456);
322 cephfs.close(fd2)
323
324 cephfs.close(fd)
325
326 @with_setup(setup_test)
327 def test_mount_unmount():
328 test_directory()
329 cephfs.unmount()
330 cephfs.mount()
331 test_open()
332
333 @with_setup(setup_test)
334 def test_lxattr():
335 fd = cephfs.open(b'/file-lxattr', 'w', 0o755)
336 cephfs.close(fd)
337 cephfs.setxattr(b"/file-lxattr", "user.key", b"value", 0)
338 cephfs.symlink(b"/file-lxattr", b"/file-sym-lxattr")
339 assert_equal(b"value", cephfs.getxattr(b"/file-sym-lxattr", "user.key"))
340 assert_raises(libcephfs.NoData, cephfs.lgetxattr, b"/file-sym-lxattr", "user.key")
341
342 cephfs.lsetxattr(b"/file-sym-lxattr", "trusted.key-sym", b"value-sym", 0)
343 assert_equal(b"value-sym", cephfs.lgetxattr(b"/file-sym-lxattr", "trusted.key-sym"))
344 cephfs.lsetxattr(b"/file-sym-lxattr", "trusted.big", b"x" * 300, 0)
345
346 # Default size is 255, get ERANGE
347 assert_raises(libcephfs.OutOfRange, cephfs.lgetxattr, b"/file-sym-lxattr", "trusted.big")
348
349 # Pass explicit size, and we'll get the value
350 assert_equal(300, len(cephfs.lgetxattr(b"/file-sym-lxattr", "trusted.big", 300)))
351
352 cephfs.lremovexattr(b"/file-sym-lxattr", "trusted.key-sym")
353 # trusted.key-sym is already removed
354 assert_raises(libcephfs.NoData, cephfs.lgetxattr, b"/file-sym-lxattr", "trusted.key-sym")
355
356 # trusted.big is only listed
357 ret_val, ret_buff = cephfs.llistxattr(b"/file-sym-lxattr")
358 assert_equal(12, ret_val)
359 assert_equal("trusted.big\x00", ret_buff.decode('utf-8'))
360 cephfs.unlink(b'/file-lxattr')
361 cephfs.unlink(b'/file-sym-lxattr')
362
363 @with_setup(setup_test)
364 def test_mount_root():
365 cephfs.mkdir(b"/mount-directory", 0o755)
366 cephfs.unmount()
367 cephfs.mount(mount_root = b"/mount-directory")
368
369 assert_raises(libcephfs.Error, cephfs.mount, mount_root = b"/nowhere")
370 cephfs.unmount()
371 cephfs.mount()
372
373 @with_setup(setup_test)
374 def test_utime():
375 fd = cephfs.open(b'/file-1', 'w', 0o755)
376 cephfs.write(fd, b'0000', 0)
377 cephfs.close(fd)
378
379 stx_pre = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
380
381 time.sleep(1)
382 cephfs.utime(b'/file-1')
383
384 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
385
386 assert_greater(stx_post['atime'], stx_pre['atime'])
387 assert_greater(stx_post['mtime'], stx_pre['mtime'])
388
389 atime_pre = int(time.mktime(stx_pre['atime'].timetuple()))
390 mtime_pre = int(time.mktime(stx_pre['mtime'].timetuple()))
391
392 cephfs.utime(b'/file-1', (atime_pre, mtime_pre))
393 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
394
395 assert_equal(stx_post['atime'], stx_pre['atime'])
396 assert_equal(stx_post['mtime'], stx_pre['mtime'])
397
398 cephfs.unlink(b'/file-1')
399
400 @with_setup(setup_test)
401 def test_futime():
402 fd = cephfs.open(b'/file-1', 'w', 0o755)
403 cephfs.write(fd, b'0000', 0)
404
405 stx_pre = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
406
407 time.sleep(1)
408 cephfs.futime(fd)
409
410 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
411
412 assert_greater(stx_post['atime'], stx_pre['atime'])
413 assert_greater(stx_post['mtime'], stx_pre['mtime'])
414
415 atime_pre = int(time.mktime(stx_pre['atime'].timetuple()))
416 mtime_pre = int(time.mktime(stx_pre['mtime'].timetuple()))
417
418 cephfs.futime(fd, (atime_pre, mtime_pre))
419 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
420
421 assert_equal(stx_post['atime'], stx_pre['atime'])
422 assert_equal(stx_post['mtime'], stx_pre['mtime'])
423
424 cephfs.close(fd)
425 cephfs.unlink(b'/file-1')
426
427 @with_setup(setup_test)
428 def test_utimes():
429 fd = cephfs.open(b'/file-1', 'w', 0o755)
430 cephfs.write(fd, b'0000', 0)
431 cephfs.close(fd)
432
433 stx_pre = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
434
435 time.sleep(1)
436 cephfs.utimes(b'/file-1')
437
438 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
439
440 assert_greater(stx_post['atime'], stx_pre['atime'])
441 assert_greater(stx_post['mtime'], stx_pre['mtime'])
442
443 atime_pre = time.mktime(stx_pre['atime'].timetuple())
444 mtime_pre = time.mktime(stx_pre['mtime'].timetuple())
445
446 cephfs.utimes(b'/file-1', (atime_pre, mtime_pre))
447 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
448
449 assert_equal(stx_post['atime'], stx_pre['atime'])
450 assert_equal(stx_post['mtime'], stx_pre['mtime'])
451
452 cephfs.unlink(b'/file-1')
453
454 @with_setup(setup_test)
455 def test_lutimes():
456 fd = cephfs.open(b'/file-1', 'w', 0o755)
457 cephfs.write(fd, b'0000', 0)
458 cephfs.close(fd)
459
460 cephfs.symlink(b'/file-1', b'/file-2')
461
462 stx_pre_t = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
463 stx_pre_s = cephfs.statx(b'/file-2', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, libcephfs.AT_SYMLINK_NOFOLLOW)
464
465 time.sleep(1)
466 cephfs.lutimes(b'/file-2')
467
468 stx_post_t = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
469 stx_post_s = cephfs.statx(b'/file-2', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, libcephfs.AT_SYMLINK_NOFOLLOW)
470
471 assert_equal(stx_post_t['atime'], stx_pre_t['atime'])
472 assert_equal(stx_post_t['mtime'], stx_pre_t['mtime'])
473
474 assert_greater(stx_post_s['atime'], stx_pre_s['atime'])
475 assert_greater(stx_post_s['mtime'], stx_pre_s['mtime'])
476
477 atime_pre = time.mktime(stx_pre_s['atime'].timetuple())
478 mtime_pre = time.mktime(stx_pre_s['mtime'].timetuple())
479
480 cephfs.lutimes(b'/file-2', (atime_pre, mtime_pre))
481 stx_post_s = cephfs.statx(b'/file-2', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, libcephfs.AT_SYMLINK_NOFOLLOW)
482
483 assert_equal(stx_post_s['atime'], stx_pre_s['atime'])
484 assert_equal(stx_post_s['mtime'], stx_pre_s['mtime'])
485
486 cephfs.unlink(b'/file-2')
487 cephfs.unlink(b'/file-1')
488
489 @with_setup(setup_test)
490 def test_futimes():
491 fd = cephfs.open(b'/file-1', 'w', 0o755)
492 cephfs.write(fd, b'0000', 0)
493
494 stx_pre = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
495
496 time.sleep(1)
497 cephfs.futimes(fd)
498
499 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
500
501 assert_greater(stx_post['atime'], stx_pre['atime'])
502 assert_greater(stx_post['mtime'], stx_pre['mtime'])
503
504 atime_pre = time.mktime(stx_pre['atime'].timetuple())
505 mtime_pre = time.mktime(stx_pre['mtime'].timetuple())
506
507 cephfs.futimes(fd, (atime_pre, mtime_pre))
508 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
509
510 assert_equal(stx_post['atime'], stx_pre['atime'])
511 assert_equal(stx_post['mtime'], stx_pre['mtime'])
512
513 cephfs.close(fd)
514 cephfs.unlink(b'/file-1')
515
516 @with_setup(setup_test)
517 def test_futimens():
518 fd = cephfs.open(b'/file-1', 'w', 0o755)
519 cephfs.write(fd, b'0000', 0)
520
521 stx_pre = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
522
523 time.sleep(1)
524 cephfs.futimens(fd)
525
526 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
527
528 assert_greater(stx_post['atime'], stx_pre['atime'])
529 assert_greater(stx_post['mtime'], stx_pre['mtime'])
530
531 atime_pre = time.mktime(stx_pre['atime'].timetuple())
532 mtime_pre = time.mktime(stx_pre['mtime'].timetuple())
533
534 cephfs.futimens(fd, (atime_pre, mtime_pre))
535 stx_post = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_MTIME, 0)
536
537 assert_equal(stx_post['atime'], stx_pre['atime'])
538 assert_equal(stx_post['mtime'], stx_pre['mtime'])
539
540 cephfs.close(fd)
541 cephfs.unlink(b'/file-1')
542
543 @with_setup(setup_test)
544 def test_lchmod():
545 fd = cephfs.open(b'/file-1', 'w', 0o755)
546 cephfs.write(fd, b'0000', 0)
547 cephfs.close(fd)
548
549 cephfs.symlink(b'/file-1', b'/file-2')
550
551 stx_pre_t = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_MODE, 0)
552 stx_pre_s = cephfs.statx(b'/file-2', libcephfs.CEPH_STATX_MODE, libcephfs.AT_SYMLINK_NOFOLLOW)
553
554 time.sleep(1)
555 cephfs.lchmod(b'/file-2', 0o400)
556
557 stx_post_t = cephfs.statx(b'/file-1', libcephfs.CEPH_STATX_MODE, 0)
558 stx_post_s = cephfs.statx(b'/file-2', libcephfs.CEPH_STATX_MODE, libcephfs.AT_SYMLINK_NOFOLLOW)
559
560 assert_equal(stx_post_t['mode'], stx_pre_t['mode'])
561 assert_not_equal(stx_post_s['mode'], stx_pre_s['mode'])
562 stx_post_s_perm_bits = stx_post_s['mode'] & ~stat.S_IFMT(stx_post_s["mode"])
563 assert_equal(stx_post_s_perm_bits, 0o400)
564
565 cephfs.unlink(b'/file-2')
566 cephfs.unlink(b'/file-1')
567
568 @with_setup(setup_test)
569 def test_fchmod():
570 fd = cephfs.open(b'/file-fchmod', 'w', 0o655)
571 st = cephfs.statx(b'/file-fchmod', libcephfs.CEPH_STATX_MODE, 0)
572 mode = st["mode"] | stat.S_IXUSR
573 cephfs.fchmod(fd, mode)
574 st = cephfs.statx(b'/file-fchmod', libcephfs.CEPH_STATX_MODE, 0)
575 assert_equal(st["mode"] & stat.S_IRWXU, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
576 assert_raises(TypeError, cephfs.fchmod, "/file-fchmod", stat.S_IXUSR)
577 assert_raises(TypeError, cephfs.fchmod, fd, "stat.S_IXUSR")
578 cephfs.close(fd)
579 cephfs.unlink(b'/file-fchmod')
580
581 @with_setup(setup_test)
582 def test_fchown():
583 fd = cephfs.open(b'/file-fchown', 'w', 0o655)
584 uid = os.getuid()
585 gid = os.getgid()
586 assert_raises(TypeError, cephfs.fchown, b'/file-fchown', uid, gid)
587 assert_raises(TypeError, cephfs.fchown, fd, "uid", "gid")
588 cephfs.fchown(fd, uid, gid)
589 st = cephfs.statx(b'/file-fchown', libcephfs.CEPH_STATX_UID | libcephfs.CEPH_STATX_GID, 0)
590 assert_equal(st["uid"], uid)
591 assert_equal(st["gid"], gid)
592 cephfs.fchown(fd, 9999, 9999)
593 st = cephfs.statx(b'/file-fchown', libcephfs.CEPH_STATX_UID | libcephfs.CEPH_STATX_GID, 0)
594 assert_equal(st["uid"], 9999)
595 assert_equal(st["gid"], 9999)
596 cephfs.close(fd)
597 cephfs.unlink(b'/file-fchown')
598
599 @with_setup(setup_test)
600 def test_truncate():
601 fd = cephfs.open(b'/file-truncate', 'w', 0o755)
602 cephfs.write(fd, b"1111", 0)
603 cephfs.truncate(b'/file-truncate', 0)
604 stat = cephfs.fsync(fd, 0)
605 st = cephfs.statx(b'/file-truncate', libcephfs.CEPH_STATX_SIZE, 0)
606 assert_equal(st["size"], 0)
607 cephfs.close(fd)
608 cephfs.unlink(b'/file-truncate')
609
610 @with_setup(setup_test)
611 def test_ftruncate():
612 fd = cephfs.open(b'/file-ftruncate', 'w', 0o755)
613 cephfs.write(fd, b"1111", 0)
614 assert_raises(TypeError, cephfs.ftruncate, b'/file-ftruncate', 0)
615 cephfs.ftruncate(fd, 0)
616 stat = cephfs.fsync(fd, 0)
617 st = cephfs.fstat(fd)
618 assert_equal(st.st_size, 0)
619 cephfs.close(fd)
620 cephfs.unlink(b'/file-ftruncate')
621
622 @with_setup(setup_test)
623 def test_fallocate():
624 fd = cephfs.open(b'/file-fallocate', 'w', 0o755)
625 assert_raises(TypeError, cephfs.fallocate, b'/file-fallocate', 0, 10)
626 cephfs.fallocate(fd, 0, 10)
627 stat = cephfs.fsync(fd, 0)
628 st = cephfs.fstat(fd)
629 assert_equal(st.st_size, 10)
630 cephfs.close(fd)
631 cephfs.unlink(b'/file-fallocate')
632
633 @with_setup(setup_test)
634 def test_mknod():
635 mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
636 cephfs.mknod(b'/file-fifo', mode)
637 st = cephfs.statx(b'/file-fifo', libcephfs.CEPH_STATX_MODE, 0)
638 assert_equal(st["mode"] & mode, mode)
639 cephfs.unlink(b'/file-fifo')
640
641 @with_setup(setup_test)
642 def test_lazyio():
643 fd = cephfs.open(b'/file-lazyio', 'w', 0o755)
644 assert_raises(TypeError, cephfs.lazyio, "fd", 1)
645 assert_raises(TypeError, cephfs.lazyio, fd, "1")
646 cephfs.lazyio(fd, 1)
647 cephfs.write(fd, b"1111", 0)
648 assert_raises(TypeError, cephfs.lazyio_propagate, "fd", 0, 4)
649 assert_raises(TypeError, cephfs.lazyio_propagate, fd, "0", 4)
650 assert_raises(TypeError, cephfs.lazyio_propagate, fd, 0, "4")
651 cephfs.lazyio_propagate(fd, 0, 4)
652 st = cephfs.fstat(fd)
653 assert_equal(st.st_size, 4)
654 cephfs.write(fd, b"2222", 4)
655 assert_raises(TypeError, cephfs.lazyio_synchronize, "fd", 0, 8)
656 assert_raises(TypeError, cephfs.lazyio_synchronize, fd, "0", 8)
657 assert_raises(TypeError, cephfs.lazyio_synchronize, fd, 0, "8")
658 cephfs.lazyio_synchronize(fd, 0, 8)
659 st = cephfs.fstat(fd)
660 assert_equal(st.st_size, 8)
661 cephfs.close(fd)
662 cephfs.unlink(b'/file-lazyio')
663
664 @with_setup(setup_test)
665 def test_replication():
666 fd = cephfs.open(b'/file-rep', 'w', 0o755)
667 assert_raises(TypeError, cephfs.get_file_replication, "fd")
668 l_dict = cephfs.get_layout(fd)
669 assert('pool_name' in l_dict.keys())
670 cnt = cephfs.get_file_replication(fd)
671 get_rep_cnt_cmd = "ceph osd pool get " + l_dict["pool_name"] + " size"
672 s=os.popen(get_rep_cnt_cmd).read().strip('\n')
673 size=int(s.split(" ")[-1])
674 assert_equal(cnt, size)
675 cnt = cephfs.get_path_replication(b'/file-rep')
676 assert_equal(cnt, size)
677 cephfs.close(fd)
678 cephfs.unlink(b'/file-rep')
679
680 @with_setup(setup_test)
681 def test_caps():
682 fd = cephfs.open(b'/file-caps', 'w', 0o755)
683 timeout = cephfs.get_cap_return_timeout()
684 assert_equal(timeout, 300)
685 fd_caps = cephfs.debug_get_fd_caps(fd)
686 file_caps = cephfs.debug_get_file_caps(b'/file-caps')
687 assert_equal(fd_caps, file_caps)
688 cephfs.close(fd)
689 cephfs.unlink(b'/file-caps')
690
691 @with_setup(setup_test)
692 def test_setuuid():
693 ses_id_uid = uuid.uuid1()
694 ses_id_str = str(ses_id_uid)
695 cephfs.set_uuid(ses_id_str)
696
697 @with_setup(setup_test)
698 def test_session_timeout():
699 assert_raises(TypeError, cephfs.set_session_timeout, "300")
700 cephfs.set_session_timeout(300)
701
702 @with_setup(setup_test)
703 def test_readdirops():
704 cephfs.chdir(b"/")
705 dirs = [b"dir-1", b"dir-2", b"dir-3"]
706 for i in dirs:
707 cephfs.mkdir(i, 0o755)
708 handler = cephfs.opendir(b"/")
709 d1 = cephfs.readdir(handler)
710 d2 = cephfs.readdir(handler)
711 d3 = cephfs.readdir(handler)
712 offset_d4 = cephfs.telldir(handler)
713 d4 = cephfs.readdir(handler)
714 cephfs.rewinddir(handler)
715 d = cephfs.readdir(handler)
716 assert_equal(d.d_name, d1.d_name)
717 cephfs.seekdir(handler, offset_d4)
718 d = cephfs.readdir(handler)
719 assert_equal(d.d_name, d4.d_name)
720 dirs += [b".", b".."]
721 cephfs.rewinddir(handler)
722 d = cephfs.readdir(handler)
723 while d:
724 assert(d.d_name in dirs)
725 dirs.remove(d.d_name)
726 d = cephfs.readdir(handler)
727 assert(len(dirs) == 0)
728 dirs = [b"/dir-1", b"/dir-2", b"/dir-3"]
729 for i in dirs:
730 cephfs.rmdir(i)
731 cephfs.closedir(handler)
732
733 def test_preadv_pwritev():
734 fd = cephfs.open(b'file-1', 'w', 0o755)
735 cephfs.pwritev(fd, [b"asdf", b"zxcvb"], 0)
736 cephfs.close(fd)
737 fd = cephfs.open(b'file-1', 'r', 0o755)
738 buf = [bytearray(i) for i in [4, 5]]
739 cephfs.preadv(fd, buf, 0)
740 assert_equal([b"asdf", b"zxcvb"], list(buf))
741 cephfs.close(fd)
742 cephfs.unlink(b'file-1')
743
744 @with_setup(setup_test)
745 def test_setattrx():
746 fd = cephfs.open(b'file-setattrx', 'w', 0o655)
747 cephfs.write(fd, b"1111", 0)
748 cephfs.close(fd)
749 st = cephfs.statx(b'file-setattrx', libcephfs.CEPH_STATX_MODE, 0)
750 mode = st["mode"] | stat.S_IXUSR
751 assert_raises(TypeError, cephfs.setattrx, b'file-setattrx', "dict", 0, 0)
752
753 time.sleep(1)
754 statx_dict = dict()
755 statx_dict["mode"] = mode
756 statx_dict["uid"] = 9999
757 statx_dict["gid"] = 9999
758 dt = datetime.now()
759 statx_dict["mtime"] = dt
760 statx_dict["atime"] = dt
761 statx_dict["ctime"] = dt
762 statx_dict["size"] = 10
763 statx_dict["btime"] = dt
764 cephfs.setattrx(b'file-setattrx', statx_dict, libcephfs.CEPH_SETATTR_MODE | libcephfs.CEPH_SETATTR_UID |
765 libcephfs.CEPH_SETATTR_GID | libcephfs.CEPH_SETATTR_MTIME |
766 libcephfs.CEPH_SETATTR_ATIME | libcephfs.CEPH_SETATTR_CTIME |
767 libcephfs.CEPH_SETATTR_SIZE | libcephfs.CEPH_SETATTR_BTIME, 0)
768 st1 = cephfs.statx(b'file-setattrx', libcephfs.CEPH_STATX_MODE | libcephfs.CEPH_STATX_UID |
769 libcephfs.CEPH_STATX_GID | libcephfs.CEPH_STATX_MTIME |
770 libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_CTIME |
771 libcephfs.CEPH_STATX_SIZE | libcephfs.CEPH_STATX_BTIME, 0)
772 assert_equal(mode, st1["mode"])
773 assert_equal(9999, st1["uid"])
774 assert_equal(9999, st1["gid"])
775 assert_equal(int(dt.timestamp()), int(st1["mtime"].timestamp()))
776 assert_equal(int(dt.timestamp()), int(st1["atime"].timestamp()))
777 assert_equal(int(dt.timestamp()), int(st1["ctime"].timestamp()))
778 assert_equal(int(dt.timestamp()), int(st1["btime"].timestamp()))
779 assert_equal(10, st1["size"])
780 cephfs.unlink(b'file-setattrx')
781
782 @with_setup(setup_test)
783 def test_fsetattrx():
784 fd = cephfs.open(b'file-fsetattrx', 'w', 0o655)
785 cephfs.write(fd, b"1111", 0)
786 st = cephfs.statx(b'file-fsetattrx', libcephfs.CEPH_STATX_MODE, 0)
787 mode = st["mode"] | stat.S_IXUSR
788 assert_raises(TypeError, cephfs.fsetattrx, fd, "dict", 0, 0)
789
790 time.sleep(1)
791 statx_dict = dict()
792 statx_dict["mode"] = mode
793 statx_dict["uid"] = 9999
794 statx_dict["gid"] = 9999
795 dt = datetime.now()
796 statx_dict["mtime"] = dt
797 statx_dict["atime"] = dt
798 statx_dict["ctime"] = dt
799 statx_dict["size"] = 10
800 statx_dict["btime"] = dt
801 cephfs.fsetattrx(fd, statx_dict, libcephfs.CEPH_SETATTR_MODE | libcephfs.CEPH_SETATTR_UID |
802 libcephfs.CEPH_SETATTR_GID | libcephfs.CEPH_SETATTR_MTIME |
803 libcephfs.CEPH_SETATTR_ATIME | libcephfs.CEPH_SETATTR_CTIME |
804 libcephfs.CEPH_SETATTR_SIZE | libcephfs.CEPH_SETATTR_BTIME)
805 st1 = cephfs.statx(b'file-fsetattrx', libcephfs.CEPH_STATX_MODE | libcephfs.CEPH_STATX_UID |
806 libcephfs.CEPH_STATX_GID | libcephfs.CEPH_STATX_MTIME |
807 libcephfs.CEPH_STATX_ATIME | libcephfs.CEPH_STATX_CTIME |
808 libcephfs.CEPH_STATX_SIZE | libcephfs.CEPH_STATX_BTIME, 0)
809 assert_equal(mode, st1["mode"])
810 assert_equal(9999, st1["uid"])
811 assert_equal(9999, st1["gid"])
812 assert_equal(int(dt.timestamp()), int(st1["mtime"].timestamp()))
813 assert_equal(int(dt.timestamp()), int(st1["atime"].timestamp()))
814 assert_equal(int(dt.timestamp()), int(st1["ctime"].timestamp()))
815 assert_equal(int(dt.timestamp()), int(st1["btime"].timestamp()))
816 assert_equal(10, st1["size"])
817 cephfs.close(fd)
818 cephfs.unlink(b'file-fsetattrx')
819
820 @with_setup(setup_test)
821 def test_get_layout():
822 fd = cephfs.open(b'file-get-layout', 'w', 0o755)
823 cephfs.write(fd, b"1111", 0)
824 assert_raises(TypeError, cephfs.get_layout, "fd")
825 l_dict = cephfs.get_layout(fd)
826 assert('stripe_unit' in l_dict.keys())
827 assert('stripe_count' in l_dict.keys())
828 assert('object_size' in l_dict.keys())
829 assert('pool_id' in l_dict.keys())
830 assert('pool_name' in l_dict.keys())
831
832 cephfs.close(fd)
833 cephfs.unlink(b'file-get-layout')
834
835 @with_setup(setup_test)
836 def test_get_default_pool():
837 dp_dict = cephfs.get_default_pool()
838 assert('pool_id' in dp_dict.keys())
839 assert('pool_name' in dp_dict.keys())
840
841 @with_setup(setup_test)
842 def test_get_pool():
843 dp_dict = cephfs.get_default_pool()
844 assert('pool_id' in dp_dict.keys())
845 assert('pool_name' in dp_dict.keys())
846 assert_equal(cephfs.get_pool_id(dp_dict["pool_name"]), dp_dict["pool_id"])
847 get_rep_cnt_cmd = "ceph osd pool get " + dp_dict["pool_name"] + " size"
848 s=os.popen(get_rep_cnt_cmd).read().strip('\n')
849 size=int(s.split(" ")[-1])
850 assert_equal(cephfs.get_pool_replication(dp_dict["pool_id"]), size)
851
852 @with_setup(setup_test)
853 def test_disk_quota_exceeeded_error():
854 cephfs.mkdir("/dir-1", 0o755)
855 cephfs.setxattr("/dir-1", "ceph.quota.max_bytes", b"5", 0)
856 fd = cephfs.open(b'/dir-1/file-1', 'w', 0o755)
857 assert_raises(libcephfs.DiskQuotaExceeded, cephfs.write, fd, b"abcdeghiklmnopqrstuvwxyz", 0)
858 cephfs.close(fd)
859 cephfs.unlink(b"/dir-1/file-1")
860
861 @with_setup(setup_test)
862 def test_empty_snapshot_info():
863 cephfs.mkdir("/dir-1", 0o755)
864
865 # snap without metadata
866 cephfs.mkdir("/dir-1/.snap/snap0", 0o755)
867 snap_info = cephfs.snap_info("/dir-1/.snap/snap0")
868 assert_equal(snap_info["metadata"], {})
869 assert_greater(snap_info["id"], 0)
870 cephfs.rmdir("/dir-1/.snap/snap0")
871
872 # remove directory
873 cephfs.rmdir("/dir-1")
874
875 @with_setup(setup_test)
876 def test_snapshot_info():
877 cephfs.mkdir("/dir-1", 0o755)
878
879 # snap with custom metadata
880 md = {"foo": "bar", "zig": "zag", "abcdefg": "12345"}
881 cephfs.mksnap("/dir-1", "snap0", 0o755, metadata=md)
882 snap_info = cephfs.snap_info("/dir-1/.snap/snap0")
883 assert_equal(snap_info["metadata"]["foo"], md["foo"])
884 assert_equal(snap_info["metadata"]["zig"], md["zig"])
885 assert_equal(snap_info["metadata"]["abcdefg"], md["abcdefg"])
886 assert_greater(snap_info["id"], 0)
887 cephfs.rmsnap("/dir-1", "snap0")
888
889 # remove directory
890 cephfs.rmdir("/dir-1")