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
7 from datetime
import datetime
13 cephfs
= libcephfs
.LibCephFS(conffile
='')
16 def teardown_module():
21 d
= cephfs
.opendir(b
"/")
22 dent
= cephfs
.readdir(d
)
24 if (dent
.d_name
not in [b
".", b
".."]):
26 cephfs
.rmdir(b
"/" + dent
.d_name
)
28 cephfs
.unlink(b
"/" + dent
.d_name
)
30 dent
= cephfs
.readdir(d
)
36 @with_setup(setup_test
)
38 fsid
= cephfs
.conf_get("fsid")
41 @with_setup(setup_test
)
45 @with_setup(setup_test
)
47 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
48 stat
= cephfs
.fstat(fd
)
49 assert(len(stat
) == 13)
52 @with_setup(setup_test
)
54 stat
= cephfs
.statfs(b
'/')
55 assert(len(stat
) == 11)
57 @with_setup(setup_test
)
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())
64 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
65 cephfs
.write(fd
, b
"1111", 0)
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')
74 @with_setup(setup_test
)
76 stat
= cephfs
.sync_fs()
78 @with_setup(setup_test
)
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)
86 #sync on non-existing fd (assume fd 12345 is not exists)
87 assert_raises(libcephfs
.Error
, cephfs
.fsync
, 12345, 0)
89 @with_setup(setup_test
)
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")
100 @with_setup(setup_test
)
103 dirs
= [b
"dir-1", b
"dir-2", b
"dir-3"]
105 cephfs
.mkdir(i
, 0o755)
106 handler
= cephfs
.opendir(b
"/")
107 d
= cephfs
.readdir(handler
)
108 dirs
+= [b
".", b
".."]
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"]
117 cephfs
.closedir(handler
)
119 @with_setup(setup_test
)
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"))
125 cephfs
.setxattr("/", "user.big", b
"x" * 300, 0)
127 # Default size is 255, get ERANGE
128 assert_raises(libcephfs
.OutOfRange
, cephfs
.getxattr
, "/", "user.big")
130 # Pass explicit size, and we'll get the value
131 assert_equal(300, len(cephfs
.getxattr("/", "user.big", 300)))
133 cephfs
.removexattr("/", "user.key")
134 # user.key is already removed
135 assert_raises(libcephfs
.NoData
, cephfs
.getxattr
, "/", "user.key")
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'))
142 @with_setup(setup_test
)
144 cephfs
.mkdir(b
"/a", 0o755)
145 cephfs
.mkdir(b
"/a/b", 0o755)
146 cephfs
.rename(b
"/a", b
"/b")
148 cephfs
.rmdir(b
"/b/b")
151 @with_setup(setup_test
)
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)
158 fd
= cephfs
.open(b
'file-1', 'r', 0o755)
159 assert_equal(cephfs
.read(fd
, 0, 4), b
"asdf")
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")
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")
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")
174 assert_raises(libcephfs
.OperationNotSupported
, cephfs
.open, b
'file-1', 'a')
175 cephfs
.unlink(b
'file-1')
177 @with_setup(setup_test
)
179 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
180 cephfs
.write(fd
, b
"1111", 0)
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")
186 fd
= cephfs
.open(b
'file-2', 'r+', 0o755)
187 cephfs
.write(fd
, b
"2222", 4)
189 fd
= cephfs
.open(b
'file-1', 'r', 0o755)
190 assert_equal(cephfs
.read(fd
, 0, 8), b
"11112222")
192 cephfs
.unlink(b
'file-2')
194 @with_setup(setup_test
)
196 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
197 cephfs
.write(fd
, b
"1111", 0)
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")
203 fd
= cephfs
.open(b
'file-2', 'r+', 0o755)
204 cephfs
.write(fd
, b
"2222", 4)
206 fd
= cephfs
.open(b
'file-1', 'r', 0o755)
207 assert_equal(cephfs
.read(fd
, 0, 8), b
"11112222")
209 cephfs
.unlink(b
'file-2')
211 @with_setup(setup_test
)
213 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
214 cephfs
.write(fd
, b
"1111", 0)
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')
222 @with_setup(setup_test
)
223 def test_delete_cwd():
224 assert_equal(b
"/", cephfs
.getcwd())
226 cephfs
.mkdir(b
"/temp-directory", 0o755)
227 cephfs
.chdir(b
"/temp-directory")
228 cephfs
.rmdir(b
"/temp-directory")
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())
235 @with_setup(setup_test
)
237 fd
= cephfs
.open(b
'file-1', 'w', 0o755)
239 cephfs
.flock(fd
, fcntl
.LOCK_EX
, 123);
240 fd2
= cephfs
.open(b
'file-1', 'w', 0o755)
242 assert_raises(libcephfs
.WouldBlock
, cephfs
.flock
, fd2
,
243 fcntl
.LOCK_EX | fcntl
.LOCK_NB
, 456);
248 @with_setup(setup_test
)
249 def test_mount_unmount():
255 @with_setup(setup_test
)
256 def test_mount_root():
257 cephfs
.mkdir(b
"/mount-directory", 0o755)
259 cephfs
.mount(mount_root
= b
"/mount-directory")
261 assert_raises(libcephfs
.Error
, cephfs
.mount
, mount_root
= b
"/nowhere")
265 @with_setup(setup_test
)
267 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
268 cephfs
.write(fd
, b
'0000', 0)
271 stx_pre
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
274 cephfs
.utime(b
'/file-1')
276 stx_post
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
278 assert_greater(stx_post
['atime'], stx_pre
['atime'])
279 assert_greater(stx_post
['mtime'], stx_pre
['mtime'])
281 atime_pre
= int(time
.mktime(stx_pre
['atime'].timetuple()))
282 mtime_pre
= int(time
.mktime(stx_pre
['mtime'].timetuple()))
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)
287 assert_equal(stx_post
['atime'], stx_pre
['atime'])
288 assert_equal(stx_post
['mtime'], stx_pre
['mtime'])
290 cephfs
.unlink(b
'/file-1')
292 @with_setup(setup_test
)
294 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
295 cephfs
.write(fd
, b
'0000', 0)
297 stx_pre
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
302 stx_post
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
304 assert_greater(stx_post
['atime'], stx_pre
['atime'])
305 assert_greater(stx_post
['mtime'], stx_pre
['mtime'])
307 atime_pre
= int(time
.mktime(stx_pre
['atime'].timetuple()))
308 mtime_pre
= int(time
.mktime(stx_pre
['mtime'].timetuple()))
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)
313 assert_equal(stx_post
['atime'], stx_pre
['atime'])
314 assert_equal(stx_post
['mtime'], stx_pre
['mtime'])
317 cephfs
.unlink(b
'/file-1')
319 @with_setup(setup_test
)
321 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
322 cephfs
.write(fd
, b
'0000', 0)
325 stx_pre
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
328 cephfs
.utimes(b
'/file-1')
330 stx_post
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
332 assert_greater(stx_post
['atime'], stx_pre
['atime'])
333 assert_greater(stx_post
['mtime'], stx_pre
['mtime'])
335 atime_pre
= time
.mktime(stx_pre
['atime'].timetuple())
336 mtime_pre
= time
.mktime(stx_pre
['mtime'].timetuple())
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)
341 assert_equal(stx_post
['atime'], stx_pre
['atime'])
342 assert_equal(stx_post
['mtime'], stx_pre
['mtime'])
344 cephfs
.unlink(b
'/file-1')
346 @with_setup(setup_test
)
348 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
349 cephfs
.write(fd
, b
'0000', 0)
352 cephfs
.symlink(b
'/file-1', b
'/file-2')
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
)
358 cephfs
.lutimes(b
'/file-2')
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
)
363 assert_equal(stx_post_t
['atime'], stx_pre_t
['atime'])
364 assert_equal(stx_post_t
['mtime'], stx_pre_t
['mtime'])
366 assert_greater(stx_post_s
['atime'], stx_pre_s
['atime'])
367 assert_greater(stx_post_s
['mtime'], stx_pre_s
['mtime'])
369 atime_pre
= time
.mktime(stx_pre_s
['atime'].timetuple())
370 mtime_pre
= time
.mktime(stx_pre_s
['mtime'].timetuple())
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
)
375 assert_equal(stx_post_s
['atime'], stx_pre_s
['atime'])
376 assert_equal(stx_post_s
['mtime'], stx_pre_s
['mtime'])
378 cephfs
.unlink(b
'/file-2')
379 cephfs
.unlink(b
'/file-1')
381 @with_setup(setup_test
)
383 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
384 cephfs
.write(fd
, b
'0000', 0)
386 stx_pre
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
391 stx_post
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
393 assert_greater(stx_post
['atime'], stx_pre
['atime'])
394 assert_greater(stx_post
['mtime'], stx_pre
['mtime'])
396 atime_pre
= time
.mktime(stx_pre
['atime'].timetuple())
397 mtime_pre
= time
.mktime(stx_pre
['mtime'].timetuple())
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)
402 assert_equal(stx_post
['atime'], stx_pre
['atime'])
403 assert_equal(stx_post
['mtime'], stx_pre
['mtime'])
406 cephfs
.unlink(b
'/file-1')
408 @with_setup(setup_test
)
410 fd
= cephfs
.open(b
'/file-1', 'w', 0o755)
411 cephfs
.write(fd
, b
'0000', 0)
413 stx_pre
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
418 stx_post
= cephfs
.statx(b
'/file-1', libcephfs
.CEPH_STATX_ATIME | libcephfs
.CEPH_STATX_MTIME
, 0)
420 assert_greater(stx_post
['atime'], stx_pre
['atime'])
421 assert_greater(stx_post
['mtime'], stx_pre
['mtime'])
423 atime_pre
= time
.mktime(stx_pre
['atime'].timetuple())
424 mtime_pre
= time
.mktime(stx_pre
['mtime'].timetuple())
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)
429 assert_equal(stx_post
['atime'], stx_pre
['atime'])
430 assert_equal(stx_post
['mtime'], stx_pre
['mtime'])
433 cephfs
.unlink(b
'/file-1')
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)
442 cephfs
.unlink(b
"/dir-1/file-1")