Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / librbd / test_MirroringWatcher.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_fixture.h"
5 #include "test/librbd/test_support.h"
6 #include "include/rbd_types.h"
7 #include "librbd/MirroringWatcher.h"
8 #include "gtest/gtest.h"
9 #include "gmock/gmock.h"
10 #include <list>
11
12 void register_test_mirroring_watcher() {
13 }
14
15 namespace librbd {
16
17 namespace {
18
19 struct MockMirroringWatcher : public MirroringWatcher<> {
20   std::string oid;
21
22   MockMirroringWatcher(ImageCtx &image_ctx)
23     : MirroringWatcher<>(image_ctx.md_ctx, image_ctx.op_work_queue) {
24   }
25
26   MOCK_METHOD1(handle_mode_updated, void(cls::rbd::MirrorMode));
27   MOCK_METHOD3(handle_image_updated, void(cls::rbd::MirrorImageState,
28                                           const std::string &,
29                                           const std::string &));
30 };
31
32 } // anonymous namespace
33
34 using ::testing::_;
35 using ::testing::AtLeast;
36 using ::testing::Invoke;
37 using ::testing::StrEq;
38 using ::testing::WithArg;
39
40 class TestMirroringWatcher : public TestFixture {
41 public:
42   void SetUp() override {
43     TestFixture::SetUp();
44
45     bufferlist bl;
46     ASSERT_EQ(0, m_ioctx.write_full(RBD_MIRRORING, bl));
47
48     librbd::ImageCtx *ictx;
49     ASSERT_EQ(0, open_image(m_image_name, &ictx));
50
51     m_image_watcher = new MockMirroringWatcher(*ictx);
52     C_SaferCond ctx;
53     m_image_watcher->register_watch(&ctx);
54     if (ctx.wait() != 0) {
55       delete m_image_watcher;
56       m_image_watcher = nullptr;
57       FAIL();
58     }
59   }
60
61   void TearDown() override {
62     if (m_image_watcher != nullptr) {
63       C_SaferCond ctx;
64       m_image_watcher->unregister_watch(&ctx);
65       ASSERT_EQ(0, ctx.wait());
66       delete m_image_watcher;
67     }
68
69     TestFixture::TearDown();
70   }
71
72   MockMirroringWatcher *m_image_watcher = nullptr;
73 };
74
75 TEST_F(TestMirroringWatcher, ModeUpdated) {
76   EXPECT_CALL(*m_image_watcher,
77               handle_mode_updated(cls::rbd::MIRROR_MODE_DISABLED))
78     .Times(AtLeast(1));
79
80   C_SaferCond ctx;
81   MockMirroringWatcher::notify_mode_updated(
82     m_ioctx, cls::rbd::MIRROR_MODE_DISABLED, &ctx);
83   ASSERT_EQ(0, ctx.wait());
84 }
85
86 TEST_F(TestMirroringWatcher, ImageStatusUpdated) {
87   EXPECT_CALL(*m_image_watcher,
88               handle_image_updated(cls::rbd::MIRROR_IMAGE_STATE_ENABLED,
89                                    StrEq("image id"),
90                                    StrEq("global image id")))
91     .Times(AtLeast(1));
92
93   C_SaferCond ctx;
94   MockMirroringWatcher::notify_image_updated(
95     m_ioctx, cls::rbd::MIRROR_IMAGE_STATE_ENABLED, "image id",
96     "global image id", &ctx);
97   ASSERT_EQ(0, ctx.wait());
98 }
99
100 } // namespace librbd