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