mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 1 | """The main job wrapper |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 2 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 3 | This is the core infrastructure. |
| 4 | """ |
| 5 | |
| 6 | __author__ = """Copyright Andy Whitcroft, Martin J. Bligh 2006""" |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 7 | |
mbligh | 8f243ec | 2006-10-10 05:55:49 +0000 | [diff] [blame] | 8 | # standard stuff |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 9 | import os, sys, re, pickle, shutil |
mbligh | 8f243ec | 2006-10-10 05:55:49 +0000 | [diff] [blame] | 10 | # autotest stuff |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 11 | from autotest_utils import * |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 12 | from parallel import * |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 13 | import kernel, xen, test, profilers, barrier, filesystem, fd_stack, boottool |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 14 | import harness, config |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 15 | |
| 16 | class job: |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 17 | """The actual job against which we do everything. |
| 18 | |
| 19 | Properties: |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 20 | autodir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 21 | The top level autotest directory (/usr/local/autotest). |
| 22 | Comes from os.environ['AUTODIR']. |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 23 | bindir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 24 | <autodir>/bin/ |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 25 | testdir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 26 | <autodir>/tests/ |
| 27 | profdir |
| 28 | <autodir>/profilers/ |
| 29 | tmpdir |
| 30 | <autodir>/tmp/ |
| 31 | resultdir |
| 32 | <autodir>/results/<jobtag> |
| 33 | stdout |
| 34 | fd_stack object for stdout |
| 35 | stderr |
| 36 | fd_stack object for stderr |
| 37 | profilers |
| 38 | the profilers object for this job |
apw | 504a7dd | 2006-10-12 17:18:37 +0000 | [diff] [blame] | 39 | harness |
| 40 | the server harness object for this job |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 41 | config |
| 42 | the job configuration for this job |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 43 | """ |
| 44 | |
mbligh | 362ab3d | 2007-08-30 11:24:04 +0000 | [diff] [blame] | 45 | def __init__(self, control, jobtag, cont, harness_type=None): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 46 | """ |
| 47 | control |
| 48 | The control file (pathname of) |
| 49 | jobtag |
| 50 | The job tag string (eg "default") |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 51 | cont |
| 52 | If this is the continuation of this job |
apw | e68a713 | 2006-12-01 11:21:37 +0000 | [diff] [blame] | 53 | harness_type |
| 54 | An alternative server harness |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 55 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 56 | self.autodir = os.environ['AUTODIR'] |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame^] | 57 | self.bindir = os.path.join(self.autodir, 'bin') |
| 58 | self.testdir = os.path.join(self.autodir, 'tests') |
| 59 | self.profdir = os.path.join(self.autodir, 'profilers') |
| 60 | self.tmpdir = os.path.join(self.autodir, 'tmp') |
| 61 | self.resultdir = os.path.join(self.autodir, 'results', jobtag) |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 62 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 63 | if not cont: |
| 64 | if os.path.exists(self.tmpdir): |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 65 | system('umount -f %s > /dev/null 2> /dev/null'%\ |
| 66 | self.tmpdir, ignorestatus=True) |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 67 | system('rm -rf ' + self.tmpdir) |
| 68 | os.mkdir(self.tmpdir) |
| 69 | |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame^] | 70 | results = os.path.join(self.autodir, 'results') |
| 71 | if not os.path.exists(results): |
| 72 | os.mkdir(results) |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 73 | |
apw | f3d2862 | 2007-09-25 16:49:17 +0000 | [diff] [blame] | 74 | download = os.path.join(self.testdir, 'download') |
| 75 | if os.path.exists(download): |
| 76 | system('rm -rf ' + download) |
| 77 | os.mkdir(download) |
| 78 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 79 | if os.path.exists(self.resultdir): |
| 80 | system('rm -rf ' + self.resultdir) |
| 81 | os.mkdir(self.resultdir) |
| 82 | |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame^] | 83 | os.mkdir(os.path.join(self.resultdir, 'debug')) |
| 84 | os.mkdir(os.path.join(self.resultdir, 'analysis')) |
| 85 | os.mkdir(os.path.join(self.resultdir, 'sysinfo')) |
| 86 | |
| 87 | shutil.copyfile(control, os.path.join(self.resultdir, 'control')) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 88 | |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 89 | self.control = control |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 90 | self.jobtab = jobtag |
| 91 | |
mbligh | 56f1fbb | 2006-10-01 15:10:56 +0000 | [diff] [blame] | 92 | self.stdout = fd_stack.fd_stack(1, sys.stdout) |
| 93 | self.stderr = fd_stack.fd_stack(2, sys.stderr) |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 94 | self.record_prefix = '' |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 95 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 96 | self.config = config.config(self) |
| 97 | |
apw | d27e55f | 2006-12-01 11:22:08 +0000 | [diff] [blame] | 98 | self.harness = harness.select(harness_type, self) |
| 99 | |
mbligh | a35553b | 2006-04-23 15:52:25 +0000 | [diff] [blame] | 100 | self.profilers = profilers.profilers(self) |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 101 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 102 | try: |
apw | 90154af | 2006-12-01 11:23:36 +0000 | [diff] [blame] | 103 | tool = self.config_get('boottool.executable') |
| 104 | self.bootloader = boottool.boottool(tool) |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 105 | except: |
| 106 | pass |
| 107 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 108 | pwd = os.getcwd() |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame^] | 109 | os.chdir(os.path.join(self.resultdir, 'sysinfo')) |
| 110 | system(os.path.join(self.bindir, 'sysinfo.py')) |
mbligh | e21abaf | 2007-07-26 16:17:30 +0000 | [diff] [blame] | 111 | system('dmesg -c > dmesg', ignorestatus=1) |
mbligh | 0674377 | 2006-05-18 21:30:19 +0000 | [diff] [blame] | 112 | os.chdir(pwd) |
mbligh | 3a6d6ca | 2006-04-23 15:50:24 +0000 | [diff] [blame] | 113 | |
apw | 357f50f | 2006-12-01 11:22:39 +0000 | [diff] [blame] | 114 | self.harness.run_start() |
| 115 | |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 116 | |
| 117 | def relative_path(self, path): |
| 118 | """\ |
| 119 | Return a patch relative to the job results directory |
| 120 | """ |
mbligh | 1c250ca | 2007-08-30 16:31:38 +0000 | [diff] [blame] | 121 | head = len(self.resultdir) + 1 # remove the / inbetween |
| 122 | return path[head:] |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 123 | |
| 124 | |
mbligh | 362ab3d | 2007-08-30 11:24:04 +0000 | [diff] [blame] | 125 | def control_get(self): |
| 126 | return self.control |
| 127 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 128 | |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 129 | def harness_select(self, which): |
| 130 | self.harness = harness.select(which, self) |
| 131 | |
| 132 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 133 | def config_set(self, name, value): |
| 134 | self.config.set(name, value) |
| 135 | |
| 136 | |
| 137 | def config_get(self, name): |
| 138 | return self.config.get(name) |
| 139 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 140 | def setup_dirs(self, results_dir, tmp_dir): |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 141 | if not tmp_dir: |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame^] | 142 | tmp_dir = os.path.join(self.tmpdir, 'build') |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 143 | if not os.path.exists(tmp_dir): |
| 144 | os.mkdir(tmp_dir) |
| 145 | if not os.path.isdir(tmp_dir): |
| 146 | raise "Temp dir (%s) is not a dir - args backwards?" \ |
| 147 | % self.tmpdir |
| 148 | |
| 149 | # We label the first build "build" and then subsequent ones |
| 150 | # as "build.2", "build.3", etc. Whilst this is a little bit |
| 151 | # inconsistent, 99.9% of jobs will only have one build |
| 152 | # (that's not done as kernbench, sparse, or buildtest), |
| 153 | # so it works out much cleaner. One of life's comprimises. |
| 154 | if not results_dir: |
| 155 | results_dir = os.path.join(self.resultdir, 'build') |
| 156 | i = 2 |
| 157 | while os.path.exists(results_dir): |
| 158 | results_dir = os.path.join(self.resultdir, 'build.%d' % i) |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 159 | i += 1 |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 160 | if not os.path.exists(results_dir): |
| 161 | os.mkdir(results_dir) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 162 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 163 | return (results_dir, tmp_dir) |
| 164 | |
| 165 | |
| 166 | def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \ |
| 167 | kjob = None ): |
| 168 | """Summon a xen object""" |
| 169 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
| 170 | build_dir = 'xen' |
| 171 | return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob) |
| 172 | |
| 173 | |
| 174 | def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False): |
| 175 | """Summon a kernel object""" |
| 176 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
| 177 | build_dir = 'linux' |
| 178 | return kernel.kernel(self, base_tree, results_dir, tmp_dir, build_dir, leave) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 179 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 180 | |
mbligh | fadca20 | 2006-09-23 04:40:01 +0000 | [diff] [blame] | 181 | def barrier(self, *args): |
| 182 | """Create a barrier object""" |
| 183 | return barrier.barrier(*args) |
| 184 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 185 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 186 | def setup_dep(self, deps): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 187 | """Set up the dependencies for this test. |
| 188 | |
| 189 | deps is a list of libraries required for this test. |
| 190 | """ |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 191 | for dep in deps: |
| 192 | try: |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame^] | 193 | os.chdir(os.path.join(self.autodir, 'deps', dep)) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 194 | system('./' + dep + '.py') |
| 195 | except: |
| 196 | error = "setting up dependency " + dep + "\n" |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 197 | raise UnhandledError(error) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 198 | |
| 199 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 200 | def __runtest(self, url, tag, args, dargs): |
| 201 | try: |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 202 | test.runtest(self, url, tag, args, dargs) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 203 | except AutotestError: |
| 204 | raise |
| 205 | except: |
| 206 | raise UnhandledError('running test ' + \ |
| 207 | self.__class__.__name__ + "\n") |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 208 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 209 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 210 | def runtest(self, tag, url, *args): |
| 211 | raise "Deprecated call to job.runtest. Use run_test instead" |
| 212 | |
| 213 | |
| 214 | def run_test(self, url, *args, **dargs): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 215 | """Summon a test object and run it. |
| 216 | |
| 217 | tag |
| 218 | tag to add to testname |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 219 | url |
| 220 | url of the test to run |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 221 | """ |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 222 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 223 | if not url: |
| 224 | raise "Test name is invalid. Switched arguments?" |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 225 | (group, testname) = test.testname(url) |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 226 | tag = None |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 227 | subdir = testname |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 228 | if dargs.has_key('tag'): |
| 229 | tag = dargs['tag'] |
| 230 | del dargs['tag'] |
apw | 5a7335c | 2007-03-12 20:32:40 +0000 | [diff] [blame] | 231 | if tag: |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 232 | subdir += '.' + tag |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 233 | try: |
| 234 | try: |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 235 | self.__runtest(url, tag, args, dargs) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 236 | except Exception, detail: |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 237 | self.record('FAIL', subdir, testname, \ |
| 238 | detail.__str__()) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 239 | |
| 240 | raise |
| 241 | else: |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 242 | self.record('GOOD', subdir, testname, \ |
| 243 | 'completed successfully') |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 244 | except TestError: |
| 245 | return 0 |
| 246 | except: |
| 247 | raise |
| 248 | else: |
| 249 | return 1 |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 250 | |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 251 | |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 252 | def run_group(self, function, *args): |
| 253 | """\ |
| 254 | function: |
| 255 | subroutine to run |
| 256 | *args: |
| 257 | arguments for the function |
| 258 | """ |
| 259 | |
| 260 | name = function.__name__ |
| 261 | # if tag: |
| 262 | # name += '.' + tag |
| 263 | old_record_prefix = self.record_prefix |
| 264 | try: |
| 265 | try: |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 266 | self.record('START', None, name) |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 267 | self.record_prefix += '\t' |
| 268 | function(*args) |
| 269 | self.record_prefix = old_record_prefix |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 270 | self.record('END GOOD', None, name) |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 271 | except: |
| 272 | self.record_prefix = old_record_prefix |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 273 | self.record('END FAIL', None, name, format_error()) |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 274 | # We don't want to raise up an error higher if it's just |
| 275 | # a TestError - we want to carry on to other tests. Hence |
| 276 | # this outer try/except block. |
| 277 | except TestError: |
| 278 | pass |
| 279 | except: |
| 280 | raise TestError(name + ' failed\n' + format_error()) |
| 281 | |
| 282 | |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 283 | def filesystem(self, device, mountpoint = None, loop_size = 0): |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 284 | if not mountpoint: |
| 285 | mountpoint = self.tmpdir |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 286 | return filesystem.filesystem(self, device, mountpoint,loop_size) |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 287 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 288 | |
| 289 | def reboot(self, tag='autotest'): |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 290 | self.harness.run_reboot() |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 291 | self.bootloader.boot_once(tag) |
| 292 | system("reboot") |
apw | 0778a2f | 2006-10-06 03:11:40 +0000 | [diff] [blame] | 293 | self.quit() |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 294 | |
| 295 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 296 | def noop(self, text): |
| 297 | print "job: noop: " + text |
| 298 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 299 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 300 | # Job control primatives. |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 301 | |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 302 | def __parallel_execute(self, func, *args): |
| 303 | func(*args) |
| 304 | |
| 305 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 306 | def parallel(self, *tasklist): |
| 307 | """Run tasks in parallel""" |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 308 | |
| 309 | pids = [] |
| 310 | for task in tasklist: |
| 311 | pids.append(fork_start(self.resultdir, |
| 312 | lambda: self.__parallel_execute(*task))) |
| 313 | for pid in pids: |
| 314 | fork_waitfor(self.resultdir, pid) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 315 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 316 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 317 | def quit(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 318 | # XXX: should have a better name. |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 319 | self.harness.run_pause() |
apw | f2c6660 | 2006-04-27 14:11:25 +0000 | [diff] [blame] | 320 | raise JobContinue("more to come") |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 321 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 322 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 323 | def complete(self, status): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 324 | """Clean up and exit""" |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 325 | # We are about to exit 'complete' so clean up the control file. |
| 326 | try: |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 327 | os.unlink(self.control + '.state') |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 328 | except: |
| 329 | pass |
mbligh | 61a6c1a | 2006-12-25 01:26:38 +0000 | [diff] [blame] | 330 | self.harness.run_complete() |
apw | 1b02190 | 2006-04-03 17:02:56 +0000 | [diff] [blame] | 331 | sys.exit(status) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 332 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 333 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 334 | steps = [] |
| 335 | def next_step(self, step): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 336 | """Define the next step""" |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 337 | step[0] = step[0].__name__ |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 338 | self.steps.append(step) |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 339 | pickle.dump(self.steps, open(self.control + '.state', 'w')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 340 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 341 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 342 | def next_step_prepend(self, step): |
| 343 | """Insert a new step, executing first""" |
| 344 | step[0] = step[0].__name__ |
| 345 | self.steps.insert(0, step) |
| 346 | pickle.dump(self.steps, open(self.control + '.state', 'w')) |
| 347 | |
| 348 | |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 349 | def step_engine(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 350 | """the stepping engine -- if the control file defines |
| 351 | step_init we will be using this engine to drive multiple runs. |
| 352 | """ |
| 353 | """Do the next step""" |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 354 | lcl = dict({'job': self}) |
| 355 | |
| 356 | str = """ |
| 357 | from error import * |
| 358 | from autotest_utils import * |
| 359 | """ |
| 360 | exec(str, lcl, lcl) |
| 361 | execfile(self.control, lcl, lcl) |
| 362 | |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 363 | state = self.control + '.state' |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 364 | # If there is a mid-job state file load that in and continue |
| 365 | # where it indicates. Otherwise start stepping at the passed |
| 366 | # entry. |
| 367 | try: |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 368 | self.steps = pickle.load(open(state, 'r')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 369 | except: |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 370 | if lcl.has_key('step_init'): |
| 371 | self.next_step([lcl['step_init']]) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 372 | |
| 373 | # Run the step list. |
| 374 | while len(self.steps) > 0: |
apw | fd922bb | 2006-04-04 07:47:00 +0000 | [diff] [blame] | 375 | step = self.steps.pop(0) |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 376 | pickle.dump(self.steps, open(state, 'w')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 377 | |
| 378 | cmd = step.pop(0) |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 379 | cmd = lcl[cmd] |
| 380 | lcl['__cmd'] = cmd |
| 381 | lcl['__args'] = step |
| 382 | exec("__cmd(*__args)", lcl, lcl) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 383 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 384 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 385 | def record(self, status_code, subdir, operation, status = ''): |
| 386 | """ |
| 387 | Record job-level status |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 388 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 389 | The intent is to make this file both machine parseable and |
| 390 | human readable. That involves a little more complexity, but |
| 391 | really isn't all that bad ;-) |
| 392 | |
| 393 | Format is <status code>\t<subdir>\t<operation>\t<status> |
| 394 | |
| 395 | status code: (GOOD|WARN|FAIL|ABORT) |
| 396 | or START |
| 397 | or END (GOOD|WARN|FAIL|ABORT) |
| 398 | |
| 399 | subdir: MUST be a relevant subdirectory in the results, |
| 400 | or None, which will be represented as '----' |
| 401 | |
| 402 | operation: description of what you ran (e.g. "dbench", or |
| 403 | "mkfs -t foobar /dev/sda9") |
| 404 | |
| 405 | status: error message or "completed sucessfully" |
| 406 | |
| 407 | ------------------------------------------------------------ |
| 408 | |
| 409 | Initial tabs indicate indent levels for grouping, and is |
| 410 | governed by self.record_prefix |
| 411 | |
| 412 | multiline messages have secondary lines prefaced by a double |
| 413 | space (' ') |
| 414 | """ |
| 415 | |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 416 | if subdir: |
| 417 | if re.match(r'[\n\t]', subdir): |
| 418 | raise "Invalid character in subdir string" |
| 419 | substr = subdir |
| 420 | else: |
| 421 | substr = '----' |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 422 | |
| 423 | if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \ |
| 424 | status_code): |
| 425 | raise "Invalid status code supplied: %s" % status_code |
| 426 | if re.match(r'[\n\t]', operation): |
| 427 | raise "Invalid character in operation string" |
| 428 | operation = operation.rstrip() |
| 429 | status = status.rstrip() |
| 430 | status = re.sub(r"\t", " ", status) |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 431 | # Ensure any continuation lines are marked so we can |
| 432 | # detect them in the status file to ensure it is parsable. |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 433 | status = re.sub(r"\n", "\n" + self.record_prefix + " ", status) |
| 434 | |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 435 | msg = '%s\t%s\t%s\t%s' %(status_code, substr, operation, status) |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 436 | |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 437 | self.harness.test_status(msg) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 438 | print msg |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 439 | status_file = os.path.join(self.resultdir, 'status') |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 440 | open(status_file, "a").write(self.record_prefix + msg + "\n") |
| 441 | if subdir: |
| 442 | status_file = os.path.join(self.resultdir, subdir, 'status') |
| 443 | open(status_file, "a").write(msg + "\n") |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 444 | |
| 445 | |
mbligh | 570e93e | 2006-11-26 05:15:56 +0000 | [diff] [blame] | 446 | def runjob(control, cont = False, tag = "default", harness_type = ''): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 447 | """The main interface to this module |
| 448 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 449 | control |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 450 | The control file to use for this job. |
| 451 | cont |
| 452 | Whether this is the continuation of a previously started job |
| 453 | """ |
mbligh | b4eef24 | 2007-07-23 18:22:49 +0000 | [diff] [blame] | 454 | control = os.path.abspath(control) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 455 | state = control + '.state' |
| 456 | |
| 457 | # instantiate the job object ready for the control file. |
| 458 | myjob = None |
| 459 | try: |
| 460 | # Check that the control file is valid |
| 461 | if not os.path.exists(control): |
| 462 | raise JobError(control + ": control file not found") |
| 463 | |
| 464 | # When continuing, the job is complete when there is no |
| 465 | # state file, ensure we don't try and continue. |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 466 | if cont and not os.path.exists(state): |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 467 | sys.exit(1) |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 468 | if cont == False and os.path.exists(state): |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 469 | os.unlink(state) |
| 470 | |
mbligh | 570e93e | 2006-11-26 05:15:56 +0000 | [diff] [blame] | 471 | myjob = job(control, tag, cont, harness_type) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 472 | |
| 473 | # Load in the users control file, may do any one of: |
| 474 | # 1) execute in toto |
| 475 | # 2) define steps, and select the first via next_step() |
| 476 | myjob.step_engine() |
| 477 | |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 478 | except JobContinue: |
| 479 | sys.exit(5) |
| 480 | |
| 481 | except JobError, instance: |
| 482 | print "JOB ERROR: " + instance.args[0] |
| 483 | if myjob != None: |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 484 | myjob.record('ABORT', None, instance.args[0]) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 485 | myjob.complete(1) |
| 486 | |
| 487 | except: |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 488 | if myjob: |
| 489 | myjob.harness.run_abort() |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 490 | # Ensure we cannot continue this job, it is in rictus. |
| 491 | if os.path.exists(state): |
| 492 | os.unlink(state) |
| 493 | raise |
mbligh | 892d37f | 2007-03-01 17:03:25 +0000 | [diff] [blame] | 494 | |
| 495 | # If we get here, then we assume the job is complete and good. |
| 496 | myjob.complete(0) |
| 497 | |