dist/redhat/env_inspect.py: initialize shared connection object for use in all testcases generator.py: make use of sharedmod --- dist/redhat/env_inspect.py | 192 ++++++++++++++++++++++++-------------------- generator.py | 15 +--- 2 files changed, 108 insertions(+), 99 deletions(-) diff --git a/dist/redhat/env_inspect.py b/dist/redhat/env_inspect.py index 8cff106..3e8da2c 100644 --- a/dist/redhat/env_inspect.py +++ b/dist/redhat/env_inspect.py @@ -19,104 +19,120 @@ # from each testcase to the corresponding testcase's # argument in the order of testcase running -import subprocess - -def childprocess(pcmd, acmd): - P1 = subprocess.Popen(pcmd, stdout = subprocess.PIPE) - P2 = subprocess.Popen(acmd, stdin = P1.stdout, stdout =subprocess.PIPE) - out = P2.communicate()[0].strip() - rc = P2.returncode - - if rc == 0: - return out - else: - return "" - -def get_libvirt_ver(): - ver = childprocess(['rpm', '-qa'], ['egrep', "^libvirt-[0-9]"]) - if ver == "": - return 100, "No libvirt installed" - else: - return 0, ver - -def get_libvirt_pyth_ver(): - ver = childprocess(['rpm', '-qa'], ['egrep', "^libvirt-python-[0-9]"]) - if ver == "": - return 100, "No libvirt-python installed" +import commands +import libvirt +import sharedmod + +def check_libvirt(logger): + virsh = 'virsh -v' + status, output = commands.getstatusoutput(virsh) + if status: + logger.error(output) + return 1 else: - return 0, ver - -def get_libvirt_cli_ver(): - ver = childprocess(['rpm', '-qa'], ['egrep', "^libvirt-client-[0-9]"]) - if ver == "": - return 100, "No libvirt-client installed" - else: - return 0, ver - -def get_qemu_kvm_ver(): - ver = childprocess(['rpm', '-qa'], ['egrep', "qemu-kvm-[0-9]"]) - if ver == "": - return 150, "No qemu-kvm installed" + logger.info(" Virsh command line tool of libvirt: %s" % output) + + libvirtd = 'libvirtd --version' + status, output = commands.getstatusoutput(libvirtd) + logger.info(" %s" % output) + if status: + return 1 + + default_uri = 'virsh uri' + status, output = commands.getstatusoutput(default_uri) + if status: + logger.error(output) + return 1 else: - return 0, ver + logger.info(" Default URI: %s" % output.strip()) + + if 'qemu' in output: + qemu = 'qemu --version' + status, output = commands.getstatusoutput(qemu) + logger.info(" %s" % output) + if status: + return 1 + elif 'xen' in output: + #TODO need to get xen hypervisor info here + pass + + return 0 + +def hostinfo(logger): + command = 'uname -a' + status, output = commands.getstatusoutput(command) + logger.info(" %s" % output) + if status: + return 1 + return 0 + +def request_credentials(credentials, user_data): + for credential in credentials: + if credential[0] == libvirt.VIR_CRED_AUTHNAME: + credential[4] = user_data[0] + + if len(credential[4]) == 0: + credential[4] = credential[3] + elif credential[0] == libvirt.VIR_CRED_PASSPHRASE: + credential[4] = user_data[1] + else: + return -1 -def get_kernel_ver(): - ver = childprocess(['rpm', '-qa'], ['egrep', "^kernel-[0-9]"]) - if ver == "": - return 100, "No kernel installed" - else: - return 0, ver + return 0 +def sharemod_init(env_parser, logger): + """ get connection object from libvirt module + initialize sharemod for use by testcases + """ + uri = env_parser.get_value('variables', 'defaulturi') + username = env_parser.get_value('variables', 'username') + password = env_parser.get_value('variables', 'password') + user_data = [username, password] + auth = [[libvirt.VIR_CRED_AUTHNAME, libvirt.VIR_CRED_PASSPHRASE], request_credentials, user_data] + conn = libvirt.openAuth(uri, auth, 0) + if not conn: + logger.error("Failed to setup libvirt connection"); + return 1 + + # initialize conn object in sharedmod + sharedmod.libvirtobj.clear() + sharedmod.data.clear() + sharedmod.libvirtobj['conn'] = conn + return 0 class EnvInspect(object): """to check and collect the testing enviroment infomation before performing testing """ - def __init__(self, logger): + def __init__(self, env_parser, logger): self.logger = logger + self.env_parser = env_parser def env_checking(self): - flag = 0 - result = "" - if get_libvirt_ver()[0] == 100: - result = NOTOK - flag = 1 - else: - result = OK - self.logger.info(" %-36s%-6s" % (get_libvirt_ver()[1], result)) - - if get_libvirt_pyth_ver()[0] == 100: - result = NOTOK - flag = 1 - else: - result = OK - self.logger.info(" %-36s%-6s" % (get_libvirt_pyth_ver()[1], result)) - - if get_libvirt_cli_ver()[0] == 100: - result = NOTOK - flag = 1 - else: - result = OK - self.logger.info(" %-36s%-6s" % (get_libvirt_cli_ver()[1], result)) - - if get_qemu_kvm_ver()[0] == 150 and flag == 0: - flag = 0 - elif get_qemu_kvm_ver()[0] == 150 and flag == 1: - flag = 1 - else: - pass - self.logger.info(" %-36s%-6s" % (get_qemu_kvm_ver()[1], OK)) - - if get_kernel_ver()[0] == 100: - result = NOTOK - flag = 1 - else: - result = OK - self.logger.info(" %-36s%-6s" % (get_kernel_ver()[1], result)) - - return flag - - -OK = "ok" -NOTOK = "not ok" + if hostinfo(self.logger): + return 1 + + if check_libvirt(self.logger): + return 1 + + if sharemod_init(self.env_parser, self.logger): + return 1 + + return 0 + + def close_hypervisor_connection(self): + conn = sharedmod.libvirtobj.get('conn', None) + if conn: + # conn probably is invalid pointer + # that means the connection is closed + # If so we ignore the error here + try: + conn.close() + conn = None + except: + pass + + sharedmod.libvirtobj.clear() + sharedmod.data.clear() + return 0 diff --git a/generator.py b/generator.py index 0af4227..d7cd985 100644 --- a/generator.py +++ b/generator.py @@ -20,6 +20,7 @@ import time import fcntl import sys +import os import traceback import mapper @@ -30,7 +31,6 @@ from utils.Python import env_parser # Import of distribution-specific code. If this is needed somewhere # else in the future, please don't copy-paste this, but create some # sensible distribution-specific package -import os for dist in os.listdir('dist'): if os.path.exists('/etc/%s-release' % dist): exec('from dist.%s import env_inspect' % dist) @@ -119,7 +119,7 @@ class FuncGen(object): start_time = time.strftime("%Y-%m-%d %H:%M:%S") env_logger.info("Checking Testing Environment... ") - envck = env_inspect.EnvInspect(env_logger) + envck = env_inspect.EnvInspect(self.env, env_logger) if envck.env_checking() == 1: sys.exit(1) @@ -157,15 +157,6 @@ class FuncGen(object): clean_ret = -1 try: try: - # Get default uri from env.cfg, if the uri is defined in - # case config file, the value will be overrode. - if 'uri' not in case_params: - case_params['uri'] = self.env.get_value("variables", "defaulturi") - if 'username' not in case_params: - case_params['username'] = self.env.get_value("variables", "username") - if 'password' not in case_params: - case_params['password'] = self.env.get_value("variables", "password") - if case_ref_name != 'sleep': case_params['logger'] = case_logger @@ -211,6 +202,8 @@ class FuncGen(object): else: self.fmt.print_string(21*" " + "Fail", env_logger) + # close hypervisor connection + envck.close_hypervisor_connection() end_time = time.strftime("%Y-%m-%d %H:%M:%S") env_logger.info("\nSummary:") -- 1.7.7.5 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list