3 # Tests for drive-backup
5 # Copyright (C) 2013 Red Hat, Inc.
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 from iotests
import qemu_img
, qemu_io
, create_image
28 backing_img
= os
.path
.join(iotests
.test_dir
, 'backing.img')
29 test_img
= os
.path
.join(iotests
.test_dir
, 'test.img')
30 target_img
= os
.path
.join(iotests
.test_dir
, 'target.img')
32 def img_create(img
, fmt
=iotests
.imgfmt
, size
='64M', **kwargs
):
33 fullname
= os
.path
.join(iotests
.test_dir
, '%s.%s' % (img
, fmt
))
35 for k
,v
in kwargs
.items():
36 optargs
= optargs
+ ['-o', '%s=%s' % (k
,v
)]
37 args
= ['create', '-f', fmt
] + optargs
+ [fullname
, size
]
38 iotests
.qemu_img(*args
)
47 def io_write_patterns(img
, patterns
):
48 for pattern
in patterns
:
49 iotests
.qemu_io('-c', 'write -P%s %s %s' % pattern
, img
)
52 class TestSyncModesNoneAndTop(iotests
.QMPTestCase
):
53 image_len
= 64 * 1024 * 1024 # MB
56 create_image(backing_img
, TestSyncModesNoneAndTop
.image_len
)
57 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
58 qemu_io('-c', 'write -P0x41 0 512', test_img
)
59 qemu_io('-c', 'write -P0xd5 1M 32k', test_img
)
60 qemu_io('-c', 'write -P0xdc 32M 124k', test_img
)
61 qemu_io('-c', 'write -P0xdc 67043328 64k', test_img
)
62 self
.vm
= iotests
.VM().add_drive(test_img
)
68 os
.remove(backing_img
)
74 def test_complete_top(self
):
75 self
.assert_no_active_block_jobs()
76 result
= self
.vm
.qmp('drive-backup', device
='drive0', sync
='top',
77 format
=iotests
.imgfmt
, target
=target_img
)
78 self
.assert_qmp(result
, 'return', {})
80 self
.wait_until_completed(check_offset
=False)
82 self
.assert_no_active_block_jobs()
84 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
85 'target image does not match source after backup')
87 def test_cancel_sync_none(self
):
88 self
.assert_no_active_block_jobs()
90 result
= self
.vm
.qmp('drive-backup', device
='drive0',
91 sync
='none', target
=target_img
)
92 self
.assert_qmp(result
, 'return', {})
94 self
.vm
.hmp_qemu_io('drive0', 'write -P0x5e 0 512')
95 self
.vm
.hmp_qemu_io('drive0', 'aio_flush')
96 # Verify that the original contents exist in the target image.
98 event
= self
.cancel_and_wait()
99 self
.assert_qmp(event
, 'data/type', 'backup')
103 self
.assertEqual(-1, qemu_io('-c', 'read -P0x41 0 512', target_img
).find("verification failed"))
105 class TestBeforeWriteNotifier(iotests
.QMPTestCase
):
107 self
.vm
= iotests
.VM().add_drive_raw("file=blkdebug::null-co://,id=drive0,align=65536,driver=blkdebug")
112 os
.remove(target_img
)
114 def test_before_write_notifier(self
):
115 self
.vm
.pause_drive("drive0")
116 result
= self
.vm
.qmp('drive-backup', device
='drive0',
117 sync
='full', target
=target_img
,
118 format
="file", speed
=1)
119 self
.assert_qmp(result
, 'return', {})
120 result
= self
.vm
.qmp('block-job-pause', device
="drive0")
121 self
.assert_qmp(result
, 'return', {})
122 # Speed is low enough that this must be an uncopied range, which will
123 # trigger the before write notifier
124 self
.vm
.hmp_qemu_io('drive0', 'aio_write -P 1 512512 512')
125 self
.vm
.resume_drive("drive0")
126 result
= self
.vm
.qmp('block-job-resume', device
="drive0")
127 self
.assert_qmp(result
, 'return', {})
128 event
= self
.cancel_and_wait()
129 self
.assert_qmp(event
, 'data/type', 'backup')
131 class BackupTest(iotests
.QMPTestCase
):
133 self
.vm
= iotests
.VM()
134 self
.test_img
= img_create('test')
135 self
.dest_img
= img_create('dest')
136 self
.ref_img
= img_create('ref')
137 self
.vm
.add_drive(self
.test_img
)
142 try_remove(self
.test_img
)
143 try_remove(self
.dest_img
)
144 try_remove(self
.ref_img
)
146 def hmp_io_writes(self
, drive
, patterns
):
147 for pattern
in patterns
:
148 self
.vm
.hmp_qemu_io(drive
, 'write -P%s %s %s' % pattern
)
149 self
.vm
.hmp_qemu_io(drive
, 'flush')
151 def qmp_backup_and_wait(self
, cmd
='drive-backup', serror
=None,
152 aerror
=None, **kwargs
):
153 if not self
.qmp_backup(cmd
, serror
, **kwargs
):
155 return self
.qmp_backup_wait(kwargs
['device'], aerror
)
157 def qmp_backup(self
, cmd
='drive-backup',
158 error
=None, **kwargs
):
159 self
.assertTrue('device' in kwargs
)
160 res
= self
.vm
.qmp(cmd
, **kwargs
)
162 self
.assert_qmp(res
, 'error/desc', error
)
164 self
.assert_qmp(res
, 'return', {})
167 def qmp_backup_wait(self
, device
, error
=None):
168 event
= self
.vm
.event_wait(name
="BLOCK_JOB_COMPLETED",
169 match
={'data': {'device': device
}})
170 self
.assertNotEqual(event
, None)
172 failure
= self
.dictpath(event
, 'data/error')
173 except AssertionError:
175 self
.assert_qmp(event
, 'data/offset', event
['data']['len'])
179 self
.assert_qmp(event
, 'data/error', qerror
)
182 def test_overlapping_writes(self
):
183 # Write something to back up
184 self
.hmp_io_writes('drive0', [('42', '0M', '2M')])
186 # Create a reference backup
187 self
.qmp_backup_and_wait(device
='drive0', format
=iotests
.imgfmt
,
188 sync
='full', target
=self
.ref_img
,
190 res
= self
.vm
.qmp('block-job-dismiss', id='drive0')
191 self
.assert_qmp(res
, 'return', {})
193 # Now to the test backup: We simulate the following guest
195 # (1) [1M + 64k, 1M + 128k): Afterwards, everything in that
196 # area should be in the target image, and we must not copy
197 # it again (because the source image has changed now)
198 # (64k is the job's cluster size)
199 # (2) [1M, 2M): The backup job must not get overeager. It
200 # must copy [1M, 1M + 64k) and [1M + 128k, 2M) separately,
201 # but not the area in between.
203 self
.qmp_backup(device
='drive0', format
=iotests
.imgfmt
, sync
='full',
204 target
=self
.dest_img
, speed
=1, auto_dismiss
=False)
206 self
.hmp_io_writes('drive0', [('23', '%ik' % (1024 + 64), '64k'),
209 # Let the job complete
210 res
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=0)
211 self
.assert_qmp(res
, 'return', {})
212 self
.qmp_backup_wait('drive0')
213 res
= self
.vm
.qmp('block-job-dismiss', id='drive0')
214 self
.assert_qmp(res
, 'return', {})
216 self
.assertTrue(iotests
.compare_images(self
.ref_img
, self
.dest_img
),
217 'target image does not match reference image')
219 def test_dismiss_false(self
):
220 res
= self
.vm
.qmp('query-block-jobs')
221 self
.assert_qmp(res
, 'return', [])
222 self
.qmp_backup_and_wait(device
='drive0', format
=iotests
.imgfmt
,
223 sync
='full', target
=self
.dest_img
,
225 res
= self
.vm
.qmp('query-block-jobs')
226 self
.assert_qmp(res
, 'return', [])
228 def test_dismiss_true(self
):
229 res
= self
.vm
.qmp('query-block-jobs')
230 self
.assert_qmp(res
, 'return', [])
231 self
.qmp_backup_and_wait(device
='drive0', format
=iotests
.imgfmt
,
232 sync
='full', target
=self
.dest_img
,
234 res
= self
.vm
.qmp('query-block-jobs')
235 self
.assert_qmp(res
, 'return[0]/status', 'concluded')
236 res
= self
.vm
.qmp('block-job-dismiss', id='drive0')
237 self
.assert_qmp(res
, 'return', {})
238 res
= self
.vm
.qmp('query-block-jobs')
239 self
.assert_qmp(res
, 'return', [])
241 def test_dismiss_bad_id(self
):
242 res
= self
.vm
.qmp('query-block-jobs')
243 self
.assert_qmp(res
, 'return', [])
244 res
= self
.vm
.qmp('block-job-dismiss', id='foobar')
245 self
.assert_qmp(res
, 'error/class', 'DeviceNotActive')
247 def test_dismiss_collision(self
):
248 res
= self
.vm
.qmp('query-block-jobs')
249 self
.assert_qmp(res
, 'return', [])
250 self
.qmp_backup_and_wait(device
='drive0', format
=iotests
.imgfmt
,
251 sync
='full', target
=self
.dest_img
,
253 res
= self
.vm
.qmp('query-block-jobs')
254 self
.assert_qmp(res
, 'return[0]/status', 'concluded')
255 # Leave zombie job un-dismissed, observe a failure:
256 res
= self
.qmp_backup_and_wait(serror
="Node 'drive0' is busy: block device is in use by block job: backup",
257 device
='drive0', format
=iotests
.imgfmt
,
258 sync
='full', target
=self
.dest_img
,
260 self
.assertEqual(res
, False)
261 # OK, dismiss the zombie.
262 res
= self
.vm
.qmp('block-job-dismiss', id='drive0')
263 self
.assert_qmp(res
, 'return', {})
264 res
= self
.vm
.qmp('query-block-jobs')
265 self
.assert_qmp(res
, 'return', [])
266 # Ensure it's really gone.
267 self
.qmp_backup_and_wait(device
='drive0', format
=iotests
.imgfmt
,
268 sync
='full', target
=self
.dest_img
,
271 def dismissal_failure(self
, dismissal_opt
):
272 res
= self
.vm
.qmp('query-block-jobs')
273 self
.assert_qmp(res
, 'return', [])
274 # Give blkdebug something to chew on
275 self
.hmp_io_writes('drive0',
277 ('0x55', '8M', '352k'),
278 ('0x78', '15872k', '1M')))
279 # Add destination node via blkdebug
280 res
= self
.vm
.qmp('blockdev-add',
282 driver
=iotests
.imgfmt
,
284 'driver': 'blkdebug',
287 'filename': self
.dest_img
290 'event': 'write_aio',
292 'immediately': False,
296 self
.assert_qmp(res
, 'return', {})
298 res
= self
.qmp_backup(cmd
='blockdev-backup',
299 device
='drive0', target
='target0',
300 on_target_error
='stop',
302 auto_dismiss
=dismissal_opt
)
304 event
= self
.vm
.event_wait(name
="BLOCK_JOB_ERROR",
305 match
={'data': {'device': 'drive0'}})
306 self
.assertNotEqual(event
, None)
307 # OK, job should be wedged
308 res
= self
.vm
.qmp('query-block-jobs')
309 self
.assert_qmp(res
, 'return[0]/status', 'paused')
310 res
= self
.vm
.qmp('block-job-dismiss', id='drive0')
311 self
.assert_qmp(res
, 'error/desc',
312 "Job 'drive0' in state 'paused' cannot accept"
313 " command verb 'dismiss'")
314 res
= self
.vm
.qmp('query-block-jobs')
315 self
.assert_qmp(res
, 'return[0]/status', 'paused')
316 # OK, unstick job and move forward.
317 res
= self
.vm
.qmp('block-job-resume', device
='drive0')
318 self
.assert_qmp(res
, 'return', {})
319 # And now we need to wait for it to conclude;
320 res
= self
.qmp_backup_wait(device
='drive0')
322 if not dismissal_opt
:
323 # Job should now be languishing:
324 res
= self
.vm
.qmp('query-block-jobs')
325 self
.assert_qmp(res
, 'return[0]/status', 'concluded')
326 res
= self
.vm
.qmp('block-job-dismiss', id='drive0')
327 self
.assert_qmp(res
, 'return', {})
328 res
= self
.vm
.qmp('query-block-jobs')
329 self
.assert_qmp(res
, 'return', [])
331 def test_dismiss_premature(self
):
332 self
.dismissal_failure(False)
334 def test_dismiss_erroneous(self
):
335 self
.dismissal_failure(True)
337 if __name__
== '__main__':
338 iotests
.main(supported_fmts
=['qcow2', 'qed'],
339 supported_protocols
=['file'])