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 --- .../exclusive_lock/test_mock_PreAcquireRequest.cc | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/test/librbd/exclusive_lock/test_mock_PreAcquireRequest.cc (limited to 'src/test/librbd/exclusive_lock/test_mock_PreAcquireRequest.cc') diff --git a/src/test/librbd/exclusive_lock/test_mock_PreAcquireRequest.cc b/src/test/librbd/exclusive_lock/test_mock_PreAcquireRequest.cc new file mode 100644 index 000000000..5b4bce6dd --- /dev/null +++ b/src/test/librbd/exclusive_lock/test_mock_PreAcquireRequest.cc @@ -0,0 +1,92 @@ +// -*- 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/librados_test_stub/MockTestMemIoCtxImpl.h" +#include "test/librados_test_stub/MockTestMemRadosClient.h" +#include "librbd/exclusive_lock/PreAcquireRequest.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include +#include + +namespace librbd { +namespace { + +struct MockTestImageCtx : public librbd::MockImageCtx { + explicit MockTestImageCtx(librbd::ImageCtx &image_ctx) + : librbd::MockImageCtx(image_ctx) { + } +}; + +inline ImageCtx &get_image_ctx(MockTestImageCtx &image_ctx) { + return *(image_ctx.image_ctx); +} + +} // anonymous namespace +} // namespace librbd + +// template definitions +#include "librbd/exclusive_lock/PreAcquireRequest.cc" +template class librbd::exclusive_lock::PreAcquireRequest; + +namespace librbd { +namespace exclusive_lock { + +using ::testing::_; +using ::testing::DoAll; +using ::testing::InSequence; +using ::testing::Invoke; +using ::testing::Return; +using ::testing::SetArgPointee; +using ::testing::StrEq; +using ::testing::WithArg; + +static const std::string TEST_COOKIE("auto 123"); + +class TestMockExclusiveLockPreAcquireRequest : public TestMockFixture { +public: + typedef PreAcquireRequest MockPreAcquireRequest; + + void expect_flush_notifies(MockTestImageCtx &mock_image_ctx) { + EXPECT_CALL(*mock_image_ctx.image_watcher, flush(_)) + .WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue)); + } + + void expect_prepare_lock(MockTestImageCtx &mock_image_ctx) { + EXPECT_CALL(*mock_image_ctx.state, prepare_lock(_)) + .WillOnce(Invoke([](Context *on_ready) { + on_ready->complete(0); + })); + } + + void expect_handle_prepare_lock_complete(MockTestImageCtx &mock_image_ctx) { + EXPECT_CALL(*mock_image_ctx.state, handle_prepare_lock_complete()); + } + +}; + +TEST_F(TestMockExclusiveLockPreAcquireRequest, Success) { + REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK); + + librbd::ImageCtx *ictx; + ASSERT_EQ(0, open_image(m_image_name, &ictx)); + + MockTestImageCtx mock_image_ctx(*ictx); + expect_op_work_queue(mock_image_ctx); + + InSequence seq; + expect_prepare_lock(mock_image_ctx); + expect_flush_notifies(mock_image_ctx); + + C_SaferCond ctx; + MockPreAcquireRequest *req = MockPreAcquireRequest::create(mock_image_ctx, + &ctx); + req->send(); + ASSERT_EQ(0, ctx.wait()); + } + +} // namespace exclusive_lock +} // namespace librbd -- cgit v1.2.3