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 | f31b0c0 | 2007-11-29 18:19:22 +0000 | [diff] [blame] | 13 | from common.error import * |
mbligh | 65938a2 | 2007-12-10 16:58:52 +0000 | [diff] [blame] | 14 | from common import barrier |
mbligh | e1417fa | 2007-12-10 16:55:13 +0000 | [diff] [blame] | 15 | import kernel, xen, test, profilers, filesystem, fd_stack, boottool |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 16 | import harness, config |
mbligh | 83ac994 | 2007-11-05 18:59:37 +0000 | [diff] [blame] | 17 | import sysinfo |
mbligh | 65938a2 | 2007-12-10 16:58:52 +0000 | [diff] [blame] | 18 | import cpuset |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 19 | |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame^] | 20 | |
| 21 | class StepError(AutotestError): |
| 22 | pass |
| 23 | |
| 24 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 25 | class base_job: |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 26 | """The actual job against which we do everything. |
| 27 | |
| 28 | Properties: |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 29 | autodir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 30 | The top level autotest directory (/usr/local/autotest). |
| 31 | Comes from os.environ['AUTODIR']. |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 32 | bindir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 33 | <autodir>/bin/ |
mbligh | d5a3883 | 2008-01-25 18:15:39 +0000 | [diff] [blame] | 34 | libdir |
| 35 | <autodir>/lib/ |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 36 | testdir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 37 | <autodir>/tests/ |
mbligh | 84bafdb | 2008-01-26 19:43:34 +0000 | [diff] [blame] | 38 | site_testdir |
| 39 | <autodir>/site_tests/ |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 40 | profdir |
| 41 | <autodir>/profilers/ |
| 42 | tmpdir |
| 43 | <autodir>/tmp/ |
| 44 | resultdir |
| 45 | <autodir>/results/<jobtag> |
| 46 | stdout |
| 47 | fd_stack object for stdout |
| 48 | stderr |
| 49 | fd_stack object for stderr |
| 50 | profilers |
| 51 | the profilers object for this job |
apw | 504a7dd | 2006-10-12 17:18:37 +0000 | [diff] [blame] | 52 | harness |
| 53 | the server harness object for this job |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 54 | config |
| 55 | the job configuration for this job |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 56 | """ |
| 57 | |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 58 | DEFAULT_LOG_FILENAME = "status" |
| 59 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 60 | def __init__(self, control, jobtag, cont, harness_type=None, |
| 61 | use_external_logging = False): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 62 | """ |
| 63 | control |
| 64 | The control file (pathname of) |
| 65 | jobtag |
| 66 | The job tag string (eg "default") |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 67 | cont |
| 68 | If this is the continuation of this job |
apw | e68a713 | 2006-12-01 11:21:37 +0000 | [diff] [blame] | 69 | harness_type |
| 70 | An alternative server harness |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 71 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 72 | self.autodir = os.environ['AUTODIR'] |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 73 | self.bindir = os.path.join(self.autodir, 'bin') |
mbligh | d5a3883 | 2008-01-25 18:15:39 +0000 | [diff] [blame] | 74 | self.libdir = os.path.join(self.autodir, 'lib') |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 75 | self.testdir = os.path.join(self.autodir, 'tests') |
mbligh | 84bafdb | 2008-01-26 19:43:34 +0000 | [diff] [blame] | 76 | self.site_testdir = os.path.join(self.autodir, 'site_tests') |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 77 | self.profdir = os.path.join(self.autodir, 'profilers') |
| 78 | self.tmpdir = os.path.join(self.autodir, 'tmp') |
| 79 | self.resultdir = os.path.join(self.autodir, 'results', jobtag) |
mbligh | 0fb8397 | 2008-01-10 16:30:02 +0000 | [diff] [blame] | 80 | self.sysinfodir = os.path.join(self.resultdir, 'sysinfo') |
mbligh | 8d83cdc | 2007-12-03 18:09:18 +0000 | [diff] [blame] | 81 | self.control = os.path.abspath(control) |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 82 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 83 | if not cont: |
| 84 | if os.path.exists(self.tmpdir): |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 85 | system('umount -f %s > /dev/null 2> /dev/null'%\ |
| 86 | self.tmpdir, ignorestatus=True) |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 87 | system('rm -rf ' + self.tmpdir) |
| 88 | os.mkdir(self.tmpdir) |
| 89 | |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 90 | results = os.path.join(self.autodir, 'results') |
| 91 | if not os.path.exists(results): |
| 92 | os.mkdir(results) |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 93 | |
apw | f3d2862 | 2007-09-25 16:49:17 +0000 | [diff] [blame] | 94 | download = os.path.join(self.testdir, 'download') |
| 95 | if os.path.exists(download): |
| 96 | system('rm -rf ' + download) |
| 97 | os.mkdir(download) |
| 98 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 99 | if os.path.exists(self.resultdir): |
| 100 | system('rm -rf ' + self.resultdir) |
| 101 | os.mkdir(self.resultdir) |
mbligh | 0fb8397 | 2008-01-10 16:30:02 +0000 | [diff] [blame] | 102 | os.mkdir(self.sysinfodir) |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 103 | |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 104 | os.mkdir(os.path.join(self.resultdir, 'debug')) |
| 105 | os.mkdir(os.path.join(self.resultdir, 'analysis')) |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 106 | |
mbligh | 8d83cdc | 2007-12-03 18:09:18 +0000 | [diff] [blame] | 107 | shutil.copyfile(self.control, |
| 108 | os.path.join(self.resultdir, 'control')) |
mbligh | f4ca14f | 2008-03-03 16:03:05 +0000 | [diff] [blame] | 109 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 110 | |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 111 | self.control = control |
mbligh | 2711360 | 2007-10-31 21:07:51 +0000 | [diff] [blame] | 112 | self.jobtag = jobtag |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 113 | self.log_filename = self.DEFAULT_LOG_FILENAME |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 114 | self.container = None |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 115 | |
mbligh | 56f1fbb | 2006-10-01 15:10:56 +0000 | [diff] [blame] | 116 | self.stdout = fd_stack.fd_stack(1, sys.stdout) |
| 117 | self.stderr = fd_stack.fd_stack(2, sys.stderr) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 118 | self.group_level = 0 |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 119 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 120 | self.config = config.config(self) |
| 121 | |
apw | d27e55f | 2006-12-01 11:22:08 +0000 | [diff] [blame] | 122 | self.harness = harness.select(harness_type, self) |
| 123 | |
mbligh | a35553b | 2006-04-23 15:52:25 +0000 | [diff] [blame] | 124 | self.profilers = profilers.profilers(self) |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 125 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 126 | try: |
apw | 90154af | 2006-12-01 11:23:36 +0000 | [diff] [blame] | 127 | tool = self.config_get('boottool.executable') |
| 128 | self.bootloader = boottool.boottool(tool) |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 129 | except: |
| 130 | pass |
| 131 | |
mbligh | 0fb8397 | 2008-01-10 16:30:02 +0000 | [diff] [blame] | 132 | sysinfo.log_per_reboot_data(self.sysinfodir) |
mbligh | 3a6d6ca | 2006-04-23 15:50:24 +0000 | [diff] [blame] | 133 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 134 | if not cont: |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 135 | self.record('START', None, None) |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 136 | self.group_level = 1 |
apw | 357f50f | 2006-12-01 11:22:39 +0000 | [diff] [blame] | 137 | |
apw | f91efaf | 2007-11-24 17:32:13 +0000 | [diff] [blame] | 138 | self.harness.run_start() |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 139 | |
| 140 | if use_external_logging: |
| 141 | self.enable_external_logging() |
apw | f91efaf | 2007-11-24 17:32:13 +0000 | [diff] [blame] | 142 | |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 143 | |
| 144 | def relative_path(self, path): |
| 145 | """\ |
| 146 | Return a patch relative to the job results directory |
| 147 | """ |
mbligh | 1c250ca | 2007-08-30 16:31:38 +0000 | [diff] [blame] | 148 | head = len(self.resultdir) + 1 # remove the / inbetween |
| 149 | return path[head:] |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 150 | |
| 151 | |
mbligh | 362ab3d | 2007-08-30 11:24:04 +0000 | [diff] [blame] | 152 | def control_get(self): |
| 153 | return self.control |
| 154 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 155 | |
mbligh | 8d83cdc | 2007-12-03 18:09:18 +0000 | [diff] [blame] | 156 | def control_set(self, control): |
| 157 | self.control = os.path.abspath(control) |
| 158 | |
| 159 | |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 160 | def harness_select(self, which): |
| 161 | self.harness = harness.select(which, self) |
| 162 | |
| 163 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 164 | def config_set(self, name, value): |
| 165 | self.config.set(name, value) |
| 166 | |
| 167 | |
| 168 | def config_get(self, name): |
| 169 | return self.config.get(name) |
| 170 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 171 | def setup_dirs(self, results_dir, tmp_dir): |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 172 | if not tmp_dir: |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 173 | tmp_dir = os.path.join(self.tmpdir, 'build') |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 174 | if not os.path.exists(tmp_dir): |
| 175 | os.mkdir(tmp_dir) |
| 176 | if not os.path.isdir(tmp_dir): |
mbligh | 642b03e | 2008-01-14 16:53:15 +0000 | [diff] [blame] | 177 | e_msg = "Temp dir (%s) is not a dir - args backwards?" % self.tmpdir |
| 178 | raise ValueError(e_msg) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 179 | |
| 180 | # We label the first build "build" and then subsequent ones |
| 181 | # as "build.2", "build.3", etc. Whilst this is a little bit |
| 182 | # inconsistent, 99.9% of jobs will only have one build |
| 183 | # (that's not done as kernbench, sparse, or buildtest), |
| 184 | # so it works out much cleaner. One of life's comprimises. |
| 185 | if not results_dir: |
| 186 | results_dir = os.path.join(self.resultdir, 'build') |
| 187 | i = 2 |
| 188 | while os.path.exists(results_dir): |
| 189 | results_dir = os.path.join(self.resultdir, 'build.%d' % i) |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 190 | i += 1 |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 191 | if not os.path.exists(results_dir): |
| 192 | os.mkdir(results_dir) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 193 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 194 | return (results_dir, tmp_dir) |
| 195 | |
| 196 | |
| 197 | def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \ |
| 198 | kjob = None ): |
| 199 | """Summon a xen object""" |
| 200 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
| 201 | build_dir = 'xen' |
| 202 | return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob) |
| 203 | |
| 204 | |
| 205 | def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False): |
| 206 | """Summon a kernel object""" |
mbligh | 669caa1 | 2007-11-05 18:32:13 +0000 | [diff] [blame] | 207 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 208 | build_dir = 'linux' |
mbligh | 6ee7ee0 | 2007-11-13 23:49:05 +0000 | [diff] [blame] | 209 | return kernel.auto_kernel(self, base_tree, results_dir, |
| 210 | tmp_dir, build_dir, leave) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 211 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 212 | |
mbligh | 6b504ff | 2007-12-12 21:03:49 +0000 | [diff] [blame] | 213 | def barrier(self, *args, **kwds): |
mbligh | fadca20 | 2006-09-23 04:40:01 +0000 | [diff] [blame] | 214 | """Create a barrier object""" |
mbligh | 6b504ff | 2007-12-12 21:03:49 +0000 | [diff] [blame] | 215 | return barrier.barrier(*args, **kwds) |
mbligh | fadca20 | 2006-09-23 04:40:01 +0000 | [diff] [blame] | 216 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 217 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 218 | def setup_dep(self, deps): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 219 | """Set up the dependencies for this test. |
| 220 | |
| 221 | deps is a list of libraries required for this test. |
| 222 | """ |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 223 | for dep in deps: |
| 224 | try: |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 225 | os.chdir(os.path.join(self.autodir, 'deps', dep)) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 226 | system('./' + dep + '.py') |
| 227 | except: |
| 228 | error = "setting up dependency " + dep + "\n" |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 229 | raise UnhandledError(error) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 230 | |
| 231 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 232 | def __runtest(self, url, tag, args, dargs): |
| 233 | try: |
mbligh | 53c4150 | 2007-10-23 20:45:04 +0000 | [diff] [blame] | 234 | l = lambda : test.runtest(self, url, tag, args, dargs) |
| 235 | pid = fork_start(self.resultdir, l) |
| 236 | fork_waitfor(self.resultdir, pid) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 237 | except AutotestError: |
| 238 | raise |
| 239 | except: |
| 240 | raise UnhandledError('running test ' + \ |
| 241 | self.__class__.__name__ + "\n") |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 242 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 243 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 244 | def run_test(self, url, *args, **dargs): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 245 | """Summon a test object and run it. |
| 246 | |
| 247 | tag |
| 248 | tag to add to testname |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 249 | url |
| 250 | url of the test to run |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 251 | """ |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 252 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 253 | if not url: |
mbligh | 642b03e | 2008-01-14 16:53:15 +0000 | [diff] [blame] | 254 | raise TypeError("Test name is invalid. Switched arguments?") |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 255 | (group, testname) = test.testname(url) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 256 | tag = dargs.pop('tag', None) |
mbligh | 65938a2 | 2007-12-10 16:58:52 +0000 | [diff] [blame] | 257 | container = dargs.pop('container', None) |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 258 | subdir = testname |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 259 | if tag: |
| 260 | subdir += '.' + tag |
| 261 | |
mbligh | 65938a2 | 2007-12-10 16:58:52 +0000 | [diff] [blame] | 262 | if container: |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 263 | cname = container.get('name', None) |
| 264 | if not cname: # get old name |
| 265 | cname = container.get('container_name', None) |
| 266 | mbytes = container.get('mbytes', None) |
| 267 | if not mbytes: # get old name |
| 268 | mbytes = container.get('mem', None) |
| 269 | cpus = container.get('cpus', None) |
| 270 | if not cpus: # get old name |
| 271 | cpus = container.get('cpu', None) |
mbligh | 9ea5260 | 2008-04-02 00:15:35 +0000 | [diff] [blame] | 272 | root = container.get('root', '') |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 273 | self.new_container(mbytes=mbytes, cpus=cpus, |
| 274 | root=root, name=cname) |
mbligh | 65938a2 | 2007-12-10 16:58:52 +0000 | [diff] [blame] | 275 | # We are running in a container now... |
| 276 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 277 | def group_func(): |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 278 | try: |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 279 | self.__runtest(url, tag, args, dargs) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 280 | except Exception, detail: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 281 | self.record('FAIL', subdir, testname, |
| 282 | str(detail)) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 283 | raise |
| 284 | else: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 285 | self.record('GOOD', subdir, testname, |
| 286 | 'completed successfully') |
mbligh | cfc6dd3 | 2007-11-20 00:44:35 +0000 | [diff] [blame] | 287 | result, exc_info = self.__rungroup(subdir, group_func) |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 288 | if container: |
| 289 | self.release_container() |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 290 | if exc_info and isinstance(exc_info[1], TestError): |
| 291 | return False |
| 292 | elif exc_info: |
mbligh | 71ea249 | 2008-01-15 20:35:52 +0000 | [diff] [blame] | 293 | raise exc_info[0], exc_info[1], exc_info[2] |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 294 | else: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 295 | return True |
| 296 | |
| 297 | |
| 298 | def __rungroup(self, name, function, *args, **dargs): |
| 299 | """\ |
| 300 | name: |
| 301 | name of the group |
| 302 | function: |
| 303 | subroutine to run |
| 304 | *args: |
| 305 | arguments for the function |
| 306 | |
| 307 | Returns a 2-tuple (result, exc_info) where result |
| 308 | is the return value of function, and exc_info is |
| 309 | the sys.exc_info() of the exception thrown by the |
| 310 | function (which may be None). |
| 311 | """ |
| 312 | |
| 313 | result, exc_info = None, None |
| 314 | try: |
| 315 | self.record('START', None, name) |
| 316 | self.group_level += 1 |
| 317 | result = function(*args, **dargs) |
| 318 | self.group_level -= 1 |
| 319 | self.record('END GOOD', None, name) |
| 320 | except Exception, e: |
| 321 | exc_info = sys.exc_info() |
| 322 | self.group_level -= 1 |
mbligh | 51144e0 | 2007-11-20 20:38:18 +0000 | [diff] [blame] | 323 | err_msg = str(e) + '\n' + format_error() |
| 324 | self.record('END FAIL', None, name, err_msg) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 325 | |
| 326 | return result, exc_info |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 327 | |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 328 | |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 329 | def run_group(self, function, *args, **dargs): |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 330 | """\ |
| 331 | function: |
| 332 | subroutine to run |
| 333 | *args: |
| 334 | arguments for the function |
| 335 | """ |
| 336 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 337 | # Allow the tag for the group to be specified |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 338 | name = function.__name__ |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 339 | tag = dargs.pop('tag', None) |
| 340 | if tag: |
| 341 | name = tag |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 342 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 343 | result, exc_info = self.__rungroup(name, function, |
| 344 | *args, **dargs) |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 345 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 346 | # if there was a non-TestError exception, raise it |
mbligh | 71ea249 | 2008-01-15 20:35:52 +0000 | [diff] [blame] | 347 | if exc_info and not isinstance(exc_info[1], TestError): |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 348 | err = ''.join(traceback.format_exception(*exc_info)) |
| 349 | raise TestError(name + ' failed\n' + err) |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 350 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 351 | # pass back the actual return value from the function |
apw | 08403ca | 2007-09-27 17:17:22 +0000 | [diff] [blame] | 352 | return result |
| 353 | |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 354 | |
mbligh | 1fc7ba1 | 2008-03-31 17:50:53 +0000 | [diff] [blame] | 355 | def new_container(self, mbytes=None, cpus=None, root='', name=None): |
mbligh | 6ca0d6a | 2008-03-03 16:22:13 +0000 | [diff] [blame] | 356 | if not grep('cpuset', '/proc/filesystems'): |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 357 | print "Containers not enabled by latest reboot" |
| 358 | return # containers weren't enabled in this kernel boot |
| 359 | pid = os.getpid() |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 360 | if not name: |
| 361 | name = 'test%d' % pid # make arbitrary unique name |
| 362 | self.container = cpuset.cpuset(name, job_size=mbytes, |
mbligh | 337bb76 | 2008-04-16 21:23:10 +0000 | [diff] [blame] | 363 | job_pid=pid, cpus=cpus, root=root) |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 364 | # This job's python shell is now running in the new container |
| 365 | # and all forked test processes will inherit that container |
| 366 | |
| 367 | |
| 368 | def release_container(self): |
| 369 | if self.container: |
mbligh | 337bb76 | 2008-04-16 21:23:10 +0000 | [diff] [blame] | 370 | self.container.release() |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 371 | self.container = None |
| 372 | |
| 373 | |
| 374 | def cpu_count(self): |
| 375 | if self.container: |
| 376 | return len(self.container.cpus) |
| 377 | return count_cpus() # use total system count |
| 378 | |
| 379 | |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 380 | # Check the passed kernel identifier against the command line |
| 381 | # and the running kernel, abort the job on missmatch. |
mbligh | 38a4a11 | 2008-03-19 13:11:34 +0000 | [diff] [blame] | 382 | def kernel_check_ident(self, expected_when, expected_id, subdir, |
| 383 | type = 'src'): |
| 384 | print (("POST BOOT: checking booted kernel " + |
| 385 | "mark=%d identity='%s' type='%s'") % |
| 386 | (expected_when, expected_id, type)) |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 387 | |
| 388 | running_id = running_os_ident() |
| 389 | |
| 390 | cmdline = read_one_line("/proc/cmdline") |
| 391 | |
| 392 | find_sum = re.compile(r'.*IDENT=(\d+)') |
| 393 | m = find_sum.match(cmdline) |
| 394 | cmdline_when = -1 |
| 395 | if m: |
| 396 | cmdline_when = int(m.groups()[0]) |
| 397 | |
| 398 | # We have all the facts, see if they indicate we |
| 399 | # booted the requested kernel or not. |
| 400 | bad = False |
mbligh | da0311e | 2007-10-25 16:03:33 +0000 | [diff] [blame] | 401 | if (type == 'src' and expected_id != running_id or |
| 402 | type == 'rpm' and not running_id.startswith(expected_id + '::')): |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 403 | print "check_kernel_ident: kernel identifier mismatch" |
| 404 | bad = True |
| 405 | if expected_when != cmdline_when: |
| 406 | print "check_kernel_ident: kernel command line mismatch" |
| 407 | bad = True |
| 408 | |
| 409 | if bad: |
| 410 | print " Expected Ident: " + expected_id |
| 411 | print " Running Ident: " + running_id |
| 412 | print " Expected Mark: %d" % (expected_when) |
| 413 | print "Command Line Mark: %d" % (cmdline_when) |
| 414 | print " Command Line: " + cmdline |
| 415 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 416 | raise JobError("boot failure", "reboot.verify") |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 417 | |
mbligh | b7fd270 | 2008-03-25 14:57:08 +0000 | [diff] [blame] | 418 | self.record('GOOD', subdir, 'reboot.verify', expected_id) |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 419 | |
| 420 | |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 421 | def filesystem(self, device, mountpoint = None, loop_size = 0): |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 422 | if not mountpoint: |
| 423 | mountpoint = self.tmpdir |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 424 | return filesystem.filesystem(self, device, mountpoint,loop_size) |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 425 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 426 | |
| 427 | def enable_external_logging(self): |
| 428 | pass |
| 429 | |
| 430 | |
| 431 | def disable_external_logging(self): |
| 432 | pass |
| 433 | |
| 434 | |
| 435 | def reboot_setup(self): |
| 436 | pass |
| 437 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 438 | |
| 439 | def reboot(self, tag='autotest'): |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 440 | self.reboot_setup() |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 441 | self.record('GOOD', None, 'reboot.start') |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 442 | self.harness.run_reboot() |
apw | 11985b7 | 2007-10-04 15:44:47 +0000 | [diff] [blame] | 443 | default = self.config_get('boot.set_default') |
| 444 | if default: |
| 445 | self.bootloader.set_default(tag) |
| 446 | else: |
| 447 | self.bootloader.boot_once(tag) |
mbligh | f3b7893 | 2007-11-07 16:52:47 +0000 | [diff] [blame] | 448 | system("(sleep 5; reboot) </dev/null >/dev/null 2>&1 &") |
apw | 0778a2f | 2006-10-06 03:11:40 +0000 | [diff] [blame] | 449 | self.quit() |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 450 | |
| 451 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 452 | def noop(self, text): |
| 453 | print "job: noop: " + text |
| 454 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 455 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 456 | def parallel(self, *tasklist): |
| 457 | """Run tasks in parallel""" |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 458 | |
| 459 | pids = [] |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 460 | old_log_filename = self.log_filename |
| 461 | for i, task in enumerate(tasklist): |
| 462 | self.log_filename = old_log_filename + (".%d" % i) |
| 463 | task_func = lambda: task[0](*task[1:]) |
| 464 | pids.append(fork_start(self.resultdir, task_func)) |
| 465 | |
| 466 | old_log_path = os.path.join(self.resultdir, old_log_filename) |
| 467 | old_log = open(old_log_path, "a") |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 468 | exceptions = [] |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 469 | for i, pid in enumerate(pids): |
| 470 | # wait for the task to finish |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 471 | try: |
| 472 | fork_waitfor(self.resultdir, pid) |
| 473 | except Exception, e: |
| 474 | exceptions.append(e) |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 475 | # copy the logs from the subtask into the main log |
| 476 | new_log_path = old_log_path + (".%d" % i) |
| 477 | if os.path.exists(new_log_path): |
| 478 | new_log = open(new_log_path) |
| 479 | old_log.write(new_log.read()) |
| 480 | new_log.close() |
| 481 | old_log.flush() |
| 482 | os.remove(new_log_path) |
| 483 | old_log.close() |
| 484 | |
| 485 | self.log_filename = old_log_filename |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 486 | |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 487 | # handle any exceptions raised by the parallel tasks |
| 488 | if exceptions: |
| 489 | msg = "%d task(s) failed" % len(exceptions) |
| 490 | raise JobError(msg, str(exceptions), exceptions) |
| 491 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 492 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 493 | def quit(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 494 | # XXX: should have a better name. |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 495 | self.harness.run_pause() |
apw | f2c6660 | 2006-04-27 14:11:25 +0000 | [diff] [blame] | 496 | raise JobContinue("more to come") |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 497 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 498 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 499 | def complete(self, status): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 500 | """Clean up and exit""" |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 501 | # We are about to exit 'complete' so clean up the control file. |
| 502 | try: |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 503 | os.unlink(self.control + '.state') |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 504 | except: |
| 505 | pass |
mbligh | c0b10d3 | 2008-03-03 16:03:28 +0000 | [diff] [blame] | 506 | |
mbligh | 61a6c1a | 2006-12-25 01:26:38 +0000 | [diff] [blame] | 507 | self.harness.run_complete() |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 508 | self.disable_external_logging() |
apw | 1b02190 | 2006-04-03 17:02:56 +0000 | [diff] [blame] | 509 | sys.exit(status) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 510 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 511 | |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame^] | 512 | def __create_step_tuple(self, fn, args, dargs): |
| 513 | # Legacy code passes in an array where the first arg is |
| 514 | # the function or its name. |
| 515 | if isinstance(fn, list): |
| 516 | assert(len(args) == 0) |
| 517 | assert(len(dargs) == 0) |
| 518 | args = fn[1:] |
| 519 | fn = fn[0] |
| 520 | # Pickling actual functions is harry, thus we have to call |
| 521 | # them by name. Unfortunately, this means only functions |
| 522 | # defined globally can be used as a next step. |
| 523 | if isinstance(fn, types.FunctionType): |
| 524 | fn = fn.__name__ |
| 525 | if not isinstance(fn, types.StringTypes): |
| 526 | raise StepError("Next steps must be functions or " |
| 527 | "strings containing the function name") |
| 528 | return (fn, args, dargs) |
| 529 | |
| 530 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 531 | steps = [] |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame^] | 532 | def next_step(self, fn, *args, **dargs): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 533 | """Define the next step""" |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame^] | 534 | step = self.__create_step_tuple(fn, args, dargs) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 535 | self.steps.append(step) |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 536 | pickle.dump(self.steps, open(self.control + '.state', 'w')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 537 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 538 | |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame^] | 539 | def next_step_prepend(self, fn, *args, **dargs): |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 540 | """Insert a new step, executing first""" |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame^] | 541 | step = self.__create_step_tuple(fn, args, dargs) |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 542 | self.steps.insert(0, step) |
| 543 | pickle.dump(self.steps, open(self.control + '.state', 'w')) |
| 544 | |
| 545 | |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 546 | def step_engine(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 547 | """the stepping engine -- if the control file defines |
| 548 | step_init we will be using this engine to drive multiple runs. |
| 549 | """ |
| 550 | """Do the next step""" |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 551 | lcl = dict({'job': self}) |
| 552 | |
| 553 | str = """ |
mbligh | f31b0c0 | 2007-11-29 18:19:22 +0000 | [diff] [blame] | 554 | from common.error import * |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 555 | from autotest_utils import * |
| 556 | """ |
| 557 | exec(str, lcl, lcl) |
| 558 | execfile(self.control, lcl, lcl) |
| 559 | |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 560 | state = self.control + '.state' |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 561 | # If there is a mid-job state file load that in and continue |
| 562 | # where it indicates. Otherwise start stepping at the passed |
| 563 | # entry. |
| 564 | try: |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 565 | self.steps = pickle.load(open(state, 'r')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 566 | except: |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 567 | if lcl.has_key('step_init'): |
| 568 | self.next_step([lcl['step_init']]) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 569 | |
| 570 | # Run the step list. |
| 571 | while len(self.steps) > 0: |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame^] | 572 | (fn, args, dargs) = self.steps.pop(0) |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 573 | pickle.dump(self.steps, open(state, 'w')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 574 | |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame^] | 575 | lcl['__args'] = args |
| 576 | lcl['__dargs'] = dargs |
| 577 | exec(fn + "(*__args, **__dargs)", lcl, lcl) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 578 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 579 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 580 | def record(self, status_code, subdir, operation, status = ''): |
| 581 | """ |
| 582 | Record job-level status |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 583 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 584 | The intent is to make this file both machine parseable and |
| 585 | human readable. That involves a little more complexity, but |
| 586 | really isn't all that bad ;-) |
| 587 | |
| 588 | Format is <status code>\t<subdir>\t<operation>\t<status> |
| 589 | |
| 590 | status code: (GOOD|WARN|FAIL|ABORT) |
| 591 | or START |
| 592 | or END (GOOD|WARN|FAIL|ABORT) |
| 593 | |
| 594 | subdir: MUST be a relevant subdirectory in the results, |
| 595 | or None, which will be represented as '----' |
| 596 | |
| 597 | operation: description of what you ran (e.g. "dbench", or |
| 598 | "mkfs -t foobar /dev/sda9") |
| 599 | |
| 600 | status: error message or "completed sucessfully" |
| 601 | |
| 602 | ------------------------------------------------------------ |
| 603 | |
| 604 | Initial tabs indicate indent levels for grouping, and is |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 605 | governed by self.group_level |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 606 | |
| 607 | multiline messages have secondary lines prefaced by a double |
| 608 | space (' ') |
| 609 | """ |
| 610 | |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 611 | if subdir: |
| 612 | if re.match(r'[\n\t]', subdir): |
mbligh | 642b03e | 2008-01-14 16:53:15 +0000 | [diff] [blame] | 613 | raise ValueError("Invalid character in subdir string") |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 614 | substr = subdir |
| 615 | else: |
| 616 | substr = '----' |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 617 | |
| 618 | if not re.match(r'(START|(END )?(GOOD|WARN|FAIL|ABORT))$', \ |
| 619 | status_code): |
mbligh | 642b03e | 2008-01-14 16:53:15 +0000 | [diff] [blame] | 620 | raise ValueError("Invalid status code supplied: %s" % status_code) |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 621 | if not operation: |
| 622 | operation = '----' |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 623 | if re.match(r'[\n\t]', operation): |
mbligh | 642b03e | 2008-01-14 16:53:15 +0000 | [diff] [blame] | 624 | raise ValueError("Invalid character in operation string") |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 625 | operation = operation.rstrip() |
| 626 | status = status.rstrip() |
| 627 | status = re.sub(r"\t", " ", status) |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 628 | # Ensure any continuation lines are marked so we can |
| 629 | # detect them in the status file to ensure it is parsable. |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 630 | status = re.sub(r"\n", "\n" + "\t" * self.group_level + " ", status) |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 631 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 632 | # Generate timestamps for inclusion in the logs |
| 633 | epoch_time = int(time.time()) # seconds since epoch, in UTC |
| 634 | local_time = time.localtime(epoch_time) |
| 635 | epoch_time_str = "timestamp=%d" % (epoch_time,) |
| 636 | local_time_str = time.strftime("localtime=%b %d %H:%M:%S", |
| 637 | local_time) |
| 638 | |
| 639 | msg = '\t'.join(str(x) for x in (status_code, substr, operation, |
| 640 | epoch_time_str, local_time_str, |
| 641 | status)) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 642 | msg = '\t' * self.group_level + msg |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 643 | |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 644 | msg_tag = "" |
| 645 | if "." in self.log_filename: |
| 646 | msg_tag = self.log_filename.split(".", 1)[1] |
| 647 | |
| 648 | self.harness.test_status_detail(status_code, substr, operation, |
| 649 | status, msg_tag) |
| 650 | self.harness.test_status(msg, msg_tag) |
| 651 | |
| 652 | # log to stdout (if enabled) |
| 653 | #if self.log_filename == self.DEFAULT_LOG_FILENAME: |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 654 | print msg |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 655 | |
| 656 | # log to the "root" status log |
| 657 | status_file = os.path.join(self.resultdir, self.log_filename) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 658 | open(status_file, "a").write(msg + "\n") |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 659 | |
| 660 | # log to the subdir status log (if subdir is set) |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 661 | if subdir: |
mbligh | adff6ca | 2008-01-22 16:38:25 +0000 | [diff] [blame] | 662 | dir = os.path.join(self.resultdir, subdir) |
| 663 | if not os.path.exists(dir): |
| 664 | os.mkdir(dir) |
| 665 | |
| 666 | status_file = os.path.join(dir, |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 667 | self.DEFAULT_LOG_FILENAME) |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 668 | open(status_file, "a").write(msg + "\n") |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 669 | |
| 670 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 671 | def runjob(control, cont = False, tag = "default", harness_type = '', |
| 672 | use_external_logging = False): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 673 | """The main interface to this module |
| 674 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 675 | control |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 676 | The control file to use for this job. |
| 677 | cont |
| 678 | Whether this is the continuation of a previously started job |
| 679 | """ |
mbligh | b4eef24 | 2007-07-23 18:22:49 +0000 | [diff] [blame] | 680 | control = os.path.abspath(control) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 681 | state = control + '.state' |
| 682 | |
| 683 | # instantiate the job object ready for the control file. |
| 684 | myjob = None |
| 685 | try: |
| 686 | # Check that the control file is valid |
| 687 | if not os.path.exists(control): |
| 688 | raise JobError(control + ": control file not found") |
| 689 | |
| 690 | # When continuing, the job is complete when there is no |
| 691 | # state file, ensure we don't try and continue. |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 692 | if cont and not os.path.exists(state): |
apw | b832e1b | 2007-11-24 20:24:38 +0000 | [diff] [blame] | 693 | raise JobComplete("all done") |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 694 | if cont == False and os.path.exists(state): |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 695 | os.unlink(state) |
| 696 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 697 | myjob = job(control, tag, cont, harness_type, |
| 698 | use_external_logging) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 699 | |
| 700 | # Load in the users control file, may do any one of: |
| 701 | # 1) execute in toto |
| 702 | # 2) define steps, and select the first via next_step() |
| 703 | myjob.step_engine() |
| 704 | |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 705 | except JobContinue: |
| 706 | sys.exit(5) |
| 707 | |
apw | b832e1b | 2007-11-24 20:24:38 +0000 | [diff] [blame] | 708 | except JobComplete: |
| 709 | sys.exit(1) |
| 710 | |
mbligh | 4768171 | 2007-11-16 21:41:51 +0000 | [diff] [blame] | 711 | except JobError, instance: |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 712 | print "JOB ERROR: " + instance.args[0] |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 713 | if myjob: |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 714 | command = None |
| 715 | if len(instance.args) > 1: |
| 716 | command = instance.args[1] |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 717 | myjob.group_level = 0 |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 718 | myjob.record('ABORT', None, command, instance.args[0]) |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 719 | myjob.record('END ABORT', None, None) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 720 | myjob.complete(1) |
apw | b832e1b | 2007-11-24 20:24:38 +0000 | [diff] [blame] | 721 | else: |
| 722 | sys.exit(1) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 723 | |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 724 | except Exception, e: |
mbligh | 51144e0 | 2007-11-20 20:38:18 +0000 | [diff] [blame] | 725 | msg = str(e) + '\n' + format_error() |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 726 | print "JOB ERROR: " + msg |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 727 | if myjob: |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 728 | myjob.group_level = 0 |
| 729 | myjob.record('ABORT', None, None, msg) |
| 730 | myjob.record('END ABORT', None, None) |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 731 | myjob.complete(1) |
apw | b832e1b | 2007-11-24 20:24:38 +0000 | [diff] [blame] | 732 | else: |
| 733 | sys.exit(1) |
mbligh | 892d37f | 2007-03-01 17:03:25 +0000 | [diff] [blame] | 734 | |
mbligh | 0144e5a | 2008-03-07 18:17:53 +0000 | [diff] [blame] | 735 | # If we get here, then we assume the job is complete and good. |
| 736 | myjob.group_level = 0 |
| 737 | myjob.record('END GOOD', None, None) |
| 738 | |
mbligh | 892d37f | 2007-03-01 17:03:25 +0000 | [diff] [blame] | 739 | myjob.complete(0) |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 740 | |
| 741 | |
| 742 | # site_job.py may be non-existant or empty, make sure that an appropriate |
| 743 | # site_job class is created nevertheless |
| 744 | try: |
| 745 | from site_job import site_job |
| 746 | except ImportError: |
| 747 | class site_job(base_job): |
| 748 | pass |
| 749 | |
| 750 | class job(site_job): |
| 751 | pass |