]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include "test/librbd/test_mock_fixture.h" | |
5 | #include "test/librbd/test_support.h" | |
6 | #include "test/librbd/mock/MockImageCtx.h" | |
7 | #include "test/journal/mock/MockJournaler.h" | |
8 | #include "librbd/journal/OpenRequest.h" | |
9 | #include "librbd/journal/PromoteRequest.h" | |
10 | ||
11 | namespace librbd { | |
12 | ||
13 | namespace { | |
14 | ||
15 | struct MockTestImageCtx : public MockImageCtx { | |
11fdf7f2 | 16 | explicit MockTestImageCtx(librbd::ImageCtx& image_ctx) : MockImageCtx(image_ctx) { |
7c673cae FG |
17 | } |
18 | }; | |
19 | ||
20 | } // anonymous namespace | |
21 | ||
22 | namespace journal { | |
23 | ||
24 | template <> | |
25 | struct TypeTraits<MockTestImageCtx> { | |
26 | typedef ::journal::MockJournalerProxy Journaler; | |
27 | typedef ::journal::MockFutureProxy Future; | |
28 | }; | |
29 | ||
30 | template <> | |
31 | struct OpenRequest<MockTestImageCtx> { | |
32 | Context *on_finish = nullptr; | |
33 | static OpenRequest *s_instance; | |
34 | static OpenRequest *create(MockTestImageCtx *image_ctx, | |
35 | ::journal::MockJournalerProxy *journaler, | |
9f95a23c | 36 | ceph::mutex *lock, ImageClientMeta *client_meta, |
7c673cae FG |
37 | uint64_t *tag_tid, journal::TagData *tag_data, |
38 | Context *on_finish) { | |
11fdf7f2 | 39 | ceph_assert(s_instance != nullptr); |
7c673cae FG |
40 | client_meta->tag_class = 456; |
41 | tag_data->mirror_uuid = Journal<>::ORPHAN_MIRROR_UUID; | |
42 | *tag_tid = 567; | |
43 | s_instance->on_finish = on_finish; | |
44 | return s_instance; | |
45 | } | |
46 | ||
47 | OpenRequest() { | |
48 | s_instance = this; | |
49 | } | |
50 | ||
51 | MOCK_METHOD0(send, void()); | |
52 | }; | |
53 | ||
54 | OpenRequest<MockTestImageCtx> *OpenRequest<MockTestImageCtx>::s_instance = nullptr; | |
55 | ||
56 | } // namespace journal | |
57 | } // namespace librbd | |
58 | ||
59 | // template definitions | |
60 | #include "librbd/journal/PromoteRequest.cc" | |
61 | template class librbd::journal::PromoteRequest<librbd::MockTestImageCtx>; | |
62 | ||
63 | namespace librbd { | |
64 | namespace journal { | |
65 | ||
66 | using ::testing::_; | |
67 | using ::testing::A; | |
68 | using ::testing::InSequence; | |
69 | using ::testing::Return; | |
70 | using ::testing::WithArg; | |
71 | ||
72 | class TestMockJournalPromoteRequest : public TestMockFixture { | |
73 | public: | |
74 | typedef PromoteRequest<MockTestImageCtx> MockPromoteRequest; | |
75 | typedef OpenRequest<MockTestImageCtx> MockOpenRequest; | |
76 | ||
77 | void expect_construct_journaler(::journal::MockJournaler &mock_journaler) { | |
78 | EXPECT_CALL(mock_journaler, construct()); | |
79 | } | |
80 | ||
81 | void expect_open_journaler(MockTestImageCtx &mock_image_ctx, | |
82 | MockOpenRequest &mock_open_request, int r) { | |
83 | EXPECT_CALL(mock_open_request, send()) | |
84 | .WillOnce(FinishRequest(&mock_open_request, r, &mock_image_ctx)); | |
85 | } | |
86 | ||
87 | void expect_allocate_tag(::journal::MockJournaler &mock_journaler, | |
88 | const journal::TagPredecessor &predecessor, int r) { | |
89 | TagData tag_data; | |
90 | tag_data.mirror_uuid = Journal<>::LOCAL_MIRROR_UUID; | |
91 | tag_data.predecessor = predecessor; | |
92 | ||
93 | bufferlist tag_data_bl; | |
11fdf7f2 TL |
94 | using ceph::encode; |
95 | encode(tag_data, tag_data_bl); | |
7c673cae FG |
96 | |
97 | EXPECT_CALL(mock_journaler, allocate_tag(456, ContentsEqual(tag_data_bl), | |
98 | _, _)) | |
99 | .WillOnce(WithArg<3>(CompleteContext(r, static_cast<ContextWQ*>(NULL)))); | |
100 | } | |
101 | ||
102 | void expect_append_journaler(::journal::MockJournaler &mock_journaler) { | |
103 | EXPECT_CALL(mock_journaler, append(_, _)) | |
104 | .WillOnce(Return(::journal::MockFutureProxy())); | |
105 | } | |
106 | ||
107 | void expect_future_flush(::journal::MockFuture &mock_future, int r) { | |
108 | EXPECT_CALL(mock_future, flush(_)) | |
109 | .WillOnce(CompleteContext(r, static_cast<ContextWQ*>(NULL))); | |
110 | } | |
111 | ||
112 | void expect_future_committed(::journal::MockJournaler &mock_journaler) { | |
113 | EXPECT_CALL(mock_journaler, committed(A<const ::journal::MockFutureProxy &>())); | |
114 | } | |
115 | ||
116 | void expect_flush_commit_position(::journal::MockJournaler &mock_journaler, | |
117 | int r) { | |
118 | EXPECT_CALL(mock_journaler, flush_commit_position(_)) | |
119 | .WillOnce(CompleteContext(r, static_cast<ContextWQ*>(NULL))); | |
120 | } | |
121 | ||
122 | void expect_start_append(::journal::MockJournaler &mock_journaler) { | |
494da23a | 123 | EXPECT_CALL(mock_journaler, start_append(_)); |
7c673cae FG |
124 | } |
125 | ||
126 | void expect_stop_append(::journal::MockJournaler &mock_journaler, int r) { | |
127 | EXPECT_CALL(mock_journaler, stop_append(_)) | |
128 | .WillOnce(CompleteContext(r, static_cast<ContextWQ*>(NULL))); | |
129 | } | |
130 | ||
131 | void expect_shut_down_journaler(::journal::MockJournaler &mock_journaler, | |
132 | int r) { | |
133 | EXPECT_CALL(mock_journaler, shut_down(_)) | |
134 | .WillOnce(CompleteContext(r, static_cast<ContextWQ*>(NULL))); | |
135 | } | |
136 | ||
137 | }; | |
138 | ||
139 | TEST_F(TestMockJournalPromoteRequest, SuccessOrderly) { | |
140 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
141 | ||
142 | librbd::ImageCtx *ictx; | |
143 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
144 | ||
145 | MockTestImageCtx mock_image_ctx(*ictx); | |
146 | ::journal::MockJournaler mock_journaler; | |
147 | MockOpenRequest mock_open_request; | |
148 | ||
149 | expect_op_work_queue(mock_image_ctx); | |
150 | ||
151 | InSequence seq; | |
152 | expect_construct_journaler(mock_journaler); | |
153 | expect_open_journaler(mock_image_ctx, mock_open_request, 0); | |
154 | expect_allocate_tag(mock_journaler, | |
155 | {Journal<>::ORPHAN_MIRROR_UUID, true, 567, 1}, 0); | |
156 | ||
157 | ::journal::MockFuture mock_future; | |
158 | expect_start_append(mock_journaler); | |
159 | expect_append_journaler(mock_journaler); | |
160 | expect_future_flush(mock_future, 0); | |
161 | expect_future_committed(mock_journaler); | |
162 | expect_flush_commit_position(mock_journaler, 0); | |
163 | expect_stop_append(mock_journaler, 0); | |
164 | ||
165 | expect_shut_down_journaler(mock_journaler, 0); | |
166 | ||
167 | C_SaferCond ctx; | |
168 | auto req = MockPromoteRequest::create(&mock_image_ctx, false, &ctx); | |
169 | req->send(); | |
170 | ASSERT_EQ(0, ctx.wait()); | |
171 | } | |
172 | ||
173 | TEST_F(TestMockJournalPromoteRequest, SuccessForced) { | |
174 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
175 | ||
176 | librbd::ImageCtx *ictx; | |
177 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
178 | ||
179 | MockTestImageCtx mock_image_ctx(*ictx); | |
180 | ::journal::MockJournaler mock_journaler; | |
181 | MockOpenRequest mock_open_request; | |
182 | ||
183 | expect_op_work_queue(mock_image_ctx); | |
184 | ||
185 | InSequence seq; | |
186 | expect_construct_journaler(mock_journaler); | |
187 | expect_open_journaler(mock_image_ctx, mock_open_request, 0); | |
188 | expect_allocate_tag(mock_journaler, | |
189 | {Journal<>::LOCAL_MIRROR_UUID, true, 567, 0}, 0); | |
190 | ||
191 | ::journal::MockFuture mock_future; | |
192 | expect_start_append(mock_journaler); | |
193 | expect_append_journaler(mock_journaler); | |
194 | expect_future_flush(mock_future, 0); | |
195 | expect_future_committed(mock_journaler); | |
196 | expect_flush_commit_position(mock_journaler, 0); | |
197 | expect_stop_append(mock_journaler, 0); | |
198 | ||
199 | expect_shut_down_journaler(mock_journaler, 0); | |
200 | ||
201 | C_SaferCond ctx; | |
202 | auto req = MockPromoteRequest::create(&mock_image_ctx, true, &ctx); | |
203 | req->send(); | |
204 | ASSERT_EQ(0, ctx.wait()); | |
205 | } | |
206 | ||
207 | TEST_F(TestMockJournalPromoteRequest, OpenError) { | |
208 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
209 | ||
210 | librbd::ImageCtx *ictx; | |
211 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
212 | ||
213 | MockTestImageCtx mock_image_ctx(*ictx); | |
214 | ::journal::MockJournaler mock_journaler; | |
215 | MockOpenRequest mock_open_request; | |
216 | ||
217 | expect_op_work_queue(mock_image_ctx); | |
218 | ||
219 | InSequence seq; | |
220 | expect_construct_journaler(mock_journaler); | |
221 | expect_open_journaler(mock_image_ctx, mock_open_request, -ENOENT); | |
222 | expect_shut_down_journaler(mock_journaler, -EINVAL); | |
223 | ||
224 | C_SaferCond ctx; | |
225 | auto req = MockPromoteRequest::create(&mock_image_ctx, false, &ctx); | |
226 | req->send(); | |
227 | ASSERT_EQ(-ENOENT, ctx.wait()); | |
228 | } | |
229 | ||
230 | TEST_F(TestMockJournalPromoteRequest, AllocateTagError) { | |
231 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
232 | ||
233 | librbd::ImageCtx *ictx; | |
234 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
235 | ||
236 | MockTestImageCtx mock_image_ctx(*ictx); | |
237 | ::journal::MockJournaler mock_journaler; | |
238 | MockOpenRequest mock_open_request; | |
239 | ||
240 | expect_op_work_queue(mock_image_ctx); | |
241 | ||
242 | InSequence seq; | |
243 | expect_construct_journaler(mock_journaler); | |
244 | expect_open_journaler(mock_image_ctx, mock_open_request, 0); | |
245 | expect_allocate_tag(mock_journaler, | |
246 | {Journal<>::LOCAL_MIRROR_UUID, true, 567, 0}, -EBADMSG); | |
247 | expect_shut_down_journaler(mock_journaler, -EINVAL); | |
248 | ||
249 | C_SaferCond ctx; | |
250 | auto req = MockPromoteRequest::create(&mock_image_ctx, true, &ctx); | |
251 | req->send(); | |
252 | ASSERT_EQ(-EBADMSG, ctx.wait()); | |
253 | } | |
254 | ||
255 | TEST_F(TestMockJournalPromoteRequest, AppendEventError) { | |
256 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
257 | ||
258 | librbd::ImageCtx *ictx; | |
259 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
260 | ||
261 | MockTestImageCtx mock_image_ctx(*ictx); | |
262 | ::journal::MockJournaler mock_journaler; | |
263 | MockOpenRequest mock_open_request; | |
264 | ||
265 | expect_op_work_queue(mock_image_ctx); | |
266 | ||
267 | InSequence seq; | |
268 | expect_construct_journaler(mock_journaler); | |
269 | expect_open_journaler(mock_image_ctx, mock_open_request, 0); | |
270 | expect_allocate_tag(mock_journaler, | |
271 | {Journal<>::ORPHAN_MIRROR_UUID, true, 567, 1}, 0); | |
272 | ||
273 | ::journal::MockFuture mock_future; | |
274 | expect_start_append(mock_journaler); | |
275 | expect_append_journaler(mock_journaler); | |
276 | expect_future_flush(mock_future, -EPERM); | |
277 | expect_stop_append(mock_journaler, 0); | |
278 | ||
279 | expect_shut_down_journaler(mock_journaler, 0); | |
280 | ||
281 | C_SaferCond ctx; | |
282 | auto req = MockPromoteRequest::create(&mock_image_ctx, false, &ctx); | |
283 | req->send(); | |
284 | ASSERT_EQ(-EPERM, ctx.wait()); | |
285 | } | |
286 | ||
287 | TEST_F(TestMockJournalPromoteRequest, CommitEventError) { | |
288 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
289 | ||
290 | librbd::ImageCtx *ictx; | |
291 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
292 | ||
293 | MockTestImageCtx mock_image_ctx(*ictx); | |
294 | ::journal::MockJournaler mock_journaler; | |
295 | MockOpenRequest mock_open_request; | |
296 | ||
297 | expect_op_work_queue(mock_image_ctx); | |
298 | ||
299 | InSequence seq; | |
300 | expect_construct_journaler(mock_journaler); | |
301 | expect_open_journaler(mock_image_ctx, mock_open_request, 0); | |
302 | expect_allocate_tag(mock_journaler, | |
303 | {Journal<>::ORPHAN_MIRROR_UUID, true, 567, 1}, 0); | |
304 | ||
305 | ::journal::MockFuture mock_future; | |
306 | expect_start_append(mock_journaler); | |
307 | expect_append_journaler(mock_journaler); | |
308 | expect_future_flush(mock_future, 0); | |
309 | expect_future_committed(mock_journaler); | |
310 | expect_flush_commit_position(mock_journaler, -EINVAL); | |
311 | expect_stop_append(mock_journaler, 0); | |
312 | ||
313 | expect_shut_down_journaler(mock_journaler, 0); | |
314 | ||
315 | C_SaferCond ctx; | |
316 | auto req = MockPromoteRequest::create(&mock_image_ctx, false, &ctx); | |
317 | req->send(); | |
318 | ASSERT_EQ(-EINVAL, ctx.wait()); | |
319 | } | |
320 | ||
321 | TEST_F(TestMockJournalPromoteRequest, ShutDownError) { | |
322 | REQUIRE_FEATURE(RBD_FEATURE_JOURNALING); | |
323 | ||
324 | librbd::ImageCtx *ictx; | |
325 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
326 | ||
327 | MockTestImageCtx mock_image_ctx(*ictx); | |
328 | ::journal::MockJournaler mock_journaler; | |
329 | MockOpenRequest mock_open_request; | |
330 | ||
331 | expect_op_work_queue(mock_image_ctx); | |
332 | ||
333 | InSequence seq; | |
334 | expect_construct_journaler(mock_journaler); | |
335 | expect_open_journaler(mock_image_ctx, mock_open_request, 0); | |
336 | expect_allocate_tag(mock_journaler, | |
337 | {Journal<>::LOCAL_MIRROR_UUID, true, 567, 0}, 0); | |
338 | ||
339 | ::journal::MockFuture mock_future; | |
340 | expect_start_append(mock_journaler); | |
341 | expect_append_journaler(mock_journaler); | |
342 | expect_future_flush(mock_future, 0); | |
343 | expect_future_committed(mock_journaler); | |
344 | expect_flush_commit_position(mock_journaler, 0); | |
345 | expect_stop_append(mock_journaler, 0); | |
346 | ||
347 | expect_shut_down_journaler(mock_journaler, -EINVAL); | |
348 | ||
349 | C_SaferCond ctx; | |
350 | auto req = MockPromoteRequest::create(&mock_image_ctx, true, &ctx); | |
351 | req->send(); | |
352 | ASSERT_EQ(-EINVAL, ctx.wait()); | |
353 | } | |
354 | ||
355 | } // namespace journal | |
356 | } // namespace librbd |