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_fixture.h"
5 #include "test/librbd/test_support.h"
6 #include "include/rbd_types.h"
7 #include "librbd/MirroringWatcher.h"
8 #include "common/Cond.h"
9 #include "gtest/gtest.h"
10 #include "gmock/gmock.h"
13 void register_test_mirroring_watcher() {
20 struct MockMirroringWatcher
: public MirroringWatcher
<> {
23 MockMirroringWatcher(ImageCtx
&image_ctx
)
24 : MirroringWatcher
<>(image_ctx
.md_ctx
, image_ctx
.op_work_queue
) {
27 MOCK_METHOD1(handle_mode_updated
, void(cls::rbd::MirrorMode
));
28 MOCK_METHOD3(handle_image_updated
, void(cls::rbd::MirrorImageState
,
30 const std::string
&));
33 } // anonymous namespace
36 using ::testing::AtLeast
;
37 using ::testing::Invoke
;
38 using ::testing::StrEq
;
39 using ::testing::WithArg
;
41 class TestMirroringWatcher
: public TestFixture
{
43 void SetUp() override
{
47 ASSERT_EQ(0, m_ioctx
.write_full(RBD_MIRRORING
, bl
));
49 librbd::ImageCtx
*ictx
;
50 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
52 m_image_watcher
= new MockMirroringWatcher(*ictx
);
54 m_image_watcher
->register_watch(&ctx
);
55 if (ctx
.wait() != 0) {
56 delete m_image_watcher
;
57 m_image_watcher
= nullptr;
62 void TearDown() override
{
63 if (m_image_watcher
!= nullptr) {
65 m_image_watcher
->unregister_watch(&ctx
);
66 ASSERT_EQ(0, ctx
.wait());
67 delete m_image_watcher
;
70 TestFixture::TearDown();
73 MockMirroringWatcher
*m_image_watcher
= nullptr;
76 TEST_F(TestMirroringWatcher
, ModeUpdated
) {
77 EXPECT_CALL(*m_image_watcher
,
78 handle_mode_updated(cls::rbd::MIRROR_MODE_DISABLED
))
82 MockMirroringWatcher::notify_mode_updated(
83 m_ioctx
, cls::rbd::MIRROR_MODE_DISABLED
, &ctx
);
84 ASSERT_EQ(0, ctx
.wait());
87 TEST_F(TestMirroringWatcher
, ImageStatusUpdated
) {
88 EXPECT_CALL(*m_image_watcher
,
89 handle_image_updated(cls::rbd::MIRROR_IMAGE_STATE_ENABLED
,
91 StrEq("global image id")))
95 MockMirroringWatcher::notify_image_updated(
96 m_ioctx
, cls::rbd::MIRROR_IMAGE_STATE_ENABLED
, "image id",
97 "global image id", &ctx
);
98 ASSERT_EQ(0, ctx
.wait());
101 } // namespace librbd