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(a)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
+
+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()
Usually, we need put the API call into try..except clause. It will
catch the API error in the case of API error. like this:
try:
domobj.get_state(guestname)
except LibvirtAPI, e:
logger.error("API error message: %s, error code is %s" %
(e.response()['message'], e.response()['code']))
logger.error("Error: fail to get the state of %s" %
guestname)
conn.close()
return 1
+
+ if(state == "shutoff"):
+ logger.info("guest is shutoff, if u want to run this case, \
+ guest must be running")
+ return 1
+
The judgment is not accurate very much. It's better to
do this work at the start of testing, like the follows:
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
+ logger.info("check the libvirtd status:")
+ result = libvirtd_check(util, logger)
+ 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
It's also good to do the ping test especially in case that
the guest hangs up.
+
+ 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
Others like ajia pointed out.
Guannan Ren