aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xm-test/lib
diff options
context:
space:
mode:
authorEwan Mellor <ewan@xensource.com>2006-12-01 17:37:09 +0000
committerEwan Mellor <ewan@xensource.com>2006-12-01 17:37:09 +0000
commitcebaab7c6b90391742eafd652c4aebc59f0e9052 (patch)
tree5898eda3895ffd1890b47a6f93d5c212e78cd02b /tools/xm-test/lib
parentc88422678e521f8a27260a2d5cffa0594c9fb300 (diff)
downloadxen-cebaab7c6b90391742eafd652c4aebc59f0e9052.tar.gz
xen-cebaab7c6b90391742eafd652c4aebc59f0e9052.tar.bz2
xen-cebaab7c6b90391742eafd652c4aebc59f0e9052.zip
This patch provides XmTestManagedDomain and XenManagedDomain classes
similar to the XmTestDomain and XenDomain classes. I have wrapped the xen-api for VM configuration creation, starting and stopping of VMs and destruction of the VM configuration in the XenManagedDomain class's methods. No device-related functions are provided through the class. The managed domains' UUIDs are tracked and all created VMs are destroyed upon failure or skipping of the test or by calling xapi.vm_destroy_all(). I am adding a new grouptest 'xapi' for running xen-api tests. Only caveat: I am using an empty username and password (XmTestList/xapi.py) with Xend's authentication deactivated to run these tests. Signed-off-by: Stefan Berger <stefanb@us.ibm.com>
Diffstat (limited to 'tools/xm-test/lib')
-rw-r--r--tools/xm-test/lib/XmTestLib/Test.py3
-rw-r--r--tools/xm-test/lib/XmTestLib/XenManagedDomain.py176
-rw-r--r--tools/xm-test/lib/XmTestLib/xapi.py66
3 files changed, 245 insertions, 0 deletions
diff --git a/tools/xm-test/lib/XmTestLib/Test.py b/tools/xm-test/lib/XmTestLib/Test.py
index 68a176c273..7efbc0f3e3 100644
--- a/tools/xm-test/lib/XmTestLib/Test.py
+++ b/tools/xm-test/lib/XmTestLib/Test.py
@@ -33,6 +33,7 @@ import select
import signal
import re
import glob
+import xapi
TEST_PASS = 0
TEST_FAIL = 255
@@ -133,10 +134,12 @@ def becomeNonRoot():
def FAIL(format, *args):
print "\nREASON:", (format % args)
+ xapi.vm_destroy_all()
sys.exit(TEST_FAIL)
def SKIP(format, *args):
print "\nREASON:", (format % args)
+ xapi.vm_destroy_all()
sys.exit(TEST_SKIP)
def saveLog(logText, filename=None):
diff --git a/tools/xm-test/lib/XmTestLib/XenManagedDomain.py b/tools/xm-test/lib/XmTestLib/XenManagedDomain.py
new file mode 100644
index 0000000000..94fbe44d3e
--- /dev/null
+++ b/tools/xm-test/lib/XmTestLib/XenManagedDomain.py
@@ -0,0 +1,176 @@
+#!/usr/bin/python
+"""
+ Copyright (C) International Business Machines Corp., 2005
+ Author: Stefan Berger <stefanb@us.ibm.com>
+
+ Based on XenDomain.py by Dan Smith <danms@us.ibm.com>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; under version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+"""
+import os
+import sys
+from XmTestLib import *
+from xen.util.xmlrpclib2 import ServerProxy
+from types import DictType
+
+
+class XenManagedConfig:
+ """An object to help create a VM configuration usable via Xen-API"""
+ def __init__(self):
+ self.opts = {}
+ #Array to translate old option to new ones
+ self.opttrlate = { 'name' : 'name_label' ,
+ 'memory' : [ 'memory_static_max' ,
+ 'memory_static_min' ,
+ 'memory_dynamic_min',
+ 'memory_dynamic_max' ],
+ 'kernel' : 'kernel_kernel',
+ 'ramdisk': 'kernel_initrd',
+ 'root' : 'kernel_args'}
+
+ def setOpt(self, name, value):
+ """Set an option in the config"""
+ if name in self.opttrlate.keys():
+ _name = self.opttrlate[name]
+ else:
+ _name = name
+
+ if isinstance(_name, list):
+ for _n in _name:
+ self.opts[_n] = value
+ else:
+ self.opts[_name] = value
+
+ def getOpt(self, name):
+ """Return the value of a config option"""
+ if name in self.opts.keys():
+ return self.opts[name]
+ else:
+ return None
+
+ def setOpts(self, opts):
+ """Batch-set options from a dictionary"""
+ for k, v in opts.items():
+ self.setOpt(k, v)
+
+ def getOpts(self):
+ return self.opts
+
+
+class XenManagedDomain(XenDomain):
+
+ def __init__(self, name=None, config=None):
+ if name:
+ self.name = name
+ else:
+ self.name = getUniqueName()
+
+ self.config = config
+ self.console = None
+ self.netEnv = "bridge"
+
+ self.server, self.session = xapi._connect()
+ server = self.server
+ try:
+ self.vm_uuid = xapi.execute(server.VM.create, self.session,
+ self.config.getOpts())
+ xapi._VMuuids.append(self.vm_uuid)
+ except:
+ raise DomainError("Could not create VM config file for "
+ "managed domain.")
+
+ #Only support PV for now.
+ self.type = "PV"
+
+ def start(self, noConsole=False):
+ #start the VM
+ server = self.server
+ if self.vm_uuid:
+ try:
+ xapi.execute(server.VM.start, self.session, self.vm_uuid)
+ except:
+ raise DomainError("Could not start domain")
+ else:
+ raise DomainError("VM has not UUID - VM config does not exist?")
+
+ if self.getDomainType() == "HVM":
+ waitForBoot()
+
+ if self.console and noConsole == True:
+ self.closeConsole()
+
+ elif self.console and noConsole == False:
+ return self.console
+
+ elif not self.console and noConsole == False:
+ return self.getConsole()
+
+ def stop(self):
+ if self.vm_uuid:
+ server = self.server
+ xapi.execute(server.VM.hard_shutdown, self.session, self.vm_uuid)
+ else:
+ raise DomainError("VM has not UUID - VM config does not exist?")
+
+ def destroy(self):
+ #Stop VM first.
+ self.stop()
+ if self.vm_uuid:
+ server = self.server
+ xapi.execute(server.VM.destroy, self.session, self.vm_uuid)
+ xapi._VMuuids.remove(self.vm_uuid)
+ else:
+ raise DomainError("VM has not UUID - VM config does not exist?")
+
+ def get_uuid(self):
+ return self.vm_uuid
+
+ def newDevice(self, Device, *args):
+ raise DomainError("No support for newDevice().")
+
+ def removeDevice(self, id):
+ raise DomainError("No support for removeDevice().")
+
+ def removeAllDevices(self, id):
+ raise DomainError("No support for removeAllDevices().")
+
+ def isRunning(self):
+ return isDomainRunning(self.name)
+
+ def getDevice(self, id):
+ raise DomainError("No support for getDevice().")
+
+
+class XmTestManagedDomain(XenManagedDomain):
+
+ """Create a new managed xm-test domain
+ @param name: The requested domain name
+ @param extraConfig: Additional configuration options
+ @param baseConfig: The initial configuration defaults to use
+ """
+ def __init__(self, name=None, extraConfig=None,
+ baseConfig=arch.configDefaults):
+ config = XenManagedConfig()
+ config.setOpts(baseConfig)
+ if extraConfig:
+ config.setOpts(extraConfig)
+
+ if name:
+ config.setOpt("name_label", name)
+ elif not config.getOpt("name_label"):
+ config.setOpt("name_label", getUniqueName())
+
+ XenManagedDomain.__init__(self, config.getOpt("name_label"),
+ config=config)
diff --git a/tools/xm-test/lib/XmTestLib/xapi.py b/tools/xm-test/lib/XmTestLib/xapi.py
new file mode 100644
index 0000000000..2a9ec209e3
--- /dev/null
+++ b/tools/xm-test/lib/XmTestLib/xapi.py
@@ -0,0 +1,66 @@
+#!/usr/bin/python
+#============================================================================
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of version 2.1 of the GNU Lesser General Public
+# License as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#============================================================================
+# Copyright (C) 2006 XenSource Ltd.
+# Copyright (C) 2006 IBM Corporation
+#============================================================================
+
+import os
+import sys
+from XmTestLib import *
+from xen.util.xmlrpclib2 import ServerProxy
+from types import DictType
+
+
+XAPI_DEFAULT_LOGIN = " "
+XAPI_DEFAULT_PASSWORD = " "
+
+class XenAPIError(Exception):
+ pass
+
+
+#A list of VMs' UUIDs that were created using vm_create
+_VMuuids = []
+
+#Terminate previously created managed(!) VMs and destroy their configs
+def vm_destroy_all():
+ server, session = _connect()
+ for uuid in _VMuuids:
+ execute(server.VM.hard_shutdown, session, uuid)
+ execute(server.VM.destroy , session, uuid)
+
+
+def execute(fn, *args):
+ result = fn(*args)
+ if type(result) != DictType:
+ raise TypeError("Function returned object of type: %s" %
+ str(type(result)))
+ if 'Value' not in result:
+ raise XenAPIError(*result['ErrorDescription'])
+ return result['Value']
+
+_initialised = False
+_server = None
+_session = None
+def _connect(*args):
+ global _server, _session, _initialised
+ if not _initialised:
+ _server = ServerProxy('httpu:///var/run/xend/xmlrpc.sock')
+ login = XAPI_DEFAULT_LOGIN
+ password = XAPI_DEFAULT_PASSWORD
+ creds = (login, password)
+ _session = execute(_server.session.login_with_password, *creds)
+ _initialised = True
+ return (_server, _session)