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