[libvirt] [libvirt-test-API][PATCH V4 0/4] Add test case for virconnect V4

V3->V4: Remove getSysinfo() check for lxc connection. Fix minor problems. V2->V3: Refactor connection_nodeinfo. Change the way of getting version number. V1->V2: Seperate check functions in each test case. Improve log message. V1: Add test case for virconnect Add test case for nodeinfo of virconnect Add connection_version test case Add conf file of virconnect test jmiao (4): Add test case for virConnect Add connection_nodeinfo test case Add connection_version test case Add test_connection.conf cases/test_connection.conf | 31 +++++++ repos/virconn/__init__.py | 0 repos/virconn/connection_attributes.py | 92 +++++++++++++++++++++ repos/virconn/connection_nodeinfo.py | 146 +++++++++++++++++++++++++++++++++ repos/virconn/connection_version.py | 119 +++++++++++++++++++++++++++ 5 files changed, 388 insertions(+) create mode 100644 cases/test_connection.conf create mode 100644 repos/virconn/__init__.py create mode 100644 repos/virconn/connection_attributes.py create mode 100644 repos/virconn/connection_nodeinfo.py create mode 100644 repos/virconn/connection_version.py -- 1.8.3.1

From: jmiao <jmiao@redhat.com> Add a connect_attributes.py for testing API: isAlive, getType, getURI, getHostname, getSysinfo, getCapabilities. Signed-off-by: jmiao <jmiao@redhat.com> --- repos/virconn/__init__.py | 0 repos/virconn/connection_attributes.py | 92 ++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 repos/virconn/__init__.py create mode 100644 repos/virconn/connection_attributes.py diff --git a/repos/virconn/__init__.py b/repos/virconn/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/repos/virconn/connection_attributes.py b/repos/virconn/connection_attributes.py new file mode 100644 index 0000000..6fd702b --- /dev/null +++ b/repos/virconn/connection_attributes.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# test libvirt connection attributes + +import libvirt +from libvirt import libvirtError + +from src import sharedmod +from utils import utils + +required_params = () +optional_params = {'conn': ''} + + +def check_conn_type(conn, logger): + """check connection's type + """ + logger.info("Test connection's type") + uri_type = conn.getType() + logger.info("The connection's type is %s" % uri_type) + uri_str = conn.getURI() + logger.info("The connection's URI is %s" % uri_str) + # because remote:// is redirect to the default hypervisor URI + if 'remote' in uri_str: + logger.info("Ignore remote:// URI testing") + else: + if str.lower(uri_type) not in str.lower(uri_str): + logger.error("The connection %s has wrong type: %s" % + (uri_str, uri_type)) + return False + return True + + +def check_conn_hostname(conn, logger): + """check connection's hostname + """ + logger.info("Test connection's hostname") + output = utils.get_local_hostname() + conn_hostname = conn.getHostname() + logger.info("The connection's hostname is %s" % conn_hostname) + + if not conn_hostname == output: + logger.error("The connection's hostname(%s) wrong, should be %s" % + (conn_hostname, output)) + return False + return True + + +def connection_attributes(params): + """test libvirt connection attributes + """ + logger = params['logger'] + + try: + # get connection firstly. + # If conn is not specified, use conn from sharedmod + if 'conn' in params: + conn = libvirt.open(params['conn']) + else: + conn = sharedmod.libvirtobj['conn'] + + # test connection is Alive + if not conn.isAlive(): + logger.error("The connection is not alive") + return 1 + + # test connection type + if not check_conn_type(conn, logger): + logger.error("Failed to check connection type") + return 1 + + # test hostname of the connection + if not check_conn_hostname(conn, logger): + logger.error("Failed to check connection hostname") + return 1 + + # test connection's sysinfo + logger.info("Test connection's sysinfo") + conn_sysinfo = conn.getSysinfo(0) + logger.info("The connection's sysinfo is:\n %s" % conn_sysinfo) + + # test connection's capabilities + logger.info("Test connection's capabilities") + conn_caps = conn.getCapabilities() + logger.info("The connection's capabilities is:\n %s" % conn_caps) + + except libvirtError, e: + logger.error("API error message: %s, error code is %s" % + e.message) + logger.error("start failed") + return 1 + + return 0 -- 1.8.3.1

From: jmiao <jmiao@redhat.com> The connection_nodeinfo.py test the node infomation the virConnect saved, like: arch, memory, cpus. Signed-off-by: jmiao <jmiao@redhat.com> --- repos/virconn/connection_nodeinfo.py | 146 +++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 repos/virconn/connection_nodeinfo.py diff --git a/repos/virconn/connection_nodeinfo.py b/repos/virconn/connection_nodeinfo.py new file mode 100644 index 0000000..85fb5cb --- /dev/null +++ b/repos/virconn/connection_nodeinfo.py @@ -0,0 +1,146 @@ +#!/usr/bin/env python +# test libvirt connection node infomation + +import libvirt +from libvirt import libvirtError + +from src import sharedmod +from utils import utils + +required_params = () +optional_params = {'conn': ''} + + +def get_model(logger): + """get nodeinfo model + """ + output = utils.get_host_arch() + logger.info("model is %s" % output) + return output + + +def get_memory(logger): + """get nodeinfo memory + """ + output = utils.get_host_memory() + # the memory value python API returned is devided by 1024 + mem_cap = int(output) / 1024 + logger.info("memory is %s MiB" % mem_cap) + return mem_cap + + +def get_cpus(logger): + """get nodeinfo cpus + """ + output = utils.get_host_cpus() + logger.info("cpus is %s" % output) + return output + + +def get_cpu_mhz(logger): + """get nodeinfo cpu_mhz + """ + output = utils.get_host_frequency() + cpu_mhz = int(output.split('.')[0]) + logger.info("cpu MHz is %d" % cpu_mhz) + return cpu_mhz + + +def get_nodes(logger): + """get nodeinfo nodes + """ + cmds = "lscpu | grep 'node(s)' | awk {'print $3'}" + (status, output) = utils.exec_cmd(cmds, shell=True) + if status != 0: + logger.error("Exec_cmd failed: %s" % cmds) + return "" + logger.info("nodes is %s" % output[0]) + return int(output[0]) + + +def get_sockets(logger): + """get nodeinfo sockets + """ + cmds = "lscpu | grep 'Socket(s)' | awk {'print $2'}" + (status, output) = utils.exec_cmd(cmds, shell=True) + if status != 0: + logger.error("Exec_cmd failed: %s" % cmds) + return "" + logger.info("cpu sockets is %s" % output[0]) + return int(output[0]) + + +def get_cores(logger): + """get nodeinfo cores + """ + cmds = "lscpu | grep 'Core(s)' | awk {'print $4'}" + (status, output) = utils.exec_cmd(cmds, shell=True) + if status != 0: + logger.error("Exec_cmd failed: %s" % cmds) + return "" + logger.info("cpu cores is %s" % output[0]) + return int(output[0]) + + +def get_threads(logger): + """get nodeinfo threads + """ + cmds = "lscpu | grep 'Thread(s)' | awk {'print $4'}" + (status, output) = utils.exec_cmd(cmds, shell=True) + if status != 0: + logger.error("Exec_cmd failed: %s" % cmds) + return "" + logger.info("cpu threads is %s" % output[0]) + return int(output[0]) + + +def check_conn_nodeinfo(conn_nodeinfo, logger): + """check each nodeinfo value + """ + ret = True + # python2.6 does not have ordered dict, so that enumerate + # could not get the defined sequence which API getInfo() returned. + res_types = ['model', 'memory', 'cpus', 'cpu_mhz', 'nodes', + 'sockets', 'cores', 'threads'] + get_res_func = [get_model, get_memory, get_cpus, get_cpu_mhz, + get_nodes, get_sockets, get_cores, get_threads] + + for idx, res in enumerate(zip(res_types, get_res_func)): + logger.debug("Checking %s" % res[0]) + logger.debug("Executing %s" % res[1]) + get_res = res[1](logger) + if get_res != conn_nodeinfo[idx]: + logger.error("Failed to check nodeinfo's %s" % res[0]) + logger.error("%s %s is wrong, should be %s" % + (res, conn_nodeinfo[idx], get_res)) + ret = False + + return ret + + +def connection_nodeinfo(params): + """test libvirt connection node infomation + """ + logger = params['logger'] + + try: + # get connection firstly. + # If conn is not specified, use conn from sharedmod + if 'conn' in params: + conn = libvirt.open(params['conn']) + else: + conn = sharedmod.libvirtobj['conn'] + + logger.info("get connection node infomation") + conn_nodeinfo = conn.getInfo() + logger.info("connection node infomation is %s" % conn_nodeinfo) + + check_conn_nodeinfo(conn_nodeinfo, logger) + + except libvirtError, e: + logger.error("API error message: %s, error code is %s" % + e.message) + logger.error("start failed") + return 1 + + return 0 -- 1.8.3.1

From: jmiao <jmiao@redhat.com> The connection_version.py test the version of libvirt and hypervisor the virConnect saved. Currently, hypervisor checking only support qemu and lxc, need expanding more hypervisors in furture. Signed-off-by: jmiao <jmiao@redhat.com> --- repos/virconn/connection_version.py | 119 ++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 repos/virconn/connection_version.py diff --git a/repos/virconn/connection_version.py b/repos/virconn/connection_version.py new file mode 100644 index 0000000..3aee44f --- /dev/null +++ b/repos/virconn/connection_version.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python +# test libvirt connection version + +import libvirt +from libvirt import libvirtError + +from src import sharedmod +from utils import utils + +required_params = () +optional_params = {'conn': ''} + + +def produce_ver_num(major, minor, release): + """produce the version number + """ + num = major * 1000000 + minor * 1000 + release + return num + + +def check_libvirt_ver_num(conn, logger): + """check libvirt version number + """ + libvirt_version = utils.get_libvirt_version() + logger.info("libvirt version is %s" % libvirt_version) + ver = libvirt_version.split('-')[1] + x = int(ver.split('.')[0]) + y = int(ver.split('.')[1]) + z = int(ver.split('.')[2]) + libvirt_ver_num = produce_ver_num(x, y, z) + + conn_lib_ver = conn.getLibVersion() + logger.info("get libvirt version from connection: %s" % conn_lib_ver) + + if conn_lib_ver != libvirt_ver_num: + logger.error("libvirt version is wrong, should be %s" % + libvirt_ver_num) + return False + return True + + +def check_hypervisor_ver_num(conn, logger): + """check hypervisor version number + """ + # TODO: modify utils.get_hypervisor, support lxc, openvz, and so on + conn_type = conn.getType() + logger.info("connection's type is %s" % conn_type) + + if str.lower(conn_type) == 'qemu': + cmds = "rpm -q qemu-kvm" + ver_num_pos = 2 + (status, output) = utils.exec_cmd(cmds, shell=True) + if status != 0: + cmds = "rpm -q qemu-kvm-rhev" + ver_num_pos = 3 + (status, output) = utils.exec_cmd(cmds, shell=True) + if status != 0: + logger.error("Could not be aware of qemu") + return False + hyper_version = output[0] + ver = hyper_version.split('-')[ver_num_pos] + x = int(ver.split('.')[0]) + y = int(ver.split('.')[1]) + z = int(ver.split('.')[2]) + elif str.lower(conn_type) == 'lxc': + cmds = "uname -r" + (status, output) = utils.exec_cmd(cmds, shell=True) + if status != 0: + logger.error("Exec_cmd failed: %s" % cmds) + return False + hyper_version = output[0] + ver = hyper_version.split('-')[0] + x = int(ver.split('.')[0]) + y = int(ver.split('.')[1]) + z = int(ver.split('.')[2]) + else: + logger.error("This hypervisor %s is unsupported currently" % conn_type) + return False + + hyper_ver_num = produce_ver_num(x, y, z) + + conn_hyper_ver = conn.getVersion() + logger.info("get hypervisor version from connection: %s" % conn_hyper_ver) + if conn_hyper_ver != hyper_ver_num: + logger.error("libvirt version is wrong, should be %s" % hyper_ver_num) + return False + return True + + +def connection_version(params): + """test libvirt connection version + """ + logger = params['logger'] + + try: + # get connection firstly. + # If conn is not specified, use conn from sharedmod + if 'conn' in params: + conn = libvirt.open(params['conn']) + else: + conn = sharedmod.libvirtobj['conn'] + + # check libvirt version number + if not check_libvirt_ver_num(conn, logger): + logger.error("Failed to check libvirt version number") + return 1 + + # check hypervisor version number + if not check_hypervisor_ver_num(conn, logger): + logger.error("Failed to check hypervisor version number") + return 1 + + except libvirtError, e: + logger.error("API error message: %s, error code is %s" % + e.message) + logger.error("start failed") + return 1 + + return 0 -- 1.8.3.1

From: jmiao <jmiao@redhat.com> Signed-off-by: jmiao <jmiao@redhat.com> --- cases/test_connection.conf | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cases/test_connection.conf diff --git a/cases/test_connection.conf b/cases/test_connection.conf new file mode 100644 index 0000000..ccde119 --- /dev/null +++ b/cases/test_connection.conf @@ -0,0 +1,31 @@ +virconn:connection_nodeinfo + conn + qemu:///system + +virconn:connection_attributes + conn + qemu:///system + +virconn:connection_version + conn + qemu:///system + +virconn:connection_nodeinfo + conn + qemu:///session + +virconn:connection_attributes + conn + qemu:///session + +virconn:connection_version + conn + qemu:///session + +virconn:connection_nodeinfo + conn + lxc:/// + +virconn:connection_version + conn + lxc:/// -- 1.8.3.1

ACK and pushed Thanks On 04/25/2014 04:55 PM, Jincheng Miao wrote:
V3->V4: Remove getSysinfo() check for lxc connection. Fix minor problems.
V2->V3: Refactor connection_nodeinfo. Change the way of getting version number.
V1->V2: Seperate check functions in each test case. Improve log message.
V1: Add test case for virconnect Add test case for nodeinfo of virconnect Add connection_version test case Add conf file of virconnect test
jmiao (4): Add test case for virConnect Add connection_nodeinfo test case Add connection_version test case Add test_connection.conf
cases/test_connection.conf | 31 +++++++ repos/virconn/__init__.py | 0 repos/virconn/connection_attributes.py | 92 +++++++++++++++++++++ repos/virconn/connection_nodeinfo.py | 146 +++++++++++++++++++++++++++++++++ repos/virconn/connection_version.py | 119 +++++++++++++++++++++++++++ 5 files changed, 388 insertions(+) create mode 100644 cases/test_connection.conf create mode 100644 repos/virconn/__init__.py create mode 100644 repos/virconn/connection_attributes.py create mode 100644 repos/virconn/connection_nodeinfo.py create mode 100644 repos/virconn/connection_version.py
participants (2)
-
hongming
-
Jincheng Miao