]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include "include/stringify.h" | |
5 | #include "test/librbd/test_mock_fixture.h" | |
6 | #include "test/librbd/test_support.h" | |
7 | #include "test/librbd/mock/MockImageCtx.h" | |
8 | #include "test/librbd/mock/MockOperations.h" | |
9 | #include "test/librados_test_stub/MockTestMemIoCtxImpl.h" | |
10 | #include "test/librados_test_stub/MockTestMemRadosClient.h" | |
11 | #include "librbd/mirror/snapshot/CreatePrimaryRequest.h" | |
12 | #include "librbd/mirror/snapshot/UnlinkPeerRequest.h" | |
13 | #include "librbd/mirror/snapshot/Utils.h" | |
14 | ||
15 | namespace librbd { | |
16 | ||
17 | namespace { | |
18 | ||
19 | struct MockTestImageCtx : public MockImageCtx { | |
20 | explicit MockTestImageCtx(librbd::ImageCtx& image_ctx) : MockImageCtx(image_ctx) { | |
21 | } | |
22 | }; | |
23 | ||
24 | } // anonymous namespace | |
25 | ||
26 | namespace mirror { | |
27 | namespace snapshot { | |
28 | namespace util { | |
29 | ||
30 | namespace { | |
31 | ||
32 | struct Mock { | |
33 | static Mock* s_instance; | |
34 | ||
35 | Mock() { | |
36 | s_instance = this; | |
37 | } | |
38 | ||
39 | MOCK_METHOD4(can_create_primary_snapshot, | |
40 | bool(librbd::MockTestImageCtx *, bool, bool, uint64_t *)); | |
41 | }; | |
42 | ||
43 | Mock *Mock::s_instance = nullptr; | |
44 | ||
45 | } // anonymous namespace | |
46 | ||
47 | template<> bool can_create_primary_snapshot(librbd::MockTestImageCtx *image_ctx, | |
48 | bool demoted, bool force, | |
49 | bool* requires_orphan, | |
50 | uint64_t *rollback_snap_id) { | |
51 | return Mock::s_instance->can_create_primary_snapshot(image_ctx, demoted, | |
52 | force, rollback_snap_id); | |
53 | } | |
54 | ||
55 | } // namespace util | |
56 | ||
57 | template <> | |
58 | struct UnlinkPeerRequest<MockTestImageCtx> { | |
59 | uint64_t snap_id = CEPH_NOSNAP; | |
60 | std::string mirror_peer_uuid; | |
61 | Context* on_finish = nullptr; | |
62 | static UnlinkPeerRequest* s_instance; | |
63 | static UnlinkPeerRequest *create(MockTestImageCtx *image_ctx, | |
64 | uint64_t snap_id, | |
65 | const std::string &mirror_peer_uuid, | |
66 | Context *on_finish) { | |
67 | ceph_assert(s_instance != nullptr); | |
68 | s_instance->snap_id = snap_id; | |
69 | s_instance->mirror_peer_uuid = mirror_peer_uuid; | |
70 | s_instance->on_finish = on_finish; | |
71 | return s_instance; | |
72 | } | |
73 | ||
74 | MOCK_METHOD0(send, void()); | |
75 | ||
76 | UnlinkPeerRequest() { | |
77 | s_instance = this; | |
78 | } | |
79 | }; | |
80 | ||
81 | UnlinkPeerRequest<MockTestImageCtx>* UnlinkPeerRequest<MockTestImageCtx>::s_instance = nullptr; | |
82 | ||
83 | } // namespace snapshot | |
84 | } // namespace mirror | |
85 | } // namespace librbd | |
86 | ||
87 | // template definitions | |
88 | #include "librbd/mirror/snapshot/CreatePrimaryRequest.cc" | |
89 | template class librbd::mirror::snapshot::CreatePrimaryRequest<librbd::MockTestImageCtx>; | |
90 | ||
91 | namespace librbd { | |
92 | namespace mirror { | |
93 | namespace snapshot { | |
94 | ||
95 | using ::testing::_; | |
96 | using ::testing::DoAll; | |
97 | using ::testing::InSequence; | |
98 | using ::testing::Invoke; | |
99 | using ::testing::Return; | |
100 | using ::testing::StrEq; | |
101 | using ::testing::WithArg; | |
102 | ||
103 | class TestMockMirrorSnapshotCreatePrimaryRequest : public TestMockFixture { | |
104 | public: | |
105 | typedef CreatePrimaryRequest<MockTestImageCtx> MockCreatePrimaryRequest; | |
106 | typedef UnlinkPeerRequest<MockTestImageCtx> MockUnlinkPeerRequest; | |
107 | typedef util::Mock MockUtils; | |
108 | ||
109 | uint64_t m_snap_seq = 0; | |
110 | ||
111 | void snap_create(MockTestImageCtx &mock_image_ctx, | |
112 | const cls::rbd::SnapshotNamespace &ns, | |
113 | const std::string& snap_name) { | |
114 | ASSERT_TRUE(mock_image_ctx.snap_info.insert( | |
115 | {m_snap_seq++, | |
116 | SnapInfo{snap_name, ns, 0, {}, 0, 0, {}}}).second); | |
117 | } | |
118 | ||
119 | void expect_clone_md_ctx(MockTestImageCtx &mock_image_ctx) { | |
120 | EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx), clone()) | |
121 | .WillOnce(Invoke([&mock_image_ctx]() { | |
122 | get_mock_io_ctx(mock_image_ctx.md_ctx).get(); | |
123 | return &get_mock_io_ctx(mock_image_ctx.md_ctx); | |
124 | })); | |
125 | } | |
126 | ||
127 | void expect_can_create_primary_snapshot(MockUtils &mock_utils, bool demoted, | |
128 | bool force, bool result) { | |
129 | EXPECT_CALL(mock_utils, | |
130 | can_create_primary_snapshot(_, demoted, force, nullptr)) | |
131 | .WillOnce(Return(result)); | |
132 | } | |
133 | ||
134 | void expect_get_mirror_peers(MockTestImageCtx &mock_image_ctx, | |
135 | const std::vector<cls::rbd::MirrorPeer> &peers, | |
136 | int r) { | |
137 | using ceph::encode; | |
138 | bufferlist bl; | |
139 | encode(peers, bl); | |
140 | ||
141 | EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx), | |
142 | exec(RBD_MIRRORING, _, StrEq("rbd"), StrEq("mirror_peer_list"), | |
f67539c2 | 143 | _, _, _, _)) |
9f95a23c TL |
144 | .WillOnce(DoAll(WithArg<5>(CopyInBufferlist(bl)), |
145 | Return(r))); | |
146 | } | |
147 | ||
148 | void expect_create_snapshot(MockTestImageCtx &mock_image_ctx, int r) { | |
f67539c2 | 149 | EXPECT_CALL(*mock_image_ctx.operations, snap_create(_, _, _, _, _)) |
9f95a23c TL |
150 | .WillOnce(DoAll( |
151 | Invoke([this, &mock_image_ctx, r]( | |
152 | const cls::rbd::SnapshotNamespace &ns, | |
153 | const std::string& snap_name, | |
f67539c2 TL |
154 | uint64_t flags, |
155 | ProgressContext &prog_ctx, | |
9f95a23c TL |
156 | Context *on_finish) { |
157 | if (r != 0) { | |
158 | return; | |
159 | } | |
160 | snap_create(mock_image_ctx, ns, snap_name); | |
161 | }), | |
f67539c2 | 162 | WithArg<4>(CompleteContext( |
9f95a23c TL |
163 | r, mock_image_ctx.image_ctx->op_work_queue)) |
164 | )); | |
165 | } | |
166 | ||
167 | void expect_unlink_peer(MockTestImageCtx &mock_image_ctx, | |
168 | MockUnlinkPeerRequest &mock_unlink_peer_request, | |
169 | uint64_t snap_id, const std::string &peer_uuid, | |
170 | int r) { | |
171 | EXPECT_CALL(mock_unlink_peer_request, send()) | |
172 | .WillOnce(Invoke([&mock_image_ctx, &mock_unlink_peer_request, snap_id, | |
173 | peer_uuid, r]() { | |
174 | ASSERT_EQ(mock_unlink_peer_request.mirror_peer_uuid, | |
175 | peer_uuid); | |
176 | ASSERT_EQ(mock_unlink_peer_request.snap_id, snap_id); | |
177 | if (r == 0) { | |
178 | auto it = mock_image_ctx.snap_info.find(snap_id); | |
179 | ASSERT_NE(it, mock_image_ctx.snap_info.end()); | |
180 | auto info = | |
181 | boost::get<cls::rbd::MirrorSnapshotNamespace>( | |
182 | &it->second.snap_namespace); | |
183 | ASSERT_NE(nullptr, info); | |
184 | ASSERT_NE(0, info->mirror_peer_uuids.erase( | |
185 | peer_uuid)); | |
186 | if (info->mirror_peer_uuids.empty()) { | |
187 | mock_image_ctx.snap_info.erase(it); | |
188 | } | |
189 | } | |
190 | mock_image_ctx.image_ctx->op_work_queue->queue( | |
191 | mock_unlink_peer_request.on_finish, r); | |
192 | })); | |
193 | } | |
194 | }; | |
195 | ||
196 | TEST_F(TestMockMirrorSnapshotCreatePrimaryRequest, Success) { | |
197 | REQUIRE_FORMAT_V2(); | |
198 | ||
199 | librbd::ImageCtx *ictx; | |
200 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
201 | ||
202 | MockTestImageCtx mock_image_ctx(*ictx); | |
203 | ||
204 | InSequence seq; | |
205 | ||
206 | expect_clone_md_ctx(mock_image_ctx); | |
207 | MockUtils mock_utils; | |
208 | expect_can_create_primary_snapshot(mock_utils, false, false, true); | |
209 | expect_get_mirror_peers(mock_image_ctx, | |
210 | {{"uuid", cls::rbd::MIRROR_PEER_DIRECTION_TX, "ceph", | |
211 | "mirror", "mirror uuid"}}, 0); | |
212 | expect_create_snapshot(mock_image_ctx, 0); | |
213 | ||
214 | C_SaferCond ctx; | |
1911f103 | 215 | auto req = new MockCreatePrimaryRequest(&mock_image_ctx, "gid", CEPH_NOSNAP, |
f67539c2 | 216 | 0U, 0U, nullptr, &ctx); |
9f95a23c TL |
217 | req->send(); |
218 | ASSERT_EQ(0, ctx.wait()); | |
219 | } | |
220 | ||
221 | TEST_F(TestMockMirrorSnapshotCreatePrimaryRequest, CanNotError) { | |
222 | REQUIRE_FORMAT_V2(); | |
223 | ||
224 | librbd::ImageCtx *ictx; | |
225 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
226 | ||
227 | MockTestImageCtx mock_image_ctx(*ictx); | |
228 | ||
229 | InSequence seq; | |
230 | ||
231 | expect_clone_md_ctx(mock_image_ctx); | |
232 | MockUtils mock_utils; | |
233 | expect_can_create_primary_snapshot(mock_utils, false, false, false); | |
234 | ||
235 | C_SaferCond ctx; | |
1911f103 | 236 | auto req = new MockCreatePrimaryRequest(&mock_image_ctx, "gid", CEPH_NOSNAP, |
f67539c2 | 237 | 0U, 0U, nullptr, &ctx); |
9f95a23c TL |
238 | req->send(); |
239 | ASSERT_EQ(-EINVAL, ctx.wait()); | |
240 | } | |
241 | ||
242 | TEST_F(TestMockMirrorSnapshotCreatePrimaryRequest, GetMirrorPeersError) { | |
243 | REQUIRE_FORMAT_V2(); | |
244 | ||
245 | librbd::ImageCtx *ictx; | |
246 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
247 | ||
248 | MockTestImageCtx mock_image_ctx(*ictx); | |
249 | ||
250 | InSequence seq; | |
251 | ||
252 | expect_clone_md_ctx(mock_image_ctx); | |
253 | MockUtils mock_utils; | |
254 | expect_can_create_primary_snapshot(mock_utils, false, false, true); | |
255 | expect_get_mirror_peers(mock_image_ctx, | |
256 | {{"uuid", cls::rbd::MIRROR_PEER_DIRECTION_TX, "ceph", | |
257 | "mirror", "mirror uuid"}}, -EINVAL); | |
258 | ||
259 | C_SaferCond ctx; | |
1911f103 | 260 | auto req = new MockCreatePrimaryRequest(&mock_image_ctx, "gid", CEPH_NOSNAP, |
f67539c2 | 261 | 0U, 0U, nullptr, &ctx); |
9f95a23c TL |
262 | req->send(); |
263 | ASSERT_EQ(-EINVAL, ctx.wait()); | |
264 | } | |
265 | ||
266 | TEST_F(TestMockMirrorSnapshotCreatePrimaryRequest, CreateSnapshotError) { | |
267 | REQUIRE_FORMAT_V2(); | |
268 | ||
269 | librbd::ImageCtx *ictx; | |
270 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
271 | ||
272 | MockTestImageCtx mock_image_ctx(*ictx); | |
273 | ||
274 | InSequence seq; | |
275 | ||
276 | expect_clone_md_ctx(mock_image_ctx); | |
277 | MockUtils mock_utils; | |
278 | expect_can_create_primary_snapshot(mock_utils, false, false, true); | |
279 | expect_get_mirror_peers(mock_image_ctx, | |
280 | {{"uuid", cls::rbd::MIRROR_PEER_DIRECTION_TX, "ceph", | |
281 | "mirror", "mirror uuid"}}, 0); | |
282 | expect_create_snapshot(mock_image_ctx, -EINVAL); | |
283 | ||
284 | C_SaferCond ctx; | |
1911f103 | 285 | auto req = new MockCreatePrimaryRequest(&mock_image_ctx, "gid", CEPH_NOSNAP, |
f67539c2 | 286 | 0U, 0U, nullptr, &ctx); |
9f95a23c TL |
287 | req->send(); |
288 | ASSERT_EQ(-EINVAL, ctx.wait()); | |
289 | } | |
290 | ||
291 | TEST_F(TestMockMirrorSnapshotCreatePrimaryRequest, SuccessUnlinkPeer) { | |
292 | REQUIRE_FORMAT_V2(); | |
293 | ||
294 | librbd::ImageCtx *ictx; | |
295 | ASSERT_EQ(0, open_image(m_image_name, &ictx)); | |
296 | ictx->config.set_val("conf_rbd_mirroring_max_mirroring_snapshots", "3"); | |
297 | ||
298 | MockTestImageCtx mock_image_ctx(*ictx); | |
299 | for (int i = 0; i < 3; i++) { | |
300 | cls::rbd::MirrorSnapshotNamespace ns{ | |
301 | cls::rbd::MIRROR_SNAPSHOT_STATE_PRIMARY, {"uuid"}, "", CEPH_NOSNAP}; | |
302 | snap_create(mock_image_ctx, ns, "mirror_snap"); | |
303 | } | |
304 | ||
305 | InSequence seq; | |
306 | ||
307 | expect_clone_md_ctx(mock_image_ctx); | |
308 | MockUtils mock_utils; | |
309 | expect_can_create_primary_snapshot(mock_utils, false, false, true); | |
310 | expect_get_mirror_peers(mock_image_ctx, | |
311 | {{"uuid", cls::rbd::MIRROR_PEER_DIRECTION_TX, "ceph", | |
312 | "mirror", "mirror uuid"}}, 0); | |
313 | expect_create_snapshot(mock_image_ctx, 0); | |
314 | MockUnlinkPeerRequest mock_unlink_peer_request; | |
315 | auto it = mock_image_ctx.snap_info.rbegin(); | |
f67539c2 | 316 | auto snap_id = it->first; |
9f95a23c TL |
317 | expect_unlink_peer(mock_image_ctx, mock_unlink_peer_request, snap_id, "uuid", |
318 | 0); | |
319 | C_SaferCond ctx; | |
1911f103 | 320 | auto req = new MockCreatePrimaryRequest(&mock_image_ctx, "gid", CEPH_NOSNAP, |
f67539c2 | 321 | 0U, 0U, nullptr, &ctx); |
9f95a23c TL |
322 | req->send(); |
323 | ASSERT_EQ(0, ctx.wait()); | |
324 | } | |
325 | ||
326 | } // namespace snapshot | |
327 | } // namespace mirror | |
328 | } // namespace librbd | |
329 |