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