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
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/io/MockObjectDispatch.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "include/stringify.h"
#include "common/bit_vector.hpp"
#include "librbd/ImageState.h"
#include "librbd/internal.h"
#include "librbd/operation/SnapshotRollbackRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace librbd {
namespace {
struct MockOperationImageCtx : public MockImageCtx {
MockOperationImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace operation {
template <>
struct ResizeRequest<MockOperationImageCtx> {
static ResizeRequest *s_instance;
Context *on_finish = nullptr;
static ResizeRequest* create(MockOperationImageCtx &image_ctx, Context *on_finish,
uint64_t new_size, bool allow_shrink,
ProgressContext &prog_ctx, uint64_t journal_op_tid,
bool disable_journal) {
ceph_assert(s_instance != nullptr);
ceph_assert(journal_op_tid == 0);
ceph_assert(disable_journal);
s_instance->on_finish = on_finish;
return s_instance;
}
ResizeRequest() {
s_instance = this;
}
MOCK_METHOD0(send, void());
};
ResizeRequest<MockOperationImageCtx> *ResizeRequest<MockOperationImageCtx>::s_instance = nullptr;
} // namespace operation
template <>
struct AsyncRequest<MockOperationImageCtx> : public AsyncRequest<MockImageCtx> {
MockOperationImageCtx &m_image_ctx;
AsyncRequest(MockOperationImageCtx &image_ctx, Context *on_finish)
: AsyncRequest<MockImageCtx>(image_ctx, on_finish), m_image_ctx(image_ctx) {
}
};
} // namespace librbd
// template definitions
#include "librbd/AsyncRequest.cc"
#include "librbd/AsyncObjectThrottle.cc"
#include "librbd/operation/Request.cc"
#include "librbd/operation/SnapshotRollbackRequest.cc"
namespace librbd {
namespace operation {
using ::testing::_;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::WithArg;
class TestMockOperationSnapshotRollbackRequest : public TestMockFixture {
public:
typedef SnapshotRollbackRequest<MockOperationImageCtx> MockSnapshotRollbackRequest;
typedef ResizeRequest<MockOperationImageCtx> MockResizeRequest;
void expect_block_writes(MockOperationImageCtx &mock_image_ctx, int r) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, block_writes(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_unblock_writes(MockOperationImageCtx &mock_image_ctx) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, unblock_writes())
.Times(1);
}
void expect_get_image_size(MockOperationImageCtx &mock_image_ctx,
uint64_t size) {
EXPECT_CALL(mock_image_ctx, get_image_size(CEPH_NOSNAP))
.WillOnce(Return(size));
}
void expect_resize(MockOperationImageCtx &mock_image_ctx,
MockResizeRequest &mock_resize_request, int r) {
expect_get_image_size(mock_image_ctx, 123);
EXPECT_CALL(mock_resize_request, send())
.WillOnce(FinishRequest(&mock_resize_request, r,
&mock_image_ctx));
}
void expect_get_flags(MockOperationImageCtx &mock_image_ctx,
uint64_t snap_id, int r) {
EXPECT_CALL(mock_image_ctx, get_flags(snap_id, _))
.WillOnce(Return(r));
}
void expect_object_may_exist(MockOperationImageCtx &mock_image_ctx,
uint64_t object_no, bool exists) {
if (mock_image_ctx.object_map != nullptr) {
EXPECT_CALL(*mock_image_ctx.object_map, object_may_exist(object_no))
.WillOnce(Return(exists));
}
}
void expect_get_snap_object_map(MockOperationImageCtx &mock_image_ctx,
MockObjectMap *mock_object_map, uint64_t snap_id) {
if (mock_image_ctx.object_map != nullptr) {
EXPECT_CALL(mock_image_ctx, create_object_map(snap_id))
.WillOnce(Return(mock_object_map));
EXPECT_CALL(*mock_object_map, open(_))
.WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
}
}
void expect_rollback_object_map(MockOperationImageCtx &mock_image_ctx,
MockObjectMap &mock_object_map) {
if (mock_image_ctx.object_map != nullptr) {
EXPECT_CALL(mock_object_map, rollback(_, _))
.WillOnce(WithArg<1>(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue)));
}
}
void expect_get_object_name(MockOperationImageCtx &mock_image_ctx,
uint64_t object_num) {
EXPECT_CALL(mock_image_ctx, get_object_name(object_num))
.WillOnce(Return("object-name-" + stringify(object_num)));
}
void expect_get_current_size(MockOperationImageCtx &mock_image_ctx, uint64_t size) {
EXPECT_CALL(mock_image_ctx, get_current_size())
.WillOnce(Return(size));
}
void expect_rollback_snap_id(MockOperationImageCtx &mock_image_ctx,
const std::string &oid, int r) {
EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.data_ctx),
selfmanaged_snap_rollback(oid, _))
.WillOnce(Return(r));
}
void expect_rollback(MockOperationImageCtx &mock_image_ctx, int r) {
expect_get_current_size(mock_image_ctx, 1);
expect_object_may_exist(mock_image_ctx, 0, true);
expect_get_object_name(mock_image_ctx, 0);
expect_rollback_snap_id(mock_image_ctx, "object-name-0", r);
}
void expect_create_object_map(MockOperationImageCtx &mock_image_ctx,
MockObjectMap *mock_object_map) {
EXPECT_CALL(mock_image_ctx, create_object_map(_))
.WillOnce(Return(mock_object_map));
}
void expect_open_object_map(MockOperationImageCtx &mock_image_ctx,
MockObjectMap &mock_object_map) {
EXPECT_CALL(mock_object_map, open(_))
.WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
}
void expect_refresh_object_map(MockOperationImageCtx &mock_image_ctx,
MockObjectMap &mock_object_map) {
if (mock_image_ctx.object_map != nullptr) {
expect_create_object_map(mock_image_ctx, &mock_object_map);
expect_open_object_map(mock_image_ctx, mock_object_map);
}
}
void expect_invalidate_cache(MockOperationImageCtx &mock_image_ctx,
int r) {
EXPECT_CALL(*mock_image_ctx.io_image_dispatcher, invalidate_cache(_))
.WillOnce(CompleteContext(r, mock_image_ctx.image_ctx->op_work_queue));
}
int when_snap_rollback(MockOperationImageCtx &mock_image_ctx,
const std::string &snap_name,
uint64_t snap_id, uint64_t snap_size) {
C_SaferCond cond_ctx;
librbd::NoOpProgressContext prog_ctx;
MockSnapshotRollbackRequest *req = new MockSnapshotRollbackRequest(
mock_image_ctx, &cond_ctx, cls::rbd::UserSnapshotNamespace(), snap_name,
snap_id, snap_size, prog_ctx);
{
std::shared_lock owner_locker{mock_image_ctx.owner_lock};
req->send();
}
return cond_ctx.wait();
}
};
TEST_F(TestMockOperationSnapshotRollbackRequest, Success) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockOperationImageCtx mock_image_ctx(*ictx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
MockObjectMap mock_snap_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
MockResizeRequest mock_resize_request;
expect_append_op_event(mock_image_ctx, false, 0);
expect_block_writes(mock_image_ctx, 0);
expect_resize(mock_image_ctx, mock_resize_request, 0);
expect_get_flags(mock_image_ctx, 123, 0);
expect_get_snap_object_map(mock_image_ctx, &mock_snap_object_map, 123);
expect_rollback_object_map(mock_image_ctx, mock_object_map);
expect_rollback(mock_image_ctx, 0);
expect_refresh_object_map(mock_image_ctx, mock_object_map);
expect_invalidate_cache(mock_image_ctx, 0);
expect_commit_op_event(mock_image_ctx, 0);
expect_unblock_writes(mock_image_ctx);
ASSERT_EQ(0, when_snap_rollback(mock_image_ctx, "snap", 123, 0));
}
TEST_F(TestMockOperationSnapshotRollbackRequest, BlockWritesError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockOperationImageCtx mock_image_ctx(*ictx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_append_op_event(mock_image_ctx, false, 0);
expect_block_writes(mock_image_ctx, -EINVAL);
expect_commit_op_event(mock_image_ctx, -EINVAL);
expect_unblock_writes(mock_image_ctx);
ASSERT_EQ(-EINVAL, when_snap_rollback(mock_image_ctx, "snap", 123, 0));
}
TEST_F(TestMockOperationSnapshotRollbackRequest, SkipResize) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockOperationImageCtx mock_image_ctx(*ictx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
MockObjectMap mock_snap_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
expect_append_op_event(mock_image_ctx, false, 0);
expect_block_writes(mock_image_ctx, 0);
expect_get_image_size(mock_image_ctx, 345);
expect_get_flags(mock_image_ctx, 123, 0);
expect_get_snap_object_map(mock_image_ctx, &mock_snap_object_map, 123);
expect_rollback_object_map(mock_image_ctx, mock_object_map);
expect_rollback(mock_image_ctx, 0);
expect_refresh_object_map(mock_image_ctx, mock_object_map);
expect_invalidate_cache(mock_image_ctx, 0);
expect_commit_op_event(mock_image_ctx, 0);
expect_unblock_writes(mock_image_ctx);
ASSERT_EQ(0, when_snap_rollback(mock_image_ctx, "snap", 123, 345));
}
TEST_F(TestMockOperationSnapshotRollbackRequest, ResizeError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockOperationImageCtx mock_image_ctx(*ictx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
MockResizeRequest mock_resize_request;
expect_append_op_event(mock_image_ctx, false, 0);
expect_block_writes(mock_image_ctx, 0);
expect_resize(mock_image_ctx, mock_resize_request, -EINVAL);
expect_commit_op_event(mock_image_ctx, -EINVAL);
expect_unblock_writes(mock_image_ctx);
ASSERT_EQ(-EINVAL, when_snap_rollback(mock_image_ctx, "snap", 123, 0));
}
TEST_F(TestMockOperationSnapshotRollbackRequest, RollbackObjectsError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
MockOperationImageCtx mock_image_ctx(*ictx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
MockObjectMap mock_snap_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
MockResizeRequest mock_resize_request;
expect_append_op_event(mock_image_ctx, false, 0);
expect_block_writes(mock_image_ctx, 0);
expect_resize(mock_image_ctx, mock_resize_request, 0);
expect_get_flags(mock_image_ctx, 123, 0);
expect_get_snap_object_map(mock_image_ctx, &mock_snap_object_map, 123);
expect_rollback_object_map(mock_image_ctx, mock_object_map);
expect_rollback(mock_image_ctx, -EINVAL);
expect_commit_op_event(mock_image_ctx, -EINVAL);
expect_unblock_writes(mock_image_ctx);
ASSERT_EQ(-EINVAL, when_snap_rollback(mock_image_ctx, "snap", 123, 0));
}
TEST_F(TestMockOperationSnapshotRollbackRequest, InvalidateCacheError) {
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
REQUIRE(ictx->cache);
MockOperationImageCtx mock_image_ctx(*ictx);
MockExclusiveLock mock_exclusive_lock;
MockJournal mock_journal;
MockObjectMap mock_object_map;
MockObjectMap mock_snap_object_map;
initialize_features(ictx, mock_image_ctx, mock_exclusive_lock, mock_journal,
mock_object_map);
expect_op_work_queue(mock_image_ctx);
InSequence seq;
MockResizeRequest mock_resize_request;
expect_append_op_event(mock_image_ctx, false, 0);
expect_block_writes(mock_image_ctx, 0);
expect_resize(mock_image_ctx, mock_resize_request, 0);
expect_get_flags(mock_image_ctx, 123, 0);
expect_get_snap_object_map(mock_image_ctx, &mock_snap_object_map, 123);
expect_rollback_object_map(mock_image_ctx, mock_object_map);
expect_rollback(mock_image_ctx, 0);
expect_refresh_object_map(mock_image_ctx, mock_object_map);
expect_invalidate_cache(mock_image_ctx, -EINVAL);
expect_commit_op_event(mock_image_ctx, -EINVAL);
expect_unblock_writes(mock_image_ctx);
ASSERT_EQ(-EINVAL, when_snap_rollback(mock_image_ctx, "snap", 123, 0));
}
} // namespace operation
} // namespace librbd
|