Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / librbd / managed_lock / test_mock_ReleaseRequest.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/librados_test_stub/MockTestMemIoCtxImpl.h"
7 #include "librbd/managed_lock/ReleaseRequest.h"
8 #include "common/WorkQueue.h"
9 #include "gmock/gmock.h"
10 #include "gtest/gtest.h"
11 #include <list>
12
13 namespace librbd {
14 namespace watcher {
15 template <>
16 struct Traits<MockImageCtx> {
17   typedef librbd::MockImageWatcher Watcher;
18 };
19 }
20 }
21
22 // template definitions
23 #include "librbd/managed_lock/ReleaseRequest.cc"
24 template class librbd::managed_lock::ReleaseRequest<librbd::MockImageCtx>;
25
26 namespace librbd {
27 namespace managed_lock {
28
29 using ::testing::_;
30 using ::testing::InSequence;
31 using ::testing::Invoke;
32 using ::testing::Return;
33 using ::testing::StrEq;
34
35 static const std::string TEST_COOKIE("auto 123");
36
37 class TestMockManagedLockReleaseRequest : public TestMockFixture {
38 public:
39   typedef ReleaseRequest<MockImageCtx> MockReleaseRequest;
40
41   void expect_unlock(MockImageCtx &mock_image_ctx, int r) {
42     EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
43                 exec(mock_image_ctx.header_oid, _, StrEq("lock"),
44                      StrEq("unlock"), _, _, _))
45                         .WillOnce(Return(r));
46   }
47
48 };
49
50 TEST_F(TestMockManagedLockReleaseRequest, Success) {
51
52   librbd::ImageCtx *ictx;
53   ASSERT_EQ(0, open_image(m_image_name, &ictx));
54
55   MockImageCtx mock_image_ctx(*ictx);
56   expect_op_work_queue(mock_image_ctx);
57
58   InSequence seq;
59
60   expect_unlock(mock_image_ctx, 0);
61
62   C_SaferCond ctx;
63   MockReleaseRequest *req = MockReleaseRequest::create(
64       mock_image_ctx.md_ctx, mock_image_ctx.image_watcher, ictx->op_work_queue,
65       mock_image_ctx.header_oid, TEST_COOKIE, &ctx);
66   req->send();
67   ASSERT_EQ(0, ctx.wait());
68 }
69
70 TEST_F(TestMockManagedLockReleaseRequest, UnlockError) {
71   librbd::ImageCtx *ictx;
72   ASSERT_EQ(0, open_image(m_image_name, &ictx));
73
74   MockImageCtx mock_image_ctx(*ictx);
75   expect_op_work_queue(mock_image_ctx);
76
77   InSequence seq;
78
79   expect_unlock(mock_image_ctx, -EINVAL);
80
81   C_SaferCond ctx;
82   MockReleaseRequest *req = MockReleaseRequest::create(
83       mock_image_ctx.md_ctx, mock_image_ctx.image_watcher, ictx->op_work_queue,
84       mock_image_ctx.header_oid, TEST_COOKIE, &ctx);
85   req->send();
86   ASSERT_EQ(0, ctx.wait());
87
88 }
89
90 } // namespace managed_lock
91 } // namespace librbd