summaryrefslogtreecommitdiffstats
path: root/src/test/librbd/object_map/test_mock_SnapshotRollbackRequest.cc
blob: 7b89a0996c11d7059d990ca476cf87b68323eb07 (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
// -*- 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/librados_test_stub/MockTestMemIoCtxImpl.h"
#include "librbd/ImageState.h"
#include "librbd/internal.h"
#include "librbd/ObjectMap.h"
#include "librbd/object_map/SnapshotRollbackRequest.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace librbd {
namespace object_map {

using ::testing::_;
using ::testing::DoDefault;
using ::testing::Return;
using ::testing::StrEq;

class TestMockObjectMapSnapshotRollbackRequest : public TestMockFixture {
public:
  void expect_read_map(librbd::ImageCtx *ictx, uint64_t snap_id, int r) {
    if (r < 0) {
      EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx),
                  read(ObjectMap<>::object_map_name(ictx->id, snap_id),
                       0, 0, _, _, _)).WillOnce(Return(r));
    } else {
      EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx),
                  read(ObjectMap<>::object_map_name(ictx->id, snap_id),
                       0, 0, _, _, _)).WillOnce(DoDefault());
    }
  }

  void expect_write_map(librbd::ImageCtx *ictx, int r) {
    EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx),
                exec(ObjectMap<>::object_map_name(ictx->id, CEPH_NOSNAP), _,
		     StrEq("lock"), StrEq("assert_locked"), _, _, _, _))
                  .WillOnce(DoDefault());
    if (r < 0) {
      EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx),
                  write_full(
                    ObjectMap<>::object_map_name(ictx->id, CEPH_NOSNAP), _, _))
                  .WillOnce(Return(r));
    } else {
      EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx),
                  write_full(
                    ObjectMap<>::object_map_name(ictx->id, CEPH_NOSNAP), _, _))
                  .WillOnce(DoDefault());
    }
  }

  void expect_invalidate(librbd::ImageCtx *ictx, uint32_t times) {
    EXPECT_CALL(get_mock_io_ctx(ictx->md_ctx),
                exec(ictx->header_oid, _, StrEq("rbd"), StrEq("set_flags"), _,
                     _, _, _))
                  .Times(times)
                  .WillRepeatedly(DoDefault());
  }
};

TEST_F(TestMockObjectMapSnapshotRollbackRequest, Success) {
  REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));
  ASSERT_EQ(0, snap_create(*ictx, "snap1"));
  ASSERT_EQ(0, ictx->state->refresh_if_required());

  uint64_t snap_id = ictx->snap_info.rbegin()->first;
  expect_read_map(ictx, snap_id, 0);
  expect_write_map(ictx, 0);

  C_SaferCond cond_ctx;
  AsyncRequest<> *request = new SnapshotRollbackRequest(
    *ictx, snap_id, &cond_ctx);
  request->send();
  ASSERT_EQ(0, cond_ctx.wait());

  expect_unlock_exclusive_lock(*ictx);
}

TEST_F(TestMockObjectMapSnapshotRollbackRequest, ReadMapError) {
  REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));
  ASSERT_EQ(0, snap_create(*ictx, "snap1"));
  ASSERT_EQ(0, ictx->state->refresh_if_required());

  uint64_t snap_id = ictx->snap_info.rbegin()->first;
  expect_read_map(ictx, snap_id, -ENOENT);
  expect_invalidate(ictx, 2);

  C_SaferCond cond_ctx;
  AsyncRequest<> *request = new SnapshotRollbackRequest(
    *ictx, snap_id, &cond_ctx);
  request->send();
  ASSERT_EQ(0, cond_ctx.wait());

  {
    std::shared_lock image_locker{ictx->image_lock};
    uint64_t flags;
    ASSERT_EQ(0, ictx->get_flags(snap_id, &flags));
    ASSERT_NE(0U, flags & RBD_FLAG_OBJECT_MAP_INVALID);
  }
  bool flags_set;
  ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP,
                                RBD_FLAG_OBJECT_MAP_INVALID, &flags_set));
  ASSERT_TRUE(flags_set);
  expect_unlock_exclusive_lock(*ictx);
}

TEST_F(TestMockObjectMapSnapshotRollbackRequest, WriteMapError) {
  REQUIRE_FEATURE(RBD_FEATURE_OBJECT_MAP);

  librbd::ImageCtx *ictx;
  ASSERT_EQ(0, open_image(m_image_name, &ictx));
  ASSERT_EQ(0, snap_create(*ictx, "snap1"));
  ASSERT_EQ(0, ictx->state->refresh_if_required());

  uint64_t snap_id = ictx->snap_info.rbegin()->first;
  expect_read_map(ictx, snap_id, 0);
  expect_write_map(ictx, -EINVAL);
  expect_invalidate(ictx, 1);

  C_SaferCond cond_ctx;
  AsyncRequest<> *request = new SnapshotRollbackRequest(
    *ictx, snap_id, &cond_ctx);
  request->send();
  ASSERT_EQ(0, cond_ctx.wait());

  {
    std::shared_lock image_locker{ictx->image_lock};
    uint64_t flags;
    ASSERT_EQ(0, ictx->get_flags(snap_id, &flags));
    ASSERT_EQ(0U, flags & RBD_FLAG_OBJECT_MAP_INVALID);
  }
  bool flags_set;
  ASSERT_EQ(0, ictx->test_flags(CEPH_NOSNAP,
                                RBD_FLAG_OBJECT_MAP_INVALID, &flags_set));
  ASSERT_TRUE(flags_set);
  expect_unlock_exclusive_lock(*ictx);
}

} // namespace object_map
} // namespace librbd