]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | /* | |
4 | * Ceph - scalable distributed file system | |
5 | * | |
6 | * Copyright (C) 2015 Red Hat, Inc. | |
7 | * | |
8 | * This is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License version 2.1, as published by the Free Software | |
11 | * Foundation. See file COPYING. | |
12 | * | |
13 | */ | |
14 | ||
15 | #include <stdint.h> | |
16 | #include <tuple> | |
17 | #include <iostream> | |
18 | #include <fstream> | |
19 | #include <stack> | |
20 | ||
21 | #include "include/rados/librgw.h" | |
22 | #include "include/rados/rgw_file.h" | |
1e59de90 TL |
23 | #include "rgw_file.h" |
24 | #include "rgw_lib.h" | |
25 | #include "rgw_lib_frontend.h" // direct requests | |
7c673cae FG |
26 | |
27 | #include "gtest/gtest.h" | |
28 | #include "common/ceph_argparse.h" | |
29 | #include "common/debug.h" | |
11fdf7f2 | 30 | #include "include/ceph_assert.h" |
7c673cae FG |
31 | |
32 | #define dout_subsys ceph_subsys_rgw | |
33 | ||
20effc67 | 34 | using namespace std; |
1e59de90 | 35 | using namespace rgw; // g_rgwlib |
20effc67 | 36 | |
7c673cae FG |
37 | namespace { |
38 | ||
39 | using namespace rgw; | |
40 | using std::get; | |
41 | using std::string; | |
42 | ||
43 | librgw_t rgw_h = nullptr; | |
44 | string userid("testuser"); | |
45 | string access_key(""); | |
46 | string secret_key(""); | |
47 | struct rgw_fs *fs = nullptr; | |
48 | CephContext* cct = nullptr; | |
49 | ||
50 | uint32_t owner_uid = 867; | |
51 | uint32_t owner_gid = 5309; | |
52 | ||
53 | uint32_t magic_uid = 1701; | |
54 | uint32_t magic_gid = 9876; | |
55 | ||
56 | uint32_t create_mask = RGW_SETATTR_UID | RGW_SETATTR_GID | RGW_SETATTR_MODE; | |
57 | ||
58 | string bucket_name("nfsroot"); | |
59 | string dirs1_bucket_name("bdirs1"); | |
60 | string readf_name("toyland"); | |
61 | string readf_out_name("rgwlib_readf.out"); | |
62 | std::string writef_name{"bigbird"}; | |
63 | ||
64 | int n_dirs1_dirs = 3; | |
65 | int n_dirs1_objs = 2; | |
66 | ||
67 | class obj_rec | |
68 | { | |
69 | public: | |
70 | string name; | |
71 | struct rgw_file_handle* fh; | |
72 | struct rgw_file_handle* parent_fh; | |
73 | RGWFileHandle* rgw_fh; // alias into fh | |
74 | ||
75 | struct state { | |
76 | bool readdir; | |
77 | state() : readdir(false) {} | |
78 | } state; | |
79 | ||
80 | obj_rec(string _name, struct rgw_file_handle* _fh, | |
81 | struct rgw_file_handle* _parent_fh, RGWFileHandle* _rgw_fh) | |
82 | : name(std::move(_name)), fh(_fh), parent_fh(_parent_fh), | |
83 | rgw_fh(_rgw_fh) {} | |
84 | ||
85 | void clear() { | |
86 | fh = nullptr; | |
87 | rgw_fh = nullptr; | |
88 | } | |
89 | ||
90 | void sync() { | |
91 | if (fh) | |
92 | rgw_fh = get_rgwfh(fh); | |
93 | } | |
94 | ||
95 | friend ostream& operator<<(ostream& os, const obj_rec& rec); | |
96 | }; | |
97 | ||
98 | ostream& operator<<(ostream& os, const obj_rec& rec) | |
99 | { | |
100 | RGWFileHandle* rgw_fh = rec.rgw_fh; | |
101 | if (rgw_fh) { | |
102 | const char* type = rgw_fh->is_dir() ? "DIR " : "FILE "; | |
103 | os << rec.rgw_fh->full_object_name() | |
104 | << " (" << rec.rgw_fh->object_name() << "): " | |
105 | << type; | |
106 | } | |
107 | return os; | |
108 | } | |
109 | ||
110 | std::stack<obj_rec> obj_stack; | |
111 | std::deque<obj_rec> cleanup_queue; | |
112 | ||
113 | typedef std::vector<obj_rec> obj_vec; | |
114 | typedef std::tuple<obj_rec, obj_vec> dirs1_rec; | |
115 | typedef std::vector<dirs1_rec> dirs1_vec; | |
116 | ||
117 | dirs1_vec dirs_vec; | |
118 | ||
119 | struct obj_rec_st | |
120 | { | |
121 | const obj_rec& obj; | |
122 | const struct stat& st; | |
123 | ||
124 | obj_rec_st(const obj_rec& _obj, const struct stat& _st) | |
125 | : obj(_obj), st(_st) {} | |
126 | }; | |
127 | ||
128 | ostream& operator<<(ostream& os, const obj_rec_st& rec) | |
129 | { | |
130 | RGWFileHandle* rgw_fh = rec.obj.rgw_fh; | |
131 | if (rgw_fh) { | |
132 | const char* type = rgw_fh->is_dir() ? "DIR " : "FILE "; | |
133 | os << rgw_fh->full_object_name() | |
134 | << " (" << rgw_fh->object_name() << "): " | |
135 | << type; | |
136 | const struct stat& st = rec.st; | |
137 | switch(uint8_t(rgw_fh->is_dir())) { | |
138 | case 1: | |
139 | os << " mode: " << st.st_mode; | |
140 | os << " nlinks: " << st.st_nlink; | |
141 | break; | |
142 | case 0: | |
143 | default: | |
144 | os << " mode: " << st.st_mode; | |
145 | os << " size: " << st.st_size; | |
146 | // xxx | |
147 | break; | |
148 | } | |
149 | } | |
150 | return os; | |
151 | } | |
152 | ||
153 | bool do_hier1 = false; | |
154 | bool do_dirs1 = false; | |
155 | bool do_readf = false; | |
156 | bool do_writef = false; | |
157 | bool do_marker1 = false; | |
158 | bool do_create = false; | |
159 | bool do_delete = false; | |
160 | bool do_rename = false; | |
161 | bool do_setattr = false; | |
162 | bool verbose = false; | |
163 | ||
164 | string marker_dir("nfs_marker"); | |
165 | struct rgw_file_handle *bucket_fh = nullptr; | |
166 | struct rgw_file_handle *marker_fh; | |
167 | static constexpr int marker_nobjs = 2*1024; | |
168 | std::deque<obj_rec> marker_objs; | |
169 | ||
170 | using dirent_t = std::tuple<std::string, uint64_t>; | |
171 | struct dirent_vec | |
172 | { | |
173 | std::vector<dirent_t> obj_names; | |
174 | uint32_t count; | |
175 | dirent_vec() : count(0) {} | |
176 | }; | |
177 | ||
178 | obj_rec dirs1_b{dirs1_bucket_name, nullptr, nullptr, nullptr}; | |
179 | ||
180 | dirs1_vec renames_vec; | |
181 | ||
182 | struct { | |
183 | int argc; | |
184 | char **argv; | |
185 | } saved_args; | |
186 | } | |
187 | ||
188 | TEST(LibRGW, INIT) { | |
189 | int ret = librgw_create(&rgw_h, saved_args.argc, saved_args.argv); | |
190 | ASSERT_EQ(ret, 0); | |
191 | ASSERT_NE(rgw_h, nullptr); | |
192 | } | |
193 | ||
20effc67 TL |
194 | TEST(LibRGW, MOUNT_NOROOT) { |
195 | /* do a mount at root="" and verify that it's root is "/" */ | |
196 | struct rgw_fs *fs = nullptr; | |
197 | int ret = rgw_mount2(rgw_h, userid.c_str(), access_key.c_str(), | |
198 | secret_key.c_str(), "", &fs, RGW_MOUNT_FLAG_NONE); | |
199 | ASSERT_EQ(ret, 0); | |
200 | ASSERT_NE(fs, nullptr); | |
201 | ||
202 | auto& root_fh = static_cast<RGWLibFS*>(fs->fs_private)->get_fh(); | |
203 | ASSERT_EQ(root_fh.get_name(), "/"); | |
204 | ||
205 | ret = rgw_umount(fs, RGW_UMOUNT_FLAG_NONE); | |
206 | ASSERT_EQ(ret, 0); | |
207 | } | |
208 | ||
7c673cae | 209 | TEST(LibRGW, MOUNT) { |
3efd9988 FG |
210 | int ret = rgw_mount2(rgw_h, userid.c_str(), access_key.c_str(), |
211 | secret_key.c_str(), "/", &fs, RGW_MOUNT_FLAG_NONE); | |
7c673cae FG |
212 | ASSERT_EQ(ret, 0); |
213 | ASSERT_NE(fs, nullptr); | |
214 | ||
215 | cct = static_cast<RGWLibFS*>(fs->fs_private)->get_context(); | |
216 | } | |
217 | ||
218 | TEST(LibRGW, SETUP_HIER1) | |
219 | { | |
220 | if (do_hier1) { | |
221 | (void) rgw_lookup(fs, fs->root_fh, bucket_name.c_str(), &bucket_fh, | |
eafe8130 | 222 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
223 | if (! bucket_fh) { |
224 | if (do_create) { | |
225 | struct stat st; | |
226 | ||
227 | st.st_uid = owner_uid; | |
228 | st.st_gid = owner_gid; | |
229 | st.st_mode = 755; | |
230 | ||
231 | int rc = rgw_mkdir(fs, fs->root_fh, bucket_name.c_str(), &st, | |
232 | create_mask, &bucket_fh, RGW_MKDIR_FLAG_NONE); | |
233 | ASSERT_EQ(rc, 0); | |
234 | } | |
235 | } | |
236 | ||
237 | ASSERT_NE(bucket_fh, nullptr); | |
238 | ||
239 | if (do_create) { | |
240 | /* create objects directly */ | |
241 | std::vector<std::string> obj_names = | |
242 | {"foo/bar/baz/quux", | |
243 | "foo/f1", | |
244 | "foo/f2", | |
245 | "foo/bar/f1", | |
246 | "foo/bar/d1/", | |
247 | "foo/bar/baz/hungry", | |
248 | "foo/bar/baz/hungry/", | |
249 | "foo/bar/baz/momma", | |
250 | "foo/bar/baz/bear/", | |
251 | "foo/bar/baz/sasquatch", | |
252 | "foo/bar/baz/sasquatch/", | |
253 | "foo/bar/baz/frobozz"}; | |
254 | ||
255 | buffer::list bl; // empty object | |
256 | RGWLibFS *fs_private = static_cast<RGWLibFS*>(fs->fs_private); | |
257 | ||
258 | for (const auto& obj_name : obj_names) { | |
259 | if (verbose) { | |
260 | std::cout << "creating: " << bucket_name << ":" << obj_name | |
261 | << std::endl; | |
262 | } | |
f67539c2 | 263 | RGWPutObjRequest req(cct, |
1e59de90 | 264 | g_rgwlib->get_driver()->get_user(fs_private->get_user()->user_id), |
f67539c2 | 265 | bucket_name, obj_name, bl); |
1e59de90 | 266 | int rc = g_rgwlib->get_fe()->execute_req(&req); |
7c673cae FG |
267 | int rc2 = req.get_ret(); |
268 | ASSERT_EQ(rc, 0); | |
269 | ASSERT_EQ(rc2, 0); | |
270 | } | |
271 | } | |
272 | } | |
273 | } | |
274 | ||
275 | TEST(LibRGW, SETUP_DIRS1) { | |
276 | if (do_dirs1) { | |
277 | int rc; | |
278 | struct stat st; | |
279 | ||
280 | st.st_uid = owner_uid; | |
281 | st.st_gid = owner_gid; | |
282 | st.st_mode = 755; | |
283 | ||
284 | dirs1_b.parent_fh = fs->root_fh; | |
285 | ||
286 | (void) rgw_lookup(fs, dirs1_b.parent_fh, dirs1_bucket_name.c_str(), | |
eafe8130 | 287 | &dirs1_b.fh, nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
288 | |
289 | if (! dirs1_b.fh) { | |
290 | if (do_create) { | |
291 | rc = rgw_mkdir(fs, dirs1_b.parent_fh, dirs1_b.name.c_str(), &st, | |
292 | create_mask, &dirs1_b.fh, RGW_MKDIR_FLAG_NONE); | |
293 | ASSERT_EQ(rc, 0); | |
294 | } else { | |
295 | /* no top-level dir and can't create it--skip remaining tests */ | |
296 | return; | |
297 | } | |
298 | } | |
299 | dirs1_b.sync(); | |
300 | ||
301 | /* make top-level dirs */ | |
302 | int d_ix; | |
303 | obj_vec ovec; | |
304 | for (d_ix = 0; d_ix < n_dirs1_dirs; ++d_ix) { | |
305 | std::string dname{"dir_"}; | |
306 | dname += to_string(d_ix); | |
307 | obj_rec dir{dname, nullptr, dirs1_b.fh, nullptr}; | |
308 | ovec.clear(); | |
309 | ||
310 | (void) rgw_lookup(fs, dir.parent_fh, dir.name.c_str(), &dir.fh, | |
eafe8130 | 311 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
312 | if (! dir.fh) { |
313 | if (do_create) { | |
314 | rc = rgw_mkdir(fs, dir.parent_fh, dir.name.c_str(), &st, create_mask, | |
315 | &dir.fh, RGW_MKDIR_FLAG_NONE); | |
316 | ASSERT_EQ(rc, 0); | |
317 | } | |
318 | } | |
319 | ||
320 | ASSERT_NE(dir.fh, nullptr); | |
321 | dir.sync(); | |
322 | ASSERT_NE(dir.rgw_fh, nullptr); | |
323 | ASSERT_TRUE(dir.rgw_fh->is_dir()); | |
324 | ||
325 | int f_ix; | |
326 | for (f_ix = 0; f_ix < n_dirs1_objs; ++f_ix) { | |
327 | /* child dir */ | |
328 | std::string sdname{"sdir_"}; | |
329 | sdname += to_string(f_ix); | |
330 | obj_rec sdir{sdname, nullptr, dir.fh, nullptr}; | |
331 | ||
332 | (void) rgw_lookup(fs, sdir.parent_fh, sdir.name.c_str(), &sdir.fh, | |
eafe8130 | 333 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
334 | |
335 | if (! sdir.fh) { | |
336 | if (do_create) { | |
337 | rc = rgw_mkdir(fs, sdir.parent_fh, sdir.name.c_str(), &st, | |
338 | create_mask, &sdir.fh, RGW_MKDIR_FLAG_NONE); | |
339 | ASSERT_EQ(rc, 0); | |
340 | } | |
341 | } | |
342 | ||
343 | ASSERT_NE(sdir.fh, nullptr); // suppress !lookup && !create case | |
344 | ||
345 | sdir.sync(); | |
346 | ASSERT_TRUE(sdir.rgw_fh->is_dir()); | |
347 | ovec.push_back(sdir); | |
348 | ||
349 | /* child file */ | |
350 | std::string sfname{"sfile_"}; | |
351 | ||
352 | sfname += to_string(f_ix); | |
353 | obj_rec sf{sfname, nullptr, dir.fh, nullptr}; | |
354 | ||
355 | (void) rgw_lookup(fs, sf.parent_fh, sf.name.c_str(), &sf.fh, | |
eafe8130 | 356 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
357 | |
358 | if (! sf.fh) { | |
359 | if (do_create) { | |
360 | /* make a new file object (the hard way) */ | |
361 | rc = rgw_lookup(fs, sf.parent_fh, sf.name.c_str(), &sf.fh, | |
eafe8130 | 362 | nullptr, 0, RGW_LOOKUP_FLAG_CREATE); |
7c673cae FG |
363 | ASSERT_EQ(rc, 0); |
364 | sf.sync(); | |
365 | ASSERT_TRUE(sf.rgw_fh->is_file()); | |
366 | ||
367 | /* because we made it the hard way, fixup attributes */ | |
368 | struct stat st; | |
369 | st.st_uid = owner_uid; | |
370 | st.st_gid = owner_gid; | |
371 | st.st_mode = 644; | |
372 | sf.rgw_fh->create_stat(&st, create_mask); | |
373 | ||
374 | /* open handle */ | |
375 | rc = rgw_open(fs, sf.fh, 0 /* posix flags */, 0 /* flags */); | |
376 | ASSERT_EQ(rc, 0); | |
377 | ASSERT_TRUE(sf.rgw_fh->is_open()); | |
378 | /* stage seq write */ | |
379 | size_t nbytes; | |
380 | string data = "data for " + sf.name; | |
381 | rc = rgw_write(fs, sf.fh, 0, data.length(), &nbytes, | |
382 | (void*) data.c_str(), RGW_WRITE_FLAG_NONE); | |
383 | ASSERT_EQ(rc, 0); | |
384 | ASSERT_EQ(nbytes, data.length()); | |
385 | /* commit write transaction */ | |
386 | rc = rgw_close(fs, sf.fh, 0 /* flags */); | |
387 | ASSERT_EQ(rc, 0); | |
388 | } | |
389 | } else { | |
390 | sf.sync(); | |
391 | ASSERT_TRUE(sf.rgw_fh->is_file()); | |
392 | } | |
393 | ||
394 | if (sf.fh) | |
395 | ovec.push_back(sf); | |
396 | } | |
397 | dirs_vec.push_back(dirs1_rec{dir, ovec}); | |
398 | } | |
399 | } /* dirs1 top-level !exist */ | |
400 | } | |
401 | ||
402 | TEST(LibRGW, SETATTR) { | |
403 | if (do_dirs1) { | |
404 | if (do_setattr) { | |
405 | ||
406 | int rc; | |
407 | struct stat st; | |
408 | ||
409 | st.st_uid = owner_uid; | |
410 | st.st_gid = owner_gid; | |
411 | st.st_mode = 755; | |
412 | ||
413 | std::string dname{"dir_0"}; | |
414 | obj_rec dir{dname, nullptr, dirs1_b.fh, nullptr}; | |
415 | ||
416 | /* dir_0 MUST exist and MUST be resident */ | |
417 | (void) rgw_lookup(fs, dir.parent_fh, dir.name.c_str(), &dir.fh, | |
eafe8130 | 418 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
419 | |
420 | ASSERT_NE(dir.fh, nullptr); | |
421 | dir.sync(); | |
422 | ASSERT_NE(dir.rgw_fh, nullptr); | |
423 | ASSERT_TRUE(dir.rgw_fh->is_dir()); | |
424 | ||
425 | /* child file */ | |
426 | std::string sfname{"setattr_file_0"}; | |
427 | obj_rec sf{sfname, nullptr, dir.fh, nullptr}; | |
428 | ||
429 | (void) rgw_lookup(fs, sf.parent_fh, sf.name.c_str(), &sf.fh, | |
eafe8130 | 430 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
431 | |
432 | if (! sf.fh) { | |
433 | /* make a new file object (the hard way) */ | |
434 | rc = rgw_lookup(fs, sf.parent_fh, sf.name.c_str(), &sf.fh, | |
eafe8130 | 435 | nullptr, 0, RGW_LOOKUP_FLAG_CREATE); |
7c673cae FG |
436 | ASSERT_EQ(rc, 0); |
437 | sf.sync(); | |
438 | ASSERT_TRUE(sf.rgw_fh->is_file()); | |
439 | ||
440 | /* because we made it the hard way, fixup attributes */ | |
441 | st.st_uid = owner_uid; | |
442 | st.st_gid = owner_gid; | |
443 | st.st_mode = 644; | |
444 | sf.rgw_fh->create_stat(&st, create_mask); | |
445 | ||
446 | /* open handle */ | |
447 | rc = rgw_open(fs, sf.fh, 0 /* posix flags */, 0 /* flags */); | |
448 | ASSERT_EQ(rc, 0); | |
449 | ASSERT_TRUE(sf.rgw_fh->is_open()); | |
450 | /* stage seq write */ | |
451 | size_t nbytes; | |
452 | string data = "data for " + sf.name; | |
453 | rc = rgw_write(fs, sf.fh, 0, data.length(), &nbytes, | |
454 | (void*) data.c_str(), RGW_WRITE_FLAG_NONE); | |
455 | ASSERT_EQ(rc, 0); | |
456 | ASSERT_EQ(nbytes, data.length()); | |
457 | /* commit write transaction */ | |
458 | rc = rgw_close(fs, sf.fh, 0 /* flags */); | |
459 | ASSERT_EQ(rc, 0); | |
460 | } else { | |
461 | sf.sync(); | |
462 | ASSERT_TRUE(sf.rgw_fh->is_file()); | |
463 | } | |
464 | ||
465 | /* sf MUST now be materialized--now change it's attributes */ | |
466 | st.st_uid = magic_uid; | |
467 | st.st_gid = magic_gid; | |
468 | ||
469 | rc = rgw_setattr(fs, sf.fh, &st, create_mask, RGW_SETATTR_FLAG_NONE); | |
470 | ASSERT_EQ(rc, 0); | |
471 | ||
472 | /* force evict--subsequent lookups must reload */ | |
473 | static_cast<RGWLibFS*>(fs->fs_private)->release_evict(sf.rgw_fh); | |
474 | ||
475 | sf.clear(); | |
476 | ||
477 | /* revalidate -- expect magic uid and gid */ | |
478 | (void) rgw_lookup(fs, sf.parent_fh, sf.name.c_str(), &sf.fh, | |
eafe8130 | 479 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
480 | sf.sync(); |
481 | ASSERT_NE(sf.fh, nullptr); | |
482 | ||
483 | memset(&st, 0, sizeof(struct stat)); /* nothing up my sleeve... */ | |
484 | ||
485 | rc = rgw_getattr(fs, sf.fh, &st, RGW_GETATTR_FLAG_NONE); | |
486 | ASSERT_EQ(rc, 0); | |
487 | ||
488 | ASSERT_EQ(st.st_uid, magic_uid); | |
489 | ASSERT_EQ(st.st_gid, magic_gid); | |
490 | ||
491 | /* release 1 ref on sf */ | |
492 | rgw_fh_rele(fs, sf.fh, RGW_FH_RELE_FLAG_NONE); | |
493 | ||
494 | /* release 1 ref on dir */ | |
495 | rgw_fh_rele(fs, dir.fh, RGW_FH_RELE_FLAG_NONE); | |
496 | } /* dirs1 */ | |
497 | } | |
498 | } | |
499 | ||
500 | TEST(LibRGW, RGW_CREATE_DIRS1) { | |
501 | /* verify rgw_create (create [empty] file objects the easy way) */ | |
502 | if (do_dirs1) { | |
503 | if (do_create) { | |
504 | int rc; | |
505 | struct stat st; | |
506 | ||
507 | st.st_uid = owner_uid; | |
508 | st.st_gid = owner_gid; | |
509 | st.st_mode = 644; | |
510 | ||
511 | for (auto& dirs_rec : dirs_vec) { | |
512 | /* create 1 more file in each sdir */ | |
513 | obj_rec& dir = get<0>(dirs_rec); | |
514 | std::string sfname{"sfile_" + to_string(n_dirs1_objs)}; | |
515 | obj_rec sf{sfname, nullptr, dir.fh, nullptr}; | |
516 | (void) rgw_lookup(fs, sf.parent_fh, sf.name.c_str(), &sf.fh, | |
eafe8130 | 517 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
518 | if (! sf.fh) { |
519 | rc = rgw_create(fs, sf.parent_fh, sf.name.c_str(), &st, create_mask, | |
520 | &sf.fh, 0 /* posix flags */, RGW_CREATE_FLAG_NONE); | |
521 | ASSERT_EQ(rc, 0); | |
522 | } | |
523 | sf.sync(); | |
524 | } | |
525 | n_dirs1_objs++; | |
526 | } | |
527 | } | |
528 | } | |
529 | ||
530 | TEST(LibRGW, RGW_SETUP_RENAME1) { | |
531 | /* verify rgw_create (create [empty] file objects the easy way) */ | |
532 | if (do_rename) { | |
533 | int rc; | |
534 | struct stat st; | |
535 | obj_vec ovec; | |
536 | ||
537 | st.st_uid = owner_uid; | |
538 | st.st_gid = owner_gid; | |
539 | st.st_mode = 755; | |
540 | ||
541 | for (int b_ix : {0, 1}) { | |
f67539c2 | 542 | std::string bname{"brename" + to_string(b_ix)}; |
7c673cae FG |
543 | obj_rec brec{bname, nullptr, nullptr, nullptr}; |
544 | (void) rgw_lookup(fs, fs->root_fh, brec.name.c_str(), &brec.fh, | |
eafe8130 | 545 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
546 | if (! brec.fh) { |
547 | if (do_create) { | |
548 | struct stat st; | |
549 | int rc = rgw_mkdir(fs, fs->root_fh, brec.name.c_str(), &st, | |
550 | create_mask, &brec.fh, RGW_MKDIR_FLAG_NONE); | |
551 | ASSERT_EQ(rc, 0); | |
552 | } | |
553 | } | |
554 | ASSERT_NE(brec.fh, nullptr); | |
555 | brec.sync(); | |
556 | ||
557 | st.st_mode = 644; /* file mask */ | |
558 | ||
559 | for (int f_ix : {0, 1}) { | |
560 | std::string rfname{"rfile_"}; | |
561 | rfname += to_string(f_ix); | |
562 | obj_rec rf{rfname, nullptr, brec.fh, nullptr}; | |
563 | (void) rgw_lookup(fs, rf.parent_fh, rf.name.c_str(), &rf.fh, | |
eafe8130 | 564 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
565 | if (! rf.fh) { |
566 | rc = rgw_create(fs, rf.parent_fh, rf.name.c_str(), &st, create_mask, | |
567 | &rf.fh, 0 /* posix flags */, RGW_CREATE_FLAG_NONE); | |
568 | ASSERT_EQ(rc, 0); | |
569 | } | |
570 | rf.sync(); | |
571 | ovec.push_back(rf); | |
572 | } | |
573 | renames_vec.push_back(dirs1_rec{brec, ovec}); | |
574 | ovec.clear(); | |
575 | } /* b_ix */ | |
576 | } | |
577 | } | |
578 | ||
579 | TEST(LibRGW, RGW_INTRABUCKET_RENAME1) { | |
580 | /* rgw_rename a file within a bucket */ | |
581 | if (do_rename) { | |
582 | int rc; | |
583 | obj_rec& bdir0 = get<0>(renames_vec[0]); | |
584 | obj_rec& src_obj = get<1>(renames_vec[0])[0]; | |
585 | std::string rfname{"rfile_r0"}; | |
586 | if (verbose) { | |
587 | std::cout << "rename file " << src_obj.name << " to " | |
588 | << rfname << " (bucket " << bdir0.name << ")" | |
589 | << std::endl; | |
590 | } | |
591 | rc = rgw_rename(fs, bdir0.fh, src_obj.name.c_str(), bdir0.fh, | |
592 | rfname.c_str(), RGW_RENAME_FLAG_NONE); | |
593 | ASSERT_EQ(rc, 0); | |
594 | } | |
595 | } | |
596 | ||
597 | TEST(LibRGW, RGW_CROSSBUCKET_RENAME1) { | |
598 | /* rgw_rename a file within a bucket */ | |
599 | if (do_rename) { | |
600 | int rc; | |
601 | obj_rec& bdir0 = get<0>(renames_vec[0]); | |
602 | obj_rec& bdir1 = get<0>(renames_vec[1]); | |
603 | obj_rec& src_obj = get<1>(renames_vec[0])[1]; | |
604 | std::string rfname{"rfile_rhilldog"}; | |
605 | if (verbose) { | |
606 | std::cout << "rename file " << src_obj.name | |
607 | << " (bucket " << bdir0.name << ") to " | |
608 | << rfname << " (bucket " << bdir1.name << ")" | |
609 | << std::endl; | |
610 | } | |
611 | rc = rgw_rename(fs, bdir0.fh, src_obj.name.c_str(), bdir1.fh, | |
612 | rfname.c_str(), RGW_RENAME_FLAG_NONE); | |
613 | ASSERT_EQ(rc, 0); | |
614 | } | |
615 | } | |
616 | ||
f67539c2 | 617 | #if 0 /* XXX inconsistent failure here */ |
7c673cae FG |
618 | TEST(LibRGW, BAD_DELETES_DIRS1) { |
619 | if (do_dirs1) { | |
620 | int rc; | |
621 | ||
622 | if (dirs_vec.size() == 0) { | |
623 | /* skip */ | |
624 | return; | |
625 | } | |
626 | ||
627 | if (do_delete) { | |
628 | /* try to unlink a non-empty directory (bucket) */ | |
629 | rc = rgw_unlink(fs, dirs1_b.parent_fh, dirs1_b.name.c_str(), | |
630 | RGW_UNLINK_FLAG_NONE); | |
631 | ASSERT_NE(rc, 0); | |
632 | } | |
633 | /* try to unlink a non-empty directory (non-bucket) */ | |
634 | obj_rec& sdir_0 = get<1>(dirs_vec[0])[0]; | |
635 | ASSERT_EQ(sdir_0.name, "sdir_0"); | |
636 | ASSERT_TRUE(sdir_0.rgw_fh->is_dir()); | |
637 | /* XXX we can't enforce this currently */ | |
638 | #if 0 | |
639 | ASSERT_EQ(sdir_0.name, "sdir_0"); | |
640 | ASSERT_TRUE(sdir_0.rgw_fh->is_dir()); | |
641 | rc = rgw_unlink(fs, sdir_0.parent_fh, sdir_0.name.c_str(), | |
642 | RGW_UNLINK_FLAG_NONE); | |
643 | ASSERT_NE(rc, 0); | |
644 | #endif | |
645 | } | |
646 | } | |
f67539c2 | 647 | #endif |
7c673cae FG |
648 | |
649 | TEST(LibRGW, GETATTR_DIRS1) | |
650 | { | |
651 | if (do_dirs1) { | |
652 | int rc; | |
653 | struct stat st; | |
654 | for (auto& dirs_rec : dirs_vec) { | |
655 | obj_rec& dir = get<0>(dirs_rec); | |
656 | if (verbose) { | |
657 | std::cout << "scanning objects in " | |
658 | << dir.rgw_fh->full_object_name() | |
659 | << std::endl; | |
660 | } | |
661 | for (auto& sobj : get<1>(dirs_rec)) { | |
662 | rc = rgw_getattr(fs, sobj.fh, &st, RGW_GETATTR_FLAG_NONE); | |
663 | ASSERT_EQ(rc, 0); | |
664 | /* validate, pretty-print */ | |
665 | if (sobj.rgw_fh->object_name().find("sfile") != std::string::npos) { | |
666 | ASSERT_TRUE(sobj.rgw_fh->is_file()); | |
667 | ASSERT_TRUE(S_ISREG(st.st_mode)); | |
668 | } | |
669 | if (sobj.rgw_fh->object_name().find("sdir") != std::string::npos) { | |
670 | ASSERT_TRUE(sobj.rgw_fh->is_dir()); | |
671 | ASSERT_TRUE(S_ISDIR(st.st_mode)); | |
672 | } | |
673 | /* validate Unix owners */ | |
674 | ASSERT_EQ(st.st_uid, owner_uid); | |
675 | ASSERT_EQ(st.st_gid, owner_gid); | |
676 | if (verbose) { | |
677 | obj_rec_st rec_st{sobj, st}; | |
678 | std::cout << "\t" | |
679 | << rec_st | |
680 | << std::endl; | |
681 | } | |
682 | } | |
683 | } | |
684 | } | |
685 | } | |
686 | ||
687 | TEST(LibRGW, READ_DIRS1) | |
688 | { | |
689 | if (do_dirs1) { | |
690 | int rc; | |
691 | char buf[256]; | |
692 | size_t nread; | |
693 | for (auto& dirs_rec : dirs_vec) { | |
694 | obj_rec& dir = get<0>(dirs_rec); | |
695 | if (verbose) { | |
696 | std::cout << "read back objects in " | |
697 | << dir.rgw_fh->full_object_name() | |
698 | << std::endl; | |
699 | } | |
700 | for (auto& sobj : get<1>(dirs_rec)) { | |
701 | /* only the first 2 file objects have data */ | |
702 | if ((sobj.rgw_fh->object_name().find("sfile_0") | |
703 | != std::string::npos) || | |
704 | (sobj.rgw_fh->object_name().find("sfile_1") | |
705 | != std::string::npos)) { | |
706 | ASSERT_TRUE(sobj.rgw_fh->is_file()); | |
707 | ASSERT_EQ(sobj.rgw_fh->get_size(), 16UL); | |
708 | // do it | |
709 | memset(buf, 0, 256); | |
710 | if (verbose) { | |
711 | std::cout << "reading 0,256 " << sobj.rgw_fh->relative_object_name() | |
712 | << std::endl; | |
713 | } | |
714 | rc = rgw_read(fs, sobj.fh, 0, 256, &nread, buf, RGW_READ_FLAG_NONE); | |
715 | ASSERT_EQ(rc, 0); | |
716 | if (verbose) { | |
717 | std::cout << "\tread back from " << sobj.name | |
718 | << " : \"" << buf << "\"" | |
719 | << std::endl; | |
720 | } | |
721 | } | |
722 | } | |
723 | } | |
724 | } | |
725 | } | |
726 | ||
727 | TEST(LibRGW, READF_SETUP1) | |
728 | { | |
729 | struct stat st; | |
730 | if (do_dirs1) { | |
731 | if (do_create) { | |
732 | if ((! stat(readf_out_name.c_str(), &st)) && | |
733 | (S_ISREG(st.st_mode)) && | |
734 | (st.st_size == 6291456)) | |
735 | return; | |
736 | ofstream of; | |
737 | of.open(readf_out_name, ios::out|ios::app|ios::binary); | |
738 | for (int ix1 = 0; ix1 < 6; ++ix1) { | |
739 | for (int ix2 = 0; ix2 < 1024*1024; ++ix2) { | |
740 | of << ix1; | |
741 | } | |
742 | } | |
743 | } | |
744 | } | |
745 | } | |
746 | ||
747 | TEST(LibRGW, READF_DIRS1) { | |
748 | if (do_dirs1) { | |
749 | if (do_readf) { | |
750 | obj_rec fobj{readf_name, nullptr, dirs1_b.fh, nullptr}; | |
751 | ||
752 | int rc = rgw_lookup(fs, dirs1_b.fh, fobj.name.c_str(), &fobj.fh, | |
eafe8130 | 753 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
754 | ASSERT_EQ(rc, 0); |
755 | ASSERT_NE(fobj.fh, nullptr); | |
756 | fobj.sync(); | |
757 | ||
758 | ofstream of; | |
759 | of.open(readf_out_name, ios::out|ios::app|ios::binary); | |
760 | int bufsz = 1024 * 1024 * sizeof(char); | |
11fdf7f2 | 761 | auto buffer = std::make_unique<char[]>(bufsz); |
7c673cae FG |
762 | |
763 | uint64_t offset = 0; | |
764 | uint64_t length = bufsz; | |
765 | for (int ix = 0; ix < 6; ++ix) { | |
766 | size_t nread = 0; | |
224ce89b WB |
767 | memset(buffer.get(), 0, length); // XXX |
768 | rc = rgw_read(fs, fobj.fh, offset, length, &nread, buffer.get(), | |
7c673cae FG |
769 | RGW_READ_FLAG_NONE); |
770 | ASSERT_EQ(rc, 0); | |
771 | ASSERT_EQ(nread, length); | |
224ce89b | 772 | of.write(buffer.get(), length); |
7c673cae FG |
773 | offset += nread; |
774 | } | |
775 | of.close(); | |
7c673cae FG |
776 | rgw_fh_rele(fs, fobj.fh, 0 /* flags */); |
777 | } | |
778 | } | |
779 | } | |
780 | ||
781 | TEST(LibRGW, WRITEF_DIRS1) { | |
782 | if (do_dirs1) { | |
783 | if (do_writef) { | |
784 | int rc; | |
785 | ifstream ifs; | |
786 | ifs.open(readf_out_name, ios::out|ios::app|ios::binary); | |
787 | ASSERT_TRUE(ifs.is_open()); | |
788 | ||
789 | obj_rec fobj{writef_name, nullptr, dirs1_b.fh, nullptr}; | |
790 | ||
791 | (void) rgw_lookup(fs, fobj.parent_fh, fobj.name.c_str(), &fobj.fh, | |
eafe8130 | 792 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
793 | if (! fobj.fh) { |
794 | if (do_create) { | |
795 | /* make a new file object (the hard way) */ | |
796 | rc = rgw_lookup(fs, fobj.parent_fh, fobj.name.c_str(), &fobj.fh, | |
eafe8130 | 797 | nullptr, 0, RGW_LOOKUP_FLAG_CREATE); |
7c673cae FG |
798 | ASSERT_EQ(rc, 0); |
799 | } | |
800 | } | |
801 | ASSERT_NE(fobj.fh, nullptr); | |
802 | fobj.sync(); | |
803 | ||
804 | /* begin write transaction */ | |
805 | rc = rgw_open(fs, fobj.fh, 0 /* posix flags */, 0 /* flags */); | |
806 | ASSERT_EQ(rc, 0); | |
807 | ASSERT_TRUE(fobj.rgw_fh->is_open()); | |
808 | ||
809 | int bufsz = 1024 * 1024 * sizeof(char); | |
810 | char *buffer = (char*) malloc(bufsz); | |
811 | ||
812 | uint64_t offset = 0; | |
813 | uint64_t length = bufsz; | |
814 | for (int ix = 0; ix < 6; ++ix) { | |
815 | ASSERT_TRUE(ifs.good()); | |
816 | ifs.read(buffer, bufsz); | |
817 | size_t nwritten = 0; | |
818 | string str; | |
819 | str.assign(buffer, 4); | |
820 | if (verbose) { | |
821 | std::cout << "read and writing " << length << " bytes" | |
822 | << " from " << readf_out_name | |
823 | << " at offset " << offset | |
824 | << " (" << str << "... [first 4 chars])" | |
825 | << std::endl; | |
826 | } | |
827 | char* leakbuf = (char*) malloc(bufsz); | |
828 | memcpy(leakbuf, buffer, length); | |
829 | rc = rgw_write(fs, fobj.fh, offset, length, &nwritten, leakbuf, | |
830 | RGW_WRITE_FLAG_NONE); | |
831 | ASSERT_EQ(rc, 0); | |
832 | ASSERT_EQ(nwritten, length); | |
833 | offset += length; | |
834 | } | |
835 | ||
836 | /* commit write transaction */ | |
837 | rc = rgw_close(fs, fobj.fh, RGW_CLOSE_FLAG_NONE); | |
838 | ASSERT_EQ(rc, 0); | |
839 | ||
840 | ifs.close(); | |
841 | free(buffer); | |
842 | rgw_fh_rele(fs, fobj.fh, 0 /* flags */); | |
843 | } | |
844 | } | |
845 | } | |
846 | ||
847 | TEST(LibRGW, RELEASE_DIRS1) { | |
848 | if (do_dirs1) { | |
849 | /* force release of handles for children of dirs1--force subsequent | |
850 | * checks to reload them from the cluster. | |
851 | * | |
852 | * while doing this, verify handle cleanup and correct LRU state | |
853 | * (not reachable) | |
854 | */ | |
855 | int rc; | |
856 | for (auto& dirs_rec : dirs_vec) { | |
857 | for (auto& obj : get<1>(dirs_rec)) { | |
858 | if (verbose) { | |
859 | std::cout << "release " << obj.name | |
860 | << " type: " << obj.rgw_fh->stype() | |
861 | << " refs: " << obj.rgw_fh->get_refcnt() | |
862 | << std::endl; | |
863 | } | |
864 | ASSERT_EQ(obj.rgw_fh->get_refcnt(), 2UL); | |
865 | rc = rgw_fh_rele(fs, obj.fh, 0 /* flags */); | |
866 | ASSERT_EQ(rc, 0); | |
867 | ASSERT_EQ(obj.rgw_fh->get_refcnt(), 1UL); | |
868 | /* try-discard handle */ | |
869 | /* clear obj_rec vec */ | |
870 | } | |
871 | } | |
872 | } | |
873 | } | |
874 | ||
875 | extern "C" { | |
1e59de90 | 876 | static int r1_cb(const char* name, void *arg, uint64_t offset, |
eafe8130 | 877 | struct stat* st, uint32_t st_mask, |
7c673cae FG |
878 | uint32_t flags) { |
879 | struct rgw_file_handle* parent_fh | |
880 | = static_cast<struct rgw_file_handle*>(arg); | |
881 | RGWFileHandle* rgw_fh = get_rgwfh(parent_fh); | |
882 | lsubdout(cct, rgw, 10) << __func__ | |
883 | << " bucket=" << rgw_fh->bucket_name() | |
884 | << " dir=" << rgw_fh->full_object_name() | |
885 | << " called back name=" << name | |
886 | << " flags=" << flags | |
887 | << dendl; | |
888 | string name_str{name}; | |
889 | if (! ((name_str == ".") || | |
890 | (name_str == ".."))) { | |
891 | obj_stack.push( | |
892 | obj_rec{std::move(name_str), nullptr, parent_fh, nullptr}); | |
893 | } | |
894 | return true; /* XXX */ | |
895 | } | |
896 | } | |
897 | ||
898 | TEST(LibRGW, HIER1) { | |
899 | if (do_hier1) { | |
900 | int rc; | |
901 | obj_stack.push( | |
902 | obj_rec{bucket_name, nullptr, nullptr, nullptr}); | |
903 | while (! obj_stack.empty()) { | |
904 | auto& elt = obj_stack.top(); | |
905 | if (! elt.fh) { | |
906 | struct rgw_file_handle* parent_fh = elt.parent_fh | |
907 | ? elt.parent_fh : fs->root_fh; | |
908 | RGWFileHandle* pfh = get_rgwfh(parent_fh); | |
909 | rgw::ignore(pfh); | |
910 | lsubdout(cct, rgw, 10) | |
911 | << "rgw_lookup:" | |
912 | << " parent object_name()=" << pfh->object_name() | |
913 | << " parent full_object_name()=" << pfh->full_object_name() | |
914 | << " elt.name=" << elt.name | |
915 | << dendl; | |
916 | rc = rgw_lookup(fs, parent_fh, elt.name.c_str(), &elt.fh, | |
eafe8130 | 917 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
918 | ASSERT_EQ(rc, 0); |
919 | // XXXX | |
920 | RGWFileHandle* efh = get_rgwfh(elt.fh); | |
921 | rgw::ignore(efh); | |
922 | lsubdout(cct, rgw, 10) | |
923 | << "rgw_lookup result:" | |
924 | << " elt object_name()=" << efh->object_name() | |
925 | << " elt full_object_name()=" << efh->full_object_name() | |
926 | << " elt.name=" << elt.name | |
927 | << dendl; | |
928 | ||
929 | ASSERT_NE(elt.fh, nullptr); | |
930 | elt.rgw_fh = get_rgwfh(elt.fh); | |
931 | elt.parent_fh = elt.rgw_fh->get_parent()->get_fh(); | |
932 | ASSERT_EQ(elt.parent_fh, parent_fh); | |
933 | continue; | |
934 | } else { | |
935 | // we have a handle in some state in top position | |
936 | switch(elt.fh->fh_type) { | |
937 | case RGW_FS_TYPE_DIRECTORY: | |
938 | if (! elt.state.readdir) { | |
939 | // descending | |
940 | uint64_t offset = 0; | |
941 | bool eof; // XXX | |
942 | lsubdout(cct, rgw, 10) | |
943 | << "readdir in" | |
944 | << " bucket: " << elt.rgw_fh->bucket_name() | |
945 | << " object_name: " << elt.rgw_fh->object_name() | |
946 | << " full_name: " << elt.rgw_fh->full_object_name() | |
947 | << dendl; | |
948 | rc = rgw_readdir(fs, elt.fh, &offset, r1_cb, elt.fh, &eof, | |
949 | RGW_READDIR_FLAG_DOTDOT); | |
950 | elt.state.readdir = true; | |
951 | ASSERT_EQ(rc, 0); | |
952 | // ASSERT_TRUE(eof); // XXXX working incorrectly w/single readdir | |
953 | } else { | |
954 | // ascending | |
955 | std::cout << elt << std::endl; | |
956 | cleanup_queue.push_back(elt); | |
957 | obj_stack.pop(); | |
958 | } | |
959 | break; | |
960 | case RGW_FS_TYPE_FILE: | |
961 | // ascending | |
962 | std::cout << elt << std::endl; | |
963 | cleanup_queue.push_back(elt); | |
964 | obj_stack.pop(); | |
965 | break; | |
966 | default: | |
11fdf7f2 | 967 | ceph_abort(); |
7c673cae FG |
968 | }; |
969 | } | |
970 | } | |
971 | } | |
972 | } | |
973 | ||
974 | TEST(LibRGW, MARKER1_SETUP_BUCKET) { | |
975 | /* "large" directory enumeration test. this one deals only with | |
976 | * file objects */ | |
977 | if (do_marker1) { | |
978 | struct stat st; | |
979 | int ret; | |
980 | ||
981 | st.st_uid = owner_uid; | |
982 | st.st_gid = owner_gid; | |
983 | st.st_mode = 755; | |
984 | ||
985 | if (do_create) { | |
986 | ret = rgw_mkdir(fs, bucket_fh, marker_dir.c_str(), &st, create_mask, | |
987 | &marker_fh, RGW_MKDIR_FLAG_NONE); | |
988 | } else { | |
989 | ret = rgw_lookup(fs, bucket_fh, marker_dir.c_str(), &marker_fh, | |
eafe8130 | 990 | nullptr, 0, RGW_LOOKUP_FLAG_NONE); |
7c673cae FG |
991 | } |
992 | ASSERT_EQ(ret, 0); | |
993 | } | |
994 | } | |
995 | ||
996 | TEST(LibRGW, MARKER1_SETUP_OBJECTS) | |
997 | { | |
998 | /* "large" directory enumeration test. this one deals only with | |
999 | * file objects */ | |
1000 | if (do_marker1 && do_create) { | |
1001 | int ret; | |
1002 | ||
1003 | for (int ix = 0; ix < marker_nobjs; ++ix) { | |
1004 | std::string object_name("f_"); | |
1005 | object_name += to_string(ix); | |
1006 | obj_rec obj{object_name, nullptr, marker_fh, nullptr}; | |
1007 | // lookup object--all operations are by handle | |
1008 | ret = rgw_lookup(fs, marker_fh, obj.name.c_str(), &obj.fh, | |
eafe8130 | 1009 | nullptr, 0, RGW_LOOKUP_FLAG_CREATE); |
7c673cae FG |
1010 | ASSERT_EQ(ret, 0); |
1011 | obj.rgw_fh = get_rgwfh(obj.fh); | |
1012 | // open object--open transaction | |
1013 | ret = rgw_open(fs, obj.fh, 0 /* posix flags */, RGW_OPEN_FLAG_NONE); | |
1014 | ASSERT_EQ(ret, 0); | |
1015 | ASSERT_TRUE(obj.rgw_fh->is_open()); | |
1016 | // unstable write data | |
1017 | size_t nbytes; | |
1018 | string data("data for "); | |
1019 | data += object_name; | |
1020 | int ret = rgw_write(fs, obj.fh, 0, data.length(), &nbytes, | |
1021 | (void*) data.c_str(), RGW_WRITE_FLAG_NONE); | |
1022 | ASSERT_EQ(ret, 0); | |
1023 | ASSERT_EQ(nbytes, data.length()); | |
1024 | // commit transaction (write on close) | |
1025 | ret = rgw_close(fs, obj.fh, 0 /* flags */); | |
1026 | ASSERT_EQ(ret, 0); | |
1027 | // save for cleanup | |
1028 | marker_objs.push_back(obj); | |
1029 | } | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | extern "C" { | |
1e59de90 | 1034 | static int r2_cb(const char* name, void *arg, uint64_t offset, |
eafe8130 | 1035 | struct stat* st, uint32_t st_mask, |
7c673cae FG |
1036 | uint32_t flags) { |
1037 | dirent_vec& dvec = | |
1038 | *(static_cast<dirent_vec*>(arg)); | |
1039 | lsubdout(cct, rgw, 10) << __func__ | |
1040 | << " bucket=" << bucket_name | |
1041 | << " dir=" << marker_dir | |
1042 | << " iv count=" << dvec.count | |
1043 | << " called back name=" << name | |
1044 | << " flags=" << flags | |
1045 | << dendl; | |
1046 | string name_str{name}; | |
1047 | if (! ((name_str == ".") || | |
1048 | (name_str == ".."))) { | |
1049 | dvec.obj_names.push_back(dirent_t{std::move(name_str), offset}); | |
1050 | } | |
1051 | return true; /* XXX */ | |
1052 | } | |
1053 | } | |
1054 | ||
1055 | TEST(LibRGW, MARKER1_READDIR) | |
1056 | { | |
1057 | if (do_marker1) { | |
1058 | using std::get; | |
1059 | ||
1060 | dirent_vec dvec; | |
1061 | uint64_t offset = 0; | |
1062 | bool eof = false; | |
1063 | ||
1064 | /* because RGWReaddirRequest::default_max is 1000 (XXX make | |
1065 | * configurable?) and marker_nobjs is 5*1024, the number | |
1066 | * of required rgw_readdir operations N should be | |
1067 | * marker_nobjs/1000 < N < marker_nobjs/1000+1, i.e., 6 when | |
1068 | * marker_nobjs==5*1024 */ | |
1069 | uint32_t max_iterations = marker_nobjs/1000+1; | |
1070 | ||
1071 | do { | |
1072 | ASSERT_TRUE(dvec.count <= max_iterations); | |
1073 | int ret = rgw_readdir(fs, marker_fh, &offset, r2_cb, &dvec, &eof, | |
1074 | RGW_READDIR_FLAG_DOTDOT); | |
1075 | ASSERT_EQ(ret, 0); | |
1076 | ASSERT_EQ(offset, get<1>(dvec.obj_names.back())); // cookie check | |
1077 | ++dvec.count; | |
1078 | } while(!eof); | |
1079 | std::cout << "Read " << dvec.obj_names.size() << " objects in " | |
1080 | << marker_dir.c_str() << std::endl; | |
1081 | } | |
1082 | } | |
1083 | ||
1084 | TEST(LibRGW, MARKER1_OBJ_CLEANUP) | |
1085 | { | |
1086 | int rc; | |
1087 | for (auto& obj : marker_objs) { | |
1088 | if (obj.fh) { | |
1089 | if (do_delete) { | |
1090 | if (verbose) { | |
1091 | std::cout << "unlinking: " << bucket_name << ":" << obj.name | |
1092 | << std::endl; | |
1093 | } | |
1094 | rc = rgw_unlink(fs, marker_fh, obj.name.c_str(), RGW_UNLINK_FLAG_NONE); | |
1095 | } | |
1096 | rc = rgw_fh_rele(fs, obj.fh, 0 /* flags */); | |
1097 | ASSERT_EQ(rc, 0); | |
1098 | } | |
1099 | } | |
1100 | marker_objs.clear(); | |
1101 | } | |
1102 | ||
1103 | TEST(LibRGW, CLEANUP) { | |
1104 | int rc; | |
1105 | ||
1106 | if (do_marker1) { | |
1107 | cleanup_queue.push_back( | |
1108 | obj_rec{bucket_name, bucket_fh, fs->root_fh, get_rgwfh(fs->root_fh)}); | |
1109 | } | |
1110 | ||
1111 | for (auto& elt : cleanup_queue) { | |
1112 | if (elt.fh) { | |
1113 | rc = rgw_fh_rele(fs, elt.fh, 0 /* flags */); | |
1114 | ASSERT_EQ(rc, 0); | |
1115 | } | |
1116 | } | |
1117 | cleanup_queue.clear(); | |
1118 | } | |
1119 | ||
1120 | TEST(LibRGW, UMOUNT) { | |
1121 | if (! fs) | |
1122 | return; | |
1123 | ||
1124 | int ret = rgw_umount(fs, RGW_UMOUNT_FLAG_NONE); | |
1125 | ASSERT_EQ(ret, 0); | |
1126 | } | |
1127 | ||
1128 | TEST(LibRGW, SHUTDOWN) { | |
1129 | librgw_shutdown(rgw_h); | |
1130 | } | |
1131 | ||
1132 | int main(int argc, char *argv[]) | |
1133 | { | |
20effc67 | 1134 | auto args = argv_to_vec(argc, argv); |
7c673cae FG |
1135 | env_to_vec(args); |
1136 | ||
20effc67 | 1137 | char* v = getenv("AWS_ACCESS_KEY_ID"); |
7c673cae FG |
1138 | if (v) { |
1139 | access_key = v; | |
1140 | } | |
1141 | ||
1142 | v = getenv("AWS_SECRET_ACCESS_KEY"); | |
1143 | if (v) { | |
1144 | secret_key = v; | |
1145 | } | |
1146 | ||
20effc67 | 1147 | string val; |
7c673cae FG |
1148 | for (auto arg_iter = args.begin(); arg_iter != args.end();) { |
1149 | if (ceph_argparse_witharg(args, arg_iter, &val, "--access", | |
1150 | (char*) nullptr)) { | |
1151 | access_key = val; | |
1152 | } else if (ceph_argparse_witharg(args, arg_iter, &val, "--secret", | |
1153 | (char*) nullptr)) { | |
1154 | secret_key = val; | |
1155 | } else if (ceph_argparse_witharg(args, arg_iter, &val, "--userid", | |
1156 | (char*) nullptr)) { | |
1157 | userid = val; | |
1158 | } else if (ceph_argparse_witharg(args, arg_iter, &val, "--bn", | |
1159 | (char*) nullptr)) { | |
1160 | bucket_name = val; | |
1161 | } else if (ceph_argparse_witharg(args, arg_iter, &val, "--uid", | |
1162 | (char*) nullptr)) { | |
1163 | owner_uid = std::stoi(val); | |
1164 | } else if (ceph_argparse_witharg(args, arg_iter, &val, "--gid", | |
1165 | (char*) nullptr)) { | |
1166 | owner_gid = std::stoi(val); | |
1167 | } else if (ceph_argparse_flag(args, arg_iter, "--hier1", | |
1168 | (char*) nullptr)) { | |
1169 | do_hier1 = true; | |
1170 | } else if (ceph_argparse_flag(args, arg_iter, "--dirs1", | |
1171 | (char*) nullptr)) { | |
1172 | do_dirs1 = true; | |
1173 | } else if (ceph_argparse_flag(args, arg_iter, "--marker1", | |
1174 | (char*) nullptr)) { | |
1175 | do_marker1 = true; | |
1176 | } else if (ceph_argparse_flag(args, arg_iter, "--setattr", | |
1177 | (char*) nullptr)) { | |
1178 | do_setattr = true; | |
1179 | } else if (ceph_argparse_flag(args, arg_iter, "--create", | |
1180 | (char*) nullptr)) { | |
1181 | do_create = true; | |
1182 | } else if (ceph_argparse_flag(args, arg_iter, "--delete", | |
1183 | (char*) nullptr)) { | |
1184 | do_delete = true; | |
1185 | } else if (ceph_argparse_flag(args, arg_iter, "--rename", | |
1186 | (char*) nullptr)) { | |
1187 | do_rename = true; | |
1188 | } else if (ceph_argparse_flag(args, arg_iter, "--readf", | |
1189 | (char*) nullptr)) { | |
1190 | do_readf = true; | |
1191 | } else if (ceph_argparse_flag(args, arg_iter, "--writef", | |
1192 | (char*) nullptr)) { | |
1193 | do_writef = true; | |
1194 | } else if (ceph_argparse_flag(args, arg_iter, "--verbose", | |
1195 | (char*) nullptr)) { | |
1196 | verbose = true; | |
1197 | } else { | |
1198 | ++arg_iter; | |
1199 | } | |
1200 | } | |
1201 | ||
11fdf7f2 | 1202 | /* don't accidentally run as anonymous */ |
7c673cae FG |
1203 | if ((access_key == "") || |
1204 | (secret_key == "")) { | |
1205 | std::cout << argv[0] << " no AWS credentials, exiting" << std::endl; | |
1206 | return EPERM; | |
1207 | } | |
1208 | ||
1209 | saved_args.argc = argc; | |
1210 | saved_args.argv = argv; | |
1211 | ||
1212 | ::testing::InitGoogleTest(&argc, argv); | |
1213 | return RUN_ALL_TESTS(); | |
1214 | } |