summaryrefslogtreecommitdiffstats
path: root/src/test/rgw/rgw_cr_test.cc
blob: dc5d25d23aeddc024672011bb723a14b8c19b608 (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp

#include <cerrno>
#include <iostream>
#include <sstream>
#include <string>

#include <fmt/format.h>

#include "include/rados/librados.hpp"

#include "common/common_init.h"
#include "common/config.h"
#include "common/ceph_argparse.h"
#include "common/debug.h"

#include "rgw_coroutine.h"
#include "rgw_cr_rados.h"
#include "rgw_sal.h"
#include "rgw_sal_rados.h"

#include "gtest/gtest.h"

using namespace std::literals;

static constexpr auto dout_subsys = ceph_subsys_rgw;

static rgw::sal::RadosStore* store = nullptr;

static const DoutPrefixProvider* dpp() {
  struct GlobalPrefix : public DoutPrefixProvider {
    CephContext *get_cct() const override { return g_ceph_context; }
    unsigned get_subsys() const override { return dout_subsys; }
    std::ostream& gen_prefix(std::ostream& out) const override { return out; }
  };
  static GlobalPrefix global_dpp;
  return &global_dpp;
}

class StoreDestructor {
  rgw::sal::Driver* driver;
public:
  explicit StoreDestructor(rgw::sal::RadosStore* _s) : driver(_s) {}
  ~StoreDestructor() {
    DriverManager::close_storage(store);
  }
};

struct TempPool {
  inline static uint64_t num = 0;
  std::string name =
    fmt::format("{}-{}-{}", ::time(nullptr), ::getpid(),num++);

  TempPool() {
    auto r = store->getRados()->get_rados_handle()->pool_create(name.c_str());
    assert(r == 0);
  }

  ~TempPool() {
    auto r = store->getRados()->get_rados_handle()->pool_delete(name.c_str());
    assert(r == 0);
  }

  operator rgw_pool() {
    return { name };
  }

  operator librados::IoCtx() {
    librados::IoCtx ioctx;
    auto r = store->getRados()->get_rados_handle()->ioctx_create(name.c_str(),
								 ioctx);
    assert(r == 0);
    return ioctx;
  }
};

int run(RGWCoroutine* cr) {
  RGWCoroutinesManager cr_mgr{store->ctx(),
                              store->getRados()->get_cr_registry()};
  std::list<RGWCoroutinesStack *> stacks;
  auto stack = new RGWCoroutinesStack(store->ctx(), &cr_mgr);
  stack->call(cr);
  stacks.push_back(stack);
  return cr_mgr.run(dpp(), stacks);
}

TEST(ReadAttrs, Unfiltered) {
  TempPool pool;
  ceph::bufferlist bl;
  auto dummy = "Dummy attribute value"s;
  encode(dummy, bl);
  const std::map<std::string, ceph::bufferlist> ref_attrs{
    { "foo"s, bl }, { "bar"s, bl }, { "baz"s, bl }
  };
  auto oid = "object"s;
  {
    librados::IoCtx ioctx(pool);
    librados::ObjectWriteOperation op;
    op.setxattr("foo", bl);
    op.setxattr("bar", bl);
    op.setxattr("baz", bl);
    auto r = ioctx.operate(oid, &op);
    ASSERT_EQ(0, r);
  }
  std::map<std::string, ceph::bufferlist> attrs;
  auto r = run(new RGWSimpleRadosReadAttrsCR(dpp(), store, {pool, oid}, &attrs,
					     true));
  ASSERT_EQ(0, r);
  ASSERT_EQ(ref_attrs, attrs);
}

TEST(ReadAttrs, Filtered) {
  TempPool pool;
  ceph::bufferlist bl;
  auto dummy = "Dummy attribute value"s;
  encode(dummy, bl);
  const std::map<std::string, ceph::bufferlist> ref_attrs{
    { RGW_ATTR_PREFIX "foo"s, bl },
    { RGW_ATTR_PREFIX "bar"s, bl },
    { RGW_ATTR_PREFIX "baz"s, bl }
  };
  auto oid = "object"s;
  {
    librados::IoCtx ioctx(pool);
    librados::ObjectWriteOperation op;
    op.setxattr(RGW_ATTR_PREFIX "foo", bl);
    op.setxattr(RGW_ATTR_PREFIX "bar", bl);
    op.setxattr(RGW_ATTR_PREFIX "baz", bl);
    op.setxattr("oneOfTheseThingsIsNotLikeTheOthers", bl);
    auto r = ioctx.operate(oid, &op);
    ASSERT_EQ(0, r);
  }
  std::map<std::string, ceph::bufferlist> attrs;
  auto r = run(new RGWSimpleRadosReadAttrsCR(dpp(), store, {pool, oid}, &attrs,
					     false));
  ASSERT_EQ(0, r);
  ASSERT_EQ(ref_attrs, attrs);
}

TEST(Read, Dne) {
  TempPool pool;
  std::string result;
  auto r = run(new RGWSimpleRadosReadCR(dpp(), store, {pool, "doesnotexist"},
					&result, false));
  ASSERT_EQ(-ENOENT, r);
}

TEST(Read, Read) {
  TempPool pool;
  auto data = "I am test data!"sv;
  auto oid = "object"s;
  {
    bufferlist bl;
    encode(data, bl);
    librados::IoCtx ioctx(pool);
    auto r = ioctx.write_full(oid, bl);
    ASSERT_EQ(0, r);
  }
  std::string result;
  auto r = run(new RGWSimpleRadosReadCR(dpp(), store, {pool, oid}, &result,
					false));
  ASSERT_EQ(0, r);
  ASSERT_EQ(result, data);
}

TEST(Read, ReadVersion) {
  TempPool pool;
  auto data = "I am test data!"sv;
  auto oid = "object"s;
  RGWObjVersionTracker wobjv;
  {
    bufferlist bl;
    encode(data, bl);
    librados::IoCtx ioctx(pool);
    librados::ObjectWriteOperation op;
    wobjv.generate_new_write_ver(store->ctx());
    wobjv.prepare_op_for_write(&op);
    op.write_full(bl);
    auto r = ioctx.operate(oid, &op);
    EXPECT_EQ(0, r);
    wobjv.apply_write();
  }
  RGWObjVersionTracker robjv;
  std::string result;
  auto r = run(new RGWSimpleRadosReadCR(dpp(), store, {pool, oid}, &result,
					false, &robjv));
  ASSERT_EQ(0, r);
  ASSERT_EQ(result, data);
  data = "I am NEW test data!";
  {
    bufferlist bl;
    encode(data, bl);
    librados::IoCtx ioctx(pool);
    librados::ObjectWriteOperation op;
    wobjv.generate_new_write_ver(store->ctx());
    wobjv.prepare_op_for_write(&op);
    op.write_full(bl);
    r = ioctx.operate(oid, &op);
    EXPECT_EQ(0, r);
    wobjv.apply_write();
  }
  result.clear();
  r = run(new RGWSimpleRadosReadCR(dpp(), store, {pool, oid}, &result, false,
				   &robjv));
  ASSERT_EQ(-ECANCELED, r);
  ASSERT_TRUE(result.empty());

  robjv.clear();
  r = run(new RGWSimpleRadosReadCR(dpp(), store, {pool, oid}, &result, false,
				   &robjv));
  ASSERT_EQ(0, r);
  ASSERT_EQ(result, data);
  ASSERT_EQ(wobjv.read_version, robjv.read_version);
}

TEST(Write, Exclusive) {
  TempPool pool;
  auto oid = "object"s;
  {
    bufferlist bl;
    bl.append("I'm some data!"s);
    librados::IoCtx ioctx(pool);
    auto r = ioctx.write_full(oid, bl);
    ASSERT_EQ(0, r);
  }
  auto r = run(new RGWSimpleRadosWriteCR(dpp(), store, {pool, oid},
					 "I am some DIFFERENT data!"s, nullptr,
					 true));
  ASSERT_EQ(-EEXIST, r);
}

TEST(Write, Write) {
  TempPool pool;
  auto oid = "object"s;
  auto data = "I'm some data!"s;
  auto r = run(new RGWSimpleRadosWriteCR(dpp(), store, {pool, oid},
					 data, nullptr, true));
  ASSERT_EQ(0, r);
  bufferlist bl;
  librados::IoCtx ioctx(pool);
  ioctx.read(oid, bl, 0, 0);
  ASSERT_EQ(0, r);
  std::string result;
  decode(result, bl);
  ASSERT_EQ(data, result);
}

TEST(Write, ObjV) {
  TempPool pool;
  auto oid = "object"s;
  RGWObjVersionTracker objv;
  objv.generate_new_write_ver(store->ctx());
  auto r = run(new RGWSimpleRadosWriteCR(dpp(), store, {pool, oid},
					 "I'm some data!"s, &objv,
					 true));
  RGWObjVersionTracker interfering_objv(objv);
  r = run(new RGWSimpleRadosWriteCR(dpp(), store, {pool, oid},
				    "I'm some newer, better data!"s,
				    &interfering_objv, false));
  ASSERT_EQ(0, r);
  r = run(new RGWSimpleRadosWriteCR(dpp(), store, {pool, oid},
				    "I'm some treacherous, obsolete data!"s,
				    &objv, false));
  ASSERT_EQ(-ECANCELED, r);
}

TEST(WriteAttrs, Attrs) {
  TempPool pool;
  auto oid = "object"s;
  bufferlist bl;
  bl.append("I'm some data.");
  std::map<std::string, bufferlist> wrattrs {
    { "foo", bl }, { "bar", bl }, { "baz", bl }
  };
  auto r = run(new RGWSimpleRadosWriteAttrsCR(dpp(), store, {pool, oid},
					      wrattrs, nullptr, true));
  ASSERT_EQ(0, r);
  std::map<std::string, bufferlist> rdattrs;
  librados::IoCtx ioctx(pool);
  r = ioctx.getxattrs(oid, rdattrs);
  ASSERT_EQ(0, r);
  ASSERT_EQ(wrattrs, rdattrs);
}

TEST(WriteAttrs, Empty) {
  TempPool pool;
  auto oid = "object"s;
  bufferlist bl;
  std::map<std::string, bufferlist> wrattrs {
    { "foo", bl }, { "bar", bl }, { "baz", bl }
  };
  // With an empty bufferlist all attributes should be skipped.
  auto r = run(new RGWSimpleRadosWriteAttrsCR(dpp(), store, {pool, oid},
					      wrattrs, nullptr, true));
  ASSERT_EQ(0, r);
  std::map<std::string, bufferlist> rdattrs;
  librados::IoCtx ioctx(pool);
  r = ioctx.getxattrs(oid, rdattrs);
  ASSERT_EQ(0, r);
  ASSERT_TRUE(rdattrs.empty());
}

int main(int argc, const char **argv)
{
  auto args = argv_to_vec(argc, argv);
  auto cct = rgw_global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
			     CODE_ENVIRONMENT_UTILITY, 0);

  // for region -> zonegroup conversion (must happen before common_init_finish())
  if (!g_conf()->rgw_region.empty() && g_conf()->rgw_zonegroup.empty()) {
    g_conf().set_val_or_die("rgw_zonegroup", g_conf()->rgw_region.c_str());
  }

  /* common_init_finish needs to be called after g_conf().set_val() */
  common_init_finish(g_ceph_context);


  DriverManager::Config cfg = DriverManager::get_config(true, g_ceph_context);

  store = static_cast<rgw::sal::RadosStore*>(
    DriverManager::get_storage(dpp(),
			      g_ceph_context,
			      cfg,
			      false,
			      false,
			      false,
			      false,
			      false,
			      true,
			      false));
  if (!store) {
    std::cerr << "couldn't init storage provider" << std::endl;
    return 5; //EIO
  }
  StoreDestructor store_destructor(static_cast<rgw::sal::RadosStore*>(store));

  std::string pool{"rgw_cr_test"};
  store->getRados()->create_pool(dpp(), pool);

  testing::InitGoogleTest();
  return RUN_ALL_TESTS();
}