-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cephfs.py
256 lines (218 loc) · 7.45 KB
/
test_cephfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# vim: expandtab smarttab shiftwidth=4 softtabstop=4
from nose.tools import assert_raises, assert_equal, with_setup
import cephfs as libcephfs
import fcntl
import os
cephfs = None
def setup_module():
global cephfs
cephfs = libcephfs.LibCephFS(conffile='')
cephfs.mount()
def teardown_module():
global cephfs
cephfs.shutdown()
def setup_test():
d = cephfs.opendir(b"/")
dent = cephfs.readdir(d)
while dent:
if (dent.d_name not in [b".", b".."]):
if dent.is_dir():
cephfs.rmdir(b"/" + dent.d_name)
else:
cephfs.unlink(b"/" + dent.d_name)
dent = cephfs.readdir(d)
cephfs.closedir(d)
cephfs.chdir(b"/")
@with_setup(setup_test)
def test_conf_get():
fsid = cephfs.conf_get("fsid")
assert(len(fsid) > 0)
@with_setup(setup_test)
def test_version():
cephfs.version()
@with_setup(setup_test)
def test_fstat():
fd = cephfs.open(b'file-1', 'w', 0o755)
stat = cephfs.fstat(fd)
assert(len(stat) == 13)
cephfs.close(fd)
@with_setup(setup_test)
def test_statfs():
stat = cephfs.statfs(b'/')
assert(len(stat) == 11)
@with_setup(setup_test)
def test_statx():
stat = cephfs.statx(b'/', libcephfs.CEPH_STATX_MODE, 0)
assert('mode' in stat.keys())
stat = cephfs.statx(b'/', libcephfs.CEPH_STATX_BTIME, 0)
assert('btime' in stat.keys())
fd = cephfs.open(b'file-1', 'w', 0o755)
cephfs.write(fd, b"1111", 0)
cephfs.close(fd)
cephfs.symlink(b'file-1', b'file-2')
stat = cephfs.statx(b'file-2', libcephfs.CEPH_STATX_MODE | libcephfs.CEPH_STATX_BTIME, libcephfs.AT_SYMLINK_NOFOLLOW)
assert('mode' in stat.keys())
assert('btime' in stat.keys())
cephfs.unlink(b'file-2')
cephfs.unlink(b'file-1')
@with_setup(setup_test)
def test_syncfs():
stat = cephfs.sync_fs()
@with_setup(setup_test)
def test_fsync():
fd = cephfs.open(b'file-1', 'w', 0o755)
cephfs.write(fd, b"asdf", 0)
stat = cephfs.fsync(fd, 0)
cephfs.write(fd, b"qwer", 0)
stat = cephfs.fsync(fd, 1)
cephfs.close(fd)
#sync on non-existing fd (assume fd 12345 is not exists)
assert_raises(libcephfs.Error, cephfs.fsync, 12345, 0)
@with_setup(setup_test)
def test_directory():
cephfs.mkdir(b"/temp-directory", 0o755)
cephfs.mkdirs(b"/temp-directory/foo/bar", 0o755)
cephfs.chdir(b"/temp-directory")
assert_equal(cephfs.getcwd(), b"/temp-directory")
cephfs.rmdir(b"/temp-directory/foo/bar")
cephfs.rmdir(b"/temp-directory/foo")
cephfs.rmdir(b"/temp-directory")
assert_raises(libcephfs.ObjectNotFound, cephfs.chdir, b"/temp-directory")
@with_setup(setup_test)
def test_walk_dir():
cephfs.chdir(b"/")
dirs = [b"dir-1", b"dir-2", b"dir-3"]
for i in dirs:
cephfs.mkdir(i, 0o755)
handler = cephfs.opendir(b"/")
d = cephfs.readdir(handler)
dirs += [b".", b".."]
while d:
assert(d.d_name in dirs)
dirs.remove(d.d_name)
d = cephfs.readdir(handler)
assert(len(dirs) == 0)
dirs = [b"/dir-1", b"/dir-2", b"/dir-3"]
for i in dirs:
cephfs.rmdir(i)
cephfs.closedir(handler)
@with_setup(setup_test)
def test_xattr():
assert_raises(libcephfs.OperationNotSupported, cephfs.setxattr, "/", "key", b"value", 0)
cephfs.setxattr("/", "user.key", b"value", 0)
assert_equal(b"value", cephfs.getxattr("/", "user.key"))
cephfs.setxattr("/", "user.big", b"x" * 300, 0)
# Default size is 255, get ERANGE
assert_raises(libcephfs.OutOfRange, cephfs.getxattr, "/", "user.big")
# Pass explicit size, and we'll get the value
assert_equal(300, len(cephfs.getxattr("/", "user.big", 300)))
cephfs.removexattr("/", "user.key")
# user.key is already removed
assert_raises(libcephfs.NoData, cephfs.getxattr, "/", "user.key")
@with_setup(setup_test)
def test_rename():
cephfs.mkdir(b"/a", 0o755)
cephfs.mkdir(b"/a/b", 0o755)
cephfs.rename(b"/a", b"/b")
cephfs.stat(b"/b/b")
cephfs.rmdir(b"/b/b")
cephfs.rmdir(b"/b")
@with_setup(setup_test)
def test_open():
assert_raises(libcephfs.ObjectNotFound, cephfs.open, b'file-1', 'r')
assert_raises(libcephfs.ObjectNotFound, cephfs.open, b'file-1', 'r+')
fd = cephfs.open(b'file-1', 'w', 0o755)
cephfs.write(fd, b"asdf", 0)
cephfs.close(fd)
fd = cephfs.open(b'file-1', 'r', 0o755)
assert_equal(cephfs.read(fd, 0, 4), b"asdf")
cephfs.close(fd)
fd = cephfs.open(b'file-1', 'r+', 0o755)
cephfs.write(fd, b"zxcv", 4)
assert_equal(cephfs.read(fd, 4, 8), b"zxcv")
cephfs.close(fd)
fd = cephfs.open(b'file-1', 'w+', 0o755)
assert_equal(cephfs.read(fd, 0, 4), b"")
cephfs.write(fd, b"zxcv", 4)
assert_equal(cephfs.read(fd, 4, 8), b"zxcv")
cephfs.close(fd)
fd = cephfs.open(b'file-1', os.O_RDWR, 0o755)
cephfs.write(fd, b"asdf", 0)
assert_equal(cephfs.read(fd, 0, 4), b"asdf")
cephfs.close(fd)
assert_raises(libcephfs.OperationNotSupported, cephfs.open, b'file-1', 'a')
cephfs.unlink(b'file-1')
@with_setup(setup_test)
def test_link():
fd = cephfs.open(b'file-1', 'w', 0o755)
cephfs.write(fd, b"1111", 0)
cephfs.close(fd)
cephfs.link(b'file-1', b'file-2')
fd = cephfs.open(b'file-2', 'r', 0o755)
assert_equal(cephfs.read(fd, 0, 4), b"1111")
cephfs.close(fd)
fd = cephfs.open(b'file-2', 'r+', 0o755)
cephfs.write(fd, b"2222", 4)
cephfs.close(fd)
fd = cephfs.open(b'file-1', 'r', 0o755)
assert_equal(cephfs.read(fd, 0, 8), b"11112222")
cephfs.close(fd)
cephfs.unlink(b'file-2')
@with_setup(setup_test)
def test_symlink():
fd = cephfs.open(b'file-1', 'w', 0o755)
cephfs.write(fd, b"1111", 0)
cephfs.close(fd)
cephfs.symlink(b'file-1', b'file-2')
fd = cephfs.open(b'file-2', 'r', 0o755)
assert_equal(cephfs.read(fd, 0, 4), b"1111")
cephfs.close(fd)
fd = cephfs.open(b'file-2', 'r+', 0o755)
cephfs.write(fd, b"2222", 4)
cephfs.close(fd)
fd = cephfs.open(b'file-1', 'r', 0o755)
assert_equal(cephfs.read(fd, 0, 8), b"11112222")
cephfs.close(fd)
cephfs.unlink(b'file-2')
@with_setup(setup_test)
def test_readlink():
fd = cephfs.open(b'/file-1', 'w', 0o755)
cephfs.write(fd, b"1111", 0)
cephfs.close(fd)
cephfs.symlink(b'/file-1', b'/file-2')
d = cephfs.readlink(b"/file-2",100)
assert_equal(d, b"/file-1")
cephfs.unlink(b'/file-2')
cephfs.unlink(b'/file-1')
@with_setup(setup_test)
def test_delete_cwd():
assert_equal(b"/", cephfs.getcwd())
cephfs.mkdir(b"/temp-directory", 0o755)
cephfs.chdir(b"/temp-directory")
cephfs.rmdir(b"/temp-directory")
# getcwd gives you something stale here: it remembers the path string
# even when things are unlinked. It's up to the caller to find out
# whether it really still exists
assert_equal(b"/temp-directory", cephfs.getcwd())
@with_setup(setup_test)
def test_flock():
fd = cephfs.open(b'file-1', 'w', 0o755)
cephfs.flock(fd, fcntl.LOCK_EX, 123);
fd2 = cephfs.open(b'file-1', 'w', 0o755)
assert_raises(libcephfs.WouldBlock, cephfs.flock, fd2,
fcntl.LOCK_EX | fcntl.LOCK_NB, 456);
cephfs.close(fd2)
cephfs.close(fd)
@with_setup(setup_test)
def test_mount_unmount():
test_directory()
cephfs.unmount()
cephfs.mount()
test_open()
@with_setup(setup_test)
def test_mount_root():
cephfs.mkdir(b"/mount-directory", 0o755)
cephfs.unmount()
cephfs.mount(mount_root = b"/mount-directory")
cephfs.unmount()
assert_raises(libcephfs.Error, cephfs.mount, mount_root = b"/nowhere")