Reworked support for Xen-API authenticated sessions and a basic XAPI test case
PATCH 1/1 Reworked support for Xen-API authenticated sessions and a basic XAPI test case
This is a reworked patch for testing xen-api inside the xm-test suite. The login and password necessary for using the xen-api are tried to be read from /etc/xen/xm-config.xml. If this file does not exist the information is attempted to be read from /etc/xen/xmtest_auth.xml which uses the same xml description and can be created with xm-test/xapi-setup.py. I have tried to share as much code with xm as possible for parsing of the config files.
I am also adding a basic test for the raw xen-api for creating a VM and pausing and unpausing it. This test is part of the grouptest 'xapi'.
xen-unstable.hg/tools/xm-test/lib/XmTestLib/xapi.py +36 -36
@@ -20,15 +20,11 @@ import os import sys from XmTestLib import * -from xen.util.xmlrpclib2 import ServerProxy +from xen.xm import main as xmmain +from xen.xm import XenAPI +from xen.xm.opts import OptionError from types import DictType - - -XAPI_DEFAULT_LOGIN = " " -XAPI_DEFAULT_PASSWORD = " " - -class XenAPIError(Exception): - pass +import xml.dom.minidom #A list of VMs' UUIDs that were created using vm_create
@@ -36,31 +32,35 @@ _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/xen-api.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) + if len(_VMuuids) > 0: + session = connect() + for uuid in _VMuuids: + session.xapi.VM.hard_shutdown(uuid) + session.xapi.VM.destroy(uuid) + +def get_login_pwd(): + if xmmain.serverType == xmmain.SERVER_XEN_API: + try: + login, password = xmmain.parseAuthentication() + return (login, password) + except: + pass + # set xmmain's gloabl 'config' variable to the xml file's dom + xmtest_auth = "/etc/xen/xmtest_auth.xml" + if os.path.isfile(xmtest_auth): + xmmain.config = xml.dom.minidom.parse(xmtest_auth) + login, password = xmmain.parseAuthentication() + else: + raise OptionError("Configuration for login/pwd not found. " + "Need to run xapi-setup.py?") + return (login, password) + + +def connect(*args): + try: + creds = get_login_pwd() + except Exception, e: + FAIL("%s" % str(e)) + _session = XenAPI.Session("httpu:///var/run/xend/xen-api.sock") + _session.login_with_password(*creds) + return _session
xen-unstable.hg/tools/xm-test/tests/vtpm/09_vtpm-xapi.py +11 -7
@@ -22,22 +22,22 @@ def do_test(): vtpmcfg['instance'] = 1 vtpmcfg['VM'] = vm_uuid - server, session = xapi._connect() + session = xapi.connect() - vtpm_uuid = xapi.execute(server.VTPM.create, session, vtpmcfg) + vtpm_uuid = session.xenapi.VTPM.create(vtpmcfg) - vtpm_id = xapi.execute(server.VTPM.get_instance, session, vtpm_uuid) - vtpm_be = xapi.execute(server.VTPM.get_backend , session, vtpm_uuid) + vtpm_id = session.xenapi.VTPM.get_instance(vtpm_uuid) + vtpm_be = session.xenapi.VTPM.get_backend(vtpm_uuid) if vtpm_be != vtpmcfg['backend']: FAIL("vTPM's backend is in '%s', expected: '%s'" % (vtpm_be, vtpmcfg['backend'])) - driver = xapi.execute(server.VTPM.get_driver, session, vtpm_uuid) + driver = session.xenapi.VTPM.get_driver(vtpm_uuid) if driver != vtpmcfg['type']: FAIL("vTPM has driver type '%s', expected: '%s'" % (driver, vtpmcfg['type'])) - vtpm_rec = xapi.execute(server.VTPM.get_record, session, vtpm_uuid) + vtpm_rec = session.xenapi.VTPM.get_record(vtpm_uuid) if vtpm_rec['driver'] != vtpmcfg['type']: FAIL("vTPM record shows driver type '%s', expected: '%s'" %
@@ -68,9 +68,13 @@ def do_test(): domain.destroy() +try: + import PAM +except ImportError: + SKIP("Skipping test. Python-PAM module not found.") try: do_test() finally: - #Make sure all domains are gone that were created in this test case + #Make sure all domains that were created in this test case are gone xapi.vm_destroy_all()
xen-unstable.hg/tools/xm-test/xapi-setup.py new file +48 -0
48 lines not on this page · raw message
xen-unstable.hg/tools/xm-test/lib/XmTestLib/XenManagedDomain.py +13 -14
@@ -36,9 +36,9 @@ class XenManagedConfig: 'memory_static_min' , 'memory_dynamic_min', 'memory_dynamic_max' ], - 'kernel' : 'kernel_kernel', - 'ramdisk': 'kernel_initrd', - 'root' : 'kernel_args'} + 'kernel' : 'PV_kernel', + 'ramdisk': 'PV_ramdisk', + 'root' : 'PV_args'} def setOpt(self, name, value): """Set an option in the config"""
@@ -81,11 +81,10 @@ class XenManagedDomain(XenDomain): self.console = None self.netEnv = "bridge" - self.server, self.session = xapi._connect() - server = self.server + self.session = xapi.connect() + session = self.session try: - self.vm_uuid = xapi.execute(server.VM.create, self.session, - self.config.getOpts()) + self.vm_uuid = session.xenapi.VM.create(self.config.getOpts()) xapi._VMuuids.append(self.vm_uuid) except: raise DomainError("Could not create VM config file for "
@@ -96,16 +95,18 @@ class XenManagedDomain(XenDomain): def start(self, noConsole=False, startpaused=False): #start the VM - server = self.server + session = self.session if self.vm_uuid: try: - xapi.execute(server.VM.start, self.session, self.vm_uuid, - startpaused) + session.xenapi.VM.start(self.vm_uuid, startpaused) except: raise DomainError("Could not start domain") else: raise DomainError("VM has not UUID - VM config does not exist?") + if startpaused: + return + if self.getDomainType() == "HVM": waitForBoot()
@@ -120,8 +121,7 @@ class XenManagedDomain(XenDomain): def stop(self): if self.vm_uuid: - server = self.server - xapi.execute(server.VM.hard_shutdown, self.session, self.vm_uuid) + self.session.xenapi.VM.hard_shutdown(self.vm_uuid) else: raise DomainError("VM has not UUID - VM config does not exist?")
@@ -129,8 +129,7 @@ class XenManagedDomain(XenDomain): #Stop VM first. self.stop() if self.vm_uuid: - server = self.server - xapi.execute(server.VM.destroy, self.session, self.vm_uuid) + self.session.xenapi.VM.destroy(self.vm_uuid) xapi._VMuuids.remove(self.vm_uuid) else: raise DomainError("VM has not UUID - VM config does not exist?")
xen-unstable.hg/tools/xm-test/configure.ac +1 -0
@@ -150,6 +150,7 @@ AC_CONFIG_FILES([ tests/vcpu-pin/Makefile tests/vcpu-disable/Makefile tests/vtpm/Makefile + tests/xapi/Makefile tests/enforce_dom0_cpus/Makefile lib/XmTestReport/xmtest.py lib/XmTestLib/config.py
xen-unstable.hg/tools/xm-test/grouptest/xapi +1 -0
@@ -1 +1,2 @@ +xapi vtpm 09_vtpm-xapi.test
xen-unstable.hg/tools/xm-test/tests/xapi/01_xapi-vm_basic.py new file +65 -0
65 lines not on this page · raw message
xen-unstable.hg/tools/xm-test/tests/xapi/Makefile.am new file +19 -0
@@ -0,0 +1,19 @@ +SUBDIRS = + +TESTS = 01_xapi-vm_basic.test + +XFAIL_TESTS = + +EXTRA_DIST = $(TESTS) $(XFAIL_TESTS) xapi_utils.py +TESTS_ENVIRONMENT=@TENV@ + +%.test: %.py + cp $< $@ + chmod +x $@ + +clean-local: am_config_clean-local + +am_config_clean-local: + rm -f *test + rm -f *log + rm -f *~
xen-unstable.hg/tools/xm-test/tests/xapi/xapi_utils.py new file +7 -0
@@ -0,0 +1,7 @@ +from XmTestLib.Test import SKIP + +try: + import PAM +except ImportError: + SKIP("Skipping test. Python-PAM module not found.") +
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel