Skip to content

Commit

Permalink
Merge pull request #197 from xcp-ng/host_version_filter
Browse files Browse the repository at this point in the history
Add support to filter VMs based on host version
  • Loading branch information
stormi authored Feb 28, 2024
2 parents bc0ec12 + 1b7ee20 commit b59e7ad
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 4 deletions.
56 changes: 52 additions & 4 deletions jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,24 @@
"tests/migration/test_host_evacuate.py::TestHostEvacuateWithNetwork",
]

def get_vm_or_vms_refs(handle):
# Returns the vm filename or None if a host_version is passed and matches the one specified
# with the vm filename in vm_data.py. ex: ("centos6-32-hvm-created_8.2-zstd.xva", "8\.2\..*")
def filter_vm(vm, host_version):
import re

if type(vm) is tuple:
if len(vm) < 2:
return None

if host_version is not None and not re.match(vm[1], host_version):
print(f"Host version pattern '{vm[1]}' for '{vm[0]}' doesn't match version '{host_version}'. Filtered out")
return None

return vm[0]

return vm

def get_vm_or_vms_refs(handle, host_version=None):
try:
from vm_data import VMS
except ImportError:
Expand All @@ -347,14 +364,45 @@ def get_vm_or_vms_refs(handle):
print("You need to update your local vm_data.py.")
print("You may also bypass this error by providing your own --vm parameter(s).")
sys.exit(1)
return VMS[category][key]

if type(VMS[category][key]) is list:
# Multi VMs
vms = list()
for vm in VMS[category][key]:
xva = filter_vm(vm, host_version)
if xva is not None:
vms.append(xva)
if len(vms) == 0:
vms = None
else:
# Single VMs
vms = filter_vm(VMS[category][key], host_version)

if vms is None:
print(f"ERROR: Could not find VMS['{category}']['{key}'] for host version {host_version}.")
print("You need to update your local vm_data.py.")
print("You may also bypass this error by providing your own --vm parameter(s).")
sys.exit(1)

return vms

def build_pytest_cmd(job_data, hosts=None, pytest_args=[]):
markers = job_data.get("markers", None)
name_filter = job_data.get("name_filter", None)

job_params = dict(job_data["params"])

host_version = None
if hosts is not None:
try:
host = hosts.split(',')[0]
cmd = ["ssh", host, "lsb_release", "-sr"]
res = subprocess.run(cmd, capture_output=True, text=True)
if res.returncode == 0:
host_version = res.stdout.strip()
except Exception as e:
print(e, file=sys.stderr)

def _join_pytest_args(arg, option):
cli_args = []
try:
Expand Down Expand Up @@ -397,11 +445,11 @@ def _join_pytest_args(arg, option):
cmd.append(f"--hosts={hosts}")
for key, value in job_params.items():
if key == "--vm[]":
vms = get_vm_or_vms_refs(value)
vms = get_vm_or_vms_refs(value, host_version)
for vm_ref in vms:
cmd.append(f"--vm={vm_ref}")
elif key == "--vm":
cmd.append(f"--vm={get_vm_or_vms_refs(value)}")
cmd.append(f"--vm={get_vm_or_vms_refs(value, host_version)}")
else:
cmd.append(f"{key}={value}")
cmd += pytest_args
Expand Down
39 changes: 39 additions & 0 deletions vm_data.py-dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@
# Consult README.md for accepted values.
# Example value: "http://somewhere/images/small_vm.xva"

# The "single" element contains 1 entry per VM filename:
# "small_vm": "alpine-minimal-3.12.0.xva",
# "small_vm_unix_tools": "centos7-64-created_8.0-zstd.xva",

# If a vm is meant to be used only on a given host version, the version can be specified
# through a tuple entry with (vm_filename, host_version) as:
# "small_vm": ("alpine-minimal-3.12.0.xva", "8\.3\.")

# The host version specified for a vm is matched using re.match(), so valid patterns
# can be "8\.2\.1", "8\.[23]\.", or "(9\.0|8\.2)".

# If host version is not specified, the vm filename will be used whatever the host version

# The "multi" element contains a list of VMs per entry
# "all": ["alpine-minimal-3.12.0.xva",
# "alpine-uefi-minimal-efitools-3.12.0.xva",
# ...],

# Same as "single", a vm can be bound to a given host version:
# "all": ["alpine-minimal-3.12.0.xva",
# ("alpine-uefi-minimal-efitools-3.12.0.xva", "8\.2\."),
# ...],

VMS = {
"single": {
# basic small VM
Expand All @@ -27,3 +50,19 @@ VMS = {
"uefi_windows": [],
}
}

# Example of use for a common XVA_LOCATION
#
# XVA_LOCATION="http://somewhere/"
#
# def prepend_xva_location(vm):
# if type(vm) is tuple:
# return (XVA_LOCATION + vm[0], vm[1])
#
# return XVA_LOCATION + vm
#
# for key, vm in dict(VMS["single"]).items():
# VMS["single"][key] = prepend_xva_location(vm)
#
# for key, vms in dict(VMS["multi"]).items():
# VMS["multi"][key] = [prepend_xva_location(vm) for vm in vms]

0 comments on commit b59e7ad

Please sign in to comment.