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
10 from datetime
import datetime
16 cephfs
= libcephfs
.LibCephFS(conffile
='')
19 def teardown_module():
24 d
= cephfs
.opendir(b
"/")
25 dent
= cephfs
.readdir(d
)
27 if (dent
.d_name
not in [b
".", b
".."]):
29 cephfs
.rmdir(b
"/" + dent
.d_name
)
31 cephfs
.unlink(b
"/" + dent
.d_name
)
33 dent
= cephfs
.readdir(d
)
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
)
44 @with_setup(setup_test
)
46 fsid
= cephfs
.conf_get("fsid")
49 @with_setup(setup_test
)
53 @with_setup(setup_test
)
55 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
56 stat
= cephfs
.fstat(fd
)
57 assert(len(stat
) == 13)
60 @with_setup(setup_test
)
62 stat
= cephfs
.statfs(b
'/')
63 assert(len(stat
) == 11)
65 @with_setup(setup_test
)
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())
72 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
73 cephfs
.write(fd
, b
"1111", 0)
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')
82 @with_setup(setup_test
)
84 stat
= cephfs
.sync_fs()
86 @with_setup(setup_test
)
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)
94 #sync on non-existing fd (assume fd 12345 is not exists)
95 assert_raises(libcephfs
.Error
, cephfs
.fsync
, 12345, 0)
97 @with_setup(setup_test
)
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")
108 @with_setup(setup_test
)
111 dirs
= [b
"dir-1", b
"dir-2", b
"dir-3"]
113 cephfs
.mkdir(i
, 0o755)
114 handler
= cephfs
.opendir(b
"/")
115 d
= cephfs
.readdir(handler
)
116 dirs
+= [b
".", b
".."]
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"]
125 cephfs
.closedir(handler
)
127 @with_setup(setup_test
)
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"))
133 cephfs
.setxattr("/", "user.big", b
"x" * 300, 0)
135 # Default size is 255, get ERANGE
136 assert_raises(libcephfs
.OutOfRange
, cephfs
.getxattr
, "/", "user.big")
138 # Pass explicit size, and we'll get the value
139 assert_equal(300, len(cephfs
.getxattr("/", "user.big", 300)))
141 cephfs
.removexattr("/", "user.key")
142 # user.key is already removed
143 assert_raises(libcephfs
.NoData
, cephfs
.getxattr
, "/", "user.key")
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'))
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')
158 mirror_xattr_enc_1
= gen_mirror_xattr()
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
)
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'))
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
)
173 # ceph.mirror.info should not show up in listing
174 ret_val
, _
= cephfs
.listxattr("/")
175 assert_equal(0, ret_val
)
177 mirror_xattr_enc_2
= gen_mirror_xattr()
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'))
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")
188 # check mirror info xattr format
189 assert_raises(libcephfs
.InvalidValue
, cephfs
.setxattr
, '/', 'ceph.mirror.info', b
"unknown", 0)
191 @with_setup(setup_test
)
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"))
201 cephfs
.fsetxattr(fd
, "user.big", b
"x" * 300, 0)
203 # Default size is 255, get ERANGE
204 assert_raises(libcephfs
.OutOfRange
, cephfs
.fgetxattr
, fd
, "user.big")
206 # Pass explicit size, and we'll get the value
207 assert_equal(300, len(cephfs
.fgetxattr(fd
, "user.big", 300)))
209 cephfs
.fremovexattr(fd
, "user.key")
210 # user.key is already removed
211 assert_raises(libcephfs
.NoData
, cephfs
.fgetxattr
, fd
, "user.key")
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'))
218 cephfs
.unlink(b
'/file-fxattr')
220 @with_setup(setup_test
)
222 cephfs
.mkdir(b
"/a", 0o755)
223 cephfs
.mkdir(b
"/a/b", 0o755)
224 cephfs
.rename(b
"/a", b
"/b")
226 cephfs
.rmdir(b
"/b/b")
229 @with_setup(setup_test
)
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)
236 fd
= cephfs
.open(b
'file-1', 'r', 0o755)
237 assert_equal(cephfs
.read(fd
, 0, 4), b
"asdf")
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")
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")
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")
252 assert_raises(libcephfs
.OperationNotSupported
, cephfs
.open, b
'file-1', 'a')
253 cephfs
.unlink(b
'file-1')
255 @with_setup(setup_test
)
257 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
258 cephfs
.write(fd
, b
"1111", 0)
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")
264 fd
= cephfs
.open(b
'file-2', 'r+', 0o755)
265 cephfs
.write(fd
, b
"2222", 4)
267 fd
= cephfs
.open(b
'file-1', 'r', 0o755)
268 assert_equal(cephfs
.read(fd
, 0, 8), b
"11112222")
270 cephfs
.unlink(b
'file-2')
272 @with_setup(setup_test
)
274 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
275 cephfs
.write(fd
, b
"1111", 0)
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")
281 fd
= cephfs
.open(b
'file-2', 'r+', 0o755)
282 cephfs
.write(fd
, b
"2222", 4)
284 fd
= cephfs
.open(b
'file-1', 'r', 0o755)
285 assert_equal(cephfs
.read(fd
, 0, 8), b
"11112222")
287 cephfs
.unlink(b
'file-2')
289 @with_setup(setup_test
)
291 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
292 cephfs
.write(fd
, b
"1111", 0)
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')
300 @with_setup(setup_test
)
301 def test_delete_cwd():
302 assert_equal(b
"/", cephfs
.getcwd())
304 cephfs
.mkdir(b
"/temp-directory", 0o755)
305 cephfs
.chdir(b
"/temp-directory")
306 cephfs
.rmdir(b
"/temp-directory")
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())
313 @with_setup(setup_test
)
315 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
317 cephfs
.flock(fd
, fcntl
.LOCK_EX
, 123);
318 fd2
= cephfs
.open(b
'file-1', 'w', 0o755)
320 assert_raises(libcephfs
.WouldBlock
, cephfs
.flock
, fd2
,
321 fcntl
.LOCK_EX | fcntl
.LOCK_NB
, 456);
326 @with_setup(setup_test
)
327 def test_mount_unmount():
333 @with_setup(setup_test
)
335 fd
= cephfs
.open(b
'/file-lxattr', 'w', 0o755)
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")
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)
346 # Default size is 255, get ERANGE
347 assert_raises(libcephfs
.OutOfRange
, cephfs
.lgetxattr
, b
"/file-sym-lxattr", "trusted.big")
349 # Pass explicit size, and we'll get the value
350 assert_equal(300, len(cephfs
.lgetxattr(b
"/file-sym-lxattr", "trusted.big", 300)))
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")
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')
363 @with_setup(setup_test
)
364 def test_mount_root():
365 cephfs
.mkdir(b
"/mount-directory", 0o755)
367 cephfs
.mount(mount_root
= b
"/mount-directory")
369 assert_raises(libcephfs
.Error
, cephfs
.mount
, mount_root
= b
"/nowhere")
373 @with_setup(setup_test
)
375 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
376 cephfs
.write(fd
, b
'0000', 0)
379 stx_pre
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
382 cephfs
.utime(b
'/file-1')
384 stx_post
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
386 assert_greater(stx_post
['atime'], stx_pre
['atime'])
387 assert_greater(stx_post
['mtime'], stx_pre
['mtime'])
389 atime_pre
= int(time
.mktime(stx_pre
['atime'].timetuple()))
390 mtime_pre
= int(time
.mktime(stx_pre
['mtime'].timetuple()))
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)
395 assert_equal(stx_post
['atime'], stx_pre
['atime'])
396 assert_equal(stx_post
['mtime'], stx_pre
['mtime'])
398 cephfs
.unlink(b
'/file-1')
400 @with_setup(setup_test
)
402 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
403 cephfs
.write(fd
, b
'0000', 0)
405 stx_pre
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
410 stx_post
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
412 assert_greater(stx_post
['atime'], stx_pre
['atime'])
413 assert_greater(stx_post
['mtime'], stx_pre
['mtime'])
415 atime_pre
= int(time
.mktime(stx_pre
['atime'].timetuple()))
416 mtime_pre
= int(time
.mktime(stx_pre
['mtime'].timetuple()))
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)
421 assert_equal(stx_post
['atime'], stx_pre
['atime'])
422 assert_equal(stx_post
['mtime'], stx_pre
['mtime'])
425 cephfs
.unlink(b
'/file-1')
427 @with_setup(setup_test
)
429 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
430 cephfs
.write(fd
, b
'0000', 0)
433 stx_pre
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
436 cephfs
.utimes(b
'/file-1')
438 stx_post
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
440 assert_greater(stx_post
['atime'], stx_pre
['atime'])
441 assert_greater(stx_post
['mtime'], stx_pre
['mtime'])
443 atime_pre
= time
.mktime(stx_pre
['atime'].timetuple())
444 mtime_pre
= time
.mktime(stx_pre
['mtime'].timetuple())
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)
449 assert_equal(stx_post
['atime'], stx_pre
['atime'])
450 assert_equal(stx_post
['mtime'], stx_pre
['mtime'])
452 cephfs
.unlink(b
'/file-1')
454 @with_setup(setup_test
)
456 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
457 cephfs
.write(fd
, b
'0000', 0)
460 cephfs
.symlink(b
'/file-1', b
'/file-2')
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
)
466 cephfs
.lutimes(b
'/file-2')
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
)
471 assert_equal(stx_post_t
['atime'], stx_pre_t
['atime'])
472 assert_equal(stx_post_t
['mtime'], stx_pre_t
['mtime'])
474 assert_greater(stx_post_s
['atime'], stx_pre_s
['atime'])
475 assert_greater(stx_post_s
['mtime'], stx_pre_s
['mtime'])
477 atime_pre
= time
.mktime(stx_pre_s
['atime'].timetuple())
478 mtime_pre
= time
.mktime(stx_pre_s
['mtime'].timetuple())
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
)
483 assert_equal(stx_post_s
['atime'], stx_pre_s
['atime'])
484 assert_equal(stx_post_s
['mtime'], stx_pre_s
['mtime'])
486 cephfs
.unlink(b
'/file-2')
487 cephfs
.unlink(b
'/file-1')
489 @with_setup(setup_test
)
491 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
492 cephfs
.write(fd
, b
'0000', 0)
494 stx_pre
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
499 stx_post
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
501 assert_greater(stx_post
['atime'], stx_pre
['atime'])
502 assert_greater(stx_post
['mtime'], stx_pre
['mtime'])
504 atime_pre
= time
.mktime(stx_pre
['atime'].timetuple())
505 mtime_pre
= time
.mktime(stx_pre
['mtime'].timetuple())
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)
510 assert_equal(stx_post
['atime'], stx_pre
['atime'])
511 assert_equal(stx_post
['mtime'], stx_pre
['mtime'])
514 cephfs
.unlink(b
'/file-1')
516 @with_setup(setup_test
)
518 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
519 cephfs
.write(fd
, b
'0000', 0)
521 stx_pre
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
526 stx_post
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
528 assert_greater(stx_post
['atime'], stx_pre
['atime'])
529 assert_greater(stx_post
['mtime'], stx_pre
['mtime'])
531 atime_pre
= time
.mktime(stx_pre
['atime'].timetuple())
532 mtime_pre
= time
.mktime(stx_pre
['mtime'].timetuple())
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)
537 assert_equal(stx_post
['atime'], stx_pre
['atime'])
538 assert_equal(stx_post
['mtime'], stx_pre
['mtime'])
541 cephfs
.unlink(b
'/file-1')
543 @with_setup(setup_test
)
545 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
546 cephfs
.write(fd
, b
'0000', 0)
549 cephfs
.symlink(b
'/file-1', b
'/file-2')
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
)
555 cephfs
.lchmod(b
'/file-2', 0o400)
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
)
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)
565 cephfs
.unlink(b
'/file-2')
566 cephfs
.unlink(b
'/file-1')
568 @with_setup(setup_test
)
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")
579 cephfs
.unlink(b
'/file-fchmod')
581 @with_setup(setup_test
)
583 fd
= cephfs
.open(b
'/file-fchown', 'w', 0o655)
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)
597 cephfs
.unlink(b
'/file-fchown')
599 @with_setup(setup_test
)
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)
608 cephfs
.unlink(b
'/file-truncate')
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)
620 cephfs
.unlink(b
'/file-ftruncate')
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)
631 cephfs
.unlink(b
'/file-fallocate')
633 @with_setup(setup_test
)
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')
641 @with_setup(setup_test
)
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")
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)
662 cephfs
.unlink(b
'/file-lazyio')
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
)
678 cephfs
.unlink(b
'/file-rep')
680 @with_setup(setup_test
)
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
)
689 cephfs
.unlink(b
'/file-caps')
691 @with_setup(setup_test
)
693 ses_id_uid
= uuid
.uuid1()
694 ses_id_str
= str(ses_id_uid
)
695 cephfs
.set_uuid(ses_id_str
)
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)
702 @with_setup(setup_test
)
703 def test_readdirops():
705 dirs
= [b
"dir-1", b
"dir-2", b
"dir-3"]
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
)
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"]
731 cephfs
.closedir(handler
)
733 def test_preadv_pwritev():
734 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
735 cephfs
.pwritev(fd
, [b
"asdf", b
"zxcvb"], 0)
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
))
742 cephfs
.unlink(b
'file-1')
744 @with_setup(setup_test
)
746 fd
= cephfs
.open(b
'file-setattrx', 'w', 0o655)
747 cephfs
.write(fd
, b
"1111", 0)
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)
755 statx_dict
["mode"] = mode
756 statx_dict
["uid"] = 9999
757 statx_dict
["gid"] = 9999
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')
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)
792 statx_dict
["mode"] = mode
793 statx_dict
["uid"] = 9999
794 statx_dict
["gid"] = 9999
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"])
818 cephfs
.unlink(b
'file-fsetattrx')
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())
833 cephfs
.unlink(b
'file-get-layout')
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())
841 @with_setup(setup_test
)
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
)
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)
859 cephfs
.unlink(b
"/dir-1/file-1")
861 @with_setup(setup_test
)
862 def test_empty_snapshot_info():
863 cephfs
.mkdir("/dir-1", 0o755)
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")
873 cephfs
.rmdir("/dir-1")
875 @with_setup(setup_test
)
876 def test_snapshot_info():
877 cephfs
.mkdir("/dir-1", 0o755)
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")
890 cephfs
.rmdir("/dir-1")