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