Skip to content

Commit

Permalink
Guestvcpus: enable/disable max vcpu by guest agent
Browse files Browse the repository at this point in the history
  • Loading branch information
rh-jugraham committed Sep 24, 2024
1 parent b89959f commit 415f7c0
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 70 deletions.
4 changes: 4 additions & 0 deletions libvirt/tests/cfg/virsh_cmd/domain/virsh_guestvcpus.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
take_regular_screendumps="no"
vcpus_num = "20"
vcpus_placement = "static"
max_test_combine = ""
option = ""
combine = ""
error_msg = []
Expand All @@ -26,6 +27,9 @@
option = "--enable"
- combine:
combine = "yes"
- max_test:
status_error = "no"
max_test_combine = "yes"
- error_test:
status_error = "yes"
variants:
Expand Down
182 changes: 112 additions & 70 deletions libvirt/tests/src/virsh_cmd/domain/virsh_guestvcpus.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging as log


from virttest import virt_vm
from virttest import virsh
from virttest import cpu as cpuutil
from virttest.utils_test import libvirt
Expand All @@ -19,113 +20,154 @@ def run(test, params, env):
The command query or modify state of vcpu in the vm
1. Prepare test environment, start vm with guest agent
2. Perform virsh guestvcpus query/enable/disable operation
3. Check the cpus in the vm
4. Recover test environment
3. Check the vcpu number by guest agent
4. Check the cpu number in the guest
5. Recover test environment
"""
vm_name = params.get("main_vm")
vm = env.get_vm(vm_name)
vcpus_num = int(params.get("vcpus_num", "20"))
vcpus_placement = params.get("vcpus_placement", "static")
max_test_combine = params.get("max_test_combine", "")
option = params.get("option", "")
combine = params.get("combine", "")
invalid_domain = params.get("invalid_domain", "")
domain_name = params.get("domain_name", "")
invalid_cpulist = params.get("invalid_cpulist", "")
status_error = params.get("status_error", "no")
error_msg = eval(params.get('error_msg', '[]'))
vcpus_list = ""
offline_vcpus = ""

# Back up domain XML
vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name)
vmxml_bakup = vmxml.copy()

# Max test: set vcpus_num to the host online cpu number
if max_test_combine == "yes":
host_cpu_info = cpuutil.get_cpu_info()
host_online_cpus = int(host_cpu_info["On-line CPU(s) list"].split("-")[1]) + 1
logging.debug("Host online CPU number: %s", str(host_online_cpus))
vcpus_num = host_online_cpus

try:
# Modify vm with static vcpus
if vm.is_alive():
vm.destroy()
vmxml.placement = vcpus_placement
vmxml.set_vm_vcpus(vm_name, vcpus_num, vcpus_num, topology_correction=True)
logging.debug("Define guest with '%s' vcpus" % str(vcpus_num))
logging.debug("Define guest with '%s' vcpus", str(vcpus_num))

# Start guest agent in vm
vm.prepare_guest_agent()
try:
vm.prepare_guest_agent()
except virt_vm.VMStartError as info:
if "not supported" in str(info).lower():
test.cancel(info)
else:
test.error(info)

# Normal test: disable/ enable guest vcpus
# Normal test: disable/enable guest vcpus
if option and status_error == "no":
for cpu in range(1, vcpus_num):
virsh.guestvcpus(vm_name, str(cpu), option, debug=True)
check_cpu_count(test, params, env, vcpus_num, option)

# Normal test: combine: --disable 1-max then --enable 1
if combine == "yes" and status_error == "no":
# Combine: --disable 1-max then --enable
if (max_test_combine == "yes" or combine == "yes") and status_error == "no":
vcpus_list = '1' + '-' + str(vcpus_num - 1)
option = "--disable"
virsh.guestvcpus(vm_name, vcpus_list, option, debug=True)
vcpus_list = '1'
option = "--enable"
virsh.guestvcpus(vm_name, vcpus_list, option, debug=True)

# Error test: invalid_domain
if invalid_domain == "yes":
vm_name = domain_name
# Error test: invalid_cpulist
if invalid_cpulist == "yes":
if option == "--enable":
vcpus_list = str(vcpus_num)
else:
vcpus_list = '0' + '-' + str(vcpus_num - 1)
ret = virsh.guestvcpus(vm_name, vcpus_list, option)
else:
# Query guest vcpus
ret = virsh.guestvcpus(vm_name)
output = ret.stdout.strip()
check_cpu_count(test, params, env, vcpus_num, option)

# Check test results
if status_error == "yes":
libvirt.check_result(ret, error_msg)
else:
# Check the test result of query
ret_output = dict([item.strip() for item in line.split(":")]
for line in output.split("\n"))
# Max test: --enable 1-max (no change to vcpus_list)
# Normal test: --enable 1
if combine == "yes":
online_vcpus = '0-1'
elif option == "--disable":
online_vcpus = '0'
else:
online_vcpus = '0' + '-' + str(vcpus_num - 1)

if ret_output["online"] != online_vcpus:
test.fail("Query result is different from"
" the '%s' command." % option)
vcpus_list = '1'

# Check the cpu in guest
session = vm.wait_for_login()
vm_cpu_info = cpuutil.get_cpu_info(session)
session.close()

if combine == "yes":
online_vcpus = '0,1'
elif option == "--disable":
online_vcpus = '0'
offline_vcpus = '1' + '-' + str(vcpus_num - 1)
else:
online_vcpus = '0' + '-' + str(vcpus_num - 1)

if offline_vcpus:
if (vm_cpu_info["Off-line CPU(s) list"] != offline_vcpus or
vm_cpu_info["On-line CPU(s) list"] != online_vcpus):
test.fail("CPUs in vm is different from"
" the %s command." % option)
elif vm_cpu_info["On-line CPU(s) list"] != online_vcpus:
test.fail("On-line CPUs in vm is different"
" from the %s command." % option)
else:
logging.debug("lscpu in vm '%s' is: \n '%s'" %
(vm_name, vm_cpu_info))
option = "--enable"
virsh.guestvcpus(vm_name, vcpus_list, option, debug=True)
check_cpu_count(test, params, env, vcpus_num, option)

finally:
# Recover VM
if vm.is_alive():
vm.destroy(gracefully=False)
logging.info("Restoring vm...")
vmxml_bakup.sync()


def check_cpu_count(test, params, env, vcpus_num, option=""):
"""
Makes any changes necessary for the error test and then
runs the (v)cpu checks specified in steps 3 and 4 of the run cmd
3. Check the vcpu number by guest agent
4. Check the cpu number in the guest
"""
vm_name = params.get("main_vm")
vm = env.get_vm(vm_name)
combine = params.get("combine", "")
invalid_domain = params.get("invalid_domain", "")
domain_name = params.get("domain_name", "")
invalid_cpulist = params.get("invalid_cpulist", "")
status_error = params.get("status_error", "no")
error_msg = eval(params.get('error_msg', '[]'))
vcpus_list = ""
offline_vcpus = ""

# Error test: invalid_domain
if invalid_domain == "yes":
vm_name = domain_name
# Error test: invalid_cpulist
if invalid_cpulist == "yes":
if option == "--enable":
vcpus_list = str(vcpus_num)
else:
vcpus_list = '0' + '-' + str(vcpus_num - 1)
ret = virsh.guestvcpus(vm_name, vcpus_list, option)
else:
# Query guest vcpus
ret = virsh.guestvcpus(vm_name)
output = ret.stdout.strip()

# Check test results
if status_error == "yes":
libvirt.check_result(ret, error_msg)
else:
# Check the test result of query
ret_output = dict([item.strip() for item in line.split(":")]
for line in output.split("\n"))
if combine == "yes" and option == "--enable":
online_vcpus = '0-1'
elif option == "--disable":
online_vcpus = '0'
else:
# either normal --enable test or max test on the --enable step
online_vcpus = '0' + '-' + str(vcpus_num - 1)

if ret_output["online"] != online_vcpus:
test.fail("Query result is different from"
" the '%s' command."
" Expected %s "
" but query result is %s." % (option, online_vcpus, ret_output["online"]))

# Check the cpu in guest
session = vm.wait_for_login()
vm_cpu_info = cpuutil.get_cpu_info(session)
session.close()

if combine == "yes" and option == "--enable":
online_vcpus = '0,1'
elif option == "--disable":
online_vcpus = '0'
offline_vcpus = '1' + '-' + str(vcpus_num - 1)
else:
# either normal --enable test or max test on the --enable step
online_vcpus = '0' + '-' + str(vcpus_num - 1)

if offline_vcpus:
if (vm_cpu_info["Off-line CPU(s) list"] != offline_vcpus or
vm_cpu_info["On-line CPU(s) list"] != online_vcpus):
test.fail("CPUs in vm is different from"
" the %s command." % option)
elif vm_cpu_info["On-line CPU(s) list"] != online_vcpus:
test.fail("On-line CPUs in vm is different"
" from the %s command." % option)
logging.debug("lscpu in vm '%s' is: \n '%s'",
vm_name, vm_cpu_info)

0 comments on commit 415f7c0

Please sign in to comment.