Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / librados / TestCase.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <errno.h>
5 #include "test/librados/test.h"
6 #include "test/librados/TestCase.h"
7 #include "include/scope_guard.h"
8
9 using namespace librados;
10
11 std::string RadosTestNS::pool_name;
12 rados_t RadosTestNS::s_cluster = NULL;
13
14 namespace {
15
16 void init_rand() {
17   static bool seeded = false;
18   if (!seeded) {
19     seeded = true;
20     int seed = getpid();
21     std::cout << "seed " << seed << std::endl;
22     srand(seed);
23   }
24 }
25
26 } // anonymous namespace
27
28 void RadosTestNS::SetUpTestCase()
29 {
30   pool_name = get_temp_pool_name();
31   ASSERT_EQ("", create_one_pool(pool_name, &s_cluster));
32 }
33
34 void RadosTestNS::TearDownTestCase()
35 {
36   ASSERT_EQ(0, destroy_one_pool(pool_name, &s_cluster));
37 }
38
39 void RadosTestNS::SetUp()
40 {
41   cluster = RadosTestNS::s_cluster;
42   ASSERT_EQ(0, rados_ioctx_create(cluster, pool_name.c_str(), &ioctx));
43   int requires;
44   ASSERT_EQ(0, rados_ioctx_pool_requires_alignment2(ioctx, &requires));
45   ASSERT_FALSE(requires);
46 }
47
48 void RadosTestNS::TearDown()
49 {
50   if (cleanup)
51     cleanup_all_objects(ioctx);
52   rados_ioctx_destroy(ioctx);
53 }
54
55 void RadosTestNS::cleanup_all_objects(rados_ioctx_t ioctx)
56 {
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;
61
62   ASSERT_EQ(0, rados_nobjects_list_open(ioctx, &list_ctx));
63   auto sg = make_scope_guard([&] { rados_nobjects_list_close(list_ctx); });
64
65   int r;
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) {
70     ASSERT_EQ(0, r);
71     rados_ioctx_locator_set_key(ioctx, key);
72     rados_ioctx_set_namespace(ioctx, nspace);
73     ASSERT_EQ(0, rados_remove(ioctx, entry));
74   }
75 }
76
77 std::string RadosTestPPNS::pool_name;
78 Rados RadosTestPPNS::s_cluster;
79
80 void RadosTestPPNS::SetUpTestCase()
81 {
82   pool_name = get_temp_pool_name();
83   ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
84 }
85
86 void RadosTestPPNS::TearDownTestCase()
87 {
88   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
89 }
90
91 void RadosTestPPNS::SetUp()
92 {
93   ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
94   bool requires;
95   ASSERT_EQ(0, ioctx.pool_requires_alignment2(&requires));
96   ASSERT_FALSE(requires);
97 }
98
99 void RadosTestPPNS::TearDown()
100 {
101   if (cleanup)
102     cleanup_all_objects(ioctx);
103   ioctx.close();
104 }
105
106 void RadosTestPPNS::cleanup_all_objects(librados::IoCtx ioctx)
107 {
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()));
116   }
117 }
118
119 std::string RadosTestParamPPNS::pool_name;
120 std::string RadosTestParamPPNS::cache_pool_name;
121 Rados RadosTestParamPPNS::s_cluster;
122
123 void RadosTestParamPPNS::SetUpTestCase()
124 {
125   pool_name = get_temp_pool_name();
126   ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
127 }
128
129 void RadosTestParamPPNS::TearDownTestCase()
130 {
131   if (cache_pool_name.length()) {
132     // tear down tiers
133     bufferlist inbl;
134     ASSERT_EQ(0, s_cluster.mon_command(
135       "{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
136       "\"}",
137       inbl, NULL, NULL));
138     ASSERT_EQ(0, s_cluster.mon_command(
139       "{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
140       "\", \"tierpool\": \"" + cache_pool_name + "\"}",
141       inbl, NULL, NULL));
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\"}",
145       inbl, NULL, NULL));
146     cache_pool_name = "";
147   }
148   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
149 }
150
151 void RadosTestParamPPNS::SetUp()
152 {
153   if (strcmp(GetParam(), "cache") == 0 && cache_pool_name.empty()) {
154     cache_pool_name = get_temp_pool_name();
155     bufferlist inbl;
156     ASSERT_EQ(0, cluster.mon_command(
157       "{\"prefix\": \"osd pool create\", \"pool\": \"" + cache_pool_name +
158       "\", \"pg_num\": 4}",
159       inbl, NULL, NULL));
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\" }",
164       inbl, NULL, NULL));
165     ASSERT_EQ(0, cluster.mon_command(
166       "{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
167       "\", \"overlaypool\": \"" + cache_pool_name + "\"}",
168       inbl, NULL, NULL));
169     ASSERT_EQ(0, cluster.mon_command(
170       "{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
171       "\", \"mode\": \"writeback\"}",
172       inbl, NULL, NULL));
173     cluster.wait_for_latest_osdmap();
174   }
175
176   ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
177   bool requires;
178   ASSERT_EQ(0, ioctx.pool_requires_alignment2(&requires));
179   ASSERT_FALSE(requires);
180 }
181
182 void RadosTestParamPPNS::TearDown()
183 {
184   if (cleanup)
185     cleanup_all_objects(ioctx);
186   ioctx.close();
187 }
188
189 void RadosTestParamPPNS::cleanup_all_objects(librados::IoCtx ioctx)
190 {
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()));
199   }
200 }
201
202 std::string RadosTestECNS::pool_name;
203 rados_t RadosTestECNS::s_cluster = NULL;
204
205 void RadosTestECNS::SetUpTestCase()
206 {
207   pool_name = get_temp_pool_name();
208   ASSERT_EQ("", create_one_ec_pool(pool_name, &s_cluster));
209 }
210
211 void RadosTestECNS::TearDownTestCase()
212 {
213   ASSERT_EQ(0, destroy_one_ec_pool(pool_name, &s_cluster));
214 }
215
216 void RadosTestECNS::SetUp()
217 {
218   cluster = RadosTestECNS::s_cluster;
219   ASSERT_EQ(0, rados_ioctx_create(cluster, pool_name.c_str(), &ioctx));
220   int requires;
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);
225 }
226
227 void RadosTestECNS::TearDown()
228 {
229   if (cleanup)
230     cleanup_all_objects(ioctx);
231   rados_ioctx_destroy(ioctx);
232 }
233
234 std::string RadosTestECPPNS::pool_name;
235 Rados RadosTestECPPNS::s_cluster;
236
237 void RadosTestECPPNS::SetUpTestCase()
238 {
239   pool_name = get_temp_pool_name();
240   ASSERT_EQ("", create_one_ec_pool_pp(pool_name, s_cluster));
241 }
242
243 void RadosTestECPPNS::TearDownTestCase()
244 {
245   ASSERT_EQ(0, destroy_one_ec_pool_pp(pool_name, s_cluster));
246 }
247
248 void RadosTestECPPNS::SetUp()
249 {
250   ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
251   bool requires;
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);
256 }
257
258 void RadosTestECPPNS::TearDown()
259 {
260   if (cleanup)
261     cleanup_all_objects(ioctx);
262   ioctx.close();
263 }
264
265 std::string RadosTest::pool_name;
266 rados_t RadosTest::s_cluster = NULL;
267
268 void RadosTest::SetUpTestCase()
269 {
270   pool_name = get_temp_pool_name();
271   ASSERT_EQ("", create_one_pool(pool_name, &s_cluster));
272 }
273
274 void RadosTest::TearDownTestCase()
275 {
276   ASSERT_EQ(0, destroy_one_pool(pool_name, &s_cluster));
277 }
278
279 void RadosTest::SetUp()
280 {
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());
285   int requires;
286   ASSERT_EQ(0, rados_ioctx_pool_requires_alignment2(ioctx, &requires));
287   ASSERT_FALSE(requires);
288 }
289
290 void RadosTest::TearDown()
291 {
292   if (cleanup) {
293     cleanup_default_namespace(ioctx);
294     cleanup_namespace(ioctx, nspace);
295   }
296   rados_ioctx_destroy(ioctx);
297 }
298
299 void RadosTest::cleanup_default_namespace(rados_ioctx_t ioctx)
300 {
301   // remove all objects from the default namespace to avoid polluting
302   // other tests
303   cleanup_namespace(ioctx, "");
304 }
305
306 void RadosTest::cleanup_namespace(rados_ioctx_t ioctx, std::string ns)
307 {
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;
311
312   ASSERT_EQ(0, rados_nobjects_list_open(ioctx, &list_ctx));
313   auto sg = make_scope_guard([&] { rados_nobjects_list_close(list_ctx); });
314
315   int r;
316   const char *entry = NULL;
317   const char *key = NULL;
318   while ((r = rados_nobjects_list_next(list_ctx, &entry, &key, NULL)) != -ENOENT) {
319     ASSERT_EQ(0, r);
320     rados_ioctx_locator_set_key(ioctx, key);
321     ASSERT_EQ(0, rados_remove(ioctx, entry));
322   }
323 }
324
325 std::string RadosTestPP::pool_name;
326 Rados RadosTestPP::s_cluster;
327
328 void RadosTestPP::SetUpTestCase()
329 {
330   init_rand();
331
332   pool_name = get_temp_pool_name();
333   ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
334 }
335
336 void RadosTestPP::TearDownTestCase()
337 {
338   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
339 }
340
341 void RadosTestPP::SetUp()
342 {
343   ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
344   nspace = get_temp_pool_name();
345   ioctx.set_namespace(nspace);
346   bool requires;
347   ASSERT_EQ(0, ioctx.pool_requires_alignment2(&requires));
348   ASSERT_FALSE(requires);
349 }
350
351 void RadosTestPP::TearDown()
352 {
353   if (cleanup) {
354     cleanup_default_namespace(ioctx);
355     cleanup_namespace(ioctx, nspace);
356   }
357   ioctx.close();
358 }
359
360 void RadosTestPP::cleanup_default_namespace(librados::IoCtx ioctx)
361 {
362   // remove all objects from the default namespace to avoid polluting
363   // other tests
364   cleanup_namespace(ioctx, "");
365 }
366
367 void RadosTestPP::cleanup_namespace(librados::IoCtx ioctx, std::string ns)
368 {
369   ioctx.snap_set_read(librados::SNAP_HEAD);
370   ioctx.set_namespace(ns);
371   int tries = 600;
372   while (--tries) {
373     int got_enoent = 0;
374     for (NObjectIterator it = ioctx.nobjects_begin();
375          it != ioctx.nobjects_end(); ++it) {
376       ioctx.locator_set_key(it->get_locator());
377       ObjectWriteOperation op;
378       op.remove();
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) {
385         ++got_enoent;
386         std::cout << " got ENOENT removing " << it->get_oid() << std::endl;
387       } else {
388         ASSERT_EQ(0, completion->get_return_value());
389       }
390     }
391     if (!got_enoent) {
392       break;
393     }
394     std::cout << " got ENOENT on " << got_enoent
395               << " objects, waiting a bit for snap"
396               << " trimming before retrying " << tries << " more times..."
397               << std::endl;
398     sleep(1);
399   }
400   if (tries == 0) {
401     std::cout << "failed to clean up" << std::endl;
402     ASSERT_TRUE(false);
403   }
404 }
405
406 std::string RadosTestParamPP::pool_name;
407 std::string RadosTestParamPP::cache_pool_name;
408 Rados RadosTestParamPP::s_cluster;
409
410 void RadosTestParamPP::SetUpTestCase()
411 {
412   pool_name = get_temp_pool_name();
413   ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
414 }
415
416 void RadosTestParamPP::TearDownTestCase()
417 {
418   if (cache_pool_name.length()) {
419     // tear down tiers
420     bufferlist inbl;
421     ASSERT_EQ(0, s_cluster.mon_command(
422       "{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
423       "\"}",
424       inbl, NULL, NULL));
425     ASSERT_EQ(0, s_cluster.mon_command(
426       "{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
427       "\", \"tierpool\": \"" + cache_pool_name + "\"}",
428       inbl, NULL, NULL));
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\"}",
432       inbl, NULL, NULL));
433     cache_pool_name = "";
434   }
435   ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
436 }
437
438 void RadosTestParamPP::SetUp()
439 {
440   if (strcmp(GetParam(), "cache") == 0 && cache_pool_name.empty()) {
441     cache_pool_name = get_temp_pool_name();
442     bufferlist inbl;
443     ASSERT_EQ(0, cluster.mon_command(
444       "{\"prefix\": \"osd pool create\", \"pool\": \"" + cache_pool_name +
445       "\", \"pg_num\": 4}",
446       inbl, NULL, NULL));
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\" }",
451       inbl, NULL, NULL));
452     ASSERT_EQ(0, cluster.mon_command(
453       "{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
454       "\", \"overlaypool\": \"" + cache_pool_name + "\"}",
455       inbl, NULL, NULL));
456     ASSERT_EQ(0, cluster.mon_command(
457       "{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
458       "\", \"mode\": \"writeback\"}",
459       inbl, NULL, NULL));
460     cluster.wait_for_latest_osdmap();
461   }
462
463   ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
464   nspace = get_temp_pool_name();
465   ioctx.set_namespace(nspace);
466   bool requires;
467   ASSERT_EQ(0, ioctx.pool_requires_alignment2(&requires));
468   ASSERT_FALSE(requires);
469 }
470
471 void RadosTestParamPP::TearDown()
472 {
473   if (cleanup) {
474     cleanup_default_namespace(ioctx);
475     cleanup_namespace(ioctx, nspace);
476   }
477   ioctx.close();
478 }
479
480 void RadosTestParamPP::cleanup_default_namespace(librados::IoCtx ioctx)
481 {
482   // remove all objects from the default namespace to avoid polluting
483   // other tests
484   cleanup_namespace(ioctx, "");
485 }
486
487 void RadosTestParamPP::cleanup_namespace(librados::IoCtx ioctx, std::string ns)
488 {
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()));
495   }
496 }
497
498 std::string RadosTestEC::pool_name;
499 rados_t RadosTestEC::s_cluster = NULL;
500
501 void RadosTestEC::SetUpTestCase()
502 {
503   pool_name = get_temp_pool_name();
504   ASSERT_EQ("", create_one_ec_pool(pool_name, &s_cluster));
505 }
506
507 void RadosTestEC::TearDownTestCase()
508 {
509   ASSERT_EQ(0, destroy_one_ec_pool(pool_name, &s_cluster));
510 }
511
512 void RadosTestEC::SetUp()
513 {
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());
518   int requires;
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);
523 }
524
525 void RadosTestEC::TearDown()
526 {
527   if (cleanup) {
528     cleanup_default_namespace(ioctx);
529     cleanup_namespace(ioctx, nspace);
530   }
531   rados_ioctx_destroy(ioctx);
532 }
533
534 std::string RadosTestECPP::pool_name;
535 Rados RadosTestECPP::s_cluster;
536
537 void RadosTestECPP::SetUpTestCase()
538 {
539   pool_name = get_temp_pool_name();
540   ASSERT_EQ("", create_one_ec_pool_pp(pool_name, s_cluster));
541 }
542
543 void RadosTestECPP::TearDownTestCase()
544 {
545   ASSERT_EQ(0, destroy_one_ec_pool_pp(pool_name, s_cluster));
546 }
547
548 void RadosTestECPP::SetUp()
549 {
550   ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
551   nspace = get_temp_pool_name();
552   ioctx.set_namespace(nspace);
553   bool requires;
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);
558 }
559
560 void RadosTestECPP::TearDown()
561 {
562   if (cleanup) {
563     cleanup_default_namespace(ioctx);
564     cleanup_namespace(ioctx, nspace);
565   }
566   ioctx.close();
567 }
568