1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2015 Red Hat, Inc.
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.
23 #include "include/rados/librgw.h"
24 #include "include/rados/rgw_file.h"
26 #include "gtest/gtest.h"
27 #include "common/ceph_argparse.h"
28 #include "common/debug.h"
29 #include "global/global_init.h"
31 #define dout_context g_ceph_context
32 #define dout_subsys ceph_subsys_rgw
35 librgw_t rgw = nullptr;
36 string userid("testuser");
37 string access_key("");
38 string secret_key("");
39 struct rgw_fs *fs = nullptr;
41 uint32_t owner_uid = 867;
42 uint32_t owner_gid = 5309;
43 uint32_t create_mask = RGW_SETATTR_UID | RGW_SETATTR_GID | RGW_SETATTR_MODE;
45 bool do_create = false;
46 bool do_delete = false;
47 bool do_large = false;
48 bool do_verify = false;
49 bool do_hexdump = false;
51 string bucket_name = "sorry_dave";
52 string object_name = "jocaml";
54 struct rgw_file_handle *bucket_fh = nullptr;
55 struct rgw_file_handle *object_fh = nullptr;
57 typedef std::tuple<string,uint64_t, struct rgw_file_handle*> fid_type;
58 std::vector<fid_type> fids;
60 std::uniform_int_distribution<uint8_t> uint_dist;
63 constexpr int iovcnt = 4;
64 constexpr int page_size = /* 65536 */ 4 * 1024*1024;
65 constexpr int seed = 8675309;
75 std::vector<ZPage*> pages;
80 iovs = (struct iovec*) calloc(n, sizeof(struct iovec));
81 for (int page_ix = 0; page_ix < n; ++page_ix) {
82 ZPage* p = new ZPage();
83 for (int data_ix = 0; data_ix < page_size; ++data_ix) {
84 p->data[data_ix] = uint_dist(rng);
86 p->cksum = XXH64(p->data, page_size, seed);
87 pages.emplace_back(p);
89 struct iovec* iov = &iovs[page_ix];
90 iov->iov_base = p->data;
91 iov->iov_len = page_size;
95 int size() { return pages.size(); }
97 struct iovec* get_iovs() { return iovs; }
99 bool operator==(const ZPageSet& rhs) {
101 for (int page_ix = 0; page_ix < n; ++page_ix) {
102 ZPage* p1 = pages[page_ix];
103 ZPage* p2 = rhs.pages[page_ix];
104 if (p1->cksum != p2->cksum)
110 bool operator==(const rgw_uio* uio) {
112 int vix = 0, off = 0;
113 rgw_vio* vio = &uio->uio_vio[vix];
114 int vio_len = vio->vio_len;
117 for (int ix = 0; ix < iovcnt; ++ix) {
118 ZPage* p1 = pages[ix];
119 data = static_cast<char*>(vio->vio_base) + off;
120 cksum = XXH64(data, page_size, seed);
122 if (p1->cksum != cksum) {
123 int r = memcmp(data, p1->data, page_size);
124 std::cout << "problem at ix " << ix << " r " << r<< std::endl;
129 if (off >= vio_len) {
130 vio = &uio->uio_vio[++vix];
131 vio_len = vio->vio_len;
140 for (int page_ix = 0; page_ix < n; ++page_ix) {
141 ZPage* p = pages[page_ix];
142 p->cksum = XXH64(p->data, page_size, seed);
146 void reset_iovs() { // VOP_READ and VOP_WRITE update
148 for (int page_ix = 0; page_ix < n; ++page_ix) {
149 ZPage* p = pages[page_ix];
150 struct iovec* iov = &iovs[page_ix];
151 iov->iov_base = p->data;
152 iov->iov_len = page_size;
157 for (unsigned int ix = 0; ix < pages.size(); ++ix)
163 ZPageSet zp_set1{iovcnt}; // random data
164 ZPageSet zp_set2{iovcnt}; // random data in 64K pages
173 int ret = librgw_create(&rgw, saved_args.argc, saved_args.argv);
175 ASSERT_NE(rgw, nullptr);
178 TEST(LibRGW, MOUNT) {
179 int ret = rgw_mount2(rgw, userid.c_str(), access_key.c_str(),
180 secret_key.c_str(), "/", &fs, RGW_MOUNT_FLAG_NONE);
182 ASSERT_NE(fs, nullptr);
185 TEST(LibRGW, CREATE_BUCKET) {
188 struct rgw_file_handle *fh;
190 st.st_uid = owner_uid;
191 st.st_gid = owner_gid;
194 int ret = rgw_mkdir(fs, fs->root_fh, bucket_name.c_str(), &st, create_mask,
195 &fh, RGW_MKDIR_FLAG_NONE);
200 TEST(LibRGW, LOOKUP_BUCKET) {
201 int ret = rgw_lookup(fs, fs->root_fh, bucket_name.c_str(), &bucket_fh,
202 RGW_LOOKUP_FLAG_NONE);
206 TEST(LibRGW, LOOKUP_OBJECT) {
207 int ret = rgw_lookup(fs, bucket_fh, object_name.c_str(), &object_fh,
208 RGW_LOOKUP_FLAG_CREATE);
213 TEST(LibRGW, OPEN1) {
214 int ret = rgw_open(fs, object_fh, 0 /* posix flags */, RGW_OPEN_FLAG_NONE);
218 TEST(LibRGW, PUT_OBJECT) {
220 struct iovec *iovs = zp_set1.get_iovs();
222 for (int ix : {0, 1}) {
223 struct iovec *iov = &iovs[ix];
225 sprintf(static_cast<char*>(iov->iov_base), "::hi mom (%d)", ix);
227 int ret = rgw_write(fs, object_fh, offset, iov->iov_len, &nbytes,
228 iov->iov_base, RGW_WRITE_FLAG_NONE);
229 offset += iov->iov_len;
231 ASSERT_EQ(nbytes, iov->iov_len);
235 TEST(LibRGW, CLOSE1) {
236 int ret = rgw_close(fs, object_fh, RGW_CLOSE_FLAG_NONE);
240 TEST(LibRGW, OPEN2) {
241 int ret = rgw_open(fs, object_fh, 0 /* posix flags */, RGW_OPEN_FLAG_NONE);
245 TEST(LibRGW, GET_OBJECT) {
248 struct iovec *iovs = zp_set1.get_iovs();
249 for (int ix : {2 , 3}) {
250 struct iovec *iov = &iovs[ix];
251 int ret = rgw_read(fs, object_fh, offset, iovs[ix-2].iov_len, &nread,
252 iov->iov_base, RGW_READ_FLAG_NONE);
253 iov->iov_len = nread;
254 offset += iov->iov_len;
256 ASSERT_EQ(nread, iovs[ix-2].iov_len);
257 std::cout << "read: " << static_cast<char*>(iov->iov_base) << std::endl;
261 TEST(LibRGW, CLOSE2) {
262 int ret = rgw_close(fs, object_fh, RGW_CLOSE_FLAG_NONE);
267 TEST (LibRGW, LARGE1) {
271 ret = rgw_open(fs, object_fh, 0 /* posix flags */, RGW_OPEN_FLAG_NONE);
275 struct iovec *iovs = zp_set1.get_iovs();
278 for (int ix = 0; ix < iovcnt; ++ix) {
279 struct iovec *iov = &iovs[ix];
280 // write iov->iov_len
281 ret = rgw_write(fs, object_fh, offset, iov->iov_len, &nbytes,
282 iov->iov_base, RGW_WRITE_FLAG_NONE);
283 offset += iov->iov_len;
285 ASSERT_EQ(nbytes, iov->iov_len);
288 ret = rgw_close(fs, object_fh, RGW_CLOSE_FLAG_NONE);
293 TEST (LibRGW, LARGE2) {
297 ret = rgw_open(fs, object_fh, 0 /* posix flags */, RGW_OPEN_FLAG_NONE);
302 struct iovec *iovs2 = zp_set2.get_iovs();
303 for (int ix = 0; ix < iovcnt; ++ix) {
304 struct iovec *iov2 = &iovs2[ix];
305 ret = rgw_read(fs, object_fh, offset2, iov2->iov_len, &nread,
306 iov2->iov_base, RGW_READ_FLAG_NONE);
307 iov2->iov_len = nread;
308 offset2 += iov2->iov_len;
313 ASSERT_TRUE(zp_set1 == zp_set2);
315 ret = rgw_close(fs, object_fh, RGW_CLOSE_FLAG_NONE);
321 TEST(LibRGW, STAT_OBJECT) {
323 int ret = rgw_getattr(fs, object_fh, &st, RGW_GETATTR_FLAG_NONE);
325 dout(15) << "rgw_getattr on " << object_name << " size = "
326 << st.st_size << dendl;
329 TEST(LibRGW, DELETE_OBJECT) {
331 int ret = rgw_unlink(fs, bucket_fh, object_name.c_str(),
332 RGW_UNLINK_FLAG_NONE);
337 TEST(LibRGW, CLEANUP) {
340 ret = rgw_fh_rele(fs, object_fh, RGW_FH_RELE_FLAG_NONE);
343 ret = rgw_fh_rele(fs, bucket_fh, 0 /* flags */);
347 TEST(LibRGW, UMOUNT) {
351 int ret = rgw_umount(fs, RGW_UMOUNT_FLAG_NONE);
355 TEST(LibRGW, SHUTDOWN) {
356 librgw_shutdown(rgw);
359 int main(int argc, char *argv[])
363 vector<const char*> args;
365 argv_to_vec(argc, const_cast<const char**>(argv), args);
368 v = getenv("AWS_ACCESS_KEY_ID");
373 v = getenv("AWS_SECRET_ACCESS_KEY");
378 for (auto arg_iter = args.begin(); arg_iter != args.end();) {
379 if (ceph_argparse_witharg(args, arg_iter, &val, "--access",
382 } else if (ceph_argparse_witharg(args, arg_iter, &val, "--secret",
385 } else if (ceph_argparse_witharg(args, arg_iter, &val, "--userid",
388 } else if (ceph_argparse_witharg(args, arg_iter, &val, "--bn",
391 } else if (ceph_argparse_witharg(args, arg_iter, &val, "--uid",
393 owner_uid = std::stoi(val);
394 } else if (ceph_argparse_witharg(args, arg_iter, &val, "--gid",
396 owner_gid = std::stoi(val);
397 } else if (ceph_argparse_flag(args, arg_iter, "--verify",
400 } else if (ceph_argparse_flag(args, arg_iter, "--create",
403 } else if (ceph_argparse_flag(args, arg_iter, "--delete",
406 } else if (ceph_argparse_flag(args, arg_iter, "--large",
409 } else if (ceph_argparse_flag(args, arg_iter, "--hexdump",
417 /* dont accidentally run as anonymous */
418 if ((access_key == "") ||
419 (secret_key == "")) {
420 std::cout << argv[0] << " no AWS credentials, exiting" << std::endl;
424 saved_args.argc = argc;
425 saved_args.argv = argv;
427 ::testing::InitGoogleTest(&argc, argv);
428 return RUN_ALL_TESTS();