1 # vim: expandtab smarttab shiftwidth=4 softtabstop=4
8 from datetime
import datetime
9 from nose
import with_setup
, SkipTest
10 from nose
.tools
import eq_
as eq
, assert_raises
, assert_not_equal
11 from rados
import (Rados
,
12 LIBRADOS_OP_FLAG_FADVISE_DONTNEED
,
13 LIBRADOS_OP_FLAG_FADVISE_NOCACHE
,
14 LIBRADOS_OP_FLAG_FADVISE_RANDOM
)
15 from rbd
import (RBD
, Image
, ImageNotFound
, InvalidArgument
, ImageExists
,
16 ImageBusy
, ImageHasSnapshots
, ReadOnlyImage
,
17 FunctionNotSupported
, ArgumentOutOfRange
,
18 DiskQuotaExceeded
, ConnectionShutdown
, PermissionError
,
19 RBD_FEATURE_LAYERING
, RBD_FEATURE_STRIPINGV2
,
20 RBD_FEATURE_EXCLUSIVE_LOCK
, RBD_FEATURE_JOURNALING
,
21 RBD_MIRROR_MODE_DISABLED
, RBD_MIRROR_MODE_IMAGE
,
22 RBD_MIRROR_MODE_POOL
, RBD_MIRROR_IMAGE_ENABLED
,
23 RBD_MIRROR_IMAGE_DISABLED
, MIRROR_IMAGE_STATUS_STATE_UNKNOWN
,
24 RBD_LOCK_MODE_EXCLUSIVE
)
33 IMG_SIZE
= 8 << 20 # 8 MiB
34 IMG_ORDER
= 22 # 4 MiB objects
38 rados
= Rados(conffile
='')
41 pool_name
= get_temp_pool_name()
42 rados
.create_pool(pool_name
)
44 ioctx
= rados
.open_ioctx(pool_name
)
45 ioctx
.application_enable('rbd')
47 features
= os
.getenv("RBD_FEATURES")
48 features
= int(features
) if features
is not None else 61
50 def teardown_module():
54 rados
.delete_pool(pool_name
)
57 def get_temp_pool_name():
60 return "test-rbd-api-" + socket
.gethostname() + '-' + str(os
.getpid()) + \
63 def get_temp_image_name():
66 return "image" + str(image_idx
)
70 image_name
= get_temp_image_name()
71 if features
is not None:
72 RBD().create(ioctx
, image_name
, IMG_SIZE
, IMG_ORDER
, old_format
=False,
73 features
=int(features
))
75 RBD().create(ioctx
, image_name
, IMG_SIZE
, IMG_ORDER
, old_format
=True)
78 if image_name
is not None:
79 RBD().remove(ioctx
, image_name
)
81 def require_new_format():
83 def _require_new_format(*args
, **kwargs
):
87 return fn(*args
, **kwargs
)
88 return functools
.wraps(fn
)(_require_new_format
)
91 def require_features(required_features
):
93 def _require_features(*args
, **kwargs
):
97 for feature
in required_features
:
98 if feature
& features
!= feature
:
100 return fn(*args
, **kwargs
)
101 return functools
.wraps(fn
)(_require_features
)
104 def blacklist_features(blacklisted_features
):
106 def _blacklist_features(*args
, **kwargs
):
108 for feature
in blacklisted_features
:
109 if features
is not None and feature
& features
== feature
:
111 return fn(*args
, **kwargs
)
112 return functools
.wraps(fn
)(_blacklist_features
)
122 def check_default_params(format
, order
=None, features
=None, stripe_count
=None,
123 stripe_unit
=None, exception
=None):
127 for k
in ['rbd_default_format', 'rbd_default_order', 'rbd_default_features',
128 'rbd_default_stripe_count', 'rbd_default_stripe_unit']:
129 orig_vals
[k
] = rados
.conf_get(k
)
131 rados
.conf_set('rbd_default_format', str(format
))
132 if order
is not None:
133 rados
.conf_set('rbd_default_order', str(order
or 0))
134 if features
is not None:
135 rados
.conf_set('rbd_default_features', str(features
or 0))
136 if stripe_count
is not None:
137 rados
.conf_set('rbd_default_stripe_count', str(stripe_count
or 0))
138 if stripe_unit
is not None:
139 rados
.conf_set('rbd_default_stripe_unit', str(stripe_unit
or 0))
140 feature_data_pool
= 0
141 datapool
= rados
.conf_get('rbd_default_data_pool')
142 if not len(datapool
) == 0:
143 feature_data_pool
= 128
144 image_name
= get_temp_image_name()
145 if exception
is None:
146 RBD().create(ioctx
, image_name
, IMG_SIZE
)
148 with
Image(ioctx
, image_name
) as image
:
149 eq(format
== 1, image
.old_format())
151 expected_order
= int(rados
.conf_get('rbd_default_order'))
152 actual_order
= image
.stat()['order']
153 eq(expected_order
, actual_order
)
155 expected_features
= features
157 expected_features
= 0
158 elif expected_features
is None:
159 expected_features
= 61 | feature_data_pool
161 expected_features |
= feature_data_pool
162 eq(expected_features
, image
.features())
164 expected_stripe_count
= stripe_count
165 if not expected_stripe_count
or format
== 1 or \
166 features
& RBD_FEATURE_STRIPINGV2
== 0:
167 expected_stripe_count
= 1
168 eq(expected_stripe_count
, image
.stripe_count())
170 expected_stripe_unit
= stripe_unit
171 if not expected_stripe_unit
or format
== 1 or \
172 features
& RBD_FEATURE_STRIPINGV2
== 0:
173 expected_stripe_unit
= 1 << actual_order
174 eq(expected_stripe_unit
, image
.stripe_unit())
176 RBD().remove(ioctx
, image_name
)
178 assert_raises(exception
, RBD().create
, ioctx
, image_name
, IMG_SIZE
)
180 for k
, v
in orig_vals
.items():
183 def test_create_defaults():
184 # basic format 1 and 2
185 check_default_params(1)
186 check_default_params(2)
188 check_default_params(1, 0, exception
=ArgumentOutOfRange
)
189 check_default_params(2, 0, exception
=ArgumentOutOfRange
)
190 check_default_params(1, 11, exception
=ArgumentOutOfRange
)
191 check_default_params(2, 11, exception
=ArgumentOutOfRange
)
192 check_default_params(1, 65, exception
=ArgumentOutOfRange
)
193 check_default_params(2, 65, exception
=ArgumentOutOfRange
)
194 # striping and features are ignored for format 1
195 check_default_params(1, 20, 0, 1, 1)
196 check_default_params(1, 20, 3, 1, 1)
197 check_default_params(1, 20, 0, 0, 0)
198 # striping is ignored if stripingv2 is not set
199 check_default_params(2, 20, 0, 1, 1 << 20)
200 check_default_params(2, 20, RBD_FEATURE_LAYERING
, 1, 1 << 20)
201 check_default_params(2, 20, 0, 0, 0)
202 # striping with stripingv2 is fine
203 check_default_params(2, 20, RBD_FEATURE_STRIPINGV2
, 1, 1 << 16)
204 check_default_params(2, 20, RBD_FEATURE_STRIPINGV2
, 10, 1 << 20)
205 check_default_params(2, 20, RBD_FEATURE_STRIPINGV2
, 10, 1 << 16)
206 check_default_params(2, 20, 0, 0, 0)
207 # make sure invalid combinations of stripe unit and order are still invalid
208 check_default_params(2, 22, RBD_FEATURE_STRIPINGV2
, 10, 1 << 50, exception
=InvalidArgument
)
209 check_default_params(2, 22, RBD_FEATURE_STRIPINGV2
, 10, 100, exception
=InvalidArgument
)
210 check_default_params(2, 22, RBD_FEATURE_STRIPINGV2
, 0, 1, exception
=InvalidArgument
)
211 check_default_params(2, 22, RBD_FEATURE_STRIPINGV2
, 1, 0, exception
=InvalidArgument
)
212 # 0 stripe unit and count are still ignored
213 check_default_params(2, 22, 0, 0, 0)
215 def test_context_manager():
216 with
Rados(conffile
='') as cluster
:
217 with cluster
.open_ioctx(pool_name
) as ioctx
:
218 image_name
= get_temp_image_name()
219 RBD().create(ioctx
, image_name
, IMG_SIZE
)
220 with
Image(ioctx
, image_name
) as image
:
221 data
= rand_data(256)
223 read
= image
.read(0, 256)
224 RBD().remove(ioctx
, image_name
)
227 def test_open_read_only():
228 with
Rados(conffile
='') as cluster
:
229 with cluster
.open_ioctx(pool_name
) as ioctx
:
230 image_name
= get_temp_image_name()
231 RBD().create(ioctx
, image_name
, IMG_SIZE
)
232 data
= rand_data(256)
233 with
Image(ioctx
, image_name
) as image
:
235 image
.create_snap('snap')
236 with
Image(ioctx
, image_name
, read_only
=True) as image
:
237 read
= image
.read(0, 256)
239 assert_raises(ReadOnlyImage
, image
.write
, data
, 0)
240 assert_raises(ReadOnlyImage
, image
.create_snap
, 'test')
241 assert_raises(ReadOnlyImage
, image
.remove_snap
, 'snap')
242 assert_raises(ReadOnlyImage
, image
.rollback_to_snap
, 'snap')
243 assert_raises(ReadOnlyImage
, image
.protect_snap
, 'snap')
244 assert_raises(ReadOnlyImage
, image
.unprotect_snap
, 'snap')
245 assert_raises(ReadOnlyImage
, image
.unprotect_snap
, 'snap')
246 assert_raises(ReadOnlyImage
, image
.flatten
)
247 with
Image(ioctx
, image_name
) as image
:
248 image
.remove_snap('snap')
249 RBD().remove(ioctx
, image_name
)
254 image_name
= get_temp_image_name()
255 assert_raises(ImageNotFound
, Image
, ioctx
, image_name
+ 'dne')
256 assert_raises(ImageNotFound
, Image
, ioctx
, image_name
, 'snap')
258 def test_open_readonly_dne():
260 image_name
= get_temp_image_name()
261 assert_raises(ImageNotFound
, Image
, ioctx
, image_name
+ 'dne',
263 assert_raises(ImageNotFound
, Image
, ioctx
, image_name
, 'snap',
266 def test_remove_dne():
267 assert_raises(ImageNotFound
, remove_image
)
269 def test_list_empty():
270 eq([], RBD().list(ioctx
))
272 @with_setup(create_image
, remove_image
)
274 eq([image_name
], RBD().list(ioctx
))
276 @with_setup(create_image
, remove_image
)
279 image_name2
= get_temp_image_name()
280 rbd
.rename(ioctx
, image_name
, image_name2
)
281 eq([image_name2
], rbd
.list(ioctx
))
282 rbd
.rename(ioctx
, image_name2
, image_name
)
283 eq([image_name
], rbd
.list(ioctx
))
286 return os
.urandom(size
)
288 def check_stat(info
, size
, order
):
289 assert 'block_name_prefix' in info
290 eq(info
['size'], size
)
291 eq(info
['order'], order
)
292 eq(info
['num_objs'], size
// (1 << order
))
293 eq(info
['obj_size'], 1 << order
)
295 class TestImage(object):
300 self
.image
= Image(ioctx
, image_name
)
307 @require_new_format()
308 @blacklist_features([RBD_FEATURE_EXCLUSIVE_LOCK
])
309 def test_update_features(self
):
310 features
= self
.image
.features()
311 self
.image
.update_features(RBD_FEATURE_EXCLUSIVE_LOCK
, True)
312 eq(features | RBD_FEATURE_EXCLUSIVE_LOCK
, self
.image
.features())
314 @require_features([RBD_FEATURE_STRIPINGV2
])
315 def test_create_with_params(self
):
317 image_name
= get_temp_image_name()
319 stripe_unit
= 1 << 20
321 self
.rbd
.create(ioctx
, image_name
, IMG_SIZE
, order
,
322 False, features
, stripe_unit
, stripe_count
)
323 image
= Image(ioctx
, image_name
)
325 check_stat(info
, IMG_SIZE
, order
)
326 eq(image
.features(), features
)
327 eq(image
.stripe_unit(), stripe_unit
)
328 eq(image
.stripe_count(), stripe_count
)
330 RBD().remove(ioctx
, image_name
)
332 @require_new_format()
334 assert_not_equal(b
'', self
.image
.id())
336 def test_block_name_prefix(self
):
337 assert_not_equal(b
'', self
.image
.block_name_prefix())
339 def test_create_timestamp(self
):
340 timestamp
= self
.image
.create_timestamp()
341 assert_not_equal(0, timestamp
.year
)
342 assert_not_equal(1970, timestamp
.year
)
344 def test_invalidate_cache(self
):
345 self
.image
.write(b
'abc', 0)
346 eq(b
'abc', self
.image
.read(0, 3))
347 self
.image
.invalidate_cache()
348 eq(b
'abc', self
.image
.read(0, 3))
351 info
= self
.image
.stat()
352 check_stat(info
, IMG_SIZE
, IMG_ORDER
)
354 def test_flags(self
):
355 flags
= self
.image
.flags()
358 def test_image_auto_close(self
):
359 image
= Image(ioctx
, image_name
)
361 def test_write(self
):
362 data
= rand_data(256)
363 self
.image
.write(data
, 0)
365 def test_write_with_fadvise_flags(self
):
366 data
= rand_data(256)
367 self
.image
.write(data
, 0, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
)
368 self
.image
.write(data
, 0, LIBRADOS_OP_FLAG_FADVISE_NOCACHE
)
371 data
= self
.image
.read(0, 20)
374 def test_read_with_fadvise_flags(self
):
375 data
= self
.image
.read(0, 20, LIBRADOS_OP_FLAG_FADVISE_DONTNEED
)
377 data
= self
.image
.read(0, 20, LIBRADOS_OP_FLAG_FADVISE_RANDOM
)
380 def test_large_write(self
):
381 data
= rand_data(IMG_SIZE
)
382 self
.image
.write(data
, 0)
384 def test_large_read(self
):
385 data
= self
.image
.read(0, IMG_SIZE
)
386 eq(data
, b
'\0' * IMG_SIZE
)
388 def test_write_read(self
):
389 data
= rand_data(256)
391 self
.image
.write(data
, offset
)
392 read
= self
.image
.read(offset
, 256)
395 def test_read_bad_offset(self
):
396 assert_raises(InvalidArgument
, self
.image
.read
, IMG_SIZE
+ 1, IMG_SIZE
)
398 def test_resize(self
):
399 new_size
= IMG_SIZE
* 2
400 self
.image
.resize(new_size
)
401 info
= self
.image
.stat()
402 check_stat(info
, new_size
, IMG_ORDER
)
405 eq(IMG_SIZE
, self
.image
.size())
406 self
.image
.create_snap('snap1')
407 new_size
= IMG_SIZE
* 2
408 self
.image
.resize(new_size
)
409 eq(new_size
, self
.image
.size())
410 self
.image
.create_snap('snap2')
411 self
.image
.set_snap('snap2')
412 eq(new_size
, self
.image
.size())
413 self
.image
.set_snap('snap1')
414 eq(IMG_SIZE
, self
.image
.size())
415 self
.image
.set_snap(None)
416 eq(new_size
, self
.image
.size())
417 self
.image
.remove_snap('snap1')
418 self
.image
.remove_snap('snap2')
420 def test_resize_down(self
):
421 new_size
= IMG_SIZE
// 2
422 data
= rand_data(256)
423 self
.image
.write(data
, IMG_SIZE
// 2);
424 self
.image
.resize(new_size
)
425 self
.image
.resize(IMG_SIZE
)
426 read
= self
.image
.read(IMG_SIZE
// 2, 256)
427 eq(b
'\0' * 256, read
)
429 def test_resize_bytes(self
):
430 new_size
= IMG_SIZE
// 2 - 5
431 data
= rand_data(256)
432 self
.image
.write(data
, IMG_SIZE
// 2 - 10);
433 self
.image
.resize(new_size
)
434 self
.image
.resize(IMG_SIZE
)
435 read
= self
.image
.read(IMG_SIZE
// 2 - 10, 5)
437 read
= self
.image
.read(IMG_SIZE
// 2 - 5, 251)
438 eq(b
'\0' * 251, read
)
440 def _test_copy(self
, features
=None, order
=None, stripe_unit
=None,
443 data
= rand_data(256)
444 self
.image
.write(data
, 256)
445 image_name
= get_temp_image_name()
447 self
.image
.copy(ioctx
, image_name
)
449 self
.image
.copy(ioctx
, image_name
, features
)
450 elif stripe_unit
is None:
451 self
.image
.copy(ioctx
, image_name
, features
, order
)
452 elif stripe_count
is None:
453 self
.image
.copy(ioctx
, image_name
, features
, order
, stripe_unit
)
455 self
.image
.copy(ioctx
, image_name
, features
, order
, stripe_unit
,
457 assert_raises(ImageExists
, self
.image
.copy
, ioctx
, image_name
)
458 copy
= Image(ioctx
, image_name
)
459 copy_data
= copy
.read(256, 256)
461 self
.rbd
.remove(ioctx
, image_name
)
467 def test_copy2(self
):
468 self
._test
_copy
(self
.image
.features(), self
.image
.stat()['order'])
470 @require_features([RBD_FEATURE_STRIPINGV2
])
471 def test_copy3(self
):
473 self
._test
_copy
(features
, self
.image
.stat()['order'],
474 self
.image
.stripe_unit(), self
.image
.stripe_count())
476 def test_create_snap(self
):
478 self
.image
.create_snap('snap1')
479 read
= self
.image
.read(0, 256)
480 eq(read
, b
'\0' * 256)
481 data
= rand_data(256)
482 self
.image
.write(data
, 0)
483 read
= self
.image
.read(0, 256)
485 at_snapshot
= Image(ioctx
, image_name
, 'snap1')
486 snap_data
= at_snapshot
.read(0, 256)
488 eq(snap_data
, b
'\0' * 256)
489 self
.image
.remove_snap('snap1')
491 def test_list_snaps(self
):
492 eq([], list(self
.image
.list_snaps()))
493 self
.image
.create_snap('snap1')
494 eq(['snap1'], [snap
['name'] for snap
in self
.image
.list_snaps()])
495 self
.image
.create_snap('snap2')
496 eq(['snap1', 'snap2'], [snap
['name'] for snap
in self
.image
.list_snaps()])
497 self
.image
.remove_snap('snap1')
498 self
.image
.remove_snap('snap2')
500 def test_list_snaps_iterator_auto_close(self
):
501 self
.image
.create_snap('snap1')
502 self
.image
.list_snaps()
503 self
.image
.remove_snap('snap1')
505 def test_remove_snap(self
):
506 eq([], list(self
.image
.list_snaps()))
507 self
.image
.create_snap('snap1')
508 eq(['snap1'], [snap
['name'] for snap
in self
.image
.list_snaps()])
509 self
.image
.remove_snap('snap1')
510 eq([], list(self
.image
.list_snaps()))
512 def test_rename_snap(self
):
513 eq([], list(self
.image
.list_snaps()))
514 self
.image
.create_snap('snap1')
515 eq(['snap1'], [snap
['name'] for snap
in self
.image
.list_snaps()])
516 self
.image
.rename_snap("snap1", "snap1-rename")
517 eq(['snap1-rename'], [snap
['name'] for snap
in self
.image
.list_snaps()])
518 self
.image
.remove_snap('snap1-rename')
519 eq([], list(self
.image
.list_snaps()))
521 @require_features([RBD_FEATURE_LAYERING
])
522 def test_protect_snap(self
):
523 self
.image
.create_snap('snap1')
524 assert(not self
.image
.is_protected_snap('snap1'))
525 self
.image
.protect_snap('snap1')
526 assert(self
.image
.is_protected_snap('snap1'))
527 assert_raises(ImageBusy
, self
.image
.remove_snap
, 'snap1')
528 self
.image
.unprotect_snap('snap1')
529 assert(not self
.image
.is_protected_snap('snap1'))
530 self
.image
.remove_snap('snap1')
531 assert_raises(ImageNotFound
, self
.image
.unprotect_snap
, 'snap1')
532 assert_raises(ImageNotFound
, self
.image
.is_protected_snap
, 'snap1')
534 def test_snap_timestamp(self
):
535 self
.image
.create_snap('snap1')
536 eq(['snap1'], [snap
['name'] for snap
in self
.image
.list_snaps()])
537 for snap
in self
.image
.list_snaps():
539 time
= self
.image
.get_snap_timestamp(snap_id
)
540 assert_not_equal(b
'', time
.year
)
541 assert_not_equal(0, time
.year
)
542 assert_not_equal(time
.year
, '1970')
543 self
.image
.remove_snap('snap1')
545 def test_limit_snaps(self
):
546 self
.image
.set_snap_limit(2)
547 eq(2, self
.image
.get_snap_limit())
548 self
.image
.create_snap('snap1')
549 self
.image
.create_snap('snap2')
550 assert_raises(DiskQuotaExceeded
, self
.image
.create_snap
, 'snap3')
551 self
.image
.remove_snap_limit()
552 self
.image
.create_snap('snap3')
554 self
.image
.remove_snap('snap1')
555 self
.image
.remove_snap('snap2')
556 self
.image
.remove_snap('snap3')
558 @require_features([RBD_FEATURE_EXCLUSIVE_LOCK
])
559 def test_remove_with_exclusive_lock(self
):
560 assert_raises(ImageBusy
, remove_image
)
562 @blacklist_features([RBD_FEATURE_EXCLUSIVE_LOCK
])
563 def test_remove_with_snap(self
):
564 self
.image
.create_snap('snap1')
565 assert_raises(ImageHasSnapshots
, remove_image
)
566 self
.image
.remove_snap('snap1')
568 @blacklist_features([RBD_FEATURE_EXCLUSIVE_LOCK
])
569 def test_remove_with_watcher(self
):
570 data
= rand_data(256)
571 self
.image
.write(data
, 0)
572 assert_raises(ImageBusy
, remove_image
)
573 read
= self
.image
.read(0, 256)
576 def test_rollback_to_snap(self
):
577 self
.image
.write(b
'\0' * 256, 0)
578 self
.image
.create_snap('snap1')
579 read
= self
.image
.read(0, 256)
580 eq(read
, b
'\0' * 256)
581 data
= rand_data(256)
582 self
.image
.write(data
, 0)
583 read
= self
.image
.read(0, 256)
585 self
.image
.rollback_to_snap('snap1')
586 read
= self
.image
.read(0, 256)
587 eq(read
, b
'\0' * 256)
588 self
.image
.remove_snap('snap1')
590 def test_rollback_to_snap_sparse(self
):
591 self
.image
.create_snap('snap1')
592 read
= self
.image
.read(0, 256)
593 eq(read
, b
'\0' * 256)
594 data
= rand_data(256)
595 self
.image
.write(data
, 0)
596 read
= self
.image
.read(0, 256)
598 self
.image
.rollback_to_snap('snap1')
599 read
= self
.image
.read(0, 256)
600 eq(read
, b
'\0' * 256)
601 self
.image
.remove_snap('snap1')
603 def test_rollback_with_resize(self
):
604 read
= self
.image
.read(0, 256)
605 eq(read
, b
'\0' * 256)
606 data
= rand_data(256)
607 self
.image
.write(data
, 0)
608 self
.image
.create_snap('snap1')
609 read
= self
.image
.read(0, 256)
611 new_size
= IMG_SIZE
* 2
612 self
.image
.resize(new_size
)
613 check_stat(self
.image
.stat(), new_size
, IMG_ORDER
)
614 self
.image
.write(data
, new_size
- 256)
615 self
.image
.create_snap('snap2')
616 read
= self
.image
.read(new_size
- 256, 256)
618 self
.image
.rollback_to_snap('snap1')
619 check_stat(self
.image
.stat(), IMG_SIZE
, IMG_ORDER
)
620 assert_raises(InvalidArgument
, self
.image
.read
, new_size
- 256, 256)
621 self
.image
.rollback_to_snap('snap2')
622 check_stat(self
.image
.stat(), new_size
, IMG_ORDER
)
623 read
= self
.image
.read(new_size
- 256, 256)
625 self
.image
.remove_snap('snap1')
626 self
.image
.remove_snap('snap2')
628 def test_set_snap(self
):
629 self
.image
.write(b
'\0' * 256, 0)
630 self
.image
.create_snap('snap1')
631 read
= self
.image
.read(0, 256)
632 eq(read
, b
'\0' * 256)
633 data
= rand_data(256)
634 self
.image
.write(data
, 0)
635 read
= self
.image
.read(0, 256)
637 self
.image
.set_snap('snap1')
638 read
= self
.image
.read(0, 256)
639 eq(read
, b
'\0' * 256)
640 self
.image
.remove_snap('snap1')
642 def test_set_no_snap(self
):
643 self
.image
.write(b
'\0' * 256, 0)
644 self
.image
.create_snap('snap1')
645 read
= self
.image
.read(0, 256)
646 eq(read
, b
'\0' * 256)
647 data
= rand_data(256)
648 self
.image
.write(data
, 0)
649 read
= self
.image
.read(0, 256)
651 self
.image
.set_snap('snap1')
652 read
= self
.image
.read(0, 256)
653 eq(read
, b
'\0' * 256)
654 self
.image
.set_snap(None)
655 read
= self
.image
.read(0, 256)
657 self
.image
.remove_snap('snap1')
659 def test_set_snap_sparse(self
):
660 self
.image
.create_snap('snap1')
661 read
= self
.image
.read(0, 256)
662 eq(read
, b
'\0' * 256)
663 data
= rand_data(256)
664 self
.image
.write(data
, 0)
665 read
= self
.image
.read(0, 256)
667 self
.image
.set_snap('snap1')
668 read
= self
.image
.read(0, 256)
669 eq(read
, b
'\0' * 256)
670 self
.image
.remove_snap('snap1')
672 def test_many_snaps(self
):
674 for i
in range(num_snaps
):
675 self
.image
.create_snap(str(i
))
676 snaps
= sorted(self
.image
.list_snaps(),
677 key
=lambda snap
: int(snap
['name']))
678 eq(len(snaps
), num_snaps
)
679 for i
, snap
in enumerate(snaps
):
680 eq(snap
['size'], IMG_SIZE
)
681 eq(snap
['name'], str(i
))
682 for i
in range(num_snaps
):
683 self
.image
.remove_snap(str(i
))
685 def test_set_snap_deleted(self
):
686 self
.image
.write(b
'\0' * 256, 0)
687 self
.image
.create_snap('snap1')
688 read
= self
.image
.read(0, 256)
689 eq(read
, b
'\0' * 256)
690 data
= rand_data(256)
691 self
.image
.write(data
, 0)
692 read
= self
.image
.read(0, 256)
694 self
.image
.set_snap('snap1')
695 self
.image
.remove_snap('snap1')
696 assert_raises(ImageNotFound
, self
.image
.read
, 0, 256)
697 self
.image
.set_snap(None)
698 read
= self
.image
.read(0, 256)
701 def test_set_snap_recreated(self
):
702 self
.image
.write(b
'\0' * 256, 0)
703 self
.image
.create_snap('snap1')
704 read
= self
.image
.read(0, 256)
705 eq(read
, b
'\0' * 256)
706 data
= rand_data(256)
707 self
.image
.write(data
, 0)
708 read
= self
.image
.read(0, 256)
710 self
.image
.set_snap('snap1')
711 self
.image
.remove_snap('snap1')
712 self
.image
.create_snap('snap1')
713 assert_raises(ImageNotFound
, self
.image
.read
, 0, 256)
714 self
.image
.set_snap(None)
715 read
= self
.image
.read(0, 256)
717 self
.image
.remove_snap('snap1')
719 def test_lock_unlock(self
):
720 assert_raises(ImageNotFound
, self
.image
.unlock
, '')
721 self
.image
.lock_exclusive('')
722 assert_raises(ImageExists
, self
.image
.lock_exclusive
, '')
723 assert_raises(ImageBusy
, self
.image
.lock_exclusive
, 'test')
724 assert_raises(ImageExists
, self
.image
.lock_shared
, '', '')
725 assert_raises(ImageBusy
, self
.image
.lock_shared
, 'foo', '')
726 self
.image
.unlock('')
728 def test_list_lockers(self
):
729 eq([], self
.image
.list_lockers())
730 self
.image
.lock_exclusive('test')
731 lockers
= self
.image
.list_lockers()
732 eq(1, len(lockers
['lockers']))
733 _
, cookie
, _
= lockers
['lockers'][0]
735 eq('', lockers
['tag'])
736 assert lockers
['exclusive']
737 self
.image
.unlock('test')
738 eq([], self
.image
.list_lockers())
741 for i
in range(num_shared
):
742 self
.image
.lock_shared(str(i
), 'tag')
743 lockers
= self
.image
.list_lockers()
744 eq('tag', lockers
['tag'])
745 assert not lockers
['exclusive']
746 eq(num_shared
, len(lockers
['lockers']))
747 cookies
= sorted(map(lambda x
: x
[1], lockers
['lockers']))
748 for i
in range(num_shared
):
749 eq(str(i
), cookies
[i
])
750 self
.image
.unlock(str(i
))
751 eq([], self
.image
.list_lockers())
753 def test_diff_iterate(self
):
754 check_diff(self
.image
, 0, IMG_SIZE
, None, [])
755 self
.image
.write(b
'a' * 256, 0)
756 check_diff(self
.image
, 0, IMG_SIZE
, None, [(0, 256, True)])
757 self
.image
.write(b
'b' * 256, 256)
758 check_diff(self
.image
, 0, IMG_SIZE
, None, [(0, 512, True)])
759 self
.image
.discard(128, 256)
760 check_diff(self
.image
, 0, IMG_SIZE
, None, [(0, 512, True)])
762 self
.image
.create_snap('snap1')
763 self
.image
.discard(0, 1 << IMG_ORDER
)
764 self
.image
.create_snap('snap2')
765 self
.image
.set_snap('snap2')
766 check_diff(self
.image
, 0, IMG_SIZE
, 'snap1', [(0, 512, False)])
767 self
.image
.remove_snap('snap1')
768 self
.image
.remove_snap('snap2')
770 def test_aio_read(self
):
771 # this is a list so that the local cb() can modify it
776 # test1: success case
777 comp
= self
.image
.aio_read(0, 20, cb
)
778 comp
.wait_for_complete_and_cb()
779 eq(retval
[0], b
'\0' * 20)
780 eq(comp
.get_return_value(), 20)
781 eq(sys
.getrefcount(comp
), 2)
785 comp
= self
.image
.aio_read(IMG_SIZE
, 20, cb
)
786 comp
.wait_for_complete_and_cb()
788 assert(comp
.get_return_value() < 0)
789 eq(sys
.getrefcount(comp
), 2)
791 def test_aio_write(self
):
794 retval
[0] = comp
.get_return_value()
796 data
= rand_data(256)
797 comp
= self
.image
.aio_write(data
, 256, cb
)
798 comp
.wait_for_complete_and_cb()
800 eq(comp
.get_return_value(), 0)
801 eq(sys
.getrefcount(comp
), 2)
802 eq(self
.image
.read(256, 256), data
)
804 def test_aio_discard(self
):
807 retval
[0] = comp
.get_return_value()
809 data
= rand_data(256)
810 self
.image
.write(data
, 0)
811 comp
= self
.image
.aio_discard(0, 256, cb
)
812 comp
.wait_for_complete_and_cb()
814 eq(comp
.get_return_value(), 0)
815 eq(sys
.getrefcount(comp
), 2)
816 eq(self
.image
.read(256, 256), b
'\0' * 256)
818 def test_aio_flush(self
):
821 retval
[0] = comp
.get_return_value()
823 comp
= self
.image
.aio_flush(cb
)
824 comp
.wait_for_complete_and_cb()
826 eq(sys
.getrefcount(comp
), 2)
828 def test_metadata(self
):
829 metadata
= list(self
.image
.metadata_list())
831 self
.image
.metadata_set("key1", "value1")
832 self
.image
.metadata_set("key2", "value2")
833 value
= self
.image
.metadata_get("key1")
835 value
= self
.image
.metadata_get("key2")
837 metadata
= list(self
.image
.metadata_list())
839 self
.image
.metadata_remove("key1")
840 metadata
= list(self
.image
.metadata_list())
842 eq(metadata
[0], ("key2", "value2"))
843 self
.image
.metadata_remove("key2")
844 metadata
= list(self
.image
.metadata_list())
849 self
.image
.metadata_set("key" + str(i
), "X" * 1025)
850 metadata
= list(self
.image
.metadata_list())
853 self
.image
.metadata_remove("key" + str(i
))
854 metadata
= list(self
.image
.metadata_list())
855 eq(len(metadata
), N
- i
- 1)
857 def check_diff(image
, offset
, length
, from_snapshot
, expected
):
859 def cb(offset
, length
, exists
):
860 extents
.append((offset
, length
, exists
))
861 image
.diff_iterate(0, IMG_SIZE
, None, cb
)
862 eq(extents
, expected
)
864 class TestClone(object):
866 @require_features([RBD_FEATURE_LAYERING
])
872 self
.image
= Image(ioctx
, image_name
)
873 data
= rand_data(256)
874 self
.image
.write(data
, IMG_SIZE
// 2)
875 self
.image
.create_snap('snap1')
877 self
.image
.protect_snap('snap1')
878 self
.clone_name
= get_temp_image_name()
879 self
.rbd
.clone(ioctx
, image_name
, 'snap1', ioctx
, self
.clone_name
,
881 self
.clone
= Image(ioctx
, self
.clone_name
)
886 self
.rbd
.remove(ioctx
, self
.clone_name
)
887 self
.image
.unprotect_snap('snap1')
888 self
.image
.remove_snap('snap1')
892 def _test_with_params(self
, features
=None, order
=None, stripe_unit
=None,
894 self
.image
.create_snap('snap2')
895 self
.image
.protect_snap('snap2')
896 clone_name2
= get_temp_image_name()
898 self
.rbd
.clone(ioctx
, image_name
, 'snap2', ioctx
, clone_name2
)
900 self
.rbd
.clone(ioctx
, image_name
, 'snap2', ioctx
, clone_name2
,
902 elif stripe_unit
is None:
903 self
.rbd
.clone(ioctx
, image_name
, 'snap2', ioctx
, clone_name2
,
905 elif stripe_count
is None:
906 self
.rbd
.clone(ioctx
, image_name
, 'snap2', ioctx
, clone_name2
,
907 features
, order
, stripe_unit
)
909 self
.rbd
.clone(ioctx
, image_name
, 'snap2', ioctx
, clone_name2
,
910 features
, order
, stripe_unit
, stripe_count
)
911 self
.rbd
.remove(ioctx
, clone_name2
)
912 self
.image
.unprotect_snap('snap2')
913 self
.image
.remove_snap('snap2')
915 def test_with_params(self
):
916 self
._test
_with
_params
()
918 def test_with_params2(self
):
920 self
._test
_with
_params
(features
, self
.image
.stat()['order'])
922 @require_features([RBD_FEATURE_STRIPINGV2
])
923 def test_with_params3(self
):
925 self
._test
_with
_params
(features
, self
.image
.stat()['order'],
926 self
.image
.stripe_unit(),
927 self
.image
.stripe_count())
929 def test_unprotected(self
):
930 self
.image
.create_snap('snap2')
932 clone_name2
= get_temp_image_name()
933 assert_raises(InvalidArgument
, self
.rbd
.clone
, ioctx
, image_name
,
934 'snap2', ioctx
, clone_name2
, features
)
935 self
.image
.remove_snap('snap2')
937 def test_unprotect_with_children(self
):
939 # can't remove a snapshot that has dependent clones
940 assert_raises(ImageBusy
, self
.image
.remove_snap
, 'snap1')
942 # validate parent info of clone created by TestClone.setUp
943 (pool
, image
, snap
) = self
.clone
.parent_info()
945 eq(image
, image_name
)
947 eq(self
.image
.id(), self
.clone
.parent_id())
949 # create a new pool...
950 pool_name2
= get_temp_pool_name()
951 rados
.create_pool(pool_name2
)
952 other_ioctx
= rados
.open_ioctx(pool_name2
)
953 other_ioctx
.application_enable('rbd')
955 # ...with a clone of the same parent
956 other_clone_name
= get_temp_image_name()
957 self
.rbd
.clone(ioctx
, image_name
, 'snap1', other_ioctx
,
958 other_clone_name
, features
)
959 self
.other_clone
= Image(other_ioctx
, other_clone_name
)
960 # validate its parent info
961 (pool
, image
, snap
) = self
.other_clone
.parent_info()
963 eq(image
, image_name
)
965 eq(self
.image
.id(), self
.other_clone
.parent_id())
967 # can't unprotect snap with children
968 assert_raises(ImageBusy
, self
.image
.unprotect_snap
, 'snap1')
970 # 2 children, check that cannot remove the parent snap
971 assert_raises(ImageBusy
, self
.image
.remove_snap
, 'snap1')
973 # close and remove other pool's clone
974 self
.other_clone
.close()
975 self
.rbd
.remove(other_ioctx
, other_clone_name
)
977 # check that we cannot yet remove the parent snap
978 assert_raises(ImageBusy
, self
.image
.remove_snap
, 'snap1')
981 rados
.delete_pool(pool_name2
)
983 # unprotect, remove parent snap happen in cleanup, and should succeed
986 image_info
= self
.image
.stat()
987 clone_info
= self
.clone
.stat()
988 eq(clone_info
['size'], image_info
['size'])
989 eq(clone_info
['size'], self
.clone
.overlap())
991 def test_resize_stat(self
):
992 self
.clone
.resize(IMG_SIZE
// 2)
993 image_info
= self
.image
.stat()
994 clone_info
= self
.clone
.stat()
995 eq(clone_info
['size'], IMG_SIZE
// 2)
996 eq(image_info
['size'], IMG_SIZE
)
997 eq(self
.clone
.overlap(), IMG_SIZE
// 2)
999 self
.clone
.resize(IMG_SIZE
* 2)
1000 image_info
= self
.image
.stat()
1001 clone_info
= self
.clone
.stat()
1002 eq(clone_info
['size'], IMG_SIZE
* 2)
1003 eq(image_info
['size'], IMG_SIZE
)
1004 eq(self
.clone
.overlap(), IMG_SIZE
// 2)
1006 def test_resize_io(self
):
1007 parent_data
= self
.image
.read(IMG_SIZE
// 2, 256)
1008 self
.image
.resize(0)
1009 self
.clone
.resize(IMG_SIZE
// 2 + 128)
1010 child_data
= self
.clone
.read(IMG_SIZE
// 2, 128)
1011 eq(child_data
, parent_data
[:128])
1012 self
.clone
.resize(IMG_SIZE
)
1013 child_data
= self
.clone
.read(IMG_SIZE
// 2, 256)
1014 eq(child_data
, parent_data
[:128] + (b
'\0' * 128))
1015 self
.clone
.resize(IMG_SIZE
// 2 + 1)
1016 child_data
= self
.clone
.read(IMG_SIZE
// 2, 1)
1017 eq(child_data
, parent_data
[0:1])
1018 self
.clone
.resize(0)
1019 self
.clone
.resize(IMG_SIZE
)
1020 child_data
= self
.clone
.read(IMG_SIZE
// 2, 256)
1021 eq(child_data
, b
'\0' * 256)
1023 def test_read(self
):
1024 parent_data
= self
.image
.read(IMG_SIZE
// 2, 256)
1025 child_data
= self
.clone
.read(IMG_SIZE
// 2, 256)
1026 eq(child_data
, parent_data
)
1028 def test_write(self
):
1029 parent_data
= self
.image
.read(IMG_SIZE
// 2, 256)
1030 new_data
= rand_data(256)
1031 self
.clone
.write(new_data
, IMG_SIZE
// 2 + 256)
1032 child_data
= self
.clone
.read(IMG_SIZE
// 2 + 256, 256)
1033 eq(child_data
, new_data
)
1034 child_data
= self
.clone
.read(IMG_SIZE
// 2, 256)
1035 eq(child_data
, parent_data
)
1036 parent_data
= self
.image
.read(IMG_SIZE
// 2 + 256, 256)
1037 eq(parent_data
, b
'\0' * 256)
1039 def check_children(self
, expected
):
1040 actual
= self
.image
.list_children()
1041 # dedup for cache pools until
1042 # http://tracker.ceph.com/issues/8187 is fixed
1043 deduped
= set([(pool_name
, image
[1]) for image
in actual
])
1044 eq(deduped
, set(expected
))
1046 def test_list_children(self
):
1049 self
.image
.set_snap('snap1')
1050 self
.check_children([(pool_name
, self
.clone_name
)])
1052 self
.rbd
.remove(ioctx
, self
.clone_name
)
1053 eq(self
.image
.list_children(), [])
1055 clone_name
= get_temp_image_name() + '_'
1056 expected_children
= []
1058 self
.rbd
.clone(ioctx
, image_name
, 'snap1', ioctx
,
1059 clone_name
+ str(i
), features
)
1060 expected_children
.append((pool_name
, clone_name
+ str(i
)))
1061 self
.check_children(expected_children
)
1064 self
.rbd
.remove(ioctx
, clone_name
+ str(i
))
1065 expected_children
.pop(0)
1066 self
.check_children(expected_children
)
1068 eq(self
.image
.list_children(), [])
1069 self
.rbd
.clone(ioctx
, image_name
, 'snap1', ioctx
, self
.clone_name
,
1071 self
.check_children([(pool_name
, self
.clone_name
)])
1072 self
.clone
= Image(ioctx
, self
.clone_name
)
1074 def test_flatten_errors(self
):
1075 # test that we can't flatten a non-clone
1076 assert_raises(InvalidArgument
, self
.image
.flatten
)
1078 # test that we can't flatten a snapshot
1079 self
.clone
.create_snap('snap2')
1080 self
.clone
.set_snap('snap2')
1081 assert_raises(ReadOnlyImage
, self
.clone
.flatten
)
1082 self
.clone
.remove_snap('snap2')
1084 def check_flatten_with_order(self
, new_order
):
1087 clone_name2
= get_temp_image_name()
1088 self
.rbd
.clone(ioctx
, image_name
, 'snap1', ioctx
, clone_name2
,
1089 features
, new_order
)
1090 #with Image(ioctx, 'clone2') as clone:
1091 clone2
= Image(ioctx
, clone_name2
)
1093 eq(clone2
.overlap(), 0)
1095 self
.rbd
.remove(ioctx
, clone_name2
)
1097 # flatten after resizing to non-block size
1098 self
.rbd
.clone(ioctx
, image_name
, 'snap1', ioctx
, clone_name2
,
1099 features
, new_order
)
1100 with
Image(ioctx
, clone_name2
) as clone
:
1101 clone
.resize(IMG_SIZE
// 2 - 1)
1103 eq(0, clone
.overlap())
1104 self
.rbd
.remove(ioctx
, clone_name2
)
1106 # flatten after resizing to non-block size
1107 self
.rbd
.clone(ioctx
, image_name
, 'snap1', ioctx
, clone_name2
,
1108 features
, new_order
)
1109 with
Image(ioctx
, clone_name2
) as clone
:
1110 clone
.resize(IMG_SIZE
// 2 + 1)
1112 eq(clone
.overlap(), 0)
1113 self
.rbd
.remove(ioctx
, clone_name2
)
1115 def test_flatten_basic(self
):
1116 self
.check_flatten_with_order(IMG_ORDER
)
1118 def test_flatten_smaller_order(self
):
1119 self
.check_flatten_with_order(IMG_ORDER
- 2)
1121 def test_flatten_larger_order(self
):
1122 self
.check_flatten_with_order(IMG_ORDER
+ 2)
1124 def test_flatten_drops_cache(self
):
1127 clone_name2
= get_temp_image_name()
1128 self
.rbd
.clone(ioctx
, image_name
, 'snap1', ioctx
, clone_name2
,
1129 features
, IMG_ORDER
)
1130 with
Image(ioctx
, clone_name2
) as clone
:
1131 with
Image(ioctx
, clone_name2
) as clone2
:
1132 # cache object non-existence
1133 data
= clone
.read(IMG_SIZE
// 2, 256)
1134 clone2_data
= clone2
.read(IMG_SIZE
// 2, 256)
1135 eq(data
, clone2_data
)
1137 assert_raises(ImageNotFound
, clone
.parent_info
)
1138 assert_raises(ImageNotFound
, clone2
.parent_info
)
1139 assert_raises(ImageNotFound
, clone
.parent_id
)
1140 assert_raises(ImageNotFound
, clone2
.parent_id
)
1141 after_flatten
= clone
.read(IMG_SIZE
// 2, 256)
1142 eq(data
, after_flatten
)
1143 after_flatten
= clone2
.read(IMG_SIZE
// 2, 256)
1144 eq(data
, after_flatten
)
1145 self
.rbd
.remove(ioctx
, clone_name2
)
1147 def test_flatten_multi_level(self
):
1148 self
.clone
.create_snap('snap2')
1149 self
.clone
.protect_snap('snap2')
1150 clone_name3
= get_temp_image_name()
1151 self
.rbd
.clone(ioctx
, self
.clone_name
, 'snap2', ioctx
, clone_name3
,
1153 self
.clone
.flatten()
1154 with
Image(ioctx
, clone_name3
) as clone3
:
1156 self
.clone
.unprotect_snap('snap2')
1157 self
.clone
.remove_snap('snap2')
1158 self
.rbd
.remove(ioctx
, clone_name3
)
1160 def test_resize_flatten_multi_level(self
):
1161 self
.clone
.create_snap('snap2')
1162 self
.clone
.protect_snap('snap2')
1163 clone_name3
= get_temp_image_name()
1164 self
.rbd
.clone(ioctx
, self
.clone_name
, 'snap2', ioctx
, clone_name3
,
1166 self
.clone
.resize(1)
1167 orig_data
= self
.image
.read(0, 256)
1168 with
Image(ioctx
, clone_name3
) as clone3
:
1169 clone3_data
= clone3
.read(0, 256)
1170 eq(orig_data
, clone3_data
)
1171 self
.clone
.flatten()
1172 with
Image(ioctx
, clone_name3
) as clone3
:
1173 clone3_data
= clone3
.read(0, 256)
1174 eq(orig_data
, clone3_data
)
1175 self
.rbd
.remove(ioctx
, clone_name3
)
1176 self
.clone
.unprotect_snap('snap2')
1177 self
.clone
.remove_snap('snap2')
1179 class TestExclusiveLock(object):
1181 @require_features([RBD_FEATURE_EXCLUSIVE_LOCK
])
1184 rados2
= Rados(conffile
='')
1187 ioctx2
= rados2
.open_ioctx(pool_name
)
1197 def test_ownership(self
):
1198 with
Image(ioctx
, image_name
) as image1
, Image(ioctx2
, image_name
) as image2
:
1199 image1
.write(b
'0'*256, 0)
1200 eq(image1
.is_exclusive_lock_owner(), True)
1201 eq(image2
.is_exclusive_lock_owner(), False)
1203 def test_snapshot_leadership(self
):
1204 with
Image(ioctx
, image_name
) as image
:
1205 image
.create_snap('snap')
1206 eq(image
.is_exclusive_lock_owner(), True)
1208 with
Image(ioctx
, image_name
) as image
:
1209 image
.write(b
'0'*256, 0)
1210 eq(image
.is_exclusive_lock_owner(), True)
1211 image
.set_snap('snap')
1212 eq(image
.is_exclusive_lock_owner(), False)
1213 with
Image(ioctx
, image_name
, snapshot
='snap') as image
:
1214 eq(image
.is_exclusive_lock_owner(), False)
1216 with
Image(ioctx
, image_name
) as image
:
1217 image
.remove_snap('snap')
1219 def test_read_only_leadership(self
):
1220 with
Image(ioctx
, image_name
, read_only
=True) as image
:
1221 eq(image
.is_exclusive_lock_owner(), False)
1223 def test_follower_flatten(self
):
1224 with
Image(ioctx
, image_name
) as image
:
1225 image
.create_snap('snap')
1226 image
.protect_snap('snap')
1228 RBD().clone(ioctx
, image_name
, 'snap', ioctx
, 'clone', features
)
1229 with
Image(ioctx
, 'clone') as image1
, Image(ioctx2
, 'clone') as image2
:
1230 data
= rand_data(256)
1231 image1
.write(data
, 0)
1233 assert_raises(ImageNotFound
, image1
.parent_info
)
1234 assert_raises(ImageNotFound
, image1
.parent_id
)
1238 image2
.parent_info()
1239 except ImageNotFound
:
1244 RBD().remove(ioctx
, 'clone')
1245 with
Image(ioctx
, image_name
) as image
:
1246 image
.unprotect_snap('snap')
1247 image
.remove_snap('snap')
1249 def test_follower_resize(self
):
1250 with
Image(ioctx
, image_name
) as image1
, Image(ioctx2
, image_name
) as image2
:
1251 image1
.write(b
'0'*256, 0)
1252 for new_size
in [IMG_SIZE
* 2, IMG_SIZE
// 2]:
1253 image2
.resize(new_size
);
1254 eq(new_size
, image1
.size())
1256 if new_size
== image2
.size():
1259 eq(new_size
, image2
.size())
1261 def test_follower_snap_create(self
):
1262 with
Image(ioctx
, image_name
) as image1
, Image(ioctx2
, image_name
) as image2
:
1263 image2
.create_snap('snap1')
1264 image1
.remove_snap('snap1')
1266 def test_follower_snap_rollback(self
):
1267 with
Image(ioctx
, image_name
) as image1
, Image(ioctx2
, image_name
) as image2
:
1268 image1
.create_snap('snap')
1270 assert_raises(ReadOnlyImage
, image2
.rollback_to_snap
, 'snap')
1271 image1
.rollback_to_snap('snap')
1273 image1
.remove_snap('snap')
1275 def test_follower_discard(self
):
1277 with
Image(ioctx
, image_name
) as image1
, Image(ioctx2
, image_name
) as image2
:
1278 data
= rand_data(256)
1279 image1
.write(data
, 0)
1280 image2
.discard(0, 256)
1281 eq(image1
.is_exclusive_lock_owner(), False)
1282 eq(image2
.is_exclusive_lock_owner(), True)
1283 read
= image2
.read(0, 256)
1284 if rados
.conf_get('rbd_skip_partial_discard') == 'false':
1285 eq(256 * b
'\0', read
)
1289 def test_follower_write(self
):
1290 with
Image(ioctx
, image_name
) as image1
, Image(ioctx2
, image_name
) as image2
:
1291 data
= rand_data(256)
1292 image1
.write(data
, 0)
1293 image2
.write(data
, IMG_SIZE
// 2)
1294 eq(image1
.is_exclusive_lock_owner(), False)
1295 eq(image2
.is_exclusive_lock_owner(), True)
1296 for offset
in [0, IMG_SIZE
// 2]:
1297 read
= image2
.read(offset
, 256)
1299 def test_acquire_release_lock(self
):
1300 with
Image(ioctx
, image_name
) as image
:
1301 image
.lock_acquire(RBD_LOCK_MODE_EXCLUSIVE
)
1302 image
.lock_release()
1304 def test_break_lock(self
):
1305 blacklist_rados
= Rados(conffile
='')
1306 blacklist_rados
.connect()
1308 blacklist_ioctx
= blacklist_rados
.open_ioctx(pool_name
)
1310 rados2
.conf_set('rbd_blacklist_on_break_lock', 'true')
1311 with
Image(ioctx2
, image_name
) as image
, \
1312 Image(blacklist_ioctx
, image_name
) as blacklist_image
:
1313 blacklist_image
.lock_acquire(RBD_LOCK_MODE_EXCLUSIVE
)
1314 assert_raises(ReadOnlyImage
, image
.lock_acquire
,
1315 RBD_LOCK_MODE_EXCLUSIVE
)
1317 lock_owners
= list(image
.lock_get_owners())
1318 eq(1, len(lock_owners
))
1319 eq(RBD_LOCK_MODE_EXCLUSIVE
, lock_owners
[0]['mode'])
1320 image
.lock_break(RBD_LOCK_MODE_EXCLUSIVE
,
1321 lock_owners
[0]['owner'])
1323 assert_raises(ConnectionShutdown
,
1324 blacklist_image
.is_exclusive_lock_owner
)
1326 blacklist_rados
.wait_for_latest_osdmap()
1327 data
= rand_data(256)
1328 assert_raises(ConnectionShutdown
,
1329 blacklist_image
.write
, data
, 0)
1331 image
.lock_acquire(RBD_LOCK_MODE_EXCLUSIVE
)
1334 blacklist_image
.close()
1335 except ConnectionShutdown
:
1338 blacklist_ioctx
.close()
1340 blacklist_rados
.shutdown()
1342 class TestMirroring(object):
1345 def check_info(info
, global_id
, state
, primary
=None):
1346 eq(global_id
, info
['global_id'])
1347 eq(state
, info
['state'])
1348 if primary
is not None:
1349 eq(primary
, info
['primary'])
1353 self
.initial_mirror_mode
= self
.rbd
.mirror_mode_get(ioctx
)
1354 self
.rbd
.mirror_mode_set(ioctx
, RBD_MIRROR_MODE_POOL
)
1356 self
.image
= Image(ioctx
, image_name
)
1361 self
.rbd
.mirror_mode_set(ioctx
, self
.initial_mirror_mode
)
1364 def test_mirror_peer(self
):
1365 eq([], list(self
.rbd
.mirror_peer_list(ioctx
)))
1366 cluster_name
= "test_cluster"
1367 client_name
= "test_client"
1368 uuid
= self
.rbd
.mirror_peer_add(ioctx
, cluster_name
, client_name
)
1372 'cluster_name' : cluster_name
,
1373 'client_name' : client_name
,
1375 eq([peer
], list(self
.rbd
.mirror_peer_list(ioctx
)))
1376 cluster_name
= "test_cluster1"
1377 self
.rbd
.mirror_peer_set_cluster(ioctx
, uuid
, cluster_name
)
1378 client_name
= "test_client1"
1379 self
.rbd
.mirror_peer_set_client(ioctx
, uuid
, client_name
)
1382 'cluster_name' : cluster_name
,
1383 'client_name' : client_name
,
1385 eq([peer
], list(self
.rbd
.mirror_peer_list(ioctx
)))
1386 self
.rbd
.mirror_peer_remove(ioctx
, uuid
)
1387 eq([], list(self
.rbd
.mirror_peer_list(ioctx
)))
1389 @require_features([RBD_FEATURE_EXCLUSIVE_LOCK
,
1390 RBD_FEATURE_JOURNALING
])
1391 def test_mirror_image(self
):
1393 self
.rbd
.mirror_mode_set(ioctx
, RBD_MIRROR_MODE_IMAGE
)
1394 self
.image
.mirror_image_disable(True)
1395 info
= self
.image
.mirror_image_get_info()
1396 self
.check_info(info
, '', RBD_MIRROR_IMAGE_DISABLED
, False)
1398 self
.image
.mirror_image_enable()
1399 info
= self
.image
.mirror_image_get_info()
1400 global_id
= info
['global_id']
1401 self
.check_info(info
, global_id
, RBD_MIRROR_IMAGE_ENABLED
, True)
1403 self
.rbd
.mirror_mode_set(ioctx
, RBD_MIRROR_MODE_POOL
)
1406 self
.image
.mirror_image_disable(True)
1407 except InvalidArgument
:
1409 eq(True, fail
) # Fails because of mirror mode pool
1411 self
.image
.mirror_image_demote()
1412 info
= self
.image
.mirror_image_get_info()
1413 self
.check_info(info
, global_id
, RBD_MIRROR_IMAGE_ENABLED
, False)
1415 self
.image
.mirror_image_resync()
1417 self
.image
.mirror_image_promote(True)
1418 info
= self
.image
.mirror_image_get_info()
1419 self
.check_info(info
, global_id
, RBD_MIRROR_IMAGE_ENABLED
, True)
1423 self
.image
.mirror_image_resync()
1424 except InvalidArgument
:
1426 eq(True, fail
) # Fails because it is primary
1428 status
= self
.image
.mirror_image_get_status()
1429 eq(image_name
, status
['name'])
1430 eq(False, status
['up'])
1431 eq(MIRROR_IMAGE_STATUS_STATE_UNKNOWN
, status
['state'])
1432 info
= status
['info']
1433 self
.check_info(info
, global_id
, RBD_MIRROR_IMAGE_ENABLED
, True)
1435 @require_features([RBD_FEATURE_EXCLUSIVE_LOCK
,
1436 RBD_FEATURE_JOURNALING
])
1437 def test_mirror_image_status(self
):
1438 info
= self
.image
.mirror_image_get_info()
1439 global_id
= info
['global_id']
1440 state
= info
['state']
1441 primary
= info
['primary']
1443 status
= self
.image
.mirror_image_get_status()
1444 eq(image_name
, status
['name'])
1445 eq(False, status
['up'])
1446 eq(MIRROR_IMAGE_STATUS_STATE_UNKNOWN
, status
['state'])
1447 info
= status
['info']
1448 self
.check_info(info
, global_id
, state
, primary
)
1450 images
= list(self
.rbd
.mirror_image_status_list(ioctx
))
1453 eq(image_name
, status
['name'])
1454 eq(False, status
['up'])
1455 eq(MIRROR_IMAGE_STATUS_STATE_UNKNOWN
, status
['state'])
1456 info
= status
['info']
1457 self
.check_info(info
, global_id
, state
)
1459 states
= self
.rbd
.mirror_image_status_summary(ioctx
)
1460 eq([(MIRROR_IMAGE_STATUS_STATE_UNKNOWN
, 1)], states
)
1464 self
.rbd
.create(ioctx
, image_name
+ str(i
), IMG_SIZE
, IMG_ORDER
,
1465 old_format
=False, features
=int(features
))
1466 images
= list(self
.rbd
.mirror_image_status_list(ioctx
))
1467 eq(N
+ 1, len(images
))
1469 self
.rbd
.remove(ioctx
, image_name
+ str(i
))
1472 class TestTrash(object):
1476 rados2
= Rados(conffile
='')
1479 ioctx2
= rados2
.open_ioctx(pool_name
)
1487 def test_move(self
):
1489 with
Image(ioctx
, image_name
) as image
:
1490 image_id
= image
.id()
1492 RBD().trash_move(ioctx
, image_name
, 1000)
1493 RBD().trash_remove(ioctx
, image_id
, True)
1495 def test_remove_denied(self
):
1497 with
Image(ioctx
, image_name
) as image
:
1498 image_id
= image
.id()
1500 RBD().trash_move(ioctx
, image_name
, 1000)
1501 assert_raises(PermissionError
, RBD().trash_remove
, ioctx
, image_id
)
1503 def test_remove(self
):
1505 with
Image(ioctx
, image_name
) as image
:
1506 image_id
= image
.id()
1508 RBD().trash_move(ioctx
, image_name
, 0)
1509 RBD().trash_remove(ioctx
, image_id
)
1513 with
Image(ioctx
, image_name
) as image
:
1514 image_id
= image
.id()
1516 RBD().trash_move(ioctx
, image_name
, 1000)
1518 info
= RBD().trash_get(ioctx
, image_id
)
1519 eq(image_id
, info
['id'])
1520 eq(image_name
, info
['name'])
1521 eq('USER', info
['source'])
1522 assert(info
['deferment_end_time'] > info
['deletion_time'])
1524 RBD().trash_remove(ioctx
, image_id
, True)
1526 def test_list(self
):
1528 with
Image(ioctx
, image_name
) as image
:
1529 image_id1
= image
.id()
1530 image_name1
= image_name
1531 RBD().trash_move(ioctx
, image_name
, 1000)
1534 with
Image(ioctx
, image_name
) as image
:
1535 image_id2
= image
.id()
1536 image_name2
= image_name
1537 RBD().trash_move(ioctx
, image_name
, 1000)
1539 entries
= list(RBD().trash_list(ioctx
))
1541 if e
['id'] == image_id1
:
1542 eq(e
['name'], image_name1
)
1543 elif e
['id'] == image_id2
:
1544 eq(e
['name'], image_name2
)
1547 eq(e
['source'], 'USER')
1548 assert e
['deferment_end_time'] > e
['deletion_time']
1550 RBD().trash_remove(ioctx
, image_id1
, True)
1551 RBD().trash_remove(ioctx
, image_id2
, True)
1553 def test_restore(self
):
1555 with
Image(ioctx
, image_name
) as image
:
1556 image_id
= image
.id()
1557 RBD().trash_move(ioctx
, image_name
, 1000)
1558 RBD().trash_restore(ioctx
, image_id
, image_name
)