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 | |
---|
13 | from XmTestLib import xapi |
---|
14 | from XmTestLib.XenAPIDomain import XmTestAPIDomain |
---|
15 | from XmTestLib import * |
---|
16 | from vtpm_utils import * |
---|
17 | import commands |
---|
18 | import os |
---|
19 | |
---|
20 | VTPM_RECORD_KEYS = [ 'backend', 'VM', 'uuid' ] |
---|
21 | |
---|
22 | try: |
---|
23 | # XmTestAPIDomain tries to establish a connection to XenD |
---|
24 | domain = XmTestAPIDomain() |
---|
25 | except Exception, e: |
---|
26 | SKIP("Skipping test. Error: %s" % str(e)) |
---|
27 | vm_uuid = domain.get_uuid() |
---|
28 | |
---|
29 | vtpmcfg = {} |
---|
30 | vtpmcfg['backend'] = DOM0_UUID |
---|
31 | vtpmcfg['VM'] = vm_uuid |
---|
32 | |
---|
33 | session = xapi.connect() |
---|
34 | |
---|
35 | vtpm_uuid = session.xenapi.VTPM.create(vtpmcfg) |
---|
36 | |
---|
37 | vtpm_be = session.xenapi.VTPM.get_backend(vtpm_uuid) |
---|
38 | if vtpm_be != vtpmcfg['backend']: |
---|
39 | FAIL("vTPM's backend is in '%s', expected: '%s'" % |
---|
40 | (vtpm_be, vtpmcfg['backend'])) |
---|
41 | |
---|
42 | vtpm_rec = session.xenapi.VTPM.get_record(vtpm_uuid) |
---|
43 | |
---|
44 | miss_keys = [] |
---|
45 | for k in VTPM_RECORD_KEYS: |
---|
46 | if k not in vtpm_rec.keys(): |
---|
47 | miss_keys.append(k) |
---|
48 | if len(miss_keys) > 0: |
---|
49 | FAIL("vTPM record is missing key(s): %s" % miss_keys) |
---|
50 | |
---|
51 | if vtpm_rec['uuid'] != vtpm_uuid: |
---|
52 | FAIL("vTPM record shows vtpm uuid '%s', expected: '%s'" % |
---|
53 | (vtpm_rec['uuid'], vtpm_uuid)) |
---|
54 | if vtpm_rec['VM'] != vm_uuid: |
---|
55 | FAIL("vTPM record shows VM uuid '%s', expected: '%s'" % |
---|
56 | (vtpm_rec['VM'], vm_uuid)) |
---|
57 | if vtpm_rec['backend'] != vtpmcfg['backend']: |
---|
58 | FAIL("vTPM record shows VM bakcned '%s', expected: '%s'" % |
---|
59 | (vtpm_rev['backend'], vtpmcfg['backend'])) |
---|
60 | |
---|
61 | badkeys = [] |
---|
62 | keys = vtpm_rec.keys() |
---|
63 | for k in keys: |
---|
64 | if k not in VTPM_RECORD_KEYS: |
---|
65 | badkeys.append(k) |
---|
66 | if len(badkeys) > 0: |
---|
67 | FAIL("Unexpected attributes in result: %s" % badkeys) |
---|
68 | |
---|
69 | if 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 | |
---|
73 | uuid = session.xenapi.VTPM.get_uuid(vtpm_uuid) |
---|
74 | if uuid != vtpm_uuid: |
---|
75 | FAIL("vTPM from VTPM.get_uuid different (%s) than expected (%s)." % |
---|
76 | (uuid, vtpm_uuid)) |
---|
77 | |
---|
78 | vtpm_ref = session.xenapi.VTPM.get_by_uuid(vtpm_uuid) |
---|
79 | if vtpm_ref != vtpm_uuid: |
---|
80 | FAIL("vTPM from VTPM.get_by_uuid different (%s) than expected (%s)." % |
---|
81 | (vtpm_ref, vtpm_uuid)) |
---|
82 | |
---|
83 | vm_vtpms = session.xenapi.VM.get_VTPMs(vm_uuid) |
---|
84 | if len(vm_vtpms) != 1: |
---|
85 | FAIL("Number of vTPMs from get_VTPMs is (%d) not what was expected (%d)" % |
---|
86 | (len(vm_vtpms), 1)) |
---|
87 | if 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 | |
---|
91 | try: |
---|
92 | console = domain.start() |
---|
93 | except DomainError, e: |
---|
94 | FAIL("Unable to create domain.") |
---|
95 | |
---|
96 | try: |
---|
97 | console.sendInput("input") |
---|
98 | except ConsoleError, e: |
---|
99 | saveLog(console.getHistory()) |
---|
100 | FAIL(str(e)) |
---|
101 | |
---|
102 | try: |
---|
103 | run = console.runCmd("cat /sys/devices/xen/vtpm-0/pcrs") |
---|
104 | except ConsoleError, e: |
---|
105 | saveLog(console.getHistory()) |
---|
106 | FAIL("1. No result from dumping the PCRs") |
---|
107 | |
---|
108 | if re.search("No such file",run["output"]): |
---|
109 | FAIL("TPM frontend support not compiled into (domU?) kernel") |
---|
110 | |
---|
111 | if 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 | |
---|
115 | try: |
---|
116 | session.xenapi.VTPM.destroy(vtpm_uuid) |
---|
117 | #Should never get here |
---|
118 | FAIL("Could destroy vTPM while VM is running") |
---|
119 | except: |
---|
120 | pass |
---|
121 | |
---|
122 | rc = session.xenapi.VM.suspend(vm_uuid) |
---|
123 | if rc: |
---|
124 | FAIL("Could not suspend VM") |
---|
125 | |
---|
126 | try: |
---|
127 | session.xenapi.VTPM.destroy(vtpm_uuid) |
---|
128 | #May not throw an exception in 'suspend' state |
---|
129 | except: |
---|
130 | pass |
---|
131 | |
---|
132 | rc = session.xenapi.VM.resume(vm_uuid, False) |
---|
133 | if rc: |
---|
134 | FAIL("Could not resume VM") |
---|
135 | |
---|
136 | try: |
---|
137 | console = domain.getConsole() |
---|
138 | except ConsoleError, e: |
---|
139 | FAIL(str(e)) |
---|
140 | |
---|
141 | try: |
---|
142 | run = console.runCmd("cat /sys/devices/xen/vtpm-0/pcrs") |
---|
143 | except ConsoleError, e: |
---|
144 | saveLog(console.getHistory()) |
---|
145 | FAIL("2. No result from dumping the PCRs. vTPM has been removed?") |
---|
146 | |
---|
147 | if 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 | |
---|
151 | domain.stop() |
---|
152 | |
---|
153 | try: |
---|
154 | session.xenapi.VTPM.destroy(vtpm_uuid) |
---|
155 | except: |
---|
156 | FAIL("Could NOT destroy vTPM while domain is halted.") |
---|
157 | |
---|
158 | domain.destroy() |
---|