Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / librbd / exclusive_lock / test_mock_PreAcquireRequest.cc
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/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>
13 #include <list>
14
15 namespace librbd {
16 namespace {
17
18 struct MockTestImageCtx : public librbd::MockImageCtx {
19   MockTestImageCtx(librbd::ImageCtx &image_ctx)
20     : librbd::MockImageCtx(image_ctx) {
21   }
22 };
23
24 inline ImageCtx &get_image_ctx(MockTestImageCtx &image_ctx) {
25   return *(image_ctx.image_ctx);
26 }
27
28 } // anonymous namespace
29 } // namespace librbd
30
31 // template definitions
32 #include "librbd/exclusive_lock/PreAcquireRequest.cc"
33 template class librbd::exclusive_lock::PreAcquireRequest<librbd::MockTestImageCtx>;
34
35 namespace librbd {
36 namespace exclusive_lock {
37
38 using ::testing::_;
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;
46
47 static const std::string TEST_COOKIE("auto 123");
48
49 class TestMockExclusiveLockPreAcquireRequest : public TestMockFixture {
50 public:
51   typedef PreAcquireRequest<MockTestImageCtx> MockPreAcquireRequest;
52
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));
56   }
57
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);
62                 }));
63   }
64
65   void expect_handle_prepare_lock_complete(MockTestImageCtx &mock_image_ctx) {
66     EXPECT_CALL(*mock_image_ctx.state, handle_prepare_lock_complete());
67   }
68
69 };
70
71 TEST_F(TestMockExclusiveLockPreAcquireRequest, Success) {
72   REQUIRE_FEATURE(RBD_FEATURE_EXCLUSIVE_LOCK);
73
74   librbd::ImageCtx *ictx;
75   ASSERT_EQ(0, open_image(m_image_name, &ictx));
76
77   MockTestImageCtx mock_image_ctx(*ictx);
78   expect_op_work_queue(mock_image_ctx);
79
80   InSequence seq;
81   expect_prepare_lock(mock_image_ctx);
82   expect_flush_notifies(mock_image_ctx);
83
84   C_SaferCond ctx;
85   MockPreAcquireRequest *req = MockPreAcquireRequest::create(mock_image_ctx,
86                                                              &ctx);
87   req->send();
88   ASSERT_EQ(0, ctx.wait());
89  }
90
91 } // namespace exclusive_lock
92 } // namespace librbd