mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 1 | """ |
| 2 | The main job wrapper for the server side. |
| 3 | |
| 4 | This is the core infrastructure. Derived from the client side job.py |
| 5 | |
| 6 | Copyright Martin J. Bligh, Andy Whitcroft 2007 |
| 7 | """ |
| 8 | |
| 9 | __author__ = """ |
| 10 | Martin J. Bligh <mbligh@google.com> |
| 11 | Andy Whitcroft <apw@shadowen.org> |
| 12 | """ |
| 13 | |
mbligh | dbdac6c | 2008-03-05 15:49:58 +0000 | [diff] [blame] | 14 | import os, sys, re, time, select, subprocess, traceback |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 15 | |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 16 | from autotest_lib.client.bin import fd_stack |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 17 | from autotest_lib.client.common_lib import error, logging |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 18 | from autotest_lib.server import test, subcommand |
| 19 | from autotest_lib.tko import db as tko_db, status_lib, utils as tko_utils |
| 20 | from autotest_lib.server.utils import * |
| 21 | |
mbligh | 3f4bced | 2007-11-05 17:55:53 +0000 | [diff] [blame] | 22 | |
mbligh | ed5a410 | 2007-11-20 00:46:41 +0000 | [diff] [blame] | 23 | # load up a control segment |
| 24 | # these are all stored in <server_dir>/control_segments |
| 25 | def load_control_segment(name): |
| 26 | server_dir = os.path.dirname(os.path.abspath(__file__)) |
mbligh | 7f86e0b | 2007-11-24 19:45:07 +0000 | [diff] [blame] | 27 | script_file = os.path.join(server_dir, "control_segments", name) |
mbligh | ed5a410 | 2007-11-20 00:46:41 +0000 | [diff] [blame] | 28 | if os.path.exists(script_file): |
| 29 | return file(script_file).read() |
| 30 | else: |
| 31 | return "" |
| 32 | |
| 33 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 34 | preamble = """\ |
| 35 | import os, sys |
| 36 | |
mbligh | ccb9e18 | 2008-04-17 15:42:10 +0000 | [diff] [blame] | 37 | from autotest_lib.server import hosts, autotest, kvm, git, standalone_profiler |
| 38 | from autotest_lib.server import source_kernel, rpm_kernel, deb_kernel |
| 39 | from autotest_lib.server import git_kernel |
| 40 | from autotest_lib.server.subcommand import * |
| 41 | from autotest_lib.server.utils import run, get_tmp_dir, sh_escape |
mbligh | ccb9e18 | 2008-04-17 15:42:10 +0000 | [diff] [blame] | 42 | from autotest_lib.client.common_lib.error import * |
| 43 | from autotest_lib.client.common_lib import barrier |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 44 | |
mbligh | 119c12a | 2007-11-12 22:13:44 +0000 | [diff] [blame] | 45 | autotest.Autotest.job = job |
mbligh | 31a49de | 2007-11-05 18:41:19 +0000 | [diff] [blame] | 46 | hosts.SSHHost.job = job |
mbligh | e1417fa | 2007-12-10 16:55:13 +0000 | [diff] [blame] | 47 | barrier = barrier.barrier |
mbligh | 1fb77cc | 2008-02-27 16:41:20 +0000 | [diff] [blame] | 48 | if len(machines) > 1: |
| 49 | open('.machines', 'w').write('\\n'.join(machines) + '\\n') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 50 | """ |
| 51 | |
| 52 | client_wrapper = """ |
| 53 | at = autotest.Autotest() |
| 54 | |
| 55 | def run_client(machine): |
| 56 | host = hosts.SSHHost(machine) |
| 57 | at.run(control, host=host) |
| 58 | |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 59 | job.parallel_simple(run_client, machines) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 60 | """ |
| 61 | |
mbligh | 303ccac | 2007-11-05 18:07:28 +0000 | [diff] [blame] | 62 | crashdumps = """ |
| 63 | def crashdumps(machine): |
| 64 | host = hosts.SSHHost(machine, initialize=False) |
| 65 | host.get_crashdumps(test_start_time) |
| 66 | |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 67 | job.parallel_simple(crashdumps, machines, log=False) |
mbligh | 303ccac | 2007-11-05 18:07:28 +0000 | [diff] [blame] | 68 | """ |
| 69 | |
mbligh | 98ff146 | 2007-12-19 16:27:55 +0000 | [diff] [blame] | 70 | reboot_segment="""\ |
| 71 | def reboot(machine): |
mbligh | 17f0c66 | 2007-11-05 18:28:19 +0000 | [diff] [blame] | 72 | host = hosts.SSHHost(machine, initialize=False) |
| 73 | host.reboot() |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 74 | |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 75 | job.parallel_simple(reboot, machines, log=False) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 76 | """ |
| 77 | |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 78 | install="""\ |
| 79 | def install(machine): |
mbligh | 17f0c66 | 2007-11-05 18:28:19 +0000 | [diff] [blame] | 80 | host = hosts.SSHHost(machine, initialize=False) |
| 81 | host.machine_install() |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 82 | |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 83 | job.parallel_simple(install, machines, log=False) |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 84 | """ |
| 85 | |
mbligh | 7f86e0b | 2007-11-24 19:45:07 +0000 | [diff] [blame] | 86 | # load up the verifier control segment, with an optional site-specific hook |
mbligh | ed5a410 | 2007-11-20 00:46:41 +0000 | [diff] [blame] | 87 | verify = load_control_segment("site_verify") |
| 88 | verify += load_control_segment("verify") |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame] | 89 | |
mbligh | 7f86e0b | 2007-11-24 19:45:07 +0000 | [diff] [blame] | 90 | # load up the repair control segment, with an optional site-specific hook |
| 91 | repair = load_control_segment("site_repair") |
| 92 | repair += load_control_segment("repair") |
| 93 | |
mbligh | 1d42d4e | 2007-11-05 22:42:00 +0000 | [diff] [blame] | 94 | |
mbligh | 970b94e | 2008-01-24 16:29:34 +0000 | [diff] [blame] | 95 | # load up site-specific code for generating site-specific job data |
| 96 | try: |
| 97 | import site_job |
| 98 | get_site_job_data = site_job.get_site_job_data |
| 99 | del site_job |
| 100 | except ImportError: |
| 101 | # by default provide a stub that generates no site data |
| 102 | def get_site_job_data(job): |
| 103 | return {} |
| 104 | |
| 105 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 106 | class base_server_job: |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 107 | """The actual job against which we do everything. |
| 108 | |
| 109 | Properties: |
| 110 | autodir |
| 111 | The top level autotest directory (/usr/local/autotest). |
| 112 | serverdir |
| 113 | <autodir>/server/ |
| 114 | clientdir |
| 115 | <autodir>/client/ |
| 116 | conmuxdir |
| 117 | <autodir>/conmux/ |
| 118 | testdir |
| 119 | <autodir>/server/tests/ |
| 120 | control |
| 121 | the control file for this job |
| 122 | """ |
| 123 | |
jadmanski | 6e8bf75 | 2008-05-14 00:17:48 +0000 | [diff] [blame] | 124 | STATUS_VERSION = 1 |
| 125 | |
| 126 | |
mbligh | e8b37a9 | 2007-12-19 15:54:11 +0000 | [diff] [blame] | 127 | def __init__(self, control, args, resultdir, label, user, machines, |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 128 | client=False, parse_job=""): |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 129 | """ |
| 130 | control |
| 131 | The control file (pathname of) |
| 132 | args |
| 133 | args to pass to the control file |
| 134 | resultdir |
| 135 | where to throw the results |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 136 | label |
| 137 | label for the job |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 138 | user |
| 139 | Username for the job (email address) |
| 140 | client |
| 141 | True if a client-side control file |
| 142 | """ |
mbligh | f5427bb | 2008-04-09 15:55:57 +0000 | [diff] [blame] | 143 | path = os.path.dirname(__file__) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 144 | self.autodir = os.path.abspath(os.path.join(path, '..')) |
| 145 | self.serverdir = os.path.join(self.autodir, 'server') |
mbligh | 0526936 | 2007-10-16 16:58:11 +0000 | [diff] [blame] | 146 | self.testdir = os.path.join(self.serverdir, 'tests') |
| 147 | self.tmpdir = os.path.join(self.serverdir, 'tmp') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 148 | self.conmuxdir = os.path.join(self.autodir, 'conmux') |
| 149 | self.clientdir = os.path.join(self.autodir, 'client') |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 150 | if control: |
| 151 | self.control = open(control, 'r').read() |
| 152 | self.control = re.sub('\r', '', self.control) |
| 153 | else: |
| 154 | self.control = None |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 155 | self.resultdir = resultdir |
| 156 | if not os.path.exists(resultdir): |
| 157 | os.mkdir(resultdir) |
mbligh | 3ccb859 | 2007-11-05 18:13:40 +0000 | [diff] [blame] | 158 | self.debugdir = os.path.join(resultdir, 'debug') |
| 159 | if not os.path.exists(self.debugdir): |
| 160 | os.mkdir(self.debugdir) |
mbligh | 3dcf2c9 | 2007-10-16 22:24:00 +0000 | [diff] [blame] | 161 | self.status = os.path.join(resultdir, 'status') |
mbligh | 18420c2 | 2007-10-16 22:27:14 +0000 | [diff] [blame] | 162 | self.label = label |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 163 | self.user = user |
| 164 | self.args = args |
mbligh | e8b37a9 | 2007-12-19 15:54:11 +0000 | [diff] [blame] | 165 | self.machines = machines |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 166 | self.client = client |
| 167 | self.record_prefix = '' |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 168 | self.warning_loggers = set() |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 169 | |
mbligh | 3f4bced | 2007-11-05 17:55:53 +0000 | [diff] [blame] | 170 | self.stdout = fd_stack.fd_stack(1, sys.stdout) |
| 171 | self.stderr = fd_stack.fd_stack(2, sys.stderr) |
| 172 | |
mbligh | 3dcf2c9 | 2007-10-16 22:24:00 +0000 | [diff] [blame] | 173 | if os.path.exists(self.status): |
| 174 | os.unlink(self.status) |
jadmanski | 6e8bf75 | 2008-05-14 00:17:48 +0000 | [diff] [blame] | 175 | job_data = {'label' : label, 'user' : user, |
| 176 | 'hostname' : ','.join(machines), |
| 177 | 'status_version' : str(self.STATUS_VERSION)} |
mbligh | 970b94e | 2008-01-24 16:29:34 +0000 | [diff] [blame] | 178 | job_data.update(get_site_job_data(self)) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 179 | write_keyval(self.resultdir, job_data) |
| 180 | |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 181 | self.parse_job = parse_job |
| 182 | if self.parse_job and len(machines) == 1: |
| 183 | self.using_parser = True |
| 184 | self.init_parser(resultdir) |
| 185 | else: |
| 186 | self.using_parser = False |
| 187 | |
| 188 | |
| 189 | def init_parser(self, resultdir): |
| 190 | """Start the continuous parsing of resultdir. This sets up |
| 191 | the database connection and inserts the basic job object into |
| 192 | the database if necessary.""" |
| 193 | # redirect parser debugging to .parse.log |
| 194 | parse_log = os.path.join(resultdir, '.parse.log') |
| 195 | parse_log = open(parse_log, 'w', 0) |
| 196 | tko_utils.redirect_parser_debugging(parse_log) |
| 197 | # create a job model object and set up the db |
| 198 | self.results_db = tko_db.db(autocommit=True) |
jadmanski | 6e8bf75 | 2008-05-14 00:17:48 +0000 | [diff] [blame] | 199 | self.parser = status_lib.parser(self.STATUS_VERSION) |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 200 | self.job_model = self.parser.make_job(resultdir) |
| 201 | self.parser.start(self.job_model) |
| 202 | # check if a job already exists in the db and insert it if |
| 203 | # it does not |
| 204 | job_idx = self.results_db.find_job(self.parse_job) |
| 205 | if job_idx is None: |
| 206 | self.results_db.insert_job(self.parse_job, |
| 207 | self.job_model) |
| 208 | else: |
| 209 | machine_idx = self.results_db.lookup_machine( |
| 210 | self.job_model.machine) |
| 211 | self.job_model.index = job_idx |
| 212 | self.job_model.machine_idx = machine_idx |
| 213 | |
| 214 | |
mbligh | fe0af11 | 2008-04-17 15:27:47 +0000 | [diff] [blame] | 215 | def cleanup_parser(self): |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 216 | """This should be called after the server job is finished |
| 217 | to carry out any remaining cleanup (e.g. flushing any |
| 218 | remaining test results to the results db)""" |
| 219 | if not self.using_parser: |
| 220 | return |
| 221 | final_tests = self.parser.end() |
| 222 | for test in final_tests: |
jadmanski | 28816c2 | 2008-05-21 18:18:05 +0000 | [diff] [blame^] | 223 | self.__insert_test(test) |
mbligh | fe0af11 | 2008-04-17 15:27:47 +0000 | [diff] [blame] | 224 | self.using_parser = False |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 225 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 226 | |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 227 | def verify(self): |
| 228 | if not self.machines: |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 229 | raise error.AutoservError( |
| 230 | 'No machines specified to verify') |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 231 | try: |
| 232 | namespace = {'machines' : self.machines, 'job' : self} |
| 233 | exec(preamble + verify, namespace, namespace) |
| 234 | except Exception, e: |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 235 | msg = ('Verify failed\n' + str(e) + '\n' |
| 236 | + traceback.format_exc()) |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 237 | self.record('ABORT', None, None, msg) |
| 238 | raise |
| 239 | |
| 240 | |
| 241 | def repair(self): |
| 242 | if not self.machines: |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 243 | raise error.AutoservError( |
| 244 | 'No machines specified to repair') |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 245 | namespace = {'machines' : self.machines, 'job' : self} |
mbligh | 16c722d | 2008-03-05 00:58:44 +0000 | [diff] [blame] | 246 | # no matter what happens during repair, go on to try to reverify |
| 247 | try: |
| 248 | exec(preamble + repair, namespace, namespace) |
| 249 | except Exception, exc: |
| 250 | print 'Exception occured during repair' |
| 251 | traceback.print_exc() |
mbligh | 8141f86 | 2008-01-25 17:20:40 +0000 | [diff] [blame] | 252 | self.verify() |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 253 | |
| 254 | |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 255 | def enable_external_logging(self): |
| 256 | """Start or restart external logging mechanism. |
| 257 | """ |
| 258 | pass |
| 259 | |
| 260 | |
| 261 | def disable_external_logging(self): |
| 262 | """ Pause or stop external logging mechanism. |
| 263 | """ |
| 264 | pass |
| 265 | |
| 266 | |
| 267 | def use_external_logging(self): |
| 268 | """Return True if external logging should be used. |
| 269 | """ |
| 270 | return False |
| 271 | |
| 272 | |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 273 | def parallel_simple(self, function, machines, log=True, timeout=None): |
| 274 | """Run 'function' using parallel_simple, with an extra |
| 275 | wrapper to handle the necessary setup for continuous parsing, |
| 276 | if possible. If continuous parsing is already properly |
| 277 | initialized then this should just work.""" |
| 278 | is_forking = not (len(machines) == 1 and |
| 279 | self.machines == machines) |
| 280 | if self.parse_job and is_forking: |
| 281 | def wrapper(machine): |
| 282 | self.parse_job += "/" + machine |
| 283 | self.using_parser = True |
| 284 | self.machines = [machine] |
| 285 | self.resultdir = os.path.join(self.resultdir, |
| 286 | machine) |
| 287 | self.init_parser(self.resultdir) |
| 288 | result = function(machine) |
mbligh | fe0af11 | 2008-04-17 15:27:47 +0000 | [diff] [blame] | 289 | self.cleanup_parser() |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 290 | return result |
| 291 | else: |
| 292 | wrapper = function |
| 293 | subcommand.parallel_simple(wrapper, machines, log, timeout) |
| 294 | |
| 295 | |
mbligh | e8b37a9 | 2007-12-19 15:54:11 +0000 | [diff] [blame] | 296 | def run(self, reboot = False, install_before = False, |
mbligh | ddd5433 | 2008-03-07 18:14:06 +0000 | [diff] [blame] | 297 | install_after = False, collect_crashdumps = True, |
| 298 | namespace = {}): |
mbligh | 60dbd50 | 2007-10-26 14:59:31 +0000 | [diff] [blame] | 299 | # use a copy so changes don't affect the original dictionary |
| 300 | namespace = namespace.copy() |
mbligh | e8b37a9 | 2007-12-19 15:54:11 +0000 | [diff] [blame] | 301 | machines = self.machines |
mbligh | 60dbd50 | 2007-10-26 14:59:31 +0000 | [diff] [blame] | 302 | |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 303 | self.aborted = False |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 304 | namespace['machines'] = machines |
| 305 | namespace['args'] = self.args |
| 306 | namespace['job'] = self |
mbligh | 6e29438 | 2007-11-05 18:11:29 +0000 | [diff] [blame] | 307 | test_start_time = int(time.time()) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 308 | |
mbligh | 87c5d88 | 2007-10-29 17:07:24 +0000 | [diff] [blame] | 309 | os.chdir(self.resultdir) |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 310 | |
| 311 | self.enable_external_logging() |
mbligh | 87c5d88 | 2007-10-29 17:07:24 +0000 | [diff] [blame] | 312 | status_log = os.path.join(self.resultdir, 'status.log') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 313 | try: |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 314 | if install_before and machines: |
| 315 | exec(preamble + install, namespace, namespace) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 316 | if self.client: |
| 317 | namespace['control'] = self.control |
| 318 | open('control', 'w').write(self.control) |
| 319 | open('control.srv', 'w').write(client_wrapper) |
| 320 | server_control = client_wrapper |
| 321 | else: |
| 322 | open('control.srv', 'w').write(self.control) |
| 323 | server_control = self.control |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 324 | exec(preamble + server_control, namespace, namespace) |
| 325 | |
| 326 | finally: |
mbligh | ddd5433 | 2008-03-07 18:14:06 +0000 | [diff] [blame] | 327 | if machines and collect_crashdumps: |
mbligh | 6e29438 | 2007-11-05 18:11:29 +0000 | [diff] [blame] | 328 | namespace['test_start_time'] = test_start_time |
mbligh | 98ff146 | 2007-12-19 16:27:55 +0000 | [diff] [blame] | 329 | exec(preamble + crashdumps, |
| 330 | namespace, namespace) |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 331 | self.disable_external_logging() |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 332 | if reboot and machines: |
mbligh | 98ff146 | 2007-12-19 16:27:55 +0000 | [diff] [blame] | 333 | exec(preamble + reboot_segment, |
| 334 | namespace, namespace) |
mbligh | f36243d | 2007-10-30 15:36:16 +0000 | [diff] [blame] | 335 | if install_after and machines: |
| 336 | exec(preamble + install, namespace, namespace) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 337 | |
| 338 | |
| 339 | def run_test(self, url, *args, **dargs): |
| 340 | """Summon a test object and run it. |
| 341 | |
| 342 | tag |
| 343 | tag to add to testname |
| 344 | url |
| 345 | url of the test to run |
| 346 | """ |
| 347 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 348 | (group, testname) = test.testname(url) |
| 349 | tag = None |
| 350 | subdir = testname |
mbligh | 43ac522 | 2007-10-16 15:55:01 +0000 | [diff] [blame] | 351 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 352 | if dargs.has_key('tag'): |
| 353 | tag = dargs['tag'] |
| 354 | del dargs['tag'] |
| 355 | if tag: |
| 356 | subdir += '.' + tag |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 357 | |
mbligh | 43ac522 | 2007-10-16 15:55:01 +0000 | [diff] [blame] | 358 | try: |
| 359 | test.runtest(self, url, tag, args, dargs) |
| 360 | self.record('GOOD', subdir, testname, 'completed successfully') |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 361 | except error.TestNAError, detail: |
| 362 | self.record('TEST_NA', subdir, testmame, str(detail)) |
mbligh | 43ac522 | 2007-10-16 15:55:01 +0000 | [diff] [blame] | 363 | except Exception, detail: |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 364 | info = str(detail) + "\n" + traceback.format_exc() |
| 365 | self.record('FAIL', subdir, testname, info) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 366 | |
| 367 | |
| 368 | def run_group(self, function, *args, **dargs): |
| 369 | """\ |
| 370 | function: |
| 371 | subroutine to run |
| 372 | *args: |
| 373 | arguments for the function |
| 374 | """ |
| 375 | |
| 376 | result = None |
| 377 | name = function.__name__ |
| 378 | |
| 379 | # Allow the tag for the group to be specified. |
| 380 | if dargs.has_key('tag'): |
| 381 | tag = dargs['tag'] |
| 382 | del dargs['tag'] |
| 383 | if tag: |
| 384 | name = tag |
| 385 | |
| 386 | # if tag: |
| 387 | # name += '.' + tag |
| 388 | old_record_prefix = self.record_prefix |
| 389 | try: |
| 390 | try: |
| 391 | self.record('START', None, name) |
| 392 | self.record_prefix += '\t' |
| 393 | result = function(*args, **dargs) |
| 394 | self.record_prefix = old_record_prefix |
| 395 | self.record('END GOOD', None, name) |
| 396 | except: |
| 397 | self.record_prefix = old_record_prefix |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 398 | self.record('END FAIL', None, name, |
| 399 | traceback.format_exc()) |
| 400 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 401 | # We don't want to raise up an error higher if it's just |
| 402 | # a TestError - we want to carry on to other tests. Hence |
| 403 | # this outer try/except block. |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 404 | except error.TestError: |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 405 | pass |
| 406 | except: |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 407 | raise error.TestError(name + ' failed\n' + |
mbligh | 0a1727a | 2008-04-21 18:10:07 +0000 | [diff] [blame] | 408 | traceback.format_exc()) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 409 | |
| 410 | return result |
| 411 | |
| 412 | |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 413 | def record(self, status_code, subdir, operation, status=''): |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 414 | """ |
| 415 | Record job-level status |
| 416 | |
| 417 | The intent is to make this file both machine parseable and |
| 418 | human readable. That involves a little more complexity, but |
| 419 | really isn't all that bad ;-) |
| 420 | |
| 421 | Format is <status code>\t<subdir>\t<operation>\t<status> |
| 422 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 423 | status code: see common_lib.logging.is_valid_status() |
| 424 | for valid status definition |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 425 | |
| 426 | subdir: MUST be a relevant subdirectory in the results, |
| 427 | or None, which will be represented as '----' |
| 428 | |
| 429 | operation: description of what you ran (e.g. "dbench", or |
| 430 | "mkfs -t foobar /dev/sda9") |
| 431 | |
| 432 | status: error message or "completed sucessfully" |
| 433 | |
| 434 | ------------------------------------------------------------ |
| 435 | |
| 436 | Initial tabs indicate indent levels for grouping, and is |
| 437 | governed by self.record_prefix |
| 438 | |
| 439 | multiline messages have secondary lines prefaced by a double |
| 440 | space (' ') |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 441 | |
| 442 | Executing this method will trigger the logging of all new |
| 443 | warnings to date from the various console loggers. |
| 444 | """ |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 445 | # poll all our warning loggers for new warnings |
| 446 | warnings = self._read_warnings() |
| 447 | for timestamp, msg in warnings: |
| 448 | self.__record("WARN", None, None, msg, timestamp) |
| 449 | |
| 450 | # write out the actual status log line |
| 451 | self.__record(status_code, subdir, operation, status) |
| 452 | |
| 453 | |
| 454 | def _read_warnings(self): |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 455 | warnings = [] |
| 456 | while True: |
| 457 | # pull in a line of output from every logger that has |
| 458 | # output ready to be read |
| 459 | loggers, _, _ = select.select(self.warning_loggers, |
| 460 | [], [], 0) |
| 461 | closed_loggers = set() |
| 462 | for logger in loggers: |
| 463 | line = logger.readline() |
| 464 | # record any broken pipes (aka line == empty) |
| 465 | if len(line) == 0: |
| 466 | closed_loggers.add(logger) |
| 467 | continue |
| 468 | timestamp, msg = line.split('\t', 1) |
| 469 | warnings.append((int(timestamp), msg.strip())) |
| 470 | |
| 471 | # stop listening to loggers that are closed |
| 472 | self.warning_loggers -= closed_loggers |
| 473 | |
| 474 | # stop if none of the loggers have any output left |
| 475 | if not loggers: |
| 476 | break |
| 477 | |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 478 | # sort into timestamp order |
| 479 | warnings.sort() |
| 480 | return warnings |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 481 | |
| 482 | |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 483 | def _render_record(self, status_code, subdir, operation, status='', |
| 484 | epoch_time=None, record_prefix=None): |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 485 | """ |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 486 | Internal Function to generate a record to be written into a |
| 487 | status log. For use by server_job.* classes only. |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 488 | """ |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 489 | if subdir: |
| 490 | if re.match(r'[\n\t]', subdir): |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 491 | raise ValueError( |
| 492 | 'Invalid character in subdir string') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 493 | substr = subdir |
| 494 | else: |
| 495 | substr = '----' |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 496 | |
mbligh | 302482e | 2008-05-01 20:06:16 +0000 | [diff] [blame] | 497 | if not logging.is_valid_status(status_code): |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 498 | raise ValueError('Invalid status code supplied: %s' % |
| 499 | status_code) |
mbligh | e25fd5b | 2008-01-22 17:23:37 +0000 | [diff] [blame] | 500 | if not operation: |
| 501 | operation = '----' |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 502 | if re.match(r'[\n\t]', operation): |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 503 | raise ValueError( |
| 504 | 'Invalid character in operation string') |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 505 | operation = operation.rstrip() |
| 506 | status = status.rstrip() |
| 507 | status = re.sub(r"\t", " ", status) |
| 508 | # Ensure any continuation lines are marked so we can |
| 509 | # detect them in the status file to ensure it is parsable. |
| 510 | status = re.sub(r"\n", "\n" + self.record_prefix + " ", status) |
| 511 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 512 | # Generate timestamps for inclusion in the logs |
mbligh | f4e0415 | 2008-02-21 16:05:53 +0000 | [diff] [blame] | 513 | if epoch_time is None: |
| 514 | epoch_time = int(time.time()) |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 515 | local_time = time.localtime(epoch_time) |
| 516 | epoch_time_str = "timestamp=%d" % (epoch_time,) |
| 517 | local_time_str = time.strftime("localtime=%b %d %H:%M:%S", |
| 518 | local_time) |
| 519 | |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 520 | if record_prefix is None: |
| 521 | record_prefix = self.record_prefix |
| 522 | |
mbligh | 3027030 | 2007-11-05 20:33:52 +0000 | [diff] [blame] | 523 | msg = '\t'.join(str(x) for x in (status_code, substr, operation, |
| 524 | epoch_time_str, local_time_str, |
| 525 | status)) |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 526 | return record_prefix + msg + '\n' |
| 527 | |
| 528 | |
| 529 | def _record_prerendered(self, msg): |
| 530 | """ |
| 531 | Record a pre-rendered msg into the status logs. The only |
| 532 | change this makes to the message is to add on the local |
| 533 | indentation. Should not be called outside of server_job.* |
| 534 | classes. Unlike __record, this does not write the message |
| 535 | to standard output. |
| 536 | """ |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 537 | lines = [] |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 538 | status_file = os.path.join(self.resultdir, 'status.log') |
| 539 | status_log = open(status_file, 'a') |
| 540 | for line in msg.splitlines(): |
| 541 | line = self.record_prefix + line + '\n' |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 542 | lines.append(line) |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 543 | status_log.write(line) |
| 544 | status_log.close() |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 545 | self.__parse_status(lines) |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 546 | |
| 547 | |
| 548 | def __record(self, status_code, subdir, operation, status='', |
| 549 | epoch_time=None): |
| 550 | """ |
| 551 | Actual function for recording a single line into the status |
| 552 | logs. Should never be called directly, only by job.record as |
| 553 | this would bypass the console monitor logging. |
| 554 | """ |
| 555 | |
| 556 | msg = self._render_record(status_code, subdir, operation, |
| 557 | status, epoch_time) |
| 558 | |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 559 | |
mbligh | 31a49de | 2007-11-05 18:41:19 +0000 | [diff] [blame] | 560 | status_file = os.path.join(self.resultdir, 'status.log') |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 561 | sys.stdout.write(msg) |
| 562 | open(status_file, "a").write(msg) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 563 | if subdir: |
mbligh | d56eb59 | 2008-01-22 16:36:34 +0000 | [diff] [blame] | 564 | test_dir = os.path.join(self.resultdir, subdir) |
| 565 | if not os.path.exists(test_dir): |
| 566 | os.mkdir(test_dir) |
| 567 | status_file = os.path.join(test_dir, 'status') |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 568 | open(status_file, "a").write(msg) |
jadmanski | 96bb764 | 2008-05-15 17:58:16 +0000 | [diff] [blame] | 569 | self.__parse_status(msg.splitlines()) |
mbligh | b03ba64 | 2008-03-13 17:37:17 +0000 | [diff] [blame] | 570 | |
| 571 | |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 572 | def __parse_status(self, new_lines): |
| 573 | if not self.using_parser: |
| 574 | return |
| 575 | new_tests = self.parser.process_lines(new_lines) |
| 576 | for test in new_tests: |
jadmanski | 28816c2 | 2008-05-21 18:18:05 +0000 | [diff] [blame^] | 577 | self.__insert_test(test) |
| 578 | |
| 579 | |
| 580 | def __insert_test(self, test): |
| 581 | """ An internal method to insert a new test result into the |
| 582 | database. This method will not raise an exception, even if an |
| 583 | error occurs during the insert, to avoid failing a test |
| 584 | simply because of unexpected database issues.""" |
| 585 | try: |
mbligh | 6437ff5 | 2008-04-17 15:24:38 +0000 | [diff] [blame] | 586 | self.results_db.insert_test(self.job_model, test) |
jadmanski | 28816c2 | 2008-05-21 18:18:05 +0000 | [diff] [blame^] | 587 | except Exception: |
| 588 | msg = ("WARNING: An unexpected error occured while " |
| 589 | "inserting test results into the database. " |
| 590 | "Ignoring error.\n" + traceback.format_exc()) |
| 591 | print >> sys.stderr, msg |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 592 | |
| 593 | |
| 594 | # a file-like object for catching stderr from an autotest client and |
| 595 | # extracting status logs from it |
| 596 | class client_logger(object): |
| 597 | """Partial file object to write to both stdout and |
| 598 | the status log file. We only implement those methods |
| 599 | utils.run() actually calls. |
| 600 | """ |
| 601 | parser = re.compile(r"^AUTOTEST_STATUS:([^:]*):(.*)$") |
| 602 | extract_indent = re.compile(r"^(\t*).*$") |
| 603 | |
| 604 | def __init__(self, job): |
| 605 | self.job = job |
| 606 | self.leftover = "" |
| 607 | self.last_line = "" |
| 608 | self.logs = {} |
| 609 | |
| 610 | |
| 611 | def _process_log_dict(self, log_dict): |
| 612 | log_list = log_dict.pop("logs", []) |
| 613 | for key in sorted(log_dict.iterkeys()): |
| 614 | log_list += self._process_log_dict(log_dict.pop(key)) |
| 615 | return log_list |
| 616 | |
| 617 | |
| 618 | def _process_logs(self): |
| 619 | """Go through the accumulated logs in self.log and print them |
| 620 | out to stdout and the status log. Note that this processes |
| 621 | logs in an ordering where: |
| 622 | |
| 623 | 1) logs to different tags are never interleaved |
| 624 | 2) logs to x.y come before logs to x.y.z for all z |
| 625 | 3) logs to x.y come before x.z whenever y < z |
| 626 | |
| 627 | Note that this will in general not be the same as the |
| 628 | chronological ordering of the logs. However, if a chronological |
| 629 | ordering is desired that one can be reconstructed from the |
| 630 | status log by looking at timestamp lines.""" |
| 631 | log_list = self._process_log_dict(self.logs) |
| 632 | for line in log_list: |
| 633 | self.job._record_prerendered(line + '\n') |
| 634 | if log_list: |
| 635 | self.last_line = log_list[-1] |
| 636 | |
| 637 | |
| 638 | def _process_quoted_line(self, tag, line): |
| 639 | """Process a line quoted with an AUTOTEST_STATUS flag. If the |
| 640 | tag is blank then we want to push out all the data we've been |
| 641 | building up in self.logs, and then the newest line. If the |
| 642 | tag is not blank, then push the line into the logs for handling |
| 643 | later.""" |
| 644 | print line |
| 645 | if tag == "": |
| 646 | self._process_logs() |
| 647 | self.job._record_prerendered(line + '\n') |
| 648 | self.last_line = line |
| 649 | else: |
| 650 | tag_parts = [int(x) for x in tag.split(".")] |
| 651 | log_dict = self.logs |
| 652 | for part in tag_parts: |
| 653 | log_dict = log_dict.setdefault(part, {}) |
| 654 | log_list = log_dict.setdefault("logs", []) |
| 655 | log_list.append(line) |
| 656 | |
| 657 | |
| 658 | def _process_line(self, line): |
| 659 | """Write out a line of data to the appropriate stream. Status |
| 660 | lines sent by autotest will be prepended with |
| 661 | "AUTOTEST_STATUS", and all other lines are ssh error |
| 662 | messages.""" |
| 663 | match = self.parser.search(line) |
| 664 | if match: |
| 665 | tag, line = match.groups() |
| 666 | self._process_quoted_line(tag, line) |
| 667 | else: |
mbligh | fe749d2 | 2008-03-07 18:14:46 +0000 | [diff] [blame] | 668 | print line |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 669 | |
| 670 | |
| 671 | def _format_warnings(self, last_line, warnings): |
mbligh | 71d340d | 2008-03-05 15:51:16 +0000 | [diff] [blame] | 672 | # use the indentation of whatever the last log line was |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 673 | indent = self.extract_indent.match(last_line).group(1) |
mbligh | 71d340d | 2008-03-05 15:51:16 +0000 | [diff] [blame] | 674 | # if the last line starts a new group, add an extra indent |
| 675 | if last_line.lstrip('\t').startswith("START\t"): |
| 676 | indent += '\t' |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 677 | return [self.job._render_record("WARN", None, None, msg, |
| 678 | timestamp, indent).rstrip('\n') |
| 679 | for timestamp, msg in warnings] |
| 680 | |
| 681 | |
| 682 | def _process_warnings(self, last_line, log_dict, warnings): |
| 683 | if log_dict.keys() in ([], ["logs"]): |
| 684 | # there are no sub-jobs, just append the warnings here |
| 685 | warnings = self._format_warnings(last_line, warnings) |
| 686 | log_list = log_dict.setdefault("logs", []) |
| 687 | log_list += warnings |
| 688 | for warning in warnings: |
| 689 | sys.stdout.write(warning + '\n') |
| 690 | else: |
| 691 | # there are sub-jobs, so put the warnings in there |
| 692 | log_list = log_dict.get("logs", []) |
| 693 | if log_list: |
| 694 | last_line = log_list[-1] |
| 695 | for key in sorted(log_dict.iterkeys()): |
| 696 | if key != "logs": |
| 697 | self._process_warnings(last_line, |
| 698 | log_dict[key], |
| 699 | warnings) |
| 700 | |
| 701 | |
| 702 | def write(self, data): |
| 703 | # first check for any new console warnings |
| 704 | warnings = self.job._read_warnings() |
| 705 | self._process_warnings(self.last_line, self.logs, warnings) |
| 706 | # now process the newest data written out |
| 707 | data = self.leftover + data |
| 708 | lines = data.split("\n") |
| 709 | # process every line but the last one |
| 710 | for line in lines[:-1]: |
| 711 | self._process_line(line) |
| 712 | # save the last line for later processing |
| 713 | # since we may not have the whole line yet |
| 714 | self.leftover = lines[-1] |
| 715 | |
| 716 | |
| 717 | def flush(self): |
| 718 | sys.stdout.flush() |
mbligh | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 719 | |
| 720 | |
| 721 | def close(self): |
| 722 | if self.leftover: |
| 723 | self._process_line(self.leftover) |
| 724 | self._process_logs() |
| 725 | self.flush() |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 726 | |
| 727 | # site_server_job.py may be non-existant or empty, make sure that an |
| 728 | # appropriate site_server_job class is created nevertheless |
| 729 | try: |
mbligh | ccb9e18 | 2008-04-17 15:42:10 +0000 | [diff] [blame] | 730 | from autotest_lib.server.site_server_job import site_server_job |
mbligh | caa62c2 | 2008-04-07 21:51:17 +0000 | [diff] [blame] | 731 | except ImportError: |
| 732 | class site_server_job(base_server_job): |
| 733 | pass |
| 734 | |
| 735 | class server_job(site_server_job): |
| 736 | pass |