3 # Tests for image streaming.
5 # Copyright (C) 2012 IBM Corp.
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.
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.
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/>.
23 from iotests
import qemu_img
, qemu_io
25 backing_img
= os
.path
.join(iotests
.test_dir
, 'backing.img')
26 mid_img
= os
.path
.join(iotests
.test_dir
, 'mid.img')
27 test_img
= os
.path
.join(iotests
.test_dir
, 'test.img')
29 class ImageStreamingTestCase(iotests
.QMPTestCase
):
30 '''Abstract base class for image streaming test cases'''
32 def assert_no_active_streams(self
):
33 result
= self
.vm
.qmp('query-block-jobs')
34 self
.assert_qmp(result
, 'return', [])
36 def cancel_and_wait(self
, drive
='drive0'):
37 '''Cancel a block job and wait for it to finish'''
38 result
= self
.vm
.qmp('block-job-cancel', device
=drive
)
39 self
.assert_qmp(result
, 'return', {})
43 for event
in self
.vm
.get_qmp_events(wait
=True):
44 if event
['event'] == 'BLOCK_JOB_CANCELLED':
45 self
.assert_qmp(event
, 'data/type', 'stream')
46 self
.assert_qmp(event
, 'data/device', drive
)
49 self
.assert_no_active_streams()
51 class TestSingleDrive(ImageStreamingTestCase
):
52 image_len
= 1 * 1024 * 1024 # MB
55 qemu_img('create', backing_img
, str(TestSingleDrive
.image_len
))
56 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, mid_img
)
57 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % mid_img
, test_img
)
58 self
.vm
= iotests
.VM().add_drive(test_img
)
65 os
.remove(backing_img
)
67 def test_stream(self
):
68 self
.assert_no_active_streams()
70 result
= self
.vm
.qmp('block-stream', device
='drive0')
71 self
.assert_qmp(result
, 'return', {})
75 for event
in self
.vm
.get_qmp_events(wait
=True):
76 if event
['event'] == 'BLOCK_JOB_COMPLETED':
77 self
.assert_qmp(event
, 'data/type', 'stream')
78 self
.assert_qmp(event
, 'data/device', 'drive0')
79 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
80 self
.assert_qmp(event
, 'data/len', self
.image_len
)
83 self
.assert_no_active_streams()
86 self
.assertEqual(qemu_io('-c', 'map', backing_img
),
87 qemu_io('-c', 'map', test_img
),
88 'image file map does not match backing file after streaming')
90 def test_stream_partial(self
):
91 self
.assert_no_active_streams()
93 result
= self
.vm
.qmp('block-stream', device
='drive0', base
=mid_img
)
94 self
.assert_qmp(result
, 'return', {})
98 for event
in self
.vm
.get_qmp_events(wait
=True):
99 if event
['event'] == 'BLOCK_JOB_COMPLETED':
100 self
.assert_qmp(event
, 'data/type', 'stream')
101 self
.assert_qmp(event
, 'data/device', 'drive0')
102 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
103 self
.assert_qmp(event
, 'data/len', self
.image_len
)
106 self
.assert_no_active_streams()
109 self
.assertEqual(qemu_io('-c', 'map', mid_img
),
110 qemu_io('-c', 'map', test_img
),
111 'image file map does not match backing file after streaming')
113 def test_device_not_found(self
):
114 result
= self
.vm
.qmp('block-stream', device
='nonexistent')
115 self
.assert_qmp(result
, 'error/class', 'DeviceNotFound')
117 class TestStreamStop(ImageStreamingTestCase
):
118 image_len
= 8 * 1024 * 1024 * 1024 # GB
121 qemu_img('create', backing_img
, str(TestStreamStop
.image_len
))
122 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
123 self
.vm
= iotests
.VM().add_drive(test_img
)
129 os
.remove(backing_img
)
131 def test_stream_stop(self
):
134 self
.assert_no_active_streams()
136 result
= self
.vm
.qmp('block-stream', device
='drive0')
137 self
.assert_qmp(result
, 'return', {})
140 events
= self
.vm
.get_qmp_events(wait
=False)
141 self
.assertEqual(events
, [], 'unexpected QMP event: %s' % events
)
143 self
.cancel_and_wait()
145 class TestSetSpeed(ImageStreamingTestCase
):
146 image_len
= 80 * 1024 * 1024 # MB
149 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
150 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
151 self
.vm
= iotests
.VM().add_drive(test_img
)
157 os
.remove(backing_img
)
159 # This is a short performance test which is not run by default.
160 # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
161 def perf_test_throughput(self
):
162 self
.assert_no_active_streams()
164 result
= self
.vm
.qmp('block-stream', device
='drive0')
165 self
.assert_qmp(result
, 'return', {})
167 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
168 self
.assert_qmp(result
, 'return', {})
172 for event
in self
.vm
.get_qmp_events(wait
=True):
173 if event
['event'] == 'BLOCK_JOB_COMPLETED':
174 self
.assert_qmp(event
, 'data/type', 'stream')
175 self
.assert_qmp(event
, 'data/device', 'drive0')
176 self
.assert_qmp(event
, 'data/offset', self
.image_len
)
177 self
.assert_qmp(event
, 'data/len', self
.image_len
)
180 self
.assert_no_active_streams()
182 def test_set_speed(self
):
183 self
.assert_no_active_streams()
185 result
= self
.vm
.qmp('block-stream', device
='drive0')
186 self
.assert_qmp(result
, 'return', {})
189 result
= self
.vm
.qmp('query-block-jobs')
190 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
191 self
.assert_qmp(result
, 'return[0]/speed', 0)
193 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
194 self
.assert_qmp(result
, 'return', {})
196 # Ensure the speed we set was accepted
197 result
= self
.vm
.qmp('query-block-jobs')
198 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
199 self
.assert_qmp(result
, 'return[0]/speed', 8 * 1024 * 1024)
201 self
.cancel_and_wait()
203 # Check setting speed in block-stream works
204 result
= self
.vm
.qmp('block-stream', device
='drive0', speed
=4 * 1024 * 1024)
205 self
.assert_qmp(result
, 'return', {})
207 result
= self
.vm
.qmp('query-block-jobs')
208 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
209 self
.assert_qmp(result
, 'return[0]/speed', 4 * 1024 * 1024)
211 self
.cancel_and_wait()
213 def test_set_speed_invalid(self
):
214 self
.assert_no_active_streams()
216 result
= self
.vm
.qmp('block-stream', device
='drive0', speed
=-1)
217 self
.assert_qmp(result
, 'error/class', 'InvalidParameter')
218 self
.assert_qmp(result
, 'error/data/name', 'speed')
220 self
.assert_no_active_streams()
222 result
= self
.vm
.qmp('block-stream', device
='drive0')
223 self
.assert_qmp(result
, 'return', {})
225 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=-1)
226 self
.assert_qmp(result
, 'error/class', 'InvalidParameter')
227 self
.assert_qmp(result
, 'error/data/name', 'speed')
229 self
.cancel_and_wait()
231 if __name__
== '__main__':
232 iotests
.main(supported_fmts
=['qcow2', 'qed'])