xref: /openbmc/qemu/tests/qemu-iotests/041 (revision 7c08eefc)
1#!/usr/bin/env python3
2# group: rw auto backing
3#
4# Tests for image mirroring.
5#
6# Copyright (C) 2012 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22import time
23import os
24import re
25import json
26import iotests
27from iotests import qemu_img, qemu_img_map, qemu_io
28
29backing_img = os.path.join(iotests.test_dir, 'backing.img')
30target_backing_img = os.path.join(iotests.test_dir, 'target-backing.img')
31test_img = os.path.join(iotests.test_dir, 'test.img')
32target_img = os.path.join(iotests.test_dir, 'target.img')
33
34quorum_img1 = os.path.join(iotests.test_dir, 'quorum1.img')
35quorum_img2 = os.path.join(iotests.test_dir, 'quorum2.img')
36quorum_img3 = os.path.join(iotests.test_dir, 'quorum3.img')
37quorum_repair_img = os.path.join(iotests.test_dir, 'quorum_repair.img')
38quorum_snapshot_file = os.path.join(iotests.test_dir, 'quorum_snapshot.img')
39
40nbd_sock_path = os.path.join(iotests.sock_dir, 'nbd.sock')
41
42class TestSingleDrive(iotests.QMPTestCase):
43    image_len = 1 * 1024 * 1024 # MB
44    qmp_cmd = 'drive-mirror'
45    qmp_target = target_img
46
47    def setUp(self):
48        iotests.create_image(backing_img, self.image_len)
49        qemu_img('create', '-f', iotests.imgfmt,
50                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
51        self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=base")
52        if iotests.qemu_default_machine == 'pc':
53            self.vm.add_drive(None, 'media=cdrom', 'ide')
54        self.vm.launch()
55
56    def tearDown(self):
57        self.vm.shutdown()
58        os.remove(test_img)
59        os.remove(backing_img)
60        try:
61            os.remove(target_img)
62        except OSError:
63            pass
64
65    def test_complete(self):
66        self.assert_no_active_block_jobs()
67
68        self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
69                    target=self.qmp_target)
70
71        self.complete_and_wait()
72        result = self.vm.qmp('query-block')
73        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
74        self.vm.shutdown()
75        self.assertTrue(iotests.compare_images(test_img, target_img),
76                        'target image does not match source after mirroring')
77
78    def test_cancel(self):
79        self.assert_no_active_block_jobs()
80
81        self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
82                    target=self.qmp_target)
83
84        self.cancel_and_wait(force=True)
85        result = self.vm.qmp('query-block')
86        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
87
88    def test_cancel_after_ready(self):
89        self.assert_no_active_block_jobs()
90
91        self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
92                    target=self.qmp_target)
93
94        self.wait_ready_and_cancel()
95        result = self.vm.qmp('query-block')
96        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
97        self.vm.shutdown()
98        self.assertTrue(iotests.compare_images(test_img, target_img),
99                        'target image does not match source after mirroring')
100
101    def test_pause(self):
102        self.assert_no_active_block_jobs()
103
104        self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
105                    target=self.qmp_target)
106
107        self.pause_job('drive0')
108
109        result = self.vm.qmp('query-block-jobs')
110        offset = self.dictpath(result, 'return[0]/offset')
111
112        time.sleep(0.5)
113        result = self.vm.qmp('query-block-jobs')
114        self.assert_qmp(result, 'return[0]/offset', offset)
115
116        self.vm.cmd('block-job-resume', device='drive0')
117
118        self.complete_and_wait()
119        self.vm.shutdown()
120        self.assertTrue(iotests.compare_images(test_img, target_img),
121                        'target image does not match source after mirroring')
122
123    def test_small_buffer(self):
124        self.assert_no_active_block_jobs()
125
126        # A small buffer is rounded up automatically
127        self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
128                    buf_size=4096, target=self.qmp_target)
129
130        self.complete_and_wait()
131        result = self.vm.qmp('query-block')
132        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
133        self.vm.shutdown()
134        self.assertTrue(iotests.compare_images(test_img, target_img),
135                        'target image does not match source after mirroring')
136
137    def test_small_buffer2(self):
138        self.assert_no_active_block_jobs()
139
140        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,size=%d'
141                        % (self.image_len, self.image_len), target_img)
142        self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
143                    buf_size=65536, mode='existing', target=self.qmp_target)
144
145        self.complete_and_wait()
146        result = self.vm.qmp('query-block')
147        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
148        self.vm.shutdown()
149        self.assertTrue(iotests.compare_images(test_img, target_img),
150                        'target image does not match source after mirroring')
151
152    def test_large_cluster(self):
153        self.assert_no_active_block_jobs()
154
155        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
156                        % (self.image_len, backing_img),
157                 '-F', 'raw', target_img)
158        self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
159                    mode='existing', target=self.qmp_target)
160
161        self.complete_and_wait()
162        result = self.vm.qmp('query-block')
163        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
164        self.vm.shutdown()
165        self.assertTrue(iotests.compare_images(test_img, target_img),
166                        'target image does not match source after mirroring')
167
168    # Tests that the insertion of the mirror_top filter node doesn't make a
169    # difference to query-block
170    def test_implicit_node(self):
171        self.assert_no_active_block_jobs()
172
173        self.vm.cmd(self.qmp_cmd, device='drive0', sync='full',
174                    target=self.qmp_target)
175
176        result = self.vm.qmp('query-block')
177        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
178        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
179        self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img)
180        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1)
181        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
182        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img)
183
184        result = self.vm.qmp('query-blockstats')
185        self.assert_qmp(result, 'return[0]/node-name', 'top')
186        self.assert_qmp(result, 'return[0]/backing/node-name', 'base')
187
188        self.cancel_and_wait(force=True)
189        result = self.vm.qmp('query-block')
190        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
191        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
192        self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img)
193        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1)
194        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
195        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img)
196
197        result = self.vm.qmp('query-blockstats')
198        self.assert_qmp(result, 'return[0]/node-name', 'top')
199        self.assert_qmp(result, 'return[0]/backing/node-name', 'base')
200
201    def test_medium_not_found(self):
202        if iotests.qemu_default_machine != 'pc':
203            return
204
205        result = self.vm.qmp(self.qmp_cmd, device='ide1-cd0', sync='full',
206                             target=self.qmp_target)
207        self.assert_qmp(result, 'error/class', 'GenericError')
208
209    def test_image_not_found(self):
210        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
211                             mode='existing', target=self.qmp_target)
212        self.assert_qmp(result, 'error/class', 'GenericError')
213
214    def test_device_not_found(self):
215        result = self.vm.qmp(self.qmp_cmd, device='nonexistent', sync='full',
216                             target=self.qmp_target)
217        self.assert_qmp(result, 'error/class', 'GenericError')
218
219class TestSingleBlockdev(TestSingleDrive):
220    qmp_cmd = 'blockdev-mirror'
221    qmp_target = 'node1'
222
223    def setUp(self):
224        TestSingleDrive.setUp(self)
225        qemu_img('create', '-f', iotests.imgfmt,
226                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
227        args = {'driver': iotests.imgfmt,
228                'node-name': self.qmp_target,
229                'file': { 'filename': target_img, 'driver': 'file' } }
230        self.vm.cmd("blockdev-add", args)
231
232    def test_mirror_to_self(self):
233        result = self.vm.qmp(self.qmp_cmd, job_id='job0',
234                             device=self.qmp_target, sync='full',
235                             target=self.qmp_target)
236        self.assert_qmp(result, 'error/class', 'GenericError')
237
238    def do_test_resize(self, device, node):
239        def pre_finalize():
240            if device:
241                result = self.vm.qmp('block_resize', device=device, size=65536)
242                self.assert_qmp(result, 'error/class', 'GenericError')
243
244            result = self.vm.qmp('block_resize', node_name=node, size=65536)
245            self.assert_qmp(result, 'error/class', 'GenericError')
246
247        self.vm.cmd(self.qmp_cmd, job_id='job0', device='drive0',
248                    sync='full', target=self.qmp_target,
249                    auto_finalize=False, auto_dismiss=False)
250
251        result = self.vm.run_job('job0', auto_finalize=False,
252                                 pre_finalize=pre_finalize)
253        self.assertEqual(result, None)
254
255    def test_source_resize(self):
256        self.do_test_resize('drive0', 'top')
257
258    def test_target_resize(self):
259        self.do_test_resize(None, self.qmp_target)
260
261    def do_test_target_size(self, size):
262        self.vm.cmd('block_resize', node_name=self.qmp_target,
263                    size=size)
264
265        self.vm.cmd(self.qmp_cmd, job_id='job0',
266                    device='drive0', sync='full', auto_dismiss=False,
267                    target=self.qmp_target)
268
269        result = self.vm.run_job('job0')
270        self.assertEqual(result, 'Source and target image have different sizes')
271
272    # qed does not support shrinking
273    @iotests.skip_for_formats(('qed'))
274    def test_small_target(self):
275        self.do_test_target_size(self.image_len // 2)
276
277    def test_large_target(self):
278        self.do_test_target_size(self.image_len * 2)
279
280    test_large_cluster = None
281    test_image_not_found = None
282    test_small_buffer2 = None
283
284class TestSingleDriveZeroLength(TestSingleDrive):
285    image_len = 0
286    test_small_buffer2 = None
287    test_large_cluster = None
288
289class TestSingleBlockdevZeroLength(TestSingleBlockdev):
290    image_len = 0
291    test_small_target = None
292    test_large_target = None
293
294class TestSingleDriveUnalignedLength(TestSingleDrive):
295    image_len = 1025 * 1024
296    test_small_buffer2 = None
297    test_large_cluster = None
298
299class TestSingleBlockdevUnalignedLength(TestSingleBlockdev):
300    image_len = 1025 * 1024
301
302class TestMirrorNoBacking(iotests.QMPTestCase):
303    image_len = 2 * 1024 * 1024 # MB
304
305    def setUp(self):
306        iotests.create_image(backing_img, TestMirrorNoBacking.image_len)
307        qemu_img('create', '-f', iotests.imgfmt,
308                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
309        self.vm = iotests.VM().add_drive(test_img)
310        self.vm.launch()
311
312    def tearDown(self):
313        self.vm.shutdown()
314        os.remove(test_img)
315        os.remove(backing_img)
316        try:
317            os.remove(target_backing_img)
318        except:
319            pass
320        os.remove(target_img)
321
322    def test_complete(self):
323        self.assert_no_active_block_jobs()
324
325        qemu_img('create', '-f', iotests.imgfmt,
326                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
327        self.vm.cmd('drive-mirror', device='drive0', sync='full',
328                    mode='existing', target=target_img)
329
330        self.complete_and_wait()
331        result = self.vm.qmp('query-block')
332        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
333        self.vm.shutdown()
334        self.assertTrue(iotests.compare_images(test_img, target_img),
335                        'target image does not match source after mirroring')
336
337    def test_cancel(self):
338        self.assert_no_active_block_jobs()
339
340        qemu_img('create', '-f', iotests.imgfmt,
341                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
342        self.vm.cmd('drive-mirror', device='drive0', sync='full',
343                    mode='existing', target=target_img)
344
345        self.wait_ready_and_cancel()
346        result = self.vm.qmp('query-block')
347        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
348        self.vm.shutdown()
349        self.assertTrue(iotests.compare_images(test_img, target_img),
350                        'target image does not match source after mirroring')
351
352    def test_large_cluster(self):
353        self.assert_no_active_block_jobs()
354
355        # qemu-img create fails if the image is not there
356        qemu_img('create', '-f', iotests.imgfmt, '-o', 'size=%d'
357                        %(TestMirrorNoBacking.image_len), target_backing_img)
358        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
359                        % (TestMirrorNoBacking.image_len, target_backing_img),
360                 '-F', iotests.imgfmt, target_img)
361
362        self.vm.cmd('drive-mirror', device='drive0', sync='full',
363                    mode='existing', target=target_img)
364
365        self.complete_and_wait()
366        result = self.vm.qmp('query-block')
367        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
368        self.vm.shutdown()
369        self.assertTrue(iotests.compare_images(test_img, target_img),
370                        'target image does not match source after mirroring')
371
372class TestMirrorResized(iotests.QMPTestCase):
373    backing_len = 1 * 1024 * 1024 # MB
374    image_len = 2 * 1024 * 1024 # MB
375
376    def setUp(self):
377        iotests.create_image(backing_img, TestMirrorResized.backing_len)
378        qemu_img('create', '-f', iotests.imgfmt,
379                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
380        qemu_img('resize', test_img, '2M')
381        self.vm = iotests.VM().add_drive(test_img)
382        self.vm.launch()
383
384    def tearDown(self):
385        self.vm.shutdown()
386        os.remove(test_img)
387        os.remove(backing_img)
388        try:
389            os.remove(target_img)
390        except OSError:
391            pass
392
393    def test_complete_top(self):
394        self.assert_no_active_block_jobs()
395
396        self.vm.cmd('drive-mirror', device='drive0', sync='top',
397                    target=target_img)
398
399        self.complete_and_wait()
400        result = self.vm.qmp('query-block')
401        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
402        self.vm.shutdown()
403        self.assertTrue(iotests.compare_images(test_img, target_img),
404                        'target image does not match source after mirroring')
405
406    def test_complete_full(self):
407        self.assert_no_active_block_jobs()
408
409        self.vm.cmd('drive-mirror', device='drive0', sync='full',
410                    target=target_img)
411
412        self.complete_and_wait()
413        result = self.vm.qmp('query-block')
414        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
415        self.vm.shutdown()
416        self.assertTrue(iotests.compare_images(test_img, target_img),
417                        'target image does not match source after mirroring')
418
419class TestReadErrors(iotests.QMPTestCase):
420    image_len = 2 * 1024 * 1024 # MB
421
422    # this should be a multiple of twice the default granularity
423    # so that we hit this offset first in state 1
424    MIRROR_GRANULARITY = 1024 * 1024
425
426    def create_blkdebug_file(self, name, event, errno):
427        file = open(name, 'w')
428        file.write('''
429[inject-error]
430state = "1"
431event = "%s"
432errno = "%d"
433immediately = "off"
434once = "on"
435sector = "%d"
436
437[set-state]
438state = "1"
439event = "%s"
440new_state = "2"
441
442[set-state]
443state = "2"
444event = "%s"
445new_state = "1"
446''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event))
447        file.close()
448
449    def setUp(self):
450        self.blkdebug_file = backing_img + ".blkdebug"
451        iotests.create_image(backing_img, TestReadErrors.image_len)
452        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
453        qemu_img('create', '-f', iotests.imgfmt,
454                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
455                       % (self.blkdebug_file, backing_img),
456                 test_img)
457        # Write something for tests that use sync='top'
458        qemu_io('-c', 'write %d 512' % (self.MIRROR_GRANULARITY + 65536),
459                        test_img)
460        self.vm = iotests.VM().add_drive(test_img)
461        self.vm.launch()
462
463    def tearDown(self):
464        self.vm.shutdown()
465        os.remove(test_img)
466        os.remove(target_img)
467        os.remove(backing_img)
468        os.remove(self.blkdebug_file)
469
470    def test_report_read(self):
471        self.assert_no_active_block_jobs()
472
473        self.vm.cmd('drive-mirror', device='drive0', sync='full',
474                    target=target_img)
475
476        completed = False
477        error = False
478        while not completed:
479            for event in self.vm.get_qmp_events(wait=True):
480                if event['event'] == 'BLOCK_JOB_ERROR':
481                    self.assert_qmp(event, 'data/device', 'drive0')
482                    self.assert_qmp(event, 'data/operation', 'read')
483                    error = True
484                elif event['event'] == 'BLOCK_JOB_READY':
485                    self.assertTrue(False, 'job completed unexpectedly')
486                elif event['event'] == 'BLOCK_JOB_COMPLETED':
487                    self.assertTrue(error, 'job completed unexpectedly')
488                    self.assert_qmp(event, 'data/type', 'mirror')
489                    self.assert_qmp(event, 'data/device', 'drive0')
490                    self.assert_qmp(event, 'data/error', 'Input/output error')
491                    completed = True
492                elif event['event'] == 'JOB_STATUS_CHANGE':
493                    self.assert_qmp(event, 'data/id', 'drive0')
494
495        self.assert_no_active_block_jobs()
496
497    def test_ignore_read(self):
498        self.assert_no_active_block_jobs()
499
500        self.vm.cmd('drive-mirror', device='drive0', sync='full',
501                    target=target_img, on_source_error='ignore')
502
503        event = self.vm.get_qmp_event(wait=True)
504        while event['event'] == 'JOB_STATUS_CHANGE':
505            self.assert_qmp(event, 'data/id', 'drive0')
506            event = self.vm.get_qmp_event(wait=True)
507
508        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
509        self.assert_qmp(event, 'data/device', 'drive0')
510        self.assert_qmp(event, 'data/operation', 'read')
511        result = self.vm.qmp('query-block-jobs')
512        self.assertIn(result['return'][0]['status'], ['running', 'ready'])
513        self.complete_and_wait()
514
515    def test_large_cluster(self):
516        self.assert_no_active_block_jobs()
517
518        # Test COW into the target image.  The first half of the
519        # cluster at MIRROR_GRANULARITY has to be copied from
520        # backing_img, even though sync='top'.
521        qemu_img('create', '-f', iotests.imgfmt,
522                 '-ocluster_size=131072,backing_file=%s' %(backing_img),
523                 '-F', 'raw', target_img)
524        self.vm.cmd('drive-mirror', device='drive0', sync='top',
525                    on_source_error='ignore',
526                    mode='existing', target=target_img)
527
528        event = self.vm.get_qmp_event(wait=True)
529        while event['event'] == 'JOB_STATUS_CHANGE':
530            self.assert_qmp(event, 'data/id', 'drive0')
531            event = self.vm.get_qmp_event(wait=True)
532
533        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
534        self.assert_qmp(event, 'data/device', 'drive0')
535        self.assert_qmp(event, 'data/operation', 'read')
536        result = self.vm.qmp('query-block-jobs')
537        self.assertIn(result['return'][0]['status'], ['running', 'ready'])
538        self.complete_and_wait()
539        self.vm.shutdown()
540
541        # Detach blkdebug to compare images successfully
542        qemu_img('rebase', '-f', iotests.imgfmt, '-u', '-b', backing_img,
543                 '-F', 'raw', test_img)
544        self.assertTrue(iotests.compare_images(test_img, target_img),
545                        'target image does not match source after mirroring')
546
547    def test_stop_read(self):
548        self.assert_no_active_block_jobs()
549
550        self.vm.cmd('drive-mirror', device='drive0', sync='full',
551                    target=target_img, on_source_error='stop')
552
553        error = False
554        ready = False
555        while not ready:
556            for event in self.vm.get_qmp_events(wait=True):
557                if event['event'] == 'BLOCK_JOB_ERROR':
558                    self.assert_qmp(event, 'data/device', 'drive0')
559                    self.assert_qmp(event, 'data/operation', 'read')
560
561                    if self.vm.qmp('query-block-jobs')['return'][0]['status'] != 'paused':
562                        self.vm.events_wait([(
563                            'JOB_STATUS_CHANGE',
564                            {'data': {'id': 'drive0', 'status': 'paused'}}
565                        )])
566
567                    result = self.vm.qmp('query-block-jobs')
568                    self.assert_qmp(result, 'return[0]/status', 'paused')
569                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
570
571                    self.vm.cmd('block-job-resume', device='drive0')
572                    error = True
573                elif event['event'] == 'BLOCK_JOB_READY':
574                    self.assertTrue(error, 'job completed unexpectedly')
575                    self.assert_qmp(event, 'data/device', 'drive0')
576                    ready = True
577
578        result = self.vm.qmp('query-block-jobs')
579        self.assert_qmp(result, 'return[0]/status', 'ready')
580        self.assert_qmp(result, 'return[0]/io-status', 'ok')
581
582        self.complete_and_wait(wait_ready=False)
583        self.assert_no_active_block_jobs()
584
585class TestWriteErrors(iotests.QMPTestCase):
586    image_len = 2 * 1024 * 1024 # MB
587
588    # this should be a multiple of twice the default granularity
589    # so that we hit this offset first in state 1
590    MIRROR_GRANULARITY = 1024 * 1024
591
592    def create_blkdebug_file(self, name, event, errno):
593        file = open(name, 'w')
594        file.write('''
595[inject-error]
596state = "1"
597event = "%s"
598errno = "%d"
599immediately = "off"
600once = "on"
601sector = "%d"
602
603[set-state]
604state = "1"
605event = "%s"
606new_state = "2"
607
608[set-state]
609state = "2"
610event = "%s"
611new_state = "1"
612''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event))
613        file.close()
614
615    def setUp(self):
616        self.blkdebug_file = target_img + ".blkdebug"
617        iotests.create_image(backing_img, TestWriteErrors.image_len)
618        self.create_blkdebug_file(self.blkdebug_file, "write_aio", 5)
619        qemu_img('create', '-f', iotests.imgfmt,
620                 '-obacking_file=%s' %(backing_img), '-F', 'raw', test_img)
621        self.vm = iotests.VM().add_drive(test_img)
622        self.target_img = 'blkdebug:%s:%s' % (self.blkdebug_file, target_img)
623        qemu_img('create', '-f', iotests.imgfmt, '-osize=%d' %(TestWriteErrors.image_len), target_img)
624        self.vm.launch()
625
626    def tearDown(self):
627        self.vm.shutdown()
628        os.remove(test_img)
629        os.remove(target_img)
630        os.remove(backing_img)
631        os.remove(self.blkdebug_file)
632
633    def test_report_write(self):
634        self.assert_no_active_block_jobs()
635
636        self.vm.cmd('drive-mirror', device='drive0', sync='full',
637                    mode='existing', target=self.target_img)
638
639        completed = False
640        error = False
641        while not completed:
642            for event in self.vm.get_qmp_events(wait=True):
643                if event['event'] == 'BLOCK_JOB_ERROR':
644                    self.assert_qmp(event, 'data/device', 'drive0')
645                    self.assert_qmp(event, 'data/operation', 'write')
646                    error = True
647                elif event['event'] == 'BLOCK_JOB_READY':
648                    self.assertTrue(False, 'job completed unexpectedly')
649                elif event['event'] == 'BLOCK_JOB_COMPLETED':
650                    self.assertTrue(error, 'job completed unexpectedly')
651                    self.assert_qmp(event, 'data/type', 'mirror')
652                    self.assert_qmp(event, 'data/device', 'drive0')
653                    self.assert_qmp(event, 'data/error', 'Input/output error')
654                    completed = True
655
656        self.assert_no_active_block_jobs()
657
658    def test_ignore_write(self):
659        self.assert_no_active_block_jobs()
660
661        self.vm.cmd('drive-mirror', device='drive0', sync='full',
662                    mode='existing', target=self.target_img,
663                    on_target_error='ignore')
664
665        event = self.vm.event_wait(name='BLOCK_JOB_ERROR')
666        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
667        self.assert_qmp(event, 'data/device', 'drive0')
668        self.assert_qmp(event, 'data/operation', 'write')
669        result = self.vm.qmp('query-block-jobs')
670        self.assertIn(result['return'][0]['status'], ['running', 'ready'])
671        self.complete_and_wait()
672
673    def test_stop_write(self):
674        self.assert_no_active_block_jobs()
675
676        self.vm.cmd('drive-mirror', device='drive0', sync='full',
677                    mode='existing', target=self.target_img,
678                    on_target_error='stop')
679
680        error = False
681        ready = False
682        while not ready:
683            for event in self.vm.get_qmp_events(wait=True):
684                if event['event'] == 'BLOCK_JOB_ERROR':
685                    self.assert_qmp(event, 'data/device', 'drive0')
686                    self.assert_qmp(event, 'data/operation', 'write')
687
688                    if self.vm.qmp('query-block-jobs')['return'][0]['status'] != 'paused':
689                        self.vm.events_wait([(
690                            'JOB_STATUS_CHANGE',
691                            {'data': {'id': 'drive0', 'status': 'paused'}}
692                        )])
693
694                    result = self.vm.qmp('query-block-jobs')
695                    self.assert_qmp(result, 'return[0]/status', 'paused')
696                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
697
698                    self.vm.cmd('block-job-resume', device='drive0')
699
700                    result = self.vm.qmp('query-block-jobs')
701                    self.assertIn(result['return'][0]['status'], ['running', 'ready'])
702                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
703                    error = True
704                elif event['event'] == 'BLOCK_JOB_READY':
705                    self.assertTrue(error, 'job completed unexpectedly')
706                    self.assert_qmp(event, 'data/device', 'drive0')
707                    ready = True
708
709        self.complete_and_wait(wait_ready=False)
710        self.assert_no_active_block_jobs()
711
712class TestSetSpeed(iotests.QMPTestCase):
713    image_len = 80 * 1024 * 1024 # MB
714
715    def setUp(self):
716        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
717        qemu_img('create', '-f', iotests.imgfmt,
718                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
719        self.vm = iotests.VM().add_drive(test_img)
720        self.vm.launch()
721
722    def tearDown(self):
723        self.vm.shutdown()
724        os.remove(test_img)
725        os.remove(backing_img)
726        os.remove(target_img)
727
728    def test_set_speed(self):
729        self.assert_no_active_block_jobs()
730
731        self.vm.cmd('drive-mirror', device='drive0', sync='full',
732                    target=target_img)
733
734        # Default speed is 0
735        result = self.vm.qmp('query-block-jobs')
736        self.assert_qmp(result, 'return[0]/device', 'drive0')
737        self.assert_qmp(result, 'return[0]/speed', 0)
738
739        self.vm.cmd('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
740
741        # Ensure the speed we set was accepted
742        result = self.vm.qmp('query-block-jobs')
743        self.assert_qmp(result, 'return[0]/device', 'drive0')
744        self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
745
746        self.wait_ready_and_cancel()
747
748        # Check setting speed in drive-mirror works
749        self.vm.cmd('drive-mirror', device='drive0', sync='full',
750                    target=target_img, speed=4*1024*1024)
751
752        result = self.vm.qmp('query-block-jobs')
753        self.assert_qmp(result, 'return[0]/device', 'drive0')
754        self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
755
756        self.wait_ready_and_cancel()
757
758    def test_set_speed_invalid(self):
759        self.assert_no_active_block_jobs()
760
761        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
762                             target=target_img, speed=-1)
763        self.assert_qmp(result, 'error/class', 'GenericError')
764
765        self.assert_no_active_block_jobs()
766
767        self.vm.cmd('drive-mirror', device='drive0', sync='full',
768                    target=target_img)
769
770        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
771        self.assert_qmp(result, 'error/class', 'GenericError')
772
773        self.wait_ready_and_cancel()
774
775class TestUnbackedSource(iotests.QMPTestCase):
776    image_len = 2 * 1024 * 1024 # MB
777
778    def setUp(self):
779        qemu_img('create', '-f', iotests.imgfmt, test_img,
780                 str(TestUnbackedSource.image_len))
781        self.vm = iotests.VM()
782        self.vm.launch()
783        self.vm.cmd('blockdev-add', node_name='drive0',
784                    driver=iotests.imgfmt,
785                    file={
786                        'driver': 'file',
787                        'filename': test_img,
788                    })
789
790    def tearDown(self):
791        self.vm.shutdown()
792        os.remove(test_img)
793        os.remove(target_img)
794
795    def test_absolute_paths_full(self):
796        self.assert_no_active_block_jobs()
797        self.vm.cmd('drive-mirror', job_id='drive0', device='drive0',
798                    sync='full', target=target_img,
799                    mode='absolute-paths')
800        self.complete_and_wait()
801        self.assert_no_active_block_jobs()
802
803    def test_absolute_paths_top(self):
804        self.assert_no_active_block_jobs()
805        self.vm.cmd('drive-mirror', job_id='drive0', device='drive0',
806                    sync='top', target=target_img,
807                    mode='absolute-paths')
808        self.complete_and_wait()
809        self.assert_no_active_block_jobs()
810
811    def test_absolute_paths_none(self):
812        self.assert_no_active_block_jobs()
813        self.vm.cmd('drive-mirror', job_id='drive0', device='drive0',
814                    sync='none', target=target_img,
815                    mode='absolute-paths')
816        self.complete_and_wait()
817        self.assert_no_active_block_jobs()
818
819    def test_existing_full(self):
820        qemu_img('create', '-f', iotests.imgfmt, target_img,
821                 str(self.image_len))
822        qemu_io('-c', 'write -P 42 0 64k', target_img)
823
824        self.assert_no_active_block_jobs()
825        self.vm.cmd('drive-mirror', job_id='drive0', device='drive0',
826                    sync='full', target=target_img, mode='existing')
827        self.complete_and_wait()
828        self.assert_no_active_block_jobs()
829
830        self.vm.cmd('blockdev-del', node_name='drive0')
831
832        self.assertTrue(iotests.compare_images(test_img, target_img),
833                        'target image does not match source after mirroring')
834
835    def test_blockdev_full(self):
836        qemu_img('create', '-f', iotests.imgfmt, target_img,
837                 str(self.image_len))
838        qemu_io('-c', 'write -P 42 0 64k', target_img)
839
840        self.vm.cmd('blockdev-add', node_name='target',
841                    driver=iotests.imgfmt,
842                    file={
843                        'driver': 'file',
844                        'filename': target_img,
845                    })
846
847        self.assert_no_active_block_jobs()
848        self.vm.cmd('blockdev-mirror', job_id='drive0', device='drive0',
849                    sync='full', target='target')
850        self.complete_and_wait()
851        self.assert_no_active_block_jobs()
852
853        self.vm.cmd('blockdev-del', node_name='drive0')
854
855        self.vm.cmd('blockdev-del', node_name='target')
856
857        self.assertTrue(iotests.compare_images(test_img, target_img),
858                        'target image does not match source after mirroring')
859
860class TestGranularity(iotests.QMPTestCase):
861    image_len = 10 * 1024 * 1024 # MB
862
863    def setUp(self):
864        qemu_img('create', '-f', iotests.imgfmt, test_img,
865                 str(TestGranularity.image_len))
866        qemu_io('-c', 'write 0 %d' % (self.image_len),
867                test_img)
868        self.vm = iotests.VM().add_drive(test_img)
869        self.vm.launch()
870
871    def tearDown(self):
872        self.vm.shutdown()
873        self.assertTrue(iotests.compare_images(test_img, target_img),
874                        'target image does not match source after mirroring')
875        os.remove(test_img)
876        os.remove(target_img)
877
878    def test_granularity(self):
879        self.assert_no_active_block_jobs()
880        self.vm.cmd('drive-mirror', device='drive0',
881                    sync='full', target=target_img,
882                    mode='absolute-paths', granularity=8192)
883
884        event = self.vm.get_qmp_event(wait=60.0)
885        while event['event'] == 'JOB_STATUS_CHANGE':
886            self.assert_qmp(event, 'data/id', 'drive0')
887            event = self.vm.get_qmp_event(wait=60.0)
888
889        # Failures will manifest as COMPLETED/ERROR.
890        self.assert_qmp(event, 'event', 'BLOCK_JOB_READY')
891        self.complete_and_wait(drive='drive0', wait_ready=False)
892        self.assert_no_active_block_jobs()
893
894class TestRepairQuorum(iotests.QMPTestCase):
895    """ This class test quorum file repair using drive-mirror.
896        It's mostly a fork of TestSingleDrive """
897    image_len = 1 * 1024 * 1024 # MB
898    IMAGES = [ quorum_img1, quorum_img2, quorum_img3 ]
899
900    @iotests.skip_if_unsupported(['quorum'])
901    def setUp(self):
902        self.vm = iotests.VM()
903
904        if iotests.qemu_default_machine == 'pc':
905            self.vm.add_drive(None, 'media=cdrom', 'ide')
906
907        # Add each individual quorum images
908        for i in self.IMAGES:
909            qemu_img('create', '-f', iotests.imgfmt, i,
910                     str(self.image_len))
911            # Assign a node name to each quorum image in order to manipulate
912            # them
913            opts = "node-name=img%i" % self.IMAGES.index(i)
914            opts += ',driver=%s' % iotests.imgfmt
915            opts += ',file.driver=file'
916            opts += ',file.filename=%s' % i
917            self.vm = self.vm.add_blockdev(opts)
918
919        self.vm.launch()
920
921        #assemble the quorum block device from the individual files
922        args = { "driver": "quorum", "node-name": "quorum0",
923                 "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] }
924        self.vm.cmd("blockdev-add", args)
925
926
927    def tearDown(self):
928        self.vm.shutdown()
929        for i in self.IMAGES + [ quorum_repair_img, quorum_snapshot_file,
930                                 nbd_sock_path ]:
931            # Do a try/except because the test may have deleted some images
932            try:
933                os.remove(i)
934            except OSError:
935                pass
936
937    def test_complete(self):
938        self.vm.cmd('drive-mirror', job_id='job0', device='quorum0',
939                    sync='full', node_name="repair0", replaces="img1",
940                    target=quorum_repair_img, format=iotests.imgfmt)
941
942        self.complete_and_wait(drive="job0")
943        self.assert_has_block_node("repair0", quorum_repair_img)
944        self.vm.assert_block_path('quorum0', '/children.1', 'repair0')
945        self.vm.shutdown()
946        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
947                        'target image does not match source after mirroring')
948
949    def test_cancel(self):
950        self.vm.cmd('drive-mirror', job_id='job0', device='quorum0',
951                    sync='full', node_name="repair0", replaces="img1",
952                    target=quorum_repair_img, format=iotests.imgfmt)
953
954        self.cancel_and_wait(drive="job0", force=True)
955        # here we check that the last registered quorum file has not been
956        # swapped out and unref
957        self.assert_has_block_node(None, quorum_img3)
958
959    def test_cancel_after_ready(self):
960        self.vm.cmd('drive-mirror', job_id='job0', device='quorum0',
961                    sync='full', node_name="repair0", replaces="img1",
962                    target=quorum_repair_img, format=iotests.imgfmt)
963
964        self.wait_ready_and_cancel(drive="job0")
965        # here we check that the last registered quorum file has not been
966        # swapped out and unref
967        self.assert_has_block_node(None, quorum_img3)
968        self.vm.shutdown()
969        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
970                        'target image does not match source after mirroring')
971
972    def test_pause(self):
973        self.vm.cmd('drive-mirror', job_id='job0', device='quorum0',
974                    sync='full', node_name="repair0", replaces="img1",
975                    target=quorum_repair_img, format=iotests.imgfmt)
976
977        self.pause_job('job0')
978
979        result = self.vm.qmp('query-block-jobs')
980        offset = self.dictpath(result, 'return[0]/offset')
981
982        time.sleep(0.5)
983        result = self.vm.qmp('query-block-jobs')
984        self.assert_qmp(result, 'return[0]/offset', offset)
985
986        self.vm.cmd('block-job-resume', device='job0')
987
988        self.complete_and_wait(drive="job0")
989        self.vm.shutdown()
990        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
991                        'target image does not match source after mirroring')
992
993    def test_medium_not_found(self):
994        if iotests.qemu_default_machine != 'pc':
995            return
996
997        result = self.vm.qmp('drive-mirror', job_id='job0', device='drive0', # CD-ROM
998                             sync='full',
999                             node_name='repair0',
1000                             replaces='img1',
1001                             target=quorum_repair_img, format=iotests.imgfmt)
1002        self.assert_qmp(result, 'error/class', 'GenericError')
1003
1004    def test_image_not_found(self):
1005        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1006                             sync='full', node_name='repair0', replaces='img1',
1007                             mode='existing', target=quorum_repair_img,
1008                             format=iotests.imgfmt)
1009        self.assert_qmp(result, 'error/class', 'GenericError')
1010
1011    def test_device_not_found(self):
1012        result = self.vm.qmp('drive-mirror', job_id='job0',
1013                             device='nonexistent', sync='full',
1014                             node_name='repair0',
1015                             replaces='img1',
1016                             target=quorum_repair_img, format=iotests.imgfmt)
1017        self.assert_qmp(result, 'error/class', 'GenericError')
1018
1019    def test_wrong_sync_mode(self):
1020        result = self.vm.qmp('drive-mirror', device='quorum0', job_id='job0',
1021                             node_name='repair0',
1022                             replaces='img1',
1023                             target=quorum_repair_img, format=iotests.imgfmt)
1024        self.assert_qmp(result, 'error/class', 'GenericError')
1025
1026    def test_no_node_name(self):
1027        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1028                             sync='full', replaces='img1',
1029                             target=quorum_repair_img, format=iotests.imgfmt)
1030        self.assert_qmp(result, 'error/class', 'GenericError')
1031
1032    def test_nonexistent_replaces(self):
1033        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1034                             sync='full', node_name='repair0', replaces='img77',
1035                             target=quorum_repair_img, format=iotests.imgfmt)
1036        self.assert_qmp(result, 'error/class', 'GenericError')
1037
1038    def test_after_a_quorum_snapshot(self):
1039        self.vm.cmd('blockdev-snapshot-sync', node_name='img1',
1040                    snapshot_file=quorum_snapshot_file,
1041                    snapshot_node_name="snap1")
1042
1043        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1044                             sync='full', node_name='repair0', replaces="img1",
1045                             target=quorum_repair_img, format=iotests.imgfmt)
1046        self.assert_qmp(result, 'error/class', 'GenericError')
1047
1048        self.vm.cmd('drive-mirror', job_id='job0', device='quorum0',
1049                    sync='full', node_name='repair0', replaces="snap1",
1050                    target=quorum_repair_img, format=iotests.imgfmt)
1051
1052        self.complete_and_wait('job0')
1053        self.assert_has_block_node("repair0", quorum_repair_img)
1054        self.vm.assert_block_path('quorum0', '/children.1', 'repair0')
1055
1056    def test_with_other_parent(self):
1057        """
1058        Check that we cannot replace a Quorum child when it has other
1059        parents.
1060        """
1061        self.vm.cmd('nbd-server-start',
1062                    addr={
1063                        'type': 'unix',
1064                        'data': {'path': nbd_sock_path}
1065                    })
1066
1067        self.vm.cmd('nbd-server-add', device='img1')
1068
1069        result = self.vm.qmp('drive-mirror', job_id='mirror', device='quorum0',
1070                             sync='full', node_name='repair0', replaces='img1',
1071                             target=quorum_repair_img, format=iotests.imgfmt)
1072        self.assert_qmp(result, 'error/desc',
1073                        "Cannot replace 'img1' by a node mirrored from "
1074                        "'quorum0', because it cannot be guaranteed that doing "
1075                        "so would not lead to an abrupt change of visible data")
1076
1077    def test_with_other_parents_after_mirror_start(self):
1078        """
1079        The same as test_with_other_parent(), but add the NBD server
1080        only when the mirror job is already running.
1081        """
1082        self.vm.cmd('nbd-server-start',
1083                    addr={
1084                        'type': 'unix',
1085                        'data': {'path': nbd_sock_path}
1086                    })
1087
1088        self.vm.cmd('drive-mirror', job_id='mirror', device='quorum0',
1089                    sync='full', node_name='repair0', replaces='img1',
1090                    target=quorum_repair_img, format=iotests.imgfmt)
1091
1092        self.vm.cmd('nbd-server-add', device='img1')
1093
1094        # The full error message goes to stderr, we will check it later
1095        self.complete_and_wait('mirror',
1096                               completion_error='Operation not permitted')
1097
1098        # Should not have been replaced
1099        self.vm.assert_block_path('quorum0', '/children.1', 'img1')
1100
1101        # Check the full error message now
1102        self.vm.shutdown()
1103        log = self.vm.get_log()
1104        log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log)
1105        log = re.sub(r'^Formatting.*\n', '', log)
1106        log = re.sub(r'\n\[I \+\d+\.\d+\] CLOSED\n?$', '', log)
1107        log = re.sub(r'^%s: ' % os.path.basename(iotests.qemu_prog), '', log)
1108
1109        self.assertEqual(log,
1110                         "Can no longer replace 'img1' by 'repair0', because " +
1111                         "it can no longer be guaranteed that doing so would " +
1112                         "not lead to an abrupt change of visible data")
1113
1114
1115# Test mirroring with a source that does not have any parents (not even a
1116# BlockBackend)
1117class TestOrphanedSource(iotests.QMPTestCase):
1118    def setUp(self):
1119        blk0 = { 'node-name': 'src',
1120                 'driver': 'null-co' }
1121
1122        blk1 = { 'node-name': 'dest',
1123                 'driver': 'null-co' }
1124
1125        blk2 = { 'node-name': 'dest-ro',
1126                 'driver': 'null-co',
1127                 'read-only': 'on' }
1128
1129        self.vm = iotests.VM()
1130        self.vm.add_blockdev(self.vm.qmp_to_opts(blk0))
1131        self.vm.add_blockdev(self.vm.qmp_to_opts(blk1))
1132        self.vm.add_blockdev(self.vm.qmp_to_opts(blk2))
1133        self.vm.launch()
1134
1135    def tearDown(self):
1136        self.vm.shutdown()
1137
1138    def test_no_job_id(self):
1139        self.assert_no_active_block_jobs()
1140
1141        result = self.vm.qmp('blockdev-mirror', device='src', sync='full',
1142                             target='dest')
1143        self.assert_qmp(result, 'error/class', 'GenericError')
1144
1145    def test_success(self):
1146        self.assert_no_active_block_jobs()
1147
1148        self.vm.cmd('blockdev-mirror', job_id='job', device='src',
1149                    sync='full', target='dest')
1150
1151        self.complete_and_wait('job')
1152
1153    def test_failing_permissions(self):
1154        self.assert_no_active_block_jobs()
1155
1156        result = self.vm.qmp('blockdev-mirror', device='src', sync='full',
1157                             target='dest-ro')
1158        self.assert_qmp(result, 'error/class', 'GenericError')
1159
1160    def test_failing_permission_in_complete(self):
1161        self.assert_no_active_block_jobs()
1162
1163        # Unshare consistent-read on the target
1164        # (The mirror job does not care)
1165        self.vm.cmd('blockdev-add',
1166                    driver='blkdebug',
1167                    node_name='dest-perm',
1168                    image='dest',
1169                    unshare_child_perms=['consistent-read'])
1170
1171        self.vm.cmd('blockdev-mirror', job_id='job', device='src',
1172                    sync='full', target='dest',
1173                    filter_node_name='mirror-filter')
1174
1175        # Require consistent-read on the source
1176        # (We can only add this node once the job has started, or it
1177        # will complain that it does not want to run on non-root nodes)
1178        self.vm.cmd('blockdev-add',
1179                    driver='blkdebug',
1180                    node_name='src-perm',
1181                    image='src',
1182                    take_child_perms=['consistent-read'])
1183
1184        # While completing, mirror will attempt to replace src by
1185        # dest, which must fail because src-perm requires
1186        # consistent-read but dest-perm does not share it; thus
1187        # aborting the job when it is supposed to complete
1188        self.complete_and_wait('job',
1189                               completion_error='Operation not permitted')
1190
1191        # Assert that all of our nodes are still there (except for the
1192        # mirror filter, which should be gone despite the failure)
1193        nodes = self.vm.qmp('query-named-block-nodes')['return']
1194        nodes = [node['node-name'] for node in nodes]
1195
1196        for expect in ('src', 'src-perm', 'dest', 'dest-perm'):
1197            self.assertTrue(expect in nodes, '%s disappeared' % expect)
1198        self.assertFalse('mirror-filter' in nodes,
1199                         'Mirror filter node did not disappear')
1200
1201# Test cases for @replaces that do not necessarily involve Quorum
1202class TestReplaces(iotests.QMPTestCase):
1203    # Each of these test cases needs their own block graph, so do not
1204    # create any nodes here
1205    def setUp(self):
1206        self.vm = iotests.VM()
1207        self.vm.launch()
1208
1209    def tearDown(self):
1210        self.vm.shutdown()
1211        for img in (test_img, target_img):
1212            try:
1213                os.remove(img)
1214            except OSError:
1215                pass
1216
1217    @iotests.skip_if_unsupported(['copy-on-read'])
1218    def test_replace_filter(self):
1219        """
1220        Check that we can replace filter nodes.
1221        """
1222        self.vm.cmd('blockdev-add', {
1223                        'driver': 'copy-on-read',
1224                        'node-name': 'filter0',
1225                        'file': {
1226                            'driver': 'copy-on-read',
1227                            'node-name': 'filter1',
1228                            'file': {
1229                                'driver': 'null-co'
1230                            }
1231                        }
1232                    })
1233
1234        self.vm.cmd('blockdev-add',
1235                    node_name='target', driver='null-co')
1236
1237        self.vm.cmd('blockdev-mirror', job_id='mirror', device='filter0',
1238                    target='target', sync='full', replaces='filter1')
1239
1240        self.complete_and_wait('mirror')
1241
1242        self.vm.assert_block_path('filter0', '/file', 'target')
1243
1244# Tests for mirror with filters (and how the mirror filter behaves, as
1245# an example for an implicit filter)
1246class TestFilters(iotests.QMPTestCase):
1247    def setUp(self):
1248        qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M')
1249        qemu_img('create', '-f', iotests.imgfmt, '-b', backing_img,
1250                 '-F', iotests.imgfmt, test_img)
1251        qemu_img('create', '-f', iotests.imgfmt, '-b', backing_img,
1252                 '-F', iotests.imgfmt, target_img)
1253
1254        qemu_io('-c', 'write -P 1 0 512k', backing_img)
1255        qemu_io('-c', 'write -P 2 512k 512k', test_img)
1256
1257        self.vm = iotests.VM().add_device('virtio-scsi,id=vio-scsi')
1258        self.vm.launch()
1259
1260        self.vm.cmd('blockdev-add', {
1261                        'node-name': 'target',
1262                        'driver': iotests.imgfmt,
1263                        'file': {
1264                            'driver': 'file',
1265                            'filename': target_img
1266                        },
1267                        'backing': None
1268                    })
1269
1270        self.filterless_chain = {
1271                'node-name': 'source',
1272                'driver': iotests.imgfmt,
1273                'file': {
1274                    'driver': 'file',
1275                    'filename': test_img
1276                },
1277                'backing': {
1278                    'node-name': 'backing',
1279                    'driver': iotests.imgfmt,
1280                    'file': {
1281                        'driver': 'file',
1282                        'filename': backing_img
1283                    }
1284                }
1285            }
1286
1287    def tearDown(self):
1288        self.vm.shutdown()
1289
1290        os.remove(test_img)
1291        os.remove(target_img)
1292        os.remove(backing_img)
1293
1294    def test_cor(self):
1295        self.vm.cmd('blockdev-add', {
1296                        'node-name': 'filter',
1297                        'driver': 'copy-on-read',
1298                        'file': self.filterless_chain
1299                    })
1300
1301        self.vm.cmd('blockdev-mirror',
1302                    job_id='mirror',
1303                    device='filter',
1304                    target='target',
1305                    sync='top')
1306
1307        self.complete_and_wait('mirror')
1308
1309        self.vm.qmp('blockdev-del', node_name='target')
1310
1311        target_map = qemu_img_map(target_img)
1312
1313        assert target_map[0]['start'] == 0
1314        assert target_map[0]['length'] == 512 * 1024
1315        assert target_map[0]['depth'] == 1
1316
1317        assert target_map[1]['start'] == 512 * 1024
1318        assert target_map[1]['length'] == 512 * 1024
1319        assert target_map[1]['depth'] == 0
1320
1321    def test_implicit_mirror_filter(self):
1322        self.vm.cmd('blockdev-add', self.filterless_chain)
1323
1324        # We need this so we can query from above the mirror node
1325        self.vm.cmd('device_add',
1326                    driver='scsi-hd',
1327                    id='virtio',
1328                    bus='vio-scsi.0',
1329                    drive='source')
1330
1331        self.vm.cmd('blockdev-mirror',
1332                    job_id='mirror',
1333                    device='source',
1334                    target='target',
1335                    sync='top')
1336
1337        # The mirror filter is now an implicit node, so it should be
1338        # invisible when querying the backing chain
1339        blockdevs = self.vm.qmp('query-block')['return']
1340        device_info = next(dev for dev in blockdevs if dev['qdev'] == 'virtio')
1341
1342        assert device_info['inserted']['node-name'] == 'source'
1343
1344        image_info = device_info['inserted']['image']
1345        assert image_info['filename'] == test_img
1346        assert image_info['backing-image']['filename'] == backing_img
1347
1348        self.complete_and_wait('mirror')
1349
1350    def test_explicit_mirror_filter(self):
1351        # Same test as above, but this time we give the mirror filter
1352        # a node-name so it will not be invisible
1353        self.vm.cmd('blockdev-add', self.filterless_chain)
1354
1355        # We need this so we can query from above the mirror node
1356        self.vm.cmd('device_add',
1357                    driver='scsi-hd',
1358                    id='virtio',
1359                    bus='vio-scsi.0',
1360                    drive='source')
1361
1362        self.vm.cmd('blockdev-mirror',
1363                    job_id='mirror',
1364                    device='source',
1365                    target='target',
1366                    sync='top',
1367                    filter_node_name='mirror-filter')
1368
1369        # With a node-name given to it, the mirror filter should now
1370        # be visible
1371        blockdevs = self.vm.qmp('query-block')['return']
1372        device_info = next(dev for dev in blockdevs if dev['qdev'] == 'virtio')
1373
1374        assert device_info['inserted']['node-name'] == 'mirror-filter'
1375
1376        self.complete_and_wait('mirror')
1377
1378
1379if __name__ == '__main__':
1380    iotests.main(supported_fmts=['qcow2', 'qed'],
1381                 supported_protocols=['file'],
1382                 supported_platforms=['linux', 'freebsd', 'netbsd', 'openbsd'])
1383