|
| 1 | +from ast import literal_eval |
| 2 | + |
| 3 | +from virttest import libvirt_version |
| 4 | +from virttest import virsh |
| 5 | +from virttest.libvirt_xml import vm_xml, xcepts |
| 6 | +from virttest.utils_libvirt import libvirt_disk |
| 7 | + |
| 8 | +from provider.virtual_disk import disk_base |
| 9 | + |
| 10 | + |
| 11 | +def run(test, params, env): |
| 12 | + """ |
| 13 | + Test disk with product info. |
| 14 | +
|
| 15 | + 1. Define a guest with a disk include product info. |
| 16 | + 2. Start guest. |
| 17 | + 3. Login guest, check the disk info. |
| 18 | + 4. Confirm that the product info is same as xml setting. |
| 19 | + """ |
| 20 | + def prepare_disk(): |
| 21 | + """ |
| 22 | + Prepare the disk with product info. |
| 23 | + """ |
| 24 | + disk_dict = literal_eval(params.get("disk_dict", "{}")) |
| 25 | + expected_error = params.get("expected_error") |
| 26 | + status_error = "yes" == params.get("status_error", "no") |
| 27 | + try: |
| 28 | + disk_obj.add_vm_disk(disk_type, disk_dict) |
| 29 | + except xcepts.LibvirtXMLError as xml_error: |
| 30 | + if not status_error: |
| 31 | + test.fail(f"Failed to define VM:\n {str(xml_error)}") |
| 32 | + else: |
| 33 | + if expected_error and expected_error not in str(xml_error): |
| 34 | + test.fail(f"Expected error '{expected_error}' not found in" |
| 35 | + " actual error: {xml_error}") |
| 36 | + test.log.debug(f"Get expected error message:\n {expected_error}") |
| 37 | + return False |
| 38 | + return True |
| 39 | + |
| 40 | + def check_guest(): |
| 41 | + """ |
| 42 | + Check the disk info in guest. |
| 43 | + """ |
| 44 | + vm_session = vm.wait_for_login() |
| 45 | + new_disk, _ = libvirt_disk.get_non_root_disk_name(vm_session) |
| 46 | + sg_command = "sg_inq -p di -v /dev/%s" % new_disk |
| 47 | + sg_output = vm_session.cmd_output(sg_command) |
| 48 | + if disk_product not in sg_output: |
| 49 | + test.fail(f"Product info '{disk_product}' not found in command output: {sg_output}") |
| 50 | + else: |
| 51 | + test.log.debug("Product info found in guest as expected.") |
| 52 | + |
| 53 | + libvirt_version.is_libvirt_feature_supported(params) |
| 54 | + vm_name = params.get("main_vm") |
| 55 | + disk_type = params.get("disk_type", "file") |
| 56 | + disk_product = params.get("disk_product") |
| 57 | + vm = env.get_vm(vm_name) |
| 58 | + |
| 59 | + vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) |
| 60 | + backup_xml = vmxml.copy() |
| 61 | + disk_obj = disk_base.DiskBase(test, vm, params) |
| 62 | + |
| 63 | + try: |
| 64 | + test.log.info("Start a guest with product info.") |
| 65 | + if not prepare_disk(): |
| 66 | + return |
| 67 | + if not vm.is_alive(): |
| 68 | + vm.start() |
| 69 | + test.log.debug(f"The current guest xml is: {virsh.dumpxml(vm_name).stdout_text}") |
| 70 | + test.log.info("Check the product info in guest.") |
| 71 | + check_guest() |
| 72 | + |
| 73 | + finally: |
| 74 | + if vm.is_alive(): |
| 75 | + vm.destroy() |
| 76 | + backup_xml.sync() |
| 77 | + disk_obj.cleanup_disk_preparation(disk_type) |
0 commit comments