ia64/xen-unstable

view tools/xm-test/lib/XmTestLib/Test.py @ 7757:139aabe357a4

Various chmod a+x.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@leeni.uk.xensource.com
date Thu Nov 10 12:00:09 2005 +0100 (2005-11-10)
parents f79854882e1f
children
line source
1 #!/usr/bin/python
3 """
4 Copyright (C) International Business Machines Corp., 2005
5 Author: Dan Smith <danms@us.ibm.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; under version 2 of the License.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 """
22 ##
23 ## These are utility functions for test cases
24 ##
26 import sys
27 import commands
28 import os
29 import pwd
30 import time
31 import pty
32 import select
33 import signal
34 import re
36 TEST_PASS = 0
37 TEST_FAIL = 255
38 TEST_SKIP = 77
40 # We currently advise waiting this many seconds for the ramdisk to
41 # boot inside a domU
42 TEST_DOMU_BOOT_DELAY = 20
44 if os.environ.get("TEST_VERBOSE"):
45 verbose = True
46 else:
47 verbose = False
49 class TimeoutError(Exception):
50 def __init__(self, msg, outputSoFar):
51 self.msg = msg
52 self.output = outputSoFar
54 def __str__(self):
55 return str(self.msg)
57 def runWithTimeout(cmd, timeout):
58 args = cmd.split()
60 pid, fd = pty.fork();
62 startTime = time.time()
64 if pid == 0:
65 os.execvp(args[0], args)
67 output = ""
69 while time.time() - startTime < timeout:
70 i, o, e = select.select([fd], [], [], timeout)
72 if fd in i:
73 try:
74 str = os.read(fd, 1)
75 output += str
76 except OSError, e:
77 exitPid, status = os.waitpid(pid, os.WNOHANG)
79 if exitPid == pid:
80 if verbose:
81 print "Child exited with %i" % status
82 return status, output
84 if verbose:
85 print "Command timed out: killing pid %i" % pid
87 os.kill(pid, signal.SIGINT)
88 raise TimeoutError("Command execution time exceeded %i seconds" % timeout,
89 outputSoFar=output)
91 def traceCommand(command, timeout=None, logOutput=True):
92 if verbose:
93 print "[dom0] Running `%s'" % command
95 if timeout:
96 status, output = runWithTimeout(command, timeout)
97 else:
98 status, output = commands.getstatusoutput(command)
100 if logOutput and verbose:
101 print output
103 return status, output
105 def getTestName():
106 script = sys.argv[0]
107 fname = os.path.basename(script)
108 match = re.match("([^\.]+)\.[a-z]+", fname)
109 if match:
110 tname = match.group(1)
111 else:
112 tname = "UNKNOWN"
114 return tname
116 def becomeNonRoot():
117 """Become a non-root user, or FAIL if this is not possible. This call
118 succeeds if we are already running as a non-root user.
119 """
121 if os.geteuid() == 0:
122 # Try and become "nobody". This user is commonly in place, but this
123 # could be extended to consider any number of users to be acceptable,
124 # if there are systems where "nobody" is not present.
125 allusers = pwd.getpwall()
126 for u in allusers:
127 if u[0] == "nobody":
128 os.setreuid(u[2], u[2])
129 break
130 if os.geteuid() == 0:
131 FAIL("Could not become a non-root user")
133 def FAIL(reason):
134 print "REASON: %s" % reason
135 sys.exit(TEST_FAIL)
137 def SKIP(reason):
138 print "REASON: %s" % reason
139 sys.exit(TEST_SKIP)
141 def saveLog(logText, filename=None):
142 if not filename:
143 filename = "log";
144 logfile = open(filename, 'w');
145 date = commands.getoutput("date");
146 logfile.write("-- BEGIN XmTest Log @" + date + "\n");
147 logfile.write(logText);
148 logfile.write("\n-- END XmTest Log\n");
149 logfile.close();
151 def waitForBoot():
152 if verbose:
153 print "[dom0] Waiting %i seconds for domU boot..." % TEST_DOMU_BOOT_DELAY
154 time.sleep(TEST_DOMU_BOOT_DELAY)
156 def timeStamp():
157 name = getTestName()
159 t = time.asctime(time.localtime())
161 print "*** Test %s started at %s %s" % (name, t,
162 time.tzname[time.daylight])
164 if __name__ == "__main__":
166 timeStamp()
168 FAIL("foo")