xref: /openbmc/qemu/tests/qemu-iotests/041 (revision dfbd2768)
1#!/usr/bin/env python
2#
3# Tests for image mirroring.
4#
5# Copyright (C) 2012 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 time
22import os
23import iotests
24from iotests import qemu_img, qemu_io
25
26backing_img = os.path.join(iotests.test_dir, 'backing.img')
27target_backing_img = os.path.join(iotests.test_dir, 'target-backing.img')
28test_img = os.path.join(iotests.test_dir, 'test.img')
29target_img = os.path.join(iotests.test_dir, 'target.img')
30
31quorum_img1 = os.path.join(iotests.test_dir, 'quorum1.img')
32quorum_img2 = os.path.join(iotests.test_dir, 'quorum2.img')
33quorum_img3 = os.path.join(iotests.test_dir, 'quorum3.img')
34quorum_repair_img = os.path.join(iotests.test_dir, 'quorum_repair.img')
35quorum_snapshot_file = os.path.join(iotests.test_dir, 'quorum_snapshot.img')
36
37class TestSingleDrive(iotests.QMPTestCase):
38    image_len = 1 * 1024 * 1024 # MB
39    qmp_cmd = 'drive-mirror'
40    qmp_target = target_img
41
42    def setUp(self):
43        iotests.create_image(backing_img, self.image_len)
44        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
45        self.vm = iotests.VM().add_drive(test_img)
46        if iotests.qemu_default_machine == 'pc':
47            self.vm.add_drive(None, 'media=cdrom', 'ide')
48        self.vm.launch()
49
50    def tearDown(self):
51        self.vm.shutdown()
52        os.remove(test_img)
53        os.remove(backing_img)
54        try:
55            os.remove(target_img)
56        except OSError:
57            pass
58
59    def test_complete(self):
60        self.assert_no_active_block_jobs()
61
62        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
63                             target=self.qmp_target)
64        self.assert_qmp(result, 'return', {})
65
66        self.complete_and_wait()
67        result = self.vm.qmp('query-block')
68        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
69        self.vm.shutdown()
70        self.assertTrue(iotests.compare_images(test_img, target_img),
71                        'target image does not match source after mirroring')
72
73    def test_cancel(self):
74        self.assert_no_active_block_jobs()
75
76        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
77                             target=self.qmp_target)
78        self.assert_qmp(result, 'return', {})
79
80        self.cancel_and_wait(force=True)
81        result = self.vm.qmp('query-block')
82        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
83        self.vm.shutdown()
84
85    def test_cancel_after_ready(self):
86        self.assert_no_active_block_jobs()
87
88        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
89                             target=self.qmp_target)
90        self.assert_qmp(result, 'return', {})
91
92        self.wait_ready_and_cancel()
93        result = self.vm.qmp('query-block')
94        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
95        self.vm.shutdown()
96        self.assertTrue(iotests.compare_images(test_img, target_img),
97                        'target image does not match source after mirroring')
98
99    def test_pause(self):
100        self.assert_no_active_block_jobs()
101
102        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
103                             target=self.qmp_target)
104        self.assert_qmp(result, 'return', {})
105
106        result = self.vm.qmp('block-job-pause', device='drive0')
107        self.assert_qmp(result, 'return', {})
108
109        time.sleep(1)
110        result = self.vm.qmp('query-block-jobs')
111        offset = self.dictpath(result, 'return[0]/offset')
112
113        time.sleep(1)
114        result = self.vm.qmp('query-block-jobs')
115        self.assert_qmp(result, 'return[0]/offset', offset)
116
117        result = self.vm.qmp('block-job-resume', device='drive0')
118        self.assert_qmp(result, 'return', {})
119
120        self.complete_and_wait()
121        self.vm.shutdown()
122        self.assertTrue(iotests.compare_images(test_img, target_img),
123                        'target image does not match source after mirroring')
124
125    def test_small_buffer(self):
126        self.assert_no_active_block_jobs()
127
128        # A small buffer is rounded up automatically
129        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
130                             buf_size=4096, target=self.qmp_target)
131        self.assert_qmp(result, 'return', {})
132
133        self.complete_and_wait()
134        result = self.vm.qmp('query-block')
135        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
136        self.vm.shutdown()
137        self.assertTrue(iotests.compare_images(test_img, target_img),
138                        'target image does not match source after mirroring')
139
140    def test_small_buffer2(self):
141        self.assert_no_active_block_jobs()
142
143        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,size=%d'
144                        % (self.image_len, self.image_len), target_img)
145        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
146                             buf_size=65536, mode='existing', target=self.qmp_target)
147        self.assert_qmp(result, 'return', {})
148
149        self.complete_and_wait()
150        result = self.vm.qmp('query-block')
151        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
152        self.vm.shutdown()
153        self.assertTrue(iotests.compare_images(test_img, target_img),
154                        'target image does not match source after mirroring')
155
156    def test_large_cluster(self):
157        self.assert_no_active_block_jobs()
158
159        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
160                        % (self.image_len, backing_img), target_img)
161        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
162                             mode='existing', target=self.qmp_target)
163        self.assert_qmp(result, 'return', {})
164
165        self.complete_and_wait()
166        result = self.vm.qmp('query-block')
167        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
168        self.vm.shutdown()
169        self.assertTrue(iotests.compare_images(test_img, target_img),
170                        'target image does not match source after mirroring')
171
172    def test_medium_not_found(self):
173        if iotests.qemu_default_machine != 'pc':
174            return
175
176        result = self.vm.qmp(self.qmp_cmd, device='ide1-cd0', sync='full',
177                             target=self.qmp_target)
178        self.assert_qmp(result, 'error/class', 'GenericError')
179
180    def test_image_not_found(self):
181        result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
182                             mode='existing', target=self.qmp_target)
183        self.assert_qmp(result, 'error/class', 'GenericError')
184
185    def test_device_not_found(self):
186        result = self.vm.qmp(self.qmp_cmd, device='nonexistent', sync='full',
187                             target=self.qmp_target)
188        self.assert_qmp(result, 'error/class', 'GenericError')
189
190class TestSingleBlockdev(TestSingleDrive):
191    qmp_cmd = 'blockdev-mirror'
192    qmp_target = 'node1'
193
194    def setUp(self):
195        TestSingleDrive.setUp(self)
196        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img)
197        args = {'driver': iotests.imgfmt,
198                'node-name': self.qmp_target,
199                'file': { 'filename': target_img, 'driver': 'file' } }
200        result = self.vm.qmp("blockdev-add", **args)
201        self.assert_qmp(result, 'return', {})
202
203    test_large_cluster = None
204    test_image_not_found = None
205    test_small_buffer2 = None
206
207class TestSingleDriveZeroLength(TestSingleDrive):
208    image_len = 0
209    test_small_buffer2 = None
210    test_large_cluster = None
211
212class TestSingleBlockdevZeroLength(TestSingleBlockdev):
213    image_len = 0
214
215class TestSingleDriveUnalignedLength(TestSingleDrive):
216    image_len = 1025 * 1024
217    test_small_buffer2 = None
218    test_large_cluster = None
219
220class TestSingleBlockdevUnalignedLength(TestSingleBlockdev):
221    image_len = 1025 * 1024
222
223class TestMirrorNoBacking(iotests.QMPTestCase):
224    image_len = 2 * 1024 * 1024 # MB
225
226    def setUp(self):
227        iotests.create_image(backing_img, TestMirrorNoBacking.image_len)
228        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
229        self.vm = iotests.VM().add_drive(test_img)
230        self.vm.launch()
231
232    def tearDown(self):
233        self.vm.shutdown()
234        os.remove(test_img)
235        os.remove(backing_img)
236        try:
237            os.remove(target_backing_img)
238        except:
239            pass
240        os.remove(target_img)
241
242    def test_complete(self):
243        self.assert_no_active_block_jobs()
244
245        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img)
246        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
247                             mode='existing', target=target_img)
248        self.assert_qmp(result, 'return', {})
249
250        self.complete_and_wait()
251        result = self.vm.qmp('query-block')
252        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
253        self.vm.shutdown()
254        self.assertTrue(iotests.compare_images(test_img, target_img),
255                        'target image does not match source after mirroring')
256
257    def test_cancel(self):
258        self.assert_no_active_block_jobs()
259
260        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img)
261        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
262                             mode='existing', target=target_img)
263        self.assert_qmp(result, 'return', {})
264
265        self.wait_ready_and_cancel()
266        result = self.vm.qmp('query-block')
267        self.assert_qmp(result, 'return[0]/inserted/file', test_img)
268        self.vm.shutdown()
269        self.assertTrue(iotests.compare_images(test_img, target_img),
270                        'target image does not match source after mirroring')
271
272    def test_large_cluster(self):
273        self.assert_no_active_block_jobs()
274
275        # qemu-img create fails if the image is not there
276        qemu_img('create', '-f', iotests.imgfmt, '-o', 'size=%d'
277                        %(TestMirrorNoBacking.image_len), target_backing_img)
278        qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
279                        % (TestMirrorNoBacking.image_len, target_backing_img), target_img)
280
281        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
282                             mode='existing', target=target_img)
283        self.assert_qmp(result, 'return', {})
284
285        self.complete_and_wait()
286        result = self.vm.qmp('query-block')
287        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
288        self.vm.shutdown()
289        self.assertTrue(iotests.compare_images(test_img, target_img),
290                        'target image does not match source after mirroring')
291
292class TestMirrorResized(iotests.QMPTestCase):
293    backing_len = 1 * 1024 * 1024 # MB
294    image_len = 2 * 1024 * 1024 # MB
295
296    def setUp(self):
297        iotests.create_image(backing_img, TestMirrorResized.backing_len)
298        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
299        qemu_img('resize', test_img, '2M')
300        self.vm = iotests.VM().add_drive(test_img)
301        self.vm.launch()
302
303    def tearDown(self):
304        self.vm.shutdown()
305        os.remove(test_img)
306        os.remove(backing_img)
307        try:
308            os.remove(target_img)
309        except OSError:
310            pass
311
312    def test_complete_top(self):
313        self.assert_no_active_block_jobs()
314
315        result = self.vm.qmp('drive-mirror', device='drive0', sync='top',
316                             target=target_img)
317        self.assert_qmp(result, 'return', {})
318
319        self.complete_and_wait()
320        result = self.vm.qmp('query-block')
321        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
322        self.vm.shutdown()
323        self.assertTrue(iotests.compare_images(test_img, target_img),
324                        'target image does not match source after mirroring')
325
326    def test_complete_full(self):
327        self.assert_no_active_block_jobs()
328
329        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
330                             target=target_img)
331        self.assert_qmp(result, 'return', {})
332
333        self.complete_and_wait()
334        result = self.vm.qmp('query-block')
335        self.assert_qmp(result, 'return[0]/inserted/file', target_img)
336        self.vm.shutdown()
337        self.assertTrue(iotests.compare_images(test_img, target_img),
338                        'target image does not match source after mirroring')
339
340class TestReadErrors(iotests.QMPTestCase):
341    image_len = 2 * 1024 * 1024 # MB
342
343    # this should be a multiple of twice the default granularity
344    # so that we hit this offset first in state 1
345    MIRROR_GRANULARITY = 1024 * 1024
346
347    def create_blkdebug_file(self, name, event, errno):
348        file = open(name, 'w')
349        file.write('''
350[inject-error]
351state = "1"
352event = "%s"
353errno = "%d"
354immediately = "off"
355once = "on"
356sector = "%d"
357
358[set-state]
359state = "1"
360event = "%s"
361new_state = "2"
362
363[set-state]
364state = "2"
365event = "%s"
366new_state = "1"
367''' % (event, errno, self.MIRROR_GRANULARITY / 512, event, event))
368        file.close()
369
370    def setUp(self):
371        self.blkdebug_file = backing_img + ".blkdebug"
372        iotests.create_image(backing_img, TestReadErrors.image_len)
373        self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5)
374        qemu_img('create', '-f', iotests.imgfmt,
375                 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
376                       % (self.blkdebug_file, backing_img),
377                 test_img)
378        # Write something for tests that use sync='top'
379        qemu_io('-c', 'write %d 512' % (self.MIRROR_GRANULARITY + 65536),
380                        test_img)
381        self.vm = iotests.VM().add_drive(test_img)
382        self.vm.launch()
383
384    def tearDown(self):
385        self.vm.shutdown()
386        os.remove(test_img)
387        os.remove(backing_img)
388        os.remove(self.blkdebug_file)
389
390    def test_report_read(self):
391        self.assert_no_active_block_jobs()
392
393        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
394                             target=target_img)
395        self.assert_qmp(result, 'return', {})
396
397        completed = False
398        error = False
399        while not completed:
400            for event in self.vm.get_qmp_events(wait=True):
401                if event['event'] == 'BLOCK_JOB_ERROR':
402                    self.assert_qmp(event, 'data/device', 'drive0')
403                    self.assert_qmp(event, 'data/operation', 'read')
404                    error = True
405                elif event['event'] == 'BLOCK_JOB_READY':
406                    self.assertTrue(False, 'job completed unexpectedly')
407                elif event['event'] == 'BLOCK_JOB_COMPLETED':
408                    self.assertTrue(error, 'job completed unexpectedly')
409                    self.assert_qmp(event, 'data/type', 'mirror')
410                    self.assert_qmp(event, 'data/device', 'drive0')
411                    self.assert_qmp(event, 'data/error', 'Input/output error')
412                    completed = True
413
414        self.assert_no_active_block_jobs()
415        self.vm.shutdown()
416
417    def test_ignore_read(self):
418        self.assert_no_active_block_jobs()
419
420        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
421                             target=target_img, on_source_error='ignore')
422        self.assert_qmp(result, 'return', {})
423
424        event = self.vm.get_qmp_event(wait=True)
425        self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
426        self.assert_qmp(event, 'data/device', 'drive0')
427        self.assert_qmp(event, 'data/operation', 'read')
428        result = self.vm.qmp('query-block-jobs')
429        self.assert_qmp(result, 'return[0]/paused', False)
430        self.complete_and_wait()
431        self.vm.shutdown()
432
433    def test_large_cluster(self):
434        self.assert_no_active_block_jobs()
435
436        # Test COW into the target image.  The first half of the
437        # cluster at MIRROR_GRANULARITY has to be copied from
438        # backing_img, even though sync='top'.
439        qemu_img('create', '-f', iotests.imgfmt, '-ocluster_size=131072,backing_file=%s' %(backing_img), target_img)
440        result = self.vm.qmp('drive-mirror', device='drive0', sync='top',
441                             on_source_error='ignore',
442                             mode='existing', target=target_img)
443        self.assert_qmp(result, 'return', {})
444
445        event = self.vm.get_qmp_event(wait=True)
446        self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
447        self.assert_qmp(event, 'data/device', 'drive0')
448        self.assert_qmp(event, 'data/operation', 'read')
449        result = self.vm.qmp('query-block-jobs')
450        self.assert_qmp(result, 'return[0]/paused', False)
451        self.complete_and_wait()
452        self.vm.shutdown()
453
454        # Detach blkdebug to compare images successfully
455        qemu_img('rebase', '-f', iotests.imgfmt, '-u', '-b', backing_img, test_img)
456        self.assertTrue(iotests.compare_images(test_img, target_img),
457                        'target image does not match source after mirroring')
458
459    def test_stop_read(self):
460        self.assert_no_active_block_jobs()
461
462        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
463                             target=target_img, on_source_error='stop')
464        self.assert_qmp(result, 'return', {})
465
466        error = False
467        ready = False
468        while not ready:
469            for event in self.vm.get_qmp_events(wait=True):
470                if event['event'] == 'BLOCK_JOB_ERROR':
471                    self.assert_qmp(event, 'data/device', 'drive0')
472                    self.assert_qmp(event, 'data/operation', 'read')
473
474                    result = self.vm.qmp('query-block-jobs')
475                    self.assert_qmp(result, 'return[0]/paused', True)
476                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
477
478                    result = self.vm.qmp('block-job-resume', device='drive0')
479                    self.assert_qmp(result, 'return', {})
480                    error = True
481                elif event['event'] == 'BLOCK_JOB_READY':
482                    self.assertTrue(error, 'job completed unexpectedly')
483                    self.assert_qmp(event, 'data/device', 'drive0')
484                    ready = True
485
486        result = self.vm.qmp('query-block-jobs')
487        self.assert_qmp(result, 'return[0]/paused', False)
488        self.assert_qmp(result, 'return[0]/io-status', 'ok')
489
490        self.complete_and_wait(wait_ready=False)
491        self.assert_no_active_block_jobs()
492        self.vm.shutdown()
493
494class TestWriteErrors(iotests.QMPTestCase):
495    image_len = 2 * 1024 * 1024 # MB
496
497    # this should be a multiple of twice the default granularity
498    # so that we hit this offset first in state 1
499    MIRROR_GRANULARITY = 1024 * 1024
500
501    def create_blkdebug_file(self, name, event, errno):
502        file = open(name, 'w')
503        file.write('''
504[inject-error]
505state = "1"
506event = "%s"
507errno = "%d"
508immediately = "off"
509once = "on"
510sector = "%d"
511
512[set-state]
513state = "1"
514event = "%s"
515new_state = "2"
516
517[set-state]
518state = "2"
519event = "%s"
520new_state = "1"
521''' % (event, errno, self.MIRROR_GRANULARITY / 512, event, event))
522        file.close()
523
524    def setUp(self):
525        self.blkdebug_file = target_img + ".blkdebug"
526        iotests.create_image(backing_img, TestWriteErrors.image_len)
527        self.create_blkdebug_file(self.blkdebug_file, "write_aio", 5)
528        qemu_img('create', '-f', iotests.imgfmt, '-obacking_file=%s' %(backing_img), test_img)
529        self.vm = iotests.VM().add_drive(test_img)
530        self.target_img = 'blkdebug:%s:%s' % (self.blkdebug_file, target_img)
531        qemu_img('create', '-f', iotests.imgfmt, '-osize=%d' %(TestWriteErrors.image_len), target_img)
532        self.vm.launch()
533
534    def tearDown(self):
535        self.vm.shutdown()
536        os.remove(test_img)
537        os.remove(backing_img)
538        os.remove(self.blkdebug_file)
539
540    def test_report_write(self):
541        self.assert_no_active_block_jobs()
542
543        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
544                             mode='existing', target=self.target_img)
545        self.assert_qmp(result, 'return', {})
546
547        completed = False
548        error = False
549        while not completed:
550            for event in self.vm.get_qmp_events(wait=True):
551                if event['event'] == 'BLOCK_JOB_ERROR':
552                    self.assert_qmp(event, 'data/device', 'drive0')
553                    self.assert_qmp(event, 'data/operation', 'write')
554                    error = True
555                elif event['event'] == 'BLOCK_JOB_READY':
556                    self.assertTrue(False, 'job completed unexpectedly')
557                elif event['event'] == 'BLOCK_JOB_COMPLETED':
558                    self.assertTrue(error, 'job completed unexpectedly')
559                    self.assert_qmp(event, 'data/type', 'mirror')
560                    self.assert_qmp(event, 'data/device', 'drive0')
561                    self.assert_qmp(event, 'data/error', 'Input/output error')
562                    completed = True
563
564        self.assert_no_active_block_jobs()
565        self.vm.shutdown()
566
567    def test_ignore_write(self):
568        self.assert_no_active_block_jobs()
569
570        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
571                             mode='existing', target=self.target_img,
572                             on_target_error='ignore')
573        self.assert_qmp(result, 'return', {})
574
575        event = self.vm.get_qmp_event(wait=True)
576        self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
577        self.assert_qmp(event, 'data/device', 'drive0')
578        self.assert_qmp(event, 'data/operation', 'write')
579        result = self.vm.qmp('query-block-jobs')
580        self.assert_qmp(result, 'return[0]/paused', False)
581        self.complete_and_wait()
582        self.vm.shutdown()
583
584    def test_stop_write(self):
585        self.assert_no_active_block_jobs()
586
587        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
588                             mode='existing', target=self.target_img,
589                             on_target_error='stop')
590        self.assert_qmp(result, 'return', {})
591
592        error = False
593        ready = False
594        while not ready:
595            for event in self.vm.get_qmp_events(wait=True):
596                if event['event'] == 'BLOCK_JOB_ERROR':
597                    self.assert_qmp(event, 'data/device', 'drive0')
598                    self.assert_qmp(event, 'data/operation', 'write')
599
600                    result = self.vm.qmp('query-block-jobs')
601                    self.assert_qmp(result, 'return[0]/paused', True)
602                    self.assert_qmp(result, 'return[0]/io-status', 'failed')
603
604                    result = self.vm.qmp('block-job-resume', device='drive0')
605                    self.assert_qmp(result, 'return', {})
606
607                    result = self.vm.qmp('query-block-jobs')
608                    self.assert_qmp(result, 'return[0]/paused', False)
609                    self.assert_qmp(result, 'return[0]/io-status', 'ok')
610                    error = True
611                elif event['event'] == 'BLOCK_JOB_READY':
612                    self.assertTrue(error, 'job completed unexpectedly')
613                    self.assert_qmp(event, 'data/device', 'drive0')
614                    ready = True
615
616        self.complete_and_wait(wait_ready=False)
617        self.assert_no_active_block_jobs()
618        self.vm.shutdown()
619
620class TestSetSpeed(iotests.QMPTestCase):
621    image_len = 80 * 1024 * 1024 # MB
622
623    def setUp(self):
624        qemu_img('create', backing_img, str(TestSetSpeed.image_len))
625        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
626        self.vm = iotests.VM().add_drive(test_img)
627        self.vm.launch()
628
629    def tearDown(self):
630        self.vm.shutdown()
631        os.remove(test_img)
632        os.remove(backing_img)
633        os.remove(target_img)
634
635    def test_set_speed(self):
636        self.assert_no_active_block_jobs()
637
638        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
639                             target=target_img)
640        self.assert_qmp(result, 'return', {})
641
642        # Default speed is 0
643        result = self.vm.qmp('query-block-jobs')
644        self.assert_qmp(result, 'return[0]/device', 'drive0')
645        self.assert_qmp(result, 'return[0]/speed', 0)
646
647        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
648        self.assert_qmp(result, 'return', {})
649
650        # Ensure the speed we set was accepted
651        result = self.vm.qmp('query-block-jobs')
652        self.assert_qmp(result, 'return[0]/device', 'drive0')
653        self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
654
655        self.wait_ready_and_cancel()
656
657        # Check setting speed in drive-mirror works
658        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
659                             target=target_img, speed=4*1024*1024)
660        self.assert_qmp(result, 'return', {})
661
662        result = self.vm.qmp('query-block-jobs')
663        self.assert_qmp(result, 'return[0]/device', 'drive0')
664        self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
665
666        self.wait_ready_and_cancel()
667
668    def test_set_speed_invalid(self):
669        self.assert_no_active_block_jobs()
670
671        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
672                             target=target_img, speed=-1)
673        self.assert_qmp(result, 'error/class', 'GenericError')
674
675        self.assert_no_active_block_jobs()
676
677        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
678                             target=target_img)
679        self.assert_qmp(result, 'return', {})
680
681        result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
682        self.assert_qmp(result, 'error/class', 'GenericError')
683
684        self.wait_ready_and_cancel()
685
686class TestUnbackedSource(iotests.QMPTestCase):
687    image_len = 2 * 1024 * 1024 # MB
688
689    def setUp(self):
690        qemu_img('create', '-f', iotests.imgfmt, test_img,
691                 str(TestUnbackedSource.image_len))
692        self.vm = iotests.VM().add_drive(test_img)
693        self.vm.launch()
694
695    def tearDown(self):
696        self.vm.shutdown()
697        os.remove(test_img)
698        os.remove(target_img)
699
700    def test_absolute_paths_full(self):
701        self.assert_no_active_block_jobs()
702        result = self.vm.qmp('drive-mirror', device='drive0',
703                             sync='full', target=target_img,
704                             mode='absolute-paths')
705        self.assert_qmp(result, 'return', {})
706        self.complete_and_wait()
707        self.assert_no_active_block_jobs()
708
709    def test_absolute_paths_top(self):
710        self.assert_no_active_block_jobs()
711        result = self.vm.qmp('drive-mirror', device='drive0',
712                             sync='top', target=target_img,
713                             mode='absolute-paths')
714        self.assert_qmp(result, 'return', {})
715        self.complete_and_wait()
716        self.assert_no_active_block_jobs()
717
718    def test_absolute_paths_none(self):
719        self.assert_no_active_block_jobs()
720        result = self.vm.qmp('drive-mirror', device='drive0',
721                             sync='none', target=target_img,
722                             mode='absolute-paths')
723        self.assert_qmp(result, 'return', {})
724        self.complete_and_wait()
725        self.assert_no_active_block_jobs()
726
727class TestGranularity(iotests.QMPTestCase):
728    image_len = 10 * 1024 * 1024 # MB
729
730    def setUp(self):
731        qemu_img('create', '-f', iotests.imgfmt, test_img,
732                 str(TestGranularity.image_len))
733        qemu_io('-c', 'write 0 %d' % (self.image_len),
734                test_img)
735        self.vm = iotests.VM().add_drive(test_img)
736        self.vm.launch()
737
738    def tearDown(self):
739        self.vm.shutdown()
740        self.assertTrue(iotests.compare_images(test_img, target_img),
741                        'target image does not match source after mirroring')
742        os.remove(test_img)
743        os.remove(target_img)
744
745    def test_granularity(self):
746        self.assert_no_active_block_jobs()
747        result = self.vm.qmp('drive-mirror', device='drive0',
748                             sync='full', target=target_img,
749                             mode='absolute-paths', granularity=8192)
750        self.assert_qmp(result, 'return', {})
751        event = self.vm.get_qmp_event(wait=60.0)
752        # Failures will manifest as COMPLETED/ERROR.
753        self.assert_qmp(event, 'event', 'BLOCK_JOB_READY')
754        self.complete_and_wait(drive='drive0', wait_ready=False)
755        self.assert_no_active_block_jobs()
756
757class TestRepairQuorum(iotests.QMPTestCase):
758    """ This class test quorum file repair using drive-mirror.
759        It's mostly a fork of TestSingleDrive """
760    image_len = 1 * 1024 * 1024 # MB
761    IMAGES = [ quorum_img1, quorum_img2, quorum_img3 ]
762
763    def setUp(self):
764        self.vm = iotests.VM()
765
766        if iotests.qemu_default_machine == 'pc':
767            self.vm.add_drive(None, 'media=cdrom', 'ide')
768
769        # Add each individual quorum images
770        for i in self.IMAGES:
771            qemu_img('create', '-f', iotests.imgfmt, i,
772                     str(TestSingleDrive.image_len))
773            # Assign a node name to each quorum image in order to manipulate
774            # them
775            opts = "node-name=img%i" % self.IMAGES.index(i)
776            self.vm = self.vm.add_drive(i, opts)
777
778        self.vm.launch()
779
780        #assemble the quorum block device from the individual files
781        args = { "driver": "quorum", "node-name": "quorum0",
782                 "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] }
783        if iotests.supports_quorum():
784            result = self.vm.qmp("blockdev-add", **args)
785            self.assert_qmp(result, 'return', {})
786
787
788    def tearDown(self):
789        self.vm.shutdown()
790        for i in self.IMAGES + [ quorum_repair_img ]:
791            # Do a try/except because the test may have deleted some images
792            try:
793                os.remove(i)
794            except OSError:
795                pass
796
797    def test_complete(self):
798        if not iotests.supports_quorum():
799            return
800
801        self.assert_no_active_block_jobs()
802
803        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
804                             sync='full', node_name="repair0", replaces="img1",
805                             target=quorum_repair_img, format=iotests.imgfmt)
806        self.assert_qmp(result, 'return', {})
807
808        self.complete_and_wait(drive="job0")
809        self.assert_has_block_node("repair0", quorum_repair_img)
810        # TODO: a better test requiring some QEMU infrastructure will be added
811        #       to check that this file is really driven by quorum
812        self.vm.shutdown()
813        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
814                        'target image does not match source after mirroring')
815
816    def test_cancel(self):
817        if not iotests.supports_quorum():
818            return
819
820        self.assert_no_active_block_jobs()
821
822        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
823                             sync='full', node_name="repair0", replaces="img1",
824                             target=quorum_repair_img, format=iotests.imgfmt)
825        self.assert_qmp(result, 'return', {})
826
827        self.cancel_and_wait(drive="job0", force=True)
828        # here we check that the last registered quorum file has not been
829        # swapped out and unref
830        self.assert_has_block_node(None, quorum_img3)
831        self.vm.shutdown()
832
833    def test_cancel_after_ready(self):
834        if not iotests.supports_quorum():
835            return
836
837        self.assert_no_active_block_jobs()
838
839        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
840                             sync='full', node_name="repair0", replaces="img1",
841                             target=quorum_repair_img, format=iotests.imgfmt)
842        self.assert_qmp(result, 'return', {})
843
844        self.wait_ready_and_cancel(drive="job0")
845        # here we check that the last registered quorum file has not been
846        # swapped out and unref
847        self.assert_has_block_node(None, quorum_img3)
848        self.vm.shutdown()
849        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
850                        'target image does not match source after mirroring')
851
852    def test_pause(self):
853        if not iotests.supports_quorum():
854            return
855
856        self.assert_no_active_block_jobs()
857
858        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
859                             sync='full', node_name="repair0", replaces="img1",
860                             target=quorum_repair_img, format=iotests.imgfmt)
861        self.assert_qmp(result, 'return', {})
862
863        result = self.vm.qmp('block-job-pause', device='job0')
864        self.assert_qmp(result, 'return', {})
865
866        time.sleep(1)
867        result = self.vm.qmp('query-block-jobs')
868        offset = self.dictpath(result, 'return[0]/offset')
869
870        time.sleep(1)
871        result = self.vm.qmp('query-block-jobs')
872        self.assert_qmp(result, 'return[0]/offset', offset)
873
874        result = self.vm.qmp('block-job-resume', device='job0')
875        self.assert_qmp(result, 'return', {})
876
877        self.complete_and_wait(drive="job0")
878        self.vm.shutdown()
879        self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
880                        'target image does not match source after mirroring')
881
882    def test_medium_not_found(self):
883        if not iotests.supports_quorum():
884            return
885
886        if iotests.qemu_default_machine != 'pc':
887            return
888
889        result = self.vm.qmp('drive-mirror', job_id='job0', device='drive0', # CD-ROM
890                             sync='full',
891                             node_name='repair0',
892                             replaces='img1',
893                             target=quorum_repair_img, format=iotests.imgfmt)
894        self.assert_qmp(result, 'error/class', 'GenericError')
895
896    def test_image_not_found(self):
897        if not iotests.supports_quorum():
898            return
899
900        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
901                             sync='full', node_name='repair0', replaces='img1',
902                             mode='existing', target=quorum_repair_img,
903                             format=iotests.imgfmt)
904        self.assert_qmp(result, 'error/class', 'GenericError')
905
906    def test_device_not_found(self):
907        if not iotests.supports_quorum():
908            return
909
910        result = self.vm.qmp('drive-mirror', job_id='job0',
911                             device='nonexistent', sync='full',
912                             node_name='repair0',
913                             replaces='img1',
914                             target=quorum_repair_img, format=iotests.imgfmt)
915        self.assert_qmp(result, 'error/class', 'GenericError')
916
917    def test_wrong_sync_mode(self):
918        if not iotests.supports_quorum():
919            return
920
921        result = self.vm.qmp('drive-mirror', device='quorum0', job_id='job0',
922                             node_name='repair0',
923                             replaces='img1',
924                             target=quorum_repair_img, format=iotests.imgfmt)
925        self.assert_qmp(result, 'error/class', 'GenericError')
926
927    def test_no_node_name(self):
928        if not iotests.supports_quorum():
929            return
930
931        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
932                             sync='full', replaces='img1',
933                             target=quorum_repair_img, format=iotests.imgfmt)
934        self.assert_qmp(result, 'error/class', 'GenericError')
935
936    def test_nonexistent_replaces(self):
937        if not iotests.supports_quorum():
938            return
939
940        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
941                             sync='full', node_name='repair0', replaces='img77',
942                             target=quorum_repair_img, format=iotests.imgfmt)
943        self.assert_qmp(result, 'error/class', 'GenericError')
944
945    def test_after_a_quorum_snapshot(self):
946        if not iotests.supports_quorum():
947            return
948
949        result = self.vm.qmp('blockdev-snapshot-sync', node_name='img1',
950                             snapshot_file=quorum_snapshot_file,
951                             snapshot_node_name="snap1");
952
953        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
954                             sync='full', node_name='repair0', replaces="img1",
955                             target=quorum_repair_img, format=iotests.imgfmt)
956        self.assert_qmp(result, 'error/class', 'GenericError')
957
958        result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
959                             sync='full', node_name='repair0', replaces="snap1",
960                             target=quorum_repair_img, format=iotests.imgfmt)
961        self.assert_qmp(result, 'return', {})
962
963        self.complete_and_wait('job0')
964        self.assert_has_block_node("repair0", quorum_repair_img)
965        # TODO: a better test requiring some QEMU infrastructure will be added
966        #       to check that this file is really driven by quorum
967        self.vm.shutdown()
968
969if __name__ == '__main__':
970    iotests.main(supported_fmts=['qcow2', 'qed'])
971