|
| 1 | +import time |
| 2 | +import logging |
| 3 | + |
| 4 | +from autotest.client.shared import error |
| 5 | +from virttest import virsh |
| 6 | + |
| 7 | + |
| 8 | +def run_libvirt_bench_domstate_switch_in_loop(test, params, env): |
| 9 | + """ |
| 10 | + Test steps: |
| 11 | +
|
| 12 | + 1) Get the params from params. |
| 13 | + 2) loop: |
| 14 | + start |
| 15 | + shutdown |
| 16 | + start |
| 17 | + suspend |
| 18 | + resume |
| 19 | + destroy |
| 20 | + 3) clean up. |
| 21 | + """ |
| 22 | + def for_each_vm(vms, virsh_func, state=None): |
| 23 | + """ |
| 24 | + Excecute the virsh_func with each vm in vms. |
| 25 | +
|
| 26 | + :Param vms: List of vm. |
| 27 | + :Param virsh_func: Function in virsh module. |
| 28 | + :Param state: State to verify the result of virsh_func. |
| 29 | + None means do not check the state. |
| 30 | + """ |
| 31 | + for vm in vms: |
| 32 | + vm_name = vm.name |
| 33 | + cmd_result = virsh_func(vm_name) |
| 34 | + if cmd_result.exit_status: |
| 35 | + raise error.TestFail(cmd_result) |
| 36 | + if state is None: |
| 37 | + continue |
| 38 | + actual_state = virsh.domstate(vm_name).stdout.strip() |
| 39 | + if not (actual_state == state): |
| 40 | + raise error.TestFail("Command %s succeed, but the state is %s," |
| 41 | + "but not %s." % |
| 42 | + (virsh_func.__name__, actual_state, state)) |
| 43 | + logging.debug("Operation %s on %s succeed.", virsh_func.__name__, vms) |
| 44 | + |
| 45 | + # Get VMs. |
| 46 | + vms = env.get_all_vms() |
| 47 | + # Get operations from params. |
| 48 | + start_in_loop = ("yes" == params.get("LB_domstate_switch_start", "no")) |
| 49 | + start_post_state = params.get("LB_domstate_switch_start_post_state", |
| 50 | + "running") |
| 51 | + shutdown_in_loop = ("yes" == params.get("LB_domstate_switch_shutdown", |
| 52 | + "no")) |
| 53 | + shutdown_post_state = params.get("LB_domstate_switch_shutdown_post_state", |
| 54 | + "shutdown") |
| 55 | + destroy_in_loop = ("yes" == params.get("LB_domstate_switch_destroy", "no")) |
| 56 | + destroy_post_state = params.get("LB_domstate_switch_destroy_post_state", |
| 57 | + "shut off") |
| 58 | + suspend_in_loop = ("yes" == params.get("LB_domstate_switch_suspend", "no")) |
| 59 | + suspend_post_state = params.get("LB_domstate_switch_suspend_post_state", |
| 60 | + "paused") |
| 61 | + resume_in_loop = ("yes" == params.get("LB_domstate_switch_resume", "no")) |
| 62 | + resume_post_state = params.get("LB_domstate_switch_resume_post_state", |
| 63 | + "running") |
| 64 | + # Get the loop_time. |
| 65 | + loop_time = int(params.get("LB_domstate_switch_loop_time", "600")) |
| 66 | + current_time = int(time.time()) |
| 67 | + end_time = current_time + loop_time |
| 68 | + # Init a counter for the loop. |
| 69 | + loop_counter = 0 |
| 70 | + try: |
| 71 | + try: |
| 72 | + # Verify the vms is all loaded completely. |
| 73 | + for vm in vms: |
| 74 | + vm.wait_for_login() |
| 75 | + # Start the loop from current_time to end_time. |
| 76 | + while current_time < end_time: |
| 77 | + if loop_counter > (len(vms) * 1000 * loop_time): |
| 78 | + raise error.TestFail("Loop ") |
| 79 | + if shutdown_in_loop: |
| 80 | + for_each_vm(vms, virsh.shutdown, shutdown_post_state) |
| 81 | + for vm in vms: |
| 82 | + if not vm.wait_for_shutdown(count=240): |
| 83 | + raise error.TestFail("Command shutdown succeed, but " |
| 84 | + "failed to wait for shutdown.") |
| 85 | + if destroy_in_loop: |
| 86 | + for_each_vm(vms, virsh.destroy, destroy_post_state) |
| 87 | + if start_in_loop: |
| 88 | + for_each_vm(vms, virsh.start, start_post_state) |
| 89 | + for vm in vms: |
| 90 | + if not vm.wait_for_login(): |
| 91 | + raise error.TestFail("Command start succeed, but " |
| 92 | + "failed to wait for login.") |
| 93 | + if suspend_in_loop: |
| 94 | + for_each_vm(vms, virsh.suspend, suspend_post_state) |
| 95 | + if resume_in_loop: |
| 96 | + for_each_vm(vms, virsh.resume, resume_post_state) |
| 97 | + logging.debug("Finish %s loop.", loop_counter) |
| 98 | + # Update the current_time and loop_counter. |
| 99 | + current_time = int(time.time()) |
| 100 | + loop_counter += 1 |
| 101 | + except error.TestFail, detail: |
| 102 | + raise error.TestFail("Succeed for %s loop, and got an error.\n" |
| 103 | + "Detail: %s." % (loop_counter, detail)) |
| 104 | + finally: |
| 105 | + # Resume vm if vm is paused. |
| 106 | + for vm in vms: |
| 107 | + if vm.is_paused(): |
| 108 | + vm.resume() |
| 109 | + vm.destroy() |
0 commit comments