]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/librbd/mock/MockImageCtx.h
import 15.2.5
[ceph.git] / ceph / src / test / librbd / mock / MockImageCtx.h
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"
17 #include "test/librbd/mock/io/MockObjectDispatcher.h"
18 #include "common/RWLock.h"
19 #include "common/WorkQueue.h"
20 #include "common/zipkin_trace.h"
21 #include "librbd/ImageCtx.h"
22 #include "gmock/gmock.h"
23 #include <string>
24
25 class MockSafeTimer;
26
27 namespace librbd {
28
29 namespace cache { class MockImageCache; }
30 namespace operation {
31 template <typename> class ResizeRequest;
32 }
33
34 struct 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) {
40 ceph_assert(s_instance != nullptr);
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),
57 old_format(image_ctx.old_format),
58 read_only(image_ctx.read_only),
59 read_only_flags(image_ctx.read_only_flags),
60 read_only_mask(image_ctx.read_only_mask),
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),
66 image_lock(image_ctx.image_lock),
67 timestamp_lock(image_ctx.timestamp_lock),
68 async_ops_lock(image_ctx.async_ops_lock),
69 copyup_list_lock(image_ctx.copyup_list_lock),
70 order(image_ctx.order),
71 size(image_ctx.size),
72 features(image_ctx.features),
73 flags(image_ctx.flags),
74 op_features(image_ctx.op_features),
75 operations_disabled(image_ctx.operations_disabled),
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()),
87 io_object_dispatcher(new io::MockObjectDispatcher()),
88 op_work_queue(new MockContextWQ()),
89 readahead_max_bytes(image_ctx.readahead_max_bytes),
90 event_socket(image_ctx.event_socket),
91 parent(NULL), operations(new MockOperations()),
92 state(new MockImageState()),
93 image_watcher(NULL), object_map(NULL),
94 exclusive_lock(NULL), journal(NULL),
95 trace_endpoint(image_ctx.trace_endpoint),
96 sparse_read_threshold_bytes(image_ctx.sparse_read_threshold_bytes),
97 discard_granularity_bytes(image_ctx.discard_granularity_bytes),
98 mirroring_replay_delay(image_ctx.mirroring_replay_delay),
99 non_blocking_aio(image_ctx.non_blocking_aio),
100 blkin_trace_all(image_ctx.blkin_trace_all),
101 enable_alloc_hint(image_ctx.enable_alloc_hint),
102 alloc_hint_flags(image_ctx.alloc_hint_flags),
103 read_flags(image_ctx.read_flags),
104 ignore_migrating(image_ctx.ignore_migrating),
105 enable_sparse_copyup(image_ctx.enable_sparse_copyup),
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)
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;
129 delete io_object_dispatcher;
130 }
131
132 void wait_for_async_requests() {
133 async_ops_lock.lock();
134 if (async_requests.empty()) {
135 async_ops_lock.unlock();
136 return;
137 }
138
139 C_SaferCond ctx;
140 async_requests_waiters.push_back(&ctx);
141 async_ops_lock.unlock();
142
143 ctx.wait();
144 }
145
146 MOCK_METHOD1(init_layout, void(int64_t));
147
148 MOCK_CONST_METHOD1(get_object_name, std::string(uint64_t));
149 MOCK_CONST_METHOD0(get_object_size, uint64_t());
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));
153 MOCK_CONST_METHOD1(get_read_flags, int(librados::snap_t));
154 MOCK_CONST_METHOD2(get_flags, int(librados::snap_t in_snap_id,
155 uint64_t *flags));
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));
160 MOCK_CONST_METHOD2(get_snap_name, int(librados::snap_t, std::string *));
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,
164 cls::rbd::ParentImageSpec *pspec));
165 MOCK_CONST_METHOD1(get_parent_info, const ParentImageInfo*(librados::snap_t));
166 MOCK_CONST_METHOD2(get_parent_overlap, int(librados::snap_t in_snap_id,
167 uint64_t *overlap));
168 MOCK_CONST_METHOD2(prune_parent_extents, uint64_t(vector<pair<uint64_t,uint64_t> >& ,
169 uint64_t));
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
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
183 MOCK_METHOD8(add_snap, void(cls::rbd::SnapshotNamespace in_snap_namespace,
184 std::string in_snap_name,
185 librados::snap_t id,
186 uint64_t in_size, const ParentImageInfo &parent,
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());
193 MOCK_METHOD1(flush_copyup, void(Context *));
194
195 MOCK_CONST_METHOD1(test_features, bool(uint64_t test_features));
196 MOCK_CONST_METHOD2(test_features, bool(uint64_t test_features,
197 const ceph::shared_mutex &in_image_lock));
198
199 MOCK_CONST_METHOD1(test_op_features, bool(uint64_t op_features));
200
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
210 MOCK_CONST_METHOD0(get_exclusive_lock_policy, exclusive_lock::Policy*());
211 MOCK_CONST_METHOD0(get_journal_policy, journal::Policy*());
212 MOCK_METHOD1(set_journal_policy, void(journal::Policy*));
213
214 MOCK_METHOD2(apply_metadata, int(const std::map<std::string, bufferlist> &,
215 bool));
216
217 MOCK_CONST_METHOD0(get_stripe_count, uint64_t());
218 MOCK_CONST_METHOD0(get_stripe_period, uint64_t());
219
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);
223
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;
236 std::map<ImageCtx::SnapKey, librados::snap_t, ImageCtx::SnapKeyComparator> snap_ids;
237
238 bool old_format;
239 bool read_only;
240 uint32_t read_only_flags;
241 uint32_t read_only_mask;
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
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;
258
259 uint8_t order;
260 uint64_t size;
261 uint64_t features;
262 uint64_t flags;
263 uint64_t op_features;
264 bool operations_disabled;
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;
271 ParentImageInfo parent_md;
272 MigrationInfo migration_info;
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
282 std::map<uint64_t, io::CopyupRequest<MockImageCtx>*> copyup_list;
283
284 io::MockImageRequestWQ *io_work_queue;
285 io::MockObjectDispatcher *io_object_dispatcher;
286 MockContextWQ *op_work_queue;
287
288 cache::MockImageCache *image_cache = nullptr;
289
290 MockReadahead readahead;
291 uint64_t readahead_max_bytes;
292
293 EventSocket &event_socket;
294
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
304 ZTracer::Endpoint trace_endpoint;
305
306 uint64_t sparse_read_threshold_bytes;
307 uint32_t discard_granularity_bytes;
308 int mirroring_replay_delay;
309 bool non_blocking_aio;
310 bool blkin_trace_all;
311 bool enable_alloc_hint;
312 uint32_t alloc_hint_flags;
313 uint32_t read_flags;
314 bool ignore_migrating;
315 bool enable_sparse_copyup;
316 uint64_t mtime_update_interval;
317 uint64_t atime_update_interval;
318 bool cache;
319
320 ConfigProxy config;
321 std::set<std::string> config_overrides;
322 };
323
324 } // namespace librbd
325
326 #endif // CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H