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 | dab3966 | 2008-02-27 16:47:55 +0000 | [diff] [blame] | 28 | import server_job |
mbligh | 119c12a | 2007-11-12 22:13:44 +0000 | [diff] [blame] | 29 | from common import logging |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 30 | from common.error import * |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 31 | |
| 32 | |
| 33 | AUTOTEST_SVN = 'svn://test.kernel.org/autotest/trunk/client' |
| 34 | AUTOTEST_HTTP = 'http://test.kernel.org/svn/autotest/trunk/client' |
| 35 | |
| 36 | # Timeouts for powering down and up respectively |
| 37 | HALT_TIME = 300 |
mbligh | 07c1eac | 2007-11-05 18:39:29 +0000 | [diff] [blame] | 38 | BOOT_TIME = 1800 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 39 | |
| 40 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 41 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 42 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 43 | |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 44 | class BaseAutotest(installable_object.InstallableObject): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 45 | """ |
| 46 | This class represents the Autotest program. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 47 | |
| 48 | Autotest is used to run tests automatically and collect the results. |
| 49 | It also supports profilers. |
| 50 | |
| 51 | Implementation details: |
| 52 | This is a leaf class in an abstract class hierarchy, it must |
| 53 | implement the unimplemented methods in parent classes. |
| 54 | """ |
mbligh | 119c12a | 2007-11-12 22:13:44 +0000 | [diff] [blame] | 55 | job = None |
| 56 | |
| 57 | |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 58 | def __init__(self, host = None): |
| 59 | self.host = host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 60 | self.got = False |
| 61 | self.installed = False |
mbligh | 9708f73 | 2007-10-18 03:18:54 +0000 | [diff] [blame] | 62 | self.serverdir = utils.get_server_dir() |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 63 | super(BaseAutotest, self).__init__() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 64 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 65 | |
mbligh | 119c12a | 2007-11-12 22:13:44 +0000 | [diff] [blame] | 66 | @logging.record |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 67 | def install(self, host = None): |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 68 | """ |
| 69 | Install autotest. If get() was not called previously, an |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 70 | attempt will be made to install from the autotest svn |
| 71 | repository. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 72 | |
| 73 | Args: |
| 74 | host: a Host instance on which autotest will be |
| 75 | installed |
| 76 | |
| 77 | Raises: |
| 78 | AutoservError: if a tarball was not specified and |
| 79 | the target host does not have svn installed in its path |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 80 | |
| 81 | TODO(poirier): check dependencies |
| 82 | autotest needs: |
| 83 | bzcat |
| 84 | liboptdev (oprofile) |
| 85 | binutils-dev (oprofile) |
| 86 | make |
mbligh | 629e39e | 2007-08-10 19:32:00 +0000 | [diff] [blame] | 87 | psutils (netperf) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 88 | """ |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 89 | if not host: |
| 90 | host = self.host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 91 | if not self.got: |
| 92 | self.get() |
mbligh | 9e787d2 | 2008-02-11 17:56:37 +0000 | [diff] [blame] | 93 | host.wait_up(timeout=30) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 94 | host.setup() |
mbligh | b84a1cf | 2007-08-09 23:08:13 +0000 | [diff] [blame] | 95 | print "Installing autotest on %s" % host.hostname |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 96 | |
| 97 | autodir = _get_autodir(host) |
| 98 | host.run('mkdir -p "%s"' % utils.sh_escape(autodir)) |
| 99 | |
| 100 | if getattr(host, 'site_install_autotest', None): |
| 101 | if host.site_install_autotest(): |
| 102 | self.installed = True |
| 103 | return |
| 104 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 105 | # try to install from file or directory |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 106 | if self.source_material: |
| 107 | if os.path.isdir(self.source_material): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 108 | # Copy autotest recursively |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 109 | host.send_file(self.source_material, autodir) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 110 | else: |
| 111 | # Copy autotest via tarball |
mbligh | 4d6feff | 2008-01-14 16:48:56 +0000 | [diff] [blame] | 112 | e_msg = 'Installation method not yet implemented!' |
| 113 | raise NotImplementedError(e_msg) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 114 | print "Installation of autotest completed" |
| 115 | self.installed = True |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 116 | return |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 117 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 118 | # if that fails try to install using svn |
| 119 | if utils.run('which svn').exit_status: |
| 120 | raise AutoservError('svn not found in path on \ |
| 121 | target machine: %s' % host.name) |
| 122 | try: |
| 123 | host.run('svn checkout %s %s' % |
mbligh | 3850f89 | 2007-11-05 22:49:24 +0000 | [diff] [blame] | 124 | (AUTOTEST_SVN, autodir)) |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 125 | except AutoservRunError, e: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 126 | host.run('svn checkout %s %s' % |
mbligh | 3850f89 | 2007-11-05 22:49:24 +0000 | [diff] [blame] | 127 | (AUTOTEST_HTTP, autodir)) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 128 | print "Installation of autotest completed" |
| 129 | self.installed = True |
| 130 | |
| 131 | |
| 132 | def get(self, location = None): |
| 133 | if not location: |
| 134 | location = os.path.join(self.serverdir, '../client') |
| 135 | location = os.path.abspath(location) |
mbligh | 8fc0e5a | 2007-10-11 18:39:03 +0000 | [diff] [blame] | 136 | # If there's stuff run on our client directory already, it |
| 137 | # can cause problems. Try giving it a quick clean first. |
| 138 | cwd = os.getcwd() |
| 139 | os.chdir(location) |
| 140 | os.system('tools/make_clean') |
| 141 | os.chdir(cwd) |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 142 | super(BaseAutotest, self).get(location) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 143 | self.got = True |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 144 | |
| 145 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 146 | def run(self, control_file, results_dir = '.', host = None, |
| 147 | timeout=None): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 148 | """ |
| 149 | Run an autotest job on the remote machine. |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 150 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 151 | Args: |
| 152 | control_file: an open file-like-obj of the control file |
| 153 | results_dir: a str path where the results should be stored |
| 154 | on the local filesystem |
| 155 | host: a Host instance on which the control file should |
| 156 | be run |
| 157 | |
| 158 | Raises: |
| 159 | AutotestRunError: if there is a problem executing |
| 160 | the control file |
| 161 | """ |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 162 | host = self._get_host_and_setup(host) |
mbligh | 55a2a3b | 2007-09-30 01:27:55 +0000 | [diff] [blame] | 163 | results_dir = os.path.abspath(results_dir) |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 164 | atrun = _Run(host, results_dir) |
| 165 | self._do_run(control_file, results_dir, host, atrun, timeout) |
| 166 | |
| 167 | |
| 168 | def _get_host_and_setup(self, host): |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 169 | if not host: |
| 170 | host = self.host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 171 | if not self.installed: |
mbligh | 7fdd169 | 2007-10-02 19:16:53 +0000 | [diff] [blame] | 172 | self.install(host) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 173 | |
mbligh | 9e787d2 | 2008-02-11 17:56:37 +0000 | [diff] [blame] | 174 | host.wait_up(timeout=30) |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 175 | return host |
| 176 | |
| 177 | |
| 178 | def _do_run(self, control_file, results_dir, host, atrun, timeout): |
mbligh | e4f0151 | 2007-08-10 01:42:50 +0000 | [diff] [blame] | 179 | try: |
| 180 | atrun.verify_machine() |
| 181 | except: |
| 182 | print "Verify machine failed on %s. Reinstalling" % \ |
| 183 | host.hostname |
| 184 | self.install(host) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 185 | atrun.verify_machine() |
| 186 | debug = os.path.join(results_dir, 'debug') |
mbligh | 55a2a3b | 2007-09-30 01:27:55 +0000 | [diff] [blame] | 187 | try: |
| 188 | os.makedirs(debug) |
| 189 | except: |
| 190 | pass |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 191 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 192 | # Ready .... Aim .... |
mbligh | a4bece1 | 2007-11-24 19:37:14 +0000 | [diff] [blame] | 193 | for control in [atrun.remote_control_file, |
| 194 | atrun.remote_control_file + '.state', |
| 195 | atrun.manual_control_file, |
| 196 | atrun.manual_control_file + '.state']: |
| 197 | host.run('rm -f ' + control) |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 198 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 199 | # Copy control_file to remote_control_file on the host |
| 200 | tmppath = utils.get(control_file) |
| 201 | host.send_file(tmppath, atrun.remote_control_file) |
mbligh | 4918bdb | 2007-12-13 16:06:09 +0000 | [diff] [blame] | 202 | if os.path.abspath(tmppath) != os.path.abspath(control_file): |
| 203 | os.remove(tmppath) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 204 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 205 | try: |
| 206 | atrun.execute_control(timeout=timeout) |
mbligh | 35b225c | 2007-12-10 17:17:27 +0000 | [diff] [blame] | 207 | finally: |
mbligh | 1f19490 | 2007-12-20 01:31:43 +0000 | [diff] [blame] | 208 | # make an effort to wait for the machine to come up |
| 209 | try: |
mbligh | 9e787d2 | 2008-02-11 17:56:37 +0000 | [diff] [blame] | 210 | host.wait_up(timeout=30) |
mbligh | 1f19490 | 2007-12-20 01:31:43 +0000 | [diff] [blame] | 211 | except AutoservError: |
| 212 | # don't worry about any errors, we'll try and |
| 213 | # get the results anyway |
| 214 | pass |
| 215 | |
mbligh | 35b225c | 2007-12-10 17:17:27 +0000 | [diff] [blame] | 216 | # get the results |
| 217 | results = os.path.join(atrun.autodir, 'results', |
| 218 | 'default') |
| 219 | # Copy all dirs in default to results_dir |
| 220 | host.get_file(results + '/', results_dir) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 221 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 222 | |
| 223 | def run_timed_test(self, test_name, results_dir = '.', host = None, |
| 224 | timeout=None, *args, **dargs): |
mbligh | d54832b | 2007-07-25 16:46:56 +0000 | [diff] [blame] | 225 | """ |
| 226 | Assemble a tiny little control file to just run one test, |
| 227 | and run it as an autotest client-side test |
| 228 | """ |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 229 | if not host: |
| 230 | host = self.host |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 231 | if not self.installed: |
mbligh | 7fdd169 | 2007-10-02 19:16:53 +0000 | [diff] [blame] | 232 | self.install(host) |
mbligh | 271d5af | 2007-08-10 19:54:01 +0000 | [diff] [blame] | 233 | opts = ["%s=%s" % (o[0], repr(o[1])) for o in dargs.items()] |
| 234 | cmd = ", ".join([repr(test_name)] + map(repr, args) + opts) |
mbligh | f1c5284 | 2007-10-16 15:21:38 +0000 | [diff] [blame] | 235 | control = "job.run_test(%s)\n" % cmd |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 236 | self.run(control, results_dir, host, timeout=timeout) |
| 237 | |
| 238 | |
| 239 | def run_test(self, test_name, results_dir = '.', host = None, |
| 240 | *args, **dargs): |
| 241 | self.run_timed_test(test_name, results_dir, host, None, |
| 242 | *args, **dargs) |
mbligh | d54832b | 2007-07-25 16:46:56 +0000 | [diff] [blame] | 243 | |
| 244 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 245 | class _Run(object): |
| 246 | """ |
| 247 | Represents a run of autotest control file. This class maintains |
| 248 | all the state necessary as an autotest control file is executed. |
| 249 | |
| 250 | It is not intended to be used directly, rather control files |
| 251 | should be run using the run method in Autotest. |
| 252 | """ |
| 253 | def __init__(self, host, results_dir): |
| 254 | self.host = host |
| 255 | self.results_dir = results_dir |
mbligh | cc53b35 | 2007-10-24 21:15:30 +0000 | [diff] [blame] | 256 | self.env = host.env |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 257 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 258 | self.autodir = _get_autodir(self.host) |
mbligh | a4bece1 | 2007-11-24 19:37:14 +0000 | [diff] [blame] | 259 | self.manual_control_file = os.path.join(self.autodir, 'control') |
| 260 | self.remote_control_file = os.path.join(self.autodir, |
| 261 | 'control.autoserv') |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 262 | |
| 263 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 264 | def verify_machine(self): |
| 265 | binary = os.path.join(self.autodir, 'bin/autotest') |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 266 | try: |
mbligh | 09f4569 | 2008-01-26 22:53:56 +0000 | [diff] [blame] | 267 | self.host.run('ls %s > /dev/null 2>&1' % binary) |
mbligh | dd81ef0 | 2007-08-10 01:18:40 +0000 | [diff] [blame] | 268 | except: |
mbligh | 8d7e347 | 2007-08-10 01:35:18 +0000 | [diff] [blame] | 269 | raise "Autotest does not appear to be installed" |
| 270 | tmpdir = os.path.join(self.autodir, 'tmp') |
| 271 | self.host.run('umount %s' % tmpdir, ignore_status=True) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 272 | |
| 273 | |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 274 | def get_full_cmd(self, section): |
mbligh | adf2aab | 2007-11-29 18:16:43 +0000 | [diff] [blame] | 275 | # build up the full command we want to run over the host |
| 276 | cmd = [os.path.join(self.autodir, 'bin/autotest_client')] |
| 277 | if section > 0: |
| 278 | cmd.append('-c') |
| 279 | cmd.append(self.remote_control_file) |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 280 | return ' '.join(cmd) |
mbligh | adf2aab | 2007-11-29 18:16:43 +0000 | [diff] [blame] | 281 | |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 282 | |
| 283 | def get_client_log(self, section): |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 284 | # open up the files we need for our logging |
| 285 | client_log_file = os.path.join(self.results_dir, 'debug', |
| 286 | 'client.log.%d' % section) |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 287 | return open(client_log_file, 'w', 0) |
| 288 | |
| 289 | |
| 290 | def execute_section(self, section, timeout): |
| 291 | print "Executing %s/bin/autotest %s/control phase %d" % \ |
| 292 | (self.autodir, self.autodir, |
| 293 | section) |
| 294 | |
| 295 | full_cmd = self.get_full_cmd(section) |
| 296 | client_log = self.get_client_log(section) |
| 297 | redirector = server_job.client_logger(self.host.job) |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 298 | |
| 299 | try: |
mbligh | d528d30 | 2007-12-19 16:19:05 +0000 | [diff] [blame] | 300 | result = self.host.run(full_cmd, ignore_status=True, |
| 301 | timeout=timeout, |
| 302 | stdout_tee=client_log, |
| 303 | stderr_tee=redirector) |
| 304 | finally: |
| 305 | redirector.close() |
mbligh | 2bf2db6 | 2007-11-27 00:53:18 +0000 | [diff] [blame] | 306 | |
mbligh | cf732d1 | 2007-11-21 18:15:03 +0000 | [diff] [blame] | 307 | if result.exit_status == 1: |
mbligh | faf0cd4 | 2007-11-19 16:00:24 +0000 | [diff] [blame] | 308 | self.host.job.aborted = True |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 309 | if not result.stderr: |
| 310 | raise AutotestRunError( |
| 311 | "execute_section: %s failed to return anything\n" |
| 312 | "stdout:%s\n" % (full_cmd, result.stdout)) |
| 313 | |
mbligh | 2bf2db6 | 2007-11-27 00:53:18 +0000 | [diff] [blame] | 314 | return redirector.last_line |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 315 | |
| 316 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 317 | def execute_control(self, timeout=None): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 318 | section = 0 |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 319 | time_left = None |
| 320 | if timeout: |
| 321 | end_time = time.time() + timeout |
| 322 | time_left = end_time - time.time() |
| 323 | while not timeout or time_left > 0: |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 324 | last = self.execute_section(section, time_left) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 325 | if timeout: |
| 326 | time_left = end_time - time.time() |
| 327 | if time_left <= 0: |
| 328 | break |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 329 | section += 1 |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 330 | if re.match(r'^END .*\t----\t----\t.*$', last): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 331 | print "Client complete" |
| 332 | return |
mbligh | c343016 | 2007-11-14 23:57:19 +0000 | [diff] [blame] | 333 | elif re.match('^\t*GOOD\t----\treboot\.start.*$', last): |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 334 | print "Client is rebooting" |
| 335 | print "Waiting for client to halt" |
| 336 | if not self.host.wait_down(HALT_TIME): |
| 337 | raise AutotestRunError("%s \ |
| 338 | failed to shutdown after %ds" % |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 339 | (self.host.hostname, |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 340 | HALT_TIME)) |
| 341 | print "Client down, waiting for restart" |
| 342 | if not self.host.wait_up(BOOT_TIME): |
| 343 | # since reboot failed |
| 344 | # hardreset the machine once if possible |
| 345 | # before failing this control file |
mbligh | ba81c68 | 2007-10-25 15:35:59 +0000 | [diff] [blame] | 346 | print "Hardresetting %s" % ( |
| 347 | self.host.hostname,) |
| 348 | try: |
| 349 | self.host.hardreset(wait=False) |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 350 | except AutoservUnsupportedError: |
mbligh | ba81c68 | 2007-10-25 15:35:59 +0000 | [diff] [blame] | 351 | print "Hardreset unsupported on %s" % ( |
| 352 | self.host.hostname,) |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 353 | raise AutotestRunError("%s failed to " |
| 354 | "boot after %ds" % ( |
| 355 | self.host.hostname, |
| 356 | BOOT_TIME,)) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 357 | continue |
mbligh | 215c55a | 2008-03-25 14:56:13 +0000 | [diff] [blame] | 358 | self.host.job.record("ABORT", None, None, |
| 359 | "Autotest client terminated " + |
| 360 | "unexpectedly") |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 361 | raise AutotestRunError("Aborting - unknown " |
| 362 | "return code: %s\n" % last) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 363 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 364 | # should only get here if we timed out |
| 365 | assert timeout |
| 366 | raise AutotestTimeoutError() |
| 367 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 368 | |
| 369 | def _get_autodir(host): |
mbligh | da13d54 | 2008-01-03 16:28:34 +0000 | [diff] [blame] | 370 | dir = host.get_autodir() |
| 371 | if dir: |
| 372 | return dir |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 373 | try: |
mbligh | e988aa5 | 2007-11-24 19:40:41 +0000 | [diff] [blame] | 374 | # There's no clean way to do this. readlink may not exist |
mbligh | 09f4569 | 2008-01-26 22:53:56 +0000 | [diff] [blame] | 375 | 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] | 376 | dir = os.path.dirname(host.run(cmd).stdout) |
| 377 | if dir: |
| 378 | return dir |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 379 | except AutoservRunError: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 380 | pass |
| 381 | for path in ['/usr/local/autotest', '/home/autotest']: |
| 382 | try: |
mbligh | 09f4569 | 2008-01-26 22:53:56 +0000 | [diff] [blame] | 383 | host.run('ls %s > /dev/null 2>&1' % \ |
mbligh | 548197f | 2007-12-12 15:36:22 +0000 | [diff] [blame] | 384 | os.path.join(path, 'bin/autotest')) |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 385 | return path |
mbligh | 03f4fc7 | 2007-11-29 20:56:14 +0000 | [diff] [blame] | 386 | except AutoservRunError: |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 387 | pass |
| 388 | raise AutotestRunError("Cannot figure out autotest directory") |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 389 | |
| 390 | |
| 391 | # site_autotest.py may be non-existant or empty, make sure that an appropriate |
| 392 | # SiteAutotest class is created nevertheless |
| 393 | try: |
| 394 | from site_autotest import SiteAutotest |
| 395 | except ImportError: |
| 396 | class SiteAutotest(BaseAutotest): |
| 397 | pass |
| 398 | |
| 399 | |
| 400 | class Autotest(SiteAutotest): |
| 401 | pass |