1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
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/librados_test_stub/MockTestMemIoCtxImpl.h"
8 #include "test/librados_test_stub/MockTestMemRadosClient.h"
9 #include "librbd/exclusive_lock/PreAcquireRequest.h"
10 #include "gmock/gmock.h"
11 #include "gtest/gtest.h"
12 #include <arpa/inet.h>
18 struct MockTestImageCtx : public librbd::MockImageCtx {
19 MockTestImageCtx(librbd::ImageCtx &image_ctx)
20 : librbd::MockImageCtx(image_ctx) {
24 inline ImageCtx &get_image_ctx(MockTestImageCtx &image_ctx) {
25 return *(image_ctx.image_ctx);
28 } // anonymous namespace
31 // template definitions
32 #include "librbd/exclusive_lock/PreAcquireRequest.cc"
33 template class librbd::exclusive_lock::PreAcquireRequest<librbd::MockTestImageCtx>;
36 namespace exclusive_lock {
39 using ::testing::DoAll;
40 using ::testing::InSequence;
41 using ::testing::Invoke;
42 using ::testing::Return;
43 using ::testing::SetArgPointee;
44 using ::testing::StrEq;
45 using ::testing::WithArg;
47 static const std::string TEST_COOKIE("auto 123");
49 class TestMockExclusiveLockPreAcquireRequest : public TestMockFixture {
51 typedef PreAcquireRequest<MockTestImageCtx> MockPreAcquireRequest;
53 void expect_flush_notifies(MockTestImageCtx &mock_image_ctx) {
54 EXPECT_CALL(*mock_image_ctx.image_watcher, flush(_))
55 .WillOnce(CompleteContext(0, mock_image_ctx.image_ctx->op_work_queue));
58 void expect_prepare_lock(MockTestImageCtx &mock_image_ctx) {
59 EXPECT_CALL(*mock_image_ctx.state, prepare_lock(_))
60 .WillOnce(Invoke([](Context *on_ready) {
61 on_ready->complete(0);
65 void expect_handle_prepare_lock_complete(MockTestImageCtx &mock_image_ctx) {
66 EXPECT_CALL(*mock_image_ctx.state, handle_prepare_lock_complete());
71 TEST_F(TestMockExclusiveLockPreAcquireRequest, Success) {
72 REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
74 librbd::ImageCtx *ictx;
75 ASSERT_EQ(0, open_image(m_image_name, &ictx));
77 MockTestImageCtx mock_image_ctx(*ictx);
78 expect_op_work_queue(mock_image_ctx);
81 expect_prepare_lock(mock_image_ctx);
82 expect_flush_notifies(mock_image_ctx);
85 MockPreAcquireRequest *req = MockPreAcquireRequest::create(mock_image_ctx,
88 ASSERT_EQ(0, ctx.wait());
91 } // namespace exclusive_lock