4 # Test the rate limit of QMP events
6 # Copyright (C) 2016 Igalia, S.L.
7 # Author: Alberto Garcia <berto@igalia.com>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 imgs = (os.path.join(iotests.test_dir, 'quorum0.img'),
27 os.path.join(iotests.test_dir, 'quorum1.img'),
28 os.path.join(iotests.test_dir, 'quorum2.img'))
30 img_conf = (os.path.join(iotests.test_dir, 'quorum0.conf'),
31 os.path.join(iotests.test_dir, 'quorum1.conf'),
32 os.path.join(iotests.test_dir, 'quorum2.conf'))
34 event_rate = 1000000000
38 class TestQuorumEvents(iotests.QMPTestCase):
39 read_pattern = 'quorum'
41 def create_blkdebug_file(self, blkdebug_file, bad_sector):
42 file = open(blkdebug_file, 'w')
51 @iotests.skip_if_unsupported(['quorum'])
53 driveopts = ['driver=quorum', 'vote-threshold=2']
54 driveopts.append('read-pattern=%s' % self.read_pattern)
55 for i in range(len(imgs)):
56 iotests.qemu_img('create', '-f', iotests.imgfmt, imgs[i], '1M')
57 self.create_blkdebug_file(img_conf[i], i + offset)
58 driveopts.append('children.%d.driver=%s' % (i, iotests.imgfmt))
59 driveopts.append('children.%d.file.driver=blkdebug' % i)
60 driveopts.append('children.%d.file.config=%s' % (i, img_conf[i]))
61 driveopts.append('children.%d.file.image.filename=%s' % (i, imgs[i]))
62 driveopts.append('children.%d.node-name=img%d' % (i, i))
63 self.vm = iotests.VM()
64 self.vm.add_drive(None, opts = ','.join(driveopts))
69 for i in range(len(imgs)):
71 os.remove(img_conf[i])
73 def do_check_event(self, node, sector = 0):
75 self.assertEqual(self.vm.get_qmp_event(), None)
78 for event in self.vm.get_qmp_events(wait=True):
79 if event['event'] == 'QUORUM_REPORT_BAD':
80 self.assert_qmp(event, 'data/node-name', node)
81 self.assert_qmp(event, 'data/sector-num', sector)
84 # Generate an error and get an event
85 self.vm.hmp_qemu_io("drive0", "aio_read %d %d" %
86 (offset * sector_size, sector_size))
87 self.vm.qtest("clock_step 10")
88 self.do_check_event('img0', offset)
90 # I/O errors in the same child: only one event is emitted
93 self.vm.hmp_qemu_io("drive0", "aio_read %d %d" %
94 (offset * sector_size, sector_size))
95 self.vm.qtest("clock_step %d" % delay)
96 self.do_check_event(None)
98 # Wait enough so the event is finally emitted
99 self.vm.qtest("clock_step %d" % (2 * event_rate))
100 self.do_check_event('img0', offset)
102 # I/O errors in the same child: all events are emitted
103 delay = 2 * event_rate
105 self.vm.hmp_qemu_io("drive0", "aio_read %d %d" %
106 (offset * sector_size, sector_size))
107 self.vm.qtest("clock_step %d" % delay)
108 self.do_check_event('img0', offset)
110 # I/O errors in different children: all events are emitted
112 for i in range(len(imgs)):
113 self.vm.hmp_qemu_io("drive0", "aio_read %d %d" %
114 ((offset + i) * sector_size, sector_size))
115 self.vm.qtest("clock_step %d" % delay)
116 # In fifo mode only errors in the first child are detected
117 if i > 0 and self.read_pattern == 'fifo':
118 self.do_check_event(None)
120 self.do_check_event('img%d' % i, offset + i)
122 # I/O errors in different children: all events are emitted
123 delay = 2 * event_rate
124 for i in range(len(imgs)):
125 self.vm.hmp_qemu_io("drive0", "aio_read %d %d" %
126 ((offset + i) * sector_size, sector_size))
127 self.vm.qtest("clock_step %d" % delay)
128 # In fifo mode only errors in the first child are detected
129 if i > 0 and self.read_pattern == 'fifo':
130 self.do_check_event(None)
132 self.do_check_event('img%d' % i, offset + i)
134 # No more pending events
135 self.do_check_event(None)
137 class TestFifoQuorumEvents(TestQuorumEvents):
138 read_pattern = 'fifo'
140 if __name__ == '__main__':
141 iotests.verify_quorum()
142 iotests.main(supported_fmts=["raw"],
143 supported_protocols=["file"])