|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import re |
| 4 | +import shutil |
| 5 | + |
| 6 | +from virttest import virsh |
| 7 | +from virttest.utils_test import libvirt |
| 8 | + |
| 9 | +LOG = logging.getLogger("avocado.test." + __name__) |
| 10 | + |
| 11 | + |
| 12 | +def vm_state_check(test, vm_name, new_disk_path, virsh_opt): |
| 13 | + """ |
| 14 | + Check domain xml and state after restore from saved image. |
| 15 | + """ |
| 16 | + cmd_re = virsh.dumpxml(vm_name, debug=True) |
| 17 | + if cmd_re.exit_status: |
| 18 | + test.fail(f"Failed to dump xml of domain '{vm_name}'") |
| 19 | + |
| 20 | + # The xml should contain the match_string |
| 21 | + xml = cmd_re.stdout.strip() |
| 22 | + xml_pattern = f"<source file='{new_disk_path}'" |
| 23 | + LOG.info(f"Checking for disk path '{new_disk_path}' in domain XML.") |
| 24 | + |
| 25 | + if not re.search(xml_pattern, xml): |
| 26 | + test.fail( |
| 27 | + f"After domain restore the xml is not expected, could not find source file='{new_disk_path}'" |
| 28 | + ) |
| 29 | + else: |
| 30 | + LOG.info(f"Found source file='{new_disk_path}' in the domain xml successfully!") |
| 31 | + |
| 32 | + dom_state = virsh.domstate(vm_name, debug=True).stdout.strip() |
| 33 | + LOG.info(f"VM state after restore is '{dom_state}'") |
| 34 | + if virsh_opt == "--running" and dom_state != "running": |
| 35 | + test.fail( |
| 36 | + "The domain state is not as expected with option '--running'. Got 'paused', expected 'running'" |
| 37 | + ) |
| 38 | + elif virsh_opt == "--paused" and dom_state != "paused": |
| 39 | + test.fail( |
| 40 | + "The domain state is not as expected with option '--paused'. Got 'running', expected 'paused'" |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def run(test, params, env): |
| 45 | + """ |
| 46 | + Test command: virsh managedsave-edit <vm_name> [option] |
| 47 | + 1) Prepare test environment, ensure VM is in requested state: running or paused |
| 48 | + 2) Do managedsave for the VM |
| 49 | + 3) Execute virsh managedsave-edit to edit xml |
| 50 | + 4) Start VM |
| 51 | + 5) Check the new xml of the VM and its state |
| 52 | + """ |
| 53 | + pre_state = params.get("pre_state") |
| 54 | + readonly = "yes" == params.get("readonly", "no") |
| 55 | + status_error = "yes" == params.get("status_error", "no") |
| 56 | + virsh_opt = params.get("virsh_opt") |
| 57 | + |
| 58 | + vm_name = params.get("main_vm") |
| 59 | + vm = env.get_vm(vm_name) |
| 60 | + new_path = None |
| 61 | + try: |
| 62 | + LOG.info("Preparing the VM state...") |
| 63 | + if not vm.is_alive(): |
| 64 | + vm.start() |
| 65 | + if pre_state == "paused": |
| 66 | + virsh.suspend(vm_name, debug=True) |
| 67 | + vm_state = virsh.domstate(vm_name).stdout_text.strip() |
| 68 | + LOG.info(f"The VM state is '{vm_state}' before save") |
| 69 | + |
| 70 | + LOG.info("TEST STEP 1: Perform 'virsh managedsave' to save the vm...") |
| 71 | + cmd_result = virsh.managedsave(vm_name, debug=True) |
| 72 | + libvirt.check_result(cmd_result) |
| 73 | + |
| 74 | + LOG.info("TEST STEP 2: Run managedsave-edit to update the disk path:") |
| 75 | + disk_path = vm.get_first_disk_devices()["source"] |
| 76 | + new_path = f"{disk_path}.test" |
| 77 | + |
| 78 | + # Ensure the new path does not already exist |
| 79 | + if os.path.exists(new_path): |
| 80 | + LOG.warning(f"The path '{new_path}' already exists. Removing it...") |
| 81 | + os.remove(new_path) |
| 82 | + |
| 83 | + shutil.copy(disk_path, new_path) |
| 84 | + LOG.info(f"The original disk path is '{disk_path}'") |
| 85 | + LOG.info(f"The new disk path will be: '{new_path}'") |
| 86 | + |
| 87 | + replace_string = r":%s /" + disk_path.replace("/", "\/") |
| 88 | + replace_string += "/" + new_path.replace("/", "\/") |
| 89 | + stat = libvirt.exec_virsh_edit( |
| 90 | + vm_name, |
| 91 | + [replace_string], |
| 92 | + managedsave_edit=True, |
| 93 | + readonly=readonly, |
| 94 | + virsh_opt=virsh_opt, |
| 95 | + ) |
| 96 | + if not stat: |
| 97 | + if status_error: |
| 98 | + LOG.info("Failed to edit the xml, this was expected!") |
| 99 | + else: |
| 100 | + test.fail("managedsave-edit failed!") |
| 101 | + else: |
| 102 | + if status_error: |
| 103 | + test.fail("managedsave-edit succeeded when it should have failed!") |
| 104 | + |
| 105 | + if not status_error: |
| 106 | + LOG.info("TEST STEP 3: Start VM for positive scenarios:") |
| 107 | + cmd_result = virsh.start(vm_name, debug=True) |
| 108 | + libvirt.check_result(cmd_result) |
| 109 | + LOG.info( |
| 110 | + "TEST STEP 4: Verify VM status and the XML content for positive scenarios:" |
| 111 | + ) |
| 112 | + vm_state_check(test, vm_name, new_path, virsh_opt) |
| 113 | + except Exception as e: |
| 114 | + test.error(f"Unexpected error happened during the test execution: {e}") |
| 115 | + finally: |
| 116 | + # Cleanup |
| 117 | + if new_path and os.path.exists(new_path): |
| 118 | + LOG.info(f"Remove the file {new_path}") |
| 119 | + os.remove(new_path) |
| 120 | + virsh.managedsave_remove(vm_name, debug=True) |
0 commit comments