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 |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 9 | import os, sys, re, pickle, shutil, time, traceback |
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 | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 13 | from error import * |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 14 | import kernel, xen, test, profilers, barrier, filesystem, fd_stack, boottool |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 15 | import harness, config |
mbligh | 83ac994 | 2007-11-05 18:59:37 +0000 | [diff] [blame] | 16 | import sysinfo |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 17 | |
| 18 | class job: |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 19 | """The actual job against which we do everything. |
| 20 | |
| 21 | Properties: |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 22 | autodir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 23 | The top level autotest directory (/usr/local/autotest). |
| 24 | Comes from os.environ['AUTODIR']. |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 25 | bindir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 26 | <autodir>/bin/ |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 27 | testdir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 28 | <autodir>/tests/ |
| 29 | profdir |
| 30 | <autodir>/profilers/ |
| 31 | tmpdir |
| 32 | <autodir>/tmp/ |
| 33 | resultdir |
| 34 | <autodir>/results/<jobtag> |
| 35 | stdout |
| 36 | fd_stack object for stdout |
| 37 | stderr |
| 38 | fd_stack object for stderr |
| 39 | profilers |
| 40 | the profilers object for this job |
apw | 504a7dd | 2006-10-12 17:18:37 +0000 | [diff] [blame] | 41 | harness |
| 42 | the server harness object for this job |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 43 | config |
| 44 | the job configuration for this job |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 45 | """ |
| 46 | |
mbligh | 362ab3d | 2007-08-30 11:24:04 +0000 | [diff] [blame] | 47 | def __init__(self, control, jobtag, cont, harness_type=None): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 48 | """ |
| 49 | control |
| 50 | The control file (pathname of) |
| 51 | jobtag |
| 52 | The job tag string (eg "default") |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 53 | cont |
| 54 | If this is the continuation of this job |
apw | e68a713 | 2006-12-01 11:21:37 +0000 | [diff] [blame] | 55 | harness_type |
| 56 | An alternative server harness |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 57 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 58 | self.autodir = os.environ['AUTODIR'] |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 59 | self.bindir = os.path.join(self.autodir, 'bin') |
| 60 | self.testdir = os.path.join(self.autodir, 'tests') |
| 61 | self.profdir = os.path.join(self.autodir, 'profilers') |
| 62 | self.tmpdir = os.path.join(self.autodir, 'tmp') |
| 63 | self.resultdir = os.path.join(self.autodir, 'results', jobtag) |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 64 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 65 | if not cont: |
| 66 | if os.path.exists(self.tmpdir): |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 67 | system('umount -f %s > /dev/null 2> /dev/null'%\ |
| 68 | self.tmpdir, ignorestatus=True) |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 69 | system('rm -rf ' + self.tmpdir) |
| 70 | os.mkdir(self.tmpdir) |
| 71 | |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 72 | results = os.path.join(self.autodir, 'results') |
| 73 | if not os.path.exists(results): |
| 74 | os.mkdir(results) |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 75 | |
apw | f3d2862 | 2007-09-25 16:49:17 +0000 | [diff] [blame] | 76 | download = os.path.join(self.testdir, 'download') |
| 77 | if os.path.exists(download): |
| 78 | system('rm -rf ' + download) |
| 79 | os.mkdir(download) |
| 80 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 81 | if os.path.exists(self.resultdir): |
| 82 | system('rm -rf ' + self.resultdir) |
| 83 | os.mkdir(self.resultdir) |
| 84 | |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 85 | os.mkdir(os.path.join(self.resultdir, 'debug')) |
| 86 | os.mkdir(os.path.join(self.resultdir, 'analysis')) |
| 87 | os.mkdir(os.path.join(self.resultdir, 'sysinfo')) |
| 88 | |
| 89 | shutil.copyfile(control, os.path.join(self.resultdir, 'control')) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 90 | |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 91 | self.control = control |
mbligh | 2711360 | 2007-10-31 21:07:51 +0000 | [diff] [blame] | 92 | self.jobtag = jobtag |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 93 | |
mbligh | 56f1fbb | 2006-10-01 15:10:56 +0000 | [diff] [blame] | 94 | self.stdout = fd_stack.fd_stack(1, sys.stdout) |
| 95 | self.stderr = fd_stack.fd_stack(2, sys.stderr) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 96 | self.group_level = 0 |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 97 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 98 | self.config = config.config(self) |
| 99 | |
apw | d27e55f | 2006-12-01 11:22:08 +0000 | [diff] [blame] | 100 | self.harness = harness.select(harness_type, self) |
| 101 | |
mbligh | a35553b | 2006-04-23 15:52:25 +0000 | [diff] [blame] | 102 | self.profilers = profilers.profilers(self) |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 103 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 104 | try: |
apw | 90154af | 2006-12-01 11:23:36 +0000 | [diff] [blame] | 105 | tool = self.config_get('boottool.executable') |
| 106 | self.bootloader = boottool.boottool(tool) |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 107 | except: |
| 108 | pass |
| 109 | |
mbligh | 83ac994 | 2007-11-05 18:59:37 +0000 | [diff] [blame] | 110 | # log "before each step" sysinfo |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 111 | pwd = os.getcwd() |
mbligh | 83ac994 | 2007-11-05 18:59:37 +0000 | [diff] [blame] | 112 | try: |
| 113 | os.chdir(os.path.join(self.resultdir, 'sysinfo')) |
| 114 | sysinfo.before_each_step() |
| 115 | finally: |
| 116 | os.chdir(pwd) |
mbligh | 3a6d6ca | 2006-04-23 15:50:24 +0000 | [diff] [blame] | 117 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 118 | if not cont: |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 119 | self.record('START', None, None) |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 120 | self.harness.run_start() |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 121 | self.group_level = 1 |
apw | 357f50f | 2006-12-01 11:22:39 +0000 | [diff] [blame] | 122 | |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 123 | |
| 124 | def relative_path(self, path): |
| 125 | """\ |
| 126 | Return a patch relative to the job results directory |
| 127 | """ |
mbligh | 1c250ca | 2007-08-30 16:31:38 +0000 | [diff] [blame] | 128 | head = len(self.resultdir) + 1 # remove the / inbetween |
| 129 | return path[head:] |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 130 | |
| 131 | |
mbligh | 362ab3d | 2007-08-30 11:24:04 +0000 | [diff] [blame] | 132 | def control_get(self): |
| 133 | return self.control |
| 134 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 135 | |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 136 | def harness_select(self, which): |
| 137 | self.harness = harness.select(which, self) |
| 138 | |
| 139 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 140 | def config_set(self, name, value): |
| 141 | self.config.set(name, value) |
| 142 | |
| 143 | |
| 144 | def config_get(self, name): |
| 145 | return self.config.get(name) |
| 146 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 147 | def setup_dirs(self, results_dir, tmp_dir): |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 148 | if not tmp_dir: |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 149 | tmp_dir = os.path.join(self.tmpdir, 'build') |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 150 | if not os.path.exists(tmp_dir): |
| 151 | os.mkdir(tmp_dir) |
| 152 | if not os.path.isdir(tmp_dir): |
| 153 | raise "Temp dir (%s) is not a dir - args backwards?" \ |
| 154 | % self.tmpdir |
| 155 | |
| 156 | # We label the first build "build" and then subsequent ones |
| 157 | # as "build.2", "build.3", etc. Whilst this is a little bit |
| 158 | # inconsistent, 99.9% of jobs will only have one build |
| 159 | # (that's not done as kernbench, sparse, or buildtest), |
| 160 | # so it works out much cleaner. One of life's comprimises. |
| 161 | if not results_dir: |
| 162 | results_dir = os.path.join(self.resultdir, 'build') |
| 163 | i = 2 |
| 164 | while os.path.exists(results_dir): |
| 165 | results_dir = os.path.join(self.resultdir, 'build.%d' % i) |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 166 | i += 1 |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 167 | if not os.path.exists(results_dir): |
| 168 | os.mkdir(results_dir) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 169 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 170 | return (results_dir, tmp_dir) |
| 171 | |
| 172 | |
| 173 | def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \ |
| 174 | kjob = None ): |
| 175 | """Summon a xen object""" |
| 176 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
| 177 | build_dir = 'xen' |
| 178 | return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob) |
| 179 | |
| 180 | |
| 181 | def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False): |
| 182 | """Summon a kernel object""" |
mbligh | 669caa1 | 2007-11-05 18:32:13 +0000 | [diff] [blame] | 183 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 184 | build_dir = 'linux' |
mbligh | 6ee7ee0 | 2007-11-13 23:49:05 +0000 | [diff] [blame] | 185 | return kernel.auto_kernel(self, base_tree, results_dir, |
| 186 | tmp_dir, build_dir, leave) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 187 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 188 | |
mbligh | fadca20 | 2006-09-23 04:40:01 +0000 | [diff] [blame] | 189 | def barrier(self, *args): |
| 190 | """Create a barrier object""" |
| 191 | return barrier.barrier(*args) |
| 192 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 193 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 194 | def setup_dep(self, deps): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 195 | """Set up the dependencies for this test. |
| 196 | |
| 197 | deps is a list of libraries required for this test. |
| 198 | """ |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 199 | for dep in deps: |
| 200 | try: |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 201 | os.chdir(os.path.join(self.autodir, 'deps', dep)) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 202 | system('./' + dep + '.py') |
| 203 | except: |
| 204 | error = "setting up dependency " + dep + "\n" |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 205 | raise UnhandledError(error) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 206 | |
| 207 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 208 | def __runtest(self, url, tag, args, dargs): |
| 209 | try: |
mbligh | 53c4150 | 2007-10-23 20:45:04 +0000 | [diff] [blame] | 210 | l = lambda : test.runtest(self, url, tag, args, dargs) |
| 211 | pid = fork_start(self.resultdir, l) |
| 212 | fork_waitfor(self.resultdir, pid) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 213 | except AutotestError: |
| 214 | raise |
| 215 | except: |
| 216 | raise UnhandledError('running test ' + \ |
| 217 | self.__class__.__name__ + "\n") |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 218 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 219 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 220 | def run_test(self, url, *args, **dargs): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 221 | """Summon a test object and run it. |
| 222 | |
| 223 | tag |
| 224 | tag to add to testname |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 225 | url |
| 226 | url of the test to run |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 227 | """ |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 228 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 229 | if not url: |
| 230 | raise "Test name is invalid. Switched arguments?" |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 231 | (group, testname) = test.testname(url) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 232 | tag = dargs.pop('tag', None) |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 233 | subdir = testname |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 234 | if tag: |
| 235 | subdir += '.' + tag |
| 236 | |
| 237 | def group_func(): |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 238 | try: |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 239 | self.__runtest(url, tag, args, dargs) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 240 | except Exception, detail: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 241 | self.record('FAIL', subdir, testname, |
| 242 | str(detail)) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 243 | raise |
| 244 | else: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 245 | self.record('GOOD', subdir, testname, |
| 246 | 'completed successfully') |
mbligh | cfc6dd3 | 2007-11-20 00:44:35 +0000 | [diff] [blame^] | 247 | result, exc_info = self.__rungroup(subdir, group_func) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 248 | |
| 249 | if exc_info and isinstance(exc_info[1], TestError): |
| 250 | return False |
| 251 | elif exc_info: |
| 252 | raise exc_info[0], exc_info[1], exc_info[2] |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 253 | else: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 254 | return True |
| 255 | |
| 256 | |
| 257 | def __rungroup(self, name, function, *args, **dargs): |
| 258 | """\ |
| 259 | name: |
| 260 | name of the group |
| 261 | function: |
| 262 | subroutine to run |
| 263 | *args: |
| 264 | arguments for the function |
| 265 | |
| 266 | Returns a 2-tuple (result, exc_info) where result |
| 267 | is the return value of function, and exc_info is |
| 268 | the sys.exc_info() of the exception thrown by the |
| 269 | function (which may be None). |
| 270 | """ |
| 271 | |
| 272 | result, exc_info = None, None |
| 273 | try: |
| 274 | self.record('START', None, name) |
| 275 | self.group_level += 1 |
| 276 | result = function(*args, **dargs) |
| 277 | self.group_level -= 1 |
| 278 | self.record('END GOOD', None, name) |
| 279 | except Exception, e: |
| 280 | exc_info = sys.exc_info() |
| 281 | self.group_level -= 1 |
| 282 | self.record('END FAIL', None, name, format_error()) |
| 283 | |
| 284 | return result, exc_info |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 285 | |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 286 | |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 287 | def run_group(self, function, *args, **dargs): |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 288 | """\ |
| 289 | function: |
| 290 | subroutine to run |
| 291 | *args: |
| 292 | arguments for the function |
| 293 | """ |
| 294 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 295 | # Allow the tag for the group to be specified |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 296 | name = function.__name__ |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 297 | tag = dargs.pop('tag', None) |
| 298 | if tag: |
| 299 | name = tag |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 300 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 301 | result, exc_info = self.__rungroup(name, function, |
| 302 | *args, **dargs) |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 303 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 304 | # if there was a non-TestError exception, raise it |
| 305 | if exc_info and isinstance(exc_info[1], TestError): |
| 306 | err = ''.join(traceback.format_exception(*exc_info)) |
| 307 | raise TestError(name + ' failed\n' + err) |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 308 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 309 | # pass back the actual return value from the function |
apw | 08403ca | 2007-09-27 17:17:22 +0000 | [diff] [blame] | 310 | return result |
| 311 | |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 312 | |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 313 | # Check the passed kernel identifier against the command line |
| 314 | # and the running kernel, abort the job on missmatch. |
mbligh | da0311e | 2007-10-25 16:03:33 +0000 | [diff] [blame] | 315 | def kernel_check_ident(self, expected_when, expected_id, expected_cl, subdir, type = 'src'): |
| 316 | print "POST BOOT: checking booted kernel mark=%d identity='%s' changelist=%s type='%s'" \ |
| 317 | % (expected_when, expected_id, expected_cl, type) |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 318 | |
| 319 | running_id = running_os_ident() |
| 320 | |
| 321 | cmdline = read_one_line("/proc/cmdline") |
| 322 | |
| 323 | find_sum = re.compile(r'.*IDENT=(\d+)') |
| 324 | m = find_sum.match(cmdline) |
| 325 | cmdline_when = -1 |
| 326 | if m: |
| 327 | cmdline_when = int(m.groups()[0]) |
| 328 | |
mbligh | da0311e | 2007-10-25 16:03:33 +0000 | [diff] [blame] | 329 | cl_re = re.compile(r'\d{7,}') |
| 330 | cl_match = cl_re.search(system_output('uname -v').split()[1]) |
| 331 | if cl_match: |
| 332 | current_cl = cl_match.group() |
| 333 | else: |
| 334 | current_cl = None |
| 335 | |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 336 | # We have all the facts, see if they indicate we |
| 337 | # booted the requested kernel or not. |
| 338 | bad = False |
mbligh | da0311e | 2007-10-25 16:03:33 +0000 | [diff] [blame] | 339 | if (type == 'src' and expected_id != running_id or |
| 340 | type == 'rpm' and not running_id.startswith(expected_id + '::')): |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 341 | print "check_kernel_ident: kernel identifier mismatch" |
| 342 | bad = True |
| 343 | if expected_when != cmdline_when: |
| 344 | print "check_kernel_ident: kernel command line mismatch" |
| 345 | bad = True |
mbligh | da0311e | 2007-10-25 16:03:33 +0000 | [diff] [blame] | 346 | if expected_cl and current_cl and str(expected_cl) != current_cl: |
| 347 | print 'check_kernel_ident: kernel changelist mismatch' |
| 348 | bad = True |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 349 | |
| 350 | if bad: |
| 351 | print " Expected Ident: " + expected_id |
| 352 | print " Running Ident: " + running_id |
| 353 | print " Expected Mark: %d" % (expected_when) |
| 354 | print "Command Line Mark: %d" % (cmdline_when) |
mbligh | da0311e | 2007-10-25 16:03:33 +0000 | [diff] [blame] | 355 | print " Expected P4 CL: %s" % expected_cl |
| 356 | print " P4 CL: %s" % current_cl |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 357 | print " Command Line: " + cmdline |
| 358 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 359 | raise JobError("boot failure", "reboot.verify") |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 360 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 361 | self.record('GOOD', subdir, 'reboot.verify') |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 362 | |
| 363 | |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 364 | def filesystem(self, device, mountpoint = None, loop_size = 0): |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 365 | if not mountpoint: |
| 366 | mountpoint = self.tmpdir |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 367 | return filesystem.filesystem(self, device, mountpoint,loop_size) |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 368 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 369 | |
| 370 | def reboot(self, tag='autotest'): |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 371 | self.record('GOOD', None, 'reboot.start') |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 372 | self.harness.run_reboot() |
apw | 11985b7 | 2007-10-04 15:44:47 +0000 | [diff] [blame] | 373 | default = self.config_get('boot.set_default') |
| 374 | if default: |
| 375 | self.bootloader.set_default(tag) |
| 376 | else: |
| 377 | self.bootloader.boot_once(tag) |
mbligh | f3b7893 | 2007-11-07 16:52:47 +0000 | [diff] [blame] | 378 | system("(sleep 5; reboot) </dev/null >/dev/null 2>&1 &") |
apw | 0778a2f | 2006-10-06 03:11:40 +0000 | [diff] [blame] | 379 | self.quit() |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 380 | |
| 381 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 382 | def noop(self, text): |
| 383 | print "job: noop: " + text |
| 384 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 385 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 386 | # Job control primatives. |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 387 | |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 388 | def __parallel_execute(self, func, *args): |
| 389 | func(*args) |
| 390 | |
| 391 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 392 | def parallel(self, *tasklist): |
| 393 | """Run tasks in parallel""" |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 394 | |
| 395 | pids = [] |
| 396 | for task in tasklist: |
| 397 | pids.append(fork_start(self.resultdir, |
| 398 | lambda: self.__parallel_execute(*task))) |
| 399 | for pid in pids: |
| 400 | fork_waitfor(self.resultdir, pid) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 401 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 402 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 403 | def quit(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 404 | # XXX: should have a better name. |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 405 | self.harness.run_pause() |
apw | f2c6660 | 2006-04-27 14:11:25 +0000 | [diff] [blame] | 406 | raise JobContinue("more to come") |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 407 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 408 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 409 | def complete(self, status): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 410 | """Clean up and exit""" |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 411 | # We are about to exit 'complete' so clean up the control file. |
| 412 | try: |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 413 | os.unlink(self.control + '.state') |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 414 | except: |
| 415 | pass |
mbligh | 61a6c1a | 2006-12-25 01:26:38 +0000 | [diff] [blame] | 416 | self.harness.run_complete() |
apw | 1b02190 | 2006-04-03 17:02:56 +0000 | [diff] [blame] | 417 | sys.exit(status) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 418 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 419 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 420 | steps = [] |
| 421 | def next_step(self, step): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 422 | """Define the next step""" |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 423 | if not isinstance(step[0], basestring): |
| 424 | step[0] = step[0].__name__ |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 425 | self.steps.append(step) |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 426 | pickle.dump(self.steps, open(self.control + '.state', 'w')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 427 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 428 | |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 429 | def next_step_prepend(self, step): |
| 430 | """Insert a new step, executing first""" |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 431 | if not isinstance(step[0], basestring): |
| 432 | step[0] = step[0].__name__ |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 433 | self.steps.insert(0, step) |
| 434 | pickle.dump(self.steps, open(self.control + '.state', 'w')) |
| 435 | |
| 436 | |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 437 | def step_engine(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 438 | """the stepping engine -- if the control file defines |
| 439 | step_init we will be using this engine to drive multiple runs. |
| 440 | """ |
| 441 | """Do the next step""" |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 442 | lcl = dict({'job': self}) |
| 443 | |
| 444 | str = """ |
| 445 | from error import * |
| 446 | from autotest_utils import * |
| 447 | """ |
| 448 | exec(str, lcl, lcl) |
| 449 | execfile(self.control, lcl, lcl) |
| 450 | |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 451 | state = self.control + '.state' |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 452 | # If there is a mid-job state file load that in and continue |
| 453 | # where it indicates. Otherwise start stepping at the passed |
| 454 | # entry. |
| 455 | try: |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 456 | self.steps = pickle.load(open(state, 'r')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 457 | except: |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 458 | if lcl.has_key('step_init'): |
| 459 | self.next_step([lcl['step_init']]) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 460 | |
| 461 | # Run the step list. |
| 462 | while len(self.steps) > 0: |
apw | fd922bb | 2006-04-04 07:47:00 +0000 | [diff] [blame] | 463 | step = self.steps.pop(0) |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 464 | pickle.dump(self.steps, open(state, 'w')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 465 | |
| 466 | cmd = step.pop(0) |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 467 | lcl['__args'] = step |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 468 | exec(cmd + "(*__args)", lcl, lcl) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 469 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 470 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 471 | def record(self, status_code, subdir, operation, status = ''): |
| 472 | """ |
| 473 | Record job-level status |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 474 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 475 | The intent is to make this file both machine parseable and |
| 476 | human readable. That involves a little more complexity, but |
| 477 | really isn't all that bad ;-) |
| 478 | |
| 479 | Format is <status code>\t<subdir>\t<operation>\t<status> |
| 480 | |
| 481 | status code: (GOOD|WARN|FAIL|ABORT) |
| 482 | or START |
| 483 | or END (GOOD|WARN|FAIL|ABORT) |
| 484 | |
| 485 | subdir: MUST be a relevant subdirectory in the results, |
| 486 | or None, which will be represented as '----' |
| 487 | |
| 488 | operation: description of what you ran (e.g. "dbench", or |
| 489 | "mkfs -t foobar /dev/sda9") |
| 490 | |
| 491 | status: error message or "completed sucessfully" |
| 492 | |
| 493 | ------------------------------------------------------------ |
| 494 | |
| 495 | Initial tabs indicate indent levels for grouping, and is |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 496 | governed by self.group_level |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 497 | |
| 498 | multiline messages have secondary lines prefaced by a double |
| 499 | space (' ') |
| 500 | """ |
| 501 | |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 502 | if subdir: |
| 503 | if re.match(r'[\n\t]', subdir): |
| 504 | raise "Invalid character in subdir string" |
| 505 | substr = subdir |
| 506 | else: |
| 507 | substr = '----' |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 508 | |
| 509 | if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \ |
| 510 | status_code): |
| 511 | raise "Invalid status code supplied: %s" % status_code |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 512 | if not operation: |
| 513 | operation = '----' |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 514 | if re.match(r'[\n\t]', operation): |
| 515 | raise "Invalid character in operation string" |
| 516 | operation = operation.rstrip() |
| 517 | status = status.rstrip() |
| 518 | status = re.sub(r"\t", " ", status) |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 519 | # Ensure any continuation lines are marked so we can |
| 520 | # detect them in the status file to ensure it is parsable. |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 521 | status = re.sub(r"\n", "\n" + "\t" * self.group_level + " ", status) |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 522 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 523 | # Generate timestamps for inclusion in the logs |
| 524 | epoch_time = int(time.time()) # seconds since epoch, in UTC |
| 525 | local_time = time.localtime(epoch_time) |
| 526 | epoch_time_str = "timestamp=%d" % (epoch_time,) |
| 527 | local_time_str = time.strftime("localtime=%b %d %H:%M:%S", |
| 528 | local_time) |
| 529 | |
| 530 | msg = '\t'.join(str(x) for x in (status_code, substr, operation, |
| 531 | epoch_time_str, local_time_str, |
| 532 | status)) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 533 | msg = '\t' * self.group_level + msg |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 534 | |
apw | 4b2e4fb | 2007-09-25 16:52:30 +0000 | [diff] [blame] | 535 | self.harness.test_status_detail(status_code, substr, |
| 536 | operation, status) |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 537 | self.harness.test_status(msg) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 538 | print msg |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 539 | status_file = os.path.join(self.resultdir, 'status') |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 540 | open(status_file, "a").write(msg + "\n") |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 541 | if subdir: |
| 542 | status_file = os.path.join(self.resultdir, subdir, 'status') |
| 543 | open(status_file, "a").write(msg + "\n") |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 544 | |
| 545 | |
mbligh | 570e93e | 2006-11-26 05:15:56 +0000 | [diff] [blame] | 546 | def runjob(control, cont = False, tag = "default", harness_type = ''): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 547 | """The main interface to this module |
| 548 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 549 | control |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 550 | The control file to use for this job. |
| 551 | cont |
| 552 | Whether this is the continuation of a previously started job |
| 553 | """ |
mbligh | b4eef24 | 2007-07-23 18:22:49 +0000 | [diff] [blame] | 554 | control = os.path.abspath(control) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 555 | state = control + '.state' |
| 556 | |
| 557 | # instantiate the job object ready for the control file. |
| 558 | myjob = None |
| 559 | try: |
| 560 | # Check that the control file is valid |
| 561 | if not os.path.exists(control): |
| 562 | raise JobError(control + ": control file not found") |
| 563 | |
| 564 | # When continuing, the job is complete when there is no |
| 565 | # state file, ensure we don't try and continue. |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 566 | if cont and not os.path.exists(state): |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 567 | sys.exit(1) |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 568 | if cont == False and os.path.exists(state): |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 569 | os.unlink(state) |
| 570 | |
mbligh | 570e93e | 2006-11-26 05:15:56 +0000 | [diff] [blame] | 571 | myjob = job(control, tag, cont, harness_type) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 572 | |
| 573 | # Load in the users control file, may do any one of: |
| 574 | # 1) execute in toto |
| 575 | # 2) define steps, and select the first via next_step() |
| 576 | myjob.step_engine() |
| 577 | |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 578 | except JobContinue: |
| 579 | sys.exit(5) |
| 580 | |
mbligh | 4768171 | 2007-11-16 21:41:51 +0000 | [diff] [blame] | 581 | except JobError, instance: |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 582 | print "JOB ERROR: " + instance.args[0] |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 583 | if myjob: |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 584 | command = None |
| 585 | if len(instance.args) > 1: |
| 586 | command = instance.args[1] |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 587 | myjob.group_level = 0 |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 588 | myjob.record('ABORT', None, command, instance.args[0]) |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 589 | myjob.record('END ABORT', None, None) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 590 | myjob.complete(1) |
| 591 | |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 592 | except Exception, e: |
| 593 | msg = format_error() |
| 594 | print "JOB ERROR: " + msg |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 595 | if myjob: |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 596 | myjob.group_level = 0 |
| 597 | myjob.record('ABORT', None, None, msg) |
| 598 | myjob.record('END ABORT', None, None) |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 599 | myjob.complete(1) |
mbligh | 892d37f | 2007-03-01 17:03:25 +0000 | [diff] [blame] | 600 | |
| 601 | # If we get here, then we assume the job is complete and good. |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 602 | myjob.group_level = 0 |
| 603 | myjob.record('END GOOD', None, None) |
mbligh | 892d37f | 2007-03-01 17:03:25 +0000 | [diff] [blame] | 604 | myjob.complete(0) |