]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/librbd/mirror/snapshot/test_mock_Utils.cc
bump version to 15.2.1-pve1
[ceph.git] / ceph / src / test / librbd / mirror / snapshot / test_mock_Utils.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 "include/stringify.h"
5 #include "test/librbd/test_mock_fixture.h"
6 #include "test/librbd/test_support.h"
7 #include "test/librbd/mock/MockImageCtx.h"
8 #include "test/librbd/mock/MockOperations.h"
9 #include "test/librados_test_stub/MockTestMemIoCtxImpl.h"
10 #include "test/librados_test_stub/MockTestMemRadosClient.h"
11 #include "librbd/mirror/snapshot/CreatePrimaryRequest.h"
12 #include "librbd/mirror/snapshot/UnlinkPeerRequest.h"
13 #include "librbd/mirror/snapshot/Utils.h"
14
15 namespace librbd {
16
17 namespace {
18
19 struct MockTestImageCtx : public MockImageCtx {
20 explicit MockTestImageCtx(librbd::ImageCtx& image_ctx) : MockImageCtx(image_ctx) {
21 }
22 };
23
24 } // anonymous namespace
25 } // namespace librbd
26
27 // template definitions
28 #include "librbd/mirror/snapshot/Utils.cc"
29 template bool librbd::mirror::snapshot::util::can_create_primary_snapshot(
30 librbd::MockTestImageCtx *image_ctx, bool demoted, bool force,
31 bool* requires_orphan, uint64_t *rollback_snap_id);
32 template bool librbd::mirror::snapshot::util::can_create_non_primary_snapshot(
33 librbd::MockTestImageCtx *image_ctx);
34
35 namespace librbd {
36 namespace mirror {
37 namespace snapshot {
38
39 using ::testing::_;
40 using ::testing::DoAll;
41 using ::testing::InSequence;
42 using ::testing::Invoke;
43 using ::testing::Return;
44 using ::testing::StrEq;
45 using ::testing::WithArg;
46
47 class TestMockMirrorSnapshotUtils : public TestMockFixture {
48 public:
49 uint64_t m_snap_seq = 0;
50
51 uint64_t snap_create(MockTestImageCtx &mock_image_ctx,
52 const cls::rbd::SnapshotNamespace &ns,
53 const std::string& snap_name) {
54 EXPECT_TRUE(mock_image_ctx.snap_info.insert(
55 {++m_snap_seq,
56 SnapInfo{snap_name, ns, 0, {}, 0, 0, {}}}).second);
57 return m_snap_seq;
58 }
59 };
60
61 TEST_F(TestMockMirrorSnapshotUtils, CanCreatePrimarySnapshot) {
62 REQUIRE_FORMAT_V2();
63
64 librbd::ImageCtx *ictx;
65 ASSERT_EQ(0, open_image(m_image_name, &ictx));
66
67 MockTestImageCtx mock_image_ctx(*ictx);
68
69 // no previous mirror snapshots found
70 bool requires_orphan;
71 uint64_t rollback_snap_id;
72 ASSERT_TRUE(util::can_create_primary_snapshot(&mock_image_ctx, false, false,
73 &requires_orphan,
74 &rollback_snap_id));
75 ASSERT_FALSE(requires_orphan);
76 ASSERT_EQ(rollback_snap_id, CEPH_NOSNAP);
77
78 cls::rbd::MirrorSnapshotNamespace nns{
79 cls::rbd::MIRROR_SNAPSHOT_STATE_NON_PRIMARY, {}, "mirror_uuid", 123};
80 nns.complete = true;
81 auto copied_snap_id = snap_create(mock_image_ctx, nns, "NPS1");
82
83 // without force, previous snapshot is non-primary
84 ASSERT_FALSE(util::can_create_primary_snapshot(&mock_image_ctx, false, false,
85 nullptr, nullptr));
86
87 // demoted, previous snapshot is non-primary
88 ASSERT_FALSE(util::can_create_primary_snapshot(&mock_image_ctx, true, true,
89 nullptr, nullptr));
90
91 // previous non-primary snapshot is copied
92 ASSERT_TRUE(util::can_create_primary_snapshot(&mock_image_ctx, false, true,
93 &requires_orphan,
94 &rollback_snap_id));
95 ASSERT_TRUE(requires_orphan);
96 ASSERT_EQ(rollback_snap_id, CEPH_NOSNAP);
97
98 nns.complete = false;
99 snap_create(mock_image_ctx, nns, "NPS2");
100
101 // previous non-primary snapshot is not copied yet
102 ASSERT_FALSE(util::can_create_primary_snapshot(&mock_image_ctx, false, true,
103 nullptr, nullptr));
104
105 // can rollback
106 ASSERT_TRUE(util::can_create_primary_snapshot(&mock_image_ctx, false, true,
107 nullptr, &rollback_snap_id));
108 ASSERT_EQ(rollback_snap_id, copied_snap_id);
109
110 nns.state = cls::rbd::MIRROR_SNAPSHOT_STATE_NON_PRIMARY_DEMOTED;
111 snap_create(mock_image_ctx, nns, "NPS3");
112
113 // previous non-primary snapshot is orphan
114 ASSERT_TRUE(util::can_create_primary_snapshot(&mock_image_ctx, false, true,
115 nullptr, nullptr));
116
117 cls::rbd::MirrorSnapshotNamespace pns{
118 cls::rbd::MIRROR_SNAPSHOT_STATE_PRIMARY_DEMOTED, {"uuid"}, "", CEPH_NOSNAP};
119 snap_create(mock_image_ctx, pns, "PS1");
120
121 // previous primary snapshot is demoted, no force
122 ASSERT_FALSE(util::can_create_primary_snapshot(&mock_image_ctx, false, false,
123 nullptr, nullptr));
124
125 // previous primary snapshot is demoted, force
126 ASSERT_TRUE(util::can_create_primary_snapshot(&mock_image_ctx, false, true,
127 nullptr, nullptr));
128
129 pns.state = cls::rbd::MIRROR_SNAPSHOT_STATE_PRIMARY;
130 snap_create(mock_image_ctx, pns, "PS2");
131
132 // previous snapshot is not demoted primary
133 ASSERT_TRUE(util::can_create_primary_snapshot(&mock_image_ctx, false, false,
134 nullptr, nullptr));
135 }
136
137 TEST_F(TestMockMirrorSnapshotUtils, CanCreateNonPrimarySnapshot) {
138 REQUIRE_FORMAT_V2();
139
140 librbd::ImageCtx *ictx;
141 ASSERT_EQ(0, open_image(m_image_name, &ictx));
142
143 MockTestImageCtx mock_image_ctx(*ictx);
144
145 // no previous mirror snapshots found
146 ASSERT_TRUE(util::can_create_non_primary_snapshot(&mock_image_ctx));
147
148 cls::rbd::MirrorSnapshotNamespace nns{
149 cls::rbd::MIRROR_SNAPSHOT_STATE_NON_PRIMARY, {}, "mirror_uuid", 123};
150 snap_create(mock_image_ctx, nns, "NPS1");
151
152 // previous non-primary snapshot is not copied yet
153 ASSERT_FALSE(util::can_create_non_primary_snapshot(&mock_image_ctx));
154
155 nns.complete = true;
156 snap_create(mock_image_ctx, nns, "NPS2");
157
158 // previous non-primary snapshot is copied
159 ASSERT_TRUE(util::can_create_non_primary_snapshot(&mock_image_ctx));
160
161 cls::rbd::MirrorSnapshotNamespace pns{
162 cls::rbd::MIRROR_SNAPSHOT_STATE_PRIMARY, {"uuid"}, "", CEPH_NOSNAP};
163 snap_create(mock_image_ctx, pns, "PS1");
164
165 // previous primary snapshot is not in demoted state
166 ASSERT_FALSE(util::can_create_non_primary_snapshot(&mock_image_ctx));
167
168 pns.state = cls::rbd::MIRROR_SNAPSHOT_STATE_NON_PRIMARY_DEMOTED;
169 snap_create(mock_image_ctx, pns, "PS2");
170
171 // previous primary snapshot is in demoted state
172 ASSERT_TRUE(util::can_create_non_primary_snapshot(&mock_image_ctx));
173 }
174
175 } // namespace snapshot
176 } // namespace mirror
177 } // namespace librbd
178