xref: /openbmc/qemu/tests/qemu-iotests/056 (revision ac89de40)
1#!/usr/bin/env python
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, '-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)
63        self.vm.launch()
64
65    def tearDown(self):
66        self.vm.shutdown()
67        os.remove(test_img)
68        os.remove(backing_img)
69        try:
70            os.remove(target_img)
71        except OSError:
72            pass
73
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', {})
79
80        self.wait_until_completed(check_offset=False)
81
82        self.assert_no_active_block_jobs()
83        self.vm.shutdown()
84        self.assertTrue(iotests.compare_images(test_img, target_img),
85                        'target image does not match source after backup')
86
87    def test_cancel_sync_none(self):
88        self.assert_no_active_block_jobs()
89
90        result = self.vm.qmp('drive-backup', device='drive0',
91                             sync='none', target=target_img)
92        self.assert_qmp(result, 'return', {})
93        time.sleep(1)
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.
97
98        event = self.cancel_and_wait()
99        self.assert_qmp(event, 'data/type', 'backup')
100
101        self.vm.shutdown()
102        time.sleep(1)
103        self.assertEqual(-1, qemu_io('-c', 'read -P0x41 0 512', target_img).find("verification failed"))
104
105class TestBeforeWriteNotifier(iotests.QMPTestCase):
106    def setUp(self):
107        self.vm = iotests.VM().add_drive_raw("file=blkdebug::null-co://,id=drive0,align=65536,driver=blkdebug")
108        self.vm.launch()
109
110    def tearDown(self):
111        self.vm.shutdown()
112        os.remove(target_img)
113
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')
130
131class BackupTest(iotests.QMPTestCase):
132    def setUp(self):
133        self.vm = iotests.VM()
134        self.test_img = img_create('test')
135        self.dest_img = img_create('dest')
136        self.vm.add_drive(self.test_img)
137        self.vm.launch()
138
139    def tearDown(self):
140        self.vm.shutdown()
141        try_remove(self.test_img)
142        try_remove(self.dest_img)
143
144    def hmp_io_writes(self, drive, patterns):
145        for pattern in patterns:
146            self.vm.hmp_qemu_io(drive, 'write -P%s %s %s' % pattern)
147        self.vm.hmp_qemu_io(drive, 'flush')
148
149    def qmp_backup_and_wait(self, cmd='drive-backup', serror=None,
150                            aerror=None, **kwargs):
151        if not self.qmp_backup(cmd, serror, **kwargs):
152            return False
153        return self.qmp_backup_wait(kwargs['device'], aerror)
154
155    def qmp_backup(self, cmd='drive-backup',
156                   error=None, **kwargs):
157        self.assertTrue('device' in kwargs)
158        res = self.vm.qmp(cmd, **kwargs)
159        if error:
160            self.assert_qmp(res, 'error/desc', error)
161            return False
162        self.assert_qmp(res, 'return', {})
163        return True
164
165    def qmp_backup_wait(self, device, error=None):
166        event = self.vm.event_wait(name="BLOCK_JOB_COMPLETED",
167                                   match={'data': {'device': device}})
168        self.assertNotEqual(event, None)
169        try:
170            failure = self.dictpath(event, 'data/error')
171        except AssertionError:
172            # Backup succeeded.
173            self.assert_qmp(event, 'data/offset', event['data']['len'])
174            return True
175        else:
176            # Failure.
177            self.assert_qmp(event, 'data/error', qerror)
178            return False
179
180    def test_dismiss_false(self):
181        res = self.vm.qmp('query-block-jobs')
182        self.assert_qmp(res, 'return', [])
183        self.qmp_backup_and_wait(device='drive0', format=iotests.imgfmt,
184                                 sync='full', target=self.dest_img,
185                                 auto_dismiss=True)
186        res = self.vm.qmp('query-block-jobs')
187        self.assert_qmp(res, 'return', [])
188
189    def test_dismiss_true(self):
190        res = self.vm.qmp('query-block-jobs')
191        self.assert_qmp(res, 'return', [])
192        self.qmp_backup_and_wait(device='drive0', format=iotests.imgfmt,
193                                 sync='full', target=self.dest_img,
194                                 auto_dismiss=False)
195        res = self.vm.qmp('query-block-jobs')
196        self.assert_qmp(res, 'return[0]/status', 'concluded')
197        res = self.vm.qmp('block-job-dismiss', id='drive0')
198        self.assert_qmp(res, 'return', {})
199        res = self.vm.qmp('query-block-jobs')
200        self.assert_qmp(res, 'return', [])
201
202    def test_dismiss_bad_id(self):
203        res = self.vm.qmp('query-block-jobs')
204        self.assert_qmp(res, 'return', [])
205        res = self.vm.qmp('block-job-dismiss', id='foobar')
206        self.assert_qmp(res, 'error/class', 'DeviceNotActive')
207
208    def test_dismiss_collision(self):
209        res = self.vm.qmp('query-block-jobs')
210        self.assert_qmp(res, 'return', [])
211        self.qmp_backup_and_wait(device='drive0', format=iotests.imgfmt,
212                                 sync='full', target=self.dest_img,
213                                 auto_dismiss=False)
214        res = self.vm.qmp('query-block-jobs')
215        self.assert_qmp(res, 'return[0]/status', 'concluded')
216        # Leave zombie job un-dismissed, observe a failure:
217        res = self.qmp_backup_and_wait(serror='Need a root block node',
218                                       device='drive0', format=iotests.imgfmt,
219                                       sync='full', target=self.dest_img,
220                                       auto_dismiss=False)
221        self.assertEqual(res, False)
222        # OK, dismiss the zombie.
223        res = self.vm.qmp('block-job-dismiss', id='drive0')
224        self.assert_qmp(res, 'return', {})
225        res = self.vm.qmp('query-block-jobs')
226        self.assert_qmp(res, 'return', [])
227        # Ensure it's really gone.
228        self.qmp_backup_and_wait(device='drive0', format=iotests.imgfmt,
229                                 sync='full', target=self.dest_img,
230                                 auto_dismiss=False)
231
232    def dismissal_failure(self, dismissal_opt):
233        res = self.vm.qmp('query-block-jobs')
234        self.assert_qmp(res, 'return', [])
235        # Give blkdebug something to chew on
236        self.hmp_io_writes('drive0',
237                           (('0x9a', 0, 512),
238                           ('0x55', '8M', '352k'),
239                           ('0x78', '15872k', '1M')))
240        # Add destination node via blkdebug
241        res = self.vm.qmp('blockdev-add',
242                          node_name='target0',
243                          driver=iotests.imgfmt,
244                          file={
245                              'driver': 'blkdebug',
246                              'image': {
247                                  'driver': 'file',
248                                  'filename': self.dest_img
249                              },
250                              'inject-error': [{
251                                  'event': 'write_aio',
252                                  'errno': 5,
253                                  'immediately': False,
254                                  'once': True
255                              }],
256                          })
257        self.assert_qmp(res, 'return', {})
258
259        res = self.qmp_backup(cmd='blockdev-backup',
260                              device='drive0', target='target0',
261                              on_target_error='stop',
262                              sync='full',
263                              auto_dismiss=dismissal_opt)
264        self.assertTrue(res)
265        event = self.vm.event_wait(name="BLOCK_JOB_ERROR",
266                                   match={'data': {'device': 'drive0'}})
267        self.assertNotEqual(event, None)
268        # OK, job should be wedged
269        res = self.vm.qmp('query-block-jobs')
270        self.assert_qmp(res, 'return[0]/status', 'paused')
271        res = self.vm.qmp('block-job-dismiss', id='drive0')
272        self.assert_qmp(res, 'error/desc',
273                        "Job 'drive0' in state 'paused' cannot accept"
274                        " command verb 'dismiss'")
275        res = self.vm.qmp('query-block-jobs')
276        self.assert_qmp(res, 'return[0]/status', 'paused')
277        # OK, unstick job and move forward.
278        res = self.vm.qmp('block-job-resume', device='drive0')
279        self.assert_qmp(res, 'return', {})
280        # And now we need to wait for it to conclude;
281        res = self.qmp_backup_wait(device='drive0')
282        self.assertTrue(res)
283        if not dismissal_opt:
284            # Job should now be languishing:
285            res = self.vm.qmp('query-block-jobs')
286            self.assert_qmp(res, 'return[0]/status', 'concluded')
287            res = self.vm.qmp('block-job-dismiss', id='drive0')
288            self.assert_qmp(res, 'return', {})
289            res = self.vm.qmp('query-block-jobs')
290            self.assert_qmp(res, 'return', [])
291
292    def test_dismiss_premature(self):
293        self.dismissal_failure(False)
294
295    def test_dismiss_erroneous(self):
296        self.dismissal_failure(True)
297
298if __name__ == '__main__':
299    iotests.main(supported_fmts=['qcow2', 'qed'])
300