Index: root/xen-unstable.hg/tools/xm-test/lib/XmTestLib/xapi.py =================================================================== --- root.orig/xen-unstable.hg/tools/xm-test/lib/XmTestLib/xapi.py +++ root/xen-unstable.hg/tools/xm-test/lib/XmTestLib/xapi.py @@ -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 Index: root/xen-unstable.hg/tools/xm-test/tests/vtpm/09_vtpm-xapi.py =================================================================== --- root.orig/xen-unstable.hg/tools/xm-test/tests/vtpm/09_vtpm-xapi.py +++ root/xen-unstable.hg/tools/xm-test/tests/vtpm/09_vtpm-xapi.py @@ -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() Index: root/xen-unstable.hg/tools/xm-test/xapi-setup.py =================================================================== --- /dev/null +++ root/xen-unstable.hg/tools/xm-test/xapi-setup.py @@ -0,0 +1,48 @@ +#!/usr/bin/python +import sys as sys +import getpass as getpass + +XAPI_AUTH_FILE = "xmtest_auth.xml" +XAPI_AUTH_PATH = "/etc/xen/" +XAPI_FILE = XAPI_AUTH_PATH + XAPI_AUTH_FILE + +def usage(): + print "Usage: xapi-setup.py [] []\n\n" \ + "Tool to setup the xm-test suite for being able to use the Xen-API\n" \ + "with authenticated sessions. Username and password are stored in\n" \ + "cleartext in %s.\n" % XAPI_FILE + +def main(): + rc = 0 + if (len(sys.argv) >= 2) and sys.argv[1] == "-?": + usage() + sys.exit(0) + print "Enter username and password for usage with Xen-API:\n" + if (len(sys.argv) >=2 ): + username = sys.argv[1] + print "Login: %s" % username + else: + username = rawinput("Login: ") + if (len(sys.argv) >= 3 ): + password = sys.argv[2] + print "Password: " + else: + password = getpass.getpass() + f = open(XAPI_FILE, "w") + if f: + f.write("\n" + "\n" + " \n" + "\n" + % (username, password)) + f.close() + else: + print("Could not open file %s for writing." % XAPI_FILE) + rc = -1 + return rc + +if __name__ == "__main__": + sys.exit(main()) Index: root/xen-unstable.hg/tools/xm-test/lib/XmTestLib/XenManagedDomain.py =================================================================== --- root.orig/xen-unstable.hg/tools/xm-test/lib/XmTestLib/XenManagedDomain.py +++ root/xen-unstable.hg/tools/xm-test/lib/XmTestLib/XenManagedDomain.py @@ -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?") Index: root/xen-unstable.hg/tools/xm-test/configure.ac =================================================================== --- root.orig/xen-unstable.hg/tools/xm-test/configure.ac +++ root/xen-unstable.hg/tools/xm-test/configure.ac @@ -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 Index: root/xen-unstable.hg/tools/xm-test/grouptest/xapi =================================================================== --- root.orig/xen-unstable.hg/tools/xm-test/grouptest/xapi +++ root/xen-unstable.hg/tools/xm-test/grouptest/xapi @@ -1 +1,2 @@ +xapi vtpm 09_vtpm-xapi.test Index: root/xen-unstable.hg/tools/xm-test/tests/xapi/01_xapi-vm_basic.py =================================================================== --- /dev/null +++ root/xen-unstable.hg/tools/xm-test/tests/xapi/01_xapi-vm_basic.py @@ -0,0 +1,65 @@ +#!/usr/bin/python + +# Copyright (C) International Business Machines Corp., 2006 +# Author: Stefan Berger + +# Basic VM creation test + +from XmTestLib import xapi +from XmTestLib.XenManagedDomain import XmTestManagedDomain +from XmTestLib import * +from xen.xend import XendAPIConstants +import xapi_utils +import commands +import os + +def do_test(): + domain = XmTestManagedDomain() + vm_uuid = domain.get_uuid() + + session = xapi.connect() + + domain.start(startpaused=True) + + res = session.xenapi.VM.get_power_state(vm_uuid) + + if res != XendAPIConstants.XEN_API_VM_POWER_STATE[XendAPIConstants.XEN_API_VM_POWER_STATE_PAUSED]: + FAIL("VM was not started in 'paused' state") + + res = session.xenapi.VM.unpause(vm_uuid) + + res = session.xenapi.VM.get_power_state(vm_uuid) + + if res != XendAPIConstants.XEN_API_VM_POWER_STATE[XendAPIConstants.XEN_API_VM_POWER_STATE_RUNNING]: + FAIL("VM could not be put into 'running' state") + + console = domain.getConsole() + + try: + run = console.runCmd("cat /proc/interrupts") + except ConsoleError, e: + saveLog(console.getHistory()) + FAIL("Could not access proc-filesystem") + + res = session.xenapi.VM.pause(vm_uuid) + + res = session.xenapi.VM.get_power_state(vm_uuid) + + if res != XendAPIConstants.XEN_API_VM_POWER_STATE[XendAPIConstants.XEN_API_VM_POWER_STATE_PAUSED]: + FAIL("VM could not be put into 'paused' state") + + res = session.xenapi.VM.unpause(vm_uuid) + + res = session.xenapi.VM.get_power_state(vm_uuid) + + if res != XendAPIConstants.XEN_API_VM_POWER_STATE[XendAPIConstants.XEN_API_VM_POWER_STATE_RUNNING]: + FAIL("VM could not be 'unpaused'") + + domain.stop() + domain.destroy() + +try: + do_test() +finally: + #Make sure all domains that were created in this test case are gone + xapi.vm_destroy_all() Index: root/xen-unstable.hg/tools/xm-test/tests/xapi/Makefile.am =================================================================== --- /dev/null +++ root/xen-unstable.hg/tools/xm-test/tests/xapi/Makefile.am @@ -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 *~ Index: root/xen-unstable.hg/tools/xm-test/tests/xapi/xapi_utils.py =================================================================== --- /dev/null +++ root/xen-unstable.hg/tools/xm-test/tests/xapi/xapi_utils.py @@ -0,0 +1,7 @@ +from XmTestLib.Test import SKIP + +try: + import PAM +except ImportError: + SKIP("Skipping test. Python-PAM module not found.") +