source: trunk/packages/xen-3.1/xen-3.1/tools/xm-test/tests/vtpm/09_vtpm-xapi.py @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

  • Property svn:mime-type set to text/script
File size: 4.3 KB
Line 
1#!/usr/bin/python
2
3# Copyright (C) International Business Machines Corp., 2006
4# Author: Stefan Berger <stefanb@us.ibm.com>
5
6# Test to test the vtpm class through the Xen-API
7#
8# Tested methods:
9#  VTPM: get_uuid, get_backend, get_by_uuid, get_record
10#        create, destroy, get_VM
11#  VM: get_VTPMS
12
13from XmTestLib import xapi
14from XmTestLib.XenAPIDomain import XmTestAPIDomain
15from XmTestLib import *
16from vtpm_utils import *
17import commands
18import os
19
20VTPM_RECORD_KEYS = [ 'backend', 'VM', 'uuid' ]
21
22try:
23    # XmTestAPIDomain tries to establish a connection to XenD
24    domain = XmTestAPIDomain()
25except Exception, e:
26    SKIP("Skipping test. Error: %s" % str(e))
27vm_uuid = domain.get_uuid()
28
29vtpmcfg = {}
30vtpmcfg['backend'] = DOM0_UUID
31vtpmcfg['VM'] = vm_uuid
32
33session = xapi.connect()
34
35vtpm_uuid = session.xenapi.VTPM.create(vtpmcfg)
36
37vtpm_be = session.xenapi.VTPM.get_backend(vtpm_uuid)
38if vtpm_be != vtpmcfg['backend']:
39    FAIL("vTPM's backend is in '%s', expected: '%s'" %
40         (vtpm_be, vtpmcfg['backend']))
41
42vtpm_rec = session.xenapi.VTPM.get_record(vtpm_uuid)
43
44miss_keys = []
45for k in VTPM_RECORD_KEYS:
46    if k not in vtpm_rec.keys():
47        miss_keys.append(k)
48if len(miss_keys) > 0:
49    FAIL("vTPM record is missing key(s): %s" % miss_keys)
50
51if vtpm_rec['uuid']  != vtpm_uuid:
52    FAIL("vTPM record shows vtpm uuid '%s', expected: '%s'" %
53         (vtpm_rec['uuid'], vtpm_uuid))
54if vtpm_rec['VM']  != vm_uuid:
55    FAIL("vTPM record shows VM uuid '%s', expected: '%s'" %
56         (vtpm_rec['VM'], vm_uuid))
57if vtpm_rec['backend'] != vtpmcfg['backend']:
58    FAIL("vTPM record shows VM bakcned '%s', expected: '%s'" %
59         (vtpm_rev['backend'], vtpmcfg['backend']))
60
61badkeys = []
62keys = vtpm_rec.keys()
63for k in keys:
64    if k not in VTPM_RECORD_KEYS:
65        badkeys.append(k)
66if len(badkeys) > 0:
67    FAIL("Unexpected attributes in result: %s" % badkeys)
68
69if vm_uuid != session.xenapi.VTPM.get_VM(vtpm_uuid):
70    FAIL("VM uuid from VTPM.get_VM different (%s) than expected (%s)." %
71         (vm_ref, vm_uuid))
72
73uuid = session.xenapi.VTPM.get_uuid(vtpm_uuid)
74if uuid != vtpm_uuid:
75    FAIL("vTPM from VTPM.get_uuid different (%s) than expected (%s)." %
76         (uuid, vtpm_uuid))
77
78vtpm_ref = session.xenapi.VTPM.get_by_uuid(vtpm_uuid)
79if vtpm_ref != vtpm_uuid:
80    FAIL("vTPM from VTPM.get_by_uuid different (%s) than expected (%s)." %
81         (vtpm_ref, vtpm_uuid))
82
83vm_vtpms = session.xenapi.VM.get_VTPMs(vm_uuid)
84if len(vm_vtpms) != 1:
85    FAIL("Number of vTPMs from get_VTPMs is (%d) not what was expected (%d)" %
86         (len(vm_vtpms), 1))
87if vtpm_uuid not in vm_vtpms:
88    FAIL("Other vTPM uuid (%s) returned from VM.get_VTPMs than expected (%s)" %
89         (vm_vtpms[0], vtpm_uuid))
90
91try:
92    console = domain.start()
93except DomainError, e:
94    FAIL("Unable to create domain.")
95
96try:
97    console.sendInput("input")
98except ConsoleError, e:
99    saveLog(console.getHistory())
100    FAIL(str(e))
101
102try:
103    run = console.runCmd("cat /sys/devices/xen/vtpm-0/pcrs")
104except ConsoleError, e:
105    saveLog(console.getHistory())
106    FAIL("1. No result from dumping the PCRs")
107
108if re.search("No such file",run["output"]):
109    FAIL("TPM frontend support not compiled into (domU?) kernel")
110
111if not re.search("PCR-00:",run["output"]):
112    saveLog(console.getHistory())
113    FAIL("1. Virtual TPM is not working correctly on /dev/vtpm on backend side: \n%s" % run["output"])
114
115try:
116    session.xenapi.VTPM.destroy(vtpm_uuid)
117    #Should never get here
118    FAIL("Could destroy vTPM while VM is running")
119except:
120    pass
121
122rc = session.xenapi.VM.suspend(vm_uuid)
123if rc:
124    FAIL("Could not suspend VM")
125
126try:
127    session.xenapi.VTPM.destroy(vtpm_uuid)
128    #May not throw an exception in 'suspend' state
129except:
130    pass
131
132rc = session.xenapi.VM.resume(vm_uuid, False)
133if rc:
134    FAIL("Could not resume VM")
135
136try:
137    console = domain.getConsole()
138except ConsoleError, e:
139    FAIL(str(e))
140
141try:
142    run = console.runCmd("cat /sys/devices/xen/vtpm-0/pcrs")
143except ConsoleError, e:
144    saveLog(console.getHistory())
145    FAIL("2. No result from dumping the PCRs. vTPM has been removed?")
146
147if not re.search("PCR-00:",run["output"]):
148    saveLog(console.getHistory())
149    FAIL("2. Virtual TPM is not working correctly on /dev/vtpm on backend side: \n%s" % run["output"])
150
151domain.stop()
152
153try:
154    session.xenapi.VTPM.destroy(vtpm_uuid)
155except:
156    FAIL("Could NOT destroy vTPM while domain is halted.")
157
158domain.destroy()
Note: See TracBrowser for help on using the repository browser.