]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/rbd_mirror/image_sync/test_mock_SyncPointCreateRequest.cc
bump version to 19.2.0-pve1
[ceph.git] / ceph / src / test / rbd_mirror / image_sync / test_mock_SyncPointCreateRequest.cc
CommitLineData
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/rbd_mirror/test_mock_fixture.h"
5#include "include/rbd/librbd.hpp"
7c673cae
FG
6#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
7#include "test/librbd/mock/MockImageCtx.h"
9f95a23c 8#include "test/rbd_mirror/mock/image_sync/MockSyncPointHandler.h"
7c673cae
FG
9#include "tools/rbd_mirror/image_sync/SyncPointCreateRequest.h"
10
11namespace librbd {
12
13namespace {
14
15struct MockTestImageCtx : public librbd::MockImageCtx {
11fdf7f2 16 explicit MockTestImageCtx(librbd::ImageCtx &image_ctx)
7c673cae
FG
17 : librbd::MockImageCtx(image_ctx) {
18 }
19};
20
21} // anonymous namespace
22
7c673cae
FG
23} // namespace librbd
24
25// template definitions
26#include "tools/rbd_mirror/image_sync/SyncPointCreateRequest.cc"
7c673cae
FG
27
28namespace rbd {
29namespace mirror {
30namespace image_sync {
31
32using ::testing::_;
9f95a23c 33using ::testing::DoAll;
7c673cae 34using ::testing::InSequence;
9f95a23c
TL
35using ::testing::Invoke;
36using ::testing::Return;
7c673cae
FG
37using ::testing::WithArg;
38
39class TestMockImageSyncSyncPointCreateRequest : public TestMockFixture {
40public:
41 typedef SyncPointCreateRequest<librbd::MockTestImageCtx> MockSyncPointCreateRequest;
42
43 void SetUp() override {
44 TestMockFixture::SetUp();
45
46 librbd::RBD rbd;
47 ASSERT_EQ(0, create_image(rbd, m_remote_io_ctx, m_image_name, m_image_size));
48 ASSERT_EQ(0, open_image(m_remote_io_ctx, m_image_name, &m_remote_image_ctx));
49 }
50
9f95a23c
TL
51 void expect_get_snap_seqs(MockSyncPointHandler& mock_sync_point_handler) {
52 EXPECT_CALL(mock_sync_point_handler, get_snap_seqs())
53 .WillRepeatedly(Return(librbd::SnapSeqs{}));
54 }
55
56 void expect_get_sync_points(MockSyncPointHandler& mock_sync_point_handler) {
57 EXPECT_CALL(mock_sync_point_handler, get_sync_points())
58 .WillRepeatedly(Invoke([this]() {
59 return m_sync_points;
60 }));
61 }
62
63 void expect_update_sync_points(MockSyncPointHandler& mock_sync_point_handler,
64 int r) {
65 EXPECT_CALL(mock_sync_point_handler, update_sync_points(_, _, false, _))
66 .WillOnce(DoAll(WithArg<1>(Invoke([this, r](const SyncPoints& sync_points) {
67 if (r >= 0) {
68 m_sync_points = sync_points;
69 }
70 })),
71 WithArg<3>(CompleteContext(r))));
7c673cae
FG
72 }
73
74 void expect_image_refresh(librbd::MockTestImageCtx &mock_remote_image_ctx, int r) {
75 EXPECT_CALL(*mock_remote_image_ctx.state, refresh(_))
76 .WillOnce(CompleteContext(r));
77 }
78
79 void expect_snap_create(librbd::MockTestImageCtx &mock_remote_image_ctx, int r) {
f67539c2
TL
80 EXPECT_CALL(*mock_remote_image_ctx.operations, snap_create(_, _, _, _, _))
81 .WillOnce(WithArg<4>(CompleteContext(r)));
7c673cae
FG
82 }
83
84 MockSyncPointCreateRequest *create_request(librbd::MockTestImageCtx &mock_remote_image_ctx,
9f95a23c 85 MockSyncPointHandler& mock_sync_point_handler,
7c673cae
FG
86 Context *ctx) {
87 return new MockSyncPointCreateRequest(&mock_remote_image_ctx, "uuid",
9f95a23c 88 &mock_sync_point_handler, ctx);
7c673cae
FG
89 }
90
91 librbd::ImageCtx *m_remote_image_ctx;
9f95a23c 92 SyncPoints m_sync_points;
7c673cae
FG
93};
94
95TEST_F(TestMockImageSyncSyncPointCreateRequest, Success) {
96 librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx);
9f95a23c
TL
97 MockSyncPointHandler mock_sync_point_handler;
98
99 expect_get_snap_seqs(mock_sync_point_handler);
100 expect_get_sync_points(mock_sync_point_handler);
7c673cae
FG
101
102 InSequence seq;
9f95a23c 103 expect_update_sync_points(mock_sync_point_handler, 0);
7c673cae
FG
104 expect_image_refresh(mock_remote_image_ctx, 0);
105 expect_snap_create(mock_remote_image_ctx, 0);
106 expect_image_refresh(mock_remote_image_ctx, 0);
107
108 C_SaferCond ctx;
109 MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx,
9f95a23c
TL
110 mock_sync_point_handler,
111 &ctx);
7c673cae
FG
112 req->send();
113 ASSERT_EQ(0, ctx.wait());
114
9f95a23c 115 ASSERT_EQ(1U, m_sync_points.size());
7c673cae
FG
116}
117
118TEST_F(TestMockImageSyncSyncPointCreateRequest, ResyncSuccess) {
9f95a23c
TL
119 m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "start snap",
120 "", boost::none);
121 auto sync_point = m_sync_points.front();
7c673cae
FG
122
123 librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx);
9f95a23c
TL
124 MockSyncPointHandler mock_sync_point_handler;
125
126 expect_get_snap_seqs(mock_sync_point_handler);
127 expect_get_sync_points(mock_sync_point_handler);
7c673cae
FG
128
129 InSequence seq;
9f95a23c 130 expect_update_sync_points(mock_sync_point_handler, 0);
7c673cae
FG
131 expect_image_refresh(mock_remote_image_ctx, 0);
132 expect_snap_create(mock_remote_image_ctx, 0);
133 expect_image_refresh(mock_remote_image_ctx, 0);
134
135 C_SaferCond ctx;
136 MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx,
9f95a23c
TL
137 mock_sync_point_handler,
138 &ctx);
7c673cae
FG
139 req->send();
140 ASSERT_EQ(0, ctx.wait());
141
9f95a23c
TL
142 ASSERT_EQ(2U, m_sync_points.size());
143 ASSERT_EQ(sync_point, m_sync_points.front());
144 ASSERT_EQ("start snap", m_sync_points.back().from_snap_name);
7c673cae
FG
145}
146
147TEST_F(TestMockImageSyncSyncPointCreateRequest, SnapshotExists) {
148 librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx);
9f95a23c
TL
149 MockSyncPointHandler mock_sync_point_handler;
150
151 expect_get_snap_seqs(mock_sync_point_handler);
152 expect_get_sync_points(mock_sync_point_handler);
7c673cae
FG
153
154 InSequence seq;
9f95a23c 155 expect_update_sync_points(mock_sync_point_handler, 0);
7c673cae
FG
156 expect_image_refresh(mock_remote_image_ctx, 0);
157 expect_snap_create(mock_remote_image_ctx, -EEXIST);
9f95a23c 158 expect_update_sync_points(mock_sync_point_handler, 0);
7c673cae
FG
159 expect_image_refresh(mock_remote_image_ctx, 0);
160 expect_snap_create(mock_remote_image_ctx, 0);
161 expect_image_refresh(mock_remote_image_ctx, 0);
162
163 C_SaferCond ctx;
164 MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx,
9f95a23c
TL
165 mock_sync_point_handler,
166 &ctx);
7c673cae
FG
167 req->send();
168 ASSERT_EQ(0, ctx.wait());
169
9f95a23c 170 ASSERT_EQ(1U, m_sync_points.size());
7c673cae
FG
171}
172
173TEST_F(TestMockImageSyncSyncPointCreateRequest, ClientUpdateError) {
174 librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx);
9f95a23c
TL
175 MockSyncPointHandler mock_sync_point_handler;
176
177 expect_get_snap_seqs(mock_sync_point_handler);
178 expect_get_sync_points(mock_sync_point_handler);
7c673cae
FG
179
180 InSequence seq;
9f95a23c 181 expect_update_sync_points(mock_sync_point_handler, -EINVAL);
7c673cae
FG
182
183 C_SaferCond ctx;
184 MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx,
9f95a23c
TL
185 mock_sync_point_handler,
186 &ctx);
7c673cae
FG
187 req->send();
188 ASSERT_EQ(-EINVAL, ctx.wait());
189
9f95a23c 190 ASSERT_TRUE(m_sync_points.empty());
7c673cae
FG
191}
192
193} // namespace image_sync
194} // namespace mirror
195} // namespace rbd