xref: /openbmc/qemu/tests/qemu-iotests/041 (revision d5699c0d4b29e919b87049d948a30527897b7e8a)
1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3
29dd003a9SVladimir Sementsov-Ogievskiy# group: rw auto backing
344c7ca5eSPaolo Bonzini#
444c7ca5eSPaolo Bonzini# Tests for image mirroring.
544c7ca5eSPaolo Bonzini#
644c7ca5eSPaolo Bonzini# Copyright (C) 2012 Red Hat, Inc.
744c7ca5eSPaolo Bonzini#
844c7ca5eSPaolo Bonzini# This program is free software; you can redistribute it and/or modify
944c7ca5eSPaolo Bonzini# it under the terms of the GNU General Public License as published by
1044c7ca5eSPaolo Bonzini# the Free Software Foundation; either version 2 of the License, or
1144c7ca5eSPaolo Bonzini# (at your option) any later version.
1244c7ca5eSPaolo Bonzini#
1344c7ca5eSPaolo Bonzini# This program is distributed in the hope that it will be useful,
1444c7ca5eSPaolo Bonzini# but WITHOUT ANY WARRANTY; without even the implied warranty of
1544c7ca5eSPaolo Bonzini# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1644c7ca5eSPaolo Bonzini# GNU General Public License for more details.
1744c7ca5eSPaolo Bonzini#
1844c7ca5eSPaolo Bonzini# You should have received a copy of the GNU General Public License
1944c7ca5eSPaolo Bonzini# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2044c7ca5eSPaolo Bonzini#
2144c7ca5eSPaolo Bonzini
2244c7ca5eSPaolo Bonziniimport time
2344c7ca5eSPaolo Bonziniimport os
24a1da1878SMax Reitzimport re
255f71c6b0SMax Reitzimport json
2644c7ca5eSPaolo Bonziniimport iotests
271670ae7aSJohn Snowfrom iotests import qemu_img, qemu_img_map, qemu_io
2844c7ca5eSPaolo Bonzini
2944c7ca5eSPaolo Bonzinibacking_img = os.path.join(iotests.test_dir, 'backing.img')
3044c7ca5eSPaolo Bonzinitarget_backing_img = os.path.join(iotests.test_dir, 'target-backing.img')
3144c7ca5eSPaolo Bonzinitest_img = os.path.join(iotests.test_dir, 'test.img')
3244c7ca5eSPaolo Bonzinitarget_img = os.path.join(iotests.test_dir, 'target.img')
3344c7ca5eSPaolo Bonzini
34d88964aeSBenoît Canetquorum_img1 = os.path.join(iotests.test_dir, 'quorum1.img')
35d88964aeSBenoît Canetquorum_img2 = os.path.join(iotests.test_dir, 'quorum2.img')
36d88964aeSBenoît Canetquorum_img3 = os.path.join(iotests.test_dir, 'quorum3.img')
37d88964aeSBenoît Canetquorum_repair_img = os.path.join(iotests.test_dir, 'quorum_repair.img')
38d88964aeSBenoît Canetquorum_snapshot_file = os.path.join(iotests.test_dir, 'quorum_snapshot.img')
39d88964aeSBenoît Canet
40e5ac52d8SMax Reitznbd_sock_path = os.path.join(iotests.sock_dir, 'nbd.sock')
41a1da1878SMax Reitz
42866323f3SFam Zhengclass TestSingleDrive(iotests.QMPTestCase):
4344c7ca5eSPaolo Bonzini    image_len = 1 * 1024 * 1024 # MB
4494ca2c73SFam Zheng    qmp_cmd = 'drive-mirror'
4594ca2c73SFam Zheng    qmp_target = target_img
4644c7ca5eSPaolo Bonzini
4744c7ca5eSPaolo Bonzini    def setUp(self):
483b9f27d2SFam Zheng        iotests.create_image(backing_img, self.image_len)
49b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
50b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
51d3c8c674SKevin Wolf        self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=base")
520ed82f7aSMax Reitz        if iotests.qemu_default_machine == 'pc':
530ed82f7aSMax Reitz            self.vm.add_drive(None, 'media=cdrom', 'ide')
5444c7ca5eSPaolo Bonzini        self.vm.launch()
5544c7ca5eSPaolo Bonzini
5644c7ca5eSPaolo Bonzini    def tearDown(self):
5744c7ca5eSPaolo Bonzini        self.vm.shutdown()
5844c7ca5eSPaolo Bonzini        os.remove(test_img)
5944c7ca5eSPaolo Bonzini        os.remove(backing_img)
6044c7ca5eSPaolo Bonzini        try:
6144c7ca5eSPaolo Bonzini            os.remove(target_img)
6244c7ca5eSPaolo Bonzini        except OSError:
6344c7ca5eSPaolo Bonzini            pass
6444c7ca5eSPaolo Bonzini
6544c7ca5eSPaolo Bonzini    def test_complete(self):
66ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6744c7ca5eSPaolo Bonzini
6894ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
6994ca2c73SFam Zheng                             target=self.qmp_target)
7044c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
7144c7ca5eSPaolo Bonzini
7244c7ca5eSPaolo Bonzini        self.complete_and_wait()
7344c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
7444c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
7544c7ca5eSPaolo Bonzini        self.vm.shutdown()
763a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
7744c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
7844c7ca5eSPaolo Bonzini
7944c7ca5eSPaolo Bonzini    def test_cancel(self):
80ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
8144c7ca5eSPaolo Bonzini
8294ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
8394ca2c73SFam Zheng                             target=self.qmp_target)
8444c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
8544c7ca5eSPaolo Bonzini
862575fe16SStefan Hajnoczi        self.cancel_and_wait(force=True)
8744c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
8844c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
8944c7ca5eSPaolo Bonzini
9044c7ca5eSPaolo Bonzini    def test_cancel_after_ready(self):
91ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
9244c7ca5eSPaolo Bonzini
9394ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
9494ca2c73SFam Zheng                             target=self.qmp_target)
9544c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
9644c7ca5eSPaolo Bonzini
972575fe16SStefan Hajnoczi        self.wait_ready_and_cancel()
9844c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
9944c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
10044c7ca5eSPaolo Bonzini        self.vm.shutdown()
1013a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
10244c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
10344c7ca5eSPaolo Bonzini
10444c7ca5eSPaolo Bonzini    def test_pause(self):
105ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
10644c7ca5eSPaolo Bonzini
10794ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
10894ca2c73SFam Zheng                             target=self.qmp_target)
10944c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
11044c7ca5eSPaolo Bonzini
1112c93c5cbSKevin Wolf        self.pause_job('drive0')
11244c7ca5eSPaolo Bonzini
11344c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
11444c7ca5eSPaolo Bonzini        offset = self.dictpath(result, 'return[0]/offset')
11544c7ca5eSPaolo Bonzini
1162c93c5cbSKevin Wolf        time.sleep(0.5)
11744c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
11844c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/offset', offset)
11944c7ca5eSPaolo Bonzini
12044c7ca5eSPaolo Bonzini        result = self.vm.qmp('block-job-resume', device='drive0')
12144c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
12244c7ca5eSPaolo Bonzini
12344c7ca5eSPaolo Bonzini        self.complete_and_wait()
12444c7ca5eSPaolo Bonzini        self.vm.shutdown()
1253a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
12644c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
12744c7ca5eSPaolo Bonzini
12808e4ed6cSPaolo Bonzini    def test_small_buffer(self):
129ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
13008e4ed6cSPaolo Bonzini
13108e4ed6cSPaolo Bonzini        # A small buffer is rounded up automatically
13294ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
13394ca2c73SFam Zheng                             buf_size=4096, target=self.qmp_target)
13408e4ed6cSPaolo Bonzini        self.assert_qmp(result, 'return', {})
13508e4ed6cSPaolo Bonzini
13608e4ed6cSPaolo Bonzini        self.complete_and_wait()
13708e4ed6cSPaolo Bonzini        result = self.vm.qmp('query-block')
13808e4ed6cSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
13908e4ed6cSPaolo Bonzini        self.vm.shutdown()
1403a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
14108e4ed6cSPaolo Bonzini                        'target image does not match source after mirroring')
14208e4ed6cSPaolo Bonzini
14308e4ed6cSPaolo Bonzini    def test_small_buffer2(self):
144ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
14508e4ed6cSPaolo Bonzini
14608e4ed6cSPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,size=%d'
1473b9f27d2SFam Zheng                        % (self.image_len, self.image_len), target_img)
14894ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
14994ca2c73SFam Zheng                             buf_size=65536, mode='existing', target=self.qmp_target)
15008e4ed6cSPaolo Bonzini        self.assert_qmp(result, 'return', {})
15108e4ed6cSPaolo Bonzini
15208e4ed6cSPaolo Bonzini        self.complete_and_wait()
15308e4ed6cSPaolo Bonzini        result = self.vm.qmp('query-block')
15408e4ed6cSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
15508e4ed6cSPaolo Bonzini        self.vm.shutdown()
1563a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
15708e4ed6cSPaolo Bonzini                        'target image does not match source after mirroring')
15808e4ed6cSPaolo Bonzini
15944c7ca5eSPaolo Bonzini    def test_large_cluster(self):
160ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
16144c7ca5eSPaolo Bonzini
16244c7ca5eSPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
163b66ff2c2SEric Blake                        % (self.image_len, backing_img),
164b66ff2c2SEric Blake                 '-F', 'raw', target_img)
16594ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
16694ca2c73SFam Zheng                             mode='existing', target=self.qmp_target)
16744c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
16844c7ca5eSPaolo Bonzini
16944c7ca5eSPaolo Bonzini        self.complete_and_wait()
17044c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
17144c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
17244c7ca5eSPaolo Bonzini        self.vm.shutdown()
1733a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
17444c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
17544c7ca5eSPaolo Bonzini
176d3c8c674SKevin Wolf    # Tests that the insertion of the mirror_top filter node doesn't make a
177d3c8c674SKevin Wolf    # difference to query-block
178d3c8c674SKevin Wolf    def test_implicit_node(self):
179d3c8c674SKevin Wolf        self.assert_no_active_block_jobs()
180d3c8c674SKevin Wolf
181d3c8c674SKevin Wolf        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
182d3c8c674SKevin Wolf                             target=self.qmp_target)
183d3c8c674SKevin Wolf        self.assert_qmp(result, 'return', {})
184d3c8c674SKevin Wolf
185d3c8c674SKevin Wolf        result = self.vm.qmp('query-block')
186d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
187d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
188d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img)
189d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1)
190d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
191d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img)
192d3c8c674SKevin Wolf
193d3c8c674SKevin Wolf        result = self.vm.qmp('query-blockstats')
194d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/node-name', 'top')
195d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/backing/node-name', 'base')
196d3c8c674SKevin Wolf
197d3c8c674SKevin Wolf        self.cancel_and_wait(force=True)
198d3c8c674SKevin Wolf        result = self.vm.qmp('query-block')
199d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
200d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt)
201d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img)
202d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1)
203d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img)
204d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img)
205d3c8c674SKevin Wolf
206d3c8c674SKevin Wolf        result = self.vm.qmp('query-blockstats')
207d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/node-name', 'top')
208d3c8c674SKevin Wolf        self.assert_qmp(result, 'return[0]/backing/node-name', 'base')
209d3c8c674SKevin Wolf
21044c7ca5eSPaolo Bonzini    def test_medium_not_found(self):
211d8683155SBo Tu        if iotests.qemu_default_machine != 'pc':
212d8683155SBo Tu            return
213d8683155SBo Tu
21494ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='ide1-cd0', sync='full',
21594ca2c73SFam Zheng                             target=self.qmp_target)
2160524e93aSKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
21744c7ca5eSPaolo Bonzini
21844c7ca5eSPaolo Bonzini    def test_image_not_found(self):
21994ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
22094ca2c73SFam Zheng                             mode='existing', target=self.qmp_target)
22144c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'error/class', 'GenericError')
22244c7ca5eSPaolo Bonzini
22344c7ca5eSPaolo Bonzini    def test_device_not_found(self):
22494ca2c73SFam Zheng        result = self.vm.qmp(self.qmp_cmd, device='nonexistent', sync='full',
22594ca2c73SFam Zheng                             target=self.qmp_target)
2260524e93aSKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
22794ca2c73SFam Zheng
22894ca2c73SFam Zhengclass TestSingleBlockdev(TestSingleDrive):
22994ca2c73SFam Zheng    qmp_cmd = 'blockdev-mirror'
23094ca2c73SFam Zheng    qmp_target = 'node1'
23194ca2c73SFam Zheng
23294ca2c73SFam Zheng    def setUp(self):
23394ca2c73SFam Zheng        TestSingleDrive.setUp(self)
234b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
235b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
2360153d2f5SKevin Wolf        args = {'driver': iotests.imgfmt,
23794ca2c73SFam Zheng                'node-name': self.qmp_target,
2380153d2f5SKevin Wolf                'file': { 'filename': target_img, 'driver': 'file' } }
23994ca2c73SFam Zheng        result = self.vm.qmp("blockdev-add", **args)
24094ca2c73SFam Zheng        self.assert_qmp(result, 'return', {})
24194ca2c73SFam Zheng
24286fae10cSKevin Wolf    def test_mirror_to_self(self):
24386fae10cSKevin Wolf        result = self.vm.qmp(self.qmp_cmd, job_id='job0',
24486fae10cSKevin Wolf                             device=self.qmp_target, sync='full',
24586fae10cSKevin Wolf                             target=self.qmp_target)
24686fae10cSKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
24786fae10cSKevin Wolf
24816cea4eeSKevin Wolf    def do_test_resize(self, device, node):
24916cea4eeSKevin Wolf        def pre_finalize():
25016cea4eeSKevin Wolf            if device:
25116cea4eeSKevin Wolf                result = self.vm.qmp('block_resize', device=device, size=65536)
25216cea4eeSKevin Wolf                self.assert_qmp(result, 'error/class', 'GenericError')
25316cea4eeSKevin Wolf
25416cea4eeSKevin Wolf            result = self.vm.qmp('block_resize', node_name=node, size=65536)
25516cea4eeSKevin Wolf            self.assert_qmp(result, 'error/class', 'GenericError')
25616cea4eeSKevin Wolf
25716cea4eeSKevin Wolf        result = self.vm.qmp(self.qmp_cmd, job_id='job0', device='drive0',
25816cea4eeSKevin Wolf                             sync='full', target=self.qmp_target,
25916cea4eeSKevin Wolf                             auto_finalize=False, auto_dismiss=False)
26016cea4eeSKevin Wolf        self.assert_qmp(result, 'return', {})
26116cea4eeSKevin Wolf
26216cea4eeSKevin Wolf        result = self.vm.run_job('job0', auto_finalize=False,
26316cea4eeSKevin Wolf                                 pre_finalize=pre_finalize)
26416cea4eeSKevin Wolf        self.assertEqual(result, None)
26516cea4eeSKevin Wolf
26616cea4eeSKevin Wolf    def test_source_resize(self):
26716cea4eeSKevin Wolf        self.do_test_resize('drive0', 'top')
26816cea4eeSKevin Wolf
26916cea4eeSKevin Wolf    def test_target_resize(self):
27016cea4eeSKevin Wolf        self.do_test_resize(None, self.qmp_target)
27116cea4eeSKevin Wolf
27216cea4eeSKevin Wolf    def do_test_target_size(self, size):
27316cea4eeSKevin Wolf        result = self.vm.qmp('block_resize', node_name=self.qmp_target,
27416cea4eeSKevin Wolf                             size=size)
27516cea4eeSKevin Wolf        self.assert_qmp(result, 'return', {})
27616cea4eeSKevin Wolf
27716cea4eeSKevin Wolf        result = self.vm.qmp(self.qmp_cmd, job_id='job0',
27816cea4eeSKevin Wolf                             device='drive0', sync='full', auto_dismiss=False,
27916cea4eeSKevin Wolf                             target=self.qmp_target)
28016cea4eeSKevin Wolf        self.assert_qmp(result, 'return', {})
28116cea4eeSKevin Wolf
28216cea4eeSKevin Wolf        result = self.vm.run_job('job0')
28316cea4eeSKevin Wolf        self.assertEqual(result, 'Source and target image have different sizes')
28416cea4eeSKevin Wolf
285c7070942SMax Reitz    # qed does not support shrinking
286c7070942SMax Reitz    @iotests.skip_for_formats(('qed'))
28716cea4eeSKevin Wolf    def test_small_target(self):
28816cea4eeSKevin Wolf        self.do_test_target_size(self.image_len // 2)
28916cea4eeSKevin Wolf
29016cea4eeSKevin Wolf    def test_large_target(self):
29116cea4eeSKevin Wolf        self.do_test_target_size(self.image_len * 2)
29216cea4eeSKevin Wolf
29394ca2c73SFam Zheng    test_large_cluster = None
29494ca2c73SFam Zheng    test_image_not_found = None
29594ca2c73SFam Zheng    test_small_buffer2 = None
29694ca2c73SFam Zheng
2973b9f27d2SFam Zhengclass TestSingleDriveZeroLength(TestSingleDrive):
2983b9f27d2SFam Zheng    image_len = 0
2993b9f27d2SFam Zheng    test_small_buffer2 = None
3003b9f27d2SFam Zheng    test_large_cluster = None
3013b9f27d2SFam Zheng
30294ca2c73SFam Zhengclass TestSingleBlockdevZeroLength(TestSingleBlockdev):
30394ca2c73SFam Zheng    image_len = 0
30416cea4eeSKevin Wolf    test_small_target = None
30516cea4eeSKevin Wolf    test_large_target = None
30694ca2c73SFam Zheng
3075a0f6fd5SKevin Wolfclass TestSingleDriveUnalignedLength(TestSingleDrive):
3085a0f6fd5SKevin Wolf    image_len = 1025 * 1024
3095a0f6fd5SKevin Wolf    test_small_buffer2 = None
3105a0f6fd5SKevin Wolf    test_large_cluster = None
3115a0f6fd5SKevin Wolf
31294ca2c73SFam Zhengclass TestSingleBlockdevUnalignedLength(TestSingleBlockdev):
31394ca2c73SFam Zheng    image_len = 1025 * 1024
31494ca2c73SFam Zheng
315866323f3SFam Zhengclass TestMirrorNoBacking(iotests.QMPTestCase):
31644c7ca5eSPaolo Bonzini    image_len = 2 * 1024 * 1024 # MB
31744c7ca5eSPaolo Bonzini
31844c7ca5eSPaolo Bonzini    def setUp(self):
3192499a096SStefan Hajnoczi        iotests.create_image(backing_img, TestMirrorNoBacking.image_len)
320b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
321b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
32244c7ca5eSPaolo Bonzini        self.vm = iotests.VM().add_drive(test_img)
32344c7ca5eSPaolo Bonzini        self.vm.launch()
32444c7ca5eSPaolo Bonzini
32544c7ca5eSPaolo Bonzini    def tearDown(self):
32644c7ca5eSPaolo Bonzini        self.vm.shutdown()
32744c7ca5eSPaolo Bonzini        os.remove(test_img)
32844c7ca5eSPaolo Bonzini        os.remove(backing_img)
329866323f3SFam Zheng        try:
33044c7ca5eSPaolo Bonzini            os.remove(target_backing_img)
331866323f3SFam Zheng        except:
332866323f3SFam Zheng            pass
33344c7ca5eSPaolo Bonzini        os.remove(target_img)
33444c7ca5eSPaolo Bonzini
33544c7ca5eSPaolo Bonzini    def test_complete(self):
336ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
33744c7ca5eSPaolo Bonzini
338b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
339b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
34044c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
34144c7ca5eSPaolo Bonzini                             mode='existing', target=target_img)
34244c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
34344c7ca5eSPaolo Bonzini
34444c7ca5eSPaolo Bonzini        self.complete_and_wait()
34544c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
34644c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
34744c7ca5eSPaolo Bonzini        self.vm.shutdown()
348866323f3SFam Zheng        self.assertTrue(iotests.compare_images(test_img, target_img),
34944c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
35044c7ca5eSPaolo Bonzini
35144c7ca5eSPaolo Bonzini    def test_cancel(self):
352ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
35344c7ca5eSPaolo Bonzini
354b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
355b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', target_img)
35644c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
35744c7ca5eSPaolo Bonzini                             mode='existing', target=target_img)
35844c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
35944c7ca5eSPaolo Bonzini
3602575fe16SStefan Hajnoczi        self.wait_ready_and_cancel()
36144c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block')
36244c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
36344c7ca5eSPaolo Bonzini        self.vm.shutdown()
364866323f3SFam Zheng        self.assertTrue(iotests.compare_images(test_img, target_img),
36544c7ca5eSPaolo Bonzini                        'target image does not match source after mirroring')
36644c7ca5eSPaolo Bonzini
367b812f671SPaolo Bonzini    def test_large_cluster(self):
368ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
369b812f671SPaolo Bonzini
370b812f671SPaolo Bonzini        # qemu-img create fails if the image is not there
371b812f671SPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt, '-o', 'size=%d'
372b812f671SPaolo Bonzini                        %(TestMirrorNoBacking.image_len), target_backing_img)
373b812f671SPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
374b66ff2c2SEric Blake                        % (TestMirrorNoBacking.image_len, target_backing_img),
375b66ff2c2SEric Blake                 '-F', iotests.imgfmt, target_img)
376b812f671SPaolo Bonzini
377b812f671SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
378b812f671SPaolo Bonzini                             mode='existing', target=target_img)
379b812f671SPaolo Bonzini        self.assert_qmp(result, 'return', {})
380b812f671SPaolo Bonzini
381b812f671SPaolo Bonzini        self.complete_and_wait()
382b812f671SPaolo Bonzini        result = self.vm.qmp('query-block')
383b812f671SPaolo Bonzini        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
384b812f671SPaolo Bonzini        self.vm.shutdown()
385866323f3SFam Zheng        self.assertTrue(iotests.compare_images(test_img, target_img),
386b812f671SPaolo Bonzini                        'target image does not match source after mirroring')
387b812f671SPaolo Bonzini
388866323f3SFam Zhengclass TestMirrorResized(iotests.QMPTestCase):
389a04eca10SVishvananda Ishaya    backing_len = 1 * 1024 * 1024 # MB
390a04eca10SVishvananda Ishaya    image_len = 2 * 1024 * 1024 # MB
391a04eca10SVishvananda Ishaya
392a04eca10SVishvananda Ishaya    def setUp(self):
3932499a096SStefan Hajnoczi        iotests.create_image(backing_img, TestMirrorResized.backing_len)
394b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
395b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
396a04eca10SVishvananda Ishaya        qemu_img('resize', test_img, '2M')
397a04eca10SVishvananda Ishaya        self.vm = iotests.VM().add_drive(test_img)
398a04eca10SVishvananda Ishaya        self.vm.launch()
399a04eca10SVishvananda Ishaya
400a04eca10SVishvananda Ishaya    def tearDown(self):
401a04eca10SVishvananda Ishaya        self.vm.shutdown()
402a04eca10SVishvananda Ishaya        os.remove(test_img)
403a04eca10SVishvananda Ishaya        os.remove(backing_img)
404a04eca10SVishvananda Ishaya        try:
405a04eca10SVishvananda Ishaya            os.remove(target_img)
406a04eca10SVishvananda Ishaya        except OSError:
407a04eca10SVishvananda Ishaya            pass
408a04eca10SVishvananda Ishaya
409a04eca10SVishvananda Ishaya    def test_complete_top(self):
410ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
411a04eca10SVishvananda Ishaya
412a04eca10SVishvananda Ishaya        result = self.vm.qmp('drive-mirror', device='drive0', sync='top',
413a04eca10SVishvananda Ishaya                             target=target_img)
414a04eca10SVishvananda Ishaya        self.assert_qmp(result, 'return', {})
415a04eca10SVishvananda Ishaya
416a04eca10SVishvananda Ishaya        self.complete_and_wait()
417a04eca10SVishvananda Ishaya        result = self.vm.qmp('query-block')
418a04eca10SVishvananda Ishaya        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
419a04eca10SVishvananda Ishaya        self.vm.shutdown()
4203a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
421a04eca10SVishvananda Ishaya                        'target image does not match source after mirroring')
422a04eca10SVishvananda Ishaya
423a04eca10SVishvananda Ishaya    def test_complete_full(self):
424ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
425a04eca10SVishvananda Ishaya
426a04eca10SVishvananda Ishaya        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
427a04eca10SVishvananda Ishaya                             target=target_img)
428a04eca10SVishvananda Ishaya        self.assert_qmp(result, 'return', {})
429a04eca10SVishvananda Ishaya
430a04eca10SVishvananda Ishaya        self.complete_and_wait()
431a04eca10SVishvananda Ishaya        result = self.vm.qmp('query-block')
432a04eca10SVishvananda Ishaya        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
433a04eca10SVishvananda Ishaya        self.vm.shutdown()
4343a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
435a04eca10SVishvananda Ishaya                        'target image does not match source after mirroring')
436a04eca10SVishvananda Ishaya
437866323f3SFam Zhengclass TestReadErrors(iotests.QMPTestCase):
4389dfa9f59SPaolo Bonzini    image_len = 2 * 1024 * 1024 # MB
4399dfa9f59SPaolo Bonzini
4409dfa9f59SPaolo Bonzini    # this should be a multiple of twice the default granularity
4419dfa9f59SPaolo Bonzini    # so that we hit this offset first in state 1
4429dfa9f59SPaolo Bonzini    MIRROR_GRANULARITY = 1024 * 1024
4439dfa9f59SPaolo Bonzini
4449dfa9f59SPaolo Bonzini    def create_blkdebug_file(self, name, event, errno):
4459dfa9f59SPaolo Bonzini        file = open(name, 'w')
4469dfa9f59SPaolo Bonzini        file.write('''
4479dfa9f59SPaolo Bonzini[inject-error]
4489dfa9f59SPaolo Bonzinistate = "1"
4499dfa9f59SPaolo Bonzinievent = "%s"
4509dfa9f59SPaolo Bonzinierrno = "%d"
4519dfa9f59SPaolo Bonziniimmediately = "off"
4529dfa9f59SPaolo Bonzinionce = "on"
4539dfa9f59SPaolo Bonzinisector = "%d"
4549dfa9f59SPaolo Bonzini
4559dfa9f59SPaolo Bonzini[set-state]
4569dfa9f59SPaolo Bonzinistate = "1"
4579dfa9f59SPaolo Bonzinievent = "%s"
4589dfa9f59SPaolo Bonzininew_state = "2"
4599dfa9f59SPaolo Bonzini
4609dfa9f59SPaolo Bonzini[set-state]
4619dfa9f59SPaolo Bonzinistate = "2"
4629dfa9f59SPaolo Bonzinievent = "%s"
4639dfa9f59SPaolo Bonzininew_state = "1"
4649a3a9a63SMax Reitz''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event))
4659dfa9f59SPaolo Bonzini        file.close()
4669dfa9f59SPaolo Bonzini
4679dfa9f59SPaolo Bonzini    def setUp(self):
4689dfa9f59SPaolo Bonzini        self.blkdebug_file = backing_img + ".blkdebug"
4692499a096SStefan Hajnoczi        iotests.create_image(backing_img, TestReadErrors.image_len)
4709dfa9f59SPaolo Bonzini        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
4719dfa9f59SPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt,
4729dfa9f59SPaolo Bonzini                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
4739dfa9f59SPaolo Bonzini                       % (self.blkdebug_file, backing_img),
4749dfa9f59SPaolo Bonzini                 test_img)
475b812f671SPaolo Bonzini        # Write something for tests that use sync='top'
476b812f671SPaolo Bonzini        qemu_io('-c', 'write %d 512' % (self.MIRROR_GRANULARITY + 65536),
477b812f671SPaolo Bonzini                        test_img)
4789dfa9f59SPaolo Bonzini        self.vm = iotests.VM().add_drive(test_img)
4799dfa9f59SPaolo Bonzini        self.vm.launch()
4809dfa9f59SPaolo Bonzini
4819dfa9f59SPaolo Bonzini    def tearDown(self):
4829dfa9f59SPaolo Bonzini        self.vm.shutdown()
4839dfa9f59SPaolo Bonzini        os.remove(test_img)
484db11d1eeSKevin Wolf        os.remove(target_img)
4859dfa9f59SPaolo Bonzini        os.remove(backing_img)
4869dfa9f59SPaolo Bonzini        os.remove(self.blkdebug_file)
4879dfa9f59SPaolo Bonzini
4889dfa9f59SPaolo Bonzini    def test_report_read(self):
489ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
4909dfa9f59SPaolo Bonzini
4919dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
4929dfa9f59SPaolo Bonzini                             target=target_img)
4939dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
4949dfa9f59SPaolo Bonzini
4959dfa9f59SPaolo Bonzini        completed = False
4969dfa9f59SPaolo Bonzini        error = False
4979dfa9f59SPaolo Bonzini        while not completed:
4989dfa9f59SPaolo Bonzini            for event in self.vm.get_qmp_events(wait=True):
4999dfa9f59SPaolo Bonzini                if event['event'] == 'BLOCK_JOB_ERROR':
5009dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
5019dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/operation', 'read')
5029dfa9f59SPaolo Bonzini                    error = True
5039dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_READY':
5049dfa9f59SPaolo Bonzini                    self.assertTrue(False, 'job completed unexpectedly')
5059dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_COMPLETED':
5069dfa9f59SPaolo Bonzini                    self.assertTrue(error, 'job completed unexpectedly')
5079dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/type', 'mirror')
5089dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
5099dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/error', 'Input/output error')
5109dfa9f59SPaolo Bonzini                    completed = True
5111dac83f1SKevin Wolf                elif event['event'] == 'JOB_STATUS_CHANGE':
5121dac83f1SKevin Wolf                    self.assert_qmp(event, 'data/id', 'drive0')
5139dfa9f59SPaolo Bonzini
514ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
5159dfa9f59SPaolo Bonzini
5169dfa9f59SPaolo Bonzini    def test_ignore_read(self):
517ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
5189dfa9f59SPaolo Bonzini
5199dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
5209dfa9f59SPaolo Bonzini                             target=target_img, on_source_error='ignore')
5219dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
5229dfa9f59SPaolo Bonzini
5239dfa9f59SPaolo Bonzini        event = self.vm.get_qmp_event(wait=True)
5241dac83f1SKevin Wolf        while event['event'] == 'JOB_STATUS_CHANGE':
5251dac83f1SKevin Wolf            self.assert_qmp(event, 'data/id', 'drive0')
5261dac83f1SKevin Wolf            event = self.vm.get_qmp_event(wait=True)
5271dac83f1SKevin Wolf
528fa1cfb40SKevin Wolf        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
5299dfa9f59SPaolo Bonzini        self.assert_qmp(event, 'data/device', 'drive0')
5309dfa9f59SPaolo Bonzini        self.assert_qmp(event, 'data/operation', 'read')
5319dfa9f59SPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
532*d5699c0dSHanna Reitz        self.assertIn(result['return'][0]['status'], ['running', 'ready'])
5339dfa9f59SPaolo Bonzini        self.complete_and_wait()
5349dfa9f59SPaolo Bonzini
535b812f671SPaolo Bonzini    def test_large_cluster(self):
536ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
537b812f671SPaolo Bonzini
538b812f671SPaolo Bonzini        # Test COW into the target image.  The first half of the
539b812f671SPaolo Bonzini        # cluster at MIRROR_GRANULARITY has to be copied from
540b812f671SPaolo Bonzini        # backing_img, even though sync='top'.
541b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
542b66ff2c2SEric Blake                 '-ocluster_size=131072,backing_file=%s' %(backing_img),
543b66ff2c2SEric Blake                 '-F', 'raw', target_img)
544b812f671SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='top',
545b812f671SPaolo Bonzini                             on_source_error='ignore',
546b812f671SPaolo Bonzini                             mode='existing', target=target_img)
547b812f671SPaolo Bonzini        self.assert_qmp(result, 'return', {})
548b812f671SPaolo Bonzini
549b812f671SPaolo Bonzini        event = self.vm.get_qmp_event(wait=True)
5501dac83f1SKevin Wolf        while event['event'] == 'JOB_STATUS_CHANGE':
5511dac83f1SKevin Wolf            self.assert_qmp(event, 'data/id', 'drive0')
5521dac83f1SKevin Wolf            event = self.vm.get_qmp_event(wait=True)
5531dac83f1SKevin Wolf
554fa1cfb40SKevin Wolf        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
555b812f671SPaolo Bonzini        self.assert_qmp(event, 'data/device', 'drive0')
556b812f671SPaolo Bonzini        self.assert_qmp(event, 'data/operation', 'read')
557b812f671SPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
558*d5699c0dSHanna Reitz        self.assertIn(result['return'][0]['status'], ['running', 'ready'])
559b812f671SPaolo Bonzini        self.complete_and_wait()
560b812f671SPaolo Bonzini        self.vm.shutdown()
561b812f671SPaolo Bonzini
562b812f671SPaolo Bonzini        # Detach blkdebug to compare images successfully
563b66ff2c2SEric Blake        qemu_img('rebase', '-f', iotests.imgfmt, '-u', '-b', backing_img,
564b66ff2c2SEric Blake                 '-F', 'raw', test_img)
5653a3918c3SStefan Hajnoczi        self.assertTrue(iotests.compare_images(test_img, target_img),
566b812f671SPaolo Bonzini                        'target image does not match source after mirroring')
567b812f671SPaolo Bonzini
5689dfa9f59SPaolo Bonzini    def test_stop_read(self):
569ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
5709dfa9f59SPaolo Bonzini
5719dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
5729dfa9f59SPaolo Bonzini                             target=target_img, on_source_error='stop')
5739dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
5749dfa9f59SPaolo Bonzini
5759dfa9f59SPaolo Bonzini        error = False
5769dfa9f59SPaolo Bonzini        ready = False
5779dfa9f59SPaolo Bonzini        while not ready:
5789dfa9f59SPaolo Bonzini            for event in self.vm.get_qmp_events(wait=True):
5799dfa9f59SPaolo Bonzini                if event['event'] == 'BLOCK_JOB_ERROR':
5809dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
5819dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/operation', 'read')
5829dfa9f59SPaolo Bonzini
583*d5699c0dSHanna Reitz                    if self.vm.qmp('query-block-jobs')['return'][0]['status'] != 'paused':
584*d5699c0dSHanna Reitz                        self.vm.events_wait([(
585*d5699c0dSHanna Reitz                            'JOB_STATUS_CHANGE',
586*d5699c0dSHanna Reitz                            {'data': {'id': 'drive0', 'status': 'paused'}}
587*d5699c0dSHanna Reitz                        )])
588*d5699c0dSHanna Reitz
5899dfa9f59SPaolo Bonzini                    result = self.vm.qmp('query-block-jobs')
590*d5699c0dSHanna Reitz                    self.assert_qmp(result, 'return[0]/status', 'paused')
5919dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
5929dfa9f59SPaolo Bonzini
5939dfa9f59SPaolo Bonzini                    result = self.vm.qmp('block-job-resume', device='drive0')
5949dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return', {})
5959dfa9f59SPaolo Bonzini                    error = True
5969dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_READY':
5979dfa9f59SPaolo Bonzini                    self.assertTrue(error, 'job completed unexpectedly')
5989dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
5999dfa9f59SPaolo Bonzini                    ready = True
6009dfa9f59SPaolo Bonzini
6019dfa9f59SPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
602*d5699c0dSHanna Reitz        self.assert_qmp(result, 'return[0]/status', 'ready')
6039dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return[0]/io-status', 'ok')
6049dfa9f59SPaolo Bonzini
6059dfa9f59SPaolo Bonzini        self.complete_and_wait(wait_ready=False)
606ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6079dfa9f59SPaolo Bonzini
608866323f3SFam Zhengclass TestWriteErrors(iotests.QMPTestCase):
6099dfa9f59SPaolo Bonzini    image_len = 2 * 1024 * 1024 # MB
6109dfa9f59SPaolo Bonzini
6119dfa9f59SPaolo Bonzini    # this should be a multiple of twice the default granularity
6129dfa9f59SPaolo Bonzini    # so that we hit this offset first in state 1
6139dfa9f59SPaolo Bonzini    MIRROR_GRANULARITY = 1024 * 1024
6149dfa9f59SPaolo Bonzini
6159dfa9f59SPaolo Bonzini    def create_blkdebug_file(self, name, event, errno):
6169dfa9f59SPaolo Bonzini        file = open(name, 'w')
6179dfa9f59SPaolo Bonzini        file.write('''
6189dfa9f59SPaolo Bonzini[inject-error]
6199dfa9f59SPaolo Bonzinistate = "1"
6209dfa9f59SPaolo Bonzinievent = "%s"
6219dfa9f59SPaolo Bonzinierrno = "%d"
6229dfa9f59SPaolo Bonziniimmediately = "off"
6239dfa9f59SPaolo Bonzinionce = "on"
6249dfa9f59SPaolo Bonzinisector = "%d"
6259dfa9f59SPaolo Bonzini
6269dfa9f59SPaolo Bonzini[set-state]
6279dfa9f59SPaolo Bonzinistate = "1"
6289dfa9f59SPaolo Bonzinievent = "%s"
6299dfa9f59SPaolo Bonzininew_state = "2"
6309dfa9f59SPaolo Bonzini
6319dfa9f59SPaolo Bonzini[set-state]
6329dfa9f59SPaolo Bonzinistate = "2"
6339dfa9f59SPaolo Bonzinievent = "%s"
6349dfa9f59SPaolo Bonzininew_state = "1"
6359a3a9a63SMax Reitz''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event))
6369dfa9f59SPaolo Bonzini        file.close()
6379dfa9f59SPaolo Bonzini
6389dfa9f59SPaolo Bonzini    def setUp(self):
6399dfa9f59SPaolo Bonzini        self.blkdebug_file = target_img + ".blkdebug"
6402499a096SStefan Hajnoczi        iotests.create_image(backing_img, TestWriteErrors.image_len)
6419dfa9f59SPaolo Bonzini        self.create_blkdebug_file(self.blkdebug_file, "write_aio", 5)
642b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
643b66ff2c2SEric Blake                 '-obacking_file=%s' %(backing_img), '-F', 'raw', test_img)
6449dfa9f59SPaolo Bonzini        self.vm = iotests.VM().add_drive(test_img)
6459dfa9f59SPaolo Bonzini        self.target_img = 'blkdebug:%s:%s' % (self.blkdebug_file, target_img)
6469dfa9f59SPaolo Bonzini        qemu_img('create', '-f', iotests.imgfmt, '-osize=%d' %(TestWriteErrors.image_len), target_img)
6479dfa9f59SPaolo Bonzini        self.vm.launch()
6489dfa9f59SPaolo Bonzini
6499dfa9f59SPaolo Bonzini    def tearDown(self):
6509dfa9f59SPaolo Bonzini        self.vm.shutdown()
6519dfa9f59SPaolo Bonzini        os.remove(test_img)
652db11d1eeSKevin Wolf        os.remove(target_img)
6539dfa9f59SPaolo Bonzini        os.remove(backing_img)
6549dfa9f59SPaolo Bonzini        os.remove(self.blkdebug_file)
6559dfa9f59SPaolo Bonzini
6569dfa9f59SPaolo Bonzini    def test_report_write(self):
657ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6589dfa9f59SPaolo Bonzini
6599dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
6609dfa9f59SPaolo Bonzini                             mode='existing', target=self.target_img)
6619dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
6629dfa9f59SPaolo Bonzini
6639dfa9f59SPaolo Bonzini        completed = False
6649dfa9f59SPaolo Bonzini        error = False
6659dfa9f59SPaolo Bonzini        while not completed:
6669dfa9f59SPaolo Bonzini            for event in self.vm.get_qmp_events(wait=True):
6679dfa9f59SPaolo Bonzini                if event['event'] == 'BLOCK_JOB_ERROR':
6689dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
6699dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/operation', 'write')
6709dfa9f59SPaolo Bonzini                    error = True
6719dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_READY':
6729dfa9f59SPaolo Bonzini                    self.assertTrue(False, 'job completed unexpectedly')
6739dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_COMPLETED':
6749dfa9f59SPaolo Bonzini                    self.assertTrue(error, 'job completed unexpectedly')
6759dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/type', 'mirror')
6769dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
6779dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/error', 'Input/output error')
6789dfa9f59SPaolo Bonzini                    completed = True
6799dfa9f59SPaolo Bonzini
680ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6819dfa9f59SPaolo Bonzini
6829dfa9f59SPaolo Bonzini    def test_ignore_write(self):
683ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
6849dfa9f59SPaolo Bonzini
6859dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
6869dfa9f59SPaolo Bonzini                             mode='existing', target=self.target_img,
6879dfa9f59SPaolo Bonzini                             on_target_error='ignore')
6889dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
6899dfa9f59SPaolo Bonzini
6901dac83f1SKevin Wolf        event = self.vm.event_wait(name='BLOCK_JOB_ERROR')
691fa1cfb40SKevin Wolf        self.assertEqual(event['event'], 'BLOCK_JOB_ERROR')
6929dfa9f59SPaolo Bonzini        self.assert_qmp(event, 'data/device', 'drive0')
6939dfa9f59SPaolo Bonzini        self.assert_qmp(event, 'data/operation', 'write')
6949dfa9f59SPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
695*d5699c0dSHanna Reitz        self.assertIn(result['return'][0]['status'], ['running', 'ready'])
6969dfa9f59SPaolo Bonzini        self.complete_and_wait()
6979dfa9f59SPaolo Bonzini
6989dfa9f59SPaolo Bonzini    def test_stop_write(self):
699ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
7009dfa9f59SPaolo Bonzini
7019dfa9f59SPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
7029dfa9f59SPaolo Bonzini                             mode='existing', target=self.target_img,
7039dfa9f59SPaolo Bonzini                             on_target_error='stop')
7049dfa9f59SPaolo Bonzini        self.assert_qmp(result, 'return', {})
7059dfa9f59SPaolo Bonzini
7069dfa9f59SPaolo Bonzini        error = False
7079dfa9f59SPaolo Bonzini        ready = False
7089dfa9f59SPaolo Bonzini        while not ready:
7099dfa9f59SPaolo Bonzini            for event in self.vm.get_qmp_events(wait=True):
7109dfa9f59SPaolo Bonzini                if event['event'] == 'BLOCK_JOB_ERROR':
7119dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
7129dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/operation', 'write')
7139dfa9f59SPaolo Bonzini
714*d5699c0dSHanna Reitz                    if self.vm.qmp('query-block-jobs')['return'][0]['status'] != 'paused':
715*d5699c0dSHanna Reitz                        self.vm.events_wait([(
716*d5699c0dSHanna Reitz                            'JOB_STATUS_CHANGE',
717*d5699c0dSHanna Reitz                            {'data': {'id': 'drive0', 'status': 'paused'}}
718*d5699c0dSHanna Reitz                        )])
719*d5699c0dSHanna Reitz
7209dfa9f59SPaolo Bonzini                    result = self.vm.qmp('query-block-jobs')
721*d5699c0dSHanna Reitz                    self.assert_qmp(result, 'return[0]/status', 'paused')
7229dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
7239dfa9f59SPaolo Bonzini
7249dfa9f59SPaolo Bonzini                    result = self.vm.qmp('block-job-resume', device='drive0')
7259dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return', {})
7269dfa9f59SPaolo Bonzini
7279dfa9f59SPaolo Bonzini                    result = self.vm.qmp('query-block-jobs')
728*d5699c0dSHanna Reitz                    self.assertIn(result['return'][0]['status'], ['running', 'ready'])
7299dfa9f59SPaolo Bonzini                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
7309dfa9f59SPaolo Bonzini                    error = True
7319dfa9f59SPaolo Bonzini                elif event['event'] == 'BLOCK_JOB_READY':
7329dfa9f59SPaolo Bonzini                    self.assertTrue(error, 'job completed unexpectedly')
7339dfa9f59SPaolo Bonzini                    self.assert_qmp(event, 'data/device', 'drive0')
7349dfa9f59SPaolo Bonzini                    ready = True
7359dfa9f59SPaolo Bonzini
7369dfa9f59SPaolo Bonzini        self.complete_and_wait(wait_ready=False)
737ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
7389dfa9f59SPaolo Bonzini
739866323f3SFam Zhengclass TestSetSpeed(iotests.QMPTestCase):
74044c7ca5eSPaolo Bonzini    image_len = 80 * 1024 * 1024 # MB
74144c7ca5eSPaolo Bonzini
74244c7ca5eSPaolo Bonzini    def setUp(self):
74344c7ca5eSPaolo Bonzini        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
744b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt,
745b66ff2c2SEric Blake                 '-o', 'backing_file=%s' % backing_img, '-F', 'raw', test_img)
74644c7ca5eSPaolo Bonzini        self.vm = iotests.VM().add_drive(test_img)
74744c7ca5eSPaolo Bonzini        self.vm.launch()
74844c7ca5eSPaolo Bonzini
74944c7ca5eSPaolo Bonzini    def tearDown(self):
75044c7ca5eSPaolo Bonzini        self.vm.shutdown()
75144c7ca5eSPaolo Bonzini        os.remove(test_img)
75244c7ca5eSPaolo Bonzini        os.remove(backing_img)
75344c7ca5eSPaolo Bonzini        os.remove(target_img)
75444c7ca5eSPaolo Bonzini
75544c7ca5eSPaolo Bonzini    def test_set_speed(self):
756ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
75744c7ca5eSPaolo Bonzini
75844c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
75944c7ca5eSPaolo Bonzini                             target=target_img)
76044c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
76144c7ca5eSPaolo Bonzini
76244c7ca5eSPaolo Bonzini        # Default speed is 0
76344c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
76444c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/device', 'drive0')
76544c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/speed', 0)
76644c7ca5eSPaolo Bonzini
76744c7ca5eSPaolo Bonzini        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
76844c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
76944c7ca5eSPaolo Bonzini
77044c7ca5eSPaolo Bonzini        # Ensure the speed we set was accepted
77144c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
77244c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/device', 'drive0')
77344c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
77444c7ca5eSPaolo Bonzini
7752575fe16SStefan Hajnoczi        self.wait_ready_and_cancel()
77644c7ca5eSPaolo Bonzini
77744c7ca5eSPaolo Bonzini        # Check setting speed in drive-mirror works
77844c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
77944c7ca5eSPaolo Bonzini                             target=target_img, speed=4*1024*1024)
78044c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
78144c7ca5eSPaolo Bonzini
78244c7ca5eSPaolo Bonzini        result = self.vm.qmp('query-block-jobs')
78344c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/device', 'drive0')
78444c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
78544c7ca5eSPaolo Bonzini
7862575fe16SStefan Hajnoczi        self.wait_ready_and_cancel()
78744c7ca5eSPaolo Bonzini
78844c7ca5eSPaolo Bonzini    def test_set_speed_invalid(self):
789ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
79044c7ca5eSPaolo Bonzini
79144c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
79244c7ca5eSPaolo Bonzini                             target=target_img, speed=-1)
79344c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'error/class', 'GenericError')
79444c7ca5eSPaolo Bonzini
795ecc1c88eSStefan Hajnoczi        self.assert_no_active_block_jobs()
79644c7ca5eSPaolo Bonzini
79744c7ca5eSPaolo Bonzini        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
79844c7ca5eSPaolo Bonzini                             target=target_img)
79944c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'return', {})
80044c7ca5eSPaolo Bonzini
80144c7ca5eSPaolo Bonzini        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
80244c7ca5eSPaolo Bonzini        self.assert_qmp(result, 'error/class', 'GenericError')
80344c7ca5eSPaolo Bonzini
8042575fe16SStefan Hajnoczi        self.wait_ready_and_cancel()
80544c7ca5eSPaolo Bonzini
806866323f3SFam Zhengclass TestUnbackedSource(iotests.QMPTestCase):
807c15badeeSMax Reitz    image_len = 2 * 1024 * 1024 # MB
808c15badeeSMax Reitz
809c15badeeSMax Reitz    def setUp(self):
810c15badeeSMax Reitz        qemu_img('create', '-f', iotests.imgfmt, test_img,
811c15badeeSMax Reitz                 str(TestUnbackedSource.image_len))
8129463ee1fSMax Reitz        self.vm = iotests.VM()
813c15badeeSMax Reitz        self.vm.launch()
8149463ee1fSMax Reitz        result = self.vm.qmp('blockdev-add', node_name='drive0',
8159463ee1fSMax Reitz                             driver=iotests.imgfmt,
8169463ee1fSMax Reitz                             file={
8179463ee1fSMax Reitz                                 'driver': 'file',
8189463ee1fSMax Reitz                                 'filename': test_img,
8199463ee1fSMax Reitz                             })
8209463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
821c15badeeSMax Reitz
822c15badeeSMax Reitz    def tearDown(self):
823c15badeeSMax Reitz        self.vm.shutdown()
824c15badeeSMax Reitz        os.remove(test_img)
825c15badeeSMax Reitz        os.remove(target_img)
826c15badeeSMax Reitz
827171d6431SMax Reitz    def test_absolute_paths_full(self):
828171d6431SMax Reitz        self.assert_no_active_block_jobs()
8299463ee1fSMax Reitz        result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0',
830171d6431SMax Reitz                             sync='full', target=target_img,
831171d6431SMax Reitz                             mode='absolute-paths')
832171d6431SMax Reitz        self.assert_qmp(result, 'return', {})
833171d6431SMax Reitz        self.complete_and_wait()
834c15badeeSMax Reitz        self.assert_no_active_block_jobs()
835c15badeeSMax Reitz
836171d6431SMax Reitz    def test_absolute_paths_top(self):
837171d6431SMax Reitz        self.assert_no_active_block_jobs()
8389463ee1fSMax Reitz        result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0',
839171d6431SMax Reitz                             sync='top', target=target_img,
840171d6431SMax Reitz                             mode='absolute-paths')
841171d6431SMax Reitz        self.assert_qmp(result, 'return', {})
842171d6431SMax Reitz        self.complete_and_wait()
843171d6431SMax Reitz        self.assert_no_active_block_jobs()
844171d6431SMax Reitz
845171d6431SMax Reitz    def test_absolute_paths_none(self):
846171d6431SMax Reitz        self.assert_no_active_block_jobs()
8479463ee1fSMax Reitz        result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0',
848171d6431SMax Reitz                             sync='none', target=target_img,
849c15badeeSMax Reitz                             mode='absolute-paths')
850c15badeeSMax Reitz        self.assert_qmp(result, 'return', {})
851c15badeeSMax Reitz        self.complete_and_wait()
852c15badeeSMax Reitz        self.assert_no_active_block_jobs()
853c15badeeSMax Reitz
8549463ee1fSMax Reitz    def test_existing_full(self):
8559463ee1fSMax Reitz        qemu_img('create', '-f', iotests.imgfmt, target_img,
8569463ee1fSMax Reitz                 str(self.image_len))
8579463ee1fSMax Reitz        qemu_io('-c', 'write -P 42 0 64k', target_img)
8589463ee1fSMax Reitz
8599463ee1fSMax Reitz        self.assert_no_active_block_jobs()
8609463ee1fSMax Reitz        result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0',
8619463ee1fSMax Reitz                             sync='full', target=target_img, mode='existing')
8629463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8639463ee1fSMax Reitz        self.complete_and_wait()
8649463ee1fSMax Reitz        self.assert_no_active_block_jobs()
8659463ee1fSMax Reitz
8669463ee1fSMax Reitz        result = self.vm.qmp('blockdev-del', node_name='drive0')
8679463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8689463ee1fSMax Reitz
8699463ee1fSMax Reitz        self.assertTrue(iotests.compare_images(test_img, target_img),
8709463ee1fSMax Reitz                        'target image does not match source after mirroring')
8719463ee1fSMax Reitz
8729463ee1fSMax Reitz    def test_blockdev_full(self):
8739463ee1fSMax Reitz        qemu_img('create', '-f', iotests.imgfmt, target_img,
8749463ee1fSMax Reitz                 str(self.image_len))
8759463ee1fSMax Reitz        qemu_io('-c', 'write -P 42 0 64k', target_img)
8769463ee1fSMax Reitz
8779463ee1fSMax Reitz        result = self.vm.qmp('blockdev-add', node_name='target',
8789463ee1fSMax Reitz                             driver=iotests.imgfmt,
8799463ee1fSMax Reitz                             file={
8809463ee1fSMax Reitz                                 'driver': 'file',
8819463ee1fSMax Reitz                                 'filename': target_img,
8829463ee1fSMax Reitz                             })
8839463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8849463ee1fSMax Reitz
8859463ee1fSMax Reitz        self.assert_no_active_block_jobs()
8869463ee1fSMax Reitz        result = self.vm.qmp('blockdev-mirror', job_id='drive0', device='drive0',
8879463ee1fSMax Reitz                             sync='full', target='target')
8889463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8899463ee1fSMax Reitz        self.complete_and_wait()
8909463ee1fSMax Reitz        self.assert_no_active_block_jobs()
8919463ee1fSMax Reitz
8929463ee1fSMax Reitz        result = self.vm.qmp('blockdev-del', node_name='drive0')
8939463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8949463ee1fSMax Reitz
8959463ee1fSMax Reitz        result = self.vm.qmp('blockdev-del', node_name='target')
8969463ee1fSMax Reitz        self.assert_qmp(result, 'return', {})
8979463ee1fSMax Reitz
8989463ee1fSMax Reitz        self.assertTrue(iotests.compare_images(test_img, target_img),
8999463ee1fSMax Reitz                        'target image does not match source after mirroring')
9009463ee1fSMax Reitz
901ccee3d8fSJohn Snowclass TestGranularity(iotests.QMPTestCase):
902ccee3d8fSJohn Snow    image_len = 10 * 1024 * 1024 # MB
903ccee3d8fSJohn Snow
904ccee3d8fSJohn Snow    def setUp(self):
905ccee3d8fSJohn Snow        qemu_img('create', '-f', iotests.imgfmt, test_img,
906ccee3d8fSJohn Snow                 str(TestGranularity.image_len))
907ccee3d8fSJohn Snow        qemu_io('-c', 'write 0 %d' % (self.image_len),
908ccee3d8fSJohn Snow                test_img)
909ccee3d8fSJohn Snow        self.vm = iotests.VM().add_drive(test_img)
910ccee3d8fSJohn Snow        self.vm.launch()
911ccee3d8fSJohn Snow
912ccee3d8fSJohn Snow    def tearDown(self):
913ccee3d8fSJohn Snow        self.vm.shutdown()
914ccee3d8fSJohn Snow        self.assertTrue(iotests.compare_images(test_img, target_img),
915ccee3d8fSJohn Snow                        'target image does not match source after mirroring')
916ccee3d8fSJohn Snow        os.remove(test_img)
917ccee3d8fSJohn Snow        os.remove(target_img)
918ccee3d8fSJohn Snow
919ccee3d8fSJohn Snow    def test_granularity(self):
920ccee3d8fSJohn Snow        self.assert_no_active_block_jobs()
921ccee3d8fSJohn Snow        result = self.vm.qmp('drive-mirror', device='drive0',
922ccee3d8fSJohn Snow                             sync='full', target=target_img,
923ccee3d8fSJohn Snow                             mode='absolute-paths', granularity=8192)
924ccee3d8fSJohn Snow        self.assert_qmp(result, 'return', {})
9251dac83f1SKevin Wolf
926ccee3d8fSJohn Snow        event = self.vm.get_qmp_event(wait=60.0)
9271dac83f1SKevin Wolf        while event['event'] == 'JOB_STATUS_CHANGE':
9281dac83f1SKevin Wolf            self.assert_qmp(event, 'data/id', 'drive0')
9291dac83f1SKevin Wolf            event = self.vm.get_qmp_event(wait=60.0)
9301dac83f1SKevin Wolf
931ccee3d8fSJohn Snow        # Failures will manifest as COMPLETED/ERROR.
932ccee3d8fSJohn Snow        self.assert_qmp(event, 'event', 'BLOCK_JOB_READY')
933ccee3d8fSJohn Snow        self.complete_and_wait(drive='drive0', wait_ready=False)
934ccee3d8fSJohn Snow        self.assert_no_active_block_jobs()
935ccee3d8fSJohn Snow
936866323f3SFam Zhengclass TestRepairQuorum(iotests.QMPTestCase):
937d88964aeSBenoît Canet    """ This class test quorum file repair using drive-mirror.
938d88964aeSBenoît Canet        It's mostly a fork of TestSingleDrive """
939d88964aeSBenoît Canet    image_len = 1 * 1024 * 1024 # MB
940d88964aeSBenoît Canet    IMAGES = [ quorum_img1, quorum_img2, quorum_img3 ]
941d88964aeSBenoît Canet
9429442bebeSThomas Huth    @iotests.skip_if_unsupported(['quorum'])
943d88964aeSBenoît Canet    def setUp(self):
944d88964aeSBenoît Canet        self.vm = iotests.VM()
945d88964aeSBenoît Canet
9460ed82f7aSMax Reitz        if iotests.qemu_default_machine == 'pc':
9470ed82f7aSMax Reitz            self.vm.add_drive(None, 'media=cdrom', 'ide')
9480ed82f7aSMax Reitz
949d88964aeSBenoît Canet        # Add each individual quorum images
950d88964aeSBenoît Canet        for i in self.IMAGES:
951d88964aeSBenoît Canet            qemu_img('create', '-f', iotests.imgfmt, i,
95289e21945SMax Reitz                     str(self.image_len))
953d88964aeSBenoît Canet            # Assign a node name to each quorum image in order to manipulate
954d88964aeSBenoît Canet            # them
955d88964aeSBenoît Canet            opts = "node-name=img%i" % self.IMAGES.index(i)
956f718ca14SMax Reitz            opts += ',driver=%s' % iotests.imgfmt
957f718ca14SMax Reitz            opts += ',file.driver=file'
958f718ca14SMax Reitz            opts += ',file.filename=%s' % i
959f718ca14SMax Reitz            self.vm = self.vm.add_blockdev(opts)
960d88964aeSBenoît Canet
961d88964aeSBenoît Canet        self.vm.launch()
962d88964aeSBenoît Canet
963d88964aeSBenoît Canet        #assemble the quorum block device from the individual files
9640153d2f5SKevin Wolf        args = { "driver": "quorum", "node-name": "quorum0",
9650153d2f5SKevin Wolf                 "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] }
966d88964aeSBenoît Canet        result = self.vm.qmp("blockdev-add", **args)
967d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
968d88964aeSBenoît Canet
969d88964aeSBenoît Canet
970d88964aeSBenoît Canet    def tearDown(self):
971d88964aeSBenoît Canet        self.vm.shutdown()
972a1da1878SMax Reitz        for i in self.IMAGES + [ quorum_repair_img, quorum_snapshot_file,
973a1da1878SMax Reitz                                 nbd_sock_path ]:
974d88964aeSBenoît Canet            # Do a try/except because the test may have deleted some images
975d88964aeSBenoît Canet            try:
976d88964aeSBenoît Canet                os.remove(i)
977d88964aeSBenoît Canet            except OSError:
978d88964aeSBenoît Canet                pass
979d88964aeSBenoît Canet
980d88964aeSBenoît Canet    def test_complete(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.complete_and_wait(drive="job0")
987e71fc0baSFam Zheng        self.assert_has_block_node("repair0", quorum_repair_img)
988c351afd6SMax Reitz        self.vm.assert_block_path('quorum0', '/children.1', 'repair0')
989d88964aeSBenoît Canet        self.vm.shutdown()
990d88964aeSBenoît Canet        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
991d88964aeSBenoît Canet                        'target image does not match source after mirroring')
992d88964aeSBenoît Canet
993d88964aeSBenoît Canet    def test_cancel(self):
994476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
995476fb028SKevin Wolf                             sync='full', node_name="repair0", replaces="img1",
996d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
997d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
998d88964aeSBenoît Canet
999476fb028SKevin Wolf        self.cancel_and_wait(drive="job0", force=True)
1000d88964aeSBenoît Canet        # here we check that the last registered quorum file has not been
1001d88964aeSBenoît Canet        # swapped out and unref
1002e71fc0baSFam Zheng        self.assert_has_block_node(None, quorum_img3)
1003d88964aeSBenoît Canet
1004d88964aeSBenoît Canet    def test_cancel_after_ready(self):
1005476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1006476fb028SKevin Wolf                             sync='full', node_name="repair0", replaces="img1",
1007d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1008d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
1009d88964aeSBenoît Canet
1010476fb028SKevin Wolf        self.wait_ready_and_cancel(drive="job0")
1011d88964aeSBenoît Canet        # here we check that the last registered quorum file has not been
1012d88964aeSBenoît Canet        # swapped out and unref
1013e71fc0baSFam Zheng        self.assert_has_block_node(None, quorum_img3)
1014d88964aeSBenoît Canet        self.vm.shutdown()
1015d88964aeSBenoît Canet        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
1016d88964aeSBenoît Canet                        'target image does not match source after mirroring')
1017d88964aeSBenoît Canet
1018d88964aeSBenoît Canet    def test_pause(self):
1019476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1020476fb028SKevin Wolf                             sync='full', node_name="repair0", replaces="img1",
1021d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1022d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
1023d88964aeSBenoît Canet
10242c93c5cbSKevin Wolf        self.pause_job('job0')
1025d88964aeSBenoît Canet
1026d88964aeSBenoît Canet        result = self.vm.qmp('query-block-jobs')
1027d88964aeSBenoît Canet        offset = self.dictpath(result, 'return[0]/offset')
1028d88964aeSBenoît Canet
10292c93c5cbSKevin Wolf        time.sleep(0.5)
1030d88964aeSBenoît Canet        result = self.vm.qmp('query-block-jobs')
1031d88964aeSBenoît Canet        self.assert_qmp(result, 'return[0]/offset', offset)
1032d88964aeSBenoît Canet
1033476fb028SKevin Wolf        result = self.vm.qmp('block-job-resume', device='job0')
1034d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
1035d88964aeSBenoît Canet
1036476fb028SKevin Wolf        self.complete_and_wait(drive="job0")
1037d88964aeSBenoît Canet        self.vm.shutdown()
1038d88964aeSBenoît Canet        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
1039d88964aeSBenoît Canet                        'target image does not match source after mirroring')
1040d88964aeSBenoît Canet
1041d88964aeSBenoît Canet    def test_medium_not_found(self):
1042d8683155SBo Tu        if iotests.qemu_default_machine != 'pc':
1043d8683155SBo Tu            return
1044d8683155SBo Tu
1045476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='drive0', # CD-ROM
10460ed82f7aSMax Reitz                             sync='full',
1047d88964aeSBenoît Canet                             node_name='repair0',
1048d88964aeSBenoît Canet                             replaces='img1',
1049d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1050d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1051d88964aeSBenoît Canet
1052d88964aeSBenoît Canet    def test_image_not_found(self):
1053476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1054476fb028SKevin Wolf                             sync='full', node_name='repair0', replaces='img1',
1055476fb028SKevin Wolf                             mode='existing', target=quorum_repair_img,
1056476fb028SKevin Wolf                             format=iotests.imgfmt)
1057d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1058d88964aeSBenoît Canet
1059d88964aeSBenoît Canet    def test_device_not_found(self):
1060476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0',
1061476fb028SKevin Wolf                             device='nonexistent', sync='full',
1062d88964aeSBenoît Canet                             node_name='repair0',
1063d88964aeSBenoît Canet                             replaces='img1',
1064d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
10650524e93aSKevin Wolf        self.assert_qmp(result, 'error/class', 'GenericError')
1066d88964aeSBenoît Canet
1067d88964aeSBenoît Canet    def test_wrong_sync_mode(self):
1068476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', device='quorum0', job_id='job0',
1069d88964aeSBenoît Canet                             node_name='repair0',
1070d88964aeSBenoît Canet                             replaces='img1',
1071d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1072d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1073d88964aeSBenoît Canet
1074d88964aeSBenoît Canet    def test_no_node_name(self):
1075476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1076476fb028SKevin Wolf                             sync='full', replaces='img1',
1077d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1078d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1079d88964aeSBenoît Canet
108067cc32ebSVeres Lajos    def test_nonexistent_replaces(self):
1081476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1082476fb028SKevin Wolf                             sync='full', node_name='repair0', replaces='img77',
1083d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1084d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1085d88964aeSBenoît Canet
1086d88964aeSBenoît Canet    def test_after_a_quorum_snapshot(self):
1087d88964aeSBenoît Canet        result = self.vm.qmp('blockdev-snapshot-sync', node_name='img1',
1088d88964aeSBenoît Canet                             snapshot_file=quorum_snapshot_file,
1089d88964aeSBenoît Canet                             snapshot_node_name="snap1");
1090d88964aeSBenoît Canet
1091476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1092476fb028SKevin Wolf                             sync='full', node_name='repair0', replaces="img1",
1093d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1094d88964aeSBenoît Canet        self.assert_qmp(result, 'error/class', 'GenericError')
1095d88964aeSBenoît Canet
1096476fb028SKevin Wolf        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
1097476fb028SKevin Wolf                             sync='full', node_name='repair0', replaces="snap1",
1098d88964aeSBenoît Canet                             target=quorum_repair_img, format=iotests.imgfmt)
1099d88964aeSBenoît Canet        self.assert_qmp(result, 'return', {})
1100d88964aeSBenoît Canet
1101476fb028SKevin Wolf        self.complete_and_wait('job0')
1102e71fc0baSFam Zheng        self.assert_has_block_node("repair0", quorum_repair_img)
1103c351afd6SMax Reitz        self.vm.assert_block_path('quorum0', '/children.1', 'repair0')
1104d88964aeSBenoît Canet
1105a1da1878SMax Reitz    def test_with_other_parent(self):
1106a1da1878SMax Reitz        """
1107a1da1878SMax Reitz        Check that we cannot replace a Quorum child when it has other
1108a1da1878SMax Reitz        parents.
1109a1da1878SMax Reitz        """
1110a1da1878SMax Reitz        result = self.vm.qmp('nbd-server-start',
1111a1da1878SMax Reitz                             addr={
1112a1da1878SMax Reitz                                 'type': 'unix',
1113a1da1878SMax Reitz                                 'data': {'path': nbd_sock_path}
1114a1da1878SMax Reitz                             })
1115a1da1878SMax Reitz        self.assert_qmp(result, 'return', {})
1116a1da1878SMax Reitz
1117a1da1878SMax Reitz        result = self.vm.qmp('nbd-server-add', device='img1')
1118a1da1878SMax Reitz        self.assert_qmp(result, 'return', {})
1119a1da1878SMax Reitz
1120a1da1878SMax Reitz        result = self.vm.qmp('drive-mirror', job_id='mirror', device='quorum0',
1121a1da1878SMax Reitz                             sync='full', node_name='repair0', replaces='img1',
1122a1da1878SMax Reitz                             target=quorum_repair_img, format=iotests.imgfmt)
1123a1da1878SMax Reitz        self.assert_qmp(result, 'error/desc',
1124a1da1878SMax Reitz                        "Cannot replace 'img1' by a node mirrored from "
1125a1da1878SMax Reitz                        "'quorum0', because it cannot be guaranteed that doing "
1126a1da1878SMax Reitz                        "so would not lead to an abrupt change of visible data")
1127a1da1878SMax Reitz
1128a1da1878SMax Reitz    def test_with_other_parents_after_mirror_start(self):
1129a1da1878SMax Reitz        """
1130a1da1878SMax Reitz        The same as test_with_other_parent(), but add the NBD server
1131a1da1878SMax Reitz        only when the mirror job is already running.
1132a1da1878SMax Reitz        """
1133a1da1878SMax Reitz        result = self.vm.qmp('nbd-server-start',
1134a1da1878SMax Reitz                             addr={
1135a1da1878SMax Reitz                                 'type': 'unix',
1136a1da1878SMax Reitz                                 'data': {'path': nbd_sock_path}
1137a1da1878SMax Reitz                             })
1138a1da1878SMax Reitz        self.assert_qmp(result, 'return', {})
1139a1da1878SMax Reitz
1140a1da1878SMax Reitz        result = self.vm.qmp('drive-mirror', job_id='mirror', device='quorum0',
1141a1da1878SMax Reitz                             sync='full', node_name='repair0', replaces='img1',
1142a1da1878SMax Reitz                             target=quorum_repair_img, format=iotests.imgfmt)
1143a1da1878SMax Reitz        self.assert_qmp(result, 'return', {})
1144a1da1878SMax Reitz
1145a1da1878SMax Reitz        result = self.vm.qmp('nbd-server-add', device='img1')
1146a1da1878SMax Reitz        self.assert_qmp(result, 'return', {})
1147a1da1878SMax Reitz
1148a1da1878SMax Reitz        # The full error message goes to stderr, we will check it later
1149a1da1878SMax Reitz        self.complete_and_wait('mirror',
1150a1da1878SMax Reitz                               completion_error='Operation not permitted')
1151a1da1878SMax Reitz
1152a1da1878SMax Reitz        # Should not have been replaced
1153a1da1878SMax Reitz        self.vm.assert_block_path('quorum0', '/children.1', 'img1')
1154a1da1878SMax Reitz
1155a1da1878SMax Reitz        # Check the full error message now
1156a1da1878SMax Reitz        self.vm.shutdown()
1157a1da1878SMax Reitz        log = self.vm.get_log()
1158a1da1878SMax Reitz        log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log)
1159a1da1878SMax Reitz        log = re.sub(r'^Formatting.*\n', '', log)
1160a1da1878SMax Reitz        log = re.sub(r'\n\[I \+\d+\.\d+\] CLOSED\n?$', '', log)
1161a1da1878SMax Reitz        log = re.sub(r'^%s: ' % os.path.basename(iotests.qemu_prog), '', log)
1162a1da1878SMax Reitz
1163a1da1878SMax Reitz        self.assertEqual(log,
1164a1da1878SMax Reitz                         "Can no longer replace 'img1' by 'repair0', because " +
1165a1da1878SMax Reitz                         "it can no longer be guaranteed that doing so would " +
1166a1da1878SMax Reitz                         "not lead to an abrupt change of visible data")
1167a1da1878SMax Reitz
1168a1da1878SMax Reitz
11695694923aSMax Reitz# Test mirroring with a source that does not have any parents (not even a
11705694923aSMax Reitz# BlockBackend)
11715694923aSMax Reitzclass TestOrphanedSource(iotests.QMPTestCase):
11725694923aSMax Reitz    def setUp(self):
11735694923aSMax Reitz        blk0 = { 'node-name': 'src',
11745694923aSMax Reitz                 'driver': 'null-co' }
11755694923aSMax Reitz
11765694923aSMax Reitz        blk1 = { 'node-name': 'dest',
11775694923aSMax Reitz                 'driver': 'null-co' }
11785694923aSMax Reitz
11795694923aSMax Reitz        blk2 = { 'node-name': 'dest-ro',
11805694923aSMax Reitz                 'driver': 'null-co',
11815694923aSMax Reitz                 'read-only': 'on' }
11825694923aSMax Reitz
11835694923aSMax Reitz        self.vm = iotests.VM()
118462a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blk0))
118562a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blk1))
118662a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blk2))
11875694923aSMax Reitz        self.vm.launch()
11885694923aSMax Reitz
11895694923aSMax Reitz    def tearDown(self):
11905694923aSMax Reitz        self.vm.shutdown()
11915694923aSMax Reitz
11925694923aSMax Reitz    def test_no_job_id(self):
11935694923aSMax Reitz        self.assert_no_active_block_jobs()
11945694923aSMax Reitz
11955694923aSMax Reitz        result = self.vm.qmp('blockdev-mirror', device='src', sync='full',
11965694923aSMax Reitz                             target='dest')
11975694923aSMax Reitz        self.assert_qmp(result, 'error/class', 'GenericError')
11985694923aSMax Reitz
11995694923aSMax Reitz    def test_success(self):
12005694923aSMax Reitz        self.assert_no_active_block_jobs()
12015694923aSMax Reitz
12025694923aSMax Reitz        result = self.vm.qmp('blockdev-mirror', job_id='job', device='src',
12035694923aSMax Reitz                             sync='full', target='dest')
12045694923aSMax Reitz        self.assert_qmp(result, 'return', {})
12055694923aSMax Reitz
12065694923aSMax Reitz        self.complete_and_wait('job')
12075694923aSMax Reitz
12085694923aSMax Reitz    def test_failing_permissions(self):
12095694923aSMax Reitz        self.assert_no_active_block_jobs()
12105694923aSMax Reitz
12115694923aSMax Reitz        result = self.vm.qmp('blockdev-mirror', device='src', sync='full',
12125694923aSMax Reitz                             target='dest-ro')
12135694923aSMax Reitz        self.assert_qmp(result, 'error/class', 'GenericError')
12145694923aSMax Reitz
12159592fe45SMax Reitz    def test_failing_permission_in_complete(self):
12169592fe45SMax Reitz        self.assert_no_active_block_jobs()
12179592fe45SMax Reitz
12189592fe45SMax Reitz        # Unshare consistent-read on the target
12199592fe45SMax Reitz        # (The mirror job does not care)
12209592fe45SMax Reitz        result = self.vm.qmp('blockdev-add',
12219592fe45SMax Reitz                             driver='blkdebug',
12229592fe45SMax Reitz                             node_name='dest-perm',
12239592fe45SMax Reitz                             image='dest',
12249592fe45SMax Reitz                             unshare_child_perms=['consistent-read'])
12259592fe45SMax Reitz        self.assert_qmp(result, 'return', {})
12269592fe45SMax Reitz
12279592fe45SMax Reitz        result = self.vm.qmp('blockdev-mirror', job_id='job', device='src',
12289592fe45SMax Reitz                             sync='full', target='dest',
12299592fe45SMax Reitz                             filter_node_name='mirror-filter')
12309592fe45SMax Reitz        self.assert_qmp(result, 'return', {})
12319592fe45SMax Reitz
12329592fe45SMax Reitz        # Require consistent-read on the source
12339592fe45SMax Reitz        # (We can only add this node once the job has started, or it
12349592fe45SMax Reitz        # will complain that it does not want to run on non-root nodes)
12359592fe45SMax Reitz        result = self.vm.qmp('blockdev-add',
12369592fe45SMax Reitz                             driver='blkdebug',
12379592fe45SMax Reitz                             node_name='src-perm',
12389592fe45SMax Reitz                             image='src',
12399592fe45SMax Reitz                             take_child_perms=['consistent-read'])
12409592fe45SMax Reitz        self.assert_qmp(result, 'return', {})
12419592fe45SMax Reitz
12429592fe45SMax Reitz        # While completing, mirror will attempt to replace src by
12439592fe45SMax Reitz        # dest, which must fail because src-perm requires
12449592fe45SMax Reitz        # consistent-read but dest-perm does not share it; thus
12459592fe45SMax Reitz        # aborting the job when it is supposed to complete
12469592fe45SMax Reitz        self.complete_and_wait('job',
12479592fe45SMax Reitz                               completion_error='Operation not permitted')
12489592fe45SMax Reitz
12499592fe45SMax Reitz        # Assert that all of our nodes are still there (except for the
12509592fe45SMax Reitz        # mirror filter, which should be gone despite the failure)
12519592fe45SMax Reitz        nodes = self.vm.qmp('query-named-block-nodes')['return']
12529592fe45SMax Reitz        nodes = [node['node-name'] for node in nodes]
12539592fe45SMax Reitz
12549592fe45SMax Reitz        for expect in ('src', 'src-perm', 'dest', 'dest-perm'):
12559592fe45SMax Reitz            self.assertTrue(expect in nodes, '%s disappeared' % expect)
12569592fe45SMax Reitz        self.assertFalse('mirror-filter' in nodes,
12579592fe45SMax Reitz                         'Mirror filter node did not disappear')
12589592fe45SMax Reitz
1259c45a88f4SMax Reitz# Test cases for @replaces that do not necessarily involve Quorum
1260c45a88f4SMax Reitzclass TestReplaces(iotests.QMPTestCase):
1261c45a88f4SMax Reitz    # Each of these test cases needs their own block graph, so do not
1262c45a88f4SMax Reitz    # create any nodes here
1263c45a88f4SMax Reitz    def setUp(self):
1264c45a88f4SMax Reitz        self.vm = iotests.VM()
1265c45a88f4SMax Reitz        self.vm.launch()
1266c45a88f4SMax Reitz
1267c45a88f4SMax Reitz    def tearDown(self):
1268c45a88f4SMax Reitz        self.vm.shutdown()
1269c45a88f4SMax Reitz        for img in (test_img, target_img):
1270c45a88f4SMax Reitz            try:
1271c45a88f4SMax Reitz                os.remove(img)
1272c45a88f4SMax Reitz            except OSError:
1273c45a88f4SMax Reitz                pass
1274c45a88f4SMax Reitz
1275c45a88f4SMax Reitz    @iotests.skip_if_unsupported(['copy-on-read'])
1276c45a88f4SMax Reitz    def test_replace_filter(self):
1277c45a88f4SMax Reitz        """
1278c45a88f4SMax Reitz        Check that we can replace filter nodes.
1279c45a88f4SMax Reitz        """
1280c45a88f4SMax Reitz        result = self.vm.qmp('blockdev-add', **{
1281c45a88f4SMax Reitz                                 'driver': 'copy-on-read',
1282c45a88f4SMax Reitz                                 'node-name': 'filter0',
1283c45a88f4SMax Reitz                                 'file': {
1284c45a88f4SMax Reitz                                     'driver': 'copy-on-read',
1285c45a88f4SMax Reitz                                     'node-name': 'filter1',
1286c45a88f4SMax Reitz                                     'file': {
1287c45a88f4SMax Reitz                                         'driver': 'null-co'
1288c45a88f4SMax Reitz                                     }
1289c45a88f4SMax Reitz                                 }
1290c45a88f4SMax Reitz                             })
1291c45a88f4SMax Reitz        self.assert_qmp(result, 'return', {})
1292c45a88f4SMax Reitz
1293c45a88f4SMax Reitz        result = self.vm.qmp('blockdev-add',
1294c45a88f4SMax Reitz                             node_name='target', driver='null-co')
1295c45a88f4SMax Reitz        self.assert_qmp(result, 'return', {})
1296c45a88f4SMax Reitz
1297c45a88f4SMax Reitz        result = self.vm.qmp('blockdev-mirror', job_id='mirror', device='filter0',
1298c45a88f4SMax Reitz                             target='target', sync='full', replaces='filter1')
1299c45a88f4SMax Reitz        self.assert_qmp(result, 'return', {})
1300c45a88f4SMax Reitz
1301c45a88f4SMax Reitz        self.complete_and_wait('mirror')
1302c45a88f4SMax Reitz
1303c45a88f4SMax Reitz        self.vm.assert_block_path('filter0', '/file', 'target')
1304c45a88f4SMax Reitz
13055f71c6b0SMax Reitz# Tests for mirror with filters (and how the mirror filter behaves, as
13065f71c6b0SMax Reitz# an example for an implicit filter)
13075f71c6b0SMax Reitzclass TestFilters(iotests.QMPTestCase):
13085f71c6b0SMax Reitz    def setUp(self):
13095f71c6b0SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M')
1310497a30dbSEric Blake        qemu_img('create', '-f', iotests.imgfmt, '-b', backing_img,
1311497a30dbSEric Blake                 '-F', iotests.imgfmt, test_img)
1312497a30dbSEric Blake        qemu_img('create', '-f', iotests.imgfmt, '-b', backing_img,
1313497a30dbSEric Blake                 '-F', iotests.imgfmt, target_img)
13145f71c6b0SMax Reitz
13155f71c6b0SMax Reitz        qemu_io('-c', 'write -P 1 0 512k', backing_img)
13165f71c6b0SMax Reitz        qemu_io('-c', 'write -P 2 512k 512k', test_img)
13175f71c6b0SMax Reitz
13185f71c6b0SMax Reitz        self.vm = iotests.VM().add_device('virtio-scsi,id=vio-scsi')
13195f71c6b0SMax Reitz        self.vm.launch()
13205f71c6b0SMax Reitz
13215f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-add', **{
13225f71c6b0SMax Reitz                                'node-name': 'target',
13235f71c6b0SMax Reitz                                'driver': iotests.imgfmt,
13245f71c6b0SMax Reitz                                'file': {
13255f71c6b0SMax Reitz                                    'driver': 'file',
13265f71c6b0SMax Reitz                                    'filename': target_img
13275f71c6b0SMax Reitz                                },
13285f71c6b0SMax Reitz                                'backing': None
13295f71c6b0SMax Reitz                            })
13305f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
13315f71c6b0SMax Reitz
13325f71c6b0SMax Reitz        self.filterless_chain = {
13335f71c6b0SMax Reitz                'node-name': 'source',
13345f71c6b0SMax Reitz                'driver': iotests.imgfmt,
13355f71c6b0SMax Reitz                'file': {
13365f71c6b0SMax Reitz                    'driver': 'file',
13375f71c6b0SMax Reitz                    'filename': test_img
13385f71c6b0SMax Reitz                },
13395f71c6b0SMax Reitz                'backing': {
13405f71c6b0SMax Reitz                    'node-name': 'backing',
13415f71c6b0SMax Reitz                    'driver': iotests.imgfmt,
13425f71c6b0SMax Reitz                    'file': {
13435f71c6b0SMax Reitz                        'driver': 'file',
13445f71c6b0SMax Reitz                        'filename': backing_img
13455f71c6b0SMax Reitz                    }
13465f71c6b0SMax Reitz                }
13475f71c6b0SMax Reitz            }
13485f71c6b0SMax Reitz
13495f71c6b0SMax Reitz    def tearDown(self):
13505f71c6b0SMax Reitz        self.vm.shutdown()
13515f71c6b0SMax Reitz
13525f71c6b0SMax Reitz        os.remove(test_img)
13535f71c6b0SMax Reitz        os.remove(target_img)
13545f71c6b0SMax Reitz        os.remove(backing_img)
13555f71c6b0SMax Reitz
13565f71c6b0SMax Reitz    def test_cor(self):
13575f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-add', **{
13585f71c6b0SMax Reitz                                'node-name': 'filter',
13595f71c6b0SMax Reitz                                'driver': 'copy-on-read',
13605f71c6b0SMax Reitz                                'file': self.filterless_chain
13615f71c6b0SMax Reitz                            })
13625f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
13635f71c6b0SMax Reitz
13645f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-mirror',
13655f71c6b0SMax Reitz                             job_id='mirror',
13665f71c6b0SMax Reitz                             device='filter',
13675f71c6b0SMax Reitz                             target='target',
13685f71c6b0SMax Reitz                             sync='top')
13695f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
13705f71c6b0SMax Reitz
13715f71c6b0SMax Reitz        self.complete_and_wait('mirror')
13725f71c6b0SMax Reitz
13735f71c6b0SMax Reitz        self.vm.qmp('blockdev-del', node_name='target')
13745f71c6b0SMax Reitz
13751670ae7aSJohn Snow        target_map = qemu_img_map(target_img)
13765f71c6b0SMax Reitz
13775f71c6b0SMax Reitz        assert target_map[0]['start'] == 0
13785f71c6b0SMax Reitz        assert target_map[0]['length'] == 512 * 1024
13795f71c6b0SMax Reitz        assert target_map[0]['depth'] == 1
13805f71c6b0SMax Reitz
13815f71c6b0SMax Reitz        assert target_map[1]['start'] == 512 * 1024
13825f71c6b0SMax Reitz        assert target_map[1]['length'] == 512 * 1024
13835f71c6b0SMax Reitz        assert target_map[1]['depth'] == 0
13845f71c6b0SMax Reitz
13855f71c6b0SMax Reitz    def test_implicit_mirror_filter(self):
13865f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-add', **self.filterless_chain)
13875f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
13885f71c6b0SMax Reitz
13895f71c6b0SMax Reitz        # We need this so we can query from above the mirror node
13905f71c6b0SMax Reitz        result = self.vm.qmp('device_add',
13915f71c6b0SMax Reitz                             driver='scsi-hd',
13925f71c6b0SMax Reitz                             id='virtio',
13935f71c6b0SMax Reitz                             bus='vio-scsi.0',
13945f71c6b0SMax Reitz                             drive='source')
13955f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
13965f71c6b0SMax Reitz
13975f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-mirror',
13985f71c6b0SMax Reitz                             job_id='mirror',
13995f71c6b0SMax Reitz                             device='source',
14005f71c6b0SMax Reitz                             target='target',
14015f71c6b0SMax Reitz                             sync='top')
14025f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
14035f71c6b0SMax Reitz
14045f71c6b0SMax Reitz        # The mirror filter is now an implicit node, so it should be
14055f71c6b0SMax Reitz        # invisible when querying the backing chain
14065f71c6b0SMax Reitz        blockdevs = self.vm.qmp('query-block')['return']
14075f71c6b0SMax Reitz        device_info = next(dev for dev in blockdevs if dev['qdev'] == 'virtio')
14085f71c6b0SMax Reitz
14095f71c6b0SMax Reitz        assert device_info['inserted']['node-name'] == 'source'
14105f71c6b0SMax Reitz
14115f71c6b0SMax Reitz        image_info = device_info['inserted']['image']
14125f71c6b0SMax Reitz        assert image_info['filename'] == test_img
14135f71c6b0SMax Reitz        assert image_info['backing-image']['filename'] == backing_img
14145f71c6b0SMax Reitz
14155f71c6b0SMax Reitz        self.complete_and_wait('mirror')
14165f71c6b0SMax Reitz
14175f71c6b0SMax Reitz    def test_explicit_mirror_filter(self):
14185f71c6b0SMax Reitz        # Same test as above, but this time we give the mirror filter
14195f71c6b0SMax Reitz        # a node-name so it will not be invisible
14205f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-add', **self.filterless_chain)
14215f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
14225f71c6b0SMax Reitz
14235f71c6b0SMax Reitz        # We need this so we can query from above the mirror node
14245f71c6b0SMax Reitz        result = self.vm.qmp('device_add',
14255f71c6b0SMax Reitz                             driver='scsi-hd',
14265f71c6b0SMax Reitz                             id='virtio',
14275f71c6b0SMax Reitz                             bus='vio-scsi.0',
14285f71c6b0SMax Reitz                             drive='source')
14295f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
14305f71c6b0SMax Reitz
14315f71c6b0SMax Reitz        result = self.vm.qmp('blockdev-mirror',
14325f71c6b0SMax Reitz                             job_id='mirror',
14335f71c6b0SMax Reitz                             device='source',
14345f71c6b0SMax Reitz                             target='target',
14355f71c6b0SMax Reitz                             sync='top',
14365f71c6b0SMax Reitz                             filter_node_name='mirror-filter')
14375f71c6b0SMax Reitz        self.assert_qmp(result, 'return', {})
14385f71c6b0SMax Reitz
14395f71c6b0SMax Reitz        # With a node-name given to it, the mirror filter should now
14405f71c6b0SMax Reitz        # be visible
14415f71c6b0SMax Reitz        blockdevs = self.vm.qmp('query-block')['return']
14425f71c6b0SMax Reitz        device_info = next(dev for dev in blockdevs if dev['qdev'] == 'virtio')
14435f71c6b0SMax Reitz
14445f71c6b0SMax Reitz        assert device_info['inserted']['node-name'] == 'mirror-filter'
14455f71c6b0SMax Reitz
14465f71c6b0SMax Reitz        self.complete_and_wait('mirror')
14475f71c6b0SMax Reitz
14485f71c6b0SMax Reitz
144944c7ca5eSPaolo Bonziniif __name__ == '__main__':
1450103cbc77SMax Reitz    iotests.main(supported_fmts=['qcow2', 'qed'],
1451877d18f2SThomas Huth                 supported_protocols=['file'],
1452877d18f2SThomas Huth                 supported_platforms=['linux', 'freebsd', 'netbsd', 'openbsd'])
1453