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
|
// -*- 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/journal/mock/MockJournaler.h"
#include "cls/journal/cls_journal_types.h"
#include "librbd/journal/CreateRequest.h"
#include "librbd/journal/RemoveRequest.h"
#include "librbd/journal/ResetRequest.h"
namespace librbd {
namespace {
struct MockTestImageCtx : public MockImageCtx {
MockTestImageCtx(librbd::ImageCtx& image_ctx) : MockImageCtx(image_ctx) {
}
};
} // anonymous namespace
namespace journal {
template <>
struct TypeTraits<MockTestImageCtx> {
typedef ::journal::MockJournalerProxy Journaler;
};
template <>
struct CreateRequest<MockTestImageCtx> {
static CreateRequest* s_instance;
Context* on_finish = nullptr;
static CreateRequest* create(IoCtx &ioctx, const std::string &imageid,
uint8_t order, uint8_t splay_width,
const std::string &object_pool,
uint64_t tag_class, TagData &tag_data,
const std::string &client_id,
ContextWQ *op_work_queue, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
CreateRequest() {
s_instance = this;
}
};
template <>
struct RemoveRequest<MockTestImageCtx> {
static RemoveRequest* s_instance;
Context* on_finish = nullptr;
static RemoveRequest* create(IoCtx &ioctx, const std::string &image_id,
const std::string &client_id,
ContextWQ *op_work_queue, Context *on_finish) {
ceph_assert(s_instance != nullptr);
s_instance->on_finish = on_finish;
return s_instance;
}
MOCK_METHOD0(send, void());
RemoveRequest() {
s_instance = this;
}
};
CreateRequest<MockTestImageCtx>* CreateRequest<MockTestImageCtx>::s_instance = nullptr;
RemoveRequest<MockTestImageCtx>* RemoveRequest<MockTestImageCtx>::s_instance = nullptr;
} // namespace journal
} // namespace librbd
#include "librbd/journal/ResetRequest.cc"
namespace librbd {
namespace journal {
using ::testing::_;
using ::testing::DoAll;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::WithArg;
class TestMockJournalResetRequest : public TestMockFixture {
public:
typedef ResetRequest<MockTestImageCtx> MockResetRequest;
typedef CreateRequest<MockTestImageCtx> MockCreateRequest;
typedef RemoveRequest<MockTestImageCtx> MockRemoveRequest;
void expect_construct_journaler(::journal::MockJournaler &mock_journaler) {
EXPECT_CALL(mock_journaler, construct());
}
void expect_init_journaler(::journal::MockJournaler &mock_journaler, int r) {
EXPECT_CALL(mock_journaler, init(_))
.WillOnce(CompleteContext(r, static_cast<ContextWQ*>(NULL)));
}
void expect_get_metadata(::journal::MockJournaler& mock_journaler) {
EXPECT_CALL(mock_journaler, get_metadata(_, _, _))
.WillOnce(Invoke([](uint8_t* order, uint8_t* splay_width,
int64_t* pool_id) {
*order = 24;
*splay_width = 4;
*pool_id = -1;
}));
}
void expect_shut_down_journaler(::journal::MockJournaler &mock_journaler,
int r) {
EXPECT_CALL(mock_journaler, shut_down(_))
.WillOnce(CompleteContext(r, static_cast<ContextWQ*>(NULL)));
}
void expect_remove(MockRemoveRequest& mock_remove_request, int r) {
EXPECT_CALL(mock_remove_request, send())
.WillOnce(Invoke([&mock_remove_request, r]() {
mock_remove_request.on_finish->complete(r);
}));
}
void expect_create(MockCreateRequest& mock_create_request, int r) {
EXPECT_CALL(mock_create_request, send())
.WillOnce(Invoke([&mock_create_request, r]() {
mock_create_request.on_finish->complete(r);
}));
}
};
TEST_F(TestMockJournalResetRequest, Success) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
InSequence seq;
::journal::MockJournaler mock_journaler;
expect_construct_journaler(mock_journaler);
expect_init_journaler(mock_journaler, 0);
expect_get_metadata(mock_journaler);
expect_shut_down_journaler(mock_journaler, 0);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, 0);
ContextWQ* context_wq;
Journal<>::get_work_queue(ictx->cct, &context_wq);
C_SaferCond ctx;
auto req = MockResetRequest::create(m_ioctx, "image id",
Journal<>::IMAGE_CLIENT_ID,
Journal<>::LOCAL_MIRROR_UUID,
context_wq, &ctx);
req->send();
ASSERT_EQ(0, ctx.wait());
}
TEST_F(TestMockJournalResetRequest, InitError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
InSequence seq;
::journal::MockJournaler mock_journaler;
expect_construct_journaler(mock_journaler);
expect_init_journaler(mock_journaler, -EINVAL);
expect_shut_down_journaler(mock_journaler, 0);
ContextWQ* context_wq;
Journal<>::get_work_queue(ictx->cct, &context_wq);
C_SaferCond ctx;
auto req = MockResetRequest::create(m_ioctx, "image id",
Journal<>::IMAGE_CLIENT_ID,
Journal<>::LOCAL_MIRROR_UUID,
context_wq, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockJournalResetRequest, ShutDownError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
InSequence seq;
::journal::MockJournaler mock_journaler;
expect_construct_journaler(mock_journaler);
expect_init_journaler(mock_journaler, 0);
expect_get_metadata(mock_journaler);
expect_shut_down_journaler(mock_journaler, -EINVAL);
ContextWQ* context_wq;
Journal<>::get_work_queue(ictx->cct, &context_wq);
C_SaferCond ctx;
auto req = MockResetRequest::create(m_ioctx, "image id",
Journal<>::IMAGE_CLIENT_ID,
Journal<>::LOCAL_MIRROR_UUID,
context_wq, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockJournalResetRequest, RemoveError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
InSequence seq;
::journal::MockJournaler mock_journaler;
expect_construct_journaler(mock_journaler);
expect_init_journaler(mock_journaler, 0);
expect_get_metadata(mock_journaler);
expect_shut_down_journaler(mock_journaler, 0);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, -EINVAL);
ContextWQ* context_wq;
Journal<>::get_work_queue(ictx->cct, &context_wq);
C_SaferCond ctx;
auto req = MockResetRequest::create(m_ioctx, "image id",
Journal<>::IMAGE_CLIENT_ID,
Journal<>::LOCAL_MIRROR_UUID,
context_wq, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
TEST_F(TestMockJournalResetRequest, CreateError) {
REQUIRE_FEATURE(RBD_FEATURE_JOURNALING);
librbd::ImageCtx *ictx;
ASSERT_EQ(0, open_image(m_image_name, &ictx));
InSequence seq;
::journal::MockJournaler mock_journaler;
expect_construct_journaler(mock_journaler);
expect_init_journaler(mock_journaler, 0);
expect_get_metadata(mock_journaler);
expect_shut_down_journaler(mock_journaler, 0);
MockRemoveRequest mock_remove_request;
expect_remove(mock_remove_request, 0);
MockCreateRequest mock_create_request;
expect_create(mock_create_request, -EINVAL);
ContextWQ* context_wq;
Journal<>::get_work_queue(ictx->cct, &context_wq);
C_SaferCond ctx;
auto req = MockResetRequest::create(m_ioctx, "image id",
Journal<>::IMAGE_CLIENT_ID,
Journal<>::LOCAL_MIRROR_UUID,
context_wq, &ctx);
req->send();
ASSERT_EQ(-EINVAL, ctx.wait());
}
} // namespace journal
} // namespace librbd
|