blob: a39d78f432420361f29164d1e7b41c42178269cc [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 *
apw0a3cc5f2006-10-10 22:54:14 +000025from parallel import *
mblighf4c35322006-03-13 01:01:10 +000026
27class test:
mbligha2508052006-05-28 21:29:53 +000028 def __init__(self, job, bindir, outputdir):
apwa1ef53e2006-04-25 10:10:04 +000029 testname = self.__class__.__name__
30
mblighf4c35322006-03-13 01:01:10 +000031 self.job = job
mbligh4b089662006-06-14 22:34:58 +000032 self.autodir = job.autodir
mbligh5b2f7f12006-05-26 22:11:10 +000033 self.outputdir = outputdir
34 os.mkdir(self.outputdir)
35 self.resultsdir = self.outputdir + "/results"
mbligh89d8ee12006-04-22 16:24:37 +000036 os.mkdir(self.resultsdir)
mbligh5b2f7f12006-05-26 22:11:10 +000037 self.profdir = self.outputdir + "/profiling"
mbligh89d8ee12006-04-22 16:24:37 +000038 os.mkdir(self.profdir)
mbligh5b2f7f12006-05-26 22:11:10 +000039 self.debugdir = self.outputdir + "/debug"
mbligh89d8ee12006-04-22 16:24:37 +000040 os.mkdir(self.debugdir)
apwa1ef53e2006-04-25 10:10:04 +000041
mbligha2508052006-05-28 21:29:53 +000042 self.bindir = bindir
43 self.srcdir = bindir + '/src'
apwa1ef53e2006-04-25 10:10:04 +000044
45 self.tmpdir = job.tmpdir + '/' + testname
apwa1ef53e2006-04-25 10:10:04 +000046 if os.path.exists(self.tmpdir):
47 system('rm -rf ' + self.tmpdir)
48 os.mkdir(self.tmpdir)
49
mbligha9c1f7a2006-06-14 22:45:18 +000050 self.initialize()
mbligha2508052006-05-28 21:29:53 +000051 # compile and install the test, if needed.
mbligh7d2bb222006-05-25 19:14:05 +000052 update_version(self.srcdir, self.version, self.setup)
mblighf4c35322006-03-13 01:01:10 +000053
apw9b634582006-04-22 12:40:39 +000054
mbligha9c1f7a2006-06-14 22:45:18 +000055 def initialize(self):
56 pass
57
58
apw7a0d5782006-04-21 14:21:17 +000059 def setup(self):
60 pass
61
apwf1a81162006-04-25 10:10:29 +000062
63 def record(self, msg):
mbligh5b2f7f12006-05-26 22:11:10 +000064 status = self.outputdir + "/status"
apwf1a81162006-04-25 10:10:29 +000065 fd = file(status, "w")
66 fd.write(msg)
67 fd.close()
68
69 def __exec(self, parameters):
70 try:
mbligh7787ebf2006-08-29 06:11:14 +000071 self.job.stdout.tee_redirect(
72 os.path.join(self.debugdir, 'stdout'))
73 self.job.stderr.tee_redirect(
74 os.path.join(self.debugdir, 'stderr'))
75
mbligh910c40e2006-09-23 14:18:52 +000076 try:
77 os.chdir(self.outputdir)
78 self.execute(*parameters)
79 finally:
80 self.job.stderr.restore()
81 self.job.stdout.restore()
apwf1a81162006-04-25 10:10:29 +000082 except AutotestError:
83 raise
84 except:
85 raise UnhandledError('running test ' + \
86 self.__class__.__name__ + "\n")
87
88 def run(self, testname, parameters):
apw8f4ec982006-04-21 14:21:56 +000089 try:
90 self.__exec(parameters)
91 except Exception, detail:
apwf1a81162006-04-25 10:10:29 +000092 self.record("FAIL " + detail.__str__() + "\n")
apw8f4ec982006-04-21 14:21:56 +000093
apw9b634582006-04-22 12:40:39 +000094 raise
apw8f4ec982006-04-21 14:21:56 +000095 else:
apwf1a81162006-04-25 10:10:29 +000096 self.record("GOOD Completed Successfully\n")
97
98
mbligh12a7df72006-10-06 03:54:33 +000099def testname(url):
100 # Extract the testname from the test url.
101 match = re.match('[^:]+://(.*)/([^/]*)$', url)
102 if not match:
103 return ('', url)
104 (group, filename) = match.groups()
105
106 # Generate the group prefix.
107 gfix = re.compile('\W')
108 group = gfix.sub('_', group)
109
110 # Drop the extension to get the raw test name.
111 tfix = re.compile('\.tgz')
112 testname = tfix.sub('', filename)
113
114 return (group, testname)
115
116def __installtest(job, url):
117 (group, name) = testname(url)
118
119 ##print "group=%s name=%s" % (group, name)
120
121 # Bail if the test is already installed
122 group_dir = os.path.join(job.testdir, "download", group)
123 if os.path.exists(os.path.join(group_dir, name)):
124 return (group, name)
125
126 # If the group directory is missing create it and add
127 # an empty __init__.py so that sub-directories are
128 # considered for import.
129 if not os.path.exists(group_dir):
130 os.mkdir(group_dir)
131 f = file(os.path.join(group_dir, '__init__.py'), 'w+')
132 f.close()
133
134 print name + ": installing test url=" + url
135 system("wget %s -O %s" % (url, os.path.join(group_dir, 'test.tgz')))
136 system("cd %s; tar zxf %s" % (group_dir, 'test.tgz'))
137 os.unlink(os.path.join(group_dir, 'test.tgz'))
138
139 # For this 'sub-object' to be importable via the name
140 # 'group.name' we need to provide an __init__.py,
141 # so link the main entry point to this.
142 os.symlink(name + '.py', os.path.join(group_dir, name,
143 '__init__.py'))
144
145 # The test is now installed.
146 return (group, name)
apw7477d9e2006-04-25 10:08:59 +0000147
148# runtest: main interface for importing and instantiating new tests.
mbligh12a7df72006-10-06 03:54:33 +0000149def __runtest(job, tag, url, test_args):
150 # If this is not a plain test name then download and install
151 # the specified test.
152 if is_url(url):
153 (group, testname) = __installtest(job, url)
154 bindir = os.path.join(job.testdir, "download", group, testname)
155 else:
156 (group, testname) = ('', url)
157 bindir = os.path.join(job.testdir, group, testname)
158
159 outputdir = os.path.join(job.resultdir, testname)
160
mbligh7880b1b2006-05-07 16:57:50 +0000161 if (tag):
mbligh5b2f7f12006-05-26 22:11:10 +0000162 outputdir += '.' + tag
163 if not os.path.exists(bindir):
apw3b36f092006-04-25 10:09:26 +0000164 raise TestError(testname + ": test does not exist")
165
166 try:
mbligh12a7df72006-10-06 03:54:33 +0000167 if group:
168 sys.path.insert(0, os.path.join(job.testdir,
169 "download"))
170 group += '.'
171 else:
172 sys.path.insert(0, os.path.join(job.testdir, testname))
mbligh9eb80ba2006-05-26 22:00:04 +0000173
mbligh12a7df72006-10-06 03:54:33 +0000174 exec "import %s%s" % (group, testname)
175 exec "mytest = %s%s.%s(job, bindir, outputdir)" % \
176 (group, testname, testname)
apw3b36f092006-04-25 10:09:26 +0000177 finally:
178 sys.path.pop(0)
apwf1a81162006-04-25 10:10:29 +0000179
mbligh9eb80ba2006-05-26 22:00:04 +0000180 pwd = os.getcwd()
mbligh5b2f7f12006-05-26 22:11:10 +0000181 os.chdir(outputdir)
apwf1a81162006-04-25 10:10:29 +0000182 mytest.run(testname, test_args)
mbligh9eb80ba2006-05-26 22:00:04 +0000183 os.chdir(pwd)
apwf1a81162006-04-25 10:10:29 +0000184
185
mbligh12a7df72006-10-06 03:54:33 +0000186def runtest(job, tag, url, test_args):
187 ##__runtest(job, tag, url, test_args)
apw0a3cc5f2006-10-10 22:54:14 +0000188 pid = fork_start(job.resultdir,
189 lambda : __runtest(job, tag, url, test_args))
190 fork_waitfor(job.resultdir, pid)