]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/librados/testcase_cxx.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / test / librados / testcase_cxx.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 "testcase_cxx.h"
5
6 #include <errno.h>
7 #include <fmt/format.h>
8 #include "test_cxx.h"
9 #include "test_shared.h"
10 #include "crimson_utils.h"
11 #include "include/scope_guard.h"
12
13 using namespace librados;
14
15 namespace {
16
17 void init_rand() {
18 static bool seeded = false;
19 if (!seeded) {
20 seeded = true;
21 int seed = getpid();
22 std::cout << "seed " << seed << std::endl;
23 srand(seed);
24 }
25 }
26
27 } // anonymous namespace
28
29 std::string RadosTestPPNS::pool_name;
30 Rados RadosTestPPNS::s_cluster;
31
32 void RadosTestPPNS::SetUpTestCase()
33 {
34 auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
35 pool_name = get_temp_pool_name(pool_prefix);
36 ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
37 }
38
39 void RadosTestPPNS::TearDownTestCase()
40 {
41 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
42 }
43
44 void RadosTestPPNS::SetUp()
45 {
46 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
47 bool req;
48 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
49 ASSERT_FALSE(req);
50 }
51
52 void RadosTestPPNS::TearDown()
53 {
54 if (cleanup)
55 cleanup_all_objects(ioctx);
56 ioctx.close();
57 }
58
59 void RadosTestPPNS::cleanup_all_objects(librados::IoCtx ioctx)
60 {
61 // remove all objects to avoid polluting other tests
62 ioctx.snap_set_read(librados::SNAP_HEAD);
63 ioctx.set_namespace(all_nspaces);
64 for (NObjectIterator it = ioctx.nobjects_begin();
65 it != ioctx.nobjects_end(); ++it) {
66 ioctx.locator_set_key(it->get_locator());
67 ioctx.set_namespace(it->get_nspace());
68 ASSERT_EQ(0, ioctx.remove(it->get_oid()));
69 }
70 }
71
72 std::string RadosTestParamPPNS::pool_name;
73 std::string RadosTestParamPPNS::cache_pool_name;
74 Rados RadosTestParamPPNS::s_cluster;
75
76 void RadosTestParamPPNS::SetUpTestCase()
77 {
78 auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
79 pool_name = get_temp_pool_name(pool_prefix);
80 ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
81 }
82
83 void RadosTestParamPPNS::TearDownTestCase()
84 {
85 if (cache_pool_name.length()) {
86 // tear down tiers
87 bufferlist inbl;
88 ASSERT_EQ(0, s_cluster.mon_command(
89 "{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
90 "\"}",
91 inbl, NULL, NULL));
92 ASSERT_EQ(0, s_cluster.mon_command(
93 "{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
94 "\", \"tierpool\": \"" + cache_pool_name + "\"}",
95 inbl, NULL, NULL));
96 ASSERT_EQ(0, s_cluster.mon_command(
97 "{\"prefix\": \"osd pool delete\", \"pool\": \"" + cache_pool_name +
98 "\", \"pool2\": \"" + cache_pool_name + "\", \"yes_i_really_really_mean_it\": true}",
99 inbl, NULL, NULL));
100 cache_pool_name = "";
101 }
102 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
103 }
104
105 void RadosTestParamPPNS::SetUp()
106 {
107 if (!is_crimson_cluster() && strcmp(GetParam(), "cache") == 0 &&
108 cache_pool_name.empty()) {
109 auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
110 cache_pool_name = get_temp_pool_name();
111 bufferlist inbl;
112 ASSERT_EQ(0, cluster.mon_command(
113 "{\"prefix\": \"osd pool create\", \"pool\": \"" + cache_pool_name +
114 "\", \"pg_num\": 4}",
115 inbl, NULL, NULL));
116 ASSERT_EQ(0, cluster.mon_command(
117 "{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
118 "\", \"tierpool\": \"" + cache_pool_name +
119 "\", \"force_nonempty\": \"--force-nonempty\" }",
120 inbl, NULL, NULL));
121 ASSERT_EQ(0, cluster.mon_command(
122 "{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
123 "\", \"overlaypool\": \"" + cache_pool_name + "\"}",
124 inbl, NULL, NULL));
125 ASSERT_EQ(0, cluster.mon_command(
126 "{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
127 "\", \"mode\": \"writeback\"}",
128 inbl, NULL, NULL));
129 cluster.wait_for_latest_osdmap();
130 }
131
132 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
133 bool req;
134 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
135 ASSERT_FALSE(req);
136 }
137
138 void RadosTestParamPPNS::TearDown()
139 {
140 if (cleanup)
141 cleanup_all_objects(ioctx);
142 ioctx.close();
143 }
144
145 void RadosTestParamPPNS::cleanup_all_objects(librados::IoCtx ioctx)
146 {
147 // remove all objects to avoid polluting other tests
148 ioctx.snap_set_read(librados::SNAP_HEAD);
149 ioctx.set_namespace(all_nspaces);
150 for (NObjectIterator it = ioctx.nobjects_begin();
151 it != ioctx.nobjects_end(); ++it) {
152 ioctx.locator_set_key(it->get_locator());
153 ioctx.set_namespace(it->get_nspace());
154 ASSERT_EQ(0, ioctx.remove(it->get_oid()));
155 }
156 }
157
158 std::string RadosTestECPPNS::pool_name;
159 Rados RadosTestECPPNS::s_cluster;
160
161 void RadosTestECPPNS::SetUpTestCase()
162 {
163 auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
164 pool_name = get_temp_pool_name(pool_prefix);
165 ASSERT_EQ("", create_one_ec_pool_pp(pool_name, s_cluster));
166 }
167
168 void RadosTestECPPNS::TearDownTestCase()
169 {
170 ASSERT_EQ(0, destroy_one_ec_pool_pp(pool_name, s_cluster));
171 }
172
173 void RadosTestECPPNS::SetUp()
174 {
175 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
176 bool req;
177 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
178 ASSERT_TRUE(req);
179 ASSERT_EQ(0, ioctx.pool_required_alignment2(&alignment));
180 ASSERT_NE(0U, alignment);
181 }
182
183 void RadosTestECPPNS::TearDown()
184 {
185 if (cleanup)
186 cleanup_all_objects(ioctx);
187 ioctx.close();
188 }
189
190 std::string RadosTestPP::pool_name;
191 Rados RadosTestPP::s_cluster;
192
193 void RadosTestPP::SetUpTestCase()
194 {
195 init_rand();
196
197 auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
198 pool_name = get_temp_pool_name(pool_prefix);
199 ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
200 }
201
202 void RadosTestPP::TearDownTestCase()
203 {
204 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
205 }
206
207 void RadosTestPP::SetUp()
208 {
209 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
210 nspace = get_temp_pool_name();
211 ioctx.set_namespace(nspace);
212 bool req;
213 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
214 ASSERT_FALSE(req);
215 }
216
217 void RadosTestPP::TearDown()
218 {
219 if (cleanup) {
220 cleanup_default_namespace(ioctx);
221 cleanup_namespace(ioctx, nspace);
222 }
223 ioctx.close();
224 }
225
226 void RadosTestPP::cleanup_default_namespace(librados::IoCtx ioctx)
227 {
228 // remove all objects from the default namespace to avoid polluting
229 // other tests
230 cleanup_namespace(ioctx, "");
231 }
232
233 void RadosTestPP::cleanup_namespace(librados::IoCtx ioctx, std::string ns)
234 {
235 ioctx.snap_set_read(librados::SNAP_HEAD);
236 ioctx.set_namespace(ns);
237 int tries = 20;
238 while (--tries) {
239 int got_enoent = 0;
240 for (NObjectIterator it = ioctx.nobjects_begin();
241 it != ioctx.nobjects_end(); ++it) {
242 ioctx.locator_set_key(it->get_locator());
243 ObjectWriteOperation op;
244 op.remove();
245 librados::AioCompletion *completion = s_cluster.aio_create_completion();
246 auto sg = make_scope_guard([&] { completion->release(); });
247 ASSERT_EQ(0, ioctx.aio_operate(it->get_oid(), completion, &op,
248 librados::OPERATION_IGNORE_CACHE));
249 completion->wait_for_complete();
250 if (completion->get_return_value() == -ENOENT) {
251 ++got_enoent;
252 std::cout << " got ENOENT removing " << it->get_oid()
253 << " in ns " << ns << std::endl;
254 } else {
255 ASSERT_EQ(0, completion->get_return_value());
256 }
257 }
258 if (!got_enoent) {
259 break;
260 }
261 std::cout << " got ENOENT on " << got_enoent
262 << " objects, waiting a bit for snap"
263 << " trimming before retrying " << tries << " more times..."
264 << std::endl;
265 sleep(1);
266 }
267 if (tries == 0) {
268 std::cout << "failed to clean up; probably need to scrub purged_snaps."
269 << std::endl;
270 }
271 }
272
273 std::string RadosTestParamPP::pool_name;
274 std::string RadosTestParamPP::cache_pool_name;
275 Rados RadosTestParamPP::s_cluster;
276
277 void RadosTestParamPP::SetUpTestCase()
278 {
279 auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
280 pool_name = get_temp_pool_name(pool_prefix);
281 ASSERT_EQ("", create_one_pool_pp(pool_name, s_cluster));
282 }
283
284 void RadosTestParamPP::TearDownTestCase()
285 {
286 if (cache_pool_name.length()) {
287 // tear down tiers
288 bufferlist inbl;
289 ASSERT_EQ(0, s_cluster.mon_command(
290 "{\"prefix\": \"osd tier remove-overlay\", \"pool\": \"" + pool_name +
291 "\"}",
292 inbl, NULL, NULL));
293 ASSERT_EQ(0, s_cluster.mon_command(
294 "{\"prefix\": \"osd tier remove\", \"pool\": \"" + pool_name +
295 "\", \"tierpool\": \"" + cache_pool_name + "\"}",
296 inbl, NULL, NULL));
297 ASSERT_EQ(0, s_cluster.mon_command(
298 "{\"prefix\": \"osd pool delete\", \"pool\": \"" + cache_pool_name +
299 "\", \"pool2\": \"" + cache_pool_name + "\", \"yes_i_really_really_mean_it\": true}",
300 inbl, NULL, NULL));
301 cache_pool_name = "";
302 }
303 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, s_cluster));
304 }
305
306 void RadosTestParamPP::SetUp()
307 {
308 if (!is_crimson_cluster() && strcmp(GetParam(), "cache") == 0 &&
309 cache_pool_name.empty()) {
310 auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
311 cache_pool_name = get_temp_pool_name();
312 bufferlist inbl;
313 ASSERT_EQ(0, cluster.mon_command(
314 "{\"prefix\": \"osd pool create\", \"pool\": \"" + cache_pool_name +
315 "\", \"pg_num\": 4}",
316 inbl, NULL, NULL));
317 ASSERT_EQ(0, cluster.mon_command(
318 "{\"prefix\": \"osd tier add\", \"pool\": \"" + pool_name +
319 "\", \"tierpool\": \"" + cache_pool_name +
320 "\", \"force_nonempty\": \"--force-nonempty\" }",
321 inbl, NULL, NULL));
322 ASSERT_EQ(0, cluster.mon_command(
323 "{\"prefix\": \"osd tier set-overlay\", \"pool\": \"" + pool_name +
324 "\", \"overlaypool\": \"" + cache_pool_name + "\"}",
325 inbl, NULL, NULL));
326 ASSERT_EQ(0, cluster.mon_command(
327 "{\"prefix\": \"osd tier cache-mode\", \"pool\": \"" + cache_pool_name +
328 "\", \"mode\": \"writeback\"}",
329 inbl, NULL, NULL));
330 cluster.wait_for_latest_osdmap();
331 }
332
333 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
334 nspace = get_temp_pool_name();
335 ioctx.set_namespace(nspace);
336 bool req;
337 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
338 ASSERT_FALSE(req);
339 }
340
341 void RadosTestParamPP::TearDown()
342 {
343 if (cleanup) {
344 cleanup_default_namespace(ioctx);
345 cleanup_namespace(ioctx, nspace);
346 }
347 ioctx.close();
348 }
349
350 void RadosTestParamPP::cleanup_default_namespace(librados::IoCtx ioctx)
351 {
352 // remove all objects from the default namespace to avoid polluting
353 // other tests
354 cleanup_namespace(ioctx, "");
355 }
356
357 void RadosTestParamPP::cleanup_namespace(librados::IoCtx ioctx, std::string ns)
358 {
359 ioctx.snap_set_read(librados::SNAP_HEAD);
360 ioctx.set_namespace(ns);
361 for (NObjectIterator it = ioctx.nobjects_begin();
362 it != ioctx.nobjects_end(); ++it) {
363 ioctx.locator_set_key(it->get_locator());
364 ASSERT_EQ(0, ioctx.remove(it->get_oid()));
365 }
366 }
367
368 std::string RadosTestECPP::pool_name;
369 Rados RadosTestECPP::s_cluster;
370
371 void RadosTestECPP::SetUpTestCase()
372 {
373 SKIP_IF_CRIMSON();
374 auto pool_prefix = fmt::format("{}_", ::testing::UnitTest::GetInstance()->current_test_case()->name());
375 pool_name = get_temp_pool_name(pool_prefix);
376 ASSERT_EQ("", create_one_ec_pool_pp(pool_name, s_cluster));
377 }
378
379 void RadosTestECPP::TearDownTestCase()
380 {
381 SKIP_IF_CRIMSON();
382 ASSERT_EQ(0, destroy_one_ec_pool_pp(pool_name, s_cluster));
383 }
384
385 void RadosTestECPP::SetUp()
386 {
387 SKIP_IF_CRIMSON();
388 ASSERT_EQ(0, cluster.ioctx_create(pool_name.c_str(), ioctx));
389 nspace = get_temp_pool_name();
390 ioctx.set_namespace(nspace);
391 bool req;
392 ASSERT_EQ(0, ioctx.pool_requires_alignment2(&req));
393 ASSERT_TRUE(req);
394 ASSERT_EQ(0, ioctx.pool_required_alignment2(&alignment));
395 ASSERT_NE(0U, alignment);
396 }
397
398 void RadosTestECPP::TearDown()
399 {
400 SKIP_IF_CRIMSON();
401 if (cleanup) {
402 cleanup_default_namespace(ioctx);
403 cleanup_namespace(ioctx, nspace);
404 }
405 ioctx.close();
406 }
407