1*80e7faaaSKevin Wolf#!/usr/bin/env python3
2*80e7faaaSKevin Wolf# group: rw quick
3*80e7faaaSKevin Wolf#
4*80e7faaaSKevin Wolf# Copyright (C) 2023 Red Hat, Inc.
5*80e7faaaSKevin Wolf#
6*80e7faaaSKevin Wolf# This program is free software; you can redistribute it and/or modify
7*80e7faaaSKevin Wolf# it under the terms of the GNU General Public License as published by
8*80e7faaaSKevin Wolf# the Free Software Foundation; either version 2 of the License, or
9*80e7faaaSKevin Wolf# (at your option) any later version.
10*80e7faaaSKevin Wolf#
11*80e7faaaSKevin Wolf# This program is distributed in the hope that it will be useful,
12*80e7faaaSKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of
13*80e7faaaSKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*80e7faaaSKevin Wolf# GNU General Public License for more details.
15*80e7faaaSKevin Wolf#
16*80e7faaaSKevin Wolf# You should have received a copy of the GNU General Public License
17*80e7faaaSKevin Wolf# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18*80e7faaaSKevin Wolf#
19*80e7faaaSKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
20*80e7faaaSKevin Wolf
21*80e7faaaSKevin Wolfimport asyncio
22*80e7faaaSKevin Wolfimport iotests
23*80e7faaaSKevin Wolf
24*80e7faaaSKevin Wolfiotests.script_initialize(supported_fmts=['qcow2', 'qcow', 'qed', 'vdi',
25*80e7faaaSKevin Wolf                                          'vmdk', 'parallels'])
26*80e7faaaSKevin Wolfiotests.verify_virtio_scsi_pci_or_ccw()
27*80e7faaaSKevin Wolf
28*80e7faaaSKevin Wolfwith iotests.FilePath('disk.img') as img_path, \
29*80e7faaaSKevin Wolf     iotests.VM() as vm:
30*80e7faaaSKevin Wolf
31*80e7faaaSKevin Wolf    iotests.qemu_img_create('-f', 'raw', img_path, '0')
32*80e7faaaSKevin Wolf
33*80e7faaaSKevin Wolf    vm.add_object('iothread,id=iothread0')
34*80e7faaaSKevin Wolf    vm.add_blockdev(f'file,node-name=img-file,read-only=on,'
35*80e7faaaSKevin Wolf                    f'filename={img_path}')
36*80e7faaaSKevin Wolf    vm.add_device('virtio-scsi,iothread=iothread0')
37*80e7faaaSKevin Wolf    vm.add_device('scsi-hd,drive=img-file,share-rw=on')
38*80e7faaaSKevin Wolf
39*80e7faaaSKevin Wolf    vm.launch()
40*80e7faaaSKevin Wolf
41*80e7faaaSKevin Wolf    iotests.log(vm.qmp(
42*80e7faaaSKevin Wolf        'blockdev-reopen',
43*80e7faaaSKevin Wolf        options=[{
44*80e7faaaSKevin Wolf            'driver': 'file',
45*80e7faaaSKevin Wolf            'filename': img_path,
46*80e7faaaSKevin Wolf            'node-name': 'img-file',
47*80e7faaaSKevin Wolf            'read-only': False,
48*80e7faaaSKevin Wolf        }],
49*80e7faaaSKevin Wolf    ))
50*80e7faaaSKevin Wolf    iotests.log(vm.qmp(
51*80e7faaaSKevin Wolf        'blockdev-create',
52*80e7faaaSKevin Wolf        job_id='job0',
53*80e7faaaSKevin Wolf        options={
54*80e7faaaSKevin Wolf            'driver': iotests.imgfmt,
55*80e7faaaSKevin Wolf            'file': 'img-file',
56*80e7faaaSKevin Wolf            'size': 1024 * 1024,
57*80e7faaaSKevin Wolf        },
58*80e7faaaSKevin Wolf    ))
59*80e7faaaSKevin Wolf
60*80e7faaaSKevin Wolf    # Should succeed and not time out
61*80e7faaaSKevin Wolf    try:
62*80e7faaaSKevin Wolf        vm.run_job('job0', wait=5.0)
63*80e7faaaSKevin Wolf        vm.shutdown()
64*80e7faaaSKevin Wolf    except asyncio.TimeoutError:
65*80e7faaaSKevin Wolf        # VM may be stuck, kill it
66*80e7faaaSKevin Wolf        vm.kill()
67*80e7faaaSKevin Wolf        raise
68