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_mock_fixture.h"
5 #include "test/librbd/test_support.h"
6 #include "include/rbd_types.h"
7 #include "librbd/TrashWatcher.h"
8 #include "gtest/gtest.h"
9 #include "gmock/gmock.h"
15 struct MockTrashWatcher
: public TrashWatcher
<> {
16 MockTrashWatcher(ImageCtx
&image_ctx
)
17 : TrashWatcher
<>(image_ctx
.md_ctx
, image_ctx
.op_work_queue
) {
20 MOCK_METHOD2(handle_image_added
, void(const std::string
&,
21 const cls::rbd::TrashImageSpec
&));
22 MOCK_METHOD1(handle_image_removed
, void(const std::string
&));
25 } // anonymous namespace
28 using ::testing::AtLeast
;
29 using ::testing::StrEq
;
31 class TestTrashWatcher
: public TestMockFixture
{
33 void SetUp() override
{
37 ASSERT_EQ(0, m_ioctx
.write_full(RBD_TRASH
, bl
));
39 librbd::ImageCtx
*ictx
;
40 ASSERT_EQ(0, open_image(m_image_name
, &ictx
));
42 m_trash_watcher
= new MockTrashWatcher(*ictx
);
45 m_trash_watcher
->register_watch(&ctx
);
46 if (ctx
.wait() != 0) {
47 delete m_trash_watcher
;
48 m_trash_watcher
= nullptr;
53 void TearDown() override
{
54 if (m_trash_watcher
!= nullptr) {
56 m_trash_watcher
->unregister_watch(&ctx
);
57 ASSERT_EQ(0, ctx
.wait());
58 delete m_trash_watcher
;
61 TestFixture::TearDown();
64 MockTrashWatcher
*m_trash_watcher
= nullptr;
67 TEST_F(TestTrashWatcher
, ImageAdded
) {
70 cls::rbd::TrashImageSpec trash_image_spec
{
71 cls::rbd::TRASH_IMAGE_SOURCE_USER
, "image name",
72 ceph_clock_now(), ceph_clock_now()};
74 EXPECT_CALL(*m_trash_watcher
, handle_image_added(StrEq("image id"),
79 MockTrashWatcher::notify_image_added(m_ioctx
, "image id", trash_image_spec
,
81 ASSERT_EQ(0, ctx
.wait());
84 TEST_F(TestTrashWatcher
, ImageRemoved
) {
87 EXPECT_CALL(*m_trash_watcher
, handle_image_removed(StrEq("image id")))
91 MockTrashWatcher::notify_image_removed(m_ioctx
, "image id", &ctx
);
92 ASSERT_EQ(0, ctx
.wait());