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 | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 10 | |
mbligh | 8f243ec | 2006-10-10 05:55:49 +0000 | [diff] [blame] | 11 | # autotest stuff |
mbligh | c61fb36 | 2008-06-05 16:22:15 +0000 | [diff] [blame] | 12 | from autotest_lib.client.bin import autotest_utils, parallel, kernel, xen |
| 13 | from autotest_lib.client.bin import profilers, fd_stack, boottool, harness |
| 14 | from autotest_lib.client.bin import config, sysinfo, cpuset, test, filesystem |
mbligh | e829ba5 | 2008-06-03 15:04:08 +0000 | [diff] [blame] | 15 | from autotest_lib.client.common_lib import error, barrier, logging, utils |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 16 | |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 17 | JOB_PREAMBLE = """ |
| 18 | from common.error import * |
| 19 | from autotest_utils import * |
| 20 | """ |
| 21 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 22 | class StepError(error.AutotestError): |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 23 | pass |
| 24 | |
| 25 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 26 | class base_job: |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 27 | """The actual job against which we do everything. |
| 28 | |
| 29 | Properties: |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 30 | autodir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 31 | The top level autotest directory (/usr/local/autotest). |
| 32 | Comes from os.environ['AUTODIR']. |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 33 | bindir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 34 | <autodir>/bin/ |
mbligh | d5a3883 | 2008-01-25 18:15:39 +0000 | [diff] [blame] | 35 | libdir |
| 36 | <autodir>/lib/ |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 37 | testdir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 38 | <autodir>/tests/ |
mbligh | 84bafdb | 2008-01-26 19:43:34 +0000 | [diff] [blame] | 39 | site_testdir |
| 40 | <autodir>/site_tests/ |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 41 | profdir |
| 42 | <autodir>/profilers/ |
| 43 | tmpdir |
| 44 | <autodir>/tmp/ |
| 45 | resultdir |
| 46 | <autodir>/results/<jobtag> |
| 47 | stdout |
| 48 | fd_stack object for stdout |
| 49 | stderr |
| 50 | fd_stack object for stderr |
| 51 | profilers |
| 52 | the profilers object for this job |
apw | 504a7dd | 2006-10-12 17:18:37 +0000 | [diff] [blame] | 53 | harness |
| 54 | the server harness object for this job |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 55 | config |
| 56 | the job configuration for this job |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 57 | """ |
| 58 | |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 59 | DEFAULT_LOG_FILENAME = "status" |
| 60 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 61 | def __init__(self, control, jobtag, cont, harness_type=None, |
| 62 | use_external_logging = False): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 63 | """ |
| 64 | control |
| 65 | The control file (pathname of) |
| 66 | jobtag |
| 67 | The job tag string (eg "default") |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 68 | cont |
| 69 | If this is the continuation of this job |
apw | e68a713 | 2006-12-01 11:21:37 +0000 | [diff] [blame] | 70 | harness_type |
| 71 | An alternative server harness |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 72 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 73 | self.autodir = os.environ['AUTODIR'] |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 74 | self.bindir = os.path.join(self.autodir, 'bin') |
mbligh | d5a3883 | 2008-01-25 18:15:39 +0000 | [diff] [blame] | 75 | self.libdir = os.path.join(self.autodir, 'lib') |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 76 | self.testdir = os.path.join(self.autodir, 'tests') |
mbligh | 84bafdb | 2008-01-26 19:43:34 +0000 | [diff] [blame] | 77 | self.site_testdir = os.path.join(self.autodir, 'site_tests') |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 78 | self.profdir = os.path.join(self.autodir, 'profilers') |
| 79 | self.tmpdir = os.path.join(self.autodir, 'tmp') |
| 80 | self.resultdir = os.path.join(self.autodir, 'results', jobtag) |
mbligh | 0fb8397 | 2008-01-10 16:30:02 +0000 | [diff] [blame] | 81 | self.sysinfodir = os.path.join(self.resultdir, 'sysinfo') |
mbligh | 8d83cdc | 2007-12-03 18:09:18 +0000 | [diff] [blame] | 82 | self.control = os.path.abspath(control) |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 83 | self.state_file = self.control + '.state' |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 84 | self.current_step_ancestry = [] |
mbligh | 8f4d043 | 2008-06-02 19:42:50 +0000 | [diff] [blame] | 85 | self.next_step_index = 0 |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 86 | self.__load_state() |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 87 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 88 | if not cont: |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 89 | """ |
| 90 | Don't cleanup the tmp dir (which contains the lockfile) |
| 91 | in the constructor, this would be a problem for multiple |
| 92 | jobs starting at the same time on the same client. Instead |
| 93 | do the delete at the server side. We simply create the tmp |
| 94 | directory here if it does not already exist. |
| 95 | """ |
| 96 | if not os.path.exists(self.tmpdir): |
| 97 | os.mkdir(self.tmpdir) |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 98 | |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 99 | results = os.path.join(self.autodir, 'results') |
| 100 | if not os.path.exists(results): |
| 101 | os.mkdir(results) |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 102 | |
apw | f3d2862 | 2007-09-25 16:49:17 +0000 | [diff] [blame] | 103 | download = os.path.join(self.testdir, 'download') |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 104 | if not os.path.exists(download): |
| 105 | os.mkdir(download) |
| 106 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 107 | if os.path.exists(self.resultdir): |
mbligh | e829ba5 | 2008-06-03 15:04:08 +0000 | [diff] [blame] | 108 | utils.system('rm -rf ' |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 109 | + self.resultdir) |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 110 | os.mkdir(self.resultdir) |
mbligh | 0fb8397 | 2008-01-10 16:30:02 +0000 | [diff] [blame] | 111 | os.mkdir(self.sysinfodir) |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 112 | |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 113 | os.mkdir(os.path.join(self.resultdir, 'debug')) |
| 114 | os.mkdir(os.path.join(self.resultdir, 'analysis')) |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 115 | |
mbligh | 8d83cdc | 2007-12-03 18:09:18 +0000 | [diff] [blame] | 116 | shutil.copyfile(self.control, |
| 117 | os.path.join(self.resultdir, 'control')) |
mbligh | f4ca14f | 2008-03-03 16:03:05 +0000 | [diff] [blame] | 118 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 119 | |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 120 | self.control = control |
mbligh | 2711360 | 2007-10-31 21:07:51 +0000 | [diff] [blame] | 121 | self.jobtag = jobtag |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 122 | self.log_filename = self.DEFAULT_LOG_FILENAME |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 123 | self.container = None |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 124 | |
mbligh | 56f1fbb | 2006-10-01 15:10:56 +0000 | [diff] [blame] | 125 | self.stdout = fd_stack.fd_stack(1, sys.stdout) |
| 126 | self.stderr = fd_stack.fd_stack(2, sys.stderr) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 127 | |
| 128 | self._init_group_level() |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 129 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 130 | self.config = config.config(self) |
| 131 | |
apw | d27e55f | 2006-12-01 11:22:08 +0000 | [diff] [blame] | 132 | self.harness = harness.select(harness_type, self) |
| 133 | |
mbligh | a35553b | 2006-04-23 15:52:25 +0000 | [diff] [blame] | 134 | self.profilers = profilers.profilers(self) |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 135 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 136 | try: |
apw | 90154af | 2006-12-01 11:23:36 +0000 | [diff] [blame] | 137 | tool = self.config_get('boottool.executable') |
| 138 | self.bootloader = boottool.boottool(tool) |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 139 | except: |
| 140 | pass |
| 141 | |
mbligh | 0fb8397 | 2008-01-10 16:30:02 +0000 | [diff] [blame] | 142 | sysinfo.log_per_reboot_data(self.sysinfodir) |
mbligh | 3a6d6ca | 2006-04-23 15:50:24 +0000 | [diff] [blame] | 143 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 144 | if not cont: |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 145 | self.record('START', None, None) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 146 | self._increment_group_level() |
apw | 357f50f | 2006-12-01 11:22:39 +0000 | [diff] [blame] | 147 | |
apw | f91efaf | 2007-11-24 17:32:13 +0000 | [diff] [blame] | 148 | self.harness.run_start() |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 149 | |
| 150 | if use_external_logging: |
| 151 | self.enable_external_logging() |
apw | f91efaf | 2007-11-24 17:32:13 +0000 | [diff] [blame] | 152 | |
jadmanski | 8415f96 | 2008-05-06 20:38:53 +0000 | [diff] [blame] | 153 | # load the max disk usage rate - default to no monitoring |
| 154 | self.max_disk_usage_rate = self.get_state('__monitor_disk', |
| 155 | default=0.0) |
| 156 | |
| 157 | |
| 158 | def monitor_disk_usage(self, max_rate): |
| 159 | """\ |
| 160 | Signal that the job should monitor disk space usage on / |
| 161 | and generate a warning if a test uses up disk space at a |
| 162 | rate exceeding 'max_rate'. |
| 163 | |
| 164 | Parameters: |
| 165 | max_rate - the maximium allowed rate of disk consumption |
| 166 | during a test, in MB/hour, or 0 to indicate |
| 167 | no limit. |
| 168 | """ |
| 169 | self.set_state('__monitor_disk', max_rate) |
| 170 | self.max_disk_usage_rate = max_rate |
| 171 | |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 172 | |
| 173 | def relative_path(self, path): |
| 174 | """\ |
| 175 | Return a patch relative to the job results directory |
| 176 | """ |
mbligh | 1c250ca | 2007-08-30 16:31:38 +0000 | [diff] [blame] | 177 | head = len(self.resultdir) + 1 # remove the / inbetween |
| 178 | return path[head:] |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 179 | |
| 180 | |
mbligh | 362ab3d | 2007-08-30 11:24:04 +0000 | [diff] [blame] | 181 | def control_get(self): |
| 182 | return self.control |
| 183 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 184 | |
mbligh | 8d83cdc | 2007-12-03 18:09:18 +0000 | [diff] [blame] | 185 | def control_set(self, control): |
| 186 | self.control = os.path.abspath(control) |
| 187 | |
| 188 | |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 189 | def harness_select(self, which): |
| 190 | self.harness = harness.select(which, self) |
| 191 | |
| 192 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 193 | def config_set(self, name, value): |
| 194 | self.config.set(name, value) |
| 195 | |
| 196 | |
| 197 | def config_get(self, name): |
| 198 | return self.config.get(name) |
| 199 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 200 | def setup_dirs(self, results_dir, tmp_dir): |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 201 | if not tmp_dir: |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 202 | tmp_dir = os.path.join(self.tmpdir, 'build') |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 203 | if not os.path.exists(tmp_dir): |
| 204 | os.mkdir(tmp_dir) |
| 205 | if not os.path.isdir(tmp_dir): |
mbligh | 642b03e | 2008-01-14 16:53:15 +0000 | [diff] [blame] | 206 | e_msg = "Temp dir (%s) is not a dir - args backwards?" % self.tmpdir |
| 207 | raise ValueError(e_msg) |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 208 | |
| 209 | # We label the first build "build" and then subsequent ones |
| 210 | # as "build.2", "build.3", etc. Whilst this is a little bit |
| 211 | # inconsistent, 99.9% of jobs will only have one build |
| 212 | # (that's not done as kernbench, sparse, or buildtest), |
| 213 | # so it works out much cleaner. One of life's comprimises. |
| 214 | if not results_dir: |
| 215 | results_dir = os.path.join(self.resultdir, 'build') |
| 216 | i = 2 |
| 217 | while os.path.exists(results_dir): |
| 218 | results_dir = os.path.join(self.resultdir, 'build.%d' % i) |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 219 | i += 1 |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 220 | if not os.path.exists(results_dir): |
| 221 | os.mkdir(results_dir) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 222 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 223 | return (results_dir, tmp_dir) |
| 224 | |
| 225 | |
| 226 | def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \ |
| 227 | kjob = None ): |
| 228 | """Summon a xen object""" |
| 229 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
| 230 | build_dir = 'xen' |
| 231 | return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob) |
| 232 | |
| 233 | |
| 234 | def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False): |
| 235 | """Summon a kernel object""" |
mbligh | 669caa1 | 2007-11-05 18:32:13 +0000 | [diff] [blame] | 236 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 237 | build_dir = 'linux' |
mbligh | 6ee7ee0 | 2007-11-13 23:49:05 +0000 | [diff] [blame] | 238 | return kernel.auto_kernel(self, base_tree, results_dir, |
| 239 | tmp_dir, build_dir, leave) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 240 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 241 | |
mbligh | 6b504ff | 2007-12-12 21:03:49 +0000 | [diff] [blame] | 242 | def barrier(self, *args, **kwds): |
mbligh | fadca20 | 2006-09-23 04:40:01 +0000 | [diff] [blame] | 243 | """Create a barrier object""" |
mbligh | 6b504ff | 2007-12-12 21:03:49 +0000 | [diff] [blame] | 244 | return barrier.barrier(*args, **kwds) |
mbligh | fadca20 | 2006-09-23 04:40:01 +0000 | [diff] [blame] | 245 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 246 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 247 | def setup_dep(self, deps): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 248 | """Set up the dependencies for this test. |
| 249 | |
| 250 | deps is a list of libraries required for this test. |
| 251 | """ |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 252 | for dep in deps: |
| 253 | try: |
apw | 870988b | 2007-09-25 16:50:53 +0000 | [diff] [blame] | 254 | os.chdir(os.path.join(self.autodir, 'deps', dep)) |
mbligh | e829ba5 | 2008-06-03 15:04:08 +0000 | [diff] [blame] | 255 | utils.system('./' + dep + '.py') |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 256 | except: |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 257 | err = "setting up dependency " + dep + "\n" |
| 258 | raise error.UnhandledError(err) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 259 | |
| 260 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 261 | def __runtest(self, url, tag, args, dargs): |
| 262 | try: |
mbligh | 53c4150 | 2007-10-23 20:45:04 +0000 | [diff] [blame] | 263 | l = lambda : test.runtest(self, url, tag, args, dargs) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 264 | pid = parallel.fork_start(self.resultdir, l) |
| 265 | parallel.fork_waitfor(self.resultdir, pid) |
| 266 | except error.AutotestError: |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 267 | raise |
jadmanski | cf8c4d6 | 2008-05-27 22:09:14 +0000 | [diff] [blame] | 268 | except Exception, e: |
| 269 | msg = "Unhandled %s error occured during test\n" |
| 270 | msg %= str(e.__class__.__name__) |
| 271 | raise error.UnhandledError(msg) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 272 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 273 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 274 | def run_test(self, url, *args, **dargs): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 275 | """Summon a test object and run it. |
| 276 | |
| 277 | tag |
| 278 | tag to add to testname |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 279 | url |
| 280 | url of the test to run |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 281 | """ |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 282 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 283 | if not url: |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 284 | raise TypeError("Test name is invalid. " |
| 285 | "Switched arguments?") |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 286 | (group, testname) = test.testname(url) |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 287 | namelen = len(testname) |
| 288 | dargs = dargs.copy() |
| 289 | tntag = dargs.pop('tag', None) |
| 290 | if tntag: # testname tag is included in reported test name |
| 291 | testname += '.' + tntag |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 292 | subdir = testname |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 293 | sdtag = dargs.pop('subdir_tag', None) |
| 294 | if sdtag: # subdir-only tag is not included in reports |
| 295 | subdir = subdir + '.' + sdtag |
| 296 | tag = subdir[namelen+1:] # '' if none |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 297 | |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 298 | outputdir = os.path.join(self.resultdir, subdir) |
| 299 | if os.path.exists(outputdir): |
| 300 | msg = ("%s already exists, test <%s> may have" |
| 301 | " already run with tag <%s>" |
| 302 | % (outputdir, testname, tag) ) |
| 303 | raise error.TestError(msg) |
| 304 | os.mkdir(outputdir) |
| 305 | |
| 306 | container = dargs.pop('container', None) |
mbligh | 65938a2 | 2007-12-10 16:58:52 +0000 | [diff] [blame] | 307 | if container: |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 308 | cname = container.get('name', None) |
| 309 | if not cname: # get old name |
| 310 | cname = container.get('container_name', None) |
| 311 | mbytes = container.get('mbytes', None) |
| 312 | if not mbytes: # get old name |
| 313 | mbytes = container.get('mem', None) |
| 314 | cpus = container.get('cpus', None) |
| 315 | if not cpus: # get old name |
| 316 | cpus = container.get('cpu', None) |
jadmanski | 87cbc7f | 2008-05-13 18:17:10 +0000 | [diff] [blame] | 317 | root = container.get('root', None) |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 318 | self.new_container(mbytes=mbytes, cpus=cpus, |
| 319 | root=root, name=cname) |
mbligh | 65938a2 | 2007-12-10 16:58:52 +0000 | [diff] [blame] | 320 | # We are running in a container now... |
| 321 | |
jadmanski | 8415f96 | 2008-05-06 20:38:53 +0000 | [diff] [blame] | 322 | def log_warning(reason): |
| 323 | self.record("WARN", subdir, testname, reason) |
| 324 | @disk_usage_monitor.watch(log_warning, "/", |
| 325 | self.max_disk_usage_rate) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 326 | def group_func(): |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 327 | try: |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 328 | self.__runtest(url, tag, args, dargs) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 329 | except error.TestNAError, detail: |
| 330 | self.record('TEST_NA', subdir, testname, |
| 331 | str(detail)) |
| 332 | raise |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 333 | except Exception, detail: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 334 | self.record('FAIL', subdir, testname, |
| 335 | str(detail)) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 336 | raise |
| 337 | else: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 338 | self.record('GOOD', subdir, testname, |
| 339 | 'completed successfully') |
jadmanski | 8415f96 | 2008-05-06 20:38:53 +0000 | [diff] [blame] | 340 | |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 341 | result, exc_info = self.__rungroup(subdir, testname, group_func) |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 342 | if container: |
| 343 | self.release_container() |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 344 | if exc_info and isinstance(exc_info[1], error.TestError): |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 345 | return False |
| 346 | elif exc_info: |
mbligh | 71ea249 | 2008-01-15 20:35:52 +0000 | [diff] [blame] | 347 | raise exc_info[0], exc_info[1], exc_info[2] |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 348 | else: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 349 | return True |
| 350 | |
| 351 | |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 352 | def __rungroup(self, subdir, testname, function, *args, **dargs): |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 353 | """\ |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 354 | subdir: |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 355 | name of the group |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 356 | testname: |
| 357 | name of the test to run, or support step |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 358 | function: |
| 359 | subroutine to run |
| 360 | *args: |
| 361 | arguments for the function |
| 362 | |
| 363 | Returns a 2-tuple (result, exc_info) where result |
| 364 | is the return value of function, and exc_info is |
| 365 | the sys.exc_info() of the exception thrown by the |
| 366 | function (which may be None). |
| 367 | """ |
| 368 | |
| 369 | result, exc_info = None, None |
| 370 | try: |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 371 | self.record('START', subdir, testname) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 372 | self._increment_group_level() |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 373 | result = function(*args, **dargs) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 374 | self._decrement_group_level() |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 375 | self.record('END GOOD', subdir, testname) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 376 | except error.TestNAError, e: |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 377 | self._decrement_group_level() |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 378 | self.record('END TEST_NA', subdir, testname, str(e)) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 379 | except Exception, e: |
| 380 | exc_info = sys.exc_info() |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 381 | self._decrement_group_level() |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 382 | err_msg = str(e) + '\n' + traceback.format_exc() |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 383 | self.record('END FAIL', subdir, testname, err_msg) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 384 | |
| 385 | return result, exc_info |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 386 | |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 387 | |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 388 | def run_group(self, function, *args, **dargs): |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 389 | """\ |
| 390 | function: |
| 391 | subroutine to run |
| 392 | *args: |
| 393 | arguments for the function |
| 394 | """ |
| 395 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 396 | # Allow the tag for the group to be specified |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 397 | name = function.__name__ |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 398 | tag = dargs.pop('tag', None) |
| 399 | if tag: |
| 400 | name = tag |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 401 | |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 402 | outputdir = os.path.join(self.resultdir, name) |
| 403 | if os.path.exists(outputdir): |
| 404 | msg = ("%s already exists, test <%s> may have" |
| 405 | " already run with tag <%s>" |
| 406 | % (outputdir, name, name) ) |
| 407 | raise error.TestError(msg) |
| 408 | os.mkdir(outputdir) |
| 409 | |
| 410 | result, exc_info = self.__rungroup(name, name, function, |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 411 | *args, **dargs) |
apw | 1da244b | 2007-09-27 17:18:01 +0000 | [diff] [blame] | 412 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 413 | # if there was a non-TestError exception, raise it |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 414 | if exc_info and not isinstance(exc_info[1], error.TestError): |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 415 | err = ''.join(traceback.format_exception(*exc_info)) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 416 | raise error.TestError(name + ' failed\n' + err) |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 417 | |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 418 | # pass back the actual return value from the function |
apw | 08403ca | 2007-09-27 17:17:22 +0000 | [diff] [blame] | 419 | return result |
| 420 | |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 421 | |
jadmanski | 87cbc7f | 2008-05-13 18:17:10 +0000 | [diff] [blame] | 422 | def new_container(self, mbytes=None, cpus=None, root=None, name=None): |
mbligh | 8ea61e2 | 2008-05-09 18:09:37 +0000 | [diff] [blame] | 423 | if not autotest_utils.grep('cpuset', '/proc/filesystems'): |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 424 | print "Containers not enabled by latest reboot" |
| 425 | return # containers weren't enabled in this kernel boot |
| 426 | pid = os.getpid() |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 427 | if not name: |
| 428 | name = 'test%d' % pid # make arbitrary unique name |
| 429 | self.container = cpuset.cpuset(name, job_size=mbytes, |
mbligh | 337bb76 | 2008-04-16 21:23:10 +0000 | [diff] [blame] | 430 | job_pid=pid, cpus=cpus, root=root) |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 431 | # This job's python shell is now running in the new container |
| 432 | # and all forked test processes will inherit that container |
| 433 | |
| 434 | |
| 435 | def release_container(self): |
| 436 | if self.container: |
mbligh | 337bb76 | 2008-04-16 21:23:10 +0000 | [diff] [blame] | 437 | self.container.release() |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 438 | self.container = None |
| 439 | |
| 440 | |
| 441 | def cpu_count(self): |
| 442 | if self.container: |
| 443 | return len(self.container.cpus) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 444 | return autotest_utils.count_cpus() # use total system count |
mbligh | 6811958 | 2008-01-25 18:16:41 +0000 | [diff] [blame] | 445 | |
| 446 | |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 447 | # Check the passed kernel identifier against the command line |
| 448 | # and the running kernel, abort the job on missmatch. |
mbligh | 38a4a11 | 2008-03-19 13:11:34 +0000 | [diff] [blame] | 449 | def kernel_check_ident(self, expected_when, expected_id, subdir, |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 450 | type = 'src', patches=[]): |
mbligh | 38a4a11 | 2008-03-19 13:11:34 +0000 | [diff] [blame] | 451 | print (("POST BOOT: checking booted kernel " + |
| 452 | "mark=%d identity='%s' type='%s'") % |
| 453 | (expected_when, expected_id, type)) |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 454 | |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 455 | running_id = autotest_utils.running_os_ident() |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 456 | |
mbligh | e829ba5 | 2008-06-03 15:04:08 +0000 | [diff] [blame] | 457 | cmdline = utils.read_one_line("/proc/cmdline") |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 458 | |
| 459 | find_sum = re.compile(r'.*IDENT=(\d+)') |
| 460 | m = find_sum.match(cmdline) |
| 461 | cmdline_when = -1 |
| 462 | if m: |
| 463 | cmdline_when = int(m.groups()[0]) |
| 464 | |
| 465 | # We have all the facts, see if they indicate we |
| 466 | # booted the requested kernel or not. |
| 467 | bad = False |
mbligh | da0311e | 2007-10-25 16:03:33 +0000 | [diff] [blame] | 468 | if (type == 'src' and expected_id != running_id or |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 469 | type == 'rpm' and |
| 470 | not running_id.startswith(expected_id + '::')): |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 471 | print "check_kernel_ident: kernel identifier mismatch" |
| 472 | bad = True |
| 473 | if expected_when != cmdline_when: |
| 474 | print "check_kernel_ident: kernel command line mismatch" |
| 475 | bad = True |
| 476 | |
| 477 | if bad: |
| 478 | print " Expected Ident: " + expected_id |
| 479 | print " Running Ident: " + running_id |
| 480 | print " Expected Mark: %d" % (expected_when) |
| 481 | print "Command Line Mark: %d" % (cmdline_when) |
| 482 | print " Command Line: " + cmdline |
| 483 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 484 | raise error.JobError("boot failure", "reboot.verify") |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 485 | |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 486 | kernel_info = {'kernel': expected_id} |
| 487 | for i, patch in enumerate(patches): |
| 488 | kernel_info["patch%d" % i] = patch |
mbligh | b7fd270 | 2008-03-25 14:57:08 +0000 | [diff] [blame] | 489 | self.record('GOOD', subdir, 'reboot.verify', expected_id) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 490 | self._decrement_group_level() |
| 491 | self.record('END GOOD', subdir, 'reboot', |
| 492 | optional_fields=kernel_info) |
apw | ce73d89 | 2007-09-25 16:53:05 +0000 | [diff] [blame] | 493 | |
| 494 | |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 495 | def filesystem(self, device, mountpoint = None, loop_size = 0): |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 496 | if not mountpoint: |
| 497 | mountpoint = self.tmpdir |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 498 | return filesystem.filesystem(self, device, mountpoint,loop_size) |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 499 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 500 | |
| 501 | def enable_external_logging(self): |
| 502 | pass |
| 503 | |
| 504 | |
| 505 | def disable_external_logging(self): |
| 506 | pass |
| 507 | |
| 508 | |
| 509 | def reboot_setup(self): |
| 510 | pass |
| 511 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 512 | |
| 513 | def reboot(self, tag='autotest'): |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 514 | self.reboot_setup() |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 515 | self.record('START', None, 'reboot') |
| 516 | self._increment_group_level() |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 517 | self.record('GOOD', None, 'reboot.start') |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 518 | self.harness.run_reboot() |
apw | 11985b7 | 2007-10-04 15:44:47 +0000 | [diff] [blame] | 519 | default = self.config_get('boot.set_default') |
| 520 | if default: |
| 521 | self.bootloader.set_default(tag) |
| 522 | else: |
| 523 | self.bootloader.boot_once(tag) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 524 | cmd = "(sleep 5; reboot) </dev/null >/dev/null 2>&1 &" |
mbligh | e829ba5 | 2008-06-03 15:04:08 +0000 | [diff] [blame] | 525 | utils.system(cmd) |
apw | 0778a2f | 2006-10-06 03:11:40 +0000 | [diff] [blame] | 526 | self.quit() |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 527 | |
| 528 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 529 | def noop(self, text): |
| 530 | print "job: noop: " + text |
| 531 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 532 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 533 | def parallel(self, *tasklist): |
| 534 | """Run tasks in parallel""" |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 535 | |
| 536 | pids = [] |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 537 | old_log_filename = self.log_filename |
| 538 | for i, task in enumerate(tasklist): |
| 539 | self.log_filename = old_log_filename + (".%d" % i) |
| 540 | task_func = lambda: task[0](*task[1:]) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 541 | pids.append(parallel.fork_start(self.resultdir, |
| 542 | task_func)) |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 543 | |
| 544 | old_log_path = os.path.join(self.resultdir, old_log_filename) |
| 545 | old_log = open(old_log_path, "a") |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 546 | exceptions = [] |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 547 | for i, pid in enumerate(pids): |
| 548 | # wait for the task to finish |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 549 | try: |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 550 | parallel.fork_waitfor(self.resultdir, pid) |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 551 | except Exception, e: |
| 552 | exceptions.append(e) |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 553 | # copy the logs from the subtask into the main log |
| 554 | new_log_path = old_log_path + (".%d" % i) |
| 555 | if os.path.exists(new_log_path): |
| 556 | new_log = open(new_log_path) |
| 557 | old_log.write(new_log.read()) |
| 558 | new_log.close() |
| 559 | old_log.flush() |
| 560 | os.remove(new_log_path) |
| 561 | old_log.close() |
| 562 | |
| 563 | self.log_filename = old_log_filename |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 564 | |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 565 | # handle any exceptions raised by the parallel tasks |
| 566 | if exceptions: |
| 567 | msg = "%d task(s) failed" % len(exceptions) |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 568 | raise error.JobError(msg, str(exceptions), exceptions) |
mbligh | d509b71 | 2008-01-14 17:41:25 +0000 | [diff] [blame] | 569 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 570 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 571 | def quit(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 572 | # XXX: should have a better name. |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 573 | self.harness.run_pause() |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 574 | raise error.JobContinue("more to come") |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 575 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 576 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 577 | def complete(self, status): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 578 | """Clean up and exit""" |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 579 | # We are about to exit 'complete' so clean up the control file. |
| 580 | try: |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 581 | os.unlink(self.state_file) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 582 | except: |
| 583 | pass |
mbligh | c0b10d3 | 2008-03-03 16:03:28 +0000 | [diff] [blame] | 584 | |
mbligh | 61a6c1a | 2006-12-25 01:26:38 +0000 | [diff] [blame] | 585 | self.harness.run_complete() |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 586 | self.disable_external_logging() |
apw | 1b02190 | 2006-04-03 17:02:56 +0000 | [diff] [blame] | 587 | sys.exit(status) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 588 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 589 | |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 590 | def set_state(self, var, val): |
| 591 | # Deep copies make sure that the state can't be altered |
| 592 | # without it being re-written. Perf wise, deep copies |
| 593 | # are overshadowed by pickling/loading. |
| 594 | self.state[var] = copy.deepcopy(val) |
| 595 | pickle.dump(self.state, open(self.state_file, 'w')) |
| 596 | |
| 597 | |
| 598 | def __load_state(self): |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 599 | assert not hasattr(self, "state") |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 600 | try: |
| 601 | self.state = pickle.load(open(self.state_file, 'r')) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 602 | self.state_existed = True |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 603 | except Exception: |
| 604 | print "Initializing the state engine." |
| 605 | self.state = {} |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 606 | self.set_state('__steps', []) # writes pickle file |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 607 | self.state_existed = False |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 608 | |
| 609 | |
| 610 | def get_state(self, var, default=None): |
| 611 | if var in self.state or default == None: |
| 612 | val = self.state[var] |
| 613 | else: |
| 614 | val = default |
| 615 | return copy.deepcopy(val) |
| 616 | |
| 617 | |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 618 | def __create_step_tuple(self, fn, args, dargs): |
| 619 | # Legacy code passes in an array where the first arg is |
| 620 | # the function or its name. |
| 621 | if isinstance(fn, list): |
| 622 | assert(len(args) == 0) |
| 623 | assert(len(dargs) == 0) |
| 624 | args = fn[1:] |
| 625 | fn = fn[0] |
| 626 | # Pickling actual functions is harry, thus we have to call |
| 627 | # them by name. Unfortunately, this means only functions |
| 628 | # defined globally can be used as a next step. |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 629 | if callable(fn): |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 630 | fn = fn.__name__ |
| 631 | if not isinstance(fn, types.StringTypes): |
| 632 | raise StepError("Next steps must be functions or " |
| 633 | "strings containing the function name") |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 634 | ancestry = copy.copy(self.current_step_ancestry) |
| 635 | return (ancestry, fn, args, dargs) |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 636 | |
| 637 | |
mbligh | 8f4d043 | 2008-06-02 19:42:50 +0000 | [diff] [blame] | 638 | def next_step_append(self, fn, *args, **dargs): |
| 639 | """Define the next step and place it at the end""" |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 640 | steps = self.get_state('__steps') |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 641 | steps.append(self.__create_step_tuple(fn, args, dargs)) |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 642 | self.set_state('__steps', steps) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 643 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 644 | |
mbligh | 8f4d043 | 2008-06-02 19:42:50 +0000 | [diff] [blame] | 645 | def next_step(self, fn, *args, **dargs): |
| 646 | """Create a new step and place it after any steps added |
| 647 | while running the current step but before any steps added in |
| 648 | previous steps""" |
| 649 | steps = self.get_state('__steps') |
| 650 | steps.insert(self.next_step_index, |
| 651 | self.__create_step_tuple(fn, args, dargs)) |
| 652 | self.next_step_index += 1 |
| 653 | self.set_state('__steps', steps) |
| 654 | |
| 655 | |
mbligh | 12a04cb | 2008-04-25 16:07:20 +0000 | [diff] [blame] | 656 | def next_step_prepend(self, fn, *args, **dargs): |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 657 | """Insert a new step, executing first""" |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 658 | steps = self.get_state('__steps') |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 659 | steps.insert(0, self.__create_step_tuple(fn, args, dargs)) |
mbligh | 8f4d043 | 2008-06-02 19:42:50 +0000 | [diff] [blame] | 660 | self.next_step_index += 1 |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 661 | self.set_state('__steps', steps) |
mbligh | 237bed3 | 2007-09-05 13:05:57 +0000 | [diff] [blame] | 662 | |
| 663 | |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 664 | def _run_step_fn(self, local_vars, fn, args, dargs): |
| 665 | """Run a (step) function within the given context""" |
| 666 | |
| 667 | local_vars['__args'] = args |
| 668 | local_vars['__dargs'] = dargs |
| 669 | exec('__ret = %s(*__args, **__dargs)' % fn, |
| 670 | local_vars, local_vars) |
| 671 | return local_vars['__ret'] |
| 672 | |
| 673 | |
| 674 | def _create_frame(self, global_vars, ancestry, fn_name): |
| 675 | """Set up the environment like it would have been when this |
| 676 | function was first defined. |
| 677 | |
| 678 | Child step engine 'implementations' must have 'return locals()' |
| 679 | at end end of their steps. Because of this, we can call the |
| 680 | parent function and get back all child functions (i.e. those |
| 681 | defined within it). |
| 682 | |
| 683 | Unfortunately, the call stack of the function calling |
| 684 | job.next_step might have been deeper than the function it |
| 685 | added. In order to make sure that the environment is what it |
| 686 | should be, we need to then pop off the frames we built until |
| 687 | we find the frame where the function was first defined.""" |
| 688 | |
| 689 | # The copies ensure that the parent frames are not modified |
| 690 | # while building child frames. This matters if we then |
| 691 | # pop some frames in the next part of this function. |
| 692 | current_frame = copy.copy(global_vars) |
| 693 | frames = [current_frame] |
| 694 | for steps_fn_name in ancestry: |
| 695 | ret = self._run_step_fn(current_frame, |
| 696 | steps_fn_name, [], {}) |
| 697 | current_frame = copy.copy(ret) |
| 698 | frames.append(current_frame) |
| 699 | |
| 700 | while len(frames) > 2: |
| 701 | if fn_name not in frames[-2]: |
| 702 | break |
| 703 | if frames[-2][fn_name] != frames[-1][fn_name]: |
| 704 | break |
| 705 | frames.pop() |
| 706 | ancestry.pop() |
| 707 | |
| 708 | return (frames[-1], ancestry) |
| 709 | |
| 710 | |
| 711 | def _add_step_init(self, local_vars, current_function): |
| 712 | """If the function returned a dictionary that includes a |
| 713 | function named 'step_init', prepend it to our list of steps. |
| 714 | This will only get run the first time a function with a nested |
| 715 | use of the step engine is run.""" |
| 716 | |
| 717 | if (isinstance(local_vars, dict) and |
| 718 | 'step_init' in local_vars and |
| 719 | callable(local_vars['step_init'])): |
| 720 | # The init step is a child of the function |
| 721 | # we were just running. |
| 722 | self.current_step_ancestry.append(current_function) |
| 723 | self.next_step_prepend('step_init') |
| 724 | |
| 725 | |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 726 | def step_engine(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 727 | """the stepping engine -- if the control file defines |
| 728 | step_init we will be using this engine to drive multiple runs. |
| 729 | """ |
| 730 | """Do the next step""" |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 731 | |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 732 | # Set up the environment and then interpret the control file. |
| 733 | # Some control files will have code outside of functions, |
| 734 | # which means we need to have our state engine initialized |
| 735 | # before reading in the file. |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 736 | global_control_vars = {'job': self} |
| 737 | exec(JOB_PREAMBLE, global_control_vars, global_control_vars) |
| 738 | execfile(self.control, global_control_vars, global_control_vars) |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 739 | |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 740 | # If we loaded in a mid-job state file, then we presumably |
| 741 | # know what steps we have yet to run. |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 742 | if not self.state_existed: |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 743 | if global_control_vars.has_key('step_init'): |
| 744 | self.next_step(global_control_vars['step_init']) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 745 | |
mbligh | 366ff1b | 2008-04-25 16:07:56 +0000 | [diff] [blame] | 746 | # Iterate through the steps. If we reboot, we'll simply |
| 747 | # continue iterating on the next step. |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 748 | while len(self.get_state('__steps')) > 0: |
| 749 | steps = self.get_state('__steps') |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 750 | (ancestry, fn_name, args, dargs) = steps.pop(0) |
mbligh | f1ae0a4 | 2008-04-25 16:09:20 +0000 | [diff] [blame] | 751 | self.set_state('__steps', steps) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 752 | |
mbligh | 8f4d043 | 2008-06-02 19:42:50 +0000 | [diff] [blame] | 753 | self.next_step_index = 0 |
mbligh | b274ef5 | 2008-06-02 19:40:01 +0000 | [diff] [blame] | 754 | ret = self._create_frame(global_control_vars, ancestry, |
| 755 | fn_name) |
| 756 | local_vars, self.current_step_ancestry = ret |
| 757 | local_vars = self._run_step_fn(local_vars, fn_name, |
| 758 | args, dargs) |
| 759 | self._add_step_init(local_vars, fn_name) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 760 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 761 | |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 762 | def _init_group_level(self): |
| 763 | self.group_level = self.get_state("__group_level", default=0) |
| 764 | |
| 765 | |
| 766 | def _increment_group_level(self): |
| 767 | self.group_level += 1 |
| 768 | self.set_state("__group_level", self.group_level) |
| 769 | |
| 770 | |
| 771 | def _decrement_group_level(self): |
| 772 | self.group_level -= 1 |
| 773 | self.set_state("__group_level", self.group_level) |
| 774 | |
| 775 | |
| 776 | def record(self, status_code, subdir, operation, status = '', |
| 777 | optional_fields=None): |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 778 | """ |
| 779 | Record job-level status |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 780 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 781 | The intent is to make this file both machine parseable and |
| 782 | human readable. That involves a little more complexity, but |
| 783 | really isn't all that bad ;-) |
| 784 | |
| 785 | Format is <status code>\t<subdir>\t<operation>\t<status> |
| 786 | |
| 787 | status code: (GOOD|WARN|FAIL|ABORT) |
| 788 | or START |
| 789 | or END (GOOD|WARN|FAIL|ABORT) |
| 790 | |
| 791 | subdir: MUST be a relevant subdirectory in the results, |
| 792 | or None, which will be represented as '----' |
| 793 | |
| 794 | operation: description of what you ran (e.g. "dbench", or |
| 795 | "mkfs -t foobar /dev/sda9") |
| 796 | |
| 797 | status: error message or "completed sucessfully" |
| 798 | |
| 799 | ------------------------------------------------------------ |
| 800 | |
| 801 | Initial tabs indicate indent levels for grouping, and is |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 802 | governed by self.group_level |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 803 | |
| 804 | multiline messages have secondary lines prefaced by a double |
| 805 | space (' ') |
| 806 | """ |
| 807 | |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 808 | if subdir: |
| 809 | if re.match(r'[\n\t]', subdir): |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 810 | raise ValueError("Invalid character in " |
| 811 | "subdir string") |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 812 | substr = subdir |
| 813 | else: |
| 814 | substr = '----' |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 815 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 816 | if not logging.is_valid_status(status_code): |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 817 | raise ValueError("Invalid status code supplied: %s" % |
| 818 | status_code) |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 819 | if not operation: |
| 820 | operation = '----' |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 821 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 822 | if re.match(r'[\n\t]', operation): |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 823 | raise ValueError("Invalid character in " |
| 824 | "operation string") |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 825 | operation = operation.rstrip() |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 826 | |
| 827 | if not optional_fields: |
| 828 | optional_fields = {} |
| 829 | |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 830 | status = status.rstrip() |
| 831 | status = re.sub(r"\t", " ", status) |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 832 | # Ensure any continuation lines are marked so we can |
| 833 | # detect them in the status file to ensure it is parsable. |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 834 | status = re.sub(r"\n", "\n" + "\t" * self.group_level + " ", |
| 835 | status) |
mbligh | 09f288a | 2007-09-18 21:34:57 +0000 | [diff] [blame] | 836 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 837 | # Generate timestamps for inclusion in the logs |
| 838 | epoch_time = int(time.time()) # seconds since epoch, in UTC |
| 839 | local_time = time.localtime(epoch_time) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 840 | optional_fields["timestamp"] = str(epoch_time) |
| 841 | optional_fields["localtime"] = time.strftime("%b %d %H:%M:%S", |
| 842 | local_time) |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 843 | |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 844 | fields = [status_code, substr, operation] |
| 845 | fields += ["%s=%s" % x for x in optional_fields.iteritems()] |
| 846 | fields.append(status) |
| 847 | |
| 848 | msg = '\t'.join(str(x) for x in fields) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 849 | msg = '\t' * self.group_level + msg |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 850 | |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 851 | msg_tag = "" |
| 852 | if "." in self.log_filename: |
| 853 | msg_tag = self.log_filename.split(".", 1)[1] |
| 854 | |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 855 | self.harness.test_status_detail(status_code, substr, |
| 856 | operation, status, msg_tag) |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 857 | self.harness.test_status(msg, msg_tag) |
| 858 | |
| 859 | # log to stdout (if enabled) |
| 860 | #if self.log_filename == self.DEFAULT_LOG_FILENAME: |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 861 | print msg |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 862 | |
| 863 | # log to the "root" status log |
| 864 | status_file = os.path.join(self.resultdir, self.log_filename) |
mbligh | 7dd510c | 2007-11-13 17:11:22 +0000 | [diff] [blame] | 865 | open(status_file, "a").write(msg + "\n") |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 866 | |
| 867 | # log to the subdir status log (if subdir is set) |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 868 | if subdir: |
mbligh | adff6ca | 2008-01-22 16:38:25 +0000 | [diff] [blame] | 869 | dir = os.path.join(self.resultdir, subdir) |
mbligh | adff6ca | 2008-01-22 16:38:25 +0000 | [diff] [blame] | 870 | status_file = os.path.join(dir, |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 871 | self.DEFAULT_LOG_FILENAME) |
mbligh | b0570ad | 2007-09-19 18:18:11 +0000 | [diff] [blame] | 872 | open(status_file, "a").write(msg + "\n") |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 873 | |
| 874 | |
jadmanski | 8415f96 | 2008-05-06 20:38:53 +0000 | [diff] [blame] | 875 | class disk_usage_monitor: |
| 876 | def __init__(self, logging_func, device, max_mb_per_hour): |
| 877 | self.func = logging_func |
| 878 | self.device = device |
| 879 | self.max_mb_per_hour = max_mb_per_hour |
| 880 | |
| 881 | |
| 882 | def start(self): |
| 883 | self.initial_space = autotest_utils.freespace(self.device) |
| 884 | self.start_time = time.time() |
| 885 | |
| 886 | |
| 887 | def stop(self): |
| 888 | # if no maximum usage rate was set, we don't need to |
| 889 | # generate any warnings |
| 890 | if not self.max_mb_per_hour: |
| 891 | return |
| 892 | |
| 893 | final_space = autotest_utils.freespace(self.device) |
| 894 | used_space = self.initial_space - final_space |
| 895 | stop_time = time.time() |
| 896 | total_time = stop_time - self.start_time |
| 897 | # round up the time to one minute, to keep extremely short |
| 898 | # tests from generating false positives due to short, badly |
| 899 | # timed bursts of activity |
| 900 | total_time = max(total_time, 60.0) |
| 901 | |
| 902 | # determine the usage rate |
| 903 | bytes_per_sec = used_space / total_time |
| 904 | mb_per_sec = bytes_per_sec / 1024**2 |
| 905 | mb_per_hour = mb_per_sec * 60 * 60 |
| 906 | |
| 907 | if mb_per_hour > self.max_mb_per_hour: |
| 908 | msg = ("disk space on %s was consumed at a rate of " |
| 909 | "%.2f MB/hour") |
| 910 | msg %= (self.device, mb_per_hour) |
| 911 | self.func(msg) |
| 912 | |
| 913 | |
| 914 | @classmethod |
| 915 | def watch(cls, *monitor_args, **monitor_dargs): |
| 916 | """ Generic decorator to wrap a function call with the |
| 917 | standard create-monitor -> start -> call -> stop idiom.""" |
| 918 | def decorator(func): |
| 919 | def watched_func(*args, **dargs): |
| 920 | monitor = cls(*monitor_args, **monitor_dargs) |
| 921 | monitor.start() |
| 922 | try: |
| 923 | func(*args, **dargs) |
| 924 | finally: |
| 925 | monitor.stop() |
| 926 | return watched_func |
| 927 | return decorator |
| 928 | |
| 929 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 930 | def runjob(control, cont = False, tag = "default", harness_type = '', |
| 931 | use_external_logging = False): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 932 | """The main interface to this module |
| 933 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 934 | control |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 935 | The control file to use for this job. |
| 936 | cont |
| 937 | Whether this is the continuation of a previously started job |
| 938 | """ |
mbligh | b4eef24 | 2007-07-23 18:22:49 +0000 | [diff] [blame] | 939 | control = os.path.abspath(control) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 940 | state = control + '.state' |
| 941 | |
| 942 | # instantiate the job object ready for the control file. |
| 943 | myjob = None |
| 944 | try: |
| 945 | # Check that the control file is valid |
| 946 | if not os.path.exists(control): |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 947 | raise error.JobError(control + |
| 948 | ": control file not found") |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 949 | |
| 950 | # When continuing, the job is complete when there is no |
| 951 | # state file, ensure we don't try and continue. |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 952 | if cont and not os.path.exists(state): |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 953 | raise error.JobComplete("all done") |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 954 | if cont == False and os.path.exists(state): |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 955 | os.unlink(state) |
| 956 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 957 | myjob = job(control, tag, cont, harness_type, |
| 958 | use_external_logging) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 959 | |
| 960 | # Load in the users control file, may do any one of: |
| 961 | # 1) execute in toto |
| 962 | # 2) define steps, and select the first via next_step() |
| 963 | myjob.step_engine() |
| 964 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 965 | except error.JobContinue: |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 966 | sys.exit(5) |
| 967 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 968 | except error.JobComplete: |
apw | b832e1b | 2007-11-24 20:24:38 +0000 | [diff] [blame] | 969 | sys.exit(1) |
| 970 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 971 | except error.JobError, instance: |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 972 | print "JOB ERROR: " + instance.args[0] |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 973 | if myjob: |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 974 | command = None |
| 975 | if len(instance.args) > 1: |
| 976 | command = instance.args[1] |
| 977 | myjob.record('ABORT', None, command, instance.args[0]) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 978 | myjob._decrement_group_level() |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 979 | myjob.record('END ABORT', None, None) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 980 | assert(myjob.group_level == 0) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 981 | myjob.complete(1) |
apw | b832e1b | 2007-11-24 20:24:38 +0000 | [diff] [blame] | 982 | else: |
| 983 | sys.exit(1) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 984 | |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 985 | except Exception, e: |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 986 | msg = str(e) + '\n' + traceback.format_exc() |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 987 | print "JOB ERROR: " + msg |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 988 | if myjob: |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 989 | myjob.record('ABORT', None, None, msg) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 990 | myjob._decrement_group_level() |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 991 | myjob.record('END ABORT', None, None) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 992 | assert(myjob.group_level == 0) |
mbligh | 9c5ac32 | 2007-10-31 18:01:59 +0000 | [diff] [blame] | 993 | myjob.complete(1) |
apw | b832e1b | 2007-11-24 20:24:38 +0000 | [diff] [blame] | 994 | else: |
| 995 | sys.exit(1) |
mbligh | 892d37f | 2007-03-01 17:03:25 +0000 | [diff] [blame] | 996 | |
mbligh | 0144e5a | 2008-03-07 18:17:53 +0000 | [diff] [blame] | 997 | # If we get here, then we assume the job is complete and good. |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 998 | myjob._decrement_group_level() |
mbligh | 0144e5a | 2008-03-07 18:17:53 +0000 | [diff] [blame] | 999 | myjob.record('END GOOD', None, None) |
jadmanski | a9c75c4 | 2008-05-01 22:05:31 +0000 | [diff] [blame] | 1000 | assert(myjob.group_level == 0) |
mbligh | 0144e5a | 2008-03-07 18:17:53 +0000 | [diff] [blame] | 1001 | |
mbligh | 892d37f | 2007-03-01 17:03:25 +0000 | [diff] [blame] | 1002 | myjob.complete(0) |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 1003 | |
| 1004 | |
| 1005 | # site_job.py may be non-existant or empty, make sure that an appropriate |
| 1006 | # site_job class is created nevertheless |
| 1007 | try: |
| 1008 | from site_job import site_job |
| 1009 | except ImportError: |
| 1010 | class site_job(base_job): |
| 1011 | pass |
| 1012 | |
| 1013 | class job(site_job): |
| 1014 | pass |
jadmanski | 87cbc7f | 2008-05-13 18:17:10 +0000 | [diff] [blame] | 1015 | |
mbligh | d660afe | 2008-06-05 22:17:53 +0000 | [diff] [blame^] | 1016 | |