From 19fcec84d8d7d21e796c7624e521b60d28ee21ed Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:45:59 +0200 Subject: Adding upstream version 16.2.11+ds. Signed-off-by: Daniel Baumann --- .../image_sync/test_mock_SyncPointCreateRequest.cc | 195 ++++++++++++ .../image_sync/test_mock_SyncPointPruneRequest.cc | 347 +++++++++++++++++++++ 2 files changed, 542 insertions(+) create mode 100644 src/test/rbd_mirror/image_sync/test_mock_SyncPointCreateRequest.cc create mode 100644 src/test/rbd_mirror/image_sync/test_mock_SyncPointPruneRequest.cc (limited to 'src/test/rbd_mirror/image_sync') diff --git a/src/test/rbd_mirror/image_sync/test_mock_SyncPointCreateRequest.cc b/src/test/rbd_mirror/image_sync/test_mock_SyncPointCreateRequest.cc new file mode 100644 index 000000000..9a4d920f8 --- /dev/null +++ b/src/test/rbd_mirror/image_sync/test_mock_SyncPointCreateRequest.cc @@ -0,0 +1,195 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "test/rbd_mirror/test_mock_fixture.h" +#include "include/rbd/librbd.hpp" +#include "test/librados_test_stub/MockTestMemIoCtxImpl.h" +#include "test/librbd/mock/MockImageCtx.h" +#include "test/rbd_mirror/mock/image_sync/MockSyncPointHandler.h" +#include "tools/rbd_mirror/image_sync/SyncPointCreateRequest.h" + +namespace librbd { + +namespace { + +struct MockTestImageCtx : public librbd::MockImageCtx { + explicit MockTestImageCtx(librbd::ImageCtx &image_ctx) + : librbd::MockImageCtx(image_ctx) { + } +}; + +} // anonymous namespace + +} // namespace librbd + +// template definitions +#include "tools/rbd_mirror/image_sync/SyncPointCreateRequest.cc" + +namespace rbd { +namespace mirror { +namespace image_sync { + +using ::testing::_; +using ::testing::DoAll; +using ::testing::InSequence; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::WithArg; + +class TestMockImageSyncSyncPointCreateRequest : public TestMockFixture { +public: + typedef SyncPointCreateRequest MockSyncPointCreateRequest; + + void SetUp() override { + TestMockFixture::SetUp(); + + librbd::RBD rbd; + ASSERT_EQ(0, create_image(rbd, m_remote_io_ctx, m_image_name, m_image_size)); + ASSERT_EQ(0, open_image(m_remote_io_ctx, m_image_name, &m_remote_image_ctx)); + } + + void expect_get_snap_seqs(MockSyncPointHandler& mock_sync_point_handler) { + EXPECT_CALL(mock_sync_point_handler, get_snap_seqs()) + .WillRepeatedly(Return(librbd::SnapSeqs{})); + } + + void expect_get_sync_points(MockSyncPointHandler& mock_sync_point_handler) { + EXPECT_CALL(mock_sync_point_handler, get_sync_points()) + .WillRepeatedly(Invoke([this]() { + return m_sync_points; + })); + } + + void expect_update_sync_points(MockSyncPointHandler& mock_sync_point_handler, + int r) { + EXPECT_CALL(mock_sync_point_handler, update_sync_points(_, _, false, _)) + .WillOnce(DoAll(WithArg<1>(Invoke([this, r](const SyncPoints& sync_points) { + if (r >= 0) { + m_sync_points = sync_points; + } + })), + WithArg<3>(CompleteContext(r)))); + } + + void expect_image_refresh(librbd::MockTestImageCtx &mock_remote_image_ctx, int r) { + EXPECT_CALL(*mock_remote_image_ctx.state, refresh(_)) + .WillOnce(CompleteContext(r)); + } + + void expect_snap_create(librbd::MockTestImageCtx &mock_remote_image_ctx, int r) { + EXPECT_CALL(*mock_remote_image_ctx.operations, snap_create(_, _, _, _, _)) + .WillOnce(WithArg<4>(CompleteContext(r))); + } + + MockSyncPointCreateRequest *create_request(librbd::MockTestImageCtx &mock_remote_image_ctx, + MockSyncPointHandler& mock_sync_point_handler, + Context *ctx) { + return new MockSyncPointCreateRequest(&mock_remote_image_ctx, "uuid", + &mock_sync_point_handler, ctx); + } + + librbd::ImageCtx *m_remote_image_ctx; + SyncPoints m_sync_points; +}; + +TEST_F(TestMockImageSyncSyncPointCreateRequest, Success) { + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_update_sync_points(mock_sync_point_handler, 0); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_snap_create(mock_remote_image_ctx, 0); + expect_image_refresh(mock_remote_image_ctx, 0); + + C_SaferCond ctx; + MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + + ASSERT_EQ(1U, m_sync_points.size()); +} + +TEST_F(TestMockImageSyncSyncPointCreateRequest, ResyncSuccess) { + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "start snap", + "", boost::none); + auto sync_point = m_sync_points.front(); + + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_update_sync_points(mock_sync_point_handler, 0); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_snap_create(mock_remote_image_ctx, 0); + expect_image_refresh(mock_remote_image_ctx, 0); + + C_SaferCond ctx; + MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + + ASSERT_EQ(2U, m_sync_points.size()); + ASSERT_EQ(sync_point, m_sync_points.front()); + ASSERT_EQ("start snap", m_sync_points.back().from_snap_name); +} + +TEST_F(TestMockImageSyncSyncPointCreateRequest, SnapshotExists) { + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_update_sync_points(mock_sync_point_handler, 0); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_snap_create(mock_remote_image_ctx, -EEXIST); + expect_update_sync_points(mock_sync_point_handler, 0); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_snap_create(mock_remote_image_ctx, 0); + expect_image_refresh(mock_remote_image_ctx, 0); + + C_SaferCond ctx; + MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + + ASSERT_EQ(1U, m_sync_points.size()); +} + +TEST_F(TestMockImageSyncSyncPointCreateRequest, ClientUpdateError) { + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_update_sync_points(mock_sync_point_handler, -EINVAL); + + C_SaferCond ctx; + MockSyncPointCreateRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + &ctx); + req->send(); + ASSERT_EQ(-EINVAL, ctx.wait()); + + ASSERT_TRUE(m_sync_points.empty()); +} + +} // namespace image_sync +} // namespace mirror +} // namespace rbd diff --git a/src/test/rbd_mirror/image_sync/test_mock_SyncPointPruneRequest.cc b/src/test/rbd_mirror/image_sync/test_mock_SyncPointPruneRequest.cc new file mode 100644 index 000000000..bd13f3cd0 --- /dev/null +++ b/src/test/rbd_mirror/image_sync/test_mock_SyncPointPruneRequest.cc @@ -0,0 +1,347 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab + +#include "test/rbd_mirror/test_mock_fixture.h" +#include "include/rbd/librbd.hpp" +#include "test/librados_test_stub/MockTestMemIoCtxImpl.h" +#include "test/librbd/mock/MockImageCtx.h" +#include "test/rbd_mirror/mock/image_sync/MockSyncPointHandler.h" +#include "tools/rbd_mirror/image_sync/SyncPointPruneRequest.h" + +namespace librbd { + +namespace { + +struct MockTestImageCtx : public librbd::MockImageCtx { + explicit MockTestImageCtx(librbd::ImageCtx &image_ctx) + : librbd::MockImageCtx(image_ctx) { + } +}; + +} // anonymous namespace + +} // namespace librbd + +// template definitions +#include "tools/rbd_mirror/image_sync/SyncPointPruneRequest.cc" +template class rbd::mirror::image_sync::SyncPointPruneRequest; + +namespace rbd { +namespace mirror { +namespace image_sync { + +using ::testing::_; +using ::testing::DoAll; +using ::testing::InSequence; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::StrEq; +using ::testing::WithArg; + +class TestMockImageSyncSyncPointPruneRequest : public TestMockFixture { +public: + typedef SyncPointPruneRequest MockSyncPointPruneRequest; + + void SetUp() override { + TestMockFixture::SetUp(); + + librbd::RBD rbd; + ASSERT_EQ(0, create_image(rbd, m_remote_io_ctx, m_image_name, m_image_size)); + ASSERT_EQ(0, open_image(m_remote_io_ctx, m_image_name, &m_remote_image_ctx)); + } + + void expect_get_snap_seqs(MockSyncPointHandler& mock_sync_point_handler) { + EXPECT_CALL(mock_sync_point_handler, get_snap_seqs()) + .WillRepeatedly(Return(librbd::SnapSeqs{})); + } + + void expect_get_sync_points(MockSyncPointHandler& mock_sync_point_handler) { + EXPECT_CALL(mock_sync_point_handler, get_sync_points()) + .WillRepeatedly(Invoke([this]() { + return m_sync_points; + })); + } + + void expect_update_sync_points(MockSyncPointHandler& mock_sync_point_handler, + bool complete, int r) { + EXPECT_CALL(mock_sync_point_handler, update_sync_points(_, _, complete, _)) + .WillOnce(DoAll(WithArg<1>(Invoke([this, r](const SyncPoints& sync_points) { + if (r >= 0) { + m_sync_points = sync_points; + } + })), + WithArg<3>(CompleteContext(r)))); + } + + void expect_get_snap_id(librbd::MockTestImageCtx &mock_remote_image_ctx, + const std::string &snap_name, uint64_t snap_id) { + EXPECT_CALL(mock_remote_image_ctx, get_snap_id(_, StrEq(snap_name))) + .WillOnce(Return(snap_id)); + } + + void expect_image_refresh(librbd::MockTestImageCtx &mock_remote_image_ctx, int r) { + EXPECT_CALL(*mock_remote_image_ctx.state, refresh(_)) + .WillOnce(CompleteContext(r)); + } + + void expect_snap_remove(librbd::MockTestImageCtx &mock_remote_image_ctx, + const std::string &snap_name, int r) { + EXPECT_CALL(*mock_remote_image_ctx.operations, snap_remove(_, StrEq(snap_name), _)) + .WillOnce(WithArg<2>(CompleteContext(r))); + } + + MockSyncPointPruneRequest *create_request(librbd::MockTestImageCtx &mock_remote_image_ctx, + MockSyncPointHandler& mock_sync_point_handler, + bool sync_complete, Context *ctx) { + return new MockSyncPointPruneRequest(&mock_remote_image_ctx, sync_complete, + &mock_sync_point_handler, ctx); + } + + librbd::ImageCtx *m_remote_image_ctx; + SyncPoints m_sync_points; +}; + +TEST_F(TestMockImageSyncSyncPointPruneRequest, SyncInProgressSuccess) { + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap1", + "", boost::none); + auto sync_points = m_sync_points; + + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_get_snap_id(mock_remote_image_ctx, "snap1", 123); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_update_sync_points(mock_sync_point_handler, false, 0); + + C_SaferCond ctx; + MockSyncPointPruneRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + false, &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + ASSERT_EQ(sync_points, m_sync_points); +} + +TEST_F(TestMockImageSyncSyncPointPruneRequest, RestartedSyncInProgressSuccess) { + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap2", + "snap1", boost::none); + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap1", "", + boost::none); + auto sync_points = m_sync_points; + + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_get_snap_id(mock_remote_image_ctx, "snap1", 123); + expect_snap_remove(mock_remote_image_ctx, "snap2", 0); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_update_sync_points(mock_sync_point_handler, false, 0); + + C_SaferCond ctx; + MockSyncPointPruneRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + false, &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + + sync_points.pop_back(); + ASSERT_EQ(sync_points, m_sync_points); +} + +TEST_F(TestMockImageSyncSyncPointPruneRequest, SyncInProgressMissingSnapSuccess) { + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap2", + "snap1", boost::none); + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap1", "", + boost::none); + + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_get_snap_id(mock_remote_image_ctx, "snap1", CEPH_NOSNAP); + expect_snap_remove(mock_remote_image_ctx, "snap2", 0); + expect_snap_remove(mock_remote_image_ctx, "snap1", 0); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_update_sync_points(mock_sync_point_handler, false, 0); + + C_SaferCond ctx; + MockSyncPointPruneRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + false, &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + + ASSERT_EQ(SyncPoints{}, m_sync_points); +} + +TEST_F(TestMockImageSyncSyncPointPruneRequest, SyncInProgressUnexpectedFromSnapSuccess) { + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap2", + "snap1", boost::none); + + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_get_snap_id(mock_remote_image_ctx, "snap2", 124); + expect_snap_remove(mock_remote_image_ctx, "snap2", 0); + expect_snap_remove(mock_remote_image_ctx, "snap1", 0); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_update_sync_points(mock_sync_point_handler, false, 0); + + C_SaferCond ctx; + MockSyncPointPruneRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + false, &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + + ASSERT_EQ(SyncPoints(), m_sync_points); +} + +TEST_F(TestMockImageSyncSyncPointPruneRequest, SyncCompleteSuccess) { + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap1", + "", boost::none); + + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_snap_remove(mock_remote_image_ctx, "snap1", 0); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_update_sync_points(mock_sync_point_handler, true, 0); + + C_SaferCond ctx; + MockSyncPointPruneRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + true, &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + ASSERT_TRUE(m_sync_points.empty()); +} + +TEST_F(TestMockImageSyncSyncPointPruneRequest, RestartedSyncCompleteSuccess) { + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap2", + "snap1", boost::none); + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap1", + "", boost::none); + auto sync_points = m_sync_points; + + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_image_refresh(mock_remote_image_ctx, 0); + expect_update_sync_points(mock_sync_point_handler, true, 0); + + C_SaferCond ctx; + MockSyncPointPruneRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + true, &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + sync_points.pop_front(); + ASSERT_EQ(sync_points, m_sync_points); +} + +TEST_F(TestMockImageSyncSyncPointPruneRequest, RestartedCatchUpSyncCompleteSuccess) { + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap3", + "snap2", boost::none); + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap2", + "snap1", boost::none); + auto sync_points = m_sync_points; + + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_snap_remove(mock_remote_image_ctx, "snap1", 0); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_update_sync_points(mock_sync_point_handler, true, 0); + + C_SaferCond ctx; + MockSyncPointPruneRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + true, &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + sync_points.pop_front(); + ASSERT_EQ(sync_points, m_sync_points); +} + +TEST_F(TestMockImageSyncSyncPointPruneRequest, SnapshotDNE) { + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap1", + "", boost::none); + + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_snap_remove(mock_remote_image_ctx, "snap1", -ENOENT); + expect_image_refresh(mock_remote_image_ctx, 0); + expect_update_sync_points(mock_sync_point_handler, true, 0); + + C_SaferCond ctx; + MockSyncPointPruneRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + true, &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + ASSERT_TRUE(m_sync_points.empty()); +} + +TEST_F(TestMockImageSyncSyncPointPruneRequest, ClientUpdateError) { + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap2", + "snap1", boost::none); + m_sync_points.emplace_front(cls::rbd::UserSnapshotNamespace(), "snap1", + "", boost::none); + auto sync_points = m_sync_points; + + librbd::MockTestImageCtx mock_remote_image_ctx(*m_remote_image_ctx); + MockSyncPointHandler mock_sync_point_handler; + + expect_get_snap_seqs(mock_sync_point_handler); + expect_get_sync_points(mock_sync_point_handler); + + InSequence seq; + expect_image_refresh(mock_remote_image_ctx, 0); + expect_update_sync_points(mock_sync_point_handler, true, -EINVAL); + + C_SaferCond ctx; + MockSyncPointPruneRequest *req = create_request(mock_remote_image_ctx, + mock_sync_point_handler, + true, &ctx); + req->send(); + ASSERT_EQ(-EINVAL, ctx.wait()); + + ASSERT_EQ(sync_points, m_sync_points); +} + +} // namespace image_sync +} // namespace mirror +} // namespace rbd -- cgit v1.2.3