[libvirt] [test-API][PATCH] Add libvirtd restart test case

Alex Jia ajia at redhat.com
Fri Sep 2 08:06:13 UTC 2011


On 09/01/2011 07:01 PM, Wayne Sun wrote:
>    * libvirtd restart should not affect the running domains. This test
>      check the libvirtd status before and after libvirtd restart, and
>      also by checking the domain pid to confirm the domain is not
>      affected.
> ---
>   repos/libvirtd/restart.py |  143 +++++++++++++++++++++++++++++++++++++++++++++
>   1 files changed, 143 insertions(+), 0 deletions(-)
>   create mode 100644 repos/libvirtd/restart.py
>
> diff --git a/repos/libvirtd/restart.py b/repos/libvirtd/restart.py
> new file mode 100644
> index 0000000..15dd43c
> --- /dev/null
> +++ b/repos/libvirtd/restart.py
> @@ -0,0 +1,143 @@
> +#!/usr/bin/evn python
> +""" Restart libvirtd testing. A running guest is required in this test.
> +    During libvirtd restart, the guest remains running and not affected
> +    by libvirtd restart.
> +    libvirtd:restart
> +        guestname
> +            #GUESTNAME#
> +"""
> +
> +__author__ = 'Wayne Sun: gsun at redhat.com'
> +__date__ = 'Thu Aug 4, 2011'
> +__version__ = '0.1.0'
> +__credits__ = 'Copyright (C) 2011 Red Hat, Inc.'
> +__all__ = ['restart']
> +
> +import os
> +import re
> +import sys
> +import time
Unused import time.
> +
> +def append_path(path):
> +    """Append root path of package"""
> +    if path not in sys.path:
> +        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
> +
> +VIRSH_LIST = "virsh list --all"
> +RESTART_CMD = "service libvirtd restart"
> +
> +def check_params(params):
> +    """Verify inputing parameter dictionary"""
> +    logger = params['logger']
> +    keys = ['guestname']
> +    for key in keys:
> +        if key not in params:
> +            logger.error("%s is required" %key)
> +            return 1
> +    return 0
> +
> +def libvirtd_check(util, logger):
> +    """check libvirtd status
> +    """
> +    cmd = "service libvirtd status"
> +    ret, out = util.exec_cmd(cmd, shell=True)
> +    if ret != 0:
> +        logger.error("failed to get libvirtd status")
> +        return 1
> +    else:
> +        logger.info(out[0])
> +
> +    logger.info(VIRSH_LIST)
> +    ret, out = util.exec_cmd(VIRSH_LIST, shell=True)
> +    if ret != 0:
> +        logger.error("failed to get virsh list result")
> +        return 1
> +    else:
> +        for i in range(len(out)):
> +            logger.info(out[i])
> +
> +    return 0
> +
> +def get_domain_pid(util, logger, guestname):
> +    """get the pid of running domain"""
> +    logger.info("get the pid of running domain %s"  % guestname)
> +    get_pid_cmd = "cat /var/run/libvirt/qemu/%s.pid" % guestname
> +    ret, pid = util.exec_cmd(get_pid_cmd, shell=True)
> +    if ret:
> +        logger.error("fail to get the pid of runnings domain %s" % \
> +                     guestname)
> +        return 1, ""
> +    else:
> +        logger.info("the pid of domain %s is %s" % \
> +                    (guestname, pid[0]))
> +        return 0, pid[0]
> +
> +def restart(params):
> +    """restart libvirtd test"""
> +    # Initiate and check parameters
> +    params_check_result = check_params(params)
> +    if params_check_result:
> +        return 1
> +
> +    logger = params['logger']
> +    guestname = params['guestname']
> +    util = utils.Utils()
> +    uri = util.get_uri('127.0.0.1')
> +
> +    conn = connectAPI.ConnectAPI()
> +    virconn = conn.open(uri)
> +    domobj = domainAPI.DomainAPI(virconn)
> +    state = domobj.get_state(guestname)
> +    conn.close()
> +
> +    if(state == "shutoff"):
> +        logger.info("guest is shutoff, if u want to run this case, \
s/info/error/
> +                     guest must be running")
> +        return 1
> +
> +    logger.info("check the libvirtd status:")
> +    result = libvirtd_check(util, logger)
Redefining name 'result' from outer scope, the previous has a global 
'result' definition:

result = re.search('(.*)libvirt-test-API', pwd)

So it had better to change a new variable name such as ret/rv etc.

Alex
> +    if result:
> +        return 1
> +
> +    ret, pid_before = get_domain_pid(util, logger, guestname)
> +    if ret:
> +        return 1
> +
> +    logger.info("restart libvirtd service:")
> +    ret, out = util.exec_cmd(RESTART_CMD, shell=True)
> +    if ret != 0:
> +        logger.error("failed to restart libvirtd")
> +        for i in range(len(out)):
> +            logger.error(out[i])
> +        return 1
> +    else:
> +        for i in range(len(out)):
> +            logger.info(out[i])
> +
> +    logger.info("recheck libvirtd status:")
> +    result = libvirtd_check(util, logger)
> +    if result:
> +        return 1
> +
> +    ret, pid_after = get_domain_pid(util, logger, guestname)
> +    if ret:
> +        return 1
> +
> +    if pid_before != pid_after:
> +        logger.error("%s pid changed during libvirtd restart" % \
> +                     guestname)
> +        return 1
> +    else:
> +        logger.info("domain pid not change, %s keeps running during \
> +                     libvirtd restart" % guestname)
> +
> +    return 0




More information about the libvir-list mailing list