On 11/30/2012 04:25 PM, Wayne Sun wrote:
v2: break down the case to small cases with separate flags * Use setVcpusFlags API to set domain vcpus with flags * 3 cases added, each only deal with one set flag value as in config, live or maximum * cases are independent on domain states, API will report error if not suitable for certain states * the sample conf is only one scenario of hotplug domain vcpus v3: merge config and maximum case to config * maximum flag can only work when domain is shutoff, merge it to config case to simplify code v4: make both vcpu and maxvcpu as optional param in config case * depend on the given params to select flags and setting Signed-off-by: Wayne Sun <gsun@xxxxxxxxxx> --- cases/set_vcpus_flags.conf | 67 ++++++++++++++++++++++++ repos/setVcpus/set_vcpus_config.py | 99 ++++++++++++++++++++++++++++++++++++ repos/setVcpus/set_vcpus_live.py | 96 ++++++++++++++++++++++++++++++++++ 3 files changed, 262 insertions(+), 0 deletions(-) create mode 100644 cases/set_vcpus_flags.conf create mode 100644 repos/setVcpus/__init__.py create mode 100644 repos/setVcpus/set_vcpus_config.py create mode 100644 repos/setVcpus/set_vcpus_live.py diff --git a/cases/set_vcpus_flags.conf b/cases/set_vcpus_flags.conf new file mode 100644 index 0000000..6cf595f --- /dev/null +++ b/cases/set_vcpus_flags.conf @@ -0,0 +1,67 @@ +domain:install_linux_cdrom + guestname + $defaultname + guestos + $defaultos + guestarch + $defaultarch + vcpu + $defaultvcpu + memory + $defaultmem + hddriver + $defaulthd + nicdriver + $defaultnic + imageformat + qcow2 + +domain:destroy + guestname + $defaultname + +setVcpus:set_vcpus_config + guestname + $defaultname + vcpu + 1 + maxvcpu + 8 + +domain:start + guestname + $defaultname + +setVcpus:set_vcpus_live + guestname + $defaultname + vcpu + 3 + username + $username + password + $password + +setVcpus:set_vcpus_config + guestname + $defaultname + vcpu + 5 + +domain:destroy + guestname + $defaultname + +domain:start + guestname + $defaultname + +domain:destroy + guestname + $defaultname + +domain:undefine + guestname + $defaultname + +options cleanup=enable diff --git a/repos/setVcpus/__init__.py b/repos/setVcpus/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/repos/setVcpus/set_vcpus_config.py b/repos/setVcpus/set_vcpus_config.py new file mode 100644 index 0000000..289dad1 --- /dev/null +++ b/repos/setVcpus/set_vcpus_config.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python +# Test set domain vcpu with flag VIR_DOMAIN_AFFECT_CONFIG or +# VIR_DOMAIN_VCPU_MAXIMUM, depend on which optional param is +# given. + +from xml.dom import minidom + +import libvirt +from libvirt import libvirtError + +from src import sharedmod + +required_params = ('guestname', ) +optional_params = {'vcpu': 1, + 'maxvcpu': 8, + } + +def get_vcpu_number(domobj): + """dump domain config xml description to get vcpu number, return + current vcpu and maximum vcpu number + """ + try: + guestxml = domobj.XMLDesc(2) + logger.debug("domain %s xml is :\n%s" %(domobj.name(), guestxml)) + xml = minidom.parseString(guestxml) + vcpu = xml.getElementsByTagName('vcpu')[0] + maxvcpu = int(vcpu.childNodes[0].data) + logger.info("domain max vcpu number is: %s" % maxvcpu) + + if vcpu.hasAttribute('current'): + attr = vcpu.getAttributeNode('current') + current = int(attr.nodeValue) + else: + logger.info("no 'current' atrribute for element vcpu") + current = int(vcpu.childNodes[0].data) + + logger.info("domain current vcpu number is: %s" % current) + + except libvirtError, e: + logger.error("libvirt call failed: " + str(e)) + return False + + return current, maxvcpu + +def set_vcpus_config(params): + """set domain vcpu with config flag and check, also set and check + max vcpu with maximum flag if optional param maxvcpu is given + """ + global logger + logger = params['logger'] + params.pop('logger') + guestname = params['guestname'] + vcpu = params.get('vcpu', None) + maxvcpu = params.get('maxvcpu', None) + + logger.info("the name of virtual machine is %s" % guestname) + if vcpu == None and maxvcpu == None: + logger.error("at least one of vcpu or maxvcpu should be provided") + return 1 + + conn = sharedmod.libvirtobj['conn'] + + try: + domobj = conn.lookupByName(guestname) + if vcpu: + logger.info("the given vcpu number is %s" % vcpu) + logger.info("set domain vcpu as %s with flag: %s" % + (vcpu, libvirt.VIR_DOMAIN_AFFECT_CONFIG)) + domobj.setVcpusFlags(int(vcpu), libvirt.VIR_DOMAIN_AFFECT_CONFIG) + logger.info("set domain vcpu succeed") + + if maxvcpu: + logger.info("the given max vcpu number is %s" % maxvcpu) + logger.info("set domain maximum vcpu as %s with flag: %s" % + (maxvcpu, libvirt.VIR_DOMAIN_VCPU_MAXIMUM)) + domobj.setVcpusFlags(int(maxvcpu), libvirt.VIR_DOMAIN_VCPU_MAXIMUM) + logger.info("set domain vcpu succeed") + + except libvirtError, e: + logger.error("libvirt call failed: " + str(e)) + return 1 + + logger.info("check domain config xml to get vcpu number") + ret = get_vcpu_number(domobj) + if vcpu: + if ret[0] == int(vcpu): + logger.info("domain current vcpu is equal as set") + else: + logger.error("domain current vcpu is not equal as set") + return 1 + + if maxvcpu: + if ret[1] == int(maxvcpu): + logger.info("domain max vcpu is equal as set") + else: + logger.error("domain max vcpu is not equal as set") + return 1 + + return 0 diff --git a/repos/setVcpus/set_vcpus_live.py b/repos/setVcpus/set_vcpus_live.py new file mode 100644 index 0000000..35a2976 --- /dev/null +++ b/repos/setVcpus/set_vcpus_live.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python +# Test set domain vcpu with flag VIR_DOMAIN_VCPU_LIVE. Check +# domain xml and inside domain to get current vcpu number. The +# live flag only work on running domain, so test on shutoff +# domain will fail. + +from xml.dom import minidom + +import libvirt +from libvirt import libvirtError + +from src import sharedmod +from utils import utils + +required_params = ('guestname', 'vcpu', 'username', 'password', ) +optional_params = {} + +def get_current_vcpu(domobj, username, password): + """dump domain live xml description to get current vcpu number + and check in domain to confirm + """ + try: + guestxml = domobj.XMLDesc(1) + guestname = domobj.name() + logger.debug("domain %s xml is :\n%s" %(guestname, guestxml)) + xml = minidom.parseString(guestxml) + vcpu = xml.getElementsByTagName('vcpu')[0] + + if vcpu.hasAttribute('current'): + attr = vcpu.getAttributeNode('current') + current = int(attr.nodeValue) + else: + logger.info("no 'current' atrribute for element vcpu") + current = int(vcpu.childNodes[0].data) + + logger.info("domain current vcpu number in live xml is: %s" % current) + + except libvirtError, e: + logger.error("libvirt call failed: " + str(e)) + return False + + logger.debug("get the mac address of vm %s" % guestname) + mac = utils.get_dom_mac_addr(guestname) + logger.debug("the mac address of vm %s is %s" % (guestname, mac)) + + logger.info("check cpu number in domain") + ip = utils.mac_to_ip(mac, 180) + + cmd = "cat /proc/cpuinfo | grep processor | wc -l" + ret, output = utils.remote_exec_pexpect(ip, username, password, cmd) + if not ret: + logger.info("cpu number in domain is %s" % output) + if int(output) == current: + logger.info("cpu in domain is equal to current vcpu value") + else: + logger.error("current vcpu is not equal as check in domain") + return False + else: + logger.error("check in domain fail") + return False + + return current + +def set_vcpus_live(params): + """set domain vcpu with live flag and check + """ + global logger + logger = params['logger'] + params.pop('logger') + guestname = params['guestname'] + vcpu = int(params['vcpu']) + username = params['username'] + password = params['password'] + + logger.info("the name of virtual machine is %s" % guestname) + logger.info("the given vcpu number is %s" % vcpu) + + conn = sharedmod.libvirtobj['conn'] + + try: + domobj = conn.lookupByName(guestname) + logger.info("set domain vcpu as %s with flag: %s" % + (vcpu, libvirt.VIR_DOMAIN_VCPU_LIVE)) + domobj.setVcpusFlags(vcpu, libvirt.VIR_DOMAIN_VCPU_LIVE) + except libvirtError, e: + logger.error("libvirt call failed: " + str(e)) + return 1 + + logger.info("check domain vcpu") + ret = get_current_vcpu(domobj, username, password) + if ret == vcpu: + logger.info("domain vcpu is equal as set") + return 0 + else: + logger.error("domain vcpu is not equal as set") + return 1
ACK and pushed. Guannan -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list