summaryrefslogtreecommitdiffstats
path: root/src/test/librbd/image/test_mock_AttachParentRequest.cc
blob: de5f644310b53d773bcf85c5fe2713d42834ae39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "test/librbd/test_mock_fixture.h"
#include "test/librbd/test_support.h"
#include "test/librbd/mock/MockImageCtx.h"
#include "test/librbd/mock/MockContextWQ.h"
#include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "librbd/image/AttachParentRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace librbd {
namespace {

struct MockTestImageCtx : public MockImageCtx {
  MockTestImageCtx(ImageCtx &image_ctx) : MockImageCtx(image_ctx) {
  }
};

} // anonymous namespace
} // namespace librbd

// template definitions
#include "librbd/image/AttachParentRequest.cc"

namespace librbd {
namespace image {

using ::testing::_;
using ::testing::InSequence;
using ::testing::Return;
using ::testing::StrEq;

class TestMockImageAttachParentRequest : public TestMockFixture {
public:
  typedef AttachParentRequest<MockTestImageCtx> MockAttachParentRequest;

  void SetUp() override {
    TestMockFixture::SetUp();

    ASSERT_EQ(0, open_image(m_image_name, &image_ctx));
  }

  void expect_parent_attach(MockImageCtx &mock_image_ctx, int r) {
    EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
                exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
                     StrEq("parent_attach"), _, _, _, _))
      .WillOnce(Return(r));
  }

  void expect_set_parent(MockImageCtx &mock_image_ctx, int r) {
    EXPECT_CALL(get_mock_io_ctx(mock_image_ctx.md_ctx),
                exec(mock_image_ctx.header_oid, _, StrEq("rbd"),
                     StrEq("set_parent"), _, _, _, _))
      .WillOnce(Return(r));
  }

  librbd::ImageCtx *image_ctx;
};

TEST_F(TestMockImageAttachParentRequest, ParentAttachSuccess) {
  REQUIRE_FEATURE(RBD_FEATURE_LAYERING);

  MockTestImageCtx mock_image_ctx(*image_ctx);

  InSequence seq;
  expect_parent_attach(mock_image_ctx, 0);

  cls::rbd::ParentImageSpec parent_image_spec{
    1, "ns", "image id", 123};

  C_SaferCond ctx;
  auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
                                             234, false, &ctx);
  req->send();
  ASSERT_EQ(0, ctx.wait());
}

TEST_F(TestMockImageAttachParentRequest, SetParentSuccess) {
  REQUIRE_FEATURE(RBD_FEATURE_LAYERING);

  MockTestImageCtx mock_image_ctx(*image_ctx);

  InSequence seq;
  expect_parent_attach(mock_image_ctx, -EOPNOTSUPP);
  expect_set_parent(mock_image_ctx, 0);

  cls::rbd::ParentImageSpec parent_image_spec{
    1, "", "image id", 123};

  C_SaferCond ctx;
  auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
                                             234, false, &ctx);
  req->send();
  ASSERT_EQ(0, ctx.wait());
}

TEST_F(TestMockImageAttachParentRequest, ParentAttachError) {
  REQUIRE_FEATURE(RBD_FEATURE_LAYERING);

  MockTestImageCtx mock_image_ctx(*image_ctx);

  InSequence seq;
  expect_parent_attach(mock_image_ctx, -EPERM);

  cls::rbd::ParentImageSpec parent_image_spec{
    1, "", "image id", 123};

  C_SaferCond ctx;
  auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
                                             234, false, &ctx);
  req->send();
  ASSERT_EQ(-EPERM, ctx.wait());
}

TEST_F(TestMockImageAttachParentRequest, SetParentError) {
  REQUIRE_FEATURE(RBD_FEATURE_LAYERING);

  MockTestImageCtx mock_image_ctx(*image_ctx);

  InSequence seq;
  expect_parent_attach(mock_image_ctx, -EOPNOTSUPP);
  expect_set_parent(mock_image_ctx, -EINVAL);

  cls::rbd::ParentImageSpec parent_image_spec{
    1, "", "image id", 123};

  C_SaferCond ctx;
  auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
                                             234, false, &ctx);
  req->send();
  ASSERT_EQ(-EINVAL, ctx.wait());
}

TEST_F(TestMockImageAttachParentRequest, NamespaceUnsupported) {
  REQUIRE_FEATURE(RBD_FEATURE_LAYERING);

  MockTestImageCtx mock_image_ctx(*image_ctx);

  InSequence seq;
  expect_parent_attach(mock_image_ctx, -EOPNOTSUPP);

  cls::rbd::ParentImageSpec parent_image_spec{
    1, "ns", "image id", 123};

  C_SaferCond ctx;
  auto req = MockAttachParentRequest::create(mock_image_ctx, parent_image_spec,
                                             234, false, &ctx);
  req->send();
  ASSERT_EQ(-EXDEV, ctx.wait());
}

} // namespace image
} // namespace librbd