ypu/autotest — comment on commit e38247e1da0a20b14e7c9537861df5d6534e95ca
File:  line 0
By lmr:

The problem here is that in _call_image_func(), there's no vm variable in the function scope, that will cause a problem while executing this block of code. For this function to do what you want to, first we have to check on the presence of params.objects('vms') and then act conditionally:

def process(test, params, env, image_func, vm_func, vm_first=False):
"""
Pre- or post-process VMs and images according to the instructions in params.
Call image_func for each image listed in params and vm_func for each VM.

@param test: An Autotest test object.
@param params: A dict containing all VM and image parameters.
@param env: The environment (a dict-like object).
@param image_func: A function to call for each image.
@param vm_func: A function to call for each VM.
@param vm_first: Call vm_func first or not.
"""
def _call_vm_func():
for vm_name in params.objects("vms"):
vm_params = params.object_params(vm_name)
vm = env.get_vm(vm_name)
vm_func(test, vm_params, env, vm_name)

def _call_image_func():
if params.objects("vms"):
for vm_name in params.objects("vms"):
vm_params = params.object_params(vm_name)
vm = env.get_vm(vm_name)
for image_name in vm_params.objects("images"):
image_params = vm_params.object_params(image_name)
# Call image_func for each image
if vm is not None and vm.is_alive():
vm.pause()
image_func(test, image_params)
if vm is not None and vm.is_alive():
vm.resume()
else:
for image_name in params.objects("images"):
image_params = params.object_params(image_name)
image_func(test, image_params)

if not vm_first:
_call_image_func()

_call_vm_func()

if vm_first:
_call_image_func()

The reason I had to mix up images with vms was when I implemented an image backup feature. It's simply not secure to do certain image operations with the vm turned on, such as performing a raw image copy, so unfortunately this is needed. Let me know if you are OK with the alternate version of the function, and if you can, test it, I'll test it here as well.