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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_fixture.h"
#include "test/librbd/test_support.h"
#include "include/rbd/librbd.h"
#include "include/rbd/librbd.hpp"
#include "test/librados/test.h"
#include "gtest/gtest.h"
#include <boost/scope_exit.hpp>
#include <chrono>
#include <vector>
void register_test_groups() {
}
class TestGroup : public TestFixture {
};
TEST_F(TestGroup, group_create)
{
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, _pool_name.c_str(), &ioctx);
BOOST_SCOPE_EXIT(ioctx) {
rados_ioctx_destroy(ioctx);
} BOOST_SCOPE_EXIT_END;
librbd::RBD rbd;
ASSERT_EQ(0, rbd_group_create(ioctx, "mygroup"));
size_t size = 0;
ASSERT_EQ(-ERANGE, rbd_group_list(ioctx, NULL, &size));
ASSERT_EQ(strlen("mygroup") + 1, size);
char groups[80];
ASSERT_EQ(static_cast<int>(strlen("mygroup") + 1),
rbd_group_list(ioctx, groups, &size));
ASSERT_STREQ("mygroup", groups);
ASSERT_EQ(0, rbd_group_remove(ioctx, "mygroup"));
ASSERT_EQ(0, rbd_group_list(ioctx, groups, &size));
}
TEST_F(TestGroup, group_createPP)
{
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), ioctx));
librbd::RBD rbd;
ASSERT_EQ(0, rbd.group_create(ioctx, "mygroup"));
std::vector<std::string> groups;
ASSERT_EQ(0, rbd.group_list(ioctx, &groups));
ASSERT_EQ(1U, groups.size());
ASSERT_EQ("mygroup", groups[0]);
groups.clear();
ASSERT_EQ(0, rbd.group_rename(ioctx, "mygroup", "newgroup"));
ASSERT_EQ(0, rbd.group_list(ioctx, &groups));
ASSERT_EQ(1U, groups.size());
ASSERT_EQ("newgroup", groups[0]);
ASSERT_EQ(0, rbd.group_remove(ioctx, "newgroup"));
groups.clear();
ASSERT_EQ(0, rbd.group_list(ioctx, &groups));
ASSERT_EQ(0U, groups.size());
}
TEST_F(TestGroup, add_image)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, _pool_name.c_str(), &ioctx);
BOOST_SCOPE_EXIT(ioctx) {
rados_ioctx_destroy(ioctx);
} BOOST_SCOPE_EXIT_END;
const char *group_name = "mycg";
ASSERT_EQ(0, rbd_group_create(ioctx, group_name));
rbd_image_t image;
ASSERT_EQ(0, rbd_open(ioctx, m_image_name.c_str(), &image, NULL));
BOOST_SCOPE_EXIT(image) {
EXPECT_EQ(0, rbd_close(image));
} BOOST_SCOPE_EXIT_END;
uint64_t features;
ASSERT_EQ(0, rbd_get_features(image, &features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) == 0ULL);
uint64_t op_features;
ASSERT_EQ(0, rbd_get_op_features(image, &op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) == 0ULL);
rbd_group_info_t group_info;
ASSERT_EQ(0, rbd_get_group(image, &group_info, sizeof(group_info)));
ASSERT_EQ(0, strcmp("", group_info.name));
ASSERT_EQ(RBD_GROUP_INVALID_POOL, group_info.pool);
rbd_group_info_cleanup(&group_info, sizeof(group_info));
ASSERT_EQ(0, rbd_group_image_add(ioctx, group_name, ioctx,
m_image_name.c_str()));
ASSERT_EQ(-ERANGE, rbd_get_group(image, &group_info, 0));
ASSERT_EQ(0, rbd_get_group(image, &group_info, sizeof(group_info)));
ASSERT_EQ(0, strcmp(group_name, group_info.name));
ASSERT_EQ(rados_ioctx_get_id(ioctx), group_info.pool);
rbd_group_info_cleanup(&group_info, sizeof(group_info));
ASSERT_EQ(0, rbd_get_features(image, &features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) ==
RBD_FEATURE_OPERATIONS);
ASSERT_EQ(0, rbd_get_op_features(image, &op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) ==
RBD_OPERATION_FEATURE_GROUP);
size_t num_images = 0;
ASSERT_EQ(-ERANGE, rbd_group_image_list(ioctx, group_name, NULL,
sizeof(rbd_group_image_info_t),
&num_images));
ASSERT_EQ(1U, num_images);
rbd_group_image_info_t images[1];
ASSERT_EQ(1, rbd_group_image_list(ioctx, group_name, images,
sizeof(rbd_group_image_info_t),
&num_images));
ASSERT_EQ(m_image_name, images[0].name);
ASSERT_EQ(rados_ioctx_get_id(ioctx), images[0].pool);
ASSERT_EQ(0, rbd_group_image_list_cleanup(images,
sizeof(rbd_group_image_info_t),
num_images));
ASSERT_EQ(0, rbd_group_image_remove(ioctx, group_name, ioctx,
m_image_name.c_str()));
ASSERT_EQ(0, rbd_get_features(image, &features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) == 0ULL);
ASSERT_EQ(0, rbd_get_op_features(image, &op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) == 0ULL);
ASSERT_EQ(0, rbd_group_image_list(ioctx, group_name, images,
sizeof(rbd_group_image_info_t),
&num_images));
ASSERT_EQ(0U, num_images);
ASSERT_EQ(0, rbd_group_remove(ioctx, group_name));
}
TEST_F(TestGroup, add_imagePP)
{
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), ioctx));
const char *group_name = "mycg";
librbd::RBD rbd;
ASSERT_EQ(0, rbd.group_create(ioctx, group_name));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, m_image_name.c_str(), NULL));
uint64_t features;
ASSERT_EQ(0, image.features(&features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) == 0ULL);
uint64_t op_features;
ASSERT_EQ(0, image.get_op_features(&op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) == 0ULL);
librbd::group_info_t group_info;
ASSERT_EQ(0, image.get_group(&group_info, sizeof(group_info)));
ASSERT_EQ(std::string(""), group_info.name);
ASSERT_EQ(RBD_GROUP_INVALID_POOL, group_info.pool);
ASSERT_EQ(0, rbd.group_image_add(ioctx, group_name, ioctx,
m_image_name.c_str()));
ASSERT_EQ(-ERANGE, image.get_group(&group_info, 0));
ASSERT_EQ(0, image.get_group(&group_info, sizeof(group_info)));
ASSERT_EQ(std::string(group_name), group_info.name);
ASSERT_EQ(ioctx.get_id(), group_info.pool);
ASSERT_EQ(0, image.features(&features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) ==
RBD_FEATURE_OPERATIONS);
ASSERT_EQ(0, image.get_op_features(&op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) ==
RBD_OPERATION_FEATURE_GROUP);
std::vector<librbd::group_image_info_t> images;
ASSERT_EQ(0, rbd.group_image_list(ioctx, group_name, &images,
sizeof(librbd::group_image_info_t)));
ASSERT_EQ(1U, images.size());
ASSERT_EQ(m_image_name, images[0].name);
ASSERT_EQ(ioctx.get_id(), images[0].pool);
ASSERT_EQ(0, rbd.group_image_remove(ioctx, group_name, ioctx,
m_image_name.c_str()));
ASSERT_EQ(0, image.features(&features));
ASSERT_TRUE((features & RBD_FEATURE_OPERATIONS) == 0ULL);
ASSERT_EQ(0, image.get_op_features(&op_features));
ASSERT_TRUE((op_features & RBD_OPERATION_FEATURE_GROUP) == 0ULL);
images.clear();
ASSERT_EQ(0, rbd.group_image_list(ioctx, group_name, &images,
sizeof(librbd::group_image_info_t)));
ASSERT_EQ(0U, images.size());
ASSERT_EQ(0, rbd.group_remove(ioctx, group_name));
}
TEST_F(TestGroup, add_snapshot)
{
REQUIRE_FORMAT_V2();
rados_ioctx_t ioctx;
rados_ioctx_create(_cluster, _pool_name.c_str(), &ioctx);
BOOST_SCOPE_EXIT(ioctx) {
rados_ioctx_destroy(ioctx);
} BOOST_SCOPE_EXIT_END;
const char *group_name = "snap_group";
const char *snap_name = "snap_snapshot";
const char orig_data[] = "orig data";
const char test_data[] = "test data";
char read_data[10];
rbd_image_t image;
ASSERT_EQ(0, rbd_open(ioctx, m_image_name.c_str(), &image, NULL));
BOOST_SCOPE_EXIT(image) {
EXPECT_EQ(0, rbd_close(image));
} BOOST_SCOPE_EXIT_END;
ASSERT_EQ(10, rbd_write(image, 0, 10, orig_data));
ASSERT_EQ(10, rbd_read(image, 0, 10, read_data));
ASSERT_EQ(0, memcmp(orig_data, read_data, 10));
ASSERT_EQ(0, rbd_group_create(ioctx, group_name));
ASSERT_EQ(0, rbd_group_image_add(ioctx, group_name, ioctx,
m_image_name.c_str()));
struct Watcher {
static void quiesce_cb(void *arg) {
Watcher *watcher = static_cast<Watcher *>(arg);
watcher->handle_quiesce();
}
static void unquiesce_cb(void *arg) {
Watcher *watcher = static_cast<Watcher *>(arg);
watcher->handle_unquiesce();
}
rbd_image_t ℑ
uint64_t handle = 0;
size_t quiesce_count = 0;
size_t unquiesce_count = 0;
int r = 0;
ceph::mutex lock = ceph::make_mutex("lock");
ceph::condition_variable cv;
Watcher(rbd_image_t &image) : image(image) {
}
void handle_quiesce() {
ASSERT_EQ(quiesce_count, unquiesce_count);
quiesce_count++;
rbd_quiesce_complete(image, handle, r);
}
void handle_unquiesce() {
std::unique_lock locker(lock);
unquiesce_count++;
cv.notify_one();
}
bool wait_for_unquiesce(size_t c) {
std::unique_lock locker(lock);
return cv.wait_for(locker, std::chrono::seconds(60),
[this, c]() { return unquiesce_count >= c; });
}
} watcher(image);
ASSERT_EQ(0, rbd_quiesce_watch(image, Watcher::quiesce_cb,
Watcher::unquiesce_cb, &watcher,
&watcher.handle));
ASSERT_EQ(0, rbd_group_snap_create(ioctx, group_name, snap_name));
ASSERT_TRUE(watcher.wait_for_unquiesce(1U));
ASSERT_EQ(1U, watcher.quiesce_count);
size_t num_snaps = 0;
ASSERT_EQ(-ERANGE, rbd_group_snap_list(ioctx, group_name, NULL,
sizeof(rbd_group_snap_info_t),
&num_snaps));
ASSERT_EQ(1U, num_snaps);
rbd_group_snap_info_t snaps[1];
ASSERT_EQ(1, rbd_group_snap_list(ioctx, group_name, snaps,
sizeof(rbd_group_snap_info_t),
&num_snaps));
ASSERT_STREQ(snap_name, snaps[0].name);
ASSERT_EQ(10, rbd_write(image, 11, 10, test_data));
ASSERT_EQ(10, rbd_read(image, 11, 10, read_data));
ASSERT_EQ(0, memcmp(test_data, read_data, 10));
ASSERT_EQ(0, rbd_group_snap_rollback(ioctx, group_name, snap_name));
ASSERT_EQ(10, rbd_read(image, 0, 10, read_data));
ASSERT_EQ(0, memcmp(orig_data, read_data, 10));
ASSERT_EQ(0, rbd_group_snap_list_cleanup(snaps, sizeof(rbd_group_snap_info_t),
num_snaps));
ASSERT_EQ(0, rbd_group_snap_remove(ioctx, group_name, snap_name));
ASSERT_EQ(0, rbd_group_snap_list(ioctx, group_name, snaps,
sizeof(rbd_group_snap_info_t),
&num_snaps));
ASSERT_EQ(0U, num_snaps);
ASSERT_EQ(-EINVAL, rbd_group_snap_create2(ioctx, group_name, snap_name,
RBD_SNAP_CREATE_SKIP_QUIESCE |
RBD_SNAP_CREATE_IGNORE_QUIESCE_ERROR));
watcher.r = -EINVAL;
ASSERT_EQ(-EINVAL, rbd_group_snap_create2(ioctx, group_name, snap_name, 0));
num_snaps = 1;
ASSERT_EQ(0, rbd_group_snap_list(ioctx, group_name, snaps,
sizeof(rbd_group_snap_info_t),
&num_snaps));
watcher.quiesce_count = 0;
watcher.unquiesce_count = 0;
ASSERT_EQ(0, rbd_group_snap_create2(ioctx, group_name, snap_name,
RBD_SNAP_CREATE_SKIP_QUIESCE));
ASSERT_EQ(0U, watcher.quiesce_count);
num_snaps = 1;
ASSERT_EQ(1, rbd_group_snap_list(ioctx, group_name, snaps,
sizeof(rbd_group_snap_info_t),
&num_snaps));
ASSERT_EQ(0, rbd_group_snap_list_cleanup(snaps, sizeof(rbd_group_snap_info_t),
num_snaps));
ASSERT_EQ(0, rbd_group_snap_remove(ioctx, group_name, snap_name));
ASSERT_EQ(0, rbd_group_snap_create2(ioctx, group_name, snap_name,
RBD_SNAP_CREATE_IGNORE_QUIESCE_ERROR));
ASSERT_EQ(1, rbd_group_snap_list(ioctx, group_name, snaps,
sizeof(rbd_group_snap_info_t),
&num_snaps));
ASSERT_EQ(0, rbd_group_snap_list_cleanup(snaps, sizeof(rbd_group_snap_info_t),
num_snaps));
ASSERT_EQ(0, rbd_group_snap_remove(ioctx, group_name, snap_name));
ASSERT_EQ(0, rbd_quiesce_unwatch(image, watcher.handle));
ASSERT_EQ(0, rbd_group_remove(ioctx, group_name));
}
TEST_F(TestGroup, add_snapshotPP)
{
REQUIRE_FORMAT_V2();
librados::IoCtx ioctx;
ASSERT_EQ(0, _rados.ioctx_create(_pool_name.c_str(), ioctx));
const char *group_name = "snap_group";
const char *snap_name = "snap_snapshot";
librbd::RBD rbd;
ASSERT_EQ(0, rbd.group_create(ioctx, group_name));
ASSERT_EQ(0, rbd.group_image_add(ioctx, group_name, ioctx,
m_image_name.c_str()));
librbd::Image image;
ASSERT_EQ(0, rbd.open(ioctx, image, m_image_name.c_str(), NULL));
bufferlist expect_bl;
bufferlist read_bl;
expect_bl.append(std::string(512, '1'));
ASSERT_EQ((ssize_t)expect_bl.length(), image.write(0, expect_bl.length(), expect_bl));
ASSERT_EQ(512, image.read(0, 512, read_bl));
ASSERT_TRUE(expect_bl.contents_equal(read_bl));
ASSERT_EQ(0, rbd.group_snap_create(ioctx, group_name, snap_name));
std::vector<librbd::group_snap_info_t> snaps;
ASSERT_EQ(0, rbd.group_snap_list(ioctx, group_name, &snaps,
sizeof(librbd::group_snap_info_t)));
ASSERT_EQ(1U, snaps.size());
ASSERT_EQ(snap_name, snaps[0].name);
bufferlist write_bl;
write_bl.append(std::string(1024, '2'));
ASSERT_EQ(1024, image.write(513, write_bl.length(), write_bl));
read_bl.clear();
ASSERT_EQ(1024, image.read(513, 1024, read_bl));
ASSERT_TRUE(write_bl.contents_equal(read_bl));
ASSERT_EQ(0, rbd.group_snap_rollback(ioctx, group_name, snap_name));
ASSERT_EQ(512, image.read(0, 512, read_bl));
ASSERT_TRUE(expect_bl.contents_equal(read_bl));
ASSERT_EQ(0, image.close());
ASSERT_EQ(0, rbd.group_snap_remove(ioctx, group_name, snap_name));
snaps.clear();
ASSERT_EQ(0, rbd.group_snap_list(ioctx, group_name, &snaps,
sizeof(librbd::group_snap_info_t)));
ASSERT_EQ(0U, snaps.size());
ASSERT_EQ(0, rbd.group_snap_create(ioctx, group_name, snap_name));
ASSERT_EQ(0, rbd.group_snap_list(ioctx, group_name, &snaps,
sizeof(librbd::group_snap_info_t)));
ASSERT_EQ(1U, snaps.size());
ASSERT_EQ(0, rbd.group_snap_remove(ioctx, group_name, snap_name));
ASSERT_EQ(-EINVAL, rbd.group_snap_create2(ioctx, group_name, snap_name,
RBD_SNAP_CREATE_SKIP_QUIESCE |
RBD_SNAP_CREATE_IGNORE_QUIESCE_ERROR));
snaps.clear();
ASSERT_EQ(0, rbd.group_snap_list(ioctx, group_name, &snaps,
sizeof(librbd::group_snap_info_t)));
ASSERT_EQ(0U, snaps.size());
ASSERT_EQ(0, rbd.group_snap_create2(ioctx, group_name, snap_name,
RBD_SNAP_CREATE_SKIP_QUIESCE));
snaps.clear();
ASSERT_EQ(0, rbd.group_snap_list(ioctx, group_name, &snaps,
sizeof(librbd::group_snap_info_t)));
ASSERT_EQ(1U, snaps.size());
ASSERT_EQ(0, rbd.group_snap_remove(ioctx, group_name, snap_name));
ASSERT_EQ(0, rbd.group_remove(ioctx, group_name));
}
|