From a5eb249ac1ba4160e85521e96c7b953ae40c9b4e Mon Sep 17 00:00:00 2001 From: chunfuwen Date: Fri, 4 Aug 2023 01:44:24 -0400 Subject: [PATCH] Add one scsi device case xx-186123: [Host device assignment][SCSI] Hot-plug the same scsi device to two guests with shareable element Signed-off-by: chunfuwen --- libvirt/tests/cfg/scsi/scsi_device.cfg | 3 ++ libvirt/tests/src/scsi/scsi_device.py | 66 ++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/libvirt/tests/cfg/scsi/scsi_device.cfg b/libvirt/tests/cfg/scsi/scsi_device.cfg index 40ea5035ec..047866c2bc 100644 --- a/libvirt/tests/cfg/scsi/scsi_device.cfg +++ b/libvirt/tests/cfg/scsi/scsi_device.cfg @@ -2,6 +2,7 @@ type = scsi_device status_error = "no" start_vm = "no" + vms = avocado-vt-vm1 vm1 variants scsi_type: - scsi_hostdev: variants: @@ -28,6 +29,8 @@ controller_type = "scsi" controller_model = "virtio-scsi" controller_index = "0" + - shared_by_two_guests: + only hotplug - negative: variants test_scenario: - max_sectors_controller: diff --git a/libvirt/tests/src/scsi/scsi_device.py b/libvirt/tests/src/scsi/scsi_device.py index 1875a1bff3..3ced58d672 100644 --- a/libvirt/tests/src/scsi/scsi_device.py +++ b/libvirt/tests/src/scsi/scsi_device.py @@ -430,6 +430,42 @@ def check_scsi_controller(test, params, env): test.fail("Get index=0 scsi controller although detached") +def test_hotplug_scsi_hostdev_shared_by_two_guests(test, params, env): + """ + Test hotplug scsi device into two guests + + :param test: one test object instance + :param params: dict wrapped with params + :param env: environment instance + """ + block_device1 = setup_iscsi_block_device() + hostdev_xml = create_host_dev(params, block_device1) + hostdev_xml.shareable = True + vm_names = params.get("vms").split() + for vm_name in vm_names: + vm = env.get_vm(vm_name) + if vm.is_dead(): + vm.start() + vm.wait_for_login().close() + virsh.attach_device(vm_name, hostdev_xml.xml, flagstr="--live", + ignore_status=False) + + +def check_hostdev_shareable_attr(test, params, env): + """ + check scsi shareable attribute + + :param test: one test object instance + :param params: dict wrapped with params + :param env: environment instance + """ + vm_names = params.get("vms").split() + for vm_name in vm_names: + vm_output = virsh.dumpxml(vm_name).stdout_text + if 'shareable' not in vm_output: + test.fail("VM: % failed to find shareable attribute in output: %s" % (vm_name, vm_output)) + + def run(test, params, env): """ Test manipulate scsi device. @@ -443,13 +479,21 @@ def run(test, params, env): vm_name = params.get("main_vm") vm = env.get_vm(vm_name) - # Back up xml file - if vm.is_alive(): - vm.destroy(gracefully=False) + vm_names = params.get("vms").split() + if len(vm_names) < 2: + test.cancel("No multi vms provided.") + + # Backup vm xml files. + vms_backup = [] + # it need use 2 VMs for testing. + for i in list(range(2)): + if virsh.is_alive(vm_name[i]): + virsh.destroy(vm_name[i], gracefully=False) + vmxml_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_names[i]) + vms_backup.append(vmxml_backup) coldplug = "yes" == params.get("coldplug") define_error = "yes" == params.get("define_error", "no") - xml_backup = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) plug_mode = params.get("plug_mode") scsi_type = params.get("scsi_type") @@ -486,12 +530,16 @@ def run(test, params, env): check_vdisk_hostdev_address_unit(test, params, env) elif test_scenario == "unplug_scsi_controller": check_scsi_controller(test, params, env) + elif test_scenario == "shared_by_two_guests": + check_hostdev_shareable_attr(test, params, env) finally: - # Recover VM. - if vm.is_alive(): - vm.destroy(gracefully=False) - LOG.info("Restoring vm...") - xml_backup.sync() + # Recover VMs. + for i in list(range(2)): + if virsh.is_alive(vm_name[i]): + virsh.destroy(vm_name[i], gracefully=False) + LOG.info("Restoring vms...") + for vmxml_backup in vms_backup: + vmxml_backup.sync() # Delete the tmp files. libvirt.setup_or_cleanup_iscsi(is_setup=False) if test_scenario == "boot_order":