1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
5 #include "test/librados/test.h"
6 #include "test/librados/TestCase.h"
7 #include "include/scope_guard.h"
9 using namespace librados;
11 std::string RadosTestNS::pool_name;
12 rados_t RadosTestNS::s_cluster = NULL;
17 static bool seeded = false;
21 std::cout << "seed " << seed << std::endl;
26 } // anonymous namespace
28 void RadosTestNS::SetUpTestCase()
30 pool_name = get_temp_pool_name();
31 ASSERT_EQ("", create_one_pool(pool_name, &s_cluster));
34 void RadosTestNS::TearDownTestCase()
36 ASSERT_EQ(0, destroy_one_pool(pool_name, &s_cluster));
39 void RadosTestNS::SetUp()
41 cluster = RadosTestNS::s_cluster;
42 ASSERT_EQ(0, rados_ioctx_create(cluster, pool_name.c_str(), &ioctx));
44 ASSERT_EQ(0, rados_ioctx_pool_requires_alignment2(ioctx, &requires));
45 ASSERT_FALSE(requires);
48 void RadosTestNS::TearDown()
51 cleanup_all_objects(ioctx);
52 rados_ioctx_destroy(ioctx);
55 void RadosTestNS::cleanup_all_objects(rados_ioctx_t ioctx)
57 // remove all objects to avoid polluting other tests
58 rados_ioctx_snap_set_read(ioctx, LIBRADOS_SNAP_HEAD);
59 rados_ioctx_set_namespace(ioctx, LIBRADOS_ALL_NSPACES);
60 rados_list_ctx_t list_ctx;
62 ASSERT_EQ(0, rados_nobjects_list_open(ioctx, &list_ctx));
63 auto sg = make_scope_guard([&] { rados_nobjects_list_close(list_ctx); });
66 const char *entry = NULL;
67 const char *key = NULL;
68 const char *nspace = NULL;
69 while ((r = rados_nobjects_list_next(list_ctx, &entry, &key, &nspace)) != -ENOENT) {
71 rados_ioctx_locator_set_key(ioctx, key);
72 rados_ioctx_set_namespace(ioctx, nspace);
73 ASSERT_EQ(0, rados_remove(ioctx, entry));
77 std::string RadosTestPPNS::pool_name;
78 Rados RadosTestPPNS::s_cluster;
80 void RadosTestPPNS::SetUpTestCase()
82 pool_name = get_temp_pool_name();
83 ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
86 void RadosTestPPNS::TearDownTestCase()
88 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
91 void RadosTestPPNS::SetUp()
93 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
95 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&requires));
96 ASSERT_FALSE(requires);
99 void RadosTestPPNS::TearDown()
102 cleanup_all_objects(ioctx);
106 void RadosTestPPNS::cleanup_all_objects(librados::IoCtx ioctx)
108 // remove all objects to avoid polluting other tests
109 ioctx.snap_set_read(librados::SNAP_HEAD);
110 ioctx.set_namespace(all_nspaces);
111 for (NObjectIterator it = ioctx.nobjects_begin();
112 it != ioctx.nobjects_end(); ++it) {
113 ioctx.locator_set_key(it->get_locator());
114 ioctx.set_namespace(it->get_nspace());
115 ASSERT_EQ(0, ioctx.remove(it->get_oid()));
119 std::string RadosTestParamPPNS::pool_name;
120 std::string RadosTestParamPPNS::cache_pool_name;
121 Rados RadosTestParamPPNS::s_cluster;
123 void RadosTestParamPPNS::SetUpTestCase()
125 pool_name = get_temp_pool_name();
126 ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
129 void RadosTestParamPPNS::TearDownTestCase()
131 if (cache_pool_name.length()) {
134 ASSERT_EQ(0, s_cluster.mon_command(
135 "{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
138 ASSERT_EQ(0, s_cluster.mon_command(
139 "{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
140 "\", \"tierpool\": \"" + cache_pool_name + "\"}",
142 ASSERT_EQ(0, s_cluster.mon_command(
143 "{\"prefix\": \"osd pool delete\", \"pool\": \"" + cache_pool_name +
144 "\", \"pool2\": \"" + cache_pool_name + "\", \"sure\": \"--yes-i-really-really-mean-it\"}",
146 cache_pool_name = "";
148 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
151 void RadosTestParamPPNS::SetUp()
153 if (strcmp(GetParam(), "cache") == 0 && cache_pool_name.empty()) {
154 cache_pool_name = get_temp_pool_name();
156 ASSERT_EQ(0, cluster.mon_command(
157 "{\"prefix\": \"osd pool create\", \"pool\": \"" + cache_pool_name +
158 "\", \"pg_num\": 4}",
160 ASSERT_EQ(0, cluster.mon_command(
161 "{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
162 "\", \"tierpool\": \"" + cache_pool_name +
163 "\", \"force_nonempty\": \"--force-nonempty\" }",
165 ASSERT_EQ(0, cluster.mon_command(
166 "{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
167 "\", \"overlaypool\": \"" + cache_pool_name + "\"}",
169 ASSERT_EQ(0, cluster.mon_command(
170 "{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
171 "\", \"mode\": \"writeback\"}",
173 cluster.wait_for_latest_osdmap();
176 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
178 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&requires));
179 ASSERT_FALSE(requires);
182 void RadosTestParamPPNS::TearDown()
185 cleanup_all_objects(ioctx);
189 void RadosTestParamPPNS::cleanup_all_objects(librados::IoCtx ioctx)
191 // remove all objects to avoid polluting other tests
192 ioctx.snap_set_read(librados::SNAP_HEAD);
193 ioctx.set_namespace(all_nspaces);
194 for (NObjectIterator it = ioctx.nobjects_begin();
195 it != ioctx.nobjects_end(); ++it) {
196 ioctx.locator_set_key(it->get_locator());
197 ioctx.set_namespace(it->get_nspace());
198 ASSERT_EQ(0, ioctx.remove(it->get_oid()));
202 std::string RadosTestECNS::pool_name;
203 rados_t RadosTestECNS::s_cluster = NULL;
205 void RadosTestECNS::SetUpTestCase()
207 pool_name = get_temp_pool_name();
208 ASSERT_EQ("", create_one_ec_pool(pool_name, &s_cluster));
211 void RadosTestECNS::TearDownTestCase()
213 ASSERT_EQ(0, destroy_one_ec_pool(pool_name, &s_cluster));
216 void RadosTestECNS::SetUp()
218 cluster = RadosTestECNS::s_cluster;
219 ASSERT_EQ(0, rados_ioctx_create(cluster, pool_name.c_str(), &ioctx));
221 ASSERT_EQ(0, rados_ioctx_pool_requires_alignment2(ioctx, &requires));
222 ASSERT_TRUE(requires);
223 ASSERT_EQ(0, rados_ioctx_pool_required_alignment2(ioctx, &alignment));
224 ASSERT_NE(0U, alignment);
227 void RadosTestECNS::TearDown()
230 cleanup_all_objects(ioctx);
231 rados_ioctx_destroy(ioctx);
234 std::string RadosTestECPPNS::pool_name;
235 Rados RadosTestECPPNS::s_cluster;
237 void RadosTestECPPNS::SetUpTestCase()
239 pool_name = get_temp_pool_name();
240 ASSERT_EQ("", create_one_ec_pool_pp(pool_name, s_cluster));
243 void RadosTestECPPNS::TearDownTestCase()
245 ASSERT_EQ(0, destroy_one_ec_pool_pp(pool_name, s_cluster));
248 void RadosTestECPPNS::SetUp()
250 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
252 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&requires));
253 ASSERT_TRUE(requires);
254 ASSERT_EQ(0, ioctx.pool_required_alignment2(&alignment));
255 ASSERT_NE(0U, alignment);
258 void RadosTestECPPNS::TearDown()
261 cleanup_all_objects(ioctx);
265 std::string RadosTest::pool_name;
266 rados_t RadosTest::s_cluster = NULL;
268 void RadosTest::SetUpTestCase()
270 pool_name = get_temp_pool_name();
271 ASSERT_EQ("", create_one_pool(pool_name, &s_cluster));
274 void RadosTest::TearDownTestCase()
276 ASSERT_EQ(0, destroy_one_pool(pool_name, &s_cluster));
279 void RadosTest::SetUp()
281 cluster = RadosTest::s_cluster;
282 ASSERT_EQ(0, rados_ioctx_create(cluster, pool_name.c_str(), &ioctx));
283 nspace = get_temp_pool_name();
284 rados_ioctx_set_namespace(ioctx, nspace.c_str());
286 ASSERT_EQ(0, rados_ioctx_pool_requires_alignment2(ioctx, &requires));
287 ASSERT_FALSE(requires);
290 void RadosTest::TearDown()
293 cleanup_default_namespace(ioctx);
294 cleanup_namespace(ioctx, nspace);
296 rados_ioctx_destroy(ioctx);
299 void RadosTest::cleanup_default_namespace(rados_ioctx_t ioctx)
301 // remove all objects from the default namespace to avoid polluting
303 cleanup_namespace(ioctx, "");
306 void RadosTest::cleanup_namespace(rados_ioctx_t ioctx, std::string ns)
308 rados_ioctx_snap_set_read(ioctx, LIBRADOS_SNAP_HEAD);
309 rados_ioctx_set_namespace(ioctx, ns.c_str());
310 rados_list_ctx_t list_ctx;
312 ASSERT_EQ(0, rados_nobjects_list_open(ioctx, &list_ctx));
313 auto sg = make_scope_guard([&] { rados_nobjects_list_close(list_ctx); });
316 const char *entry = NULL;
317 const char *key = NULL;
318 while ((r = rados_nobjects_list_next(list_ctx, &entry, &key, NULL)) != -ENOENT) {
320 rados_ioctx_locator_set_key(ioctx, key);
321 ASSERT_EQ(0, rados_remove(ioctx, entry));
325 std::string RadosTestPP::pool_name;
326 Rados RadosTestPP::s_cluster;
328 void RadosTestPP::SetUpTestCase()
332 pool_name = get_temp_pool_name();
333 ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
336 void RadosTestPP::TearDownTestCase()
338 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
341 void RadosTestPP::SetUp()
343 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
344 nspace = get_temp_pool_name();
345 ioctx.set_namespace(nspace);
347 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&requires));
348 ASSERT_FALSE(requires);
351 void RadosTestPP::TearDown()
354 cleanup_default_namespace(ioctx);
355 cleanup_namespace(ioctx, nspace);
360 void RadosTestPP::cleanup_default_namespace(librados::IoCtx ioctx)
362 // remove all objects from the default namespace to avoid polluting
364 cleanup_namespace(ioctx, "");
367 void RadosTestPP::cleanup_namespace(librados::IoCtx ioctx, std::string ns)
369 ioctx.snap_set_read(librados::SNAP_HEAD);
370 ioctx.set_namespace(ns);
374 for (NObjectIterator it = ioctx.nobjects_begin();
375 it != ioctx.nobjects_end(); ++it) {
376 ioctx.locator_set_key(it->get_locator());
377 ObjectWriteOperation op;
379 librados::AioCompletion *completion = s_cluster.aio_create_completion();
380 auto sg = make_scope_guard([&] { completion->release(); });
381 ASSERT_EQ(0, ioctx.aio_operate(it->get_oid(), completion, &op,
382 librados::OPERATION_IGNORE_CACHE));
383 completion->wait_for_safe();
384 if (completion->get_return_value() == -ENOENT) {
386 std::cout << " got ENOENT removing " << it->get_oid() << std::endl;
388 ASSERT_EQ(0, completion->get_return_value());
394 std::cout << " got ENOENT on " << got_enoent
395 << " objects, waiting a bit for snap"
396 << " trimming before retrying " << tries << " more times..."
401 std::cout << "failed to clean up" << std::endl;
406 std::string RadosTestParamPP::pool_name;
407 std::string RadosTestParamPP::cache_pool_name;
408 Rados RadosTestParamPP::s_cluster;
410 void RadosTestParamPP::SetUpTestCase()
412 pool_name = get_temp_pool_name();
413 ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
416 void RadosTestParamPP::TearDownTestCase()
418 if (cache_pool_name.length()) {
421 ASSERT_EQ(0, s_cluster.mon_command(
422 "{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
425 ASSERT_EQ(0, s_cluster.mon_command(
426 "{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
427 "\", \"tierpool\": \"" + cache_pool_name + "\"}",
429 ASSERT_EQ(0, s_cluster.mon_command(
430 "{\"prefix\": \"osd pool delete\", \"pool\": \"" + cache_pool_name +
431 "\", \"pool2\": \"" + cache_pool_name + "\", \"sure\": \"--yes-i-really-really-mean-it\"}",
433 cache_pool_name = "";
435 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
438 void RadosTestParamPP::SetUp()
440 if (strcmp(GetParam(), "cache") == 0 && cache_pool_name.empty()) {
441 cache_pool_name = get_temp_pool_name();
443 ASSERT_EQ(0, cluster.mon_command(
444 "{\"prefix\": \"osd pool create\", \"pool\": \"" + cache_pool_name +
445 "\", \"pg_num\": 4}",
447 ASSERT_EQ(0, cluster.mon_command(
448 "{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
449 "\", \"tierpool\": \"" + cache_pool_name +
450 "\", \"force_nonempty\": \"--force-nonempty\" }",
452 ASSERT_EQ(0, cluster.mon_command(
453 "{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
454 "\", \"overlaypool\": \"" + cache_pool_name + "\"}",
456 ASSERT_EQ(0, cluster.mon_command(
457 "{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
458 "\", \"mode\": \"writeback\"}",
460 cluster.wait_for_latest_osdmap();
463 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
464 nspace = get_temp_pool_name();
465 ioctx.set_namespace(nspace);
467 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&requires));
468 ASSERT_FALSE(requires);
471 void RadosTestParamPP::TearDown()
474 cleanup_default_namespace(ioctx);
475 cleanup_namespace(ioctx, nspace);
480 void RadosTestParamPP::cleanup_default_namespace(librados::IoCtx ioctx)
482 // remove all objects from the default namespace to avoid polluting
484 cleanup_namespace(ioctx, "");
487 void RadosTestParamPP::cleanup_namespace(librados::IoCtx ioctx, std::string ns)
489 ioctx.snap_set_read(librados::SNAP_HEAD);
490 ioctx.set_namespace(ns);
491 for (NObjectIterator it = ioctx.nobjects_begin();
492 it != ioctx.nobjects_end(); ++it) {
493 ioctx.locator_set_key(it->get_locator());
494 ASSERT_EQ(0, ioctx.remove(it->get_oid()));
498 std::string RadosTestEC::pool_name;
499 rados_t RadosTestEC::s_cluster = NULL;
501 void RadosTestEC::SetUpTestCase()
503 pool_name = get_temp_pool_name();
504 ASSERT_EQ("", create_one_ec_pool(pool_name, &s_cluster));
507 void RadosTestEC::TearDownTestCase()
509 ASSERT_EQ(0, destroy_one_ec_pool(pool_name, &s_cluster));
512 void RadosTestEC::SetUp()
514 cluster = RadosTestEC::s_cluster;
515 ASSERT_EQ(0, rados_ioctx_create(cluster, pool_name.c_str(), &ioctx));
516 nspace = get_temp_pool_name();
517 rados_ioctx_set_namespace(ioctx, nspace.c_str());
519 ASSERT_EQ(0, rados_ioctx_pool_requires_alignment2(ioctx, &requires));
520 ASSERT_TRUE(requires);
521 ASSERT_EQ(0, rados_ioctx_pool_required_alignment2(ioctx, &alignment));
522 ASSERT_NE(0U, alignment);
525 void RadosTestEC::TearDown()
528 cleanup_default_namespace(ioctx);
529 cleanup_namespace(ioctx, nspace);
531 rados_ioctx_destroy(ioctx);
534 std::string RadosTestECPP::pool_name;
535 Rados RadosTestECPP::s_cluster;
537 void RadosTestECPP::SetUpTestCase()
539 pool_name = get_temp_pool_name();
540 ASSERT_EQ("", create_one_ec_pool_pp(pool_name, s_cluster));
543 void RadosTestECPP::TearDownTestCase()
545 ASSERT_EQ(0, destroy_one_ec_pool_pp(pool_name, s_cluster));
548 void RadosTestECPP::SetUp()
550 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
551 nspace = get_temp_pool_name();
552 ioctx.set_namespace(nspace);
554 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&requires));
555 ASSERT_TRUE(requires);
556 ASSERT_EQ(0, ioctx.pool_required_alignment2(&alignment));
557 ASSERT_NE(0U, alignment);
560 void RadosTestECPP::TearDown()
563 cleanup_default_namespace(ioctx);
564 cleanup_namespace(ioctx, nspace);