blob: ebebe0d6e63f639c28063a519cf3866d5b7e1e7f [file] [log] [blame]
mbligh05269362007-10-16 16:58:11 +00001# Copyright Martin J. Bligh, Andy Whitcroft, 2007
2#
3# Shell class for a test, inherited by all individual tests
4#
5# Methods:
6# __init__ initialise
7# initialize run once for each job
8# setup run once for each new version of the test installed
9# run run the test (wrapped by job.run_test())
10#
11# Data:
12# job backreference to the job this test instance is part of
13# outputdir eg. results/<job>/<testname.tag>
14# resultsdir eg. results/<job>/<testname.tag>/results
15# profdir eg. results/<job>/<testname.tag>/profiling
16# debugdir eg. results/<job>/<testname.tag>/debug
17# bindir eg. tests/<test>
18# src eg. tests/<test>/src
19# tmpdir eg. tmp/<test>
20
21import os, pickle, tempfile, re
22from subcommand import *
mblighf31b0c02007-11-29 18:19:22 +000023from common.error import *
mbligh05269362007-10-16 16:58:11 +000024from utils import *
25
26class test:
27 preserve_srcdir = False
28
29 def __init__(self, job, bindir, outputdir):
30 testname = self.__class__.__name__
31
32 self.job = job
33 self.outputdir = outputdir
34 os.mkdir(self.outputdir)
35 self.resultsdir = self.outputdir + "/results"
36 os.mkdir(self.resultsdir)
37 self.profdir = self.outputdir + "/profiling"
38 os.mkdir(self.profdir)
39 self.debugdir = self.outputdir + "/debug"
40 os.mkdir(self.debugdir)
41
42 self.bindir = bindir
43 self.srcdir = bindir + '/src'
44
45 self.tmpdir = job.tmpdir + '/' + testname
46 if os.path.exists(self.tmpdir):
47 system('rm -rf ' + self.tmpdir)
48 os.mkdir(self.tmpdir)
49
50 self.initialize()
51 # compile and install the test, if needed.
52 update_version(self.srcdir, self.preserve_srcdir, self.version, self.setup)
53
54
55 def initialize(self):
56 pass
57
58
59 def setup(self):
60 pass
61
62
mbligh29d65032007-10-25 15:51:06 +000063 def cleanup(self):
64 pass
65
66
mbligh05269362007-10-16 16:58:11 +000067 def run(self, args, dargs):
68 try:
69 # self.job.stdout.tee_redirect(
70 # os.path.join(self.debugdir, 'stdout'))
71 # self.job.stderr.tee_redirect(
72 # os.path.join(self.debugdir, 'stderr'))
73
mbligh29d65032007-10-25 15:51:06 +000074 try:
75 os.chdir(self.outputdir)
76 write_keyval(self.outputdir, { 'version':self.version })
77 self.execute(*args, **dargs)
78 finally:
79 self.cleanup()
mbligh05269362007-10-16 16:58:11 +000080 # self.job.stderr.restore()
81 # self.job.stdout.restore()
82 except AutotestError:
83 raise
84 except:
85 raise UnhandledError('running test ' + \
86 self.__class__.__name__ + "\n")
87
88
89def testname(url):
90 # Extract the testname from the test url.
91 match = re.match('[^:]+://(.*)/([^/]*)$', url)
92 if not match:
93 return ('', url)
94 (group, filename) = match.groups()
95
96 # Generate the group prefix.
97 gfix = re.compile('\W')
98 group = gfix.sub('_', group)
99
100 # Drop the extension to get the raw test name.
101 tfix = re.compile('\.tgz')
102 testname = tfix.sub('', filename)
103
104 return (group, testname)
105
106
107def __installtest(job, url):
108 (group, name) = testname(url)
109
110 ##print "group=%s name=%s" % (group, name)
111
112 # Bail if the test is already installed
113 group_dir = os.path.join(job.testdir, "download", group)
114 if os.path.exists(os.path.join(group_dir, name)):
115 return (group, name)
116
117 # If the group directory is missing create it and add
118 # an empty __init__.py so that sub-directories are
119 # considered for import.
120 if not os.path.exists(group_dir):
121 os.mkdir(group_dir)
122 f = file(os.path.join(group_dir, '__init__.py'), 'w+')
123 f.close()
124
125 print name + ": installing test url=" + url
126 system("wget %s -O %s" % (url, os.path.join(group_dir, 'test.tgz')))
127 system("cd %s; tar zxf %s" % (group_dir, 'test.tgz'))
128 os.unlink(os.path.join(group_dir, 'test.tgz'))
129
130 # For this 'sub-object' to be importable via the name
131 # 'group.name' we need to provide an __init__.py,
132 # so link the main entry point to this.
133 os.symlink(name + '.py', os.path.join(group_dir, name,
134 '__init__.py'))
135
136 # The test is now installed.
137 return (group, name)
138
139
140# runtest: main interface for importing and instantiating new tests.
141def __runtest(job, url, tag, args, dargs):
142 (group, testname) = ('', url)
143 bindir = os.path.join(job.testdir, group, testname)
144
145 outputdir = os.path.join(job.resultdir, testname)
146 if (tag):
147 outputdir += '.' + tag
148 if os.path.exists(outputdir):
149 system('rm -rf ' + outputdir)
150
151 if not os.path.exists(bindir):
152 raise TestError(testname + ": test does not exist")
153
154 try:
155 if group:
156 sys.path.insert(0, os.path.join(job.testdir,
157 "download"))
158 group += '.'
159 else:
160 sys.path.insert(0, os.path.join(job.testdir, testname))
161
162 exec "import %s%s" % (group, testname)
163 exec "mytest = %s%s.%s(job, bindir, outputdir)" % \
164 (group, testname, testname)
165 finally:
166 sys.path.pop(0)
167
168 pwd = os.getcwd()
169 os.chdir(outputdir)
170 mytest.run(args, dargs)
171 os.chdir(pwd)
172
173
174def runtest(job, url, tag, args, dargs):
175 t = subcommand(__runtest, [job, url, tag, args, dargs])
176 t.fork_start()
177 t.fork_waitfor()