xref: /openbmc/qemu/tests/qemu-iotests/129 (revision a68694cd)
1#!/usr/bin/env python3
2#
3# Tests that "bdrv_drain_all" doesn't drain block jobs
4#
5# Copyright (C) 2015 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21import os
22import iotests
23import time
24
25class TestStopWithBlockJob(iotests.QMPTestCase):
26    test_img = os.path.join(iotests.test_dir, 'test.img')
27    target_img = os.path.join(iotests.test_dir, 'target.img')
28    base_img = os.path.join(iotests.test_dir, 'base.img')
29
30    def setUp(self):
31        iotests.qemu_img('create', '-f', iotests.imgfmt, self.base_img, "1G")
32        iotests.qemu_img('create', '-f', iotests.imgfmt, self.test_img,
33                         "-b", self.base_img, '-F', iotests.imgfmt)
34        iotests.qemu_io('-f', iotests.imgfmt, '-c', 'write -P0x5d 1M 128M', self.test_img)
35        self.vm = iotests.VM().add_drive(self.test_img)
36        self.vm.launch()
37
38    def tearDown(self):
39        params = {"device": "drive0",
40                  "bps": 0,
41                  "bps_rd": 0,
42                  "bps_wr": 0,
43                  "iops": 0,
44                  "iops_rd": 0,
45                  "iops_wr": 0,
46                 }
47        result = self.vm.qmp("block_set_io_throttle", conv_keys=False,
48                             **params)
49        self.vm.shutdown()
50
51    def do_test_stop(self, cmd, **args):
52        """Test 'stop' while block job is running on a throttled drive.
53        The 'stop' command shouldn't drain the job"""
54        params = {"device": "drive0",
55                  "bps": 1024,
56                  "bps_rd": 0,
57                  "bps_wr": 0,
58                  "iops": 0,
59                  "iops_rd": 0,
60                  "iops_wr": 0,
61                 }
62        result = self.vm.qmp("block_set_io_throttle", conv_keys=False,
63                             **params)
64        self.assert_qmp(result, 'return', {})
65        result = self.vm.qmp(cmd, **args)
66        self.assert_qmp(result, 'return', {})
67        result = self.vm.qmp("stop")
68        self.assert_qmp(result, 'return', {})
69        result = self.vm.qmp("query-block-jobs")
70        self.assert_qmp(result, 'return[0]/busy', True)
71        self.assert_qmp(result, 'return[0]/ready', False)
72
73    def test_drive_mirror(self):
74        self.do_test_stop("drive-mirror", device="drive0",
75                          target=self.target_img,
76                          sync="full")
77
78    def test_drive_backup(self):
79        self.do_test_stop("drive-backup", device="drive0",
80                          target=self.target_img,
81                          sync="full")
82
83    def test_block_commit(self):
84        self.do_test_stop("block-commit", device="drive0")
85
86if __name__ == '__main__':
87    iotests.main(supported_fmts=["qcow2"],
88                 supported_protocols=["file"])
89