]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/librbd/image/test_mock_DetachParentRequest.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / test / librbd / image / test_mock_DetachParentRequest.cc
CommitLineData
11fdf7f2
TL
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 "test/librbd/mock/MockImageCtx.h"
7#include "test/librbd/mock/MockContextWQ.h"
8#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
9#include "librbd/image/DetachParentRequest.h"
10#include "gmock/gmock.h"
11#include "gtest/gtest.h"
12
13namespace librbd {
14namespace {
15
16struct MockTestImageCtx : public MockImageCtx {
17 MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
18 }
19};
20
21} // anonymous namespace
22} // namespace librbd
23
24// template definitions
25#include "librbd/image/DetachParentRequest.cc"
26
27namespace librbd {
28namespace image {
29
30using ::testing::_;
31using ::testing::InSequence;
32using ::testing::Return;
33using ::testing::StrEq;
34
35class TestMockImageDetachParentRequest : public TestMockFixture {
36public:
37 typedef DetachParentRequest<MockTestImageCtx> MockDetachParentRequest;
38
39 void SetUp() override {
40 TestMockFixture::SetUp();
41
42 ASSERT_EQ(0, open_image(m_image_name, &image_ctx));
43 }
44
45 void expect_parent_detach(MockImageCtx &mock_image_ctx, int r) {
46 EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
47 exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
f67539c2 48 StrEq("parent_detach"), _, _, _, _))
11fdf7f2
TL
49 .WillOnce(Return(r));
50 }
51
52 void expect_remove_parent(MockImageCtx &mock_image_ctx, int r) {
53 EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
54 exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
f67539c2 55 StrEq("remove_parent"), _, _, _, _))
11fdf7f2
TL
56 .WillOnce(Return(r));
57 }
58
59 librbd::ImageCtx *image_ctx;
60};
61
62TEST_F(TestMockImageDetachParentRequest, ParentDetachSuccess) {
63 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
64
65 MockTestImageCtx mock_image_ctx(*image_ctx);
66
67 InSequence seq;
68 expect_parent_detach(mock_image_ctx, 0);
69
70 C_SaferCond ctx;
71 auto req = MockDetachParentRequest::create(mock_image_ctx, &ctx);
72 req->send();
73 ASSERT_EQ(0, ctx.wait());
74}
75
76TEST_F(TestMockImageDetachParentRequest, RemoveParentSuccess) {
77 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
78
79 MockTestImageCtx mock_image_ctx(*image_ctx);
80
81 InSequence seq;
82 expect_parent_detach(mock_image_ctx, -EOPNOTSUPP);
83 expect_remove_parent(mock_image_ctx, 0);
84
85 C_SaferCond ctx;
86 auto req = MockDetachParentRequest::create(mock_image_ctx, &ctx);
87 req->send();
88 ASSERT_EQ(0, ctx.wait());
89}
90
91TEST_F(TestMockImageDetachParentRequest, ParentDNE) {
92 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
93
94 MockTestImageCtx mock_image_ctx(*image_ctx);
95
96 InSequence seq;
97 expect_parent_detach(mock_image_ctx, -ENOENT);
98
99 C_SaferCond ctx;
100 auto req = MockDetachParentRequest::create(mock_image_ctx, &ctx);
101 req->send();
102 ASSERT_EQ(0, ctx.wait());
103}
104
105TEST_F(TestMockImageDetachParentRequest, ParentDetachError) {
106 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
107
108 MockTestImageCtx mock_image_ctx(*image_ctx);
109
110 InSequence seq;
111 expect_parent_detach(mock_image_ctx, -EPERM);
112
113 C_SaferCond ctx;
114 auto req = MockDetachParentRequest::create(mock_image_ctx, &ctx);
115 req->send();
116 ASSERT_EQ(-EPERM, ctx.wait());
117}
118
119TEST_F(TestMockImageDetachParentRequest, RemoveParentError) {
120 REQUIRE_FEATURE(RBD_FEATURE_LAYERING);
121
122 MockTestImageCtx mock_image_ctx(*image_ctx);
123
124 InSequence seq;
125 expect_parent_detach(mock_image_ctx, -EOPNOTSUPP);
126 expect_remove_parent(mock_image_ctx, -EINVAL);
127
128 C_SaferCond ctx;
129 auto req = MockDetachParentRequest::create(mock_image_ctx, &ctx);
130 req->send();
131 ASSERT_EQ(-EINVAL, ctx.wait());
132}
133
134} // namespace image
135} // namespace librbd