xref: /openbmc/qemu/tests/qemu-iotests/030 (revision 80adf54e)
1#!/usr/bin/env python
2#
3# Tests for image streaming.
4#
5# Copyright (C) 2012 IBM Corp.
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 time
22import os
23import iotests
24from iotests import qemu_img, qemu_io
25
26backing_img = os.path.join(iotests.test_dir, 'backing.img')
27mid_img = os.path.join(iotests.test_dir, 'mid.img')
28test_img = os.path.join(iotests.test_dir, 'test.img')
29
30class TestSingleDrive(iotests.QMPTestCase):
31    image_len = 1 * 1024 * 1024 # MB
32
33    def setUp(self):
34        iotests.create_image(backing_img, TestSingleDrive.image_len)
35        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
36        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
37        qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 512', backing_img)
38        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 524288 512', mid_img)
39        self.vm = iotests.VM().add_drive("blkdebug::" + test_img, "backing.node-name=mid")
40        self.vm.launch()
41
42    def tearDown(self):
43        self.vm.shutdown()
44        os.remove(test_img)
45        os.remove(mid_img)
46        os.remove(backing_img)
47
48    def test_stream(self):
49        self.assert_no_active_block_jobs()
50
51        result = self.vm.qmp('block-stream', device='drive0')
52        self.assert_qmp(result, 'return', {})
53
54        self.wait_until_completed()
55
56        self.assert_no_active_block_jobs()
57        self.vm.shutdown()
58
59        self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img),
60                         qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
61                         'image file map does not match backing file after streaming')
62
63    def test_stream_intermediate(self):
64        self.assert_no_active_block_jobs()
65
66        self.assertNotEqual(qemu_io('-f', 'raw', '-rU', '-c', 'map', backing_img),
67                            qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', mid_img),
68                            'image file map matches backing file before streaming')
69
70        result = self.vm.qmp('block-stream', device='mid', job_id='stream-mid')
71        self.assert_qmp(result, 'return', {})
72
73        self.wait_until_completed(drive='stream-mid')
74
75        self.assert_no_active_block_jobs()
76        self.vm.shutdown()
77
78        self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img),
79                         qemu_io('-f', iotests.imgfmt, '-c', 'map', mid_img),
80                         'image file map does not match backing file after streaming')
81
82    def test_stream_pause(self):
83        self.assert_no_active_block_jobs()
84
85        self.vm.pause_drive('drive0')
86        result = self.vm.qmp('block-stream', device='drive0')
87        self.assert_qmp(result, 'return', {})
88
89        result = self.vm.qmp('block-job-pause', device='drive0')
90        self.assert_qmp(result, 'return', {})
91
92        self.vm.resume_drive('drive0')
93        self.pause_job('drive0')
94
95        result = self.vm.qmp('query-block-jobs')
96        offset = self.dictpath(result, 'return[0]/offset')
97
98        time.sleep(0.5)
99        result = self.vm.qmp('query-block-jobs')
100        self.assert_qmp(result, 'return[0]/offset', offset)
101
102        result = self.vm.qmp('block-job-resume', device='drive0')
103        self.assert_qmp(result, 'return', {})
104
105        self.wait_until_completed()
106
107        self.assert_no_active_block_jobs()
108        self.vm.shutdown()
109
110        self.assertEqual(qemu_io('-f', 'raw', '-c', 'map', backing_img),
111                         qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
112                         'image file map does not match backing file after streaming')
113
114    def test_stream_no_op(self):
115        self.assert_no_active_block_jobs()
116
117        # The image map is empty before the operation
118        empty_map = qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', test_img)
119
120        # This is a no-op: no data should ever be copied from the base image
121        result = self.vm.qmp('block-stream', device='drive0', base=mid_img)
122        self.assert_qmp(result, 'return', {})
123
124        self.wait_until_completed()
125
126        self.assert_no_active_block_jobs()
127        self.vm.shutdown()
128
129        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
130                         empty_map, 'image file map changed after a no-op')
131
132    def test_stream_partial(self):
133        self.assert_no_active_block_jobs()
134
135        result = self.vm.qmp('block-stream', device='drive0', base=backing_img)
136        self.assert_qmp(result, 'return', {})
137
138        self.wait_until_completed()
139
140        self.assert_no_active_block_jobs()
141        self.vm.shutdown()
142
143        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', mid_img),
144                         qemu_io('-f', iotests.imgfmt, '-c', 'map', test_img),
145                         'image file map does not match backing file after streaming')
146
147    def test_device_not_found(self):
148        result = self.vm.qmp('block-stream', device='nonexistent')
149        self.assert_qmp(result, 'error/class', 'GenericError')
150
151    def test_job_id_missing(self):
152        result = self.vm.qmp('block-stream', device='mid')
153        self.assert_qmp(result, 'error/class', 'GenericError')
154
155
156class TestParallelOps(iotests.QMPTestCase):
157    num_ops = 4 # Number of parallel block-stream operations
158    num_imgs = num_ops * 2 + 1
159    image_len = num_ops * 1024 * 1024
160    imgs = []
161
162    def setUp(self):
163        opts = []
164        self.imgs = []
165
166        # Initialize file names and command-line options
167        for i in range(self.num_imgs):
168            img_depth = self.num_imgs - i - 1
169            opts.append("backing." * img_depth + "node-name=node%d" % i)
170            self.imgs.append(os.path.join(iotests.test_dir, 'img-%d.img' % i))
171
172        # Create all images
173        iotests.create_image(self.imgs[0], self.image_len)
174        for i in range(1, self.num_imgs):
175            qemu_img('create', '-f', iotests.imgfmt,
176                     '-o', 'backing_file=%s' % self.imgs[i-1], self.imgs[i])
177
178        # Put data into the images we are copying data from
179        for i in range(self.num_imgs / 2):
180            img_index = i * 2 + 1
181            # Alternate between 512k and 1M.
182            # This way jobs will not finish in the same order they were created
183            num_kb = 512 + 512 * (i % 2)
184            qemu_io('-f', iotests.imgfmt,
185                    '-c', 'write -P %d %d %d' % (i, i*1024*1024, num_kb * 1024),
186                    self.imgs[img_index])
187
188        # Attach the drive to the VM
189        self.vm = iotests.VM()
190        self.vm.add_drive(self.imgs[-1], ','.join(opts))
191        self.vm.launch()
192
193    def tearDown(self):
194        self.vm.shutdown()
195        for img in self.imgs:
196            os.remove(img)
197
198    # Test that it's possible to run several block-stream operations
199    # in parallel in the same snapshot chain
200    def test_stream_parallel(self):
201        self.assert_no_active_block_jobs()
202
203        # Check that the maps don't match before the streaming operations
204        for i in range(2, self.num_imgs, 2):
205            self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[i]),
206                                qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[i-1]),
207                                'image file map matches backing file before streaming')
208
209        # Create all streaming jobs
210        pending_jobs = []
211        for i in range(2, self.num_imgs, 2):
212            node_name = 'node%d' % i
213            job_id = 'stream-%s' % node_name
214            pending_jobs.append(job_id)
215            result = self.vm.qmp('block-stream', device=node_name, job_id=job_id, base=self.imgs[i-2], speed=512*1024)
216            self.assert_qmp(result, 'return', {})
217
218        # Wait for all jobs to be finished.
219        while len(pending_jobs) > 0:
220            for event in self.vm.get_qmp_events(wait=True):
221                if event['event'] == 'BLOCK_JOB_COMPLETED':
222                    job_id = self.dictpath(event, 'data/device')
223                    self.assertTrue(job_id in pending_jobs)
224                    self.assert_qmp_absent(event, 'data/error')
225                    pending_jobs.remove(job_id)
226
227        self.assert_no_active_block_jobs()
228        self.vm.shutdown()
229
230        # Check that all maps match now
231        for i in range(2, self.num_imgs, 2):
232            self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[i]),
233                             qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[i-1]),
234                             'image file map does not match backing file after streaming')
235
236    # Test that it's not possible to perform two block-stream
237    # operations if there are nodes involved in both.
238    def test_overlapping_1(self):
239        self.assert_no_active_block_jobs()
240
241        # Set a speed limit to make sure that this job blocks the rest
242        result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4', base=self.imgs[1], speed=1024*1024)
243        self.assert_qmp(result, 'return', {})
244
245        result = self.vm.qmp('block-stream', device='node5', job_id='stream-node5', base=self.imgs[2])
246        self.assert_qmp(result, 'error/class', 'GenericError')
247
248        result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3', base=self.imgs[2])
249        self.assert_qmp(result, 'error/class', 'GenericError')
250
251        result = self.vm.qmp('block-stream', device='node4', job_id='stream-node4-v2')
252        self.assert_qmp(result, 'error/class', 'GenericError')
253
254        # block-commit should also fail if it touches nodes used by the stream job
255        result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[4], job_id='commit-node4')
256        self.assert_qmp(result, 'error/class', 'GenericError')
257
258        result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[1], top=self.imgs[3], job_id='commit-node1')
259        self.assert_qmp(result, 'error/class', 'GenericError')
260
261        # This fails because it needs to modify the backing string in node2, which is blocked
262        result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[0], top=self.imgs[1], job_id='commit-node0')
263        self.assert_qmp(result, 'error/class', 'GenericError')
264
265        self.wait_until_completed(drive='stream-node4')
266        self.assert_no_active_block_jobs()
267
268    # Similar to test_overlapping_1, but with block-commit
269    # blocking the other jobs
270    def test_overlapping_2(self):
271        self.assertLessEqual(9, self.num_imgs)
272        self.assert_no_active_block_jobs()
273
274        # Set a speed limit to make sure that this job blocks the rest
275        result = self.vm.qmp('block-commit', device='drive0', top=self.imgs[5], base=self.imgs[3], job_id='commit-node3', speed=1024*1024)
276        self.assert_qmp(result, 'return', {})
277
278        result = self.vm.qmp('block-stream', device='node3', job_id='stream-node3')
279        self.assert_qmp(result, 'error/class', 'GenericError')
280
281        result = self.vm.qmp('block-stream', device='node6', base=self.imgs[2], job_id='stream-node6')
282        self.assert_qmp(result, 'error/class', 'GenericError')
283
284        result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], job_id='stream-node4')
285        self.assert_qmp(result, 'error/class', 'GenericError')
286
287        result = self.vm.qmp('block-stream', device='node6', base=self.imgs[4], job_id='stream-node6-v2')
288        self.assert_qmp(result, 'error/class', 'GenericError')
289
290        # This fails because block-commit needs to block node6, the overlay of the 'top' image
291        result = self.vm.qmp('block-stream', device='node7', base=self.imgs[5], job_id='stream-node6-v3')
292        self.assert_qmp(result, 'error/class', 'GenericError')
293
294        # This fails because block-commit currently blocks the active layer even if it's not used
295        result = self.vm.qmp('block-stream', device='drive0', base=self.imgs[5], job_id='stream-drive0')
296        self.assert_qmp(result, 'error/class', 'GenericError')
297
298        self.wait_until_completed(drive='commit-node3')
299
300    # Similar to test_overlapping_2, but here block-commit doesn't use the 'top' parameter.
301    # Internally this uses a mirror block job, hence the separate test case.
302    def test_overlapping_3(self):
303        self.assertLessEqual(8, self.num_imgs)
304        self.assert_no_active_block_jobs()
305
306        # Set a speed limit to make sure that this job blocks the rest
307        result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3], job_id='commit-drive0', speed=1024*1024)
308        self.assert_qmp(result, 'return', {})
309
310        result = self.vm.qmp('block-stream', device='node5', base=self.imgs[3], job_id='stream-node6')
311        self.assert_qmp(result, 'error/class', 'GenericError')
312
313        event = self.vm.get_qmp_event(wait=True)
314        self.assertEqual(event['event'], 'BLOCK_JOB_READY')
315        self.assert_qmp(event, 'data/device', 'commit-drive0')
316        self.assert_qmp(event, 'data/type', 'commit')
317        self.assert_qmp_absent(event, 'data/error')
318
319        result = self.vm.qmp('block-job-complete', device='commit-drive0')
320        self.assert_qmp(result, 'return', {})
321
322        self.wait_until_completed(drive='commit-drive0')
323
324    # Test a block-stream and a block-commit job in parallel
325    def test_stream_commit(self):
326        self.assertLessEqual(8, self.num_imgs)
327        self.assert_no_active_block_jobs()
328
329        # Stream from node0 into node2
330        result = self.vm.qmp('block-stream', device='node2', job_id='node2')
331        self.assert_qmp(result, 'return', {})
332
333        # Commit from the active layer into node3
334        result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3])
335        self.assert_qmp(result, 'return', {})
336
337        # Wait for all jobs to be finished.
338        pending_jobs = ['node2', 'drive0']
339        while len(pending_jobs) > 0:
340            for event in self.vm.get_qmp_events(wait=True):
341                if event['event'] == 'BLOCK_JOB_COMPLETED':
342                    node_name = self.dictpath(event, 'data/device')
343                    self.assertTrue(node_name in pending_jobs)
344                    self.assert_qmp_absent(event, 'data/error')
345                    pending_jobs.remove(node_name)
346                if event['event'] == 'BLOCK_JOB_READY':
347                    self.assert_qmp(event, 'data/device', 'drive0')
348                    self.assert_qmp(event, 'data/type', 'commit')
349                    self.assert_qmp_absent(event, 'data/error')
350                    self.assertTrue('drive0' in pending_jobs)
351                    self.vm.qmp('block-job-complete', device='drive0')
352
353        self.assert_no_active_block_jobs()
354
355    # Test the base_node parameter
356    def test_stream_base_node_name(self):
357        self.assert_no_active_block_jobs()
358
359        self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[4]),
360                            qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[3]),
361                            'image file map matches backing file before streaming')
362
363        # Error: the base node does not exist
364        result = self.vm.qmp('block-stream', device='node4', base_node='none', job_id='stream')
365        self.assert_qmp(result, 'error/class', 'GenericError')
366
367        # Error: the base node is not a backing file of the top node
368        result = self.vm.qmp('block-stream', device='node4', base_node='node6', job_id='stream')
369        self.assert_qmp(result, 'error/class', 'GenericError')
370
371        # Error: the base node is the same as the top node
372        result = self.vm.qmp('block-stream', device='node4', base_node='node4', job_id='stream')
373        self.assert_qmp(result, 'error/class', 'GenericError')
374
375        # Error: cannot specify 'base' and 'base-node' at the same time
376        result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], base_node='node2', job_id='stream')
377        self.assert_qmp(result, 'error/class', 'GenericError')
378
379        # Success: the base node is a backing file of the top node
380        result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='stream')
381        self.assert_qmp(result, 'return', {})
382
383        self.wait_until_completed(drive='stream')
384
385        self.assert_no_active_block_jobs()
386        self.vm.shutdown()
387
388        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[4]),
389                         qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[3]),
390                         'image file map matches backing file after streaming')
391
392class TestQuorum(iotests.QMPTestCase):
393    num_children = 3
394    children = []
395    backing = []
396
397    def setUp(self):
398        opts = ['driver=quorum', 'vote-threshold=2']
399
400        # Initialize file names and command-line options
401        for i in range(self.num_children):
402            child_img = os.path.join(iotests.test_dir, 'img-%d.img' % i)
403            backing_img = os.path.join(iotests.test_dir, 'backing-%d.img' % i)
404            self.children.append(child_img)
405            self.backing.append(backing_img)
406            qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M')
407            qemu_io('-f', iotests.imgfmt,
408                    '-c', 'write -P 0x55 0 1024', backing_img)
409            qemu_img('create', '-f', iotests.imgfmt,
410                     '-o', 'backing_file=%s' % backing_img, child_img)
411            opts.append("children.%d.file.filename=%s" % (i, child_img))
412            opts.append("children.%d.node-name=node%d" % (i, i))
413
414        # Attach the drive to the VM
415        self.vm = iotests.VM()
416        self.vm.add_drive(path = None, opts = ','.join(opts))
417        self.vm.launch()
418
419    def tearDown(self):
420        self.vm.shutdown()
421        for img in self.children:
422            os.remove(img)
423        for img in self.backing:
424            os.remove(img)
425
426    def test_stream_quorum(self):
427        if not iotests.supports_quorum():
428            return
429
430        self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.children[0]),
431                            qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.backing[0]),
432                            'image file map matches backing file before streaming')
433
434        self.assert_no_active_block_jobs()
435
436        result = self.vm.qmp('block-stream', device='node0', job_id='stream-node0')
437        self.assert_qmp(result, 'return', {})
438
439        self.wait_until_completed(drive='stream-node0')
440
441        self.assert_no_active_block_jobs()
442        self.vm.shutdown()
443
444        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.children[0]),
445                         qemu_io('-f', iotests.imgfmt, '-c', 'map', self.backing[0]),
446                         'image file map does not match backing file after streaming')
447
448class TestSmallerBackingFile(iotests.QMPTestCase):
449    backing_len = 1 * 1024 * 1024 # MB
450    image_len = 2 * backing_len
451
452    def setUp(self):
453        iotests.create_image(backing_img, self.backing_len)
454        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img, str(self.image_len))
455        self.vm = iotests.VM().add_drive(test_img)
456        self.vm.launch()
457
458    # If this hangs, then you are missing a fix to complete streaming when the
459    # end of the backing file is reached.
460    def test_stream(self):
461        self.assert_no_active_block_jobs()
462
463        result = self.vm.qmp('block-stream', device='drive0')
464        self.assert_qmp(result, 'return', {})
465
466        self.wait_until_completed()
467
468        self.assert_no_active_block_jobs()
469        self.vm.shutdown()
470
471class TestErrors(iotests.QMPTestCase):
472    image_len = 2 * 1024 * 1024 # MB
473
474    # this should match STREAM_BUFFER_SIZE/512 in block/stream.c
475    STREAM_BUFFER_SIZE = 512 * 1024
476
477    def create_blkdebug_file(self, name, event, errno):
478        file = open(name, 'w')
479        file.write('''
480[inject-error]
481state = "1"
482event = "%s"
483errno = "%d"
484immediately = "off"
485once = "on"
486sector = "%d"
487
488[set-state]
489state = "1"
490event = "%s"
491new_state = "2"
492
493[set-state]
494state = "2"
495event = "%s"
496new_state = "1"
497''' % (event, errno, self.STREAM_BUFFER_SIZE / 512, event, event))
498        file.close()
499
500class TestEIO(TestErrors):
501    def setUp(self):
502        self.blkdebug_file = backing_img + ".blkdebug"
503        iotests.create_image(backing_img, TestErrors.image_len)
504        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
505        qemu_img('create', '-f', iotests.imgfmt,
506                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
507                       % (self.blkdebug_file, backing_img),
508                 test_img)
509        self.vm = iotests.VM().add_drive(test_img)
510        self.vm.launch()
511
512    def tearDown(self):
513        self.vm.shutdown()
514        os.remove(test_img)
515        os.remove(backing_img)
516        os.remove(self.blkdebug_file)
517
518    def test_report(self):
519        self.assert_no_active_block_jobs()
520
521        result = self.vm.qmp('block-stream', device='drive0')
522        self.assert_qmp(result, 'return', {})
523
524        completed = False
525        error = False
526        while not completed:
527            for event in self.vm.get_qmp_events(wait=True):
528                if event['event'] == 'BLOCK_JOB_ERROR':
529                    self.assert_qmp(event, 'data/device', 'drive0')
530                    self.assert_qmp(event, 'data/operation', 'read')
531                    error = True
532                elif event['event'] == 'BLOCK_JOB_COMPLETED':
533                    self.assertTrue(error, 'job completed unexpectedly')
534                    self.assert_qmp(event, 'data/type', 'stream')
535                    self.assert_qmp(event, 'data/device', 'drive0')
536                    self.assert_qmp(event, 'data/error', 'Input/output error')
537                    self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
538                    self.assert_qmp(event, 'data/len', self.image_len)
539                    completed = True
540
541        self.assert_no_active_block_jobs()
542        self.vm.shutdown()
543
544    def test_ignore(self):
545        self.assert_no_active_block_jobs()
546
547        result = self.vm.qmp('block-stream', device='drive0', on_error='ignore')
548        self.assert_qmp(result, 'return', {})
549
550        error = False
551        completed = False
552        while not completed:
553            for event in self.vm.get_qmp_events(wait=True):
554                if event['event'] == 'BLOCK_JOB_ERROR':
555                    error = True
556                    self.assert_qmp(event, 'data/device', 'drive0')
557                    self.assert_qmp(event, 'data/operation', 'read')
558                    result = self.vm.qmp('query-block-jobs')
559                    if result == {'return': []}:
560                        # Job finished too quickly
561                        continue
562                    self.assert_qmp(result, 'return[0]/paused', False)
563                elif event['event'] == 'BLOCK_JOB_COMPLETED':
564                    self.assertTrue(error, 'job completed unexpectedly')
565                    self.assert_qmp(event, 'data/type', 'stream')
566                    self.assert_qmp(event, 'data/device', 'drive0')
567                    self.assert_qmp(event, 'data/error', 'Input/output error')
568                    self.assert_qmp(event, 'data/offset', self.image_len)
569                    self.assert_qmp(event, 'data/len', self.image_len)
570                    completed = True
571
572        self.assert_no_active_block_jobs()
573        self.vm.shutdown()
574
575    def test_stop(self):
576        self.assert_no_active_block_jobs()
577
578        result = self.vm.qmp('block-stream', device='drive0', on_error='stop')
579        self.assert_qmp(result, 'return', {})
580
581        error = False
582        completed = False
583        while not completed:
584            for event in self.vm.get_qmp_events(wait=True):
585                if event['event'] == 'BLOCK_JOB_ERROR':
586                    error = True
587                    self.assert_qmp(event, 'data/device', 'drive0')
588                    self.assert_qmp(event, 'data/operation', 'read')
589
590                    result = self.vm.qmp('query-block-jobs')
591                    self.assert_qmp(result, 'return[0]/paused', True)
592                    self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
593                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
594
595                    result = self.vm.qmp('block-job-resume', device='drive0')
596                    self.assert_qmp(result, 'return', {})
597
598                    result = self.vm.qmp('query-block-jobs')
599                    if result == {'return': []}:
600                        # Race; likely already finished. Check.
601                        continue
602                    self.assert_qmp(result, 'return[0]/paused', False)
603                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
604                elif event['event'] == 'BLOCK_JOB_COMPLETED':
605                    self.assertTrue(error, 'job completed unexpectedly')
606                    self.assert_qmp(event, 'data/type', 'stream')
607                    self.assert_qmp(event, 'data/device', 'drive0')
608                    self.assert_qmp_absent(event, 'data/error')
609                    self.assert_qmp(event, 'data/offset', self.image_len)
610                    self.assert_qmp(event, 'data/len', self.image_len)
611                    completed = True
612
613        self.assert_no_active_block_jobs()
614        self.vm.shutdown()
615
616    def test_enospc(self):
617        self.assert_no_active_block_jobs()
618
619        result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
620        self.assert_qmp(result, 'return', {})
621
622        completed = False
623        error = False
624        while not completed:
625            for event in self.vm.get_qmp_events(wait=True):
626                if event['event'] == 'BLOCK_JOB_ERROR':
627                    self.assert_qmp(event, 'data/device', 'drive0')
628                    self.assert_qmp(event, 'data/operation', 'read')
629                    error = True
630                elif event['event'] == 'BLOCK_JOB_COMPLETED':
631                    self.assertTrue(error, 'job completed unexpectedly')
632                    self.assert_qmp(event, 'data/type', 'stream')
633                    self.assert_qmp(event, 'data/device', 'drive0')
634                    self.assert_qmp(event, 'data/error', 'Input/output error')
635                    self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
636                    self.assert_qmp(event, 'data/len', self.image_len)
637                    completed = True
638
639        self.assert_no_active_block_jobs()
640        self.vm.shutdown()
641
642class TestENOSPC(TestErrors):
643    def setUp(self):
644        self.blkdebug_file = backing_img + ".blkdebug"
645        iotests.create_image(backing_img, TestErrors.image_len)
646        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 28)
647        qemu_img('create', '-f', iotests.imgfmt,
648                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
649                       % (self.blkdebug_file, backing_img),
650                 test_img)
651        self.vm = iotests.VM().add_drive(test_img)
652        self.vm.launch()
653
654    def tearDown(self):
655        self.vm.shutdown()
656        os.remove(test_img)
657        os.remove(backing_img)
658        os.remove(self.blkdebug_file)
659
660    def test_enospc(self):
661        self.assert_no_active_block_jobs()
662
663        result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
664        self.assert_qmp(result, 'return', {})
665
666        error = False
667        completed = False
668        while not completed:
669            for event in self.vm.get_qmp_events(wait=True):
670                if event['event'] == 'BLOCK_JOB_ERROR':
671                    self.assert_qmp(event, 'data/device', 'drive0')
672                    self.assert_qmp(event, 'data/operation', 'read')
673
674                    result = self.vm.qmp('query-block-jobs')
675                    self.assert_qmp(result, 'return[0]/paused', True)
676                    self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
677                    self.assert_qmp(result, 'return[0]/io-status', 'nospace')
678
679                    result = self.vm.qmp('block-job-resume', device='drive0')
680                    self.assert_qmp(result, 'return', {})
681
682                    result = self.vm.qmp('query-block-jobs')
683                    self.assert_qmp(result, 'return[0]/paused', False)
684                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
685                    error = True
686                elif event['event'] == 'BLOCK_JOB_COMPLETED':
687                    self.assertTrue(error, 'job completed unexpectedly')
688                    self.assert_qmp(event, 'data/type', 'stream')
689                    self.assert_qmp(event, 'data/device', 'drive0')
690                    self.assert_qmp_absent(event, 'data/error')
691                    self.assert_qmp(event, 'data/offset', self.image_len)
692                    self.assert_qmp(event, 'data/len', self.image_len)
693                    completed = True
694
695        self.assert_no_active_block_jobs()
696        self.vm.shutdown()
697
698class TestStreamStop(iotests.QMPTestCase):
699    image_len = 8 * 1024 * 1024 * 1024 # GB
700
701    def setUp(self):
702        qemu_img('create', backing_img, str(TestStreamStop.image_len))
703        qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
704        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
705        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
706        self.vm = iotests.VM().add_drive("blkdebug::" + test_img)
707        self.vm.launch()
708
709    def tearDown(self):
710        self.vm.shutdown()
711        os.remove(test_img)
712        os.remove(backing_img)
713
714    def test_stream_stop(self):
715        self.assert_no_active_block_jobs()
716
717        self.vm.pause_drive('drive0')
718        result = self.vm.qmp('block-stream', device='drive0')
719        self.assert_qmp(result, 'return', {})
720
721        time.sleep(0.1)
722        events = self.vm.get_qmp_events(wait=False)
723        self.assertEqual(events, [], 'unexpected QMP event: %s' % events)
724
725        self.cancel_and_wait(resume=True)
726
727class TestSetSpeed(iotests.QMPTestCase):
728    image_len = 80 * 1024 * 1024 # MB
729
730    def setUp(self):
731        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
732        qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
733        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
734        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
735        self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
736        self.vm.launch()
737
738    def tearDown(self):
739        self.vm.shutdown()
740        os.remove(test_img)
741        os.remove(backing_img)
742
743    # This is a short performance test which is not run by default.
744    # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
745    def perf_test_throughput(self):
746        self.assert_no_active_block_jobs()
747
748        result = self.vm.qmp('block-stream', device='drive0')
749        self.assert_qmp(result, 'return', {})
750
751        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
752        self.assert_qmp(result, 'return', {})
753
754        self.wait_until_completed()
755
756        self.assert_no_active_block_jobs()
757
758    def test_set_speed(self):
759        self.assert_no_active_block_jobs()
760
761        self.vm.pause_drive('drive0')
762        result = self.vm.qmp('block-stream', device='drive0')
763        self.assert_qmp(result, 'return', {})
764
765        # Default speed is 0
766        result = self.vm.qmp('query-block-jobs')
767        self.assert_qmp(result, 'return[0]/device', 'drive0')
768        self.assert_qmp(result, 'return[0]/speed', 0)
769
770        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
771        self.assert_qmp(result, 'return', {})
772
773        # Ensure the speed we set was accepted
774        result = self.vm.qmp('query-block-jobs')
775        self.assert_qmp(result, 'return[0]/device', 'drive0')
776        self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
777
778        self.cancel_and_wait(resume=True)
779        self.vm.pause_drive('drive0')
780
781        # Check setting speed in block-stream works
782        result = self.vm.qmp('block-stream', device='drive0', speed=4 * 1024 * 1024)
783        self.assert_qmp(result, 'return', {})
784
785        result = self.vm.qmp('query-block-jobs')
786        self.assert_qmp(result, 'return[0]/device', 'drive0')
787        self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
788
789        self.cancel_and_wait(resume=True)
790
791    def test_set_speed_invalid(self):
792        self.assert_no_active_block_jobs()
793
794        result = self.vm.qmp('block-stream', device='drive0', speed=-1)
795        self.assert_qmp(result, 'error/class', 'GenericError')
796
797        self.assert_no_active_block_jobs()
798
799        result = self.vm.qmp('block-stream', device='drive0')
800        self.assert_qmp(result, 'return', {})
801
802        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
803        self.assert_qmp(result, 'error/class', 'GenericError')
804
805        self.cancel_and_wait()
806
807if __name__ == '__main__':
808    iotests.main(supported_fmts=['qcow2', 'qed'])
809