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