Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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 "common/RWLock.h"
18 #include "common/WorkQueue.h"
19 #include "common/zipkin_trace.h"
20 #include "librbd/ImageCtx.h"
21 #include "gmock/gmock.h"
22 #include <string>
23
24 namespace librbd {
25
26 namespace cache { class MockImageCache; }
27 namespace operation {
28 template <typename> class ResizeRequest;
29 }
30
31 struct MockImageCtx {
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,
36                               bool read_only) {
37     assert(s_instance != nullptr);
38     return s_instance;
39   }
40   MOCK_METHOD0(destroy, void());
41
42   MockImageCtx(librbd::ImageCtx &image_ctx)
43     : image_ctx(&image_ctx),
44       cct(image_ctx.cct),
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),
70       size(image_ctx.size),
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),
77       id(image_ctx.id),
78       name(image_ctx.name),
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)
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   }
130
131   void wait_for_async_requests() {
132     async_ops_lock.Lock();
133     if (async_requests.empty()) {
134       async_ops_lock.Unlock();
135       return;
136     }
137
138     C_SaferCond ctx;
139     async_requests_waiters.push_back(&ctx);
140     async_ops_lock.Unlock();
141
142     ctx.wait();
143   }
144
145   MOCK_METHOD0(init_layout, void());
146
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,
158                                           ParentSpec *pspec));
159   MOCK_CONST_METHOD2(get_parent_overlap, int(librados::snap_t in_snap_id,
160                                              uint64_t *overlap));
161
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));
166
167   MOCK_METHOD8(add_snap, void(cls::rbd::SnapshotNamespace in_snap_namespace,
168                               std::string in_snap_name,
169                               librados::snap_t id,
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));
175
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 *));
180
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());
185
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));
189
190   MOCK_METHOD1(cancel_async_requests, void(Context*));
191
192   MOCK_METHOD0(create_exclusive_lock, MockExclusiveLock*());
193   MOCK_METHOD1(create_object_map, MockObjectMap*(uint64_t));
194   MOCK_METHOD0(create_journal, MockJournal*());
195
196   MOCK_METHOD0(notify_update, void());
197   MOCK_METHOD1(notify_update, void(Context *));
198
199   MOCK_CONST_METHOD0(get_exclusive_lock_policy, exclusive_lock::Policy*());
200
201   MOCK_CONST_METHOD0(get_journal_policy, journal::Policy*());
202   MOCK_CONST_METHOD1(set_journal_policy, void(journal::Policy*));
203
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 *));
208
209   MOCK_CONST_METHOD0(get_stripe_count, uint64_t());
210   MOCK_CONST_METHOD0(get_stripe_period, uint64_t());
211
212   ImageCtx *image_ctx;
213   CephContext *cct;
214   PerfCounters *perfcounter;
215
216   cls::rbd::SnapshotNamespace snap_namespace;
217   std::string snap_name;
218   uint64_t snap_id;
219   bool snap_exists;
220
221   ::SnapContext snapc;
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;
225
226   ObjectCacher *object_cacher;
227   ObjectCacher::ObjectSet *object_set;
228
229   bool old_format;
230   bool read_only;
231
232   bool clone_copy_on_read;
233
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;
238
239   librados::IoCtx md_ctx;
240   librados::IoCtx data_ctx;
241
242   RWLock &owner_lock;
243   RWLock &md_lock;
244   Mutex &cache_lock;
245   RWLock &snap_lock;
246   RWLock &parent_lock;
247   RWLock &object_map_lock;
248   Mutex &async_ops_lock;
249
250   uint8_t order;
251   uint64_t size;
252   uint64_t features;
253   uint64_t flags;
254   uint64_t stripe_unit;
255   uint64_t stripe_count;
256   std::string object_prefix;
257   std::string header_oid;
258   std::string id;
259   std::string name;
260   ParentInfo parent_md;
261   char *format_string;
262   cls::rbd::GroupSpec group_spec;
263
264   file_layout_t layout;
265
266   xlist<operation::ResizeRequest<MockImageCtx>*> resize_reqs;
267   xlist<AsyncRequest<MockImageCtx>*> async_requests;
268   std::list<Context*> async_requests_waiters;
269
270   io::MockImageRequestWQ *io_work_queue;
271   MockContextWQ *op_work_queue;
272
273   cache::MockImageCache *image_cache = nullptr;
274
275   MockReadahead readahead;
276   uint64_t readahead_max_bytes;
277
278   EventSocket &event_socket;
279
280   MockImageCtx *parent;
281   MockOperations *operations;
282   MockImageState *state;
283
284   MockImageWatcher *image_watcher;
285   MockObjectMap *object_map;
286   MockExclusiveLock *exclusive_lock;
287   MockJournal *journal;
288
289   ZTracer::Endpoint trace_endpoint;
290
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;
307 };
308
309 } // namespace librbd
310
311 #endif // CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H