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 "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"
29 namespace cache
{ class MockImageCache
; }
31 template <typename
> class ResizeRequest
;
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
,
40 ceph_assert(s_instance
!= nullptr);
43 MOCK_METHOD0(destroy
, void());
45 MockImageCtx(librbd::ImageCtx
&image_ctx
)
46 : image_ctx(&image_ctx
),
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
),
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
),
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
)
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
;
129 delete io_object_dispatcher
;
132 void wait_for_async_requests() {
133 async_ops_lock
.lock();
134 if (async_requests
.empty()) {
135 async_ops_lock
.unlock();
140 async_requests_waiters
.push_back(&ctx
);
141 async_ops_lock
.unlock();
146 MOCK_METHOD1(init_layout
, void(int64_t));
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
,
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
,
168 MOCK_CONST_METHOD2(prune_parent_extents
, uint64_t(vector
<pair
<uint64_t,uint64_t> >& ,
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
));
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());
180 MOCK_METHOD1(set_access_timestamp
, void(const utime_t at
));
181 MOCK_METHOD1(set_modify_timestamp
, void(const utime_t at
));
183 MOCK_METHOD8(add_snap
, void(cls::rbd::SnapshotNamespace in_snap_namespace
,
184 std::string in_snap_name
,
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
));
192 MOCK_METHOD0(user_flushed
, void());
193 MOCK_METHOD1(flush_copyup
, void(Context
*));
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
));
199 MOCK_CONST_METHOD1(test_op_features
, bool(uint64_t op_features
));
201 MOCK_METHOD1(cancel_async_requests
, void(Context
*));
203 MOCK_METHOD0(create_exclusive_lock
, MockExclusiveLock
*());
204 MOCK_METHOD1(create_object_map
, MockObjectMap
*(uint64_t));
205 MOCK_METHOD0(create_journal
, MockJournal
*());
207 MOCK_METHOD0(notify_update
, void());
208 MOCK_METHOD1(notify_update
, void(Context
*));
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
*));
214 MOCK_METHOD2(apply_metadata
, int(const std::map
<std::string
, bufferlist
> &,
217 MOCK_CONST_METHOD0(get_stripe_count
, uint64_t());
218 MOCK_CONST_METHOD0(get_stripe_period
, uint64_t());
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
);
226 PerfCounters
*perfcounter
;
228 cls::rbd::SnapshotNamespace snap_namespace
;
229 std::string snap_name
;
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
;
240 uint32_t read_only_flags
;
241 uint32_t read_only_mask
;
243 bool clone_copy_on_read
;
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
;
250 librados::IoCtx md_ctx
;
251 librados::IoCtx data_ctx
;
253 ceph::shared_mutex
&owner_lock
;
254 ceph::shared_mutex
&image_lock
;
255 ceph::shared_mutex
×tamp_lock
;
256 ceph::mutex
&async_ops_lock
;
257 ceph::mutex
©up_list_lock
;
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
;
271 ParentImageInfo parent_md
;
272 MigrationInfo migration_info
;
274 cls::rbd::GroupSpec group_spec
;
276 file_layout_t layout
;
278 xlist
<operation::ResizeRequest
<MockImageCtx
>*> resize_reqs
;
279 xlist
<AsyncRequest
<MockImageCtx
>*> async_requests
;
280 std::list
<Context
*> async_requests_waiters
;
282 std::map
<uint64_t, io::CopyupRequest
<MockImageCtx
>*> copyup_list
;
284 io::MockImageRequestWQ
*io_work_queue
;
285 io::MockObjectDispatcher
*io_object_dispatcher
;
286 MockContextWQ
*op_work_queue
;
288 cache::MockImageCache
*image_cache
= nullptr;
290 MockReadahead readahead
;
291 uint64_t readahead_max_bytes
;
293 EventSocket
&event_socket
;
295 MockImageCtx
*parent
;
296 MockOperations
*operations
;
297 MockImageState
*state
;
299 MockImageWatcher
*image_watcher
;
300 MockObjectMap
*object_map
;
301 MockExclusiveLock
*exclusive_lock
;
302 MockJournal
*journal
;
304 ZTracer::Endpoint trace_endpoint
;
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
;
314 bool ignore_migrating
;
315 bool enable_sparse_copyup
;
316 uint64_t mtime_update_interval
;
317 uint64_t atime_update_interval
;
321 std::set
<std::string
> config_overrides
;
324 } // namespace librbd
326 #endif // CEPH_TEST_LIBRBD_MOCK_IMAGE_CTX_H