blob: f0959533bb466b3064a16c5f331c1fe011c279b1 [file] [log] [blame]
mbligha2508052006-05-28 21:29:53 +00001# Copyright Martin J. Bligh, Andy Whitcroft, 2006
2#
3# Shell class for a test, inherited by all individual tests
4#
5# Methods:
6# __init__ initialise
mbligha9c1f7a2006-06-14 22:45:18 +00007# initialize run once for each job
mbligha2508052006-05-28 21:29:53 +00008# setup run once for each new version of the test installed
9# record record an entry in the status file
10# run run the test (wrapped by job.runtest())
11#
12# Data:
13# job backreference to the job this test instance is part of
14# outputdir eg. results/<job>/<testname.tag>
15# resultsdir eg. results/<job>/<testname.tag>/results
16# profdir eg. results/<job>/<testname.tag>/profiling
17# debugdir eg. results/<job>/<testname.tag>/debug
18# bindir eg. tests/<test>
19# src eg. tests/<test>/src
20# tmpdir eg. tmp/<test>
21
apw87f37ed2006-04-03 17:05:00 +000022import os, pickle, tempfile
mblighf4c35322006-03-13 01:01:10 +000023from autotest_utils import *
apwfd2baa92006-04-04 08:48:59 +000024from error import *
mblighf4c35322006-03-13 01:01:10 +000025
26class test:
mbligha2508052006-05-28 21:29:53 +000027 def __init__(self, job, bindir, outputdir):
apwa1ef53e2006-04-25 10:10:04 +000028 testname = self.__class__.__name__
29
mblighf4c35322006-03-13 01:01:10 +000030 self.job = job
mbligh4b089662006-06-14 22:34:58 +000031 self.autodir = job.autodir
mbligh5b2f7f12006-05-26 22:11:10 +000032 self.outputdir = outputdir
33 os.mkdir(self.outputdir)
34 self.resultsdir = self.outputdir + "/results"
mbligh89d8ee12006-04-22 16:24:37 +000035 os.mkdir(self.resultsdir)
mbligh5b2f7f12006-05-26 22:11:10 +000036 self.profdir = self.outputdir + "/profiling"
mbligh89d8ee12006-04-22 16:24:37 +000037 os.mkdir(self.profdir)
mbligh5b2f7f12006-05-26 22:11:10 +000038 self.debugdir = self.outputdir + "/debug"
mbligh89d8ee12006-04-22 16:24:37 +000039 os.mkdir(self.debugdir)
apwa1ef53e2006-04-25 10:10:04 +000040
mbligha2508052006-05-28 21:29:53 +000041 self.bindir = bindir
42 self.srcdir = bindir + '/src'
apwa1ef53e2006-04-25 10:10:04 +000043
44 self.tmpdir = job.tmpdir + '/' + testname
apwa1ef53e2006-04-25 10:10:04 +000045 if os.path.exists(self.tmpdir):
46 system('rm -rf ' + self.tmpdir)
47 os.mkdir(self.tmpdir)
48
mbligha9c1f7a2006-06-14 22:45:18 +000049 self.initialize()
mbligha2508052006-05-28 21:29:53 +000050 # compile and install the test, if needed.
mbligh7d2bb222006-05-25 19:14:05 +000051 update_version(self.srcdir, self.version, self.setup)
mblighf4c35322006-03-13 01:01:10 +000052
apw9b634582006-04-22 12:40:39 +000053
mbligha9c1f7a2006-06-14 22:45:18 +000054 def initialize(self):
55 pass
56
57
apw7a0d5782006-04-21 14:21:17 +000058 def setup(self):
59 pass
60
apwf1a81162006-04-25 10:10:29 +000061
62 def record(self, msg):
mbligh5b2f7f12006-05-26 22:11:10 +000063 status = self.outputdir + "/status"
apwf1a81162006-04-25 10:10:29 +000064 fd = file(status, "w")
65 fd.write(msg)
66 fd.close()
67
68 def __exec(self, parameters):
69 try:
mbligh5b2f7f12006-05-26 22:11:10 +000070 os.chdir(self.outputdir)
apwf1a81162006-04-25 10:10:29 +000071 self.execute(*parameters)
72 except AutotestError:
73 raise
74 except:
75 raise UnhandledError('running test ' + \
76 self.__class__.__name__ + "\n")
77
78 def run(self, testname, parameters):
apw8f4ec982006-04-21 14:21:56 +000079 try:
80 self.__exec(parameters)
81 except Exception, detail:
apwf1a81162006-04-25 10:10:29 +000082 self.record("FAIL " + detail.__str__() + "\n")
apw8f4ec982006-04-21 14:21:56 +000083
apw9b634582006-04-22 12:40:39 +000084 raise
apw8f4ec982006-04-21 14:21:56 +000085 else:
apwf1a81162006-04-25 10:10:29 +000086 self.record("GOOD Completed Successfully\n")
87
88
89def fork_lambda(tmp, l):
90 sys.stdout.flush()
91 sys.stderr.flush()
92 pid = os.fork()
93 if pid: # parent
94 (pid, status) = os.waitpid (pid,0)
95
96 ename = tmp + "/debug/error-%d" % pid
97 if (os.path.exists(ename)):
98 fd = file(ename, 'r')
99 err = pickle.load(fd)
apw8f4ec982006-04-21 14:21:56 +0000100 fd.close()
apw7477d9e2006-04-25 10:08:59 +0000101
apwf1a81162006-04-25 10:10:29 +0000102 raise err
103
104 if (status != 0):
105 raise TestError("test failed rc=%d" % (status))
106
107 else: # child
108 try:
109 try:
110 l()
111
112 except AutotestError:
113 raise
114
115 except:
116 raise UnhandledError("test failed and threw:\n")
117
118 except Exception, detail:
119 ename = tmp + "/debug/error-%d" % (
120 os.getpid())
121 pickle.dump(detail, open(ename, "w"))
122
123 sys.stdout.flush()
124 sys.stderr.flush()
125 os._exit(1)
126
127 sys.stdout.flush()
128 sys.stderr.flush()
129 os._exit(0)
apw7477d9e2006-04-25 10:08:59 +0000130
131# runtest: main interface for importing and instantiating new tests.
mbligh9eb80ba2006-05-26 22:00:04 +0000132def __runtest(job, tag, testname, test_args):
mbligh5b2f7f12006-05-26 22:11:10 +0000133 bindir = job.testdir + '/' + testname
134 outputdir = job.resultdir + '/' + testname
mbligh7880b1b2006-05-07 16:57:50 +0000135 if (tag):
mbligh5b2f7f12006-05-26 22:11:10 +0000136 outputdir += '.' + tag
137 if not os.path.exists(bindir):
apw3b36f092006-04-25 10:09:26 +0000138 raise TestError(testname + ": test does not exist")
139
140 try:
mbligh5b2f7f12006-05-26 22:11:10 +0000141 sys.path.insert(0, bindir)
mbligh9eb80ba2006-05-26 22:00:04 +0000142
143 exec "import %s" % (testname)
mbligha2508052006-05-28 21:29:53 +0000144 exec "mytest = %s.%s(job, bindir, outputdir)" % \
apw3b36f092006-04-25 10:09:26 +0000145 (testname, testname)
apw3b36f092006-04-25 10:09:26 +0000146 finally:
147 sys.path.pop(0)
apwf1a81162006-04-25 10:10:29 +0000148
mbligh9eb80ba2006-05-26 22:00:04 +0000149 pwd = os.getcwd()
mbligh5b2f7f12006-05-26 22:11:10 +0000150 os.chdir(outputdir)
apwf1a81162006-04-25 10:10:29 +0000151 mytest.run(testname, test_args)
mbligh9eb80ba2006-05-26 22:00:04 +0000152 os.chdir(pwd)
apwf1a81162006-04-25 10:10:29 +0000153
154
mbligh9eb80ba2006-05-26 22:00:04 +0000155def runtest(job, tag, testname, test_args):
156 ##__runtest(job, tag, testname, test_args)
157 fork_lambda(job.resultdir,
158 lambda : __runtest(job, tag, testname, test_args))