xref: /openbmc/qemu/tests/qemu-iotests/030 (revision 83974cf4)
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 currently blocks the active layer even if it's not used
291        result = self.vm.qmp('block-stream', device='drive0', base=self.imgs[5], job_id='stream-drive0')
292        self.assert_qmp(result, 'error/class', 'GenericError')
293
294        self.wait_until_completed(drive='commit-node3')
295
296    # Similar to test_overlapping_2, but here block-commit doesn't use the 'top' parameter.
297    # Internally this uses a mirror block job, hence the separate test case.
298    def test_overlapping_3(self):
299        self.assertLessEqual(8, self.num_imgs)
300        self.assert_no_active_block_jobs()
301
302        # Set a speed limit to make sure that this job blocks the rest
303        result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3], job_id='commit-drive0', speed=1024*1024)
304        self.assert_qmp(result, 'return', {})
305
306        result = self.vm.qmp('block-stream', device='node5', base=self.imgs[3], job_id='stream-node6')
307        self.assert_qmp(result, 'error/class', 'GenericError')
308
309        event = self.vm.get_qmp_event(wait=True)
310        self.assertEqual(event['event'], 'BLOCK_JOB_READY')
311        self.assert_qmp(event, 'data/device', 'commit-drive0')
312        self.assert_qmp(event, 'data/type', 'commit')
313        self.assert_qmp_absent(event, 'data/error')
314
315        result = self.vm.qmp('block-job-complete', device='commit-drive0')
316        self.assert_qmp(result, 'return', {})
317
318        self.wait_until_completed(drive='commit-drive0')
319
320    # Test a block-stream and a block-commit job in parallel
321    def test_stream_commit(self):
322        self.assertLessEqual(8, self.num_imgs)
323        self.assert_no_active_block_jobs()
324
325        # Stream from node0 into node2
326        result = self.vm.qmp('block-stream', device='node2', job_id='node2')
327        self.assert_qmp(result, 'return', {})
328
329        # Commit from the active layer into node3
330        result = self.vm.qmp('block-commit', device='drive0', base=self.imgs[3])
331        self.assert_qmp(result, 'return', {})
332
333        # Wait for all jobs to be finished.
334        pending_jobs = ['node2', 'drive0']
335        while len(pending_jobs) > 0:
336            for event in self.vm.get_qmp_events(wait=True):
337                if event['event'] == 'BLOCK_JOB_COMPLETED':
338                    node_name = self.dictpath(event, 'data/device')
339                    self.assertTrue(node_name in pending_jobs)
340                    self.assert_qmp_absent(event, 'data/error')
341                    pending_jobs.remove(node_name)
342                if event['event'] == 'BLOCK_JOB_READY':
343                    self.assert_qmp(event, 'data/device', 'drive0')
344                    self.assert_qmp(event, 'data/type', 'commit')
345                    self.assert_qmp_absent(event, 'data/error')
346                    self.assertTrue('drive0' in pending_jobs)
347                    self.vm.qmp('block-job-complete', device='drive0')
348
349        self.assert_no_active_block_jobs()
350
351    # Test the base_node parameter
352    def test_stream_base_node_name(self):
353        self.assert_no_active_block_jobs()
354
355        self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[4]),
356                            qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.imgs[3]),
357                            'image file map matches backing file before streaming')
358
359        # Error: the base node does not exist
360        result = self.vm.qmp('block-stream', device='node4', base_node='none', job_id='stream')
361        self.assert_qmp(result, 'error/class', 'GenericError')
362
363        # Error: the base node is not a backing file of the top node
364        result = self.vm.qmp('block-stream', device='node4', base_node='node6', job_id='stream')
365        self.assert_qmp(result, 'error/class', 'GenericError')
366
367        # Error: the base node is the same as the top node
368        result = self.vm.qmp('block-stream', device='node4', base_node='node4', job_id='stream')
369        self.assert_qmp(result, 'error/class', 'GenericError')
370
371        # Error: cannot specify 'base' and 'base-node' at the same time
372        result = self.vm.qmp('block-stream', device='node4', base=self.imgs[2], base_node='node2', job_id='stream')
373        self.assert_qmp(result, 'error/class', 'GenericError')
374
375        # Success: the base node is a backing file of the top node
376        result = self.vm.qmp('block-stream', device='node4', base_node='node2', job_id='stream')
377        self.assert_qmp(result, 'return', {})
378
379        self.wait_until_completed(drive='stream')
380
381        self.assert_no_active_block_jobs()
382        self.vm.shutdown()
383
384        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[4]),
385                         qemu_io('-f', iotests.imgfmt, '-c', 'map', self.imgs[3]),
386                         'image file map matches backing file after streaming')
387
388class TestQuorum(iotests.QMPTestCase):
389    num_children = 3
390    children = []
391    backing = []
392
393    def setUp(self):
394        opts = ['driver=quorum', 'vote-threshold=2']
395
396        # Initialize file names and command-line options
397        for i in range(self.num_children):
398            child_img = os.path.join(iotests.test_dir, 'img-%d.img' % i)
399            backing_img = os.path.join(iotests.test_dir, 'backing-%d.img' % i)
400            self.children.append(child_img)
401            self.backing.append(backing_img)
402            qemu_img('create', '-f', iotests.imgfmt, backing_img, '1M')
403            qemu_io('-f', iotests.imgfmt,
404                    '-c', 'write -P 0x55 0 1024', backing_img)
405            qemu_img('create', '-f', iotests.imgfmt,
406                     '-o', 'backing_file=%s' % backing_img, child_img)
407            opts.append("children.%d.file.filename=%s" % (i, child_img))
408            opts.append("children.%d.node-name=node%d" % (i, i))
409
410        # Attach the drive to the VM
411        self.vm = iotests.VM()
412        self.vm.add_drive(path = None, opts = ','.join(opts))
413        self.vm.launch()
414
415    def tearDown(self):
416        self.vm.shutdown()
417        for img in self.children:
418            os.remove(img)
419        for img in self.backing:
420            os.remove(img)
421
422    def test_stream_quorum(self):
423        if not iotests.supports_quorum():
424            return
425
426        self.assertNotEqual(qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.children[0]),
427                            qemu_io('-f', iotests.imgfmt, '-rU', '-c', 'map', self.backing[0]),
428                            'image file map matches backing file before streaming')
429
430        self.assert_no_active_block_jobs()
431
432        result = self.vm.qmp('block-stream', device='node0', job_id='stream-node0')
433        self.assert_qmp(result, 'return', {})
434
435        self.wait_until_completed(drive='stream-node0')
436
437        self.assert_no_active_block_jobs()
438        self.vm.shutdown()
439
440        self.assertEqual(qemu_io('-f', iotests.imgfmt, '-c', 'map', self.children[0]),
441                         qemu_io('-f', iotests.imgfmt, '-c', 'map', self.backing[0]),
442                         'image file map does not match backing file after streaming')
443
444class TestSmallerBackingFile(iotests.QMPTestCase):
445    backing_len = 1 * 1024 * 1024 # MB
446    image_len = 2 * backing_len
447
448    def setUp(self):
449        iotests.create_image(backing_img, self.backing_len)
450        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img, str(self.image_len))
451        self.vm = iotests.VM().add_drive(test_img)
452        self.vm.launch()
453
454    # If this hangs, then you are missing a fix to complete streaming when the
455    # end of the backing file is reached.
456    def test_stream(self):
457        self.assert_no_active_block_jobs()
458
459        result = self.vm.qmp('block-stream', device='drive0')
460        self.assert_qmp(result, 'return', {})
461
462        self.wait_until_completed()
463
464        self.assert_no_active_block_jobs()
465        self.vm.shutdown()
466
467class TestErrors(iotests.QMPTestCase):
468    image_len = 2 * 1024 * 1024 # MB
469
470    # this should match STREAM_BUFFER_SIZE/512 in block/stream.c
471    STREAM_BUFFER_SIZE = 512 * 1024
472
473    def create_blkdebug_file(self, name, event, errno):
474        file = open(name, 'w')
475        file.write('''
476[inject-error]
477state = "1"
478event = "%s"
479errno = "%d"
480immediately = "off"
481once = "on"
482sector = "%d"
483
484[set-state]
485state = "1"
486event = "%s"
487new_state = "2"
488
489[set-state]
490state = "2"
491event = "%s"
492new_state = "1"
493''' % (event, errno, self.STREAM_BUFFER_SIZE / 512, event, event))
494        file.close()
495
496class TestEIO(TestErrors):
497    def setUp(self):
498        self.blkdebug_file = backing_img + ".blkdebug"
499        iotests.create_image(backing_img, TestErrors.image_len)
500        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
501        qemu_img('create', '-f', iotests.imgfmt,
502                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
503                       % (self.blkdebug_file, backing_img),
504                 test_img)
505        self.vm = iotests.VM().add_drive(test_img)
506        self.vm.launch()
507
508    def tearDown(self):
509        self.vm.shutdown()
510        os.remove(test_img)
511        os.remove(backing_img)
512        os.remove(self.blkdebug_file)
513
514    def test_report(self):
515        self.assert_no_active_block_jobs()
516
517        result = self.vm.qmp('block-stream', device='drive0')
518        self.assert_qmp(result, 'return', {})
519
520        completed = False
521        error = False
522        while not completed:
523            for event in self.vm.get_qmp_events(wait=True):
524                if event['event'] == 'BLOCK_JOB_ERROR':
525                    self.assert_qmp(event, 'data/device', 'drive0')
526                    self.assert_qmp(event, 'data/operation', 'read')
527                    error = True
528                elif event['event'] == 'BLOCK_JOB_COMPLETED':
529                    self.assertTrue(error, 'job completed unexpectedly')
530                    self.assert_qmp(event, 'data/type', 'stream')
531                    self.assert_qmp(event, 'data/device', 'drive0')
532                    self.assert_qmp(event, 'data/error', 'Input/output error')
533                    self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
534                    self.assert_qmp(event, 'data/len', self.image_len)
535                    completed = True
536
537        self.assert_no_active_block_jobs()
538        self.vm.shutdown()
539
540    def test_ignore(self):
541        self.assert_no_active_block_jobs()
542
543        result = self.vm.qmp('block-stream', device='drive0', on_error='ignore')
544        self.assert_qmp(result, 'return', {})
545
546        error = False
547        completed = False
548        while not completed:
549            for event in self.vm.get_qmp_events(wait=True):
550                if event['event'] == 'BLOCK_JOB_ERROR':
551                    error = True
552                    self.assert_qmp(event, 'data/device', 'drive0')
553                    self.assert_qmp(event, 'data/operation', 'read')
554                    result = self.vm.qmp('query-block-jobs')
555                    if result == {'return': []}:
556                        # Job finished too quickly
557                        continue
558                    self.assert_qmp(result, 'return[0]/paused', False)
559                elif event['event'] == 'BLOCK_JOB_COMPLETED':
560                    self.assertTrue(error, 'job completed unexpectedly')
561                    self.assert_qmp(event, 'data/type', 'stream')
562                    self.assert_qmp(event, 'data/device', 'drive0')
563                    self.assert_qmp(event, 'data/error', 'Input/output error')
564                    self.assert_qmp(event, 'data/offset', self.image_len)
565                    self.assert_qmp(event, 'data/len', self.image_len)
566                    completed = True
567
568        self.assert_no_active_block_jobs()
569        self.vm.shutdown()
570
571    def test_stop(self):
572        self.assert_no_active_block_jobs()
573
574        result = self.vm.qmp('block-stream', device='drive0', on_error='stop')
575        self.assert_qmp(result, 'return', {})
576
577        error = False
578        completed = False
579        while not completed:
580            for event in self.vm.get_qmp_events(wait=True):
581                if event['event'] == 'BLOCK_JOB_ERROR':
582                    error = True
583                    self.assert_qmp(event, 'data/device', 'drive0')
584                    self.assert_qmp(event, 'data/operation', 'read')
585
586                    result = self.vm.qmp('query-block-jobs')
587                    self.assert_qmp(result, 'return[0]/paused', True)
588                    self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
589                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
590
591                    result = self.vm.qmp('block-job-resume', device='drive0')
592                    self.assert_qmp(result, 'return', {})
593
594                    result = self.vm.qmp('query-block-jobs')
595                    if result == {'return': []}:
596                        # Race; likely already finished. Check.
597                        continue
598                    self.assert_qmp(result, 'return[0]/paused', False)
599                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
600                elif event['event'] == 'BLOCK_JOB_COMPLETED':
601                    self.assertTrue(error, 'job completed unexpectedly')
602                    self.assert_qmp(event, 'data/type', 'stream')
603                    self.assert_qmp(event, 'data/device', 'drive0')
604                    self.assert_qmp_absent(event, 'data/error')
605                    self.assert_qmp(event, 'data/offset', self.image_len)
606                    self.assert_qmp(event, 'data/len', self.image_len)
607                    completed = True
608
609        self.assert_no_active_block_jobs()
610        self.vm.shutdown()
611
612    def test_enospc(self):
613        self.assert_no_active_block_jobs()
614
615        result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
616        self.assert_qmp(result, 'return', {})
617
618        completed = False
619        error = False
620        while not completed:
621            for event in self.vm.get_qmp_events(wait=True):
622                if event['event'] == 'BLOCK_JOB_ERROR':
623                    self.assert_qmp(event, 'data/device', 'drive0')
624                    self.assert_qmp(event, 'data/operation', 'read')
625                    error = True
626                elif event['event'] == 'BLOCK_JOB_COMPLETED':
627                    self.assertTrue(error, 'job completed unexpectedly')
628                    self.assert_qmp(event, 'data/type', 'stream')
629                    self.assert_qmp(event, 'data/device', 'drive0')
630                    self.assert_qmp(event, 'data/error', 'Input/output error')
631                    self.assert_qmp(event, 'data/offset', self.STREAM_BUFFER_SIZE)
632                    self.assert_qmp(event, 'data/len', self.image_len)
633                    completed = True
634
635        self.assert_no_active_block_jobs()
636        self.vm.shutdown()
637
638class TestENOSPC(TestErrors):
639    def setUp(self):
640        self.blkdebug_file = backing_img + ".blkdebug"
641        iotests.create_image(backing_img, TestErrors.image_len)
642        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 28)
643        qemu_img('create', '-f', iotests.imgfmt,
644                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
645                       % (self.blkdebug_file, backing_img),
646                 test_img)
647        self.vm = iotests.VM().add_drive(test_img)
648        self.vm.launch()
649
650    def tearDown(self):
651        self.vm.shutdown()
652        os.remove(test_img)
653        os.remove(backing_img)
654        os.remove(self.blkdebug_file)
655
656    def test_enospc(self):
657        self.assert_no_active_block_jobs()
658
659        result = self.vm.qmp('block-stream', device='drive0', on_error='enospc')
660        self.assert_qmp(result, 'return', {})
661
662        error = False
663        completed = False
664        while not completed:
665            for event in self.vm.get_qmp_events(wait=True):
666                if event['event'] == 'BLOCK_JOB_ERROR':
667                    self.assert_qmp(event, 'data/device', 'drive0')
668                    self.assert_qmp(event, 'data/operation', 'read')
669
670                    result = self.vm.qmp('query-block-jobs')
671                    self.assert_qmp(result, 'return[0]/paused', True)
672                    self.assert_qmp(result, 'return[0]/offset', self.STREAM_BUFFER_SIZE)
673                    self.assert_qmp(result, 'return[0]/io-status', 'nospace')
674
675                    result = self.vm.qmp('block-job-resume', device='drive0')
676                    self.assert_qmp(result, 'return', {})
677
678                    result = self.vm.qmp('query-block-jobs')
679                    self.assert_qmp(result, 'return[0]/paused', False)
680                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
681                    error = True
682                elif event['event'] == 'BLOCK_JOB_COMPLETED':
683                    self.assertTrue(error, 'job completed unexpectedly')
684                    self.assert_qmp(event, 'data/type', 'stream')
685                    self.assert_qmp(event, 'data/device', 'drive0')
686                    self.assert_qmp_absent(event, 'data/error')
687                    self.assert_qmp(event, 'data/offset', self.image_len)
688                    self.assert_qmp(event, 'data/len', self.image_len)
689                    completed = True
690
691        self.assert_no_active_block_jobs()
692        self.vm.shutdown()
693
694class TestStreamStop(iotests.QMPTestCase):
695    image_len = 8 * 1024 * 1024 * 1024 # GB
696
697    def setUp(self):
698        qemu_img('create', backing_img, str(TestStreamStop.image_len))
699        qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
700        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
701        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
702        self.vm = iotests.VM().add_drive("blkdebug::" + test_img)
703        self.vm.launch()
704
705    def tearDown(self):
706        self.vm.shutdown()
707        os.remove(test_img)
708        os.remove(backing_img)
709
710    def test_stream_stop(self):
711        self.assert_no_active_block_jobs()
712
713        self.vm.pause_drive('drive0')
714        result = self.vm.qmp('block-stream', device='drive0')
715        self.assert_qmp(result, 'return', {})
716
717        time.sleep(0.1)
718        events = self.vm.get_qmp_events(wait=False)
719        self.assertEqual(events, [], 'unexpected QMP event: %s' % events)
720
721        self.cancel_and_wait(resume=True)
722
723class TestSetSpeed(iotests.QMPTestCase):
724    image_len = 80 * 1024 * 1024 # MB
725
726    def setUp(self):
727        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
728        qemu_io('-f', 'raw', '-c', 'write -P 0x1 0 32M', backing_img)
729        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
730        qemu_io('-f', iotests.imgfmt, '-c', 'write -P 0x1 32M 32M', test_img)
731        self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
732        self.vm.launch()
733
734    def tearDown(self):
735        self.vm.shutdown()
736        os.remove(test_img)
737        os.remove(backing_img)
738
739    # This is a short performance test which is not run by default.
740    # Invoke "IMGFMT=qed ./030 TestSetSpeed.perf_test_throughput"
741    def perf_test_throughput(self):
742        self.assert_no_active_block_jobs()
743
744        result = self.vm.qmp('block-stream', device='drive0')
745        self.assert_qmp(result, 'return', {})
746
747        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
748        self.assert_qmp(result, 'return', {})
749
750        self.wait_until_completed()
751
752        self.assert_no_active_block_jobs()
753
754    def test_set_speed(self):
755        self.assert_no_active_block_jobs()
756
757        self.vm.pause_drive('drive0')
758        result = self.vm.qmp('block-stream', device='drive0')
759        self.assert_qmp(result, 'return', {})
760
761        # Default speed is 0
762        result = self.vm.qmp('query-block-jobs')
763        self.assert_qmp(result, 'return[0]/device', 'drive0')
764        self.assert_qmp(result, 'return[0]/speed', 0)
765
766        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
767        self.assert_qmp(result, 'return', {})
768
769        # Ensure the speed we set was accepted
770        result = self.vm.qmp('query-block-jobs')
771        self.assert_qmp(result, 'return[0]/device', 'drive0')
772        self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
773
774        self.cancel_and_wait(resume=True)
775        self.vm.pause_drive('drive0')
776
777        # Check setting speed in block-stream works
778        result = self.vm.qmp('block-stream', device='drive0', speed=4 * 1024 * 1024)
779        self.assert_qmp(result, 'return', {})
780
781        result = self.vm.qmp('query-block-jobs')
782        self.assert_qmp(result, 'return[0]/device', 'drive0')
783        self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
784
785        self.cancel_and_wait(resume=True)
786
787    def test_set_speed_invalid(self):
788        self.assert_no_active_block_jobs()
789
790        result = self.vm.qmp('block-stream', device='drive0', speed=-1)
791        self.assert_qmp(result, 'error/class', 'GenericError')
792
793        self.assert_no_active_block_jobs()
794
795        result = self.vm.qmp('block-stream', device='drive0')
796        self.assert_qmp(result, 'return', {})
797
798        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
799        self.assert_qmp(result, 'error/class', 'GenericError')
800
801        self.cancel_and_wait()
802
803if __name__ == '__main__':
804    iotests.main(supported_fmts=['qcow2', 'qed'])
805