-#!/usr/bin/env python
+#!/usr/bin/env python3
#
# Tests for incremental drive-backup
#
def transaction_action(action, **kwargs):
return {
'type': action,
- 'data': dict((k.replace('_', '-'), v) for k, v in kwargs.iteritems())
+ 'data': dict((k.replace('_', '-'), v) for k, v in kwargs.items())
}
def transaction_drive_backup(device, target, **kwargs):
- return transaction_action('drive-backup', device=device, target=target,
- **kwargs)
+ return transaction_action('drive-backup', job_id=device, device=device,
+ target=target, **kwargs)
class Bitmap:
# Create a base image with a distinctive patterning
drive0 = self.add_node('drive0')
self.img_create(drive0['file'], drive0['fmt'])
- self.vm.add_drive(drive0['file'])
+ self.vm.add_drive(drive0['file'], opts='node-name=node0')
self.write_default_pattern(drive0['file'])
self.vm.launch()
def img_create(self, img, fmt=iotests.imgfmt, size='64M',
- parent=None, parentFormat=None):
+ parent=None, parentFormat=None, **kwargs):
+ optargs = []
+ for k,v in kwargs.items():
+ optargs = optargs + ['-o', '%s=%s' % (k,v)]
+ args = ['create', '-f', fmt] + optargs + [img, size]
if parent:
if parentFormat is None:
parentFormat = fmt
- iotests.qemu_img('create', '-f', fmt, img, size,
- '-b', parent, '-F', parentFormat)
- else:
- iotests.qemu_img('create', '-f', fmt, img, size)
+ args = args + ['-b', parent, '-F', parentFormat]
+ iotests.qemu_img(*args)
self.files.append(img)
return self.wait_qmp_backup(kwargs['device'], error)
+ def ignore_job_status_change_events(self):
+ while True:
+ e = self.vm.event_wait(name="JOB_STATUS_CHANGE")
+ if e['data']['status'] == 'null':
+ break
+
def wait_qmp_backup(self, device, error='Input/output error'):
event = self.vm.event_wait(name="BLOCK_JOB_COMPLETED",
match={'data': {'device': device}})
self.assertNotEqual(event, None)
+ self.ignore_job_status_change_events()
try:
failure = self.dictpath(event, 'data/error')
event = self.vm.event_wait(name='BLOCK_JOB_CANCELLED',
match={'data': {'device': device}})
self.assertNotEqual(event, None)
+ self.ignore_job_status_change_events()
def create_anchor_backup(self, drive=None):
if drive is None:
drive = self.drives[-1]
- res = self.do_qmp_backup(device=drive['id'], sync='full',
+ res = self.do_qmp_backup(job_id=drive['id'],
+ device=drive['id'], sync='full',
format=drive['fmt'], target=drive['backup'])
self.assertTrue(res)
self.files.append(drive['backup'])
if bitmap is None:
bitmap = self.bitmaps[-1]
_, reference = bitmap.last_target()
- res = self.do_qmp_backup(device=bitmap.drive['id'], sync='full',
+ res = self.do_qmp_backup(job_id=bitmap.drive['id'],
+ device=bitmap.drive['id'], sync='full',
format=bitmap.drive['fmt'], target=reference)
self.assertTrue(res)
return bitmap
- def prepare_backup(self, bitmap=None, parent=None):
+ def prepare_backup(self, bitmap=None, parent=None, **kwargs):
if bitmap is None:
bitmap = self.bitmaps[-1]
if parent is None:
parent, _ = bitmap.last_target()
target, _ = bitmap.new_target()
- self.img_create(target, bitmap.drive['fmt'], parent=parent)
+ self.img_create(target, bitmap.drive['fmt'], parent=parent,
+ **kwargs)
return target
def create_incremental(self, bitmap=None, parent=None,
- parentFormat=None, validate=True):
+ parentFormat=None, validate=True,
+ target=None):
if bitmap is None:
bitmap = self.bitmaps[-1]
if parent is None:
parent, _ = bitmap.last_target()
- target = self.prepare_backup(bitmap, parent)
- res = self.do_qmp_backup(device=bitmap.drive['id'],
+ if target is None:
+ target = self.prepare_backup(bitmap, parent)
+ res = self.do_qmp_backup(job_id=bitmap.drive['id'],
+ device=bitmap.drive['id'],
sync='incremental', bitmap=bitmap.name,
format=bitmap.drive['fmt'], target=target,
mode='existing')
return self.do_incremental_simple(granularity=131072)
+ def test_larger_cluster_target(self):
+ '''
+ Test: Create and verify backups made to a larger cluster size target.
+
+ With a default granularity of 64KiB, verify that backups made to a
+ larger cluster size target of 128KiB without a backing file works.
+ '''
+ drive0 = self.drives[0]
+
+ # Create a cluster_size=128k full backup / "anchor" backup
+ self.img_create(drive0['backup'], cluster_size='128k')
+ self.assertTrue(self.do_qmp_backup(device=drive0['id'], sync='full',
+ format=drive0['fmt'],
+ target=drive0['backup'],
+ mode='existing'))
+
+ # Create bitmap and dirty it with some new writes.
+ # overwrite [32736, 32799] which will dirty bitmap clusters at
+ # 32M-64K and 32M. 32M+64K will be left undirtied.
+ bitmap0 = self.add_bitmap('bitmap0', drive0)
+ self.hmp_io_writes(drive0['id'],
+ (('0xab', 0, 512),
+ ('0xfe', '16M', '256k'),
+ ('0x64', '32736k', '64k')))
+ # Check the dirty bitmap stats
+ self.assertTrue(self.vm.check_bitmap_status(
+ 'node0', bitmap0.name, {
+ 'name': 'bitmap0',
+ 'count': 458752,
+ 'granularity': 65536,
+ 'status': 'active',
+ 'persistent': False
+ }))
+
+ # Prepare a cluster_size=128k backup target without a backing file.
+ (target, _) = bitmap0.new_target()
+ self.img_create(target, bitmap0.drive['fmt'], cluster_size='128k')
+
+ # Perform Incremental Backup
+ self.assertTrue(self.do_qmp_backup(device=bitmap0.drive['id'],
+ sync='incremental',
+ bitmap=bitmap0.name,
+ format=bitmap0.drive['fmt'],
+ target=target,
+ mode='existing'))
+ self.make_reference_backup(bitmap0)
+
+ # Add the backing file, then compare and exit.
+ iotests.qemu_img('rebase', '-f', drive0['fmt'], '-u', '-b',
+ drive0['backup'], '-F', drive0['fmt'], target)
+ self.vm.shutdown()
+ self.check_backups()
+
+
def test_incremental_transaction(self):
'''Test: Verify backups made from transactionally created bitmaps.
self.check_backups()
- def test_transaction_failure(self):
- '''Test: Verify backups made from a transaction that partially fails.
-
- Add a second drive with its own unique pattern, and add a bitmap to each
- drive. Use blkdebug to interfere with the backup on just one drive and
- attempt to create a coherent incremental backup across both drives.
-
- verify a failure in one but not both, then delete the failed stubs and
- re-run the same transaction.
-
- verify that both incrementals are created successfully.
- '''
-
+ def do_transaction_failure_test(self, race=False):
# Create a second drive, with pattern:
drive1 = self.add_node('drive1')
self.img_create(drive1['file'], drive1['fmt'])
('0xcd', '32M', '124k')))
# Create a blkdebug interface to this img as 'drive1'
- result = self.vm.qmp('blockdev-add', options={
- 'id': drive1['id'],
- 'driver': drive1['fmt'],
- 'file': {
+ result = self.vm.qmp('blockdev-add',
+ node_name=drive1['id'],
+ driver=drive1['fmt'],
+ file={
'driver': 'blkdebug',
'image': {
'driver': 'file',
'once': True
}],
}
- })
+ )
self.assert_qmp(result, 'return', {})
# Create bitmaps and full backups for both drives
self.assertFalse(self.vm.get_qmp_events(wait=False))
# Emulate some writes
- self.hmp_io_writes(drive0['id'], (('0xab', 0, 512),
- ('0xfe', '16M', '256k'),
- ('0x64', '32736k', '64k')))
+ if not race:
+ self.hmp_io_writes(drive0['id'], (('0xab', 0, 512),
+ ('0xfe', '16M', '256k'),
+ ('0x64', '32736k', '64k')))
self.hmp_io_writes(drive1['id'], (('0xba', 0, 512),
('0xef', '16M', '256k'),
('0x46', '32736k', '64k')))
target1 = self.prepare_backup(dr1bm0)
# Ask for a new incremental backup per-each drive,
- # expecting drive1's backup to fail:
+ # expecting drive1's backup to fail. In the 'race' test,
+ # we expect drive1 to attempt to cancel the empty drive0 job.
transaction = [
transaction_drive_backup(drive0['id'], target0, sync='incremental',
format=drive0['fmt'], mode='existing',
self.assert_no_active_block_jobs()
# Delete drive0's successful target and eliminate our record of the
- # unsuccessful drive1 target. Then re-run the same transaction.
+ # unsuccessful drive1 target.
dr0bm0.del_target()
dr1bm0.del_target()
+ if race:
+ # Don't re-run the transaction, we only wanted to test the race.
+ self.vm.shutdown()
+ return
+
+ # Re-run the same transaction:
target0 = self.prepare_backup(dr0bm0)
target1 = self.prepare_backup(dr1bm0)
self.vm.shutdown()
self.check_backups()
+ def test_transaction_failure(self):
+ '''Test: Verify backups made from a transaction that partially fails.
+
+ Add a second drive with its own unique pattern, and add a bitmap to each
+ drive. Use blkdebug to interfere with the backup on just one drive and
+ attempt to create a coherent incremental backup across both drives.
+
+ verify a failure in one but not both, then delete the failed stubs and
+ re-run the same transaction.
+
+ verify that both incrementals are created successfully.
+ '''
+ self.do_transaction_failure_test()
+
+ def test_transaction_failure_race(self):
+ '''Test: Verify that transactions with jobs that have no data to
+ transfer do not cause race conditions in the cancellation of the entire
+ transaction job group.
+ '''
+ self.do_transaction_failure_test(race=True)
+
def test_sync_dirty_bitmap_missing(self):
self.assert_no_active_block_jobs()
'bitmap0', self.drives[0],
granularity=64000)
+ def test_growing_before_backup(self):
+ '''
+ Test: Add a bitmap, truncate the image, write past the old
+ end, do a backup.
+
+ Incremental backup should not ignore dirty bits past the old
+ image end.
+ '''
+ self.assert_no_active_block_jobs()
+
+ self.create_anchor_backup()
+
+ self.add_bitmap('bitmap0', self.drives[0])
+
+ res = self.vm.qmp('block_resize', device=self.drives[0]['id'],
+ size=(65 * 1048576))
+ self.assert_qmp(res, 'return', {})
+
+ # Dirty the image past the old end
+ self.vm.hmp_qemu_io(self.drives[0]['id'], 'write 64M 64k')
+
+ target = self.prepare_backup(size='65M')
+ self.create_incremental(target=target)
+
+ self.vm.shutdown()
+ self.check_backups()
+
class TestIncrementalBackupBlkdebug(TestIncrementalBackupBase):
'''Incremental backup tests that utilize a BlkDebug filter on drive0.'''
'''
drive0 = self.drives[0]
- result = self.vm.qmp('blockdev-add', options={
- 'id': drive0['id'],
- 'driver': drive0['fmt'],
- 'file': {
+ result = self.vm.qmp('blockdev-add',
+ node_name=drive0['id'],
+ driver=drive0['fmt'],
+ file={
'driver': 'blkdebug',
'image': {
'driver': 'file',
'once': True
}],
}
- })
+ )
self.assert_qmp(result, 'return', {})
self.create_anchor_backup(drive0)
self.vm.shutdown()
self.check_backups()
+ def test_incremental_pause(self):
+ """
+ Test an incremental backup that errors into a pause and is resumed.
+ """
+
+ drive0 = self.drives[0]
+ # NB: The blkdebug script here looks for a "flush, read" pattern.
+ # The flush occurs in hmp_io_writes, and the read during the block job.
+ result = self.vm.qmp('blockdev-add',
+ node_name=drive0['id'],
+ driver=drive0['fmt'],
+ file={
+ 'driver': 'blkdebug',
+ 'image': {
+ 'driver': 'file',
+ 'filename': drive0['file']
+ },
+ 'set-state': [{
+ 'event': 'flush_to_disk',
+ 'state': 1,
+ 'new_state': 2
+ }],
+ 'inject-error': [{
+ 'event': 'read_aio',
+ 'errno': 5,
+ 'state': 2,
+ 'immediately': False,
+ 'once': True
+ }],
+ })
+ self.assert_qmp(result, 'return', {})
+ self.create_anchor_backup(drive0)
+ bitmap = self.add_bitmap('bitmap0', drive0)
+
+ # Emulate guest activity
+ self.hmp_io_writes(drive0['id'], (('0xab', 0, 512),
+ ('0xfe', '16M', '256k'),
+ ('0x64', '32736k', '64k')))
+
+ # Bitmap Status Check
+ self.assertTrue(self.vm.check_bitmap_status(
+ drive0['id'], bitmap.name, {
+ 'count': 458752,
+ 'granularity': 65536,
+ 'status': 'active',
+ 'busy': False,
+ 'recording': True
+ }))
+
+ # Start backup
+ parent, _ = bitmap.last_target()
+ target = self.prepare_backup(bitmap, parent)
+ res = self.vm.qmp('drive-backup',
+ job_id=bitmap.drive['id'],
+ device=bitmap.drive['id'],
+ sync='incremental',
+ bitmap=bitmap.name,
+ format=bitmap.drive['fmt'],
+ target=target,
+ mode='existing',
+ on_source_error='stop')
+ self.assert_qmp(res, 'return', {})
+
+ # Wait for the error
+ event = self.vm.event_wait(name="BLOCK_JOB_ERROR",
+ match={"data":{"device":bitmap.drive['id']}})
+ self.assert_qmp(event, 'data', {'device': bitmap.drive['id'],
+ 'action': 'stop',
+ 'operation': 'read'})
+
+ # Bitmap Status Check
+ self.assertTrue(self.vm.check_bitmap_status(
+ drive0['id'], bitmap.name, {
+ 'count': 458752,
+ 'granularity': 65536,
+ 'status': 'frozen',
+ 'busy': True,
+ 'recording': True
+ }))
+
+ # Resume and check incremental backup for consistency
+ res = self.vm.qmp('block-job-resume', device=bitmap.drive['id'])
+ self.assert_qmp(res, 'return', {})
+ self.wait_qmp_backup(bitmap.drive['id'])
+
+ # Bitmap Status Check
+ self.assertTrue(self.vm.check_bitmap_status(
+ drive0['id'], bitmap.name, {
+ 'count': 0,
+ 'granularity': 65536,
+ 'status': 'active',
+ 'busy': False,
+ 'recording': True
+ }))
+
+ # Finalize / Cleanup
+ self.make_reference_backup(bitmap)
+ self.vm.shutdown()
+ self.check_backups()
+
if __name__ == '__main__':
- iotests.main(supported_fmts=['qcow2'])
+ iotests.main(supported_fmts=['qcow2'],
+ supported_protocols=['file'])