4 # Test case for the QMP 'change' command and all other associated
7 # Copyright (C) 2015 Red Hat, Inc.
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 from iotests import qemu_img
29 old_img = os.path.join(iotests.test_dir, 'test0.img')
30 new_img = os.path.join(iotests.test_dir, 'test1.img')
32 def interface_to_device_name(interface):
33 if interface == 'ide':
35 elif interface == 'floppy':
37 elif interface == 'scsi':
42 class ChangeBaseClass(iotests.QMPTestCase):
49 def process_events(self):
50 for event in self.vm.get_qmp_events(wait=False):
51 if (event['event'] == 'DEVICE_TRAY_MOVED' and
52 (event['data']['device'] == 'drive0' or
53 event['data']['id'] == self.device_name)):
54 if event['data']['tray-open'] == False:
55 self.has_closed = True
57 self.has_opened = True
59 def wait_for_open(self):
60 if not self.has_real_tray:
63 with iotests.Timeout(3, 'Timeout while waiting for the tray to open'):
64 while not self.has_opened:
67 def wait_for_close(self):
68 if not self.has_real_tray:
71 with iotests.Timeout(3, 'Timeout while waiting for the tray to close'):
72 while not self.has_closed:
75 class GeneralChangeTestsBaseClass(ChangeBaseClass):
77 def test_change(self):
78 # 'change' requires a drive name, so skip the test for blockdev
79 if not self.use_drive:
82 result = self.vm.qmp('change', device='drive0', target=new_img,
84 self.assert_qmp(result, 'return', {})
89 result = self.vm.qmp('query-block')
90 if self.has_real_tray:
91 self.assert_qmp(result, 'return[0]/tray_open', False)
92 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
94 def test_blockdev_change_medium(self):
95 result = self.vm.qmp('blockdev-change-medium',
96 id=self.device_name, filename=new_img,
97 format=iotests.imgfmt)
99 self.assert_qmp(result, 'return', {})
102 self.wait_for_close()
104 result = self.vm.qmp('query-block')
105 if self.has_real_tray:
106 self.assert_qmp(result, 'return[0]/tray_open', False)
107 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
109 def test_eject(self):
110 result = self.vm.qmp('eject', id=self.device_name, force=True)
111 self.assert_qmp(result, 'return', {})
115 result = self.vm.qmp('query-block')
116 if self.has_real_tray:
117 self.assert_qmp(result, 'return[0]/tray_open', True)
118 self.assert_qmp_absent(result, 'return[0]/inserted')
120 def test_tray_eject_change(self):
121 result = self.vm.qmp('eject', id=self.device_name, force=True)
122 self.assert_qmp(result, 'return', {})
126 result = self.vm.qmp('query-block')
127 if self.has_real_tray:
128 self.assert_qmp(result, 'return[0]/tray_open', True)
129 self.assert_qmp_absent(result, 'return[0]/inserted')
131 result = self.vm.qmp('blockdev-change-medium', id=self.device_name,
132 filename=new_img, format=iotests.imgfmt)
133 self.assert_qmp(result, 'return', {})
135 self.wait_for_close()
137 result = self.vm.qmp('query-block')
138 if self.has_real_tray:
139 self.assert_qmp(result, 'return[0]/tray_open', False)
140 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
142 def test_tray_open_close(self):
143 result = self.vm.qmp('blockdev-open-tray',
144 id=self.device_name, force=True)
145 self.assert_qmp(result, 'return', {})
149 result = self.vm.qmp('query-block')
150 if self.has_real_tray:
151 self.assert_qmp(result, 'return[0]/tray_open', True)
152 if self.was_empty == True:
153 self.assert_qmp_absent(result, 'return[0]/inserted')
155 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
157 result = self.vm.qmp('blockdev-close-tray', id=self.device_name)
158 self.assert_qmp(result, 'return', {})
160 if self.has_real_tray or not self.was_empty:
161 self.wait_for_close()
163 result = self.vm.qmp('query-block')
164 if self.has_real_tray:
165 self.assert_qmp(result, 'return[0]/tray_open', False)
166 if self.was_empty == True:
167 self.assert_qmp_absent(result, 'return[0]/inserted')
169 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
171 def test_tray_eject_close(self):
172 result = self.vm.qmp('eject', id=self.device_name, force=True)
173 self.assert_qmp(result, 'return', {})
177 result = self.vm.qmp('query-block')
178 if self.has_real_tray:
179 self.assert_qmp(result, 'return[0]/tray_open', True)
180 self.assert_qmp_absent(result, 'return[0]/inserted')
182 result = self.vm.qmp('blockdev-close-tray', id=self.device_name)
183 self.assert_qmp(result, 'return', {})
185 self.wait_for_close()
187 result = self.vm.qmp('query-block')
188 if self.has_real_tray:
189 self.assert_qmp(result, 'return[0]/tray_open', False)
190 self.assert_qmp_absent(result, 'return[0]/inserted')
192 def test_tray_open_change(self):
193 result = self.vm.qmp('blockdev-open-tray', id=self.device_name,
195 self.assert_qmp(result, 'return', {})
199 result = self.vm.qmp('query-block')
200 if self.has_real_tray:
201 self.assert_qmp(result, 'return[0]/tray_open', True)
202 if self.was_empty == True:
203 self.assert_qmp_absent(result, 'return[0]/inserted')
205 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
207 result = self.vm.qmp('blockdev-change-medium', id=self.device_name,
209 format=iotests.imgfmt)
210 self.assert_qmp(result, 'return', {})
212 self.wait_for_close()
214 result = self.vm.qmp('query-block')
215 if self.has_real_tray:
216 self.assert_qmp(result, 'return[0]/tray_open', False)
217 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
219 def test_cycle(self, read_only_node=False):
220 result = self.vm.qmp('blockdev-add',
222 driver=iotests.imgfmt,
223 read_only=read_only_node,
224 file={'filename': new_img,
226 self.assert_qmp(result, 'return', {})
228 result = self.vm.qmp('blockdev-open-tray',
229 id=self.device_name, force=True)
230 self.assert_qmp(result, 'return', {})
234 result = self.vm.qmp('query-block')
235 if self.has_real_tray:
236 self.assert_qmp(result, 'return[0]/tray_open', True)
237 if self.was_empty == True:
238 self.assert_qmp_absent(result, 'return[0]/inserted')
240 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
242 result = self.vm.qmp('blockdev-remove-medium',
244 self.assert_qmp(result, 'return', {})
246 result = self.vm.qmp('query-block')
247 if self.has_real_tray:
248 self.assert_qmp(result, 'return[0]/tray_open', True)
249 self.assert_qmp_absent(result, 'return[0]/inserted')
251 result = self.vm.qmp('blockdev-insert-medium',
252 id=self.device_name, node_name='new')
253 self.assert_qmp(result, 'return', {})
255 result = self.vm.qmp('query-block')
256 if self.has_real_tray:
257 self.assert_qmp(result, 'return[0]/tray_open', True)
258 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
260 result = self.vm.qmp('blockdev-close-tray', id=self.device_name)
261 self.assert_qmp(result, 'return', {})
263 self.wait_for_close()
265 result = self.vm.qmp('query-block')
266 if self.has_real_tray:
267 self.assert_qmp(result, 'return[0]/tray_open', False)
268 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
270 def test_cycle_read_only_media(self):
271 self.test_cycle(True)
273 def test_close_on_closed(self):
274 result = self.vm.qmp('blockdev-close-tray', id=self.device_name)
276 self.assert_qmp(result, 'return', {})
277 self.assertEqual(self.vm.get_qmp_events(wait=False), [])
279 def test_remove_on_closed(self):
280 if not self.has_real_tray:
283 result = self.vm.qmp('blockdev-remove-medium', id=self.device_name)
284 self.assert_qmp(result, 'error/class', 'GenericError')
286 def test_insert_on_closed(self):
287 if not self.has_real_tray:
290 result = self.vm.qmp('blockdev-add',
292 driver=iotests.imgfmt,
293 file={'filename': new_img,
295 self.assert_qmp(result, 'return', {})
297 result = self.vm.qmp('blockdev-insert-medium', id=self.device_name,
299 self.assert_qmp(result, 'error/class', 'GenericError')
301 class TestInitiallyFilled(GeneralChangeTestsBaseClass):
305 qemu_img('create', '-f', iotests.imgfmt, old_img, '1440k')
306 qemu_img('create', '-f', iotests.imgfmt, new_img, '1440k')
307 self.vm = iotests.VM()
309 self.vm.add_drive(old_img, 'media=%s' % self.media, 'none')
311 self.vm.add_blockdev([ 'node-name=drive0',
312 'driver=%s' % iotests.imgfmt,
314 'file.filename=%s' % old_img ])
315 if self.interface == 'scsi':
316 self.vm.add_device('virtio-scsi-pci')
317 self.vm.add_device('%s,drive=drive0,id=%s' %
318 (interface_to_device_name(self.interface),
327 def test_insert_on_filled(self):
328 result = self.vm.qmp('blockdev-add',
330 driver=iotests.imgfmt,
331 file={'filename': new_img,
333 self.assert_qmp(result, 'return', {})
335 result = self.vm.qmp('blockdev-open-tray', id=self.device_name)
336 self.assert_qmp(result, 'return', {})
340 result = self.vm.qmp('blockdev-insert-medium', id=self.device_name,
342 self.assert_qmp(result, 'error/class', 'GenericError')
344 class TestInitiallyEmpty(GeneralChangeTestsBaseClass):
348 qemu_img('create', '-f', iotests.imgfmt, new_img, '1440k')
349 self.vm = iotests.VM()
351 self.vm.add_drive(None, 'media=%s' % self.media, 'none')
352 if self.interface == 'scsi':
353 self.vm.add_device('virtio-scsi-pci')
354 self.vm.add_device('%s,%sid=%s' %
355 (interface_to_device_name(self.interface),
356 'drive=drive0,' if self.use_drive else '',
364 def test_remove_on_empty(self):
365 result = self.vm.qmp('blockdev-open-tray', id=self.device_name)
366 self.assert_qmp(result, 'return', {})
370 result = self.vm.qmp('blockdev-remove-medium', id=self.device_name)
372 self.assert_qmp(result, 'return', {})
374 # Do this in a function to avoid leaking variables like case into the global
375 # name space (otherwise tests would be run for the abstract base classes)
376 def create_basic_test_classes():
377 for (media, interface, has_real_tray) in [ ('cdrom', 'ide', True),
378 ('cdrom', 'scsi', True),
379 ('disk', 'floppy', False) ]:
381 for case in [ TestInitiallyFilled, TestInitiallyEmpty ]:
382 for use_drive in [ True, False ]:
383 attr = { 'media': media,
384 'interface': interface,
385 'has_real_tray': has_real_tray,
386 'use_drive': use_drive }
388 name = '%s_%s_%s_%s' % (case.__name__, media, interface,
389 'drive' if use_drive else 'blockdev')
390 globals()[name] = type(name, (case, ), attr)
392 create_basic_test_classes()
394 class TestChangeReadOnly(ChangeBaseClass):
395 device_name = 'qdev0'
398 qemu_img('create', '-f', iotests.imgfmt, old_img, '1440k')
399 qemu_img('create', '-f', iotests.imgfmt, new_img, '1440k')
400 self.vm = iotests.VM()
404 os.chmod(old_img, 0o666)
405 os.chmod(new_img, 0o666)
409 def test_ro_ro_retain(self):
410 os.chmod(old_img, 0o444)
411 os.chmod(new_img, 0o444)
412 self.vm.add_drive(old_img, 'media=disk,read-only=on', 'none')
413 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
416 result = self.vm.qmp('query-block')
417 self.assert_qmp(result, 'return[0]/inserted/ro', True)
418 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
420 result = self.vm.qmp('blockdev-change-medium', id=self.device_name,
422 format=iotests.imgfmt,
423 read_only_mode='retain')
424 self.assert_qmp(result, 'return', {})
426 result = self.vm.qmp('query-block')
427 self.assert_qmp(result, 'return[0]/inserted/ro', True)
428 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
430 def test_ro_rw_retain(self):
431 os.chmod(old_img, 0o444)
432 self.vm.add_drive(old_img, 'media=disk,read-only=on', 'none')
433 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
436 result = self.vm.qmp('query-block')
437 self.assert_qmp(result, 'return[0]/inserted/ro', True)
438 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
440 result = self.vm.qmp('blockdev-change-medium', id=self.device_name,
442 format=iotests.imgfmt,
443 read_only_mode='retain')
444 self.assert_qmp(result, 'return', {})
446 result = self.vm.qmp('query-block')
447 self.assert_qmp(result, 'return[0]/inserted/ro', True)
448 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
450 @iotests.skip_if_user_is_root
451 def test_rw_ro_retain(self):
452 os.chmod(new_img, 0o444)
453 self.vm.add_drive(old_img, 'media=disk', 'none')
454 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
457 result = self.vm.qmp('query-block')
458 self.assert_qmp(result, 'return[0]/inserted/ro', False)
459 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
461 result = self.vm.qmp('blockdev-change-medium', id=self.device_name,
463 format=iotests.imgfmt,
464 read_only_mode='retain')
465 self.assert_qmp(result, 'error/class', 'GenericError')
467 self.assertEqual(self.vm.get_qmp_events(wait=False), [])
469 result = self.vm.qmp('query-block')
470 self.assert_qmp(result, 'return[0]/inserted/ro', False)
471 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
473 def test_ro_rw(self):
474 os.chmod(old_img, 0o444)
475 self.vm.add_drive(old_img, 'media=disk,read-only=on', 'none')
476 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
479 result = self.vm.qmp('query-block')
480 self.assert_qmp(result, 'return[0]/inserted/ro', True)
481 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
483 result = self.vm.qmp('blockdev-change-medium',
486 format=iotests.imgfmt,
487 read_only_mode='read-write')
488 self.assert_qmp(result, 'return', {})
490 result = self.vm.qmp('query-block')
491 self.assert_qmp(result, 'return[0]/inserted/ro', False)
492 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
494 def test_rw_ro(self):
495 os.chmod(new_img, 0o444)
496 self.vm.add_drive(old_img, 'media=disk', 'none')
497 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
500 result = self.vm.qmp('query-block')
501 self.assert_qmp(result, 'return[0]/inserted/ro', False)
502 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
504 result = self.vm.qmp('blockdev-change-medium',
507 format=iotests.imgfmt,
508 read_only_mode='read-only')
509 self.assert_qmp(result, 'return', {})
511 result = self.vm.qmp('query-block')
512 self.assert_qmp(result, 'return[0]/inserted/ro', True)
513 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
515 def test_make_rw_ro(self):
516 self.vm.add_drive(old_img, 'media=disk', 'none')
517 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
520 result = self.vm.qmp('query-block')
521 self.assert_qmp(result, 'return[0]/inserted/ro', False)
522 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
524 result = self.vm.qmp('blockdev-change-medium',
527 format=iotests.imgfmt,
528 read_only_mode='read-only')
529 self.assert_qmp(result, 'return', {})
531 result = self.vm.qmp('query-block')
532 self.assert_qmp(result, 'return[0]/inserted/ro', True)
533 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
535 @iotests.skip_if_user_is_root
536 def test_make_ro_rw(self):
537 os.chmod(new_img, 0o444)
538 self.vm.add_drive(old_img, 'media=disk', 'none')
539 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
542 result = self.vm.qmp('query-block')
543 self.assert_qmp(result, 'return[0]/inserted/ro', False)
544 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
546 result = self.vm.qmp('blockdev-change-medium',
549 format=iotests.imgfmt,
550 read_only_mode='read-write')
551 self.assert_qmp(result, 'error/class', 'GenericError')
553 result = self.vm.qmp('query-block')
554 self.assert_qmp(result, 'return[0]/inserted/ro', False)
555 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
557 def test_make_rw_ro_by_retain(self):
558 os.chmod(old_img, 0o444)
559 self.vm.add_drive(old_img, 'media=disk,read-only=on', 'none')
560 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
563 result = self.vm.qmp('query-block')
564 self.assert_qmp(result, 'return[0]/inserted/ro', True)
565 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
567 result = self.vm.qmp('blockdev-change-medium', id=self.device_name,
569 format=iotests.imgfmt,
570 read_only_mode='retain')
571 self.assert_qmp(result, 'return', {})
573 result = self.vm.qmp('query-block')
574 self.assert_qmp(result, 'return[0]/inserted/ro', True)
575 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
577 @iotests.skip_if_user_is_root
578 def test_make_ro_rw_by_retain(self):
579 os.chmod(new_img, 0o444)
580 self.vm.add_drive(old_img, 'media=disk', 'none')
581 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
584 result = self.vm.qmp('query-block')
585 self.assert_qmp(result, 'return[0]/inserted/ro', False)
586 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
588 result = self.vm.qmp('blockdev-change-medium', id=self.device_name,
590 format=iotests.imgfmt,
591 read_only_mode='retain')
592 self.assert_qmp(result, 'error/class', 'GenericError')
594 result = self.vm.qmp('query-block')
595 self.assert_qmp(result, 'return[0]/inserted/ro', False)
596 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
598 def test_rw_ro_cycle(self):
599 os.chmod(new_img, 0o444)
600 self.vm.add_drive(old_img, 'media=disk', 'none')
601 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
604 result = self.vm.qmp('query-block')
605 self.assert_qmp(result, 'return[0]/inserted/ro', False)
606 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
608 result = self.vm.qmp('blockdev-add',
610 driver=iotests.imgfmt,
612 file={'filename': new_img,
614 self.assert_qmp(result, 'return', {})
616 result = self.vm.qmp('query-block')
617 self.assert_qmp(result, 'return[0]/inserted/ro', False)
618 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
620 result = self.vm.qmp('blockdev-remove-medium', id=self.device_name)
621 self.assert_qmp(result, 'return', {})
623 result = self.vm.qmp('query-block')
624 self.assert_qmp_absent(result, 'return[0]/inserted')
626 result = self.vm.qmp('blockdev-insert-medium', id=self.device_name,
628 self.assert_qmp(result, 'return', {})
630 result = self.vm.qmp('query-block')
631 self.assert_qmp(result, 'return[0]/inserted/ro', True)
632 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
634 result = self.vm.qmp('query-block')
635 self.assert_qmp(result, 'return[0]/inserted/ro', True)
636 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
638 GeneralChangeTestsBaseClass = None
639 TestInitiallyFilled = None
640 TestInitiallyEmpty = None
643 class TestBlockJobsAfterCycle(ChangeBaseClass):
644 device_name = 'qdev0'
647 qemu_img('create', '-f', iotests.imgfmt, old_img, '1440K')
649 self.vm = iotests.VM()
650 self.vm.add_drive_raw("id=drive0,driver=null-co,if=none")
651 self.vm.add_device('floppy,drive=drive0,id=%s' % self.device_name)
654 result = self.vm.qmp('query-block')
655 self.assert_qmp(result, 'return[0]/inserted/image/format', 'null-co')
657 # For device-less BBs, calling blockdev-open-tray or blockdev-close-tray
659 result = self.vm.qmp('blockdev-remove-medium', id=self.device_name)
660 self.assert_qmp(result, 'return', {})
662 result = self.vm.qmp('query-block')
663 self.assert_qmp_absent(result, 'return[0]/inserted')
665 result = self.vm.qmp('blockdev-add',
667 driver=iotests.imgfmt,
668 file={'filename': old_img,
670 self.assert_qmp(result, 'return', {})
672 result = self.vm.qmp('blockdev-insert-medium', id=self.device_name,
674 self.assert_qmp(result, 'return', {})
676 result = self.vm.qmp('query-block')
677 self.assert_qmp(result, 'return[0]/inserted/image/filename', old_img)
687 # We need backing file support
688 @iotests.skip_for_formats(('vpc', 'parallels', 'qcow', 'vdi', 'vmdk', 'raw',
690 def test_snapshot_and_commit(self):
691 result = self.vm.qmp('blockdev-snapshot-sync', device='drive0',
692 snapshot_file=new_img,
693 format=iotests.imgfmt)
694 self.assert_qmp(result, 'return', {})
696 result = self.vm.qmp('query-block')
697 self.assert_qmp(result, 'return[0]/inserted/image/filename', new_img)
698 self.assert_qmp(result,
699 'return[0]/inserted/image/backing-image/filename',
702 result = self.vm.qmp('block-commit', device='drive0')
703 self.assert_qmp(result, 'return', {})
705 self.vm.event_wait(name='BLOCK_JOB_READY')
707 result = self.vm.qmp('query-block-jobs')
708 self.assert_qmp(result, 'return[0]/device', 'drive0')
710 result = self.vm.qmp('block-job-complete', device='drive0')
711 self.assert_qmp(result, 'return', {})
713 self.vm.event_wait(name='BLOCK_JOB_COMPLETED')
716 if __name__ == '__main__':
717 if iotests.qemu_default_machine != 'pc':
718 # We need floppy and IDE CD-ROM
719 iotests.notrun('not suitable for this machine type: %s' %
720 iotests.qemu_default_machine)
721 # Need to support image creation
722 iotests.main(supported_fmts=['vpc', 'parallels', 'qcow', 'vdi', 'qcow2',
723 'vmdk', 'raw', 'vhdx', 'qed'],
724 supported_protocols=['file'])