xref: /openbmc/qemu/tests/qemu-iotests/056 (revision 0ed93f4c)
1#!/usr/bin/env python3
2#
3# Tests for drive-backup
4#
5# Copyright (C) 2013 Red Hat, Inc.
6#
7# Based on 041.
8#
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.
13#
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.
18#
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/>.
21#
22
23import time
24import os
25import iotests
26from iotests import qemu_img, qemu_io, create_image
27
28backing_img = os.path.join(iotests.test_dir, 'backing.img')
29test_img = os.path.join(iotests.test_dir, 'test.img')
30target_img = os.path.join(iotests.test_dir, 'target.img')
31
32def img_create(img, fmt=iotests.imgfmt, size='64M', **kwargs):
33    fullname = os.path.join(iotests.test_dir, '%s.%s' % (img, fmt))
34    optargs = []
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)
39    return fullname
40
41def try_remove(img):
42    try:
43        os.remove(img)
44    except OSError:
45        pass
46
47def io_write_patterns(img, patterns):
48    for pattern in patterns:
49        iotests.qemu_io('-c', 'write -P%s %s %s' % pattern, img)
50
51
52class TestSyncModesNoneAndTop(iotests.QMPTestCase):
53    image_len = 64 * 1024 * 1024 # MB
54
55    def setUp(self):
56        create_image(backing_img, TestSyncModesNoneAndTop.image_len)
57        qemu_img('create', '-f', iotests.imgfmt,
58                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
59        qemu_io('-c', 'write -P0x41 0 512', test_img)
60        qemu_io('-c', 'write -P0xd5 1M 32k', test_img)
61        qemu_io('-c', 'write -P0xdc 32M 124k', test_img)
62        qemu_io('-c', 'write -P0xdc 67043328 64k', test_img)
63        self.vm = iotests.VM().add_drive(test_img)
64        self.vm.launch()
65
66    def tearDown(self):
67        self.vm.shutdown()
68        os.remove(test_img)
69        os.remove(backing_img)
70        try:
71            os.remove(target_img)
72        except OSError:
73            pass
74
75    def test_complete_top(self):
76        self.assert_no_active_block_jobs()
77        result = self.vm.qmp('drive-backup', device='drive0', sync='top',
78                             format=iotests.imgfmt, target=target_img)
79        self.assert_qmp(result, 'return', {})
80
81        self.wait_until_completed(check_offset=False)
82
83        self.assert_no_active_block_jobs()
84        self.vm.shutdown()
85        self.assertTrue(iotests.compare_images(test_img, target_img),
86                        'target image does not match source after backup')
87
88    def test_cancel_sync_none(self):
89        self.assert_no_active_block_jobs()
90
91        result = self.vm.qmp('drive-backup', device='drive0',
92                             sync='none', target=target_img)
93        self.assert_qmp(result, 'return', {})
94        time.sleep(1)
95        self.vm.hmp_qemu_io('drive0', 'write -P0x5e 0 512')
96        self.vm.hmp_qemu_io('drive0', 'aio_flush')
97        # Verify that the original contents exist in the target image.
98
99        event = self.cancel_and_wait()
100        self.assert_qmp(event, 'data/type', 'backup')
101
102        self.vm.shutdown()
103        time.sleep(1)
104        self.assertEqual(-1, qemu_io('-c', 'read -P0x41 0 512', target_img).find("verification failed"))
105
106class TestBeforeWriteNotifier(iotests.QMPTestCase):
107    def setUp(self):
108        self.vm = iotests.VM().add_drive_raw("file=blkdebug::null-co://,id=drive0,align=65536,driver=blkdebug")
109        self.vm.launch()
110
111    def tearDown(self):
112        self.vm.shutdown()
113        os.remove(target_img)
114
115    def test_before_write_notifier(self):
116        self.vm.pause_drive("drive0")
117        result = self.vm.qmp('drive-backup', device='drive0',
118                             sync='full', target=target_img,
119                             format="file", speed=1)
120        self.assert_qmp(result, 'return', {})
121        result = self.vm.qmp('block-job-pause', device="drive0")
122        self.assert_qmp(result, 'return', {})
123        # Speed is low enough that this must be an uncopied range, which will
124        # trigger the before write notifier
125        self.vm.hmp_qemu_io('drive0', 'aio_write -P 1 512512 512')
126        self.vm.resume_drive("drive0")
127        result = self.vm.qmp('block-job-resume', device="drive0")
128        self.assert_qmp(result, 'return', {})
129        event = self.cancel_and_wait()
130        self.assert_qmp(event, 'data/type', 'backup')
131
132class BackupTest(iotests.QMPTestCase):
133    def setUp(self):
134        self.vm = iotests.VM()
135        self.test_img = img_create('test')
136        self.dest_img = img_create('dest')
137        self.dest_img2 = img_create('dest2')
138        self.ref_img = img_create('ref')
139        self.vm.add_drive(self.test_img)
140        self.vm.launch()
141
142    def tearDown(self):
143        self.vm.shutdown()
144        try_remove(self.test_img)
145        try_remove(self.dest_img)
146        try_remove(self.dest_img2)
147        try_remove(self.ref_img)
148
149    def hmp_io_writes(self, drive, patterns):
150        for pattern in patterns:
151            self.vm.hmp_qemu_io(drive, 'write -P%s %s %s' % pattern)
152        self.vm.hmp_qemu_io(drive, 'flush')
153
154    def qmp_backup_and_wait(self, cmd='drive-backup', serror=None,
155                            aerror=None, **kwargs):
156        if not self.qmp_backup(cmd, serror, **kwargs):
157            return False
158        return self.qmp_backup_wait(kwargs['device'], aerror)
159
160    def qmp_backup(self, cmd='drive-backup',
161                   error=None, **kwargs):
162        self.assertTrue('device' in kwargs)
163        res = self.vm.qmp(cmd, **kwargs)
164        if error:
165            self.assert_qmp(res, 'error/desc', error)
166            return False
167        self.assert_qmp(res, 'return', {})
168        return True
169
170    def qmp_backup_wait(self, device, error=None):
171        event = self.vm.event_wait(name="BLOCK_JOB_COMPLETED",
172                                   match={'data': {'device': device}})
173        self.assertNotEqual(event, None)
174        try:
175            failure = self.dictpath(event, 'data/error')
176        except AssertionError:
177            # Backup succeeded.
178            self.assert_qmp(event, 'data/offset', event['data']['len'])
179            return True
180        else:
181            # Failure.
182            self.assert_qmp(event, 'data/error', qerror)
183            return False
184
185    def test_overlapping_writes(self):
186        # Write something to back up
187        self.hmp_io_writes('drive0', [('42', '0M', '2M')])
188
189        # Create a reference backup
190        self.qmp_backup_and_wait(device='drive0', format=iotests.imgfmt,
191                                 sync='full', target=self.ref_img,
192                                 auto_dismiss=False)
193        res = self.vm.qmp('block-job-dismiss', id='drive0')
194        self.assert_qmp(res, 'return', {})
195
196        # Now to the test backup: We simulate the following guest
197        # writes:
198        # (1) [1M + 64k, 1M + 128k): Afterwards, everything in that
199        #     area should be in the target image, and we must not copy
200        #     it again (because the source image has changed now)
201        #     (64k is the job's cluster size)
202        # (2) [1M, 2M): The backup job must not get overeager.  It
203        #     must copy [1M, 1M + 64k) and [1M + 128k, 2M) separately,
204        #     but not the area in between.
205
206        self.qmp_backup(device='drive0', format=iotests.imgfmt, sync='full',
207                        target=self.dest_img, speed=1, auto_dismiss=False)
208
209        self.hmp_io_writes('drive0', [('23', '%ik' % (1024 + 64), '64k'),
210                                      ('66', '1M', '1M')])
211
212        # Let the job complete
213        res = self.vm.qmp('block-job-set-speed', device='drive0', speed=0)
214        self.assert_qmp(res, 'return', {})
215        self.qmp_backup_wait('drive0')
216        res = self.vm.qmp('block-job-dismiss', id='drive0')
217        self.assert_qmp(res, 'return', {})
218
219        self.assertTrue(iotests.compare_images(self.ref_img, self.dest_img),
220                        'target image does not match reference image')
221
222    def test_dismiss_false(self):
223        res = self.vm.qmp('query-block-jobs')
224        self.assert_qmp(res, 'return', [])
225        self.qmp_backup_and_wait(device='drive0', format=iotests.imgfmt,
226                                 sync='full', target=self.dest_img,
227                                 auto_dismiss=True)
228        res = self.vm.qmp('query-block-jobs')
229        self.assert_qmp(res, 'return', [])
230
231    def test_dismiss_true(self):
232        res = self.vm.qmp('query-block-jobs')
233        self.assert_qmp(res, 'return', [])
234        self.qmp_backup_and_wait(device='drive0', format=iotests.imgfmt,
235                                 sync='full', target=self.dest_img,
236                                 auto_dismiss=False)
237        res = self.vm.qmp('query-block-jobs')
238        self.assert_qmp(res, 'return[0]/status', 'concluded')
239        res = self.vm.qmp('block-job-dismiss', id='drive0')
240        self.assert_qmp(res, 'return', {})
241        res = self.vm.qmp('query-block-jobs')
242        self.assert_qmp(res, 'return', [])
243
244    def test_dismiss_bad_id(self):
245        res = self.vm.qmp('query-block-jobs')
246        self.assert_qmp(res, 'return', [])
247        res = self.vm.qmp('block-job-dismiss', id='foobar')
248        self.assert_qmp(res, 'error/class', 'DeviceNotActive')
249
250    def test_dismiss_collision(self):
251        res = self.vm.qmp('query-block-jobs')
252        self.assert_qmp(res, 'return', [])
253        self.qmp_backup_and_wait(device='drive0', format=iotests.imgfmt,
254                                 sync='full', target=self.dest_img,
255                                 auto_dismiss=False)
256        res = self.vm.qmp('query-block-jobs')
257        self.assert_qmp(res, 'return[0]/status', 'concluded')
258        # Leave zombie job un-dismissed, observe a failure:
259        res = self.qmp_backup_and_wait(serror="Job ID 'drive0' already in use",
260                                       device='drive0', format=iotests.imgfmt,
261                                       sync='full', target=self.dest_img2,
262                                       auto_dismiss=False)
263        self.assertEqual(res, False)
264        # OK, dismiss the zombie.
265        res = self.vm.qmp('block-job-dismiss', id='drive0')
266        self.assert_qmp(res, 'return', {})
267        res = self.vm.qmp('query-block-jobs')
268        self.assert_qmp(res, 'return', [])
269        # Ensure it's really gone.
270        self.qmp_backup_and_wait(device='drive0', format=iotests.imgfmt,
271                                 sync='full', target=self.dest_img2,
272                                 auto_dismiss=False)
273
274    def dismissal_failure(self, dismissal_opt):
275        res = self.vm.qmp('query-block-jobs')
276        self.assert_qmp(res, 'return', [])
277        # Give blkdebug something to chew on
278        self.hmp_io_writes('drive0',
279                           (('0x9a', 0, 512),
280                           ('0x55', '8M', '352k'),
281                           ('0x78', '15872k', '1M')))
282        # Add destination node via blkdebug
283        res = self.vm.qmp('blockdev-add',
284                          node_name='target0',
285                          driver=iotests.imgfmt,
286                          file={
287                              'driver': 'blkdebug',
288                              'image': {
289                                  'driver': 'file',
290                                  'filename': self.dest_img
291                              },
292                              'inject-error': [{
293                                  'event': 'write_aio',
294                                  'errno': 5,
295                                  'immediately': False,
296                                  'once': True
297                              }],
298                          })
299        self.assert_qmp(res, 'return', {})
300
301        res = self.qmp_backup(cmd='blockdev-backup',
302                              device='drive0', target='target0',
303                              on_target_error='stop',
304                              sync='full',
305                              auto_dismiss=dismissal_opt)
306        self.assertTrue(res)
307        event = self.vm.event_wait(name="BLOCK_JOB_ERROR",
308                                   match={'data': {'device': 'drive0'}})
309        self.assertNotEqual(event, None)
310        # OK, job should be wedged
311        res = self.vm.qmp('query-block-jobs')
312        self.assert_qmp(res, 'return[0]/status', 'paused')
313        res = self.vm.qmp('block-job-dismiss', id='drive0')
314        self.assert_qmp(res, 'error/desc',
315                        "Job 'drive0' in state 'paused' cannot accept"
316                        " command verb 'dismiss'")
317        res = self.vm.qmp('query-block-jobs')
318        self.assert_qmp(res, 'return[0]/status', 'paused')
319        # OK, unstick job and move forward.
320        res = self.vm.qmp('block-job-resume', device='drive0')
321        self.assert_qmp(res, 'return', {})
322        # And now we need to wait for it to conclude;
323        res = self.qmp_backup_wait(device='drive0')
324        self.assertTrue(res)
325        if not dismissal_opt:
326            # Job should now be languishing:
327            res = self.vm.qmp('query-block-jobs')
328            self.assert_qmp(res, 'return[0]/status', 'concluded')
329            res = self.vm.qmp('block-job-dismiss', id='drive0')
330            self.assert_qmp(res, 'return', {})
331            res = self.vm.qmp('query-block-jobs')
332            self.assert_qmp(res, 'return', [])
333
334    def test_dismiss_premature(self):
335        self.dismissal_failure(False)
336
337    def test_dismiss_erroneous(self):
338        self.dismissal_failure(True)
339
340if __name__ == '__main__':
341    iotests.main(supported_fmts=['qcow2', 'qed'],
342                 supported_protocols=['file'])
343