mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 1 | # 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 |
mbligh | a9c1f7a | 2006-06-14 22:45:18 +0000 | [diff] [blame] | 7 | # initialize run once for each job |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 8 | # 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 | |
apw | 87f37ed | 2006-04-03 17:05:00 +0000 | [diff] [blame] | 22 | import os, pickle, tempfile |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 23 | from autotest_utils import * |
apw | fd2baa9 | 2006-04-04 08:48:59 +0000 | [diff] [blame] | 24 | from error import * |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 25 | |
| 26 | class test: |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 27 | def __init__(self, job, bindir, outputdir): |
apw | a1ef53e | 2006-04-25 10:10:04 +0000 | [diff] [blame] | 28 | testname = self.__class__.__name__ |
| 29 | |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 30 | self.job = job |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 31 | self.autodir = job.autodir |
mbligh | 5b2f7f1 | 2006-05-26 22:11:10 +0000 | [diff] [blame] | 32 | self.outputdir = outputdir |
| 33 | os.mkdir(self.outputdir) |
| 34 | self.resultsdir = self.outputdir + "/results" |
mbligh | 89d8ee1 | 2006-04-22 16:24:37 +0000 | [diff] [blame] | 35 | os.mkdir(self.resultsdir) |
mbligh | 5b2f7f1 | 2006-05-26 22:11:10 +0000 | [diff] [blame] | 36 | self.profdir = self.outputdir + "/profiling" |
mbligh | 89d8ee1 | 2006-04-22 16:24:37 +0000 | [diff] [blame] | 37 | os.mkdir(self.profdir) |
mbligh | 5b2f7f1 | 2006-05-26 22:11:10 +0000 | [diff] [blame] | 38 | self.debugdir = self.outputdir + "/debug" |
mbligh | 89d8ee1 | 2006-04-22 16:24:37 +0000 | [diff] [blame] | 39 | os.mkdir(self.debugdir) |
apw | a1ef53e | 2006-04-25 10:10:04 +0000 | [diff] [blame] | 40 | |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 41 | self.bindir = bindir |
| 42 | self.srcdir = bindir + '/src' |
apw | a1ef53e | 2006-04-25 10:10:04 +0000 | [diff] [blame] | 43 | |
| 44 | self.tmpdir = job.tmpdir + '/' + testname |
apw | a1ef53e | 2006-04-25 10:10:04 +0000 | [diff] [blame] | 45 | if os.path.exists(self.tmpdir): |
| 46 | system('rm -rf ' + self.tmpdir) |
| 47 | os.mkdir(self.tmpdir) |
| 48 | |
mbligh | a9c1f7a | 2006-06-14 22:45:18 +0000 | [diff] [blame] | 49 | self.initialize() |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 50 | # compile and install the test, if needed. |
mbligh | 7d2bb22 | 2006-05-25 19:14:05 +0000 | [diff] [blame] | 51 | update_version(self.srcdir, self.version, self.setup) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 52 | |
apw | 9b63458 | 2006-04-22 12:40:39 +0000 | [diff] [blame] | 53 | |
mbligh | a9c1f7a | 2006-06-14 22:45:18 +0000 | [diff] [blame] | 54 | def initialize(self): |
| 55 | pass |
| 56 | |
| 57 | |
apw | 7a0d578 | 2006-04-21 14:21:17 +0000 | [diff] [blame] | 58 | def setup(self): |
| 59 | pass |
| 60 | |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 61 | |
| 62 | def record(self, msg): |
mbligh | 5b2f7f1 | 2006-05-26 22:11:10 +0000 | [diff] [blame] | 63 | status = self.outputdir + "/status" |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 64 | fd = file(status, "w") |
| 65 | fd.write(msg) |
| 66 | fd.close() |
| 67 | |
| 68 | def __exec(self, parameters): |
| 69 | try: |
mbligh | 5b2f7f1 | 2006-05-26 22:11:10 +0000 | [diff] [blame] | 70 | os.chdir(self.outputdir) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 71 | 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): |
apw | 8f4ec98 | 2006-04-21 14:21:56 +0000 | [diff] [blame] | 79 | try: |
| 80 | self.__exec(parameters) |
| 81 | except Exception, detail: |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 82 | self.record("FAIL " + detail.__str__() + "\n") |
apw | 8f4ec98 | 2006-04-21 14:21:56 +0000 | [diff] [blame] | 83 | |
apw | 9b63458 | 2006-04-22 12:40:39 +0000 | [diff] [blame] | 84 | raise |
apw | 8f4ec98 | 2006-04-21 14:21:56 +0000 | [diff] [blame] | 85 | else: |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 86 | self.record("GOOD Completed Successfully\n") |
| 87 | |
| 88 | |
| 89 | def 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) |
apw | 8f4ec98 | 2006-04-21 14:21:56 +0000 | [diff] [blame] | 100 | fd.close() |
apw | 7477d9e | 2006-04-25 10:08:59 +0000 | [diff] [blame] | 101 | |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 102 | 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) |
apw | 7477d9e | 2006-04-25 10:08:59 +0000 | [diff] [blame] | 130 | |
| 131 | # runtest: main interface for importing and instantiating new tests. |
mbligh | 9eb80ba | 2006-05-26 22:00:04 +0000 | [diff] [blame] | 132 | def __runtest(job, tag, testname, test_args): |
mbligh | 5b2f7f1 | 2006-05-26 22:11:10 +0000 | [diff] [blame] | 133 | bindir = job.testdir + '/' + testname |
| 134 | outputdir = job.resultdir + '/' + testname |
mbligh | 7880b1b | 2006-05-07 16:57:50 +0000 | [diff] [blame] | 135 | if (tag): |
mbligh | 5b2f7f1 | 2006-05-26 22:11:10 +0000 | [diff] [blame] | 136 | outputdir += '.' + tag |
| 137 | if not os.path.exists(bindir): |
apw | 3b36f09 | 2006-04-25 10:09:26 +0000 | [diff] [blame] | 138 | raise TestError(testname + ": test does not exist") |
| 139 | |
| 140 | try: |
mbligh | 5b2f7f1 | 2006-05-26 22:11:10 +0000 | [diff] [blame] | 141 | sys.path.insert(0, bindir) |
mbligh | 9eb80ba | 2006-05-26 22:00:04 +0000 | [diff] [blame] | 142 | |
| 143 | exec "import %s" % (testname) |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 144 | exec "mytest = %s.%s(job, bindir, outputdir)" % \ |
apw | 3b36f09 | 2006-04-25 10:09:26 +0000 | [diff] [blame] | 145 | (testname, testname) |
apw | 3b36f09 | 2006-04-25 10:09:26 +0000 | [diff] [blame] | 146 | finally: |
| 147 | sys.path.pop(0) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 148 | |
mbligh | 9eb80ba | 2006-05-26 22:00:04 +0000 | [diff] [blame] | 149 | pwd = os.getcwd() |
mbligh | 5b2f7f1 | 2006-05-26 22:11:10 +0000 | [diff] [blame] | 150 | os.chdir(outputdir) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 151 | mytest.run(testname, test_args) |
mbligh | 9eb80ba | 2006-05-26 22:00:04 +0000 | [diff] [blame] | 152 | os.chdir(pwd) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 153 | |
| 154 | |
mbligh | 9eb80ba | 2006-05-26 22:00:04 +0000 | [diff] [blame] | 155 | def 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)) |