1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 #ifndef CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H
5 #define CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H
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 "common/RWLock.h"
18 #include "common/WorkQueue.h"
19 #include "common/zipkin_trace.h"
20 #include "librbd/ImageCtx.h"
21 #include "gmock/gmock.h"
26 namespace cache { class MockImageCache; }
28 template <typename> class ResizeRequest;
32 static MockImageCtx *s_instance;
33 static MockImageCtx *create(const std::string &image_name,
34 const std::string &image_id,
35 const char *snap, librados::IoCtx& p,
37 assert(s_instance != nullptr);
40 MOCK_METHOD0(destroy, void());
42 MockImageCtx(librbd::ImageCtx &image_ctx)
43 : image_ctx(&image_ctx),
45 perfcounter(image_ctx.perfcounter),
46 snap_namespace(image_ctx.snap_namespace),
47 snap_name(image_ctx.snap_name),
48 snap_id(image_ctx.snap_id),
49 snap_exists(image_ctx.snap_exists),
50 snapc(image_ctx.snapc),
51 snaps(image_ctx.snaps),
52 snap_info(image_ctx.snap_info),
53 snap_ids(image_ctx.snap_ids),
54 object_cacher(image_ctx.object_cacher),
55 object_set(image_ctx.object_set),
56 old_format(image_ctx.old_format),
57 read_only(image_ctx.read_only),
58 clone_copy_on_read(image_ctx.clone_copy_on_read),
59 lockers(image_ctx.lockers),
60 exclusive_locked(image_ctx.exclusive_locked),
61 lock_tag(image_ctx.lock_tag),
62 owner_lock(image_ctx.owner_lock),
63 md_lock(image_ctx.md_lock),
64 cache_lock(image_ctx.cache_lock),
65 snap_lock(image_ctx.snap_lock),
66 parent_lock(image_ctx.parent_lock),
67 object_map_lock(image_ctx.object_map_lock),
68 async_ops_lock(image_ctx.async_ops_lock),
69 order(image_ctx.order),
71 features(image_ctx.features),
72 flags(image_ctx.flags),
73 stripe_unit(image_ctx.stripe_unit),
74 stripe_count(image_ctx.stripe_count),
75 object_prefix(image_ctx.object_prefix),
76 header_oid(image_ctx.header_oid),
79 parent_md(image_ctx.parent_md),
80 format_string(image_ctx.format_string),
81 group_spec(image_ctx.group_spec),
82 layout(image_ctx.layout),
83 io_work_queue(new io::MockImageRequestWQ()),
84 op_work_queue(new MockContextWQ()),
85 readahead_max_bytes(image_ctx.readahead_max_bytes),
86 event_socket(image_ctx.event_socket),
87 parent(NULL), operations(new MockOperations()),
88 state(new MockImageState()),
89 image_watcher(NULL), object_map(NULL),
90 exclusive_lock(NULL), journal(NULL),
91 trace_endpoint(image_ctx.trace_endpoint),
92 concurrent_management_ops(image_ctx.concurrent_management_ops),
93 blacklist_on_break_lock(image_ctx.blacklist_on_break_lock),
94 blacklist_expire_seconds(image_ctx.blacklist_expire_seconds),
95 journal_order(image_ctx.journal_order),
96 journal_splay_width(image_ctx.journal_splay_width),
97 journal_commit_age(image_ctx.journal_commit_age),
98 journal_object_flush_interval(image_ctx.journal_object_flush_interval),
99 journal_object_flush_bytes(image_ctx.journal_object_flush_bytes),
100 journal_object_flush_age(image_ctx.journal_object_flush_age),
101 journal_pool(image_ctx.journal_pool),
102 journal_max_payload_bytes(image_ctx.journal_max_payload_bytes),
103 journal_max_concurrent_object_sets(
104 image_ctx.journal_max_concurrent_object_sets),
105 mirroring_resync_after_disconnect(
106 image_ctx.mirroring_resync_after_disconnect),
107 mirroring_replay_delay(image_ctx.mirroring_replay_delay),
108 non_blocking_aio(image_ctx.non_blocking_aio),
109 blkin_trace_all(image_ctx.blkin_trace_all)
111 md_ctx.dup(image_ctx.md_ctx);
112 data_ctx.dup(image_ctx.data_ctx);
114 if (image_ctx.image_watcher != NULL) {
115 image_watcher = new MockImageWatcher();
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();
126 delete image_watcher;
127 delete op_work_queue;
128 delete io_work_queue;
131 void wait_for_async_requests() {
132 async_ops_lock.Lock();
133 if (async_requests.empty()) {
134 async_ops_lock.Unlock();
139 async_requests_waiters.push_back(&ctx);
140 async_ops_lock.Unlock();
145 MOCK_METHOD0(init_layout, void());
147 MOCK_CONST_METHOD1(get_object_name, std::string(uint64_t));
148 MOCK_CONST_METHOD0(get_current_size, uint64_t());
149 MOCK_CONST_METHOD1(get_image_size, uint64_t(librados::snap_t));
150 MOCK_CONST_METHOD1(get_object_count, uint64_t(librados::snap_t));
151 MOCK_CONST_METHOD2(get_snap_id,
152 librados::snap_t(cls::rbd::SnapshotNamespace snap_namespace,
153 std::string in_snap_name));
154 MOCK_CONST_METHOD1(get_snap_info, const SnapInfo*(librados::snap_t));
155 MOCK_CONST_METHOD2(get_snap_namespace, int(librados::snap_t,
156 cls::rbd::SnapshotNamespace *out_snap_namespace));
157 MOCK_CONST_METHOD2(get_parent_spec, int(librados::snap_t in_snap_id,
159 MOCK_CONST_METHOD2(get_parent_overlap, int(librados::snap_t in_snap_id,
162 MOCK_CONST_METHOD2(is_snap_protected, int(librados::snap_t in_snap_id,
163 bool *is_protected));
164 MOCK_CONST_METHOD2(is_snap_unprotected, int(librados::snap_t in_snap_id,
165 bool *is_unprotected));
167 MOCK_METHOD8(add_snap, void(cls::rbd::SnapshotNamespace in_snap_namespace,
168 std::string in_snap_name,
170 uint64_t in_size, const ParentInfo &parent,
171 uint8_t protection_status, uint64_t flags, utime_t timestamp));
172 MOCK_METHOD3(rm_snap, void(cls::rbd::SnapshotNamespace in_snap_namespace,
173 std::string in_snap_name,
174 librados::snap_t id));
176 MOCK_METHOD0(user_flushed, void());
177 MOCK_METHOD1(flush, void(Context *));
178 MOCK_METHOD1(flush_async_operations, void(Context *));
179 MOCK_METHOD1(flush_copyup, void(Context *));
181 MOCK_METHOD1(flush_cache, void(Context *));
182 MOCK_METHOD2(invalidate_cache, void(bool, Context *));
183 MOCK_METHOD1(shut_down_cache, void(Context *));
184 MOCK_METHOD0(is_cache_empty, bool());
186 MOCK_CONST_METHOD1(test_features, bool(uint64_t test_features));
187 MOCK_CONST_METHOD2(test_features, bool(uint64_t test_features,
188 const RWLock &in_snap_lock));
190 MOCK_METHOD1(cancel_async_requests, void(Context*));
192 MOCK_METHOD0(create_exclusive_lock, MockExclusiveLock*());
193 MOCK_METHOD1(create_object_map, MockObjectMap*(uint64_t));
194 MOCK_METHOD0(create_journal, MockJournal*());
196 MOCK_METHOD0(notify_update, void());
197 MOCK_METHOD1(notify_update, void(Context *));
199 MOCK_CONST_METHOD0(get_exclusive_lock_policy, exclusive_lock::Policy*());
201 MOCK_CONST_METHOD0(get_journal_policy, journal::Policy*());
202 MOCK_CONST_METHOD1(set_journal_policy, void(journal::Policy*));
204 MOCK_METHOD8(aio_read_from_cache, void(object_t, uint64_t, bufferlist *,
205 size_t, uint64_t, Context *, int, ZTracer::Trace *));
206 MOCK_METHOD8(write_to_cache, void(object_t, const bufferlist&, size_t,
207 uint64_t, Context *, int, uint64_t, ZTracer::Trace *));
209 MOCK_CONST_METHOD0(get_stripe_count, uint64_t());
210 MOCK_CONST_METHOD0(get_stripe_period, uint64_t());
214 PerfCounters *perfcounter;
216 cls::rbd::SnapshotNamespace snap_namespace;
217 std::string snap_name;
222 std::vector<librados::snap_t> snaps;
223 std::map<librados::snap_t, SnapInfo> snap_info;
224 std::map<std::pair<cls::rbd::SnapshotNamespace, std::string>, librados::snap_t> snap_ids;
226 ObjectCacher *object_cacher;
227 ObjectCacher::ObjectSet *object_set;
232 bool clone_copy_on_read;
234 std::map<rados::cls::lock::locker_id_t,
235 rados::cls::lock::locker_info_t> lockers;
236 bool exclusive_locked;
237 std::string lock_tag;
239 librados::IoCtx md_ctx;
240 librados::IoCtx data_ctx;
247 RWLock &object_map_lock;
248 Mutex &async_ops_lock;
254 uint64_t stripe_unit;
255 uint64_t stripe_count;
256 std::string object_prefix;
257 std::string header_oid;
260 ParentInfo parent_md;
262 cls::rbd::GroupSpec group_spec;
264 file_layout_t layout;
266 xlist<operation::ResizeRequest<MockImageCtx>*> resize_reqs;
267 xlist<AsyncRequest<MockImageCtx>*> async_requests;
268 std::list<Context*> async_requests_waiters;
270 io::MockImageRequestWQ *io_work_queue;
271 MockContextWQ *op_work_queue;
273 cache::MockImageCache *image_cache = nullptr;
275 MockReadahead readahead;
276 uint64_t readahead_max_bytes;
278 EventSocket &event_socket;
280 MockImageCtx *parent;
281 MockOperations *operations;
282 MockImageState *state;
284 MockImageWatcher *image_watcher;
285 MockObjectMap *object_map;
286 MockExclusiveLock *exclusive_lock;
287 MockJournal *journal;
289 ZTracer::Endpoint trace_endpoint;
291 int concurrent_management_ops;
292 bool blacklist_on_break_lock;
293 uint32_t blacklist_expire_seconds;
294 uint8_t journal_order;
295 uint8_t journal_splay_width;
296 double journal_commit_age;
297 int journal_object_flush_interval;
298 uint64_t journal_object_flush_bytes;
299 double journal_object_flush_age;
300 std::string journal_pool;
301 uint32_t journal_max_payload_bytes;
302 int journal_max_concurrent_object_sets;
303 bool mirroring_resync_after_disconnect;
304 int mirroring_replay_delay;
305 bool non_blocking_aio;
306 bool blkin_trace_all;
309 } // namespace librbd
311 #endif // CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H