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