xref: /openbmc/qemu/tests/qemu-iotests/041 (revision 5f71c6b004c7c4509297f4426f0bbc836cac106f)
1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3
244c7ca5eSPaolo Bonzini#
344c7ca5eSPaolo Bonzini# Tests for image mirroring.
444c7ca5eSPaolo Bonzini#
544c7ca5eSPaolo Bonzini# Copyright (C) 2012 Red Hat, Inc.
644c7ca5eSPaolo Bonzini#
744c7ca5eSPaolo Bonzini# This program is free software; you can redistribute it and/or modify
844c7ca5eSPaolo Bonzini# it under the terms of the GNU General Public License as published by
944c7ca5eSPaolo Bonzini# the Free Software Foundation; either version 2 of the License, or
1044c7ca5eSPaolo Bonzini# (at your option) any later version.
1144c7ca5eSPaolo Bonzini#
1244c7ca5eSPaolo Bonzini# This program is distributed in the hope that it will be useful,
1344c7ca5eSPaolo Bonzini# but WITHOUT ANY WARRANTY; without even the implied warranty of
1444c7ca5eSPaolo Bonzini# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1544c7ca5eSPaolo Bonzini# GNU General Public License for more details.
1644c7ca5eSPaolo Bonzini#
1744c7ca5eSPaolo Bonzini# You should have received a copy of the GNU General Public License
1844c7ca5eSPaolo Bonzini# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1944c7ca5eSPaolo Bonzini#
2044c7ca5eSPaolo Bonzini
2144c7ca5eSPaolo Bonziniimport time
2244c7ca5eSPaolo Bonziniimport os
23a1da1878SMax Reitzimport re
24*5f71c6b0SMax Reitzimport json
2544c7ca5eSPaolo Bonziniimport iotests
26*5f71c6b0SMax Reitzfrom iotests import qemu_img, qemu_img_pipe, qemu_io
2744c7ca5eSPaolo Bonzini
2844c7ca5eSPaolo Bonzinibacking_img = os.path.join(iotests.test_dir, 'backing.img')
2944c7ca5eSPaolo Bonzinitarget_backing_img = os.path.join(iotests.test_dir, 'target-backing.img')
3044c7ca5eSPaolo Bonzinitest_img = os.path.join(iotests.test_dir, 'test.img')
3144c7ca5eSPaolo Bonzinitarget_img = os.path.join(iotests.test_dir, 'target.img')
3244c7ca5eSPaolo Bonzini
33d88964aeSBenoît Canetquorum_img1 = os.path.join(iotests.test_dir, 'quorum1.img')
34d88964aeSBenoît Canetquorum_img2 = os.path.join(iotests.test_dir, 'quorum2.img')
35d88964aeSBenoît Canetquorum_img3 = os.path.join(iotests.test_dir, 'quorum3.img')
36d88964aeSBenoît Canetquorum_repair_img = os.path.join(iotests.test_dir, 'quorum_repair.img')
37d88964aeSBenoît Canetquorum_snapshot_file = os.path.join(iotests.test_dir, 'quorum_snapshot.img')
38d88964aeSBenoît Canet
39e5ac52d8SMax Reitznbd_sock_path = os.path.join(iotests.sock_dir, 'nbd.sock')
40a1da1878SMax Reitz
41866323f3SFam Zhengclass TestSingleDrive(iotests.QMPTestCase):
4244c7ca5eSPaolo Bonzini    image_len = 1 * 1024 * 1024 # MB
4394ca2c73SFam Zheng    qmp_cmd = 'drive-mirror'
4494ca2c73SFam Zheng    qmp_target = target_img
4544c7ca5eSPaolo Bonzini
4644c7ca5eSPaolo Bonzini    def setUp(self):
473b9f27d2SFam Zheng        iotests.create_image(backing_img, self.image_len)
48b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
49b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
50d3c8c674SKevin Wolf        self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=base")
510ed82f7aSMax Reitz        if iotests.qemu_default_machine == 'pc':
520ed82f7aSMax Reitz            self.vm.add_drive(None, 'media=cdrom', 'ide')
5344c7ca5eSPaolo Bonzini        self.vm.launch()
5444c7ca5eSPaolo Bonzini
5544c7ca5eSPaolo Bonzini    def tearDown(self):
5644c7ca5eSPaolo Bonzini        self.vm.shutdown()
5744c7ca5eSPaolo Bonzini        os.remove(test_img)
5844c7ca5eSPaolo Bonzini        os.remove(backing_img)
5944c7ca5eSPaolo Bonzini        try:
6044c7ca5eSPaolo Bonzini            os.remove(target_img)
6144c7ca5eSPaolo Bonzini        except OSError:
6244c7ca5eSPaolo Bonzini            pass
6344c7ca5eSPaolo Bonzini
6444c7ca5eSPaolo Bonzini    def test_complete(self):
65ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6644c7ca5eSPaolo Bonzini
6794ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
6894ca2c73SFam Zheng                             target=self.qmp_target)
6944c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
7044c7ca5eSPaolo Bonzini
7144c7ca5eSPaolo Bonzini        self.complete_and_wait()
7244c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
7344c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
7444c7ca5eSPaolo Bonzini        self.vm.shutdown()
753a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
7644c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
7744c7ca5eSPaolo Bonzini
7844c7ca5eSPaolo Bonzini    def test_cancel(self):
79ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
8044c7ca5eSPaolo Bonzini
8194ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
8294ca2c73SFam Zheng                             target=self.qmp_target)
8344c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
8444c7ca5eSPaolo Bonzini
852575fe16SStefan Hajnoczi        self.cancel_and_wait(force=True)
8644c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
8744c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
8844c7ca5eSPaolo Bonzini
8944c7ca5eSPaolo Bonzini    def test_cancel_after_ready(self):
90ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
9144c7ca5eSPaolo Bonzini
9294ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
9394ca2c73SFam Zheng                             target=self.qmp_target)
9444c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
9544c7ca5eSPaolo Bonzini
962575fe16SStefan Hajnoczi        self.wait_ready_and_cancel()
9744c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
9844c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
9944c7ca5eSPaolo Bonzini        self.vm.shutdown()
1003a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
10144c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
10244c7ca5eSPaolo Bonzini
10344c7ca5eSPaolo Bonzini    def test_pause(self):
104ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
10544c7ca5eSPaolo Bonzini
10694ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
10794ca2c73SFam Zheng                             target=self.qmp_target)
10844c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
10944c7ca5eSPaolo Bonzini
1102c93c5cbSKevin Wolf        self.pause_job('drive0')
11144c7ca5eSPaolo Bonzini
11244c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
11344c7ca5eSPaolo Bonzini        offset = self.dictpath(result, 'return[0]/offset')
11444c7ca5eSPaolo Bonzini
1152c93c5cbSKevin Wolf        time.sleep(0.5)
11644c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
11744c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/offset', offset)
11844c7ca5eSPaolo Bonzini
11944c7ca5eSPaolo Bonzini        result = self.vm.qmp('block-job-resume', device='drive0')
12044c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
12144c7ca5eSPaolo Bonzini
12244c7ca5eSPaolo Bonzini        self.complete_and_wait()
12344c7ca5eSPaolo Bonzini        self.vm.shutdown()
1243a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
12544c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
12644c7ca5eSPaolo Bonzini
12708e4ed6cSPaolo Bonzini    def test_small_buffer(self):
128ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
12908e4ed6cSPaolo Bonzini
13008e4ed6cSPaolo Bonzini        # A small buffer is rounded up automatically
13194ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
13294ca2c73SFam Zheng                             buf_size=4096, target=self.qmp_target)
13308e4ed6cSPaolo Bonzini        self.assert_qmp(result, 'return', {})
13408e4ed6cSPaolo Bonzini
13508e4ed6cSPaolo Bonzini        self.complete_and_wait()
13608e4ed6cSPaolo Bonzini        result = self.vm.qmp('query-block')
13708e4ed6cSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
13808e4ed6cSPaolo Bonzini        self.vm.shutdown()
1393a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
14008e4ed6cSPaolo Bonzini                        'target image does not match source after mirroring')
14108e4ed6cSPaolo Bonzini
14208e4ed6cSPaolo Bonzini    def test_small_buffer2(self):
143ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
14408e4ed6cSPaolo Bonzini
14508e4ed6cSPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,size=%d'
1463b9f27d2SFam Zheng                        % (self.image_len, self.image_len), target_img)
14794ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
14894ca2c73SFam Zheng                             buf_size=65536, mode='existing', target=self.qmp_target)
14908e4ed6cSPaolo Bonzini        self.assert_qmp(result, 'return', {})
15008e4ed6cSPaolo Bonzini
15108e4ed6cSPaolo Bonzini        self.complete_and_wait()
15208e4ed6cSPaolo Bonzini        result = self.vm.qmp('query-block')
15308e4ed6cSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
15408e4ed6cSPaolo Bonzini        self.vm.shutdown()
1553a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
15608e4ed6cSPaolo Bonzini                        'target image does not match source after mirroring')
15708e4ed6cSPaolo Bonzini
15844c7ca5eSPaolo Bonzini    def test_large_cluster(self):
159ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
16044c7ca5eSPaolo Bonzini
16144c7ca5eSPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
162b66ff2c2SEric Blake                        % (self.image_len, backing_img),
163b66ff2c2SEric Blake                 '-F', 'raw', target_img)
16494ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
16594ca2c73SFam Zheng                             mode='existing', target=self.qmp_target)
16644c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
16744c7ca5eSPaolo Bonzini
16844c7ca5eSPaolo Bonzini        self.complete_and_wait()
16944c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
17044c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
17144c7ca5eSPaolo Bonzini        self.vm.shutdown()
1723a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
17344c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
17444c7ca5eSPaolo Bonzini
175d3c8c674SKevin Wolf    # Tests that the insertion of the mirror_top filter node doesn't make a
176d3c8c674SKevin Wolf    # difference to query-block
177d3c8c674SKevin Wolf    def test_implicit_node(self):
178d3c8c674SKevin Wolf        self.assert_no_active_block_jobs()
179d3c8c674SKevin Wolf
180d3c8c674SKevin Wolf        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
181d3c8c674SKevin Wolf                             target=self.qmp_target)
182d3c8c674SKevin Wolf        self.assert_qmp(result, 'return', {})
183d3c8c674SKevin Wolf
184d3c8c674SKevin Wolf        result = self.vm.qmp('query-block')
185d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
186d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
187d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img)
188d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1)
189d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
190d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img)
191d3c8c674SKevin Wolf
192d3c8c674SKevin Wolf        result = self.vm.qmp('query-blockstats')
193d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/node-name', 'top')
194d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/backing/node-name', 'base')
195d3c8c674SKevin Wolf
196d3c8c674SKevin Wolf        self.cancel_and_wait(force=True)
197d3c8c674SKevin Wolf        result = self.vm.qmp('query-block')
198d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
199d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
200d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img)
201d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1)
202d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
203d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img)
204d3c8c674SKevin Wolf
205d3c8c674SKevin Wolf        result = self.vm.qmp('query-blockstats')
206d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/node-name', 'top')
207d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/backing/node-name', 'base')
208d3c8c674SKevin Wolf
20944c7ca5eSPaolo Bonzini    def test_medium_not_found(self):
210d8683155SBo Tu        if iotests.qemu_default_machine != 'pc':
211d8683155SBo Tu            return
212d8683155SBo Tu
21394ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='ide1-cd0', sync='full',
21494ca2c73SFam Zheng                             target=self.qmp_target)
2150524e93aSKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
21644c7ca5eSPaolo Bonzini
21744c7ca5eSPaolo Bonzini    def test_image_not_found(self):
21894ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
21994ca2c73SFam Zheng                             mode='existing', target=self.qmp_target)
22044c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'error/class', 'GenericError')
22144c7ca5eSPaolo Bonzini
22244c7ca5eSPaolo Bonzini    def test_device_not_found(self):
22394ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='nonexistent', sync='full',
22494ca2c73SFam Zheng                             target=self.qmp_target)
2250524e93aSKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
22694ca2c73SFam Zheng
22794ca2c73SFam Zhengclass TestSingleBlockdev(TestSingleDrive):
22894ca2c73SFam Zheng    qmp_cmd = 'blockdev-mirror'
22994ca2c73SFam Zheng    qmp_target = 'node1'
23094ca2c73SFam Zheng
23194ca2c73SFam Zheng    def setUp(self):
23294ca2c73SFam Zheng        TestSingleDrive.setUp(self)
233b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
234b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
2350153d2f5SKevin Wolf        args = {'driver': iotests.imgfmt,
23694ca2c73SFam Zheng                'node-name': self.qmp_target,
2370153d2f5SKevin Wolf                'file': { 'filename': target_img, 'driver': 'file' } }
23894ca2c73SFam Zheng        result = self.vm.qmp("blockdev-add", **args)
23994ca2c73SFam Zheng        self.assert_qmp(result, 'return', {})
24094ca2c73SFam Zheng
24186fae10cSKevin Wolf    def test_mirror_to_self(self):
24286fae10cSKevin Wolf        result = self.vm.qmp(self.qmp_cmd, job_id='job0',
24386fae10cSKevin Wolf                             device=self.qmp_target, sync='full',
24486fae10cSKevin Wolf                             target=self.qmp_target)
24586fae10cSKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
24686fae10cSKevin Wolf
24716cea4eeSKevin Wolf    def do_test_resize(self, device, node):
24816cea4eeSKevin Wolf        def pre_finalize():
24916cea4eeSKevin Wolf            if device:
25016cea4eeSKevin Wolf                result = self.vm.qmp('block_resize', device=device, size=65536)
25116cea4eeSKevin Wolf                self.assert_qmp(result, 'error/class', 'GenericError')
25216cea4eeSKevin Wolf
25316cea4eeSKevin Wolf            result = self.vm.qmp('block_resize', node_name=node, size=65536)
25416cea4eeSKevin Wolf            self.assert_qmp(result, 'error/class', 'GenericError')
25516cea4eeSKevin Wolf
25616cea4eeSKevin Wolf        result = self.vm.qmp(self.qmp_cmd, job_id='job0', device='drive0',
25716cea4eeSKevin Wolf                             sync='full', target=self.qmp_target,
25816cea4eeSKevin Wolf                             auto_finalize=False, auto_dismiss=False)
25916cea4eeSKevin Wolf        self.assert_qmp(result, 'return', {})
26016cea4eeSKevin Wolf
26116cea4eeSKevin Wolf        result = self.vm.run_job('job0', auto_finalize=False,
26216cea4eeSKevin Wolf                                 pre_finalize=pre_finalize)
26316cea4eeSKevin Wolf        self.assertEqual(result, None)
26416cea4eeSKevin Wolf
26516cea4eeSKevin Wolf    def test_source_resize(self):
26616cea4eeSKevin Wolf        self.do_test_resize('drive0', 'top')
26716cea4eeSKevin Wolf
26816cea4eeSKevin Wolf    def test_target_resize(self):
26916cea4eeSKevin Wolf        self.do_test_resize(None, self.qmp_target)
27016cea4eeSKevin Wolf
27116cea4eeSKevin Wolf    def do_test_target_size(self, size):
27216cea4eeSKevin Wolf        result = self.vm.qmp('block_resize', node_name=self.qmp_target,
27316cea4eeSKevin Wolf                             size=size)
27416cea4eeSKevin Wolf        self.assert_qmp(result, 'return', {})
27516cea4eeSKevin Wolf
27616cea4eeSKevin Wolf        result = self.vm.qmp(self.qmp_cmd, job_id='job0',
27716cea4eeSKevin Wolf                             device='drive0', sync='full', auto_dismiss=False,
27816cea4eeSKevin Wolf                             target=self.qmp_target)
27916cea4eeSKevin Wolf        self.assert_qmp(result, 'return', {})
28016cea4eeSKevin Wolf
28116cea4eeSKevin Wolf        result = self.vm.run_job('job0')
28216cea4eeSKevin Wolf        self.assertEqual(result, 'Source and target image have different sizes')
28316cea4eeSKevin Wolf
284c7070942SMax Reitz    # qed does not support shrinking
285c7070942SMax Reitz    @iotests.skip_for_formats(('qed'))
28616cea4eeSKevin Wolf    def test_small_target(self):
28716cea4eeSKevin Wolf        self.do_test_target_size(self.image_len // 2)
28816cea4eeSKevin Wolf
28916cea4eeSKevin Wolf    def test_large_target(self):
29016cea4eeSKevin Wolf        self.do_test_target_size(self.image_len * 2)
29116cea4eeSKevin Wolf
29294ca2c73SFam Zheng    test_large_cluster = None
29394ca2c73SFam Zheng    test_image_not_found = None
29494ca2c73SFam Zheng    test_small_buffer2 = None
29594ca2c73SFam Zheng
2963b9f27d2SFam Zhengclass TestSingleDriveZeroLength(TestSingleDrive):
2973b9f27d2SFam Zheng    image_len = 0
2983b9f27d2SFam Zheng    test_small_buffer2 = None
2993b9f27d2SFam Zheng    test_large_cluster = None
3003b9f27d2SFam Zheng
30194ca2c73SFam Zhengclass TestSingleBlockdevZeroLength(TestSingleBlockdev):
30294ca2c73SFam Zheng    image_len = 0
30316cea4eeSKevin Wolf    test_small_target = None
30416cea4eeSKevin Wolf    test_large_target = None
30594ca2c73SFam Zheng
3065a0f6fd5SKevin Wolfclass TestSingleDriveUnalignedLength(TestSingleDrive):
3075a0f6fd5SKevin Wolf    image_len = 1025 * 1024
3085a0f6fd5SKevin Wolf    test_small_buffer2 = None
3095a0f6fd5SKevin Wolf    test_large_cluster = None
3105a0f6fd5SKevin Wolf
31194ca2c73SFam Zhengclass TestSingleBlockdevUnalignedLength(TestSingleBlockdev):
31294ca2c73SFam Zheng    image_len = 1025 * 1024
31394ca2c73SFam Zheng
314866323f3SFam Zhengclass TestMirrorNoBacking(iotests.QMPTestCase):
31544c7ca5eSPaolo Bonzini    image_len = 2 * 1024 * 1024 # MB
31644c7ca5eSPaolo Bonzini
31744c7ca5eSPaolo Bonzini    def setUp(self):
3182499a096SStefan Hajnoczi        iotests.create_image(backing_img, TestMirrorNoBacking.image_len)
319b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
320b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
32144c7ca5eSPaolo Bonzini        self.vm = iotests.VM().add_drive(test_img)
32244c7ca5eSPaolo Bonzini        self.vm.launch()
32344c7ca5eSPaolo Bonzini
32444c7ca5eSPaolo Bonzini    def tearDown(self):
32544c7ca5eSPaolo Bonzini        self.vm.shutdown()
32644c7ca5eSPaolo Bonzini        os.remove(test_img)
32744c7ca5eSPaolo Bonzini        os.remove(backing_img)
328866323f3SFam Zheng        try:
32944c7ca5eSPaolo Bonzini            os.remove(target_backing_img)
330866323f3SFam Zheng        except:
331866323f3SFam Zheng            pass
33244c7ca5eSPaolo Bonzini        os.remove(target_img)
33344c7ca5eSPaolo Bonzini
33444c7ca5eSPaolo Bonzini    def test_complete(self):
335ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
33644c7ca5eSPaolo Bonzini
337b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
338b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
33944c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
34044c7ca5eSPaolo Bonzini                             mode='existing', target=target_img)
34144c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
34244c7ca5eSPaolo Bonzini
34344c7ca5eSPaolo Bonzini        self.complete_and_wait()
34444c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
34544c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
34644c7ca5eSPaolo Bonzini        self.vm.shutdown()
347866323f3SFam Zheng        self.assertTrue(iotests.compare_images(test_img, target_img),
34844c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
34944c7ca5eSPaolo Bonzini
35044c7ca5eSPaolo Bonzini    def test_cancel(self):
351ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
35244c7ca5eSPaolo Bonzini
353b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
354b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
35544c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
35644c7ca5eSPaolo Bonzini                             mode='existing', target=target_img)
35744c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
35844c7ca5eSPaolo Bonzini
3592575fe16SStefan Hajnoczi        self.wait_ready_and_cancel()
36044c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
36144c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
36244c7ca5eSPaolo Bonzini        self.vm.shutdown()
363866323f3SFam Zheng        self.assertTrue(iotests.compare_images(test_img, target_img),
36444c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
36544c7ca5eSPaolo Bonzini
366b812f671SPaolo Bonzini    def test_large_cluster(self):
367ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
368b812f671SPaolo Bonzini
369b812f671SPaolo Bonzini        # qemu-img create fails if the image is not there
370b812f671SPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt, '-o', 'size=%d'
371b812f671SPaolo Bonzini                        %(TestMirrorNoBacking.image_len), target_backing_img)
372b812f671SPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
373b66ff2c2SEric Blake                        % (TestMirrorNoBacking.image_len, target_backing_img),
374b66ff2c2SEric Blake                 '-F', iotests.imgfmt, target_img)
375b812f671SPaolo Bonzini
376b812f671SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
377b812f671SPaolo Bonzini                             mode='existing', target=target_img)
378b812f671SPaolo Bonzini        self.assert_qmp(result, 'return', {})
379b812f671SPaolo Bonzini
380b812f671SPaolo Bonzini        self.complete_and_wait()
381b812f671SPaolo Bonzini        result = self.vm.qmp('query-block')
382b812f671SPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
383b812f671SPaolo Bonzini        self.vm.shutdown()
384866323f3SFam Zheng        self.assertTrue(iotests.compare_images(test_img, target_img),
385b812f671SPaolo Bonzini                        'target image does not match source after mirroring')
386b812f671SPaolo Bonzini
387866323f3SFam Zhengclass TestMirrorResized(iotests.QMPTestCase):
388a04eca10SVishvananda Ishaya    backing_len = 1 * 1024 * 1024 # MB
389a04eca10SVishvananda Ishaya    image_len = 2 * 1024 * 1024 # MB
390a04eca10SVishvananda Ishaya
391a04eca10SVishvananda Ishaya    def setUp(self):
3922499a096SStefan Hajnoczi        iotests.create_image(backing_img, TestMirrorResized.backing_len)
393b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
394b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
395a04eca10SVishvananda Ishaya        qemu_img('resize', test_img, '2M')
396a04eca10SVishvananda Ishaya        self.vm = iotests.VM().add_drive(test_img)
397a04eca10SVishvananda Ishaya        self.vm.launch()
398a04eca10SVishvananda Ishaya
399a04eca10SVishvananda Ishaya    def tearDown(self):
400a04eca10SVishvananda Ishaya        self.vm.shutdown()
401a04eca10SVishvananda Ishaya        os.remove(test_img)
402a04eca10SVishvananda Ishaya        os.remove(backing_img)
403a04eca10SVishvananda Ishaya        try:
404a04eca10SVishvananda Ishaya            os.remove(target_img)
405a04eca10SVishvananda Ishaya        except OSError:
406a04eca10SVishvananda Ishaya            pass
407a04eca10SVishvananda Ishaya
408a04eca10SVishvananda Ishaya    def test_complete_top(self):
409ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
410a04eca10SVishvananda Ishaya
411a04eca10SVishvananda Ishaya        result = self.vm.qmp('drive-mirror', device='drive0', sync='top',
412a04eca10SVishvananda Ishaya                             target=target_img)
413a04eca10SVishvananda Ishaya        self.assert_qmp(result, 'return', {})
414a04eca10SVishvananda Ishaya
415a04eca10SVishvananda Ishaya        self.complete_and_wait()
416a04eca10SVishvananda Ishaya        result = self.vm.qmp('query-block')
417a04eca10SVishvananda Ishaya        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
418a04eca10SVishvananda Ishaya        self.vm.shutdown()
4193a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
420a04eca10SVishvananda Ishaya                        'target image does not match source after mirroring')
421a04eca10SVishvananda Ishaya
422a04eca10SVishvananda Ishaya    def test_complete_full(self):
423ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
424a04eca10SVishvananda Ishaya
425a04eca10SVishvananda Ishaya        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
426a04eca10SVishvananda Ishaya                             target=target_img)
427a04eca10SVishvananda Ishaya        self.assert_qmp(result, 'return', {})
428a04eca10SVishvananda Ishaya
429a04eca10SVishvananda Ishaya        self.complete_and_wait()
430a04eca10SVishvananda Ishaya        result = self.vm.qmp('query-block')
431a04eca10SVishvananda Ishaya        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
432a04eca10SVishvananda Ishaya        self.vm.shutdown()
4333a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
434a04eca10SVishvananda Ishaya                        'target image does not match source after mirroring')
435a04eca10SVishvananda Ishaya
436866323f3SFam Zhengclass TestReadErrors(iotests.QMPTestCase):
4379dfa9f59SPaolo Bonzini    image_len = 2 * 1024 * 1024 # MB
4389dfa9f59SPaolo Bonzini
4399dfa9f59SPaolo Bonzini    # this should be a multiple of twice the default granularity
4409dfa9f59SPaolo Bonzini    # so that we hit this offset first in state 1
4419dfa9f59SPaolo Bonzini    MIRROR_GRANULARITY = 1024 * 1024
4429dfa9f59SPaolo Bonzini
4439dfa9f59SPaolo Bonzini    def create_blkdebug_file(self, name, event, errno):
4449dfa9f59SPaolo Bonzini        file = open(name, 'w')
4459dfa9f59SPaolo Bonzini        file.write('''
4469dfa9f59SPaolo Bonzini[inject-error]
4479dfa9f59SPaolo Bonzinistate = "1"
4489dfa9f59SPaolo Bonzinievent = "%s"
4499dfa9f59SPaolo Bonzinierrno = "%d"
4509dfa9f59SPaolo Bonziniimmediately = "off"
4519dfa9f59SPaolo Bonzinionce = "on"
4529dfa9f59SPaolo Bonzinisector = "%d"
4539dfa9f59SPaolo Bonzini
4549dfa9f59SPaolo Bonzini[set-state]
4559dfa9f59SPaolo Bonzinistate = "1"
4569dfa9f59SPaolo Bonzinievent = "%s"
4579dfa9f59SPaolo Bonzininew_state = "2"
4589dfa9f59SPaolo Bonzini
4599dfa9f59SPaolo Bonzini[set-state]
4609dfa9f59SPaolo Bonzinistate = "2"
4619dfa9f59SPaolo Bonzinievent = "%s"
4629dfa9f59SPaolo Bonzininew_state = "1"
4639a3a9a63SMax Reitz''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event))
4649dfa9f59SPaolo Bonzini        file.close()
4659dfa9f59SPaolo Bonzini
4669dfa9f59SPaolo Bonzini    def setUp(self):
4679dfa9f59SPaolo Bonzini        self.blkdebug_file = backing_img + ".blkdebug"
4682499a096SStefan Hajnoczi        iotests.create_image(backing_img, TestReadErrors.image_len)
4699dfa9f59SPaolo Bonzini        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
4709dfa9f59SPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt,
4719dfa9f59SPaolo Bonzini                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
4729dfa9f59SPaolo Bonzini                       % (self.blkdebug_file, backing_img),
4739dfa9f59SPaolo Bonzini                 test_img)
474b812f671SPaolo Bonzini        # Write something for tests that use sync='top'
475b812f671SPaolo Bonzini        qemu_io('-c', 'write %d 512' % (self.MIRROR_GRANULARITY + 65536),
476b812f671SPaolo Bonzini                        test_img)
4779dfa9f59SPaolo Bonzini        self.vm = iotests.VM().add_drive(test_img)
4789dfa9f59SPaolo Bonzini        self.vm.launch()
4799dfa9f59SPaolo Bonzini
4809dfa9f59SPaolo Bonzini    def tearDown(self):
4819dfa9f59SPaolo Bonzini        self.vm.shutdown()
4829dfa9f59SPaolo Bonzini        os.remove(test_img)
483db11d1eeSKevin Wolf        os.remove(target_img)
4849dfa9f59SPaolo Bonzini        os.remove(backing_img)
4859dfa9f59SPaolo Bonzini        os.remove(self.blkdebug_file)
4869dfa9f59SPaolo Bonzini
4879dfa9f59SPaolo Bonzini    def test_report_read(self):
488ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
4899dfa9f59SPaolo Bonzini
4909dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
4919dfa9f59SPaolo Bonzini                             target=target_img)
4929dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
4939dfa9f59SPaolo Bonzini
4949dfa9f59SPaolo Bonzini        completed = False
4959dfa9f59SPaolo Bonzini        error = False
4969dfa9f59SPaolo Bonzini        while not completed:
4979dfa9f59SPaolo Bonzini            for event in self.vm.get_qmp_events(wait=True):
4989dfa9f59SPaolo Bonzini                if event['event'] == 'BLOCK_JOB_ERROR':
4999dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
5009dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/operation', 'read')
5019dfa9f59SPaolo Bonzini                    error = True
5029dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_READY':
5039dfa9f59SPaolo Bonzini                    self.assertTrue(False, 'job completed unexpectedly')
5049dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_COMPLETED':
5059dfa9f59SPaolo Bonzini                    self.assertTrue(error, 'job completed unexpectedly')
5069dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/type', 'mirror')
5079dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
5089dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/error', 'Input/output error')
5099dfa9f59SPaolo Bonzini                    completed = True
5101dac83f1SKevin Wolf                elif event['event'] == 'JOB_STATUS_CHANGE':
5111dac83f1SKevin Wolf                    self.assert_qmp(event, 'data/id', 'drive0')
5129dfa9f59SPaolo Bonzini
513ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
5149dfa9f59SPaolo Bonzini
5159dfa9f59SPaolo Bonzini    def test_ignore_read(self):
516ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
5179dfa9f59SPaolo Bonzini
5189dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
5199dfa9f59SPaolo Bonzini                             target=target_img, on_source_error='ignore')
5209dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
5219dfa9f59SPaolo Bonzini
5229dfa9f59SPaolo Bonzini        event = self.vm.get_qmp_event(wait=True)
5231dac83f1SKevin Wolf        while event['event'] == 'JOB_STATUS_CHANGE':
5241dac83f1SKevin Wolf            self.assert_qmp(event, 'data/id', 'drive0')
5251dac83f1SKevin Wolf            event = self.vm.get_qmp_event(wait=True)
5261dac83f1SKevin Wolf
527fa1cfb40SKevin Wolf        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
5289dfa9f59SPaolo Bonzini        self.assert_qmp(event, 'data/device', 'drive0')
5299dfa9f59SPaolo Bonzini        self.assert_qmp(event, 'data/operation', 'read')
5309dfa9f59SPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
5319dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return[0]/paused', False)
5329dfa9f59SPaolo Bonzini        self.complete_and_wait()
5339dfa9f59SPaolo Bonzini
534b812f671SPaolo Bonzini    def test_large_cluster(self):
535ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
536b812f671SPaolo Bonzini
537b812f671SPaolo Bonzini        # Test COW into the target image.  The first half of the
538b812f671SPaolo Bonzini        # cluster at MIRROR_GRANULARITY has to be copied from
539b812f671SPaolo Bonzini        # backing_img, even though sync='top'.
540b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
541b66ff2c2SEric Blake                 '-ocluster_size=131072,backing_file=%s' %(backing_img),
542b66ff2c2SEric Blake                 '-F', 'raw', target_img)
543b812f671SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='top',
544b812f671SPaolo Bonzini                             on_source_error='ignore',
545b812f671SPaolo Bonzini                             mode='existing', target=target_img)
546b812f671SPaolo Bonzini        self.assert_qmp(result, 'return', {})
547b812f671SPaolo Bonzini
548b812f671SPaolo Bonzini        event = self.vm.get_qmp_event(wait=True)
5491dac83f1SKevin Wolf        while event['event'] == 'JOB_STATUS_CHANGE':
5501dac83f1SKevin Wolf            self.assert_qmp(event, 'data/id', 'drive0')
5511dac83f1SKevin Wolf            event = self.vm.get_qmp_event(wait=True)
5521dac83f1SKevin Wolf
553fa1cfb40SKevin Wolf        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
554b812f671SPaolo Bonzini        self.assert_qmp(event, 'data/device', 'drive0')
555b812f671SPaolo Bonzini        self.assert_qmp(event, 'data/operation', 'read')
556b812f671SPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
557b812f671SPaolo Bonzini        self.assert_qmp(result, 'return[0]/paused', False)
558b812f671SPaolo Bonzini        self.complete_and_wait()
559b812f671SPaolo Bonzini        self.vm.shutdown()
560b812f671SPaolo Bonzini
561b812f671SPaolo Bonzini        # Detach blkdebug to compare images successfully
562b66ff2c2SEric Blake        qemu_img('rebase', '-f', iotests.imgfmt, '-u', '-b', backing_img,
563b66ff2c2SEric Blake                 '-F', 'raw', test_img)
5643a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
565b812f671SPaolo Bonzini                        'target image does not match source after mirroring')
566b812f671SPaolo Bonzini
5679dfa9f59SPaolo Bonzini    def test_stop_read(self):
568ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
5699dfa9f59SPaolo Bonzini
5709dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
5719dfa9f59SPaolo Bonzini                             target=target_img, on_source_error='stop')
5729dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
5739dfa9f59SPaolo Bonzini
5749dfa9f59SPaolo Bonzini        error = False
5759dfa9f59SPaolo Bonzini        ready = False
5769dfa9f59SPaolo Bonzini        while not ready:
5779dfa9f59SPaolo Bonzini            for event in self.vm.get_qmp_events(wait=True):
5789dfa9f59SPaolo Bonzini                if event['event'] == 'BLOCK_JOB_ERROR':
5799dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
5809dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/operation', 'read')
5819dfa9f59SPaolo Bonzini
5829dfa9f59SPaolo Bonzini                    result = self.vm.qmp('query-block-jobs')
5839dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return[0]/paused', True)
5849dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
5859dfa9f59SPaolo Bonzini
5869dfa9f59SPaolo Bonzini                    result = self.vm.qmp('block-job-resume', device='drive0')
5879dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return', {})
5889dfa9f59SPaolo Bonzini                    error = True
5899dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_READY':
5909dfa9f59SPaolo Bonzini                    self.assertTrue(error, 'job completed unexpectedly')
5919dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
5929dfa9f59SPaolo Bonzini                    ready = True
5939dfa9f59SPaolo Bonzini
5949dfa9f59SPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
5959dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return[0]/paused', False)
5969dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return[0]/io-status', 'ok')
5979dfa9f59SPaolo Bonzini
5989dfa9f59SPaolo Bonzini        self.complete_and_wait(wait_ready=False)
599ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6009dfa9f59SPaolo Bonzini
601866323f3SFam Zhengclass TestWriteErrors(iotests.QMPTestCase):
6029dfa9f59SPaolo Bonzini    image_len = 2 * 1024 * 1024 # MB
6039dfa9f59SPaolo Bonzini
6049dfa9f59SPaolo Bonzini    # this should be a multiple of twice the default granularity
6059dfa9f59SPaolo Bonzini    # so that we hit this offset first in state 1
6069dfa9f59SPaolo Bonzini    MIRROR_GRANULARITY = 1024 * 1024
6079dfa9f59SPaolo Bonzini
6089dfa9f59SPaolo Bonzini    def create_blkdebug_file(self, name, event, errno):
6099dfa9f59SPaolo Bonzini        file = open(name, 'w')
6109dfa9f59SPaolo Bonzini        file.write('''
6119dfa9f59SPaolo Bonzini[inject-error]
6129dfa9f59SPaolo Bonzinistate = "1"
6139dfa9f59SPaolo Bonzinievent = "%s"
6149dfa9f59SPaolo Bonzinierrno = "%d"
6159dfa9f59SPaolo Bonziniimmediately = "off"
6169dfa9f59SPaolo Bonzinionce = "on"
6179dfa9f59SPaolo Bonzinisector = "%d"
6189dfa9f59SPaolo Bonzini
6199dfa9f59SPaolo Bonzini[set-state]
6209dfa9f59SPaolo Bonzinistate = "1"
6219dfa9f59SPaolo Bonzinievent = "%s"
6229dfa9f59SPaolo Bonzininew_state = "2"
6239dfa9f59SPaolo Bonzini
6249dfa9f59SPaolo Bonzini[set-state]
6259dfa9f59SPaolo Bonzinistate = "2"
6269dfa9f59SPaolo Bonzinievent = "%s"
6279dfa9f59SPaolo Bonzininew_state = "1"
6289a3a9a63SMax Reitz''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event))
6299dfa9f59SPaolo Bonzini        file.close()
6309dfa9f59SPaolo Bonzini
6319dfa9f59SPaolo Bonzini    def setUp(self):
6329dfa9f59SPaolo Bonzini        self.blkdebug_file = target_img + ".blkdebug"
6332499a096SStefan Hajnoczi        iotests.create_image(backing_img, TestWriteErrors.image_len)
6349dfa9f59SPaolo Bonzini        self.create_blkdebug_file(self.blkdebug_file, "write_aio", 5)
635b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
636b66ff2c2SEric Blake                 '-obacking_file=%s' %(backing_img), '-F', 'raw', test_img)
6379dfa9f59SPaolo Bonzini        self.vm = iotests.VM().add_drive(test_img)
6389dfa9f59SPaolo Bonzini        self.target_img = 'blkdebug:%s:%s' % (self.blkdebug_file, target_img)
6399dfa9f59SPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt, '-osize=%d' %(TestWriteErrors.image_len), target_img)
6409dfa9f59SPaolo Bonzini        self.vm.launch()
6419dfa9f59SPaolo Bonzini
6429dfa9f59SPaolo Bonzini    def tearDown(self):
6439dfa9f59SPaolo Bonzini        self.vm.shutdown()
6449dfa9f59SPaolo Bonzini        os.remove(test_img)
645db11d1eeSKevin Wolf        os.remove(target_img)
6469dfa9f59SPaolo Bonzini        os.remove(backing_img)
6479dfa9f59SPaolo Bonzini        os.remove(self.blkdebug_file)
6489dfa9f59SPaolo Bonzini
6499dfa9f59SPaolo Bonzini    def test_report_write(self):
650ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6519dfa9f59SPaolo Bonzini
6529dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
6539dfa9f59SPaolo Bonzini                             mode='existing', target=self.target_img)
6549dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
6559dfa9f59SPaolo Bonzini
6569dfa9f59SPaolo Bonzini        completed = False
6579dfa9f59SPaolo Bonzini        error = False
6589dfa9f59SPaolo Bonzini        while not completed:
6599dfa9f59SPaolo Bonzini            for event in self.vm.get_qmp_events(wait=True):
6609dfa9f59SPaolo Bonzini                if event['event'] == 'BLOCK_JOB_ERROR':
6619dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
6629dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/operation', 'write')
6639dfa9f59SPaolo Bonzini                    error = True
6649dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_READY':
6659dfa9f59SPaolo Bonzini                    self.assertTrue(False, 'job completed unexpectedly')
6669dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_COMPLETED':
6679dfa9f59SPaolo Bonzini                    self.assertTrue(error, 'job completed unexpectedly')
6689dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/type', 'mirror')
6699dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
6709dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/error', 'Input/output error')
6719dfa9f59SPaolo Bonzini                    completed = True
6729dfa9f59SPaolo Bonzini
673ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6749dfa9f59SPaolo Bonzini
6759dfa9f59SPaolo Bonzini    def test_ignore_write(self):
676ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6779dfa9f59SPaolo Bonzini
6789dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
6799dfa9f59SPaolo Bonzini                             mode='existing', target=self.target_img,
6809dfa9f59SPaolo Bonzini                             on_target_error='ignore')
6819dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
6829dfa9f59SPaolo Bonzini
6831dac83f1SKevin Wolf        event = self.vm.event_wait(name='BLOCK_JOB_ERROR')
684fa1cfb40SKevin Wolf        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
6859dfa9f59SPaolo Bonzini        self.assert_qmp(event, 'data/device', 'drive0')
6869dfa9f59SPaolo Bonzini        self.assert_qmp(event, 'data/operation', 'write')
6879dfa9f59SPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
6889dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return[0]/paused', False)
6899dfa9f59SPaolo Bonzini        self.complete_and_wait()
6909dfa9f59SPaolo Bonzini
6919dfa9f59SPaolo Bonzini    def test_stop_write(self):
692ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6939dfa9f59SPaolo Bonzini
6949dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
6959dfa9f59SPaolo Bonzini                             mode='existing', target=self.target_img,
6969dfa9f59SPaolo Bonzini                             on_target_error='stop')
6979dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
6989dfa9f59SPaolo Bonzini
6999dfa9f59SPaolo Bonzini        error = False
7009dfa9f59SPaolo Bonzini        ready = False
7019dfa9f59SPaolo Bonzini        while not ready:
7029dfa9f59SPaolo Bonzini            for event in self.vm.get_qmp_events(wait=True):
7039dfa9f59SPaolo Bonzini                if event['event'] == 'BLOCK_JOB_ERROR':
7049dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
7059dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/operation', 'write')
7069dfa9f59SPaolo Bonzini
7079dfa9f59SPaolo Bonzini                    result = self.vm.qmp('query-block-jobs')
7089dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return[0]/paused', True)
7099dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
7109dfa9f59SPaolo Bonzini
7119dfa9f59SPaolo Bonzini                    result = self.vm.qmp('block-job-resume', device='drive0')
7129dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return', {})
7139dfa9f59SPaolo Bonzini
7149dfa9f59SPaolo Bonzini                    result = self.vm.qmp('query-block-jobs')
7159dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return[0]/paused', False)
7169dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
7179dfa9f59SPaolo Bonzini                    error = True
7189dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_READY':
7199dfa9f59SPaolo Bonzini                    self.assertTrue(error, 'job completed unexpectedly')
7209dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
7219dfa9f59SPaolo Bonzini                    ready = True
7229dfa9f59SPaolo Bonzini
7239dfa9f59SPaolo Bonzini        self.complete_and_wait(wait_ready=False)
724ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
7259dfa9f59SPaolo Bonzini
726866323f3SFam Zhengclass TestSetSpeed(iotests.QMPTestCase):
72744c7ca5eSPaolo Bonzini    image_len = 80 * 1024 * 1024 # MB
72844c7ca5eSPaolo Bonzini
72944c7ca5eSPaolo Bonzini    def setUp(self):
73044c7ca5eSPaolo Bonzini        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
731b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
732b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
73344c7ca5eSPaolo Bonzini        self.vm = iotests.VM().add_drive(test_img)
73444c7ca5eSPaolo Bonzini        self.vm.launch()
73544c7ca5eSPaolo Bonzini
73644c7ca5eSPaolo Bonzini    def tearDown(self):
73744c7ca5eSPaolo Bonzini        self.vm.shutdown()
73844c7ca5eSPaolo Bonzini        os.remove(test_img)
73944c7ca5eSPaolo Bonzini        os.remove(backing_img)
74044c7ca5eSPaolo Bonzini        os.remove(target_img)
74144c7ca5eSPaolo Bonzini
74244c7ca5eSPaolo Bonzini    def test_set_speed(self):
743ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
74444c7ca5eSPaolo Bonzini
74544c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
74644c7ca5eSPaolo Bonzini                             target=target_img)
74744c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
74844c7ca5eSPaolo Bonzini
74944c7ca5eSPaolo Bonzini        # Default speed is 0
75044c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
75144c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/device', 'drive0')
75244c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/speed', 0)
75344c7ca5eSPaolo Bonzini
75444c7ca5eSPaolo Bonzini        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
75544c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
75644c7ca5eSPaolo Bonzini
75744c7ca5eSPaolo Bonzini        # Ensure the speed we set was accepted
75844c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
75944c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/device', 'drive0')
76044c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
76144c7ca5eSPaolo Bonzini
7622575fe16SStefan Hajnoczi        self.wait_ready_and_cancel()
76344c7ca5eSPaolo Bonzini
76444c7ca5eSPaolo Bonzini        # Check setting speed in drive-mirror works
76544c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
76644c7ca5eSPaolo Bonzini                             target=target_img, speed=4*1024*1024)
76744c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
76844c7ca5eSPaolo Bonzini
76944c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
77044c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/device', 'drive0')
77144c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
77244c7ca5eSPaolo Bonzini
7732575fe16SStefan Hajnoczi        self.wait_ready_and_cancel()
77444c7ca5eSPaolo Bonzini
77544c7ca5eSPaolo Bonzini    def test_set_speed_invalid(self):
776ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
77744c7ca5eSPaolo Bonzini
77844c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
77944c7ca5eSPaolo Bonzini                             target=target_img, speed=-1)
78044c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'error/class', 'GenericError')
78144c7ca5eSPaolo Bonzini
782ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
78344c7ca5eSPaolo Bonzini
78444c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
78544c7ca5eSPaolo Bonzini                             target=target_img)
78644c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
78744c7ca5eSPaolo Bonzini
78844c7ca5eSPaolo Bonzini        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
78944c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'error/class', 'GenericError')
79044c7ca5eSPaolo Bonzini
7912575fe16SStefan Hajnoczi        self.wait_ready_and_cancel()
79244c7ca5eSPaolo Bonzini
793866323f3SFam Zhengclass TestUnbackedSource(iotests.QMPTestCase):
794c15badeeSMax Reitz    image_len = 2 * 1024 * 1024 # MB
795c15badeeSMax Reitz
796c15badeeSMax Reitz    def setUp(self):
797c15badeeSMax Reitz        qemu_img('create', '-f', iotests.imgfmt, test_img,
798c15badeeSMax Reitz                 str(TestUnbackedSource.image_len))
7999463ee1fSMax Reitz        self.vm = iotests.VM()
800c15badeeSMax Reitz        self.vm.launch()
8019463ee1fSMax Reitz        result = self.vm.qmp('blockdev-add', node_name='drive0',
8029463ee1fSMax Reitz                             driver=iotests.imgfmt,
8039463ee1fSMax Reitz                             file={
8049463ee1fSMax Reitz                                 'driver': 'file',
8059463ee1fSMax Reitz                                 'filename': test_img,
8069463ee1fSMax Reitz                             })
8079463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
808c15badeeSMax Reitz
809c15badeeSMax Reitz    def tearDown(self):
810c15badeeSMax Reitz        self.vm.shutdown()
811c15badeeSMax Reitz        os.remove(test_img)
812c15badeeSMax Reitz        os.remove(target_img)
813c15badeeSMax Reitz
814171d6431SMax Reitz    def test_absolute_paths_full(self):
815171d6431SMax Reitz        self.assert_no_active_block_jobs()
8169463ee1fSMax Reitz        result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0',
817171d6431SMax Reitz                             sync='full', target=target_img,
818171d6431SMax Reitz                             mode='absolute-paths')
819171d6431SMax Reitz        self.assert_qmp(result, 'return', {})
820171d6431SMax Reitz        self.complete_and_wait()
821c15badeeSMax Reitz        self.assert_no_active_block_jobs()
822c15badeeSMax Reitz
823171d6431SMax Reitz    def test_absolute_paths_top(self):
824171d6431SMax Reitz        self.assert_no_active_block_jobs()
8259463ee1fSMax Reitz        result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0',
826171d6431SMax Reitz                             sync='top', target=target_img,
827171d6431SMax Reitz                             mode='absolute-paths')
828171d6431SMax Reitz        self.assert_qmp(result, 'return', {})
829171d6431SMax Reitz        self.complete_and_wait()
830171d6431SMax Reitz        self.assert_no_active_block_jobs()
831171d6431SMax Reitz
832171d6431SMax Reitz    def test_absolute_paths_none(self):
833171d6431SMax Reitz        self.assert_no_active_block_jobs()
8349463ee1fSMax Reitz        result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0',
835171d6431SMax Reitz                             sync='none', target=target_img,
836c15badeeSMax Reitz                             mode='absolute-paths')
837c15badeeSMax Reitz        self.assert_qmp(result, 'return', {})
838c15badeeSMax Reitz        self.complete_and_wait()
839c15badeeSMax Reitz        self.assert_no_active_block_jobs()
840c15badeeSMax Reitz
8419463ee1fSMax Reitz    def test_existing_full(self):
8429463ee1fSMax Reitz        qemu_img('create', '-f', iotests.imgfmt, target_img,
8439463ee1fSMax Reitz                 str(self.image_len))
8449463ee1fSMax Reitz        qemu_io('-c', 'write -P 42 0 64k', target_img)
8459463ee1fSMax Reitz
8469463ee1fSMax Reitz        self.assert_no_active_block_jobs()
8479463ee1fSMax Reitz        result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0',
8489463ee1fSMax Reitz                             sync='full', target=target_img, mode='existing')
8499463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8509463ee1fSMax Reitz        self.complete_and_wait()
8519463ee1fSMax Reitz        self.assert_no_active_block_jobs()
8529463ee1fSMax Reitz
8539463ee1fSMax Reitz        result = self.vm.qmp('blockdev-del', node_name='drive0')
8549463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8559463ee1fSMax Reitz
8569463ee1fSMax Reitz        self.assertTrue(iotests.compare_images(test_img, target_img),
8579463ee1fSMax Reitz                        'target image does not match source after mirroring')
8589463ee1fSMax Reitz
8599463ee1fSMax Reitz    def test_blockdev_full(self):
8609463ee1fSMax Reitz        qemu_img('create', '-f', iotests.imgfmt, target_img,
8619463ee1fSMax Reitz                 str(self.image_len))
8629463ee1fSMax Reitz        qemu_io('-c', 'write -P 42 0 64k', target_img)
8639463ee1fSMax Reitz
8649463ee1fSMax Reitz        result = self.vm.qmp('blockdev-add', node_name='target',
8659463ee1fSMax Reitz                             driver=iotests.imgfmt,
8669463ee1fSMax Reitz                             file={
8679463ee1fSMax Reitz                                 'driver': 'file',
8689463ee1fSMax Reitz                                 'filename': target_img,
8699463ee1fSMax Reitz                             })
8709463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8719463ee1fSMax Reitz
8729463ee1fSMax Reitz        self.assert_no_active_block_jobs()
8739463ee1fSMax Reitz        result = self.vm.qmp('blockdev-mirror', job_id='drive0', device='drive0',
8749463ee1fSMax Reitz                             sync='full', target='target')
8759463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8769463ee1fSMax Reitz        self.complete_and_wait()
8779463ee1fSMax Reitz        self.assert_no_active_block_jobs()
8789463ee1fSMax Reitz
8799463ee1fSMax Reitz        result = self.vm.qmp('blockdev-del', node_name='drive0')
8809463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8819463ee1fSMax Reitz
8829463ee1fSMax Reitz        result = self.vm.qmp('blockdev-del', node_name='target')
8839463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8849463ee1fSMax Reitz
8859463ee1fSMax Reitz        self.assertTrue(iotests.compare_images(test_img, target_img),
8869463ee1fSMax Reitz                        'target image does not match source after mirroring')
8879463ee1fSMax Reitz
888ccee3d8fSJohn Snowclass TestGranularity(iotests.QMPTestCase):
889ccee3d8fSJohn Snow    image_len = 10 * 1024 * 1024 # MB
890ccee3d8fSJohn Snow
891ccee3d8fSJohn Snow    def setUp(self):
892ccee3d8fSJohn Snow        qemu_img('create', '-f', iotests.imgfmt, test_img,
893ccee3d8fSJohn Snow                 str(TestGranularity.image_len))
894ccee3d8fSJohn Snow        qemu_io('-c', 'write 0 %d' % (self.image_len),
895ccee3d8fSJohn Snow                test_img)
896ccee3d8fSJohn Snow        self.vm = iotests.VM().add_drive(test_img)
897ccee3d8fSJohn Snow        self.vm.launch()
898ccee3d8fSJohn Snow
899ccee3d8fSJohn Snow    def tearDown(self):
900ccee3d8fSJohn Snow        self.vm.shutdown()
901ccee3d8fSJohn Snow        self.assertTrue(iotests.compare_images(test_img, target_img),
902ccee3d8fSJohn Snow                        'target image does not match source after mirroring')
903ccee3d8fSJohn Snow        os.remove(test_img)
904ccee3d8fSJohn Snow        os.remove(target_img)
905ccee3d8fSJohn Snow
906ccee3d8fSJohn Snow    def test_granularity(self):
907ccee3d8fSJohn Snow        self.assert_no_active_block_jobs()
908ccee3d8fSJohn Snow        result = self.vm.qmp('drive-mirror', device='drive0',
909ccee3d8fSJohn Snow                             sync='full', target=target_img,
910ccee3d8fSJohn Snow                             mode='absolute-paths', granularity=8192)
911ccee3d8fSJohn Snow        self.assert_qmp(result, 'return', {})
9121dac83f1SKevin Wolf
913ccee3d8fSJohn Snow        event = self.vm.get_qmp_event(wait=60.0)
9141dac83f1SKevin Wolf        while event['event'] == 'JOB_STATUS_CHANGE':
9151dac83f1SKevin Wolf            self.assert_qmp(event, 'data/id', 'drive0')
9161dac83f1SKevin Wolf            event = self.vm.get_qmp_event(wait=60.0)
9171dac83f1SKevin Wolf
918ccee3d8fSJohn Snow        # Failures will manifest as COMPLETED/ERROR.
919ccee3d8fSJohn Snow        self.assert_qmp(event, 'event', 'BLOCK_JOB_READY')
920ccee3d8fSJohn Snow        self.complete_and_wait(drive='drive0', wait_ready=False)
921ccee3d8fSJohn Snow        self.assert_no_active_block_jobs()
922ccee3d8fSJohn Snow
923866323f3SFam Zhengclass TestRepairQuorum(iotests.QMPTestCase):
924d88964aeSBenoît Canet    """ This class test quorum file repair using drive-mirror.
925d88964aeSBenoît Canet        It's mostly a fork of TestSingleDrive """
926d88964aeSBenoît Canet    image_len = 1 * 1024 * 1024 # MB
927d88964aeSBenoît Canet    IMAGES = [ quorum_img1, quorum_img2, quorum_img3 ]
928d88964aeSBenoît Canet
9299442bebeSThomas Huth    @iotests.skip_if_unsupported(['quorum'])
930d88964aeSBenoît Canet    def setUp(self):
931d88964aeSBenoît Canet        self.vm = iotests.VM()
932d88964aeSBenoît Canet
9330ed82f7aSMax Reitz        if iotests.qemu_default_machine == 'pc':
9340ed82f7aSMax Reitz            self.vm.add_drive(None, 'media=cdrom', 'ide')
9350ed82f7aSMax Reitz
936d88964aeSBenoît Canet        # Add each individual quorum images
937d88964aeSBenoît Canet        for i in self.IMAGES:
938d88964aeSBenoît Canet            qemu_img('create', '-f', iotests.imgfmt, i,
93989e21945SMax Reitz                     str(self.image_len))
940d88964aeSBenoît Canet            # Assign a node name to each quorum image in order to manipulate
941d88964aeSBenoît Canet            # them
942d88964aeSBenoît Canet            opts = "node-name=img%i" % self.IMAGES.index(i)
943f718ca14SMax Reitz            opts += ',driver=%s' % iotests.imgfmt
944f718ca14SMax Reitz            opts += ',file.driver=file'
945f718ca14SMax Reitz            opts += ',file.filename=%s' % i
946f718ca14SMax Reitz            self.vm = self.vm.add_blockdev(opts)
947d88964aeSBenoît Canet
948d88964aeSBenoît Canet        self.vm.launch()
949d88964aeSBenoît Canet
950d88964aeSBenoît Canet        #assemble the quorum block device from the individual files
9510153d2f5SKevin Wolf        args = { "driver": "quorum", "node-name": "quorum0",
9520153d2f5SKevin Wolf                 "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] }
953d88964aeSBenoît Canet        result = self.vm.qmp("blockdev-add", **args)
954d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
955d88964aeSBenoît Canet
956d88964aeSBenoît Canet
957d88964aeSBenoît Canet    def tearDown(self):
958d88964aeSBenoît Canet        self.vm.shutdown()
959a1da1878SMax Reitz        for i in self.IMAGES + [ quorum_repair_img, quorum_snapshot_file,
960a1da1878SMax Reitz                                 nbd_sock_path ]:
961d88964aeSBenoît Canet            # Do a try/except because the test may have deleted some images
962d88964aeSBenoît Canet            try:
963d88964aeSBenoît Canet                os.remove(i)
964d88964aeSBenoît Canet            except OSError:
965d88964aeSBenoît Canet                pass
966d88964aeSBenoît Canet
967d88964aeSBenoît Canet    def test_complete(self):
968476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
969476fb028SKevin Wolf                             sync='full', node_name="repair0", replaces="img1",
970d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
971d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
972d88964aeSBenoît Canet
973476fb028SKevin Wolf        self.complete_and_wait(drive="job0")
974e71fc0baSFam Zheng        self.assert_has_block_node("repair0", quorum_repair_img)
975c351afd6SMax Reitz        self.vm.assert_block_path('quorum0', '/children.1', 'repair0')
976d88964aeSBenoît Canet        self.vm.shutdown()
977d88964aeSBenoît Canet        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
978d88964aeSBenoît Canet                        'target image does not match source after mirroring')
979d88964aeSBenoît Canet
980d88964aeSBenoît Canet    def test_cancel(self):
981476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
982476fb028SKevin Wolf                             sync='full', node_name="repair0", replaces="img1",
983d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
984d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
985d88964aeSBenoît Canet
986476fb028SKevin Wolf        self.cancel_and_wait(drive="job0", force=True)
987d88964aeSBenoît Canet        # here we check that the last registered quorum file has not been
988d88964aeSBenoît Canet        # swapped out and unref
989e71fc0baSFam Zheng        self.assert_has_block_node(None, quorum_img3)
990d88964aeSBenoît Canet
991d88964aeSBenoît Canet    def test_cancel_after_ready(self):
992476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
993476fb028SKevin Wolf                             sync='full', node_name="repair0", replaces="img1",
994d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
995d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
996d88964aeSBenoît Canet
997476fb028SKevin Wolf        self.wait_ready_and_cancel(drive="job0")
998d88964aeSBenoît Canet        # here we check that the last registered quorum file has not been
999d88964aeSBenoît Canet        # swapped out and unref
1000e71fc0baSFam Zheng        self.assert_has_block_node(None, quorum_img3)
1001d88964aeSBenoît Canet        self.vm.shutdown()
1002d88964aeSBenoît Canet        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
1003d88964aeSBenoît Canet                        'target image does not match source after mirroring')
1004d88964aeSBenoît Canet
1005d88964aeSBenoît Canet    def test_pause(self):
1006476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1007476fb028SKevin Wolf                             sync='full', node_name="repair0", replaces="img1",
1008d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1009d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
1010d88964aeSBenoît Canet
10112c93c5cbSKevin Wolf        self.pause_job('job0')
1012d88964aeSBenoît Canet
1013d88964aeSBenoît Canet        result = self.vm.qmp('query-block-jobs')
1014d88964aeSBenoît Canet        offset = self.dictpath(result, 'return[0]/offset')
1015d88964aeSBenoît Canet
10162c93c5cbSKevin Wolf        time.sleep(0.5)
1017d88964aeSBenoît Canet        result = self.vm.qmp('query-block-jobs')
1018d88964aeSBenoît Canet        self.assert_qmp(result, 'return[0]/offset', offset)
1019d88964aeSBenoît Canet
1020476fb028SKevin Wolf        result = self.vm.qmp('block-job-resume', device='job0')
1021d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
1022d88964aeSBenoît Canet
1023476fb028SKevin Wolf        self.complete_and_wait(drive="job0")
1024d88964aeSBenoît Canet        self.vm.shutdown()
1025d88964aeSBenoît Canet        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
1026d88964aeSBenoît Canet                        'target image does not match source after mirroring')
1027d88964aeSBenoît Canet
1028d88964aeSBenoît Canet    def test_medium_not_found(self):
1029d8683155SBo Tu        if iotests.qemu_default_machine != 'pc':
1030d8683155SBo Tu            return
1031d8683155SBo Tu
1032476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='drive0', # CD-ROM
10330ed82f7aSMax Reitz                             sync='full',
1034d88964aeSBenoît Canet                             node_name='repair0',
1035d88964aeSBenoît Canet                             replaces='img1',
1036d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1037d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1038d88964aeSBenoît Canet
1039d88964aeSBenoît Canet    def test_image_not_found(self):
1040476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1041476fb028SKevin Wolf                             sync='full', node_name='repair0', replaces='img1',
1042476fb028SKevin Wolf                             mode='existing', target=quorum_repair_img,
1043476fb028SKevin Wolf                             format=iotests.imgfmt)
1044d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1045d88964aeSBenoît Canet
1046d88964aeSBenoît Canet    def test_device_not_found(self):
1047476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0',
1048476fb028SKevin Wolf                             device='nonexistent', sync='full',
1049d88964aeSBenoît Canet                             node_name='repair0',
1050d88964aeSBenoît Canet                             replaces='img1',
1051d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
10520524e93aSKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
1053d88964aeSBenoît Canet
1054d88964aeSBenoît Canet    def test_wrong_sync_mode(self):
1055476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', device='quorum0', job_id='job0',
1056d88964aeSBenoît Canet                             node_name='repair0',
1057d88964aeSBenoît Canet                             replaces='img1',
1058d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1059d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1060d88964aeSBenoît Canet
1061d88964aeSBenoît Canet    def test_no_node_name(self):
1062476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1063476fb028SKevin Wolf                             sync='full', replaces='img1',
1064d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1065d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1066d88964aeSBenoît Canet
106767cc32ebSVeres Lajos    def test_nonexistent_replaces(self):
1068476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1069476fb028SKevin Wolf                             sync='full', node_name='repair0', replaces='img77',
1070d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1071d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1072d88964aeSBenoît Canet
1073d88964aeSBenoît Canet    def test_after_a_quorum_snapshot(self):
1074d88964aeSBenoît Canet        result = self.vm.qmp('blockdev-snapshot-sync', node_name='img1',
1075d88964aeSBenoît Canet                             snapshot_file=quorum_snapshot_file,
1076d88964aeSBenoît Canet                             snapshot_node_name="snap1");
1077d88964aeSBenoît Canet
1078476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1079476fb028SKevin Wolf                             sync='full', node_name='repair0', replaces="img1",
1080d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1081d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1082d88964aeSBenoît Canet
1083476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1084476fb028SKevin Wolf                             sync='full', node_name='repair0', replaces="snap1",
1085d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1086d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
1087d88964aeSBenoît Canet
1088476fb028SKevin Wolf        self.complete_and_wait('job0')
1089e71fc0baSFam Zheng        self.assert_has_block_node("repair0", quorum_repair_img)
1090c351afd6SMax Reitz        self.vm.assert_block_path('quorum0', '/children.1', 'repair0')
1091d88964aeSBenoît Canet
1092a1da1878SMax Reitz    def test_with_other_parent(self):
1093a1da1878SMax Reitz        """
1094a1da1878SMax Reitz        Check that we cannot replace a Quorum child when it has other
1095a1da1878SMax Reitz        parents.
1096a1da1878SMax Reitz        """
1097a1da1878SMax Reitz        result = self.vm.qmp('nbd-server-start',
1098a1da1878SMax Reitz                             addr={
1099a1da1878SMax Reitz                                 'type': 'unix',
1100a1da1878SMax Reitz                                 'data': {'path': nbd_sock_path}
1101a1da1878SMax Reitz                             })
1102a1da1878SMax Reitz        self.assert_qmp(result, 'return', {})
1103a1da1878SMax Reitz
1104a1da1878SMax Reitz        result = self.vm.qmp('nbd-server-add', device='img1')
1105a1da1878SMax Reitz        self.assert_qmp(result, 'return', {})
1106a1da1878SMax Reitz
1107a1da1878SMax Reitz        result = self.vm.qmp('drive-mirror', job_id='mirror', device='quorum0',
1108a1da1878SMax Reitz                             sync='full', node_name='repair0', replaces='img1',
1109a1da1878SMax Reitz                             target=quorum_repair_img, format=iotests.imgfmt)
1110a1da1878SMax Reitz        self.assert_qmp(result, 'error/desc',
1111a1da1878SMax Reitz                        "Cannot replace 'img1' by a node mirrored from "
1112a1da1878SMax Reitz                        "'quorum0', because it cannot be guaranteed that doing "
1113a1da1878SMax Reitz                        "so would not lead to an abrupt change of visible data")
1114a1da1878SMax Reitz
1115a1da1878SMax Reitz    def test_with_other_parents_after_mirror_start(self):
1116a1da1878SMax Reitz        """
1117a1da1878SMax Reitz        The same as test_with_other_parent(), but add the NBD server
1118a1da1878SMax Reitz        only when the mirror job is already running.
1119a1da1878SMax Reitz        """
1120a1da1878SMax Reitz        result = self.vm.qmp('nbd-server-start',
1121a1da1878SMax Reitz                             addr={
1122a1da1878SMax Reitz                                 'type': 'unix',
1123a1da1878SMax Reitz                                 'data': {'path': nbd_sock_path}
1124a1da1878SMax Reitz                             })
1125a1da1878SMax Reitz        self.assert_qmp(result, 'return', {})
1126a1da1878SMax Reitz
1127a1da1878SMax Reitz        result = self.vm.qmp('drive-mirror', job_id='mirror', device='quorum0',
1128a1da1878SMax Reitz                             sync='full', node_name='repair0', replaces='img1',
1129a1da1878SMax Reitz                             target=quorum_repair_img, format=iotests.imgfmt)
1130a1da1878SMax Reitz        self.assert_qmp(result, 'return', {})
1131a1da1878SMax Reitz
1132a1da1878SMax Reitz        result = self.vm.qmp('nbd-server-add', device='img1')
1133a1da1878SMax Reitz        self.assert_qmp(result, 'return', {})
1134a1da1878SMax Reitz
1135a1da1878SMax Reitz        # The full error message goes to stderr, we will check it later
1136a1da1878SMax Reitz        self.complete_and_wait('mirror',
1137a1da1878SMax Reitz                               completion_error='Operation not permitted')
1138a1da1878SMax Reitz
1139a1da1878SMax Reitz        # Should not have been replaced
1140a1da1878SMax Reitz        self.vm.assert_block_path('quorum0', '/children.1', 'img1')
1141a1da1878SMax Reitz
1142a1da1878SMax Reitz        # Check the full error message now
1143a1da1878SMax Reitz        self.vm.shutdown()
1144a1da1878SMax Reitz        log = self.vm.get_log()
1145a1da1878SMax Reitz        log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log)
1146a1da1878SMax Reitz        log = re.sub(r'^Formatting.*\n', '', log)
1147a1da1878SMax Reitz        log = re.sub(r'\n\[I \+\d+\.\d+\] CLOSED\n?$', '', log)
1148a1da1878SMax Reitz        log = re.sub(r'^%s: ' % os.path.basename(iotests.qemu_prog), '', log)
1149a1da1878SMax Reitz
1150a1da1878SMax Reitz        self.assertEqual(log,
1151a1da1878SMax Reitz                         "Can no longer replace 'img1' by 'repair0', because " +
1152a1da1878SMax Reitz                         "it can no longer be guaranteed that doing so would " +
1153a1da1878SMax Reitz                         "not lead to an abrupt change of visible data")
1154a1da1878SMax Reitz
1155a1da1878SMax Reitz
11565694923aSMax Reitz# Test mirroring with a source that does not have any parents (not even a
11575694923aSMax Reitz# BlockBackend)
11585694923aSMax Reitzclass TestOrphanedSource(iotests.QMPTestCase):
11595694923aSMax Reitz    def setUp(self):
11605694923aSMax Reitz        blk0 = { 'node-name': 'src',
11615694923aSMax Reitz                 'driver': 'null-co' }
11625694923aSMax Reitz
11635694923aSMax Reitz        blk1 = { 'node-name': 'dest',
11645694923aSMax Reitz                 'driver': 'null-co' }
11655694923aSMax Reitz
11665694923aSMax Reitz        blk2 = { 'node-name': 'dest-ro',
11675694923aSMax Reitz                 'driver': 'null-co',
11685694923aSMax Reitz                 'read-only': 'on' }
11695694923aSMax Reitz
11705694923aSMax Reitz        self.vm = iotests.VM()
117162a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blk0))
117262a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blk1))
117362a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blk2))
11745694923aSMax Reitz        self.vm.launch()
11755694923aSMax Reitz
11765694923aSMax Reitz    def tearDown(self):
11775694923aSMax Reitz        self.vm.shutdown()
11785694923aSMax Reitz
11795694923aSMax Reitz    def test_no_job_id(self):
11805694923aSMax Reitz        self.assert_no_active_block_jobs()
11815694923aSMax Reitz
11825694923aSMax Reitz        result = self.vm.qmp('blockdev-mirror', device='src', sync='full',
11835694923aSMax Reitz                             target='dest')
11845694923aSMax Reitz        self.assert_qmp(result, 'error/class', 'GenericError')
11855694923aSMax Reitz
11865694923aSMax Reitz    def test_success(self):
11875694923aSMax Reitz        self.assert_no_active_block_jobs()
11885694923aSMax Reitz
11895694923aSMax Reitz        result = self.vm.qmp('blockdev-mirror', job_id='job', device='src',
11905694923aSMax Reitz                             sync='full', target='dest')
11915694923aSMax Reitz        self.assert_qmp(result, 'return', {})
11925694923aSMax Reitz
11935694923aSMax Reitz        self.complete_and_wait('job')
11945694923aSMax Reitz
11955694923aSMax Reitz    def test_failing_permissions(self):
11965694923aSMax Reitz        self.assert_no_active_block_jobs()
11975694923aSMax Reitz
11985694923aSMax Reitz        result = self.vm.qmp('blockdev-mirror', device='src', sync='full',
11995694923aSMax Reitz                             target='dest-ro')
12005694923aSMax Reitz        self.assert_qmp(result, 'error/class', 'GenericError')
12015694923aSMax Reitz
12029592fe45SMax Reitz    def test_failing_permission_in_complete(self):
12039592fe45SMax Reitz        self.assert_no_active_block_jobs()
12049592fe45SMax Reitz
12059592fe45SMax Reitz        # Unshare consistent-read on the target
12069592fe45SMax Reitz        # (The mirror job does not care)
12079592fe45SMax Reitz        result = self.vm.qmp('blockdev-add',
12089592fe45SMax Reitz                             driver='blkdebug',
12099592fe45SMax Reitz                             node_name='dest-perm',
12109592fe45SMax Reitz                             image='dest',
12119592fe45SMax Reitz                             unshare_child_perms=['consistent-read'])
12129592fe45SMax Reitz        self.assert_qmp(result, 'return', {})
12139592fe45SMax Reitz
12149592fe45SMax Reitz        result = self.vm.qmp('blockdev-mirror', job_id='job', device='src',
12159592fe45SMax Reitz                             sync='full', target='dest',
12169592fe45SMax Reitz                             filter_node_name='mirror-filter')
12179592fe45SMax Reitz        self.assert_qmp(result, 'return', {})
12189592fe45SMax Reitz
12199592fe45SMax Reitz        # Require consistent-read on the source
12209592fe45SMax Reitz        # (We can only add this node once the job has started, or it
12219592fe45SMax Reitz        # will complain that it does not want to run on non-root nodes)
12229592fe45SMax Reitz        result = self.vm.qmp('blockdev-add',
12239592fe45SMax Reitz                             driver='blkdebug',
12249592fe45SMax Reitz                             node_name='src-perm',
12259592fe45SMax Reitz                             image='src',
12269592fe45SMax Reitz                             take_child_perms=['consistent-read'])
12279592fe45SMax Reitz        self.assert_qmp(result, 'return', {})
12289592fe45SMax Reitz
12299592fe45SMax Reitz        # While completing, mirror will attempt to replace src by
12309592fe45SMax Reitz        # dest, which must fail because src-perm requires
12319592fe45SMax Reitz        # consistent-read but dest-perm does not share it; thus
12329592fe45SMax Reitz        # aborting the job when it is supposed to complete
12339592fe45SMax Reitz        self.complete_and_wait('job',
12349592fe45SMax Reitz                               completion_error='Operation not permitted')
12359592fe45SMax Reitz
12369592fe45SMax Reitz        # Assert that all of our nodes are still there (except for the
12379592fe45SMax Reitz        # mirror filter, which should be gone despite the failure)
12389592fe45SMax Reitz        nodes = self.vm.qmp('query-named-block-nodes')['return']
12399592fe45SMax Reitz        nodes = [node['node-name'] for node in nodes]
12409592fe45SMax Reitz
12419592fe45SMax Reitz        for expect in ('src', 'src-perm', 'dest', 'dest-perm'):
12429592fe45SMax Reitz            self.assertTrue(expect in nodes, '%s disappeared' % expect)
12439592fe45SMax Reitz        self.assertFalse('mirror-filter' in nodes,
12449592fe45SMax Reitz                         'Mirror filter node did not disappear')
12459592fe45SMax Reitz
1246c45a88f4SMax Reitz# Test cases for @replaces that do not necessarily involve Quorum
1247c45a88f4SMax Reitzclass TestReplaces(iotests.QMPTestCase):
1248c45a88f4SMax Reitz    # Each of these test cases needs their own block graph, so do not
1249c45a88f4SMax Reitz    # create any nodes here
1250c45a88f4SMax Reitz    def setUp(self):
1251c45a88f4SMax Reitz        self.vm = iotests.VM()
1252c45a88f4SMax Reitz        self.vm.launch()
1253c45a88f4SMax Reitz
1254c45a88f4SMax Reitz    def tearDown(self):
1255c45a88f4SMax Reitz        self.vm.shutdown()
1256c45a88f4SMax Reitz        for img in (test_img, target_img):
1257c45a88f4SMax Reitz            try:
1258c45a88f4SMax Reitz                os.remove(img)
1259c45a88f4SMax Reitz            except OSError:
1260c45a88f4SMax Reitz                pass
1261c45a88f4SMax Reitz
1262c45a88f4SMax Reitz    @iotests.skip_if_unsupported(['copy-on-read'])
1263c45a88f4SMax Reitz    def test_replace_filter(self):
1264c45a88f4SMax Reitz        """
1265c45a88f4SMax Reitz        Check that we can replace filter nodes.
1266c45a88f4SMax Reitz        """
1267c45a88f4SMax Reitz        result = self.vm.qmp('blockdev-add', **{
1268c45a88f4SMax Reitz                                 'driver': 'copy-on-read',
1269c45a88f4SMax Reitz                                 'node-name': 'filter0',
1270c45a88f4SMax Reitz                                 'file': {
1271c45a88f4SMax Reitz                                     'driver': 'copy-on-read',
1272c45a88f4SMax Reitz                                     'node-name': 'filter1',
1273c45a88f4SMax Reitz                                     'file': {
1274c45a88f4SMax Reitz                                         'driver': 'null-co'
1275c45a88f4SMax Reitz                                     }
1276c45a88f4SMax Reitz                                 }
1277c45a88f4SMax Reitz                             })
1278c45a88f4SMax Reitz        self.assert_qmp(result, 'return', {})
1279c45a88f4SMax Reitz
1280c45a88f4SMax Reitz        result = self.vm.qmp('blockdev-add',
1281c45a88f4SMax Reitz                             node_name='target', driver='null-co')
1282c45a88f4SMax Reitz        self.assert_qmp(result, 'return', {})
1283c45a88f4SMax Reitz
1284c45a88f4SMax Reitz        result = self.vm.qmp('blockdev-mirror', job_id='mirror', device='filter0',
1285c45a88f4SMax Reitz                             target='target', sync='full', replaces='filter1')
1286c45a88f4SMax Reitz        self.assert_qmp(result, 'return', {})
1287c45a88f4SMax Reitz
1288c45a88f4SMax Reitz        self.complete_and_wait('mirror')
1289c45a88f4SMax Reitz
1290c45a88f4SMax Reitz        self.vm.assert_block_path('filter0', '/file', 'target')
1291c45a88f4SMax Reitz
1292*5f71c6b0SMax Reitz# Tests for mirror with filters (and how the mirror filter behaves, as
1293*5f71c6b0SMax Reitz# an example for an implicit filter)
1294*5f71c6b0SMax Reitzclass TestFilters(iotests.QMPTestCase):
1295*5f71c6b0SMax Reitz    def setUp(self):
1296*5f71c6b0SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M')
1297*5f71c6b0SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', backing_img, test_img)
1298*5f71c6b0SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', backing_img, target_img)
1299*5f71c6b0SMax Reitz
1300*5f71c6b0SMax Reitz        qemu_io('-c', 'write -P 1 0 512k', backing_img)
1301*5f71c6b0SMax Reitz        qemu_io('-c', 'write -P 2 512k 512k', test_img)
1302*5f71c6b0SMax Reitz
1303*5f71c6b0SMax Reitz        self.vm = iotests.VM().add_device('virtio-scsi,id=vio-scsi')
1304*5f71c6b0SMax Reitz        self.vm.launch()
1305*5f71c6b0SMax Reitz
1306*5f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-add', **{
1307*5f71c6b0SMax Reitz                                'node-name': 'target',
1308*5f71c6b0SMax Reitz                                'driver': iotests.imgfmt,
1309*5f71c6b0SMax Reitz                                'file': {
1310*5f71c6b0SMax Reitz                                    'driver': 'file',
1311*5f71c6b0SMax Reitz                                    'filename': target_img
1312*5f71c6b0SMax Reitz                                },
1313*5f71c6b0SMax Reitz                                'backing': None
1314*5f71c6b0SMax Reitz                            })
1315*5f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
1316*5f71c6b0SMax Reitz
1317*5f71c6b0SMax Reitz        self.filterless_chain = {
1318*5f71c6b0SMax Reitz                'node-name': 'source',
1319*5f71c6b0SMax Reitz                'driver': iotests.imgfmt,
1320*5f71c6b0SMax Reitz                'file': {
1321*5f71c6b0SMax Reitz                    'driver': 'file',
1322*5f71c6b0SMax Reitz                    'filename': test_img
1323*5f71c6b0SMax Reitz                },
1324*5f71c6b0SMax Reitz                'backing': {
1325*5f71c6b0SMax Reitz                    'node-name': 'backing',
1326*5f71c6b0SMax Reitz                    'driver': iotests.imgfmt,
1327*5f71c6b0SMax Reitz                    'file': {
1328*5f71c6b0SMax Reitz                        'driver': 'file',
1329*5f71c6b0SMax Reitz                        'filename': backing_img
1330*5f71c6b0SMax Reitz                    }
1331*5f71c6b0SMax Reitz                }
1332*5f71c6b0SMax Reitz            }
1333*5f71c6b0SMax Reitz
1334*5f71c6b0SMax Reitz    def tearDown(self):
1335*5f71c6b0SMax Reitz        self.vm.shutdown()
1336*5f71c6b0SMax Reitz
1337*5f71c6b0SMax Reitz        os.remove(test_img)
1338*5f71c6b0SMax Reitz        os.remove(target_img)
1339*5f71c6b0SMax Reitz        os.remove(backing_img)
1340*5f71c6b0SMax Reitz
1341*5f71c6b0SMax Reitz    def test_cor(self):
1342*5f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-add', **{
1343*5f71c6b0SMax Reitz                                'node-name': 'filter',
1344*5f71c6b0SMax Reitz                                'driver': 'copy-on-read',
1345*5f71c6b0SMax Reitz                                'file': self.filterless_chain
1346*5f71c6b0SMax Reitz                            })
1347*5f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
1348*5f71c6b0SMax Reitz
1349*5f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-mirror',
1350*5f71c6b0SMax Reitz                             job_id='mirror',
1351*5f71c6b0SMax Reitz                             device='filter',
1352*5f71c6b0SMax Reitz                             target='target',
1353*5f71c6b0SMax Reitz                             sync='top')
1354*5f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
1355*5f71c6b0SMax Reitz
1356*5f71c6b0SMax Reitz        self.complete_and_wait('mirror')
1357*5f71c6b0SMax Reitz
1358*5f71c6b0SMax Reitz        self.vm.qmp('blockdev-del', node_name='target')
1359*5f71c6b0SMax Reitz
1360*5f71c6b0SMax Reitz        target_map = qemu_img_pipe('map', '--output=json', target_img)
1361*5f71c6b0SMax Reitz        target_map = json.loads(target_map)
1362*5f71c6b0SMax Reitz
1363*5f71c6b0SMax Reitz        assert target_map[0]['start'] == 0
1364*5f71c6b0SMax Reitz        assert target_map[0]['length'] == 512 * 1024
1365*5f71c6b0SMax Reitz        assert target_map[0]['depth'] == 1
1366*5f71c6b0SMax Reitz
1367*5f71c6b0SMax Reitz        assert target_map[1]['start'] == 512 * 1024
1368*5f71c6b0SMax Reitz        assert target_map[1]['length'] == 512 * 1024
1369*5f71c6b0SMax Reitz        assert target_map[1]['depth'] == 0
1370*5f71c6b0SMax Reitz
1371*5f71c6b0SMax Reitz    def test_implicit_mirror_filter(self):
1372*5f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-add', **self.filterless_chain)
1373*5f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
1374*5f71c6b0SMax Reitz
1375*5f71c6b0SMax Reitz        # We need this so we can query from above the mirror node
1376*5f71c6b0SMax Reitz        result = self.vm.qmp('device_add',
1377*5f71c6b0SMax Reitz                             driver='scsi-hd',
1378*5f71c6b0SMax Reitz                             id='virtio',
1379*5f71c6b0SMax Reitz                             bus='vio-scsi.0',
1380*5f71c6b0SMax Reitz                             drive='source')
1381*5f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
1382*5f71c6b0SMax Reitz
1383*5f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-mirror',
1384*5f71c6b0SMax Reitz                             job_id='mirror',
1385*5f71c6b0SMax Reitz                             device='source',
1386*5f71c6b0SMax Reitz                             target='target',
1387*5f71c6b0SMax Reitz                             sync='top')
1388*5f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
1389*5f71c6b0SMax Reitz
1390*5f71c6b0SMax Reitz        # The mirror filter is now an implicit node, so it should be
1391*5f71c6b0SMax Reitz        # invisible when querying the backing chain
1392*5f71c6b0SMax Reitz        blockdevs = self.vm.qmp('query-block')['return']
1393*5f71c6b0SMax Reitz        device_info = next(dev for dev in blockdevs if dev['qdev'] == 'virtio')
1394*5f71c6b0SMax Reitz
1395*5f71c6b0SMax Reitz        assert device_info['inserted']['node-name'] == 'source'
1396*5f71c6b0SMax Reitz
1397*5f71c6b0SMax Reitz        image_info = device_info['inserted']['image']
1398*5f71c6b0SMax Reitz        assert image_info['filename'] == test_img
1399*5f71c6b0SMax Reitz        assert image_info['backing-image']['filename'] == backing_img
1400*5f71c6b0SMax Reitz
1401*5f71c6b0SMax Reitz        self.complete_and_wait('mirror')
1402*5f71c6b0SMax Reitz
1403*5f71c6b0SMax Reitz    def test_explicit_mirror_filter(self):
1404*5f71c6b0SMax Reitz        # Same test as above, but this time we give the mirror filter
1405*5f71c6b0SMax Reitz        # a node-name so it will not be invisible
1406*5f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-add', **self.filterless_chain)
1407*5f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
1408*5f71c6b0SMax Reitz
1409*5f71c6b0SMax Reitz        # We need this so we can query from above the mirror node
1410*5f71c6b0SMax Reitz        result = self.vm.qmp('device_add',
1411*5f71c6b0SMax Reitz                             driver='scsi-hd',
1412*5f71c6b0SMax Reitz                             id='virtio',
1413*5f71c6b0SMax Reitz                             bus='vio-scsi.0',
1414*5f71c6b0SMax Reitz                             drive='source')
1415*5f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
1416*5f71c6b0SMax Reitz
1417*5f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-mirror',
1418*5f71c6b0SMax Reitz                             job_id='mirror',
1419*5f71c6b0SMax Reitz                             device='source',
1420*5f71c6b0SMax Reitz                             target='target',
1421*5f71c6b0SMax Reitz                             sync='top',
1422*5f71c6b0SMax Reitz                             filter_node_name='mirror-filter')
1423*5f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
1424*5f71c6b0SMax Reitz
1425*5f71c6b0SMax Reitz        # With a node-name given to it, the mirror filter should now
1426*5f71c6b0SMax Reitz        # be visible
1427*5f71c6b0SMax Reitz        blockdevs = self.vm.qmp('query-block')['return']
1428*5f71c6b0SMax Reitz        device_info = next(dev for dev in blockdevs if dev['qdev'] == 'virtio')
1429*5f71c6b0SMax Reitz
1430*5f71c6b0SMax Reitz        assert device_info['inserted']['node-name'] == 'mirror-filter'
1431*5f71c6b0SMax Reitz
1432*5f71c6b0SMax Reitz        self.complete_and_wait('mirror')
1433*5f71c6b0SMax Reitz
1434*5f71c6b0SMax Reitz
143544c7ca5eSPaolo Bonziniif __name__ == '__main__':
1436103cbc77SMax Reitz    iotests.main(supported_fmts=['qcow2', 'qed'],
1437877d18f2SThomas Huth                 supported_protocols=['file'],
1438877d18f2SThomas Huth                 supported_platforms=['linux', 'freebsd', 'netbsd', 'openbsd'])
1439