mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. Released under the GPL v2 |
| 4 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 5 | """ |
| 6 | This module defines the Autotest class |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 7 | |
| 8 | Autotest: software to run tests automatically |
| 9 | """ |
| 10 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 11 | __author__ = """ |
| 12 | mbligh@google.com (Martin J. Bligh), |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 13 | poirier@google.com (Benjamin Poirier), |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 14 | stutsman@google.com (Ryan Stutsman) |
| 15 | """ |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 16 | |
| 17 | import re |
| 18 | import os |
| 19 | import sys |
| 20 | import subprocess |
| 21 | import urllib |
| 22 | import tempfile |
| 23 | import shutil |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 24 | import time |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 25 | |
| 26 | import installable_object |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 27 | import utils |
mbligh | 119c12a | 2007-11-12 22:13:44 +0000 | [diff] [blame] | 28 | from common import logging |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 29 | from common.error import * |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | AUTOTEST_SVN = 'svn://test.kernel.org/autotest/trunk/client' |
| 33 | AUTOTEST_HTTP = 'http://test.kernel.org/svn/autotest/trunk/client' |
| 34 | |
| 35 | # Timeouts for powering down and up respectively |
| 36 | HALT_TIME = 300 |
mbligh | 07c1eac | 2007-11-05 18:39:29 +0000 | [diff] [blame] | 37 | BOOT_TIME = 1800 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 38 | |
| 39 | |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 40 | class AutotestRunError(AutoservRunError): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 41 | pass |
| 42 | |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 43 | class AutotestTimeoutError(AutoservRunError): |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 44 | """This exception is raised when an autotest test exceeds the timeout |
| 45 | parameter passed to run_timed_test and is killed. |
| 46 | """ |
| 47 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 48 | |
| 49 | class Autotest(installable_object.InstallableObject): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 50 | """ |
| 51 | This class represents the Autotest program. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 52 | |
| 53 | Autotest is used to run tests automatically and collect the results. |
| 54 | It also supports profilers. |
| 55 | |
| 56 | Implementation details: |
| 57 | This is a leaf class in an abstract class hierarchy, it must |
| 58 | implement the unimplemented methods in parent classes. |
| 59 | """ |
mbligh | 119c12a | 2007-11-12 22:13:44 +0000 | [diff] [blame] | 60 | job = None |
| 61 | |
| 62 | |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 63 | def __init__(self, host = None): |
| 64 | self.host = host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 65 | self.got = False |
| 66 | self.installed = False |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 67 | self.serverdir = utils.get_server_dir() |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 68 | super(Autotest, self).__init__() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 69 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 70 | |
mbligh | 119c12a | 2007-11-12 22:13:44 +0000 | [diff] [blame] | 71 | @logging.record |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 72 | def install(self, host = None): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 73 | """ |
| 74 | Install autotest. If get() was not called previously, an |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 75 | attempt will be made to install from the autotest svn |
| 76 | repository. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 77 | |
| 78 | Args: |
| 79 | host: a Host instance on which autotest will be |
| 80 | installed |
| 81 | |
| 82 | Raises: |
| 83 | AutoservError: if a tarball was not specified and |
| 84 | the target host does not have svn installed in its path |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 85 | |
| 86 | TODO(poirier): check dependencies |
| 87 | autotest needs: |
| 88 | bzcat |
| 89 | liboptdev (oprofile) |
| 90 | binutils-dev (oprofile) |
| 91 | make |
mbligh | 629e39e | 2007-08-10 19:32:00 +0000 | [diff] [blame] | 92 | psutils (netperf) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 93 | """ |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 94 | if not host: |
| 95 | host = self.host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 96 | if not self.got: |
| 97 | self.get() |
mbligh | 7386abc | 2007-08-31 08:57:56 +0000 | [diff] [blame] | 98 | host.ensure_up() |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 99 | host.setup() |
mbligh | b84a1cf | 2007-08-09 23:08:13 +0000 | [diff] [blame] | 100 | print "Installing autotest on %s" % host.hostname |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 101 | |
| 102 | autodir = _get_autodir(host) |
| 103 | host.run('mkdir -p "%s"' % utils.sh_escape(autodir)) |
| 104 | |
| 105 | if getattr(host, 'site_install_autotest', None): |
| 106 | if host.site_install_autotest(): |
| 107 | self.installed = True |
| 108 | return |
| 109 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 110 | # try to install from file or directory |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 111 | if self.source_material: |
| 112 | if os.path.isdir(self.source_material): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 113 | # Copy autotest recursively |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 114 | host.send_file(self.source_material, autodir) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 115 | else: |
| 116 | # Copy autotest via tarball |
mbligh | 4d6feff | 2008-01-14 16:48:56 +0000 | [diff] [blame] | 117 | e_msg = 'Installation method not yet implemented!' |
| 118 | raise NotImplementedError(e_msg) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 119 | print "Installation of autotest completed" |
| 120 | self.installed = True |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 121 | return |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 122 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 123 | # if that fails try to install using svn |
| 124 | if utils.run('which svn').exit_status: |
| 125 | raise AutoservError('svn not found in path on \ |
| 126 | target machine: %s' % host.name) |
| 127 | try: |
| 128 | host.run('svn checkout %s %s' % |
mbligh | 3850f89 | 2007-11-05 22:49:24 +0000 | [diff] [blame] | 129 | (AUTOTEST_SVN, autodir)) |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 130 | except AutoservRunError, e: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 131 | host.run('svn checkout %s %s' % |
mbligh | 3850f89 | 2007-11-05 22:49:24 +0000 | [diff] [blame] | 132 | (AUTOTEST_HTTP, autodir)) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 133 | print "Installation of autotest completed" |
| 134 | self.installed = True |
| 135 | |
| 136 | |
| 137 | def get(self, location = None): |
| 138 | if not location: |
| 139 | location = os.path.join(self.serverdir, '../client') |
| 140 | location = os.path.abspath(location) |
mbligh | 8fc0e5a | 2007-10-11 18:39:03 +0000 | [diff] [blame] | 141 | # If there's stuff run on our client directory already, it |
| 142 | # can cause problems. Try giving it a quick clean first. |
| 143 | cwd = os.getcwd() |
| 144 | os.chdir(location) |
| 145 | os.system('tools/make_clean') |
| 146 | os.chdir(cwd) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 147 | super(Autotest, self).get(location) |
| 148 | self.got = True |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 149 | |
| 150 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 151 | def run(self, control_file, results_dir = '.', host = None, |
| 152 | timeout=None): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 153 | """ |
| 154 | Run an autotest job on the remote machine. |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 155 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 156 | Args: |
| 157 | control_file: an open file-like-obj of the control file |
| 158 | results_dir: a str path where the results should be stored |
| 159 | on the local filesystem |
| 160 | host: a Host instance on which the control file should |
| 161 | be run |
| 162 | |
| 163 | Raises: |
| 164 | AutotestRunError: if there is a problem executing |
| 165 | the control file |
| 166 | """ |
mbligh | 55a2a3b | 2007-09-30 01:27:55 +0000 | [diff] [blame] | 167 | results_dir = os.path.abspath(results_dir) |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 168 | if not host: |
| 169 | host = self.host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 170 | if not self.installed: |
mbligh | 7fdd169 | 2007-10-02 19:16:53 +0000 | [diff] [blame] | 171 | self.install(host) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 172 | |
mbligh | dbe4a38 | 2007-07-26 19:41:28 +0000 | [diff] [blame] | 173 | host.ensure_up() |
| 174 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 175 | atrun = _Run(host, results_dir) |
mbligh | e4f0151 | 2007-08-10 01:42:50 +0000 | [diff] [blame] | 176 | try: |
| 177 | atrun.verify_machine() |
| 178 | except: |
| 179 | print "Verify machine failed on %s. Reinstalling" % \ |
| 180 | host.hostname |
| 181 | self.install(host) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 182 | atrun.verify_machine() |
| 183 | debug = os.path.join(results_dir, 'debug') |
mbligh | 55a2a3b | 2007-09-30 01:27:55 +0000 | [diff] [blame] | 184 | try: |
| 185 | os.makedirs(debug) |
| 186 | except: |
| 187 | pass |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 188 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 189 | # Ready .... Aim .... |
mbligh | a4bece1 | 2007-11-24 19:37:14 +0000 | [diff] [blame] | 190 | for control in [atrun.remote_control_file, |
| 191 | atrun.remote_control_file + '.state', |
| 192 | atrun.manual_control_file, |
| 193 | atrun.manual_control_file + '.state']: |
| 194 | host.run('rm -f ' + control) |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 195 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 196 | # Copy control_file to remote_control_file on the host |
| 197 | tmppath = utils.get(control_file) |
| 198 | host.send_file(tmppath, atrun.remote_control_file) |
mbligh | 4918bdb | 2007-12-13 16:06:09 +0000 | [diff] [blame] | 199 | if os.path.abspath(tmppath) != os.path.abspath(control_file): |
| 200 | os.remove(tmppath) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 201 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 202 | try: |
| 203 | atrun.execute_control(timeout=timeout) |
mbligh | 35b225c | 2007-12-10 17:17:27 +0000 | [diff] [blame] | 204 | finally: |
mbligh | 1f19490 | 2007-12-20 01:31:43 +0000 | [diff] [blame] | 205 | # make an effort to wait for the machine to come up |
| 206 | try: |
| 207 | host.ensure_up() |
| 208 | except AutoservError: |
| 209 | # don't worry about any errors, we'll try and |
| 210 | # get the results anyway |
| 211 | pass |
| 212 | |
mbligh | 35b225c | 2007-12-10 17:17:27 +0000 | [diff] [blame] | 213 | # get the results |
| 214 | results = os.path.join(atrun.autodir, 'results', |
| 215 | 'default') |
| 216 | # Copy all dirs in default to results_dir |
| 217 | host.get_file(results + '/', results_dir) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 218 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 219 | |
| 220 | def run_timed_test(self, test_name, results_dir = '.', host = None, |
| 221 | timeout=None, *args, **dargs): |
mbligh | d54832b | 2007-07-25 16:46:56 +0000 | [diff] [blame] | 222 | """ |
| 223 | Assemble a tiny little control file to just run one test, |
| 224 | and run it as an autotest client-side test |
| 225 | """ |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 226 | if not host: |
| 227 | host = self.host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 228 | if not self.installed: |
mbligh | 7fdd169 | 2007-10-02 19:16:53 +0000 | [diff] [blame] | 229 | self.install(host) |
mbligh | 271d5af | 2007-08-10 19:54:01 +0000 | [diff] [blame] | 230 | opts = ["%s=%s" % (o[0], repr(o[1])) for o in dargs.items()] |
| 231 | cmd = ", ".join([repr(test_name)] + map(repr, args) + opts) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 232 | control = "job.run_test(%s)\n" % cmd |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 233 | self.run(control, results_dir, host, timeout=timeout) |
| 234 | |
| 235 | |
| 236 | def run_test(self, test_name, results_dir = '.', host = None, |
| 237 | *args, **dargs): |
| 238 | self.run_timed_test(test_name, results_dir, host, None, |
| 239 | *args, **dargs) |
mbligh | d54832b | 2007-07-25 16:46:56 +0000 | [diff] [blame] | 240 | |
| 241 | |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 242 | # a file-like object for catching stderr from the autotest client and |
| 243 | # extracting status logs from it |
| 244 | class StdErrRedirector(object): |
| 245 | """Partial file object to write to both stdout and |
| 246 | the status log file. We only implement those methods |
| 247 | utils.run() actually calls. |
| 248 | """ |
| 249 | parser = re.compile(r"^AUTOTEST_STATUS:([^:]*):(.*)$") |
| 250 | |
| 251 | def __init__(self, status_log): |
| 252 | self.status_log = status_log |
| 253 | self.leftover = "" |
| 254 | self.last_line = "" |
| 255 | self.logs = {} |
| 256 | |
| 257 | |
| 258 | def _process_log_dict(self, log_dict): |
| 259 | log_list = log_dict.pop("logs", []) |
| 260 | for key in sorted(log_dict.iterkeys()): |
| 261 | log_list += self._process_log_dict(log_dict.pop(key)) |
| 262 | return log_list |
| 263 | |
| 264 | |
| 265 | def _process_logs(self): |
| 266 | """Go through the accumulated logs in self.log and print them |
| 267 | out to stdout and the status log. Note that this processes |
| 268 | logs in an ordering where: |
| 269 | |
| 270 | 1) logs to different tags are never interleaved |
| 271 | 2) logs to x.y come before logs to x.y.z for all z |
| 272 | 3) logs to x.y come before x.z whenever y < z |
| 273 | |
| 274 | Note that this will in general not be the same as the |
| 275 | chronological ordering of the logs. However, if a chronological |
| 276 | ordering is desired that one can be reconstructed from the |
| 277 | status log by looking at timestamp lines.""" |
| 278 | log_list = self._process_log_dict(self.logs) |
| 279 | for line in log_list: |
| 280 | print >> self.status_log, line |
| 281 | if log_list: |
| 282 | self.last_line = log_list[-1] |
| 283 | |
| 284 | |
| 285 | def _process_quoted_line(self, tag, line): |
| 286 | """Process a line quoted with an AUTOTEST_STATUS flag. If the |
| 287 | tag is blank then we want to push out all the data we've been |
| 288 | building up in self.logs, and then the newest line. If the |
| 289 | tag is not blank, then push the line into the logs for handling |
| 290 | later.""" |
| 291 | print line |
| 292 | if tag == "": |
| 293 | self._process_logs() |
| 294 | print >> self.status_log, line |
| 295 | self.last_line = line |
| 296 | else: |
| 297 | tag_parts = [int(x) for x in tag.split(".")] |
| 298 | log_dict = self.logs |
| 299 | for part in tag_parts: |
| 300 | log_dict = log_dict.setdefault(part, {}) |
| 301 | log_list = log_dict.setdefault("logs", []) |
| 302 | log_list.append(line) |
| 303 | |
| 304 | |
| 305 | def _process_line(self, line): |
| 306 | """Write out a line of data to the appropriate stream. Status |
| 307 | lines sent by autotest will be prepended with |
| 308 | "AUTOTEST_STATUS", and all other lines are ssh error |
| 309 | messages.""" |
| 310 | match = self.parser.search(line) |
| 311 | if match: |
| 312 | tag, line = match.groups() |
| 313 | self._process_quoted_line(tag, line) |
| 314 | else: |
| 315 | print >> sys.stderr, line |
| 316 | |
| 317 | |
| 318 | def write(self, data): |
| 319 | data = self.leftover + data |
| 320 | lines = data.split("\n") |
| 321 | # process every line but the last one |
| 322 | for line in lines[:-1]: |
| 323 | self._process_line(line) |
| 324 | # save the last line for later processing |
| 325 | # since we may not have the whole line yet |
| 326 | self.leftover = lines[-1] |
| 327 | |
| 328 | |
| 329 | def flush(self): |
| 330 | sys.stdout.flush() |
| 331 | sys.stderr.flush() |
| 332 | self.status_log.flush() |
| 333 | |
| 334 | |
| 335 | def close(self): |
| 336 | if self.leftover: |
| 337 | self._process_line(self.leftover) |
| 338 | self._process_logs() |
| 339 | self.flush() |
| 340 | |
| 341 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 342 | class _Run(object): |
| 343 | """ |
| 344 | Represents a run of autotest control file. This class maintains |
| 345 | all the state necessary as an autotest control file is executed. |
| 346 | |
| 347 | It is not intended to be used directly, rather control files |
| 348 | should be run using the run method in Autotest. |
| 349 | """ |
| 350 | def __init__(self, host, results_dir): |
| 351 | self.host = host |
| 352 | self.results_dir = results_dir |
mbligh | cc53b35 | 2007-10-24 21:15:30 +0000 | [diff] [blame] | 353 | self.env = host.env |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 354 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 355 | self.autodir = _get_autodir(self.host) |
mbligh | a4bece1 | 2007-11-24 19:37:14 +0000 | [diff] [blame] | 356 | self.manual_control_file = os.path.join(self.autodir, 'control') |
| 357 | self.remote_control_file = os.path.join(self.autodir, |
| 358 | 'control.autoserv') |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 359 | |
| 360 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 361 | def verify_machine(self): |
| 362 | binary = os.path.join(self.autodir, 'bin/autotest') |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 363 | try: |
mbligh | 09f4569 | 2008-01-26 22:53:56 +0000 | [diff] [blame^] | 364 | self.host.run('ls %s > /dev/null 2>&1' % binary) |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 365 | except: |
mbligh | 8d7e347 | 2007-08-10 01:35:18 +0000 | [diff] [blame] | 366 | raise "Autotest does not appear to be installed" |
| 367 | tmpdir = os.path.join(self.autodir, 'tmp') |
| 368 | self.host.run('umount %s' % tmpdir, ignore_status=True) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 369 | |
| 370 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 371 | def __execute_section(self, section, timeout): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 372 | print "Executing %s/bin/autotest %s/control phase %d" % \ |
| 373 | (self.autodir, self.autodir, |
| 374 | section) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 375 | |
mbligh | adf2aab | 2007-11-29 18:16:43 +0000 | [diff] [blame] | 376 | # build up the full command we want to run over the host |
| 377 | cmd = [os.path.join(self.autodir, 'bin/autotest_client')] |
| 378 | if section > 0: |
| 379 | cmd.append('-c') |
| 380 | cmd.append(self.remote_control_file) |
| 381 | full_cmd = ' '.join(cmd) |
| 382 | |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 383 | # open up the files we need for our logging |
| 384 | client_log_file = os.path.join(self.results_dir, 'debug', |
| 385 | 'client.log.%d' % section) |
| 386 | client_log = open(client_log_file, 'w', 0) |
| 387 | status_log_file = os.path.join(self.results_dir, 'status.log') |
| 388 | status_log = open(status_log_file, 'a', 0) |
| 389 | |
| 390 | try: |
| 391 | redirector = StdErrRedirector(status_log) |
| 392 | result = self.host.run(full_cmd, ignore_status=True, |
| 393 | timeout=timeout, |
| 394 | stdout_tee=client_log, |
| 395 | stderr_tee=redirector) |
| 396 | finally: |
| 397 | redirector.close() |
mbligh | 2bf2db6 | 2007-11-27 00:53:18 +0000 | [diff] [blame] | 398 | |
mbligh | cf732d1 | 2007-11-21 18:15:03 +0000 | [diff] [blame] | 399 | if result.exit_status == 1: |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 400 | self.host.job.aborted = True |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 401 | if not result.stderr: |
| 402 | raise AutotestRunError( |
| 403 | "execute_section: %s failed to return anything\n" |
| 404 | "stdout:%s\n" % (full_cmd, result.stdout)) |
| 405 | |
mbligh | 2bf2db6 | 2007-11-27 00:53:18 +0000 | [diff] [blame] | 406 | return redirector.last_line |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 407 | |
| 408 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 409 | def execute_control(self, timeout=None): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 410 | section = 0 |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 411 | time_left = None |
| 412 | if timeout: |
| 413 | end_time = time.time() + timeout |
| 414 | time_left = end_time - time.time() |
| 415 | while not timeout or time_left > 0: |
| 416 | last = self.__execute_section(section, time_left) |
| 417 | if timeout: |
| 418 | time_left = end_time - time.time() |
| 419 | if time_left <= 0: |
| 420 | break |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 421 | section += 1 |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 422 | if re.match(r'^END .*\t----\t----\t.*$', last): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 423 | print "Client complete" |
| 424 | return |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 425 | elif re.match('^\t*GOOD\t----\treboot\.start.*$', last): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 426 | print "Client is rebooting" |
| 427 | print "Waiting for client to halt" |
| 428 | if not self.host.wait_down(HALT_TIME): |
| 429 | raise AutotestRunError("%s \ |
| 430 | failed to shutdown after %ds" % |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 431 | (self.host.hostname, |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 432 | HALT_TIME)) |
| 433 | print "Client down, waiting for restart" |
| 434 | if not self.host.wait_up(BOOT_TIME): |
| 435 | # since reboot failed |
| 436 | # hardreset the machine once if possible |
| 437 | # before failing this control file |
mbligh | ba81c68 | 2007-10-25 15:35:59 +0000 | [diff] [blame] | 438 | print "Hardresetting %s" % ( |
| 439 | self.host.hostname,) |
| 440 | try: |
| 441 | self.host.hardreset(wait=False) |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 442 | except AutoservUnsupportedError: |
mbligh | ba81c68 | 2007-10-25 15:35:59 +0000 | [diff] [blame] | 443 | print "Hardreset unsupported on %s" % ( |
| 444 | self.host.hostname,) |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 445 | raise AutotestRunError("%s failed to " |
| 446 | "boot after %ds" % ( |
| 447 | self.host.hostname, |
| 448 | BOOT_TIME,)) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 449 | continue |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 450 | raise AutotestRunError("Aborting - unknown " |
| 451 | "return code: %s\n" % last) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 452 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 453 | # should only get here if we timed out |
| 454 | assert timeout |
| 455 | raise AutotestTimeoutError() |
| 456 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 457 | |
| 458 | def _get_autodir(host): |
mbligh | da13d54 | 2008-01-03 16:28:34 +0000 | [diff] [blame] | 459 | dir = host.get_autodir() |
| 460 | if dir: |
| 461 | return dir |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 462 | try: |
mbligh | e988aa5 | 2007-11-24 19:40:41 +0000 | [diff] [blame] | 463 | # There's no clean way to do this. readlink may not exist |
mbligh | 09f4569 | 2008-01-26 22:53:56 +0000 | [diff] [blame^] | 464 | cmd = "python -c 'import os,sys; print os.readlink(sys.argv[1])' /etc/autotest.conf 2> /dev/null" |
mbligh | e988aa5 | 2007-11-24 19:40:41 +0000 | [diff] [blame] | 465 | dir = os.path.dirname(host.run(cmd).stdout) |
| 466 | if dir: |
| 467 | return dir |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 468 | except AutoservRunError: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 469 | pass |
| 470 | for path in ['/usr/local/autotest', '/home/autotest']: |
| 471 | try: |
mbligh | 09f4569 | 2008-01-26 22:53:56 +0000 | [diff] [blame^] | 472 | host.run('ls %s > /dev/null 2>&1' % \ |
mbligh | 548197f | 2007-12-12 15:36:22 +0000 | [diff] [blame] | 473 | os.path.join(path, 'bin/autotest')) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 474 | return path |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 475 | except AutoservRunError: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 476 | pass |
| 477 | raise AutotestRunError("Cannot figure out autotest directory") |