Skip to content

Commit

Permalink
Merge pull request autotest#5122 from chunfuwen/add_scsi_cdrom_eject_…
Browse files Browse the repository at this point in the history
…case

Add eject the hotplug cdrom media by change-media
  • Loading branch information
dzhengfy authored Sep 18, 2023
2 parents d1703e7 + 31052f2 commit 39aa600
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libvirt/tests/cfg/virtual_disks/virtual_disks_cdrom_device.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@
tray_original_state = "not locked, tray open"
tray_reset_state = "locked, tray closed"
only coldplug..positive_test
- scsi_cdrom_hot_eject_backend:
func_supported_since_libvirt_ver = (9, 3, 0)
type_name = "file"
target_format = "raw"
virt_disk_device_source = "/var/lib/libvirt/images/test_eject_scsi_cdrom.iso"
source_file = ${virt_disk_device_source}
target_dev = "sdb"
backend_device = "scsi_cdrom_hot_eject_backend"
tray_state = "locked, tray closed"
only hotplug..positive_test
variants:
- hotplug:
virt_device_hotplug = "yes"
Expand Down
61 changes: 61 additions & 0 deletions libvirt/tests/src/virtual_disks/virtual_disks_cdrom_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,59 @@ def _qemu_state():
test.fail("Can not find matched event:%s from event output: %s" % (event_matched, ret_output))


def check_scsi_cdrom_hot_eject(vm, params, test):
"""
Check eject hotplug scsi cdrom
:param vm: one object representing VM
:param params: wrapped parameters in dictionary format
:param test: test assert object
"""
# start loop to wait for tray-change event
virsh_session = aexpect.ShellSession(virsh.VIRSH_EXEC, auto_close=True)
event_cmd = "event --domain %s --event tray-change --loop" % vm.name
virsh_session.sendline(event_cmd)

device_target = params.get("target_dev")
virsh.change_media(vm.name, device_target, " --eject --live",
ignore_status=False, debug=True)

vm.wait_for_login().close()
# Check cdrom has been changed with empty
check_source_in_cdrom_device(vm, None, test)

def _get_qemu_state():
"""
Get qemu state
"""
qemu_output = virsh.qemu_monitor_command(name=vm.name, cmd="info block", options='--hmp',
**{'debug': True, 'ignore_status': True})
LOG.debug("qemu output:\n%s", qemu_output.stdout_text)
return qemu_output.stdout_text

tray_state_close = params.get("tray_state")
qemu_state = _get_qemu_state()
if tray_state_close not in qemu_state:
test.fail("Failed since tray state is close, but get state from qemu: %s" % qemu_state)

# check tray-change event
virsh_session.send_ctrl("^C")
ret_output = virsh_session.get_stripped_output().replace("\n", "").strip()

event_matched_opened = r"event 'tray-change' for domain '%s' disk .*opened" % vm.name
if not re.search(event_matched_opened, ret_output):
test.fail("Can not find matched event:%s from event output: %s" % (event_matched_opened, ret_output))
event_matched_closed = r"event 'tray-change' for domain '%s' disk .*closed" % vm.name
if not re.search(event_matched_closed, ret_output):
test.fail("Can not find matched event:%s from event output: %s" % (event_matched_closed, ret_output))

virsh.detach_disk(vm.name, device_target, " --live", ignore_status=False)
cdrom_vmxml = vm_xml.VMXML.new_from_dumpxml(vm.name)
cdrom_devices = cdrom_vmxml.get_disk_all_by_expr('device==cdrom')
if len(cdrom_devices) > 0:
test.fail("cdrom disk can not be detached successfully")


def run(test, params, env):
"""
Test attach cdrom device with option.
Expand Down Expand Up @@ -510,6 +563,12 @@ def run(test, params, env):
vmxml.sync()
vm.start()
vm.wait_for_login().close()
if backend_device == "scsi_cdrom_hot_eject_backend":
device_obj = create_open_tray_cdrom_disk(params)
virsh.attach_device(vm_name, device_obj, flagstr="--live", debug=True)
iso_file_path = params.get("virt_disk_device_source")
# Check cdrom has been filled with iso file
check_source_in_cdrom_device(vm, iso_file_path, test)
if status_error:
if hotplug:
LOG.info("attaching devices, expecting error...")
Expand Down Expand Up @@ -553,6 +612,8 @@ def run(test, params, env):
check_libvirtd_not_crash_on_domstats(vm, old_pid_of_libvirtd, test)
elif backend_device == "cdrom_reboot_reset_backend":
check_cdrom_reboot_reset(vm, params, test)
elif backend_device == "scsi_cdrom_hot_eject_backend":
check_scsi_cdrom_hot_eject(vm, params, test)
finally:
# Recover VM.
if vm.is_alive():
Expand Down

0 comments on commit 39aa600

Please sign in to comment.