]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/librbd/mock/MockImageCtx.h
import 15.2.5
[ceph.git] / ceph / src / test / librbd / mock / MockImageCtx.h
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
4#ifndef CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H
5#define CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H
6
7#include "include/rados/librados.hpp"
8#include "test/librbd/mock/MockContextWQ.h"
9#include "test/librbd/mock/MockExclusiveLock.h"
10#include "test/librbd/mock/MockImageState.h"
11#include "test/librbd/mock/MockImageWatcher.h"
12#include "test/librbd/mock/MockJournal.h"
13#include "test/librbd/mock/MockObjectMap.h"
14#include "test/librbd/mock/MockOperations.h"
15#include "test/librbd/mock/MockReadahead.h"
16#include "test/librbd/mock/io/MockImageRequestWQ.h"
11fdf7f2 17#include "test/librbd/mock/io/MockObjectDispatcher.h"
7c673cae
FG
18#include "common/RWLock.h"
19#include "common/WorkQueue.h"
31f18b77 20#include "common/zipkin_trace.h"
7c673cae
FG
21#include "librbd/ImageCtx.h"
22#include "gmock/gmock.h"
23#include <string>
24
9f95a23c
TL
25class MockSafeTimer;
26
7c673cae
FG
27namespace librbd {
28
29namespace cache { class MockImageCache; }
30namespace operation {
31template <typename> class ResizeRequest;
32}
33
34struct MockImageCtx {
35 static MockImageCtx *s_instance;
36 static MockImageCtx *create(const std::string &image_name,
37 const std::string &image_id,
38 const char *snap, librados::IoCtx& p,
39 bool read_only) {
11fdf7f2 40 ceph_assert(s_instance != nullptr);
7c673cae
FG
41 return s_instance;
42 }
43 MOCK_METHOD0(destroy, void());
44
45 MockImageCtx(librbd::ImageCtx &image_ctx)
46 : image_ctx(&image_ctx),
47 cct(image_ctx.cct),
48 perfcounter(image_ctx.perfcounter),
49 snap_namespace(image_ctx.snap_namespace),
50 snap_name(image_ctx.snap_name),
51 snap_id(image_ctx.snap_id),
52 snap_exists(image_ctx.snap_exists),
53 snapc(image_ctx.snapc),
54 snaps(image_ctx.snaps),
55 snap_info(image_ctx.snap_info),
56 snap_ids(image_ctx.snap_ids),
7c673cae
FG
57 old_format(image_ctx.old_format),
58 read_only(image_ctx.read_only),
9f95a23c
TL
59 read_only_flags(image_ctx.read_only_flags),
60 read_only_mask(image_ctx.read_only_mask),
7c673cae
FG
61 clone_copy_on_read(image_ctx.clone_copy_on_read),
62 lockers(image_ctx.lockers),
63 exclusive_locked(image_ctx.exclusive_locked),
64 lock_tag(image_ctx.lock_tag),
65 owner_lock(image_ctx.owner_lock),
9f95a23c 66 image_lock(image_ctx.image_lock),
11fdf7f2 67 timestamp_lock(image_ctx.timestamp_lock),
7c673cae 68 async_ops_lock(image_ctx.async_ops_lock),
b32b8144 69 copyup_list_lock(image_ctx.copyup_list_lock),
7c673cae
FG
70 order(image_ctx.order),
71 size(image_ctx.size),
72 features(image_ctx.features),
73 flags(image_ctx.flags),
11fdf7f2
TL
74 op_features(image_ctx.op_features),
75 operations_disabled(image_ctx.operations_disabled),
7c673cae
FG
76 stripe_unit(image_ctx.stripe_unit),
77 stripe_count(image_ctx.stripe_count),
78 object_prefix(image_ctx.object_prefix),
79 header_oid(image_ctx.header_oid),
80 id(image_ctx.id),
81 name(image_ctx.name),
82 parent_md(image_ctx.parent_md),
83 format_string(image_ctx.format_string),
84 group_spec(image_ctx.group_spec),
85 layout(image_ctx.layout),
86 io_work_queue(new io::MockImageRequestWQ()),
11fdf7f2 87 io_object_dispatcher(new io::MockObjectDispatcher()),
7c673cae
FG
88 op_work_queue(new MockContextWQ()),
89 readahead_max_bytes(image_ctx.readahead_max_bytes),
224ce89b 90 event_socket(image_ctx.event_socket),
7c673cae
FG
91 parent(NULL), operations(new MockOperations()),
92 state(new MockImageState()),
93 image_watcher(NULL), object_map(NULL),
94 exclusive_lock(NULL), journal(NULL),
31f18b77 95 trace_endpoint(image_ctx.trace_endpoint),
b32b8144 96 sparse_read_threshold_bytes(image_ctx.sparse_read_threshold_bytes),
11fdf7f2 97 discard_granularity_bytes(image_ctx.discard_granularity_bytes),
224ce89b 98 mirroring_replay_delay(image_ctx.mirroring_replay_delay),
181888fb 99 non_blocking_aio(image_ctx.non_blocking_aio),
b32b8144 100 blkin_trace_all(image_ctx.blkin_trace_all),
11fdf7f2 101 enable_alloc_hint(image_ctx.enable_alloc_hint),
92f5a8d4 102 alloc_hint_flags(image_ctx.alloc_hint_flags),
9f95a23c 103 read_flags(image_ctx.read_flags),
11fdf7f2 104 ignore_migrating(image_ctx.ignore_migrating),
9f95a23c 105 enable_sparse_copyup(image_ctx.enable_sparse_copyup),
11fdf7f2
TL
106 mtime_update_interval(image_ctx.mtime_update_interval),
107 atime_update_interval(image_ctx.atime_update_interval),
108 cache(image_ctx.cache),
109 config(image_ctx.config)
7c673cae
FG
110 {
111 md_ctx.dup(image_ctx.md_ctx);
112 data_ctx.dup(image_ctx.data_ctx);
113
114 if (image_ctx.image_watcher != NULL) {
115 image_watcher = new MockImageWatcher();
116 }
117 }
118
119 ~MockImageCtx() {
120 wait_for_async_requests();
121 image_ctx->md_ctx.aio_flush();
122 image_ctx->data_ctx.aio_flush();
123 image_ctx->op_work_queue->drain();
124 delete state;
125 delete operations;
126 delete image_watcher;
127 delete op_work_queue;
128 delete io_work_queue;
11fdf7f2 129 delete io_object_dispatcher;
7c673cae
FG
130 }
131
132 void wait_for_async_requests() {
9f95a23c 133 async_ops_lock.lock();
7c673cae 134 if (async_requests.empty()) {
9f95a23c 135 async_ops_lock.unlock();
7c673cae
FG
136 return;
137 }
138
139 C_SaferCond ctx;
140 async_requests_waiters.push_back(&ctx);
9f95a23c 141 async_ops_lock.unlock();
7c673cae
FG
142
143 ctx.wait();
144 }
145
eafe8130 146 MOCK_METHOD1(init_layout, void(int64_t));
7c673cae
FG
147
148 MOCK_CONST_METHOD1(get_object_name, std::string(uint64_t));
b32b8144 149 MOCK_CONST_METHOD0(get_object_size, uint64_t());
7c673cae
FG
150 MOCK_CONST_METHOD0(get_current_size, uint64_t());
151 MOCK_CONST_METHOD1(get_image_size, uint64_t(librados::snap_t));
152 MOCK_CONST_METHOD1(get_object_count, uint64_t(librados::snap_t));
b32b8144 153 MOCK_CONST_METHOD1(get_read_flags, int(librados::snap_t));
11fdf7f2
TL
154 MOCK_CONST_METHOD2(get_flags, int(librados::snap_t in_snap_id,
155 uint64_t *flags));
7c673cae
FG
156 MOCK_CONST_METHOD2(get_snap_id,
157 librados::snap_t(cls::rbd::SnapshotNamespace snap_namespace,
158 std::string in_snap_name));
159 MOCK_CONST_METHOD1(get_snap_info, const SnapInfo*(librados::snap_t));
11fdf7f2 160 MOCK_CONST_METHOD2(get_snap_name, int(librados::snap_t, std::string *));
7c673cae
FG
161 MOCK_CONST_METHOD2(get_snap_namespace, int(librados::snap_t,
162 cls::rbd::SnapshotNamespace *out_snap_namespace));
163 MOCK_CONST_METHOD2(get_parent_spec, int(librados::snap_t in_snap_id,
11fdf7f2
TL
164 cls::rbd::ParentImageSpec *pspec));
165 MOCK_CONST_METHOD1(get_parent_info, const ParentImageInfo*(librados::snap_t));
3efd9988
FG
166 MOCK_CONST_METHOD2(get_parent_overlap, int(librados::snap_t in_snap_id,
167 uint64_t *overlap));
b32b8144
FG
168 MOCK_CONST_METHOD2(prune_parent_extents, uint64_t(vector<pair<uint64_t,uint64_t> >& ,
169 uint64_t));
7c673cae
FG
170
171 MOCK_CONST_METHOD2(is_snap_protected, int(librados::snap_t in_snap_id,
172 bool *is_protected));
173 MOCK_CONST_METHOD2(is_snap_unprotected, int(librados::snap_t in_snap_id,
174 bool *is_unprotected));
175
11fdf7f2
TL
176 MOCK_CONST_METHOD0(get_create_timestamp, utime_t());
177 MOCK_CONST_METHOD0(get_access_timestamp, utime_t());
178 MOCK_CONST_METHOD0(get_modify_timestamp, utime_t());
179
180 MOCK_METHOD1(set_access_timestamp, void(const utime_t at));
181 MOCK_METHOD1(set_modify_timestamp, void(const utime_t at));
182
7c673cae
FG
183 MOCK_METHOD8(add_snap, void(cls::rbd::SnapshotNamespace in_snap_namespace,
184 std::string in_snap_name,
185 librados::snap_t id,
11fdf7f2 186 uint64_t in_size, const ParentImageInfo &parent,
7c673cae
FG
187 uint8_t protection_status, uint64_t flags, utime_t timestamp));
188 MOCK_METHOD3(rm_snap, void(cls::rbd::SnapshotNamespace in_snap_namespace,
189 std::string in_snap_name,
190 librados::snap_t id));
191
192 MOCK_METHOD0(user_flushed, void());
7c673cae
FG
193 MOCK_METHOD1(flush_copyup, void(Context *));
194
7c673cae
FG
195 MOCK_CONST_METHOD1(test_features, bool(uint64_t test_features));
196 MOCK_CONST_METHOD2(test_features, bool(uint64_t test_features,
9f95a23c 197 const ceph::shared_mutex &in_image_lock));
7c673cae 198
11fdf7f2
TL
199 MOCK_CONST_METHOD1(test_op_features, bool(uint64_t op_features));
200
7c673cae
FG
201 MOCK_METHOD1(cancel_async_requests, void(Context*));
202
203 MOCK_METHOD0(create_exclusive_lock, MockExclusiveLock*());
204 MOCK_METHOD1(create_object_map, MockObjectMap*(uint64_t));
205 MOCK_METHOD0(create_journal, MockJournal*());
206
207 MOCK_METHOD0(notify_update, void());
208 MOCK_METHOD1(notify_update, void(Context *));
209
224ce89b 210 MOCK_CONST_METHOD0(get_exclusive_lock_policy, exclusive_lock::Policy*());
7c673cae 211 MOCK_CONST_METHOD0(get_journal_policy, journal::Policy*());
11fdf7f2 212 MOCK_METHOD1(set_journal_policy, void(journal::Policy*));
7c673cae 213
b32b8144
FG
214 MOCK_METHOD2(apply_metadata, int(const std::map<std::string, bufferlist> &,
215 bool));
7c673cae 216
3efd9988
FG
217 MOCK_CONST_METHOD0(get_stripe_count, uint64_t());
218 MOCK_CONST_METHOD0(get_stripe_period, uint64_t());
219
9f95a23c
TL
220 static void set_timer_instance(MockSafeTimer *timer, ceph::mutex *timer_lock);
221 static void get_timer_instance(CephContext *cct, MockSafeTimer **timer,
222 ceph::mutex **timer_lock);
28e407b8 223
7c673cae
FG
224 ImageCtx *image_ctx;
225 CephContext *cct;
226 PerfCounters *perfcounter;
227
228 cls::rbd::SnapshotNamespace snap_namespace;
229 std::string snap_name;
230 uint64_t snap_id;
231 bool snap_exists;
232
233 ::SnapContext snapc;
234 std::vector<librados::snap_t> snaps;
235 std::map<librados::snap_t, SnapInfo> snap_info;
9f95a23c 236 std::map<ImageCtx::SnapKey, librados::snap_t, ImageCtx::SnapKeyComparator> snap_ids;
7c673cae 237
7c673cae
FG
238 bool old_format;
239 bool read_only;
9f95a23c
TL
240 uint32_t read_only_flags;
241 uint32_t read_only_mask;
7c673cae
FG
242
243 bool clone_copy_on_read;
244
245 std::map<rados::cls::lock::locker_id_t,
246 rados::cls::lock::locker_info_t> lockers;
247 bool exclusive_locked;
248 std::string lock_tag;
249
250 librados::IoCtx md_ctx;
251 librados::IoCtx data_ctx;
252
9f95a23c
TL
253 ceph::shared_mutex &owner_lock;
254 ceph::shared_mutex &image_lock;
255 ceph::shared_mutex &timestamp_lock;
256 ceph::mutex &async_ops_lock;
257 ceph::mutex &copyup_list_lock;
7c673cae
FG
258
259 uint8_t order;
260 uint64_t size;
261 uint64_t features;
262 uint64_t flags;
11fdf7f2
TL
263 uint64_t op_features;
264 bool operations_disabled;
7c673cae
FG
265 uint64_t stripe_unit;
266 uint64_t stripe_count;
267 std::string object_prefix;
268 std::string header_oid;
269 std::string id;
270 std::string name;
11fdf7f2
TL
271 ParentImageInfo parent_md;
272 MigrationInfo migration_info;
7c673cae
FG
273 char *format_string;
274 cls::rbd::GroupSpec group_spec;
275
276 file_layout_t layout;
277
278 xlist<operation::ResizeRequest<MockImageCtx>*> resize_reqs;
279 xlist<AsyncRequest<MockImageCtx>*> async_requests;
280 std::list<Context*> async_requests_waiters;
281
b32b8144
FG
282 std::map<uint64_t, io::CopyupRequest<MockImageCtx>*> copyup_list;
283
7c673cae 284 io::MockImageRequestWQ *io_work_queue;
11fdf7f2 285 io::MockObjectDispatcher *io_object_dispatcher;
7c673cae
FG
286 MockContextWQ *op_work_queue;
287
288 cache::MockImageCache *image_cache = nullptr;
289
290 MockReadahead readahead;
291 uint64_t readahead_max_bytes;
292
224ce89b
WB
293 EventSocket &event_socket;
294
7c673cae
FG
295 MockImageCtx *parent;
296 MockOperations *operations;
297 MockImageState *state;
298
299 MockImageWatcher *image_watcher;
300 MockObjectMap *object_map;
301 MockExclusiveLock *exclusive_lock;
302 MockJournal *journal;
303
31f18b77
FG
304 ZTracer::Endpoint trace_endpoint;
305
b32b8144 306 uint64_t sparse_read_threshold_bytes;
11fdf7f2 307 uint32_t discard_granularity_bytes;
7c673cae 308 int mirroring_replay_delay;
224ce89b 309 bool non_blocking_aio;
181888fb 310 bool blkin_trace_all;
b32b8144 311 bool enable_alloc_hint;
92f5a8d4 312 uint32_t alloc_hint_flags;
9f95a23c 313 uint32_t read_flags;
11fdf7f2 314 bool ignore_migrating;
9f95a23c 315 bool enable_sparse_copyup;
11fdf7f2
TL
316 uint64_t mtime_update_interval;
317 uint64_t atime_update_interval;
318 bool cache;
319
320 ConfigProxy config;
f6b5b4d7 321 std::set<std::string> config_overrides;
7c673cae
FG
322};
323
324} // namespace librbd
325
326#endif // CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H