[libvirt] [test-API][PATCH] Add test case set_cpu_shares.py for setting cpu scheduler info

* repos/domain/set_cpu_shares.py: set the value of cpu_shares property of the guest. --- repos/domain/set_cpu_shares.py | 111 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 111 insertions(+), 0 deletions(-) create mode 100644 repos/domain/set_cpu_shares.py diff --git a/repos/domain/set_cpu_shares.py b/repos/domain/set_cpu_shares.py new file mode 100644 index 0000000..4560809 --- /dev/null +++ b/repos/domain/set_cpu_shares.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +"""Set the value of cpu_shares property of the guest + domain:set_cpu_shares + guestname + xxx + flags + 0|1|2 +""" + +__author__ = 'Nan Zhang: nzhang@redhat.com' +__date__ = 'Tue Sep 27, 2011' +__version__ = '0.1.0' +__credits__ = 'Copyright (C) 2011 Red Hat, Inc.' +__all__ = ['check_params', 'check_cpu_shares', 'set_cpu_shares'] + +import os +import re +import sys +import time +from xml.dom import minidom + + +def append_path(path): + """Append root path of package""" + if path in sys.path: + pass + else: + sys.path.append(path) + +pwd = os.getcwd() +result = re.search('(.*)libvirt-test-API', pwd) +append_path(result.group(0)) + +from lib import connectAPI +from lib import domainAPI +from utils.Python import utils +from utils.Python import xmlbuilder +from exception import LibvirtAPI + +def check_params(params): + """Verify inputing parameter dictionary""" + logger = params['logger'] + keys = ['guestname', 'flags'] + for key in keys: + if key not in params: + logger.error("%s is required" %key) + return 1 + return 0 + +def check_cpu_shares(params, util, guestname, cpu_shares): + """Check the value of cpu_shares""" + logger = params['logger'] + cmd = "cat /cgroup/cpu/libvirt/qemu/%s/cpu.shares" % guestname + ret, out = util.exec_cmd(cmd, shell=True) + if ret: + logger.error("fail to set the value of cpu_shares: %s" % out[0]) + return 0 + else: + logger.info("from cgroup, the value of cpu_shares is %s" % out[0]) + + if cmp(int(out[0]), cpu_shares): + return 1 + else: + logger.info("the value of cpu_shares does match the original \ +cpu scheduler information.") + return 0 + +def set_cpu_shares(params): + """Get the cpu scheduler information""" + # Initiate and check parameters + params_check_result = check_params(params) + if params_check_result: + return 1 + logger = params['logger'] + guestname = params['guestname'] + flags = params['flags'] + schedinfo = {} + schedinfo['cpu_shares'] = 2048 + cpu_shares = schedinfo['cpu_shares'] + + # Connect to local hypervisor connection URI + util = utils.Utils() + uri = util.get_uri('127.0.0.1') + conn = connectAPI.ConnectAPI() + virconn = conn.open(uri) + + caps = conn.get_caps() + logger.debug(caps) + + domobj = domainAPI.DomainAPI(virconn) + try: + domobj.set_sched_params_flags(guestname, schedinfo, int(flags)) + logger.debug("set the value of cpu_shares with %s" % cpu_shares) + except LibvirtAPI, e: + logger.error("API error message: %s, error code is %s" % + (e.response()['message'], e.response()['code'])) + return 1 + + check_result = check_cpu_shares(params, util, guestname, cpu_shares) + if check_result: + logger.error("cpu_shares does not match.") + conn.close() + return 1 + + logger.info("success to set scheduler parameters.") + conn.close() + return 0 + +def set_cpu_shares_clean(): + """Clean testing environment""" + pass -- 1.7.4.4

On 2011年12月12日 15:22, Nan Zhang wrote:
* repos/domain/set_cpu_shares.py: set the value of cpu_shares property of the guest. --- repos/domain/set_cpu_shares.py | 111 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 111 insertions(+), 0 deletions(-) create mode 100644 repos/domain/set_cpu_shares.py
diff --git a/repos/domain/set_cpu_shares.py b/repos/domain/set_cpu_shares.py new file mode 100644 index 0000000..4560809 --- /dev/null +++ b/repos/domain/set_cpu_shares.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +"""Set the value of cpu_shares property of the guest + domain:set_cpu_shares + guestname + xxx + flags + 0|1|2 +"""
Per you are testing the cpu_shares, there should be one parameter for it, so that one can pass whatever value for cpu_shares as he wants. I see you hardcode it as 2048. Which is not the right way to go. And 0|1|2 is not that visible for one get what the actual meaning is. You might want to use more sensiable strings to do that e.g. "live", "config", "current". Also it's quite bad to use the integers (such as 0|1|2) directly in the codes, which is not good for reading and future maintaining. So you might want to do like following: "live" --convert--> "libvirt.VIR_DOMAIN_AFFECT_LIVE" dom.setSchedulerParametersFlags(params,flags) Regards, Osier

On 12/12/2011 03:46 PM, Osier Yang wrote:
On 2011年12月12日 15:22, Nan Zhang wrote:
* repos/domain/set_cpu_shares.py: set the value of cpu_shares property of the guest. --- repos/domain/set_cpu_shares.py | 111 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 111 insertions(+), 0 deletions(-) create mode 100644 repos/domain/set_cpu_shares.py
diff --git a/repos/domain/set_cpu_shares.py b/repos/domain/set_cpu_shares.py new file mode 100644 index 0000000..4560809 --- /dev/null +++ b/repos/domain/set_cpu_shares.py @@ -0,0 +1,111 @@ +#!/usr/bin/env python +"""Set the value of cpu_shares property of the guest + domain:set_cpu_shares + guestname + xxx + flags + 0|1|2 +"""
Per you are testing the cpu_shares, there should be one parameter for it, so that one can pass whatever value for cpu_shares as he wants. I see you hardcode it as 2048. Which is not the right way to go.
And 0|1|2 is not that visible for one get what the actual meaning is. You might want to use more sensiable strings to do that e.g. "live", "config", "current".
Also it's quite bad to use the integers (such as 0|1|2) directly in the codes, which is not good for reading and future maintaining.
So you might want to do like following:
"live" --convert--> "libvirt.VIR_DOMAIN_AFFECT_LIVE"
dom.setSchedulerParametersFlags(params,flags)
Regards, Osier
Hey, It's really good suggestions for me, thanks Osier :-) I will update and re-send a new patch for it. Regards, nzhang

* repos/domain/get_cpu_shares.py: get the value of cpu_shares property of the guest. --- lib/domainAPI.py | 2 +- repos/domain/get_cpu_shares.py | 117 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletions(-) create mode 100644 repos/domain/get_cpu_shares.py diff --git a/lib/domainAPI.py b/lib/domainAPI.py index a6efab7..0058254 100644 --- a/lib/domainAPI.py +++ b/lib/domainAPI.py @@ -546,7 +546,7 @@ class DomainAPI(object): def set_sched_params_flags(self, domname, params, flags): try: dom_obj = self.get_domain_by_name(domname) - retval = dom_obj.setSchedulerParameters(params, flags) + retval = dom_obj.setSchedulerParametersFlags(params, flags) return retval except libvirt.libvirtError, e: message = e.get_error_message() diff --git a/repos/domain/get_cpu_shares.py b/repos/domain/get_cpu_shares.py new file mode 100644 index 0000000..5d26e82 --- /dev/null +++ b/repos/domain/get_cpu_shares.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +"""Get the value of cpu_shares property of the guest + domain:get_cpu_shares + guestname + xxx + flags + current|live|config +""" + +__author__ = 'Nan Zhang: nzhang@redhat.com' +__date__ = 'Tue Sep 27, 2011' +__version__ = '0.1.0' +__credits__ = 'Copyright (C) 2011 Red Hat, Inc.' +__all__ = ['check_params', 'check_cpu_shares', 'get_cpu_shares'] + +import os +import re +import sys +import time +from xml.dom import minidom + + +def append_path(path): + """Append root path of package""" + if path in sys.path: + pass + else: + sys.path.append(path) + +pwd = os.getcwd() +result = re.search('(.*)libvirt-test-API', pwd) +append_path(result.group(0)) + +from lib import connectAPI +from lib import domainAPI +from utils.Python import utils +from utils.Python import xmlbuilder +from exception import LibvirtAPI + +def check_params(params): + """Verify inputing parameter dictionary""" + logger = params['logger'] + keys = ['guestname', 'flags'] + for key in keys: + if key not in params: + logger.error("%s is required" %key) + return 1 + return 0 + +def check_cpu_shares(params, util, guestname, cpu_shares, flags): + """Check the value of cpu_shares""" + logger = params['logger'] + cmd = "cat /cgroup/cpu/libvirt/qemu/%s/cpu.shares" % guestname + ret, out = util.exec_cmd(cmd, shell=True) + if ret: + logger.error("fail to get the value of cpu_shares: %s" % out[0]) + else: + logger.info("from cgroup, the value of cpu_shares: %s" % out[0]) + + if flags == domainAPI.VIR_DOMAIN_AFFECT_CONFIG: + return 0 + + if cmp(int(out[0]), cpu_shares): + return 1 + else: + logger.info("the value of cpu_shares does match the original \ +cpu scheduler information.") + return 0 + +def get_cpu_shares(params): + """Get the cpu scheduler information""" + # Initiate and check parameters + params_check_result = check_params(params) + if params_check_result: + return 1 + logger = params['logger'] + guestname = params['guestname'] + + if params['flags'] == 'current': + flags = domainAPI.VIR_DOMAIN_AFFECT_CURRENT + elif params['flags'] == 'live': + flags = domainAPI.VIR_DOMAIN_AFFECT_LIVE + elif params['flags'] == 'config': + flags = domainAPI.VIR_DOMAIN_AFFECT_CONFIG + else: + logger.error("Invalid flag was specified.") + return 1 + + # Connect to local hypervisor connection URI + util = utils.Utils() + uri = util.get_uri('127.0.0.1') + conn = connectAPI.ConnectAPI() + virconn = conn.open(uri) + + domobj = domainAPI.DomainAPI(virconn) + try: + sched_info = domobj.get_sched_params_flags(guestname, flags) + cpu_shares = sched_info['cpu_shares'] + logger.debug("the value of cpu_shares is %s" % cpu_shares) + except LibvirtAPI, e: + logger.error("API error message: %s, error code is %s" % + (e.response()['message'], e.response()['code'])) + return 1 + + check_result = check_cpu_shares(params, util, guestname, cpu_shares, flags) + if check_result: + logger.error("cpu_shares does not match.") + conn.close() + return 1 + + logger.info("success to get scheduler parameters.") + conn.close() + return 0 + +def get_cpu_shares_clean(): + """Clean testing environment""" + pass -- 1.7.4.4

* repos/domain/set_cpu_shares.py: set the value of cpu_shares property of the guest. --- repos/domain/set_cpu_shares.py | 121 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 121 insertions(+), 0 deletions(-) create mode 100644 repos/domain/set_cpu_shares.py diff --git a/repos/domain/set_cpu_shares.py b/repos/domain/set_cpu_shares.py new file mode 100644 index 0000000..809ccef --- /dev/null +++ b/repos/domain/set_cpu_shares.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python +"""Set the value of cpu_shares property of the guest + domain:set_cpu_shares + guestname + xxx + cpu_shares + integer value + flags + current|live|config +""" + +__author__ = 'Nan Zhang: nzhang@redhat.com' +__date__ = 'Tue Sep 27, 2011' +__version__ = '0.1.0' +__credits__ = 'Copyright (C) 2011 Red Hat, Inc.' +__all__ = ['check_params', 'check_cpu_shares', 'set_cpu_shares'] + +import os +import re +import sys +import time +from xml.dom import minidom + + +def append_path(path): + """Append root path of package""" + if path in sys.path: + pass + else: + sys.path.append(path) + +pwd = os.getcwd() +result = re.search('(.*)libvirt-test-API', pwd) +append_path(result.group(0)) + +from lib import connectAPI +from lib import domainAPI +from utils.Python import utils +from utils.Python import xmlbuilder +from exception import LibvirtAPI + +def check_params(params): + """Verify inputing parameter dictionary""" + logger = params['logger'] + keys = ['guestname', 'flags'] + for key in keys: + if key not in params: + logger.error("%s is required" %key) + return 1 + return 0 + +def check_cpu_shares(params, util, guestname, cpu_shares, flags): + """Check the value of cpu_shares""" + logger = params['logger'] + cmd = "cat /cgroup/cpu/libvirt/qemu/%s/cpu.shares" % guestname + ret, out = util.exec_cmd(cmd, shell=True) + if ret: + logger.error("fail to set the value of cpu_shares: %s" % out[0]) + else: + logger.info("from cgroup, the value of cpu_shares is %s" % out[0]) + + if flags == domainAPI.VIR_DOMAIN_AFFECT_CONFIG: + return 0 + + if cmp(int(out[0]), cpu_shares): + return 1 + else: + logger.info("the value of cpu_shares does match the original \ +cpu scheduler information.") + return 0 + +def set_cpu_shares(params): + """Get the cpu scheduler information""" + # Initiate and check parameters + params_check_result = check_params(params) + if params_check_result: + return 1 + logger = params['logger'] + guestname = params['guestname'] + schedinfo = {} + schedinfo['cpu_shares'] = int(params['cpu_shares']) + cpu_shares = schedinfo['cpu_shares'] + + if params['flags'] == 'current': + flags = domainAPI.VIR_DOMAIN_AFFECT_CURRENT + elif params['flags'] == 'live': + flags = domainAPI.VIR_DOMAIN_AFFECT_LIVE + elif params['flags'] == 'config': + flags = domainAPI.VIR_DOMAIN_AFFECT_CONFIG + else: + logger.error("Invalid flag was specified.") + return 1 + + # Connect to local hypervisor connection URI + util = utils.Utils() + uri = util.get_uri('127.0.0.1') + conn = connectAPI.ConnectAPI() + virconn = conn.open(uri) + + domobj = domainAPI.DomainAPI(virconn) + try: + domobj.set_sched_params_flags(guestname, schedinfo, flags) + logger.debug("set the value of cpu_shares with %s" % cpu_shares) + except LibvirtAPI, e: + logger.error("API error message: %s, error code is %s" % + (e.response()['message'], e.response()['code'])) + return 1 + + check_result = check_cpu_shares(params, util, guestname, cpu_shares, flags) + if check_result: + logger.error("cpu_shares does not match.") + conn.close() + return 1 + + logger.info("success to set scheduler parameters.") + conn.close() + return 0 + +def set_cpu_shares_clean(): + """Clean testing environment""" + pass -- 1.7.4.4

On 12/13/2011 11:45 AM, Nan Zhang wrote:
* repos/domain/set_cpu_shares.py: set the value of cpu_shares property of the guest. --- repos/domain/set_cpu_shares.py | 121 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 121 insertions(+), 0 deletions(-) create mode 100644 repos/domain/set_cpu_shares.py
diff --git a/repos/domain/set_cpu_shares.py b/repos/domain/set_cpu_shares.py new file mode 100644 index 0000000..809ccef --- /dev/null +++ b/repos/domain/set_cpu_shares.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python +"""Set the value of cpu_shares property of the guest + domain:set_cpu_shares + guestname + xxx + cpu_shares + integer value + flags + current|live|config +""" + +__author__ = 'Nan Zhang: nzhang@redhat.com' +__date__ = 'Tue Sep 27, 2011' +__version__ = '0.1.0' +__credits__ = 'Copyright (C) 2011 Red Hat, Inc.' +__all__ = ['check_params', 'check_cpu_shares', 'set_cpu_shares'] + +import os +import re +import sys +import time time module not used. +from xml.dom import minidom + + +def append_path(path): + """Append root path of package""" + if path in sys.path: + pass + else: + sys.path.append(path) + +pwd = os.getcwd() +result = re.search('(.*)libvirt-test-API', pwd) +append_path(result.group(0)) + +from lib import connectAPI +from lib import domainAPI +from utils.Python import utils +from utils.Python import xmlbuilder +from exception import LibvirtAPI + +def check_params(params): + """Verify inputing parameter dictionary""" + logger = params['logger'] + keys = ['guestname', 'flags'] + for key in keys: + if key not in params: + logger.error("%s is required" %key) + return 1 + return 0 + +def check_cpu_shares(params, util, guestname, cpu_shares, flags): + """Check the value of cpu_shares""" + logger = params['logger'] + cmd = "cat /cgroup/cpu/libvirt/qemu/%s/cpu.shares" % guestname + ret, out = util.exec_cmd(cmd, shell=True) + if ret: + logger.error("fail to set the value of cpu_shares: %s" % out[0]) + else: + logger.info("from cgroup, the value of cpu_shares is %s" % out[0]) + + if flags == domainAPI.VIR_DOMAIN_AFFECT_CONFIG: + return 0 + + if cmp(int(out[0]), cpu_shares): + return 1 + else: + logger.info("the value of cpu_shares does match the original \ +cpu scheduler information.") + return 0 + +def set_cpu_shares(params): + """Get the cpu scheduler information""" s/Get/Set in function info + # Initiate and check parameters + params_check_result = check_params(params) + if params_check_result: + return 1 + logger = params['logger'] + guestname = params['guestname'] + schedinfo = {} + schedinfo['cpu_shares'] = int(params['cpu_shares']) + cpu_shares = schedinfo['cpu_shares'] + + if params['flags'] == 'current': + flags = domainAPI.VIR_DOMAIN_AFFECT_CURRENT + elif params['flags'] == 'live': + flags = domainAPI.VIR_DOMAIN_AFFECT_LIVE + elif params['flags'] == 'config': + flags = domainAPI.VIR_DOMAIN_AFFECT_CONFIG + else: + logger.error("Invalid flag was specified.") + return 1 + + # Connect to local hypervisor connection URI + util = utils.Utils() + uri = util.get_uri('127.0.0.1') + conn = connectAPI.ConnectAPI() + virconn = conn.open(uri) + + domobj = domainAPI.DomainAPI(virconn) + try: + domobj.set_sched_params_flags(guestname, schedinfo, flags) + logger.debug("set the value of cpu_shares with %s" % cpu_shares) + except LibvirtAPI, e: + logger.error("API error message: %s, error code is %s" % + (e.response()['message'], e.response()['code'])) + return 1 + + check_result = check_cpu_shares(params, util, guestname, cpu_shares, flags) + if check_result: + logger.error("cpu_shares does not match.") + conn.close() + return 1 + + logger.info("success to set scheduler parameters.") + conn.close() + return 0 + +def set_cpu_shares_clean(): + """Clean testing environment""" + pass Ack, left is fine.

On 12/14/2011 02:59 PM, Wayne Sun wrote:
On 12/13/2011 11:45 AM, Nan Zhang wrote:
* repos/domain/set_cpu_shares.py: set the value of cpu_shares property of the guest. --- repos/domain/set_cpu_shares.py | 121 ++++++++++++++++++++++++++++++++++++++++ 1 files changed, 121 insertions(+), 0 deletions(-) create mode 100644 repos/domain/set_cpu_shares.py
diff --git a/repos/domain/set_cpu_shares.py b/repos/domain/set_cpu_shares.py new file mode 100644 index 0000000..809ccef --- /dev/null +++ b/repos/domain/set_cpu_shares.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python +"""Set the value of cpu_shares property of the guest + domain:set_cpu_shares + guestname + xxx + cpu_shares + integer value + flags + current|live|config +""" + +__author__ = 'Nan Zhang: nzhang@redhat.com' +__date__ = 'Tue Sep 27, 2011' +__version__ = '0.1.0' +__credits__ = 'Copyright (C) 2011 Red Hat, Inc.' +__all__ = ['check_params', 'check_cpu_shares', 'set_cpu_shares'] + +import os +import re +import sys +import time time module not used. +from xml.dom import minidom + + +def append_path(path): + """Append root path of package""" + if path in sys.path: + pass + else: + sys.path.append(path) + +pwd = os.getcwd() +result = re.search('(.*)libvirt-test-API', pwd) +append_path(result.group(0)) + +from lib import connectAPI +from lib import domainAPI +from utils.Python import utils +from utils.Python import xmlbuilder +from exception import LibvirtAPI + +def check_params(params): + """Verify inputing parameter dictionary""" + logger = params['logger'] + keys = ['guestname', 'flags'] + for key in keys: + if key not in params: + logger.error("%s is required" %key) + return 1 + return 0 + +def check_cpu_shares(params, util, guestname, cpu_shares, flags): + """Check the value of cpu_shares""" + logger = params['logger'] + cmd = "cat /cgroup/cpu/libvirt/qemu/%s/cpu.shares" % guestname + ret, out = util.exec_cmd(cmd, shell=True) + if ret: + logger.error("fail to set the value of cpu_shares: %s" % out[0]) + else: + logger.info("from cgroup, the value of cpu_shares is %s" % out[0]) + + if flags == domainAPI.VIR_DOMAIN_AFFECT_CONFIG: + return 0 + + if cmp(int(out[0]), cpu_shares): + return 1 + else: + logger.info("the value of cpu_shares does match the original \ +cpu scheduler information.") + return 0 + +def set_cpu_shares(params): + """Get the cpu scheduler information""" s/Get/Set in function info + # Initiate and check parameters + params_check_result = check_params(params) + if params_check_result: + return 1 + logger = params['logger'] + guestname = params['guestname'] + schedinfo = {} + schedinfo['cpu_shares'] = int(params['cpu_shares']) + cpu_shares = schedinfo['cpu_shares'] + + if params['flags'] == 'current': + flags = domainAPI.VIR_DOMAIN_AFFECT_CURRENT + elif params['flags'] == 'live': + flags = domainAPI.VIR_DOMAIN_AFFECT_LIVE + elif params['flags'] == 'config': + flags = domainAPI.VIR_DOMAIN_AFFECT_CONFIG + else: + logger.error("Invalid flag was specified.") + return 1 + If the guest is running, the VIR_DOMAIN_AFFECT_LIVE only changed on the active domain instance only and is not added to the persisted domain configuration. that means, after rebooting the guest, the original value of cpu_shares come back. VIR_DOMAIN_AFFECT_CONFIG specifies that the device shall be changed on the persisted domain configuration only. that means after you changed the value of cpu_shares, it didn't change until you reboot the domain next time.
So, about the testcase, it is better to check the state of guest first, then according to the flags, give different checking.
+ # Connect to local hypervisor connection URI + util = utils.Utils() + uri = util.get_uri('127.0.0.1') + conn = connectAPI.ConnectAPI() + virconn = conn.open(uri) + + domobj = domainAPI.DomainAPI(virconn) + try: + domobj.set_sched_params_flags(guestname, schedinfo, flags) + logger.debug("set the value of cpu_shares with %s" % cpu_shares) + except LibvirtAPI, e: + logger.error("API error message: %s, error code is %s" % + (e.response()['message'], e.response()['code']))
add conn.close()
+ return 1 + + check_result = check_cpu_shares(params, util, guestname, cpu_shares, flags) + if check_result: + logger.error("cpu_shares does not match.") + conn.close() + return 1 + + logger.info("success to set scheduler parameters.") + conn.close() + return 0 + +def set_cpu_shares_clean(): + """Clean testing environment""" + pass Ack, left is fine.
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list

On 12/13/2011 11:45 AM, Nan Zhang wrote:
* repos/domain/get_cpu_shares.py: get the value of cpu_shares property of the guest. --- lib/domainAPI.py | 2 +- repos/domain/get_cpu_shares.py | 117 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletions(-) create mode 100644 repos/domain/get_cpu_shares.py
diff --git a/lib/domainAPI.py b/lib/domainAPI.py index a6efab7..0058254 100644 --- a/lib/domainAPI.py +++ b/lib/domainAPI.py @@ -546,7 +546,7 @@ class DomainAPI(object): def set_sched_params_flags(self, domname, params, flags): try: dom_obj = self.get_domain_by_name(domname) - retval = dom_obj.setSchedulerParameters(params, flags) + retval = dom_obj.setSchedulerParametersFlags(params, flags) return retval except libvirt.libvirtError, e: message = e.get_error_message() diff --git a/repos/domain/get_cpu_shares.py b/repos/domain/get_cpu_shares.py new file mode 100644 index 0000000..5d26e82 --- /dev/null +++ b/repos/domain/get_cpu_shares.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +"""Get the value of cpu_shares property of the guest + domain:get_cpu_shares + guestname + xxx + flags + current|live|config +""" + +__author__ = 'Nan Zhang: nzhang@redhat.com' +__date__ = 'Tue Sep 27, 2011' +__version__ = '0.1.0' +__credits__ = 'Copyright (C) 2011 Red Hat, Inc.' +__all__ = ['check_params', 'check_cpu_shares', 'get_cpu_shares'] + +import os +import re +import sys +import time time module is not used +from xml.dom import minidom + + +def append_path(path): + """Append root path of package""" + if path in sys.path: + pass + else: + sys.path.append(path) + +pwd = os.getcwd() +result = re.search('(.*)libvirt-test-API', pwd) +append_path(result.group(0)) + +from lib import connectAPI +from lib import domainAPI +from utils.Python import utils +from utils.Python import xmlbuilder +from exception import LibvirtAPI + +def check_params(params): + """Verify inputing parameter dictionary""" + logger = params['logger'] + keys = ['guestname', 'flags'] + for key in keys: + if key not in params: + logger.error("%s is required" %key) + return 1 + return 0 + +def check_cpu_shares(params, util, guestname, cpu_shares, flags): + """Check the value of cpu_shares""" + logger = params['logger'] + cmd = "cat /cgroup/cpu/libvirt/qemu/%s/cpu.shares" % guestname + ret, out = util.exec_cmd(cmd, shell=True) + if ret: + logger.error("fail to get the value of cpu_shares: %s" % out[0]) + else: + logger.info("from cgroup, the value of cpu_shares: %s" % out[0]) + + if flags == domainAPI.VIR_DOMAIN_AFFECT_CONFIG: + return 0 + + if cmp(int(out[0]), cpu_shares): + return 1 + else: + logger.info("the value of cpu_shares does match the original \ +cpu scheduler information.") + return 0 + +def get_cpu_shares(params): + """Get the cpu scheduler information""" + # Initiate and check parameters + params_check_result = check_params(params) + if params_check_result: + return 1 + logger = params['logger'] + guestname = params['guestname'] + + if params['flags'] == 'current': + flags = domainAPI.VIR_DOMAIN_AFFECT_CURRENT + elif params['flags'] == 'live': + flags = domainAPI.VIR_DOMAIN_AFFECT_LIVE + elif params['flags'] == 'config': + flags = domainAPI.VIR_DOMAIN_AFFECT_CONFIG + else: + logger.error("Invalid flag was specified.") + return 1 + + # Connect to local hypervisor connection URI + util = utils.Utils() + uri = util.get_uri('127.0.0.1') + conn = connectAPI.ConnectAPI() + virconn = conn.open(uri) + + domobj = domainAPI.DomainAPI(virconn) + try: + sched_info = domobj.get_sched_params_flags(guestname, flags) + cpu_shares = sched_info['cpu_shares'] + logger.debug("the value of cpu_shares is %s" % cpu_shares) + except LibvirtAPI, e: + logger.error("API error message: %s, error code is %s" % + (e.response()['message'], e.response()['code'])) + return 1 + + check_result = check_cpu_shares(params, util, guestname, cpu_shares, flags) + if check_result: + logger.error("cpu_shares does not match.") + conn.close() + return 1 + + logger.info("success to get scheduler parameters.") + conn.close() + return 0 + +def get_cpu_shares_clean(): + """Clean testing environment""" + pass Ack, left is fine.

On 12/14/2011 02:59 PM, Wayne Sun wrote:
On 12/13/2011 11:45 AM, Nan Zhang wrote:
* repos/domain/get_cpu_shares.py: get the value of cpu_shares property of the guest. --- lib/domainAPI.py | 2 +- repos/domain/get_cpu_shares.py | 117 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletions(-) create mode 100644 repos/domain/get_cpu_shares.py
diff --git a/lib/domainAPI.py b/lib/domainAPI.py index a6efab7..0058254 100644 --- a/lib/domainAPI.py +++ b/lib/domainAPI.py @@ -546,7 +546,7 @@ class DomainAPI(object): def set_sched_params_flags(self, domname, params, flags): try: dom_obj = self.get_domain_by_name(domname) - retval = dom_obj.setSchedulerParameters(params, flags) + retval = dom_obj.setSchedulerParametersFlags(params, flags) return retval except libvirt.libvirtError, e: message = e.get_error_message() diff --git a/repos/domain/get_cpu_shares.py b/repos/domain/get_cpu_shares.py new file mode 100644 index 0000000..5d26e82 --- /dev/null +++ b/repos/domain/get_cpu_shares.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +"""Get the value of cpu_shares property of the guest + domain:get_cpu_shares + guestname + xxx + flags + current|live|config +""" + +__author__ = 'Nan Zhang: nzhang@redhat.com' +__date__ = 'Tue Sep 27, 2011' +__version__ = '0.1.0' +__credits__ = 'Copyright (C) 2011 Red Hat, Inc.' +__all__ = ['check_params', 'check_cpu_shares', 'get_cpu_shares'] + +import os +import re +import sys +import time time module is not used +from xml.dom import minidom + + +def append_path(path): + """Append root path of package""" + if path in sys.path: + pass + else: + sys.path.append(path) + +pwd = os.getcwd() +result = re.search('(.*)libvirt-test-API', pwd) +append_path(result.group(0)) + +from lib import connectAPI +from lib import domainAPI +from utils.Python import utils +from utils.Python import xmlbuilder +from exception import LibvirtAPI + +def check_params(params): + """Verify inputing parameter dictionary""" + logger = params['logger'] + keys = ['guestname', 'flags'] + for key in keys: + if key not in params: + logger.error("%s is required" %key) + return 1 + return 0 + +def check_cpu_shares(params, util, guestname, cpu_shares, flags): + """Check the value of cpu_shares""" + logger = params['logger'] + cmd = "cat /cgroup/cpu/libvirt/qemu/%s/cpu.shares" % guestname + ret, out = util.exec_cmd(cmd, shell=True) + if ret: + logger.error("fail to get the value of cpu_shares: %s" % out[0]) + else: + logger.info("from cgroup, the value of cpu_shares: %s" % out[0]) + + if flags == domainAPI.VIR_DOMAIN_AFFECT_CONFIG: + return 0 + + if cmp(int(out[0]), cpu_shares): + return 1 + else: + logger.info("the value of cpu_shares does match the original \ +cpu scheduler information.") + return 0 + +def get_cpu_shares(params): + """Get the cpu scheduler information""" + # Initiate and check parameters + params_check_result = check_params(params) + if params_check_result: + return 1 + logger = params['logger'] + guestname = params['guestname'] +
I think we need to do the check to the state of guestname before doing the test. The test is related to the state of guest as the flags said: VIR_DOMAIN_AFFECT_CURRENT = 0: Affect current domain state. VIR_DOMAIN_AFFECT_LIVE = 1: Affect running domain state. VIR_DOMAIN_AFFECT_CONFIG = 2: Affect persistent domain state When the guest is inactive, running the scripts get the following result: " File "/libvirt-test-API/repos/domain/get_cpu_shares.py", line 56, in check_cpu_shares logger.error("fail to get the value of cpu_shares: %s" % out[0]) IndexError: list index out of range " " File "/libvirt-test-API/repos/domain/get_cpu_shares.py", line 98, in get_cpu_shares cpu_shares = sched_info['cpu_shares'] TypeError: 'NoneType' object is unsubscriptable "
+ if params['flags'] == 'current': + flags = domainAPI.VIR_DOMAIN_AFFECT_CURRENT + elif params['flags'] == 'live': + flags = domainAPI.VIR_DOMAIN_AFFECT_LIVE + elif params['flags'] == 'config': + flags = domainAPI.VIR_DOMAIN_AFFECT_CONFIG + else: + logger.error("Invalid flag was specified.") + return 1 + + # Connect to local hypervisor connection URI + util = utils.Utils() + uri = util.get_uri('127.0.0.1') + conn = connectAPI.ConnectAPI() + virconn = conn.open(uri) + + domobj = domainAPI.DomainAPI(virconn) + try: + sched_info = domobj.get_sched_params_flags(guestname, flags) + cpu_shares = sched_info['cpu_shares'] + logger.debug("the value of cpu_shares is %s" % cpu_shares) + except LibvirtAPI, e: + logger.error("API error message: %s, error code is %s" % + (e.response()['message'], e.response()['code']))
add conn.close();
+ return 1 + + check_result = check_cpu_shares(params, util, guestname, cpu_shares, flags) + if check_result: + logger.error("cpu_shares does not match.") + conn.close() + return 1 + + logger.info("success to get scheduler parameters.") + conn.close() + return 0 + +def get_cpu_shares_clean(): + """Clean testing environment""" + pass Ack, left is fine.
-- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
participants (4)
-
Guannan Ren
-
Nan Zhang
-
Osier Yang
-
Wayne Sun