
On 04/03/2012 09:20 PM, Peter Krempa wrote:
This test case checks if the console connection code works in a safe way that the connection don't get messed up. --- repos/domain/console_mutex.py | 89 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 89 insertions(+), 0 deletions(-) create mode 100644 repos/domain/console_mutex.py
diff --git a/repos/domain/console_mutex.py b/repos/domain/console_mutex.py new file mode 100644 index 0000000..f9df815 --- /dev/null +++ b/repos/domain/console_mutex.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python +""" A test case to test console mutual exclusivity + mandatory arguments: guestname +""" +import libvirt +from libvirt import libvirtError + +from utils.Python import utils +from utils.Python.testError import TestError
we could just use logger.error(), then close hypervisor connection, then quit.
+ +def usage(params): + """Verify parameter dictionary""" + logger = params['logger'] + keys = ['guestname'] + for key in keys: + if key not in params: + logger.error("%s is required" %key) + return 1 + +def console_mutex(params): + """Attach to console""" + logger = params['logger']
call usage() to check for mandatory options
+ guest = params['guestname'] + if "device" not in params: + device = "serial0" + else: + device = params['device']
It's ok here, guest = params.get('device', 'serial0') get better.
+ + util = utils.Utils() + uri = params['uri'] + + ret = 1 + + try: + logger.info("Connecting to hypervisor: " + uri) + conn = libvirt.open(uri) + dom = conn.lookupByName(guest) + + logger.info("Creating stream object") + stream = conn.newStream(0) + + logger.info("Forcibly open console on domain") + dom.openConsole(device, stream, libvirt.VIR_DOMAIN_CONSOLE_FORCE); + + logger.info("Creating another stream object") + stream2 = conn.newStream(0) + + logger.info("Open safe console connection while an existing one is open");
the semicolon is optional. and same for the following appearance.
+ failed = False + try: + dom.openConsole(device, stream2, libvirt.VIR_DOMAIN_CONSOLE_SAFE); + except libvirtError, e: + logger.info("Opening failed - OK") + failed = True + + if not failed: + raise TestError("Opening of console succeeded although shoud fail") + + logger.info("Abort the existing stream") + stream.abort() + + logger.info("Re-try connecting to the console") + dom.openConsole(device, stream2, libvirt.VIR_DOMAIN_CONSOLE_SAFE); + + logger.info("Re-try forcibly on already open console") + + logger.info("Creating stream object") + stream = conn.newStream(0) + + dom.openConsole(device, stream, libvirt.VIR_DOMAIN_CONSOLE_FORCE); + + except libvirtError, e: + logger.error("Libvirt call failed") + raise e; + + except TestError, e: + logger.error("Test failed: " + str(e)); + + else: + logger.info("All tests succeeded") + ret = 0 + + finally: + logger.info("Closing hypervisor connection") + conn.close()
I really not sure about the stream, if we need to call stream.abort() in finally to close the certain FD at server part.
+ + logger.info("Done") + + return ret
The clean function is necessary for testcases. So we need to add another function named 'console_mutex_clean' to cleanup the testing environment messed by the testcase. The params is same as main function. If nothing need to clean, just write like this. def console_mutex_clean(params): """clean testing environment""" pass Guannan Ren