]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/librbd/test_mock_TrashWatcher.cc
import 15.2.4
[ceph.git] / ceph / src / test / librbd / test_mock_TrashWatcher.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 "include/rbd_types.h"
7 #include "librbd/TrashWatcher.h"
8 #include "gtest/gtest.h"
9 #include "gmock/gmock.h"
10 #include <list>
11
12 namespace librbd {
13 namespace {
14
15 struct MockTrashWatcher : public TrashWatcher<> {
16 MockTrashWatcher(ImageCtx &image_ctx)
17 : TrashWatcher<>(image_ctx.md_ctx, image_ctx.op_work_queue) {
18 }
19
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&));
23 };
24
25 } // anonymous namespace
26
27 using ::testing::_;
28 using ::testing::AtLeast;
29 using ::testing::StrEq;
30
31 class TestTrashWatcher : public TestMockFixture {
32 public:
33 void SetUp() override {
34 TestFixture::SetUp();
35
36 bufferlist bl;
37 ASSERT_EQ(0, m_ioctx.write_full(RBD_TRASH, bl));
38
39 librbd::ImageCtx *ictx;
40 ASSERT_EQ(0, open_image(m_image_name, &ictx));
41
42 m_trash_watcher = new MockTrashWatcher(*ictx);
43
44 C_SaferCond ctx;
45 m_trash_watcher->register_watch(&ctx);
46 if (ctx.wait() != 0) {
47 delete m_trash_watcher;
48 m_trash_watcher = nullptr;
49 FAIL();
50 }
51 }
52
53 void TearDown() override {
54 if (m_trash_watcher != nullptr) {
55 C_SaferCond ctx;
56 m_trash_watcher->unregister_watch(&ctx);
57 ASSERT_EQ(0, ctx.wait());
58 delete m_trash_watcher;
59 }
60
61 TestFixture::TearDown();
62 }
63
64 MockTrashWatcher *m_trash_watcher = nullptr;
65 };
66
67 TEST_F(TestTrashWatcher, ImageAdded) {
68 REQUIRE_FORMAT_V2();
69
70 cls::rbd::TrashImageSpec trash_image_spec{
71 cls::rbd::TRASH_IMAGE_SOURCE_USER, "image name",
72 ceph_clock_now(), ceph_clock_now()};
73
74 EXPECT_CALL(*m_trash_watcher, handle_image_added(StrEq("image id"),
75 trash_image_spec))
76 .Times(AtLeast(1));
77
78 C_SaferCond ctx;
79 MockTrashWatcher::notify_image_added(m_ioctx, "image id", trash_image_spec,
80 &ctx);
81 ASSERT_EQ(0, ctx.wait());
82 }
83
84 TEST_F(TestTrashWatcher, ImageRemoved) {
85 REQUIRE_FORMAT_V2();
86
87 EXPECT_CALL(*m_trash_watcher, handle_image_removed(StrEq("image id")))
88 .Times(AtLeast(1));
89
90 C_SaferCond ctx;
91 MockTrashWatcher::notify_image_removed(m_ioctx, "image id", &ctx);
92 ASSERT_EQ(0, ctx.wait());
93 }
94
95 } // namespace librbd