[libvirt] [test-API][PATCH v2 2/4] Add libvirtd restart test case

Guannan Ren gren at redhat.com
Thu Sep 15 06:17:11 UTC 2011


On 09/15/2011 10:59 AM, Alex Jia wrote:
> On 09/14/2011 06:31 PM, Wayne Sun wrote:
>>    * repos/libvirtd/restart.py
>>
>>      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 |  180 
>> +++++++++++++++++++++++++++++++++++++++++++++
>>   1 files changed, 180 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..7ccbb38
>> --- /dev/null
>> +++ b/repos/libvirtd/restart.py
>> @@ -0,0 +1,180 @@
>> +#!/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
>> +
>> +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 check_domain_running(domobj, guestname, logger):
>> +    """ check if the domain exists, may or may not be active """
>> +    guest_names = domobj.get_list()
>> +
>> +    if guestname not in guest_names:
>> +        logger.error("%s doesn't exist or not running" % guestname)
>> +        return 1
>> +    else:
>> +        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)
>> +
>> +    logger.info("check the domain state")
>> +    ret = check_domain_running(domobj, guestname, logger)
>> +    if ret:
>> +        return 1
>> +
>> +    conn.close()
>> +
>> +    logger.info("check the libvirtd status:")
>> +    ret = libvirtd_check(util, logger)
>> +    if ret:
>> +        return 1
>> +
>> +    # Get domain ip
>> +    logger.info("get the mac address of domain %s" % guestname)
>> +    mac = util.get_dom_mac_addr(guestname)
>> +    logger.info("the mac address of domain %s is %s" % (guestname, 
>> mac))
>> +    logger.info("get ip by mac address")
>> +    ip = util.mac_to_ip(mac, 180)
>> +    logger.info("the ip address of domain %s is %s" % (guestname, ip))
>> +    timeout = 600
> Assigning 600 to timeout variable, but unused variable 'timeout'.
>> +
>> +    logger.info("ping to domain %s" % guestname)
>> +    if util.do_ping(ip, 0):
>> +        logger.info("Success ping domain %s" % guestname)
>> +    else:
>> +        logger.error("fail to ping domain %s" % guestname)
>> +        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:")
>> +    ret = libvirtd_check(util, logger)
>> +    if ret:
>> +        return 1
>> +
>> +    logger.info("ping to domain %s again" % guestname)
>> +    if util.do_ping(ip, 0):
>> +        logger.info("Success ping domain %s" % guestname)
>> +    else:
>> +        logger.error("fail to ping domain %s" % guestname)
>> +        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
>> +
>> +def restart_clean(params):
>> +    """ clean testing environment """
>> +    pass
>> +
>
> -- 
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list

    ACK and pushed with this fixed.

    Guannan Ren




More information about the libvir-list mailing list