mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 1 | # Copyright 2007 Google Inc. Released under the GPL v2 |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 2 | #pylint: disable-msg=C0111 |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 3 | |
beeps | 07f53b9 | 2013-01-08 12:55:10 -0800 | [diff] [blame] | 4 | import re, os, sys, traceback, time, glob, tempfile |
| 5 | import logging |
mbligh | 1f572e5 | 2010-04-01 17:15:53 +0000 | [diff] [blame] | 6 | from autotest_lib.server import installable_object, prebuild, utils |
beeps | 07f53b9 | 2013-01-08 12:55:10 -0800 | [diff] [blame] | 7 | from autotest_lib.client.common_lib import base_job, error, autotemp |
Dan Shi | 1889ca1 | 2015-04-15 09:36:06 -0700 | [diff] [blame] | 8 | from autotest_lib.client.common_lib import base_packages, packages |
| 9 | from autotest_lib.client.common_lib import global_config |
mbligh | a700772 | 2009-01-13 00:37:11 +0000 | [diff] [blame] | 10 | from autotest_lib.client.common_lib import utils as client_utils |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 11 | from autotest_lib.client.common_lib.cros.graphite import autotest_stats |
mbligh | 3c7a150 | 2008-07-24 18:08:47 +0000 | [diff] [blame] | 12 | |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 13 | AUTOTEST_SVN = 'svn://test.kernel.org/autotest/trunk/client' |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 14 | AUTOTEST_HTTP = 'http://test.kernel.org/svn/autotest/trunk/client' |
| 15 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 16 | |
mbligh | 1f572e5 | 2010-04-01 17:15:53 +0000 | [diff] [blame] | 17 | get_value = global_config.global_config.get_config_value |
| 18 | autoserv_prebuild = get_value('AUTOSERV', 'enable_server_prebuild', |
| 19 | type=bool, default=False) |
| 20 | |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 21 | |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 22 | class AutodirNotFoundError(Exception): |
| 23 | """No Autotest installation could be found.""" |
| 24 | |
| 25 | |
beeps | 607428b | 2013-03-25 16:43:20 -0700 | [diff] [blame] | 26 | class AutotestFailure(Exception): |
| 27 | """Gereric exception class for failures during a test run.""" |
| 28 | |
| 29 | |
| 30 | class AutotestAbort(AutotestFailure): |
| 31 | """ |
| 32 | AutotestAborts are thrown when the DUT seems fine, |
| 33 | and the test doesn't give us an explicit reason for |
| 34 | failure; In this case we have no choice but to abort. |
| 35 | """ |
| 36 | |
| 37 | |
| 38 | class AutotestDeviceError(AutotestFailure): |
| 39 | """ |
| 40 | Exceptions that inherit from AutotestDeviceError |
| 41 | are thrown when we can determine the current |
| 42 | state of the DUT and conclude that it probably |
| 43 | lead to the test failing; these exceptions lead |
| 44 | to failures instead of aborts. |
| 45 | """ |
| 46 | |
| 47 | |
| 48 | class AutotestDeviceNotPingable(AutotestDeviceError): |
| 49 | """Error for when a DUT becomes unpingable.""" |
| 50 | |
| 51 | |
| 52 | class AutotestDeviceNotSSHable(AutotestDeviceError): |
| 53 | """Error for when a DUT is pingable but not SSHable.""" |
| 54 | |
| 55 | |
| 56 | class AutotestDeviceRebooted(AutotestDeviceError): |
| 57 | """Error for when a DUT rebooted unexpectedly.""" |
| 58 | |
| 59 | |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 60 | class BaseAutotest(installable_object.InstallableObject): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 61 | """ |
| 62 | This class represents the Autotest program. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 63 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 64 | Autotest is used to run tests automatically and collect the results. |
| 65 | It also supports profilers. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 66 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 67 | Implementation details: |
| 68 | This is a leaf class in an abstract class hierarchy, it must |
| 69 | implement the unimplemented methods in parent classes. |
| 70 | """ |
mbligh | 119c12a | 2007-11-12 22:13:44 +0000 | [diff] [blame] | 71 | |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 72 | def __init__(self, host=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 73 | self.host = host |
| 74 | self.got = False |
| 75 | self.installed = False |
| 76 | self.serverdir = utils.get_server_dir() |
| 77 | super(BaseAutotest, self).__init__() |
mbligh | c8949b8 | 2007-07-23 16:33:58 +0000 | [diff] [blame] | 78 | |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 79 | |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame] | 80 | install_in_tmpdir = False |
| 81 | @classmethod |
| 82 | def set_install_in_tmpdir(cls, flag): |
| 83 | """ Sets a flag that controls whether or not Autotest should by |
| 84 | default be installed in a "standard" directory (e.g. |
| 85 | /home/autotest, /usr/local/autotest) or a temporary directory. """ |
| 86 | cls.install_in_tmpdir = flag |
| 87 | |
| 88 | |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 89 | @classmethod |
| 90 | def get_client_autodir_paths(cls, host): |
| 91 | return global_config.global_config.get_config_value( |
| 92 | 'AUTOSERV', 'client_autodir_paths', type=list) |
| 93 | |
| 94 | |
| 95 | @classmethod |
| 96 | def get_installed_autodir(cls, host): |
| 97 | """ |
| 98 | Find where the Autotest client is installed on the host. |
| 99 | @returns an absolute path to an installed Autotest client root. |
| 100 | @raises AutodirNotFoundError if no Autotest installation can be found. |
| 101 | """ |
| 102 | autodir = host.get_autodir() |
| 103 | if autodir: |
| 104 | logging.debug('Using existing host autodir: %s', autodir) |
| 105 | return autodir |
| 106 | |
| 107 | for path in Autotest.get_client_autodir_paths(host): |
| 108 | try: |
| 109 | autotest_binary = os.path.join(path, 'bin', 'autotest') |
| 110 | host.run('test -x %s' % utils.sh_escape(autotest_binary)) |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 111 | host.run('test -w %s' % utils.sh_escape(path)) |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 112 | logging.debug('Found existing autodir at %s', path) |
| 113 | return path |
| 114 | except error.AutoservRunError: |
| 115 | logging.debug('%s does not exist on %s', autotest_binary, |
| 116 | host.hostname) |
| 117 | raise AutodirNotFoundError |
| 118 | |
| 119 | |
| 120 | @classmethod |
| 121 | def get_install_dir(cls, host): |
| 122 | """ |
| 123 | Determines the location where autotest should be installed on |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame] | 124 | host. If self.install_in_tmpdir is set, it will return a unique |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 125 | temporary directory that autotest can be installed in. Otherwise, looks |
| 126 | for an existing installation to use; if none is found, looks for a |
| 127 | usable directory in the global config client_autodir_paths. |
| 128 | """ |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame] | 129 | try: |
lmr | 9dcf083 | 2009-12-08 21:28:55 +0000 | [diff] [blame] | 130 | install_dir = cls.get_installed_autodir(host) |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 131 | except AutodirNotFoundError: |
lmr | 9dcf083 | 2009-12-08 21:28:55 +0000 | [diff] [blame] | 132 | install_dir = cls._find_installable_dir(host) |
| 133 | |
| 134 | if cls.install_in_tmpdir: |
| 135 | return host.get_tmp_dir(parent=install_dir) |
| 136 | return install_dir |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 137 | |
| 138 | |
| 139 | @classmethod |
| 140 | def _find_installable_dir(cls, host): |
| 141 | client_autodir_paths = cls.get_client_autodir_paths(host) |
| 142 | for path in client_autodir_paths: |
| 143 | try: |
J. Richard Barnette | f44ff96 | 2011-01-05 10:14:28 -0800 | [diff] [blame] | 144 | host.run('mkdir -p %s' % utils.sh_escape(path)) |
Eric Li | 861b2d5 | 2011-02-04 14:50:35 -0800 | [diff] [blame] | 145 | host.run('test -w %s' % utils.sh_escape(path)) |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 146 | return path |
| 147 | except error.AutoservRunError: |
| 148 | logging.debug('Failed to create %s', path) |
Dan Shi | a8ff78d | 2015-09-18 13:59:51 -0700 | [diff] [blame] | 149 | metadata = {'_type': 'AutoservInstallError', |
| 150 | 'hostname': host.hostname} |
| 151 | autotest_stats.Counter('AutoservInstallError', |
| 152 | metadata=metadata).increment() |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 153 | raise error.AutoservInstallError( |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 154 | 'Unable to find a place to install Autotest; tried %s' % |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 155 | ', '.join(client_autodir_paths)) |
jadmanski | f22fea8 | 2008-11-26 20:57:07 +0000 | [diff] [blame] | 156 | |
| 157 | |
Eric Li | d656d56 | 2011-04-20 11:48:29 -0700 | [diff] [blame] | 158 | def get_fetch_location(self): |
| 159 | c = global_config.global_config |
| 160 | repos = c.get_config_value("PACKAGES", 'fetch_location', type=list, |
| 161 | default=[]) |
| 162 | repos.reverse() |
| 163 | return repos |
| 164 | |
| 165 | |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 166 | def install(self, host=None, autodir=None, use_packaging=True): |
| 167 | self._install(host=host, autodir=autodir, use_packaging=use_packaging) |
jadmanski | 54f90af | 2008-10-10 16:20:55 +0000 | [diff] [blame] | 168 | |
| 169 | |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 170 | def install_full_client(self, host=None, autodir=None): |
| 171 | self._install(host=host, autodir=autodir, use_autoserv=False, |
| 172 | use_packaging=False) |
| 173 | |
| 174 | |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 175 | def install_no_autoserv(self, host=None, autodir=None): |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 176 | self._install(host=host, autodir=autodir, use_autoserv=False) |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 177 | |
| 178 | |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 179 | def _install_using_packaging(self, host, autodir): |
Eric Li | d656d56 | 2011-04-20 11:48:29 -0700 | [diff] [blame] | 180 | repos = self.get_fetch_location() |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 181 | if not repos: |
| 182 | raise error.PackageInstallError("No repos to install an " |
| 183 | "autotest client from") |
| 184 | pkgmgr = packages.PackageManager(autodir, hostname=host.hostname, |
| 185 | repo_urls=repos, |
| 186 | do_locking=False, |
| 187 | run_function=host.run, |
| 188 | run_function_dargs=dict(timeout=600)) |
| 189 | # The packages dir is used to store all the packages that |
| 190 | # are fetched on that client. (for the tests,deps etc. |
| 191 | # too apart from the client) |
| 192 | pkg_dir = os.path.join(autodir, 'packages') |
| 193 | # clean up the autodir except for the packages directory |
| 194 | host.run('cd %s && ls | grep -v "^packages$"' |
Laurence Goodby | 2de320d | 2015-07-08 14:45:18 -0700 | [diff] [blame] | 195 | ' | xargs rm -rf && rm -rf .[!.]*' % autodir) |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 196 | pkgmgr.install_pkg('autotest', 'client', pkg_dir, autodir, |
| 197 | preserve_install_dir=True) |
| 198 | self.installed = True |
| 199 | |
| 200 | |
| 201 | def _install_using_send_file(self, host, autodir): |
Chris Sosa | 3ee5d5c | 2012-02-23 11:18:41 -0800 | [diff] [blame] | 202 | dirs_to_exclude = set(["tests", "site_tests", "deps", "profilers", |
| 203 | "packages"]) |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 204 | light_files = [os.path.join(self.source_material, f) |
| 205 | for f in os.listdir(self.source_material) |
| 206 | if f not in dirs_to_exclude] |
| 207 | host.send_file(light_files, autodir, delete_dest=True) |
| 208 | |
| 209 | # create empty dirs for all the stuff we excluded |
| 210 | commands = [] |
| 211 | for path in dirs_to_exclude: |
| 212 | abs_path = os.path.join(autodir, path) |
| 213 | abs_path = utils.sh_escape(abs_path) |
| 214 | commands.append("mkdir -p '%s'" % abs_path) |
| 215 | commands.append("touch '%s'/__init__.py" % abs_path) |
| 216 | host.run(';'.join(commands)) |
| 217 | |
| 218 | |
| 219 | def _install(self, host=None, autodir=None, use_autoserv=True, |
| 220 | use_packaging=True): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 221 | """ |
| 222 | Install autotest. If get() was not called previously, an |
| 223 | attempt will be made to install from the autotest svn |
| 224 | repository. |
mbligh | 9a3f5e5 | 2008-05-28 21:21:43 +0000 | [diff] [blame] | 225 | |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 226 | @param host A Host instance on which autotest will be installed |
| 227 | @param autodir Location on the remote host to install to |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 228 | @param use_autoserv Enable install modes that depend on the client |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 229 | running with the autoserv harness |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 230 | @param use_packaging Enable install modes that use the packaging system |
mbligh | 9a3f5e5 | 2008-05-28 21:21:43 +0000 | [diff] [blame] | 231 | |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 232 | @exception AutoservError if a tarball was not specified and |
| 233 | the target host does not have svn installed in its path |
| 234 | """ |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 235 | if not host: |
| 236 | host = self.host |
| 237 | if not self.got: |
| 238 | self.get() |
| 239 | host.wait_up(timeout=30) |
| 240 | host.setup() |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 241 | logging.info("Installing autotest on %s", host.hostname) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 242 | |
jadmanski | 54f90af | 2008-10-10 16:20:55 +0000 | [diff] [blame] | 243 | # set up the autotest directory on the remote machine |
| 244 | if not autodir: |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 245 | autodir = self.get_install_dir(host) |
| 246 | logging.info('Using installation dir %s', autodir) |
mbligh | 0562e65 | 2008-08-20 20:11:45 +0000 | [diff] [blame] | 247 | host.set_autodir(autodir) |
jadmanski | 3c23694 | 2009-03-04 17:51:26 +0000 | [diff] [blame] | 248 | host.run('mkdir -p %s' % utils.sh_escape(autodir)) |
mbligh | 40f122a | 2007-11-03 23:08:46 +0000 | [diff] [blame] | 249 | |
jadmanski | 1c3c07b | 2009-03-03 23:29:36 +0000 | [diff] [blame] | 250 | # make sure there are no files in $AUTODIR/results |
| 251 | results_path = os.path.join(autodir, 'results') |
jadmanski | 3c23694 | 2009-03-04 17:51:26 +0000 | [diff] [blame] | 252 | host.run('rm -rf %s/*' % utils.sh_escape(results_path), |
jadmanski | 1c3c07b | 2009-03-03 23:29:36 +0000 | [diff] [blame] | 253 | ignore_status=True) |
| 254 | |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 255 | # Fetch the autotest client from the nearest repository |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 256 | if use_packaging: |
| 257 | try: |
| 258 | self._install_using_packaging(host, autodir) |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 259 | logging.info("Installation of autotest completed using the " |
| 260 | "packaging system.") |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 261 | return |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 262 | except (error.PackageInstallError, error.AutoservRunError, |
| 263 | global_config.ConfigError), e: |
mbligh | b8aa75b | 2009-09-18 16:50:37 +0000 | [diff] [blame] | 264 | logging.info("Could not install autotest using the packaging " |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 265 | "system: %s. Trying other methods", e) |
Dan Shi | 1889ca1 | 2015-04-15 09:36:06 -0700 | [diff] [blame] | 266 | else: |
| 267 | # Delete the package checksum file to force dut updating local |
| 268 | # packages. |
| 269 | command = ('rm -f "%s"' % |
| 270 | (os.path.join(autodir, base_packages.CHECKSUM_FILE))) |
| 271 | host.run(command) |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 272 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 273 | # try to install from file or directory |
| 274 | if self.source_material: |
jadmanski | 69bdaac | 2010-07-28 16:27:20 +0000 | [diff] [blame] | 275 | c = global_config.global_config |
| 276 | supports_autoserv_packaging = c.get_config_value( |
| 277 | "PACKAGES", "serve_packages_from_autoserv", type=bool) |
| 278 | # Copy autotest recursively |
| 279 | if supports_autoserv_packaging and use_autoserv: |
| 280 | self._install_using_send_file(host, autodir) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 281 | else: |
jadmanski | 69bdaac | 2010-07-28 16:27:20 +0000 | [diff] [blame] | 282 | host.send_file(self.source_material, autodir, delete_dest=True) |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 283 | logging.info("Installation of autotest completed from %s", |
| 284 | self.source_material) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 285 | self.installed = True |
| 286 | return |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 287 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 288 | # if that fails try to install using svn |
| 289 | if utils.run('which svn').exit_status: |
Dale Curtis | 74a314b | 2011-06-23 14:55:46 -0700 | [diff] [blame] | 290 | raise error.AutoservError('svn not found on target machine: %s' % |
| 291 | host.name) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 292 | try: |
mbligh | 78bf535 | 2008-07-11 20:27:36 +0000 | [diff] [blame] | 293 | host.run('svn checkout %s %s' % (AUTOTEST_SVN, autodir)) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 294 | except error.AutoservRunError, e: |
mbligh | 78bf535 | 2008-07-11 20:27:36 +0000 | [diff] [blame] | 295 | host.run('svn checkout %s %s' % (AUTOTEST_HTTP, autodir)) |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 296 | logging.info("Installation of autotest completed using SVN.") |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 297 | self.installed = True |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 298 | |
| 299 | |
jadmanski | 7c7aff3 | 2009-03-25 22:43:07 +0000 | [diff] [blame] | 300 | def uninstall(self, host=None): |
| 301 | """ |
| 302 | Uninstall (i.e. delete) autotest. Removes the autotest client install |
| 303 | from the specified host. |
| 304 | |
| 305 | @params host a Host instance from which the client will be removed |
| 306 | """ |
| 307 | if not self.installed: |
| 308 | return |
| 309 | if not host: |
| 310 | host = self.host |
| 311 | autodir = host.get_autodir() |
| 312 | if not autodir: |
| 313 | return |
| 314 | |
| 315 | # perform the actual uninstall |
| 316 | host.run("rm -rf %s" % utils.sh_escape(autodir), ignore_status=True) |
| 317 | host.set_autodir(None) |
| 318 | self.installed = False |
| 319 | |
| 320 | |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 321 | def get(self, location=None): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 322 | if not location: |
| 323 | location = os.path.join(self.serverdir, '../client') |
| 324 | location = os.path.abspath(location) |
| 325 | # If there's stuff run on our client directory already, it |
| 326 | # can cause problems. Try giving it a quick clean first. |
| 327 | cwd = os.getcwd() |
| 328 | os.chdir(location) |
showard | 4b97607 | 2009-10-20 23:50:08 +0000 | [diff] [blame] | 329 | try: |
| 330 | utils.system('tools/make_clean', ignore_status=True) |
| 331 | finally: |
| 332 | os.chdir(cwd) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 333 | super(BaseAutotest, self).get(location) |
| 334 | self.got = True |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 335 | |
| 336 | |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 337 | def run(self, control_file, results_dir='.', host=None, timeout=None, |
| 338 | tag=None, parallel_flag=False, background=False, |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 339 | client_disconnect_timeout=None, use_packaging=True): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 340 | """ |
| 341 | Run an autotest job on the remote machine. |
mbligh | 9a3f5e5 | 2008-05-28 21:21:43 +0000 | [diff] [blame] | 342 | |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 343 | @param control_file: An open file-like-obj of the control file. |
| 344 | @param results_dir: A str path where the results should be stored |
| 345 | on the local filesystem. |
| 346 | @param host: A Host instance on which the control file should |
| 347 | be run. |
| 348 | @param timeout: Maximum number of seconds to wait for the run or None. |
| 349 | @param tag: Tag name for the client side instance of autotest. |
| 350 | @param parallel_flag: Flag set when multiple jobs are run at the |
| 351 | same time. |
| 352 | @param background: Indicates that the client should be launched as |
| 353 | a background job; the code calling run will be responsible |
| 354 | for monitoring the client and collecting the results. |
| 355 | @param client_disconnect_timeout: Seconds to wait for the remote host |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 356 | to come back after a reboot. Defaults to the host setting for |
| 357 | DEFAULT_REBOOT_TIMEOUT. |
mbligh | e7d9c60 | 2009-07-02 19:02:33 +0000 | [diff] [blame] | 358 | |
| 359 | @raises AutotestRunError: If there is a problem executing |
| 360 | the control file. |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 361 | """ |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 362 | host = self._get_host_and_setup(host, use_packaging=use_packaging) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 363 | results_dir = os.path.abspath(results_dir) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 364 | |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 365 | if client_disconnect_timeout is None: |
| 366 | client_disconnect_timeout = host.DEFAULT_REBOOT_TIMEOUT |
| 367 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 368 | if tag: |
| 369 | results_dir = os.path.join(results_dir, tag) |
mbligh | c1cbc99 | 2008-05-27 20:01:45 +0000 | [diff] [blame] | 370 | |
mbligh | b3c0c91 | 2008-11-27 00:32:45 +0000 | [diff] [blame] | 371 | atrun = _Run(host, results_dir, tag, parallel_flag, background) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 372 | self._do_run(control_file, results_dir, host, atrun, timeout, |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 373 | client_disconnect_timeout, use_packaging=use_packaging) |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 374 | |
| 375 | |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 376 | def _get_host_and_setup(self, host, use_packaging=True): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 377 | if not host: |
| 378 | host = self.host |
| 379 | if not self.installed: |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 380 | self.install(host, use_packaging=use_packaging) |
mbligh | 9133490 | 2007-09-28 01:47:59 +0000 | [diff] [blame] | 381 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 382 | host.wait_up(timeout=30) |
| 383 | return host |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 384 | |
| 385 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 386 | def _do_run(self, control_file, results_dir, host, atrun, timeout, |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 387 | client_disconnect_timeout, use_packaging=True): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 388 | try: |
| 389 | atrun.verify_machine() |
| 390 | except: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 391 | logging.error("Verify failed on %s. Reinstalling autotest", |
| 392 | host.hostname) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 393 | self.install(host) |
Fang Deng | b9cd83c | 2015-01-27 10:16:08 -0800 | [diff] [blame] | 394 | atrun.verify_machine() |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 395 | debug = os.path.join(results_dir, 'debug') |
| 396 | try: |
| 397 | os.makedirs(debug) |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 398 | except Exception: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 399 | pass |
mbligh | 9a3f5e5 | 2008-05-28 21:21:43 +0000 | [diff] [blame] | 400 | |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 401 | delete_file_list = [atrun.remote_control_file, |
| 402 | atrun.remote_control_file + '.state', |
| 403 | atrun.manual_control_file, |
| 404 | atrun.manual_control_file + '.state'] |
| 405 | cmd = ';'.join('rm -f ' + control for control in delete_file_list) |
| 406 | host.run(cmd, ignore_status=True) |
mbligh | 9a3f5e5 | 2008-05-28 21:21:43 +0000 | [diff] [blame] | 407 | |
Dale Curtis | 386eea7 | 2011-09-21 18:43:04 -0700 | [diff] [blame] | 408 | tmppath = utils.get(control_file, local_copy=True) |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 409 | |
jadmanski | cb0e161 | 2009-02-27 18:03:10 +0000 | [diff] [blame] | 410 | # build up the initialization prologue for the control file |
| 411 | prologue_lines = [] |
jadmanski | 23afbec | 2008-09-17 18:12:07 +0000 | [diff] [blame] | 412 | |
mbligh | 2f07683 | 2010-03-30 17:08:20 +0000 | [diff] [blame] | 413 | # Add the additional user arguments |
jadmanski | 808f4b1 | 2010-04-09 22:30:31 +0000 | [diff] [blame] | 414 | prologue_lines.append("args = %r\n" % self.job.args) |
mbligh | 2f07683 | 2010-03-30 17:08:20 +0000 | [diff] [blame] | 415 | |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 416 | # If the packaging system is being used, add the repository list. |
mbligh | ddc9a40 | 2010-01-15 20:33:34 +0000 | [diff] [blame] | 417 | repos = None |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 418 | try: |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 419 | if use_packaging: |
| 420 | repos = self.get_fetch_location() |
| 421 | prologue_lines.append('job.add_repository(%s)\n' % repos) |
| 422 | else: |
| 423 | logging.debug('use_packaging is set to False, do not add any ' |
| 424 | 'repository.') |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 425 | except global_config.ConfigError, e: |
mbligh | ddc9a40 | 2010-01-15 20:33:34 +0000 | [diff] [blame] | 426 | # If repos is defined packaging is enabled so log the error |
| 427 | if repos: |
| 428 | logging.error(e) |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 429 | |
jadmanski | e2eef7b | 2009-03-03 23:55:13 +0000 | [diff] [blame] | 430 | # on full-size installs, turn on any profilers the server is using |
jadmanski | 27b5291 | 2009-08-14 17:09:15 +0000 | [diff] [blame] | 431 | if not atrun.background: |
jadmanski | e2eef7b | 2009-03-03 23:55:13 +0000 | [diff] [blame] | 432 | running_profilers = host.job.profilers.add_log.iteritems() |
| 433 | for profiler, (args, dargs) in running_profilers: |
| 434 | call_args = [repr(profiler)] |
| 435 | call_args += [repr(arg) for arg in args] |
| 436 | call_args += ["%s=%r" % item for item in dargs.iteritems()] |
| 437 | prologue_lines.append("job.profilers.add(%s)\n" |
| 438 | % ", ".join(call_args)) |
| 439 | cfile = "".join(prologue_lines) |
| 440 | |
mbligh | 0910844 | 2008-10-15 16:27:38 +0000 | [diff] [blame] | 441 | cfile += open(tmppath).read() |
| 442 | open(tmppath, "w").write(cfile) |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 443 | |
jadmanski | c09fc15 | 2008-10-15 17:56:59 +0000 | [diff] [blame] | 444 | # Create and copy state file to remote_control_file + '.state' |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 445 | state_file = host.job.preprocess_client_state() |
mbligh | fbf73ae | 2009-12-19 05:22:42 +0000 | [diff] [blame] | 446 | host.send_file(state_file, atrun.remote_control_file + '.init.state') |
jadmanski | c09fc15 | 2008-10-15 17:56:59 +0000 | [diff] [blame] | 447 | os.remove(state_file) |
| 448 | |
mbligh | c5ddfd1 | 2008-08-04 17:15:00 +0000 | [diff] [blame] | 449 | # Copy control_file to remote_control_file on the host |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 450 | host.send_file(tmppath, atrun.remote_control_file) |
| 451 | if os.path.abspath(tmppath) != os.path.abspath(control_file): |
| 452 | os.remove(tmppath) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 453 | |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 454 | atrun.execute_control( |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 455 | timeout=timeout, |
Dale Curtis | 9285ddf | 2011-01-05 11:47:24 -0800 | [diff] [blame] | 456 | client_disconnect_timeout=client_disconnect_timeout) |
jadmanski | 23afbec | 2008-09-17 18:12:07 +0000 | [diff] [blame] | 457 | |
| 458 | |
harpreet | f531d07 | 2016-04-19 18:37:26 -0700 | [diff] [blame^] | 459 | @classmethod |
| 460 | def _check_client_test_result(cls, host, test_name): |
| 461 | """ |
| 462 | Check result of client test. |
| 463 | Autotest will store results in the file name status. |
| 464 | We check that second to last line in that file begins with 'END GOOD' |
| 465 | |
| 466 | @raises TestFail: If client test does not pass. |
| 467 | """ |
| 468 | client_result_dir = '%s/results/default' % host.autodir |
| 469 | command = 'tail -2 %s/status | head -1' % client_result_dir |
| 470 | status = host.run(command).stdout.strip() |
| 471 | logging.info(status) |
| 472 | if status[:8] != 'END GOOD': |
| 473 | raise error.TestFail('%s client test did not pass.' % test_name) |
| 474 | |
| 475 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 476 | def run_timed_test(self, test_name, results_dir='.', host=None, |
Dennis Jeffrey | b0be88b | 2013-04-18 11:18:38 -0700 | [diff] [blame] | 477 | timeout=None, parallel_flag=False, background=False, |
| 478 | client_disconnect_timeout=None, *args, **dargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 479 | """ |
| 480 | Assemble a tiny little control file to just run one test, |
| 481 | and run it as an autotest client-side test |
| 482 | """ |
| 483 | if not host: |
| 484 | host = self.host |
| 485 | if not self.installed: |
| 486 | self.install(host) |
| 487 | opts = ["%s=%s" % (o[0], repr(o[1])) for o in dargs.items()] |
| 488 | cmd = ", ".join([repr(test_name)] + map(repr, args) + opts) |
| 489 | control = "job.run_test(%s)\n" % cmd |
Dennis Jeffrey | b0be88b | 2013-04-18 11:18:38 -0700 | [diff] [blame] | 490 | self.run(control, results_dir, host, timeout=timeout, |
Andrew Bresticker | 2da1b76 | 2013-01-15 16:11:18 -0800 | [diff] [blame] | 491 | parallel_flag=parallel_flag, background=background, |
| 492 | client_disconnect_timeout=client_disconnect_timeout) |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 493 | |
harpreet | f531d07 | 2016-04-19 18:37:26 -0700 | [diff] [blame^] | 494 | dargs_dict = dict(dargs) |
| 495 | if 'check_client_result' in dargs_dict: |
| 496 | if dargs_dict['check_client_result']: |
| 497 | self._check_client_test_result(host, test_name) |
| 498 | |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 499 | |
Dennis Jeffrey | b0be88b | 2013-04-18 11:18:38 -0700 | [diff] [blame] | 500 | def run_test(self, test_name, results_dir='.', host=None, |
Andrew Bresticker | 2da1b76 | 2013-01-15 16:11:18 -0800 | [diff] [blame] | 501 | parallel_flag=False, background=False, |
| 502 | client_disconnect_timeout=None, *args, **dargs): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 503 | self.run_timed_test(test_name, results_dir, host, timeout=None, |
Dennis Jeffrey | b0be88b | 2013-04-18 11:18:38 -0700 | [diff] [blame] | 504 | parallel_flag=parallel_flag, background=background, |
Andrew Bresticker | 2da1b76 | 2013-01-15 16:11:18 -0800 | [diff] [blame] | 505 | client_disconnect_timeout=client_disconnect_timeout, |
jadmanski | c98c470 | 2009-01-05 15:50:06 +0000 | [diff] [blame] | 506 | *args, **dargs) |
mbligh | d54832b | 2007-07-25 16:46:56 +0000 | [diff] [blame] | 507 | |
| 508 | |
jadmanski | 69bdaac | 2010-07-28 16:27:20 +0000 | [diff] [blame] | 509 | class _BaseRun(object): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 510 | """ |
| 511 | Represents a run of autotest control file. This class maintains |
| 512 | all the state necessary as an autotest control file is executed. |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 513 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 514 | It is not intended to be used directly, rather control files |
| 515 | should be run using the run method in Autotest. |
| 516 | """ |
mbligh | b3c0c91 | 2008-11-27 00:32:45 +0000 | [diff] [blame] | 517 | def __init__(self, host, results_dir, tag, parallel_flag, background): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 518 | self.host = host |
| 519 | self.results_dir = results_dir |
| 520 | self.env = host.env |
| 521 | self.tag = tag |
| 522 | self.parallel_flag = parallel_flag |
mbligh | b3c0c91 | 2008-11-27 00:32:45 +0000 | [diff] [blame] | 523 | self.background = background |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 524 | self.autodir = Autotest.get_installed_autodir(self.host) |
mbligh | 78bf535 | 2008-07-11 20:27:36 +0000 | [diff] [blame] | 525 | control = os.path.join(self.autodir, 'control') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 526 | if tag: |
mbligh | 78bf535 | 2008-07-11 20:27:36 +0000 | [diff] [blame] | 527 | control += '.' + tag |
| 528 | self.manual_control_file = control |
| 529 | self.remote_control_file = control + '.autoserv' |
lmr | 6d08b3c | 2009-11-18 19:26:38 +0000 | [diff] [blame] | 530 | self.config_file = os.path.join(self.autodir, 'global_config.ini') |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 531 | |
| 532 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 533 | def verify_machine(self): |
| 534 | binary = os.path.join(self.autodir, 'bin/autotest') |
| 535 | try: |
| 536 | self.host.run('ls %s > /dev/null 2>&1' % binary) |
| 537 | except: |
lmr | d6d27ed | 2009-12-08 19:58:33 +0000 | [diff] [blame] | 538 | raise error.AutoservInstallError( |
| 539 | "Autotest does not appear to be installed") |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 540 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 541 | if not self.parallel_flag: |
| 542 | tmpdir = os.path.join(self.autodir, 'tmp') |
| 543 | download = os.path.join(self.autodir, 'tests/download') |
| 544 | self.host.run('umount %s' % tmpdir, ignore_status=True) |
| 545 | self.host.run('umount %s' % download, ignore_status=True) |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 546 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 547 | |
| 548 | def get_base_cmd_args(self, section): |
showard | 234b296 | 2009-07-28 20:02:30 +0000 | [diff] [blame] | 549 | args = ['--verbose'] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 550 | if section > 0: |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 551 | args.append('-c') |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 552 | if self.tag: |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 553 | args.append('-t %s' % self.tag) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 554 | if self.host.job.use_external_logging(): |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 555 | args.append('-l') |
mbligh | ce955fc | 2009-08-24 21:59:02 +0000 | [diff] [blame] | 556 | if self.host.hostname: |
| 557 | args.append('--hostname=%s' % self.host.hostname) |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 558 | args.append('--user=%s' % self.host.job.user) |
mbligh | ce955fc | 2009-08-24 21:59:02 +0000 | [diff] [blame] | 559 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 560 | args.append(self.remote_control_file) |
| 561 | return args |
| 562 | |
| 563 | |
| 564 | def get_background_cmd(self, section): |
| 565 | cmd = ['nohup', os.path.join(self.autodir, 'bin/autotest_client')] |
| 566 | cmd += self.get_base_cmd_args(section) |
jadmanski | 69bdaac | 2010-07-28 16:27:20 +0000 | [diff] [blame] | 567 | cmd += ['>/dev/null', '2>/dev/null', '&'] |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 568 | return ' '.join(cmd) |
| 569 | |
| 570 | |
| 571 | def get_daemon_cmd(self, section, monitor_dir): |
| 572 | cmd = ['nohup', os.path.join(self.autodir, 'bin/autotestd'), |
| 573 | monitor_dir, '-H autoserv'] |
| 574 | cmd += self.get_base_cmd_args(section) |
jadmanski | 69bdaac | 2010-07-28 16:27:20 +0000 | [diff] [blame] | 575 | cmd += ['>/dev/null', '2>/dev/null', '&'] |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 576 | return ' '.join(cmd) |
| 577 | |
| 578 | |
| 579 | def get_monitor_cmd(self, monitor_dir, stdout_read, stderr_read): |
| 580 | cmd = [os.path.join(self.autodir, 'bin', 'autotestd_monitor'), |
| 581 | monitor_dir, str(stdout_read), str(stderr_read)] |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 582 | return ' '.join(cmd) |
mbligh | adf2aab | 2007-11-29 18:16:43 +0000 | [diff] [blame] | 583 | |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 584 | |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 585 | def get_client_log(self): |
| 586 | """Find what the "next" client.* prefix should be |
| 587 | |
| 588 | @returns A string of the form client.INTEGER that should be prefixed |
| 589 | to all client debug log files. |
| 590 | """ |
| 591 | max_digit = -1 |
| 592 | debug_dir = os.path.join(self.results_dir, 'debug') |
| 593 | client_logs = glob.glob(os.path.join(debug_dir, 'client.*.*')) |
| 594 | for log in client_logs: |
| 595 | _, number, _ = log.split('.', 2) |
| 596 | if number.isdigit(): |
| 597 | max_digit = max(max_digit, int(number)) |
| 598 | return 'client.%d' % (max_digit + 1) |
| 599 | |
| 600 | |
| 601 | def copy_client_config_file(self, client_log_prefix=None): |
| 602 | """ |
| 603 | Create and copy the client config file based on the server config. |
| 604 | |
| 605 | @param client_log_prefix: Optional prefix to prepend to log files. |
| 606 | """ |
| 607 | client_config_file = self._create_client_config_file(client_log_prefix) |
| 608 | self.host.send_file(client_config_file, self.config_file) |
| 609 | os.remove(client_config_file) |
| 610 | |
| 611 | |
| 612 | def _create_client_config_file(self, client_log_prefix=None): |
| 613 | """ |
| 614 | Create a temporary file with the [CLIENT] section configuration values |
| 615 | taken from the server global_config.ini. |
| 616 | |
| 617 | @param client_log_prefix: Optional prefix to prepend to log files. |
| 618 | |
| 619 | @return: Path of the temporary file generated. |
| 620 | """ |
| 621 | config = global_config.global_config.get_section_values('CLIENT') |
| 622 | if client_log_prefix: |
| 623 | config.set('CLIENT', 'default_logging_name', client_log_prefix) |
| 624 | return self._create_aux_file(config.write) |
| 625 | |
| 626 | |
| 627 | def _create_aux_file(self, func, *args): |
| 628 | """ |
| 629 | Creates a temporary file and writes content to it according to a |
| 630 | content creation function. The file object is appended to *args, which |
| 631 | is then passed to the content creation function |
| 632 | |
| 633 | @param func: Function that will be used to write content to the |
| 634 | temporary file. |
| 635 | @param *args: List of parameters that func takes. |
| 636 | @return: Path to the temporary file that was created. |
| 637 | """ |
| 638 | fd, path = tempfile.mkstemp(dir=self.host.job.tmpdir) |
| 639 | aux_file = os.fdopen(fd, "w") |
| 640 | try: |
| 641 | list_args = list(args) |
| 642 | list_args.append(aux_file) |
| 643 | func(*list_args) |
| 644 | finally: |
| 645 | aux_file.close() |
| 646 | return path |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 647 | |
| 648 | |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 649 | @staticmethod |
| 650 | def is_client_job_finished(last_line): |
| 651 | return bool(re.match(r'^END .*\t----\t----\t.*$', last_line)) |
| 652 | |
| 653 | |
| 654 | @staticmethod |
| 655 | def is_client_job_rebooting(last_line): |
| 656 | return bool(re.match(r'^\t*GOOD\t----\treboot\.start.*$', last_line)) |
| 657 | |
| 658 | |
beeps | 607428b | 2013-03-25 16:43:20 -0700 | [diff] [blame] | 659 | def _diagnose_dut(self, old_boot_id=None): |
| 660 | """ |
| 661 | Run diagnostic checks on a DUT. |
| 662 | |
| 663 | 1. ping: A dead host will not respond to pings. |
| 664 | 2. ssh (happens with 3.): DUT hangs usually fail in authentication |
| 665 | but respond to pings. |
| 666 | 3. Check if a reboot occured: A healthy but unexpected reboot leaves the |
| 667 | host running with a new boot id. |
| 668 | |
| 669 | This method will always raise an exception from the AutotestFailure |
| 670 | family and should only get called when the reason for a test failing |
| 671 | is ambiguous. |
| 672 | |
| 673 | @raises AutotestDeviceNotPingable: If the DUT doesn't respond to ping. |
| 674 | @raises AutotestDeviceNotSSHable: If we cannot SSH into the DUT. |
| 675 | @raises AutotestDeviceRebooted: If the boot id changed. |
| 676 | @raises AutotestAbort: If none of the above exceptions were raised. |
| 677 | Since we have no recourse we must abort at this stage. |
| 678 | """ |
| 679 | msg = 'Autotest client terminated unexpectedly: ' |
| 680 | if utils.ping(self.host.hostname, tries=1, deadline=1) != 0: |
| 681 | msg += 'DUT is no longer pingable, it may have rebooted or hung.\n' |
| 682 | raise AutotestDeviceNotPingable(msg) |
| 683 | |
| 684 | if old_boot_id: |
| 685 | try: |
| 686 | new_boot_id = self.host.get_boot_id(timeout=60) |
| 687 | except Exception as e: |
| 688 | msg += ('DUT is pingable but not SSHable, it most likely' |
beeps | 1476881 | 2013-09-25 12:58:45 -0700 | [diff] [blame] | 689 | ' sporadically rebooted during testing. %s\n' % str(e)) |
beeps | 607428b | 2013-03-25 16:43:20 -0700 | [diff] [blame] | 690 | raise AutotestDeviceNotSSHable(msg) |
| 691 | else: |
| 692 | if new_boot_id != old_boot_id: |
| 693 | msg += 'DUT rebooted during the test run.\n' |
| 694 | raise AutotestDeviceRebooted(msg) |
| 695 | |
| 696 | msg += ('DUT is pingable, SSHable and did NOT restart ' |
| 697 | 'un-expectedly. We probably lost connectivity during the ' |
| 698 | 'test.') |
| 699 | else: |
| 700 | msg += ('DUT is pingable, could not determine if an un-expected ' |
| 701 | 'reboot occured during the test.') |
| 702 | |
| 703 | raise AutotestAbort(msg) |
| 704 | |
| 705 | |
beeps | 07f53b9 | 2013-01-08 12:55:10 -0800 | [diff] [blame] | 706 | def log_unexpected_abort(self, stderr_redirector, old_boot_id=None): |
| 707 | """ |
beeps | 607428b | 2013-03-25 16:43:20 -0700 | [diff] [blame] | 708 | Logs that something unexpected happened, then tries to diagnose the |
| 709 | failure. The purpose of this function is only to close out the status |
| 710 | log with the appropriate error message, not to critically terminate |
| 711 | the program. |
beeps | 07f53b9 | 2013-01-08 12:55:10 -0800 | [diff] [blame] | 712 | |
| 713 | @param stderr_redirector: log stream. |
| 714 | @param old_boot_id: boot id used to infer if a reboot occured. |
| 715 | """ |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 716 | stderr_redirector.flush_all_buffers() |
beeps | 607428b | 2013-03-25 16:43:20 -0700 | [diff] [blame] | 717 | try: |
| 718 | self._diagnose_dut(old_boot_id) |
| 719 | except AutotestFailure as e: |
| 720 | self.host.job.record('END ABORT', None, None, str(e)) |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 721 | |
| 722 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 723 | def _execute_in_background(self, section, timeout): |
| 724 | full_cmd = self.get_background_cmd(section) |
| 725 | devnull = open(os.devnull, "w") |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 726 | |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 727 | self.copy_client_config_file(self.get_client_log()) |
| 728 | |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 729 | self.host.job.push_execution_context(self.results_dir) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 730 | try: |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 731 | result = self.host.run(full_cmd, ignore_status=True, |
| 732 | timeout=timeout, |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 733 | stdout_tee=devnull, |
| 734 | stderr_tee=devnull) |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 735 | finally: |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 736 | self.host.job.pop_execution_context() |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 737 | |
| 738 | return result |
| 739 | |
| 740 | |
| 741 | @staticmethod |
| 742 | def _strip_stderr_prologue(stderr): |
| 743 | """Strips the 'standard' prologue that get pre-pended to every |
| 744 | remote command and returns the text that was actually written to |
| 745 | stderr by the remote command.""" |
| 746 | stderr_lines = stderr.split("\n")[1:] |
| 747 | if not stderr_lines: |
| 748 | return "" |
| 749 | elif stderr_lines[0].startswith("NOTE: autotestd_monitor"): |
| 750 | del stderr_lines[0] |
| 751 | return "\n".join(stderr_lines) |
| 752 | |
| 753 | |
| 754 | def _execute_daemon(self, section, timeout, stderr_redirector, |
| 755 | client_disconnect_timeout): |
| 756 | monitor_dir = self.host.get_tmp_dir() |
| 757 | daemon_cmd = self.get_daemon_cmd(section, monitor_dir) |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 758 | |
| 759 | # grab the location for the server-side client log file |
| 760 | client_log_prefix = self.get_client_log() |
| 761 | client_log_path = os.path.join(self.results_dir, 'debug', |
| 762 | client_log_prefix + '.log') |
| 763 | client_log = open(client_log_path, 'w', 0) |
| 764 | self.copy_client_config_file(client_log_prefix) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 765 | |
| 766 | stdout_read = stderr_read = 0 |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 767 | self.host.job.push_execution_context(self.results_dir) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 768 | try: |
| 769 | self.host.run(daemon_cmd, ignore_status=True, timeout=timeout) |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 770 | disconnect_warnings = [] |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 771 | while True: |
| 772 | monitor_cmd = self.get_monitor_cmd(monitor_dir, stdout_read, |
| 773 | stderr_read) |
| 774 | try: |
| 775 | result = self.host.run(monitor_cmd, ignore_status=True, |
| 776 | timeout=timeout, |
| 777 | stdout_tee=client_log, |
| 778 | stderr_tee=stderr_redirector) |
| 779 | except error.AutoservRunError, e: |
| 780 | result = e.result_obj |
| 781 | result.exit_status = None |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 782 | disconnect_warnings.append(e.description) |
| 783 | |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 784 | stderr_redirector.log_warning( |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 785 | "Autotest client was disconnected: %s" % e.description, |
| 786 | "NETWORK") |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 787 | except error.AutoservSSHTimeout: |
| 788 | result = utils.CmdResult(monitor_cmd, "", "", None, 0) |
| 789 | stderr_redirector.log_warning( |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 790 | "Attempt to connect to Autotest client timed out", |
| 791 | "NETWORK") |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 792 | |
| 793 | stdout_read += len(result.stdout) |
| 794 | stderr_read += len(self._strip_stderr_prologue(result.stderr)) |
| 795 | |
| 796 | if result.exit_status is not None: |
Simran Basi | bca10a6 | 2013-01-24 15:52:35 -0800 | [diff] [blame] | 797 | # TODO (crosbug.com/38224)- sbasi: Remove extra logging. |
| 798 | logging.debug('Result exit status is %d.', |
| 799 | result.exit_status) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 800 | return result |
| 801 | elif not self.host.wait_up(client_disconnect_timeout): |
| 802 | raise error.AutoservSSHTimeout( |
| 803 | "client was disconnected, reconnect timed out") |
| 804 | finally: |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 805 | client_log.close() |
mbligh | 0d0f67d | 2009-11-06 03:15:03 +0000 | [diff] [blame] | 806 | self.host.job.pop_execution_context() |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 807 | |
| 808 | |
| 809 | def execute_section(self, section, timeout, stderr_redirector, |
Dale Curtis | 9285ddf | 2011-01-05 11:47:24 -0800 | [diff] [blame] | 810 | client_disconnect_timeout): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 811 | logging.info("Executing %s/bin/autotest %s/control phase %d", |
| 812 | self.autodir, self.autodir, section) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 813 | |
| 814 | if self.background: |
| 815 | result = self._execute_in_background(section, timeout) |
| 816 | else: |
| 817 | result = self._execute_daemon(section, timeout, stderr_redirector, |
| 818 | client_disconnect_timeout) |
| 819 | |
| 820 | last_line = stderr_redirector.last_line |
mbligh | 2bf2db6 | 2007-11-27 00:53:18 +0000 | [diff] [blame] | 821 | |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 822 | # check if we failed hard enough to warrant an exception |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 823 | if result.exit_status == 1: |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 824 | err = error.AutotestRunError("client job was aborted") |
| 825 | elif not self.background and not result.stderr: |
| 826 | err = error.AutotestRunError( |
jadmanski | e413053 | 2009-03-17 18:01:28 +0000 | [diff] [blame] | 827 | "execute_section %s failed to return anything\n" |
| 828 | "stdout:%s\n" % (section, result.stdout)) |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 829 | else: |
| 830 | err = None |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 831 | |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 832 | # log something if the client failed AND never finished logging |
Dale Curtis | 9285ddf | 2011-01-05 11:47:24 -0800 | [diff] [blame] | 833 | if err and not self.is_client_job_finished(last_line): |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 834 | self.log_unexpected_abort(stderr_redirector) |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 835 | |
| 836 | if err: |
| 837 | raise err |
| 838 | else: |
| 839 | return stderr_redirector.last_line |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 840 | |
| 841 | |
jadmanski | c035491 | 2010-01-12 15:57:29 +0000 | [diff] [blame] | 842 | def _wait_for_reboot(self, old_boot_id): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 843 | logging.info("Client is rebooting") |
| 844 | logging.info("Waiting for client to halt") |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 845 | if not self.host.wait_down(self.host.WAIT_DOWN_REBOOT_TIMEOUT, |
| 846 | old_boot_id=old_boot_id): |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 847 | err = "%s failed to shutdown after %d" |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 848 | err %= (self.host.hostname, self.host.WAIT_DOWN_REBOOT_TIMEOUT) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 849 | raise error.AutotestRunError(err) |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 850 | logging.info("Client down, waiting for restart") |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 851 | if not self.host.wait_up(self.host.DEFAULT_REBOOT_TIMEOUT): |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 852 | # since reboot failed |
| 853 | # hardreset the machine once if possible |
| 854 | # before failing this control file |
| 855 | warning = "%s did not come back up, hard resetting" |
| 856 | warning %= self.host.hostname |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 857 | logging.warning(warning) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 858 | try: |
| 859 | self.host.hardreset(wait=False) |
mbligh | d99d3b27 | 2008-12-22 14:41:27 +0000 | [diff] [blame] | 860 | except (AttributeError, error.AutoservUnsupportedError): |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 861 | warning = "Hard reset unsupported on %s" |
| 862 | warning %= self.host.hostname |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 863 | logging.warning(warning) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 864 | raise error.AutotestRunError("%s failed to boot after %ds" % |
Dale Curtis | cb7bfaf | 2011-06-07 16:21:57 -0700 | [diff] [blame] | 865 | (self.host.hostname, |
| 866 | self.host.DEFAULT_REBOOT_TIMEOUT)) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 867 | self.host.reboot_followup() |
mbligh | dc735a2 | 2007-08-02 16:54:37 +0000 | [diff] [blame] | 868 | |
| 869 | |
Dale Curtis | 9285ddf | 2011-01-05 11:47:24 -0800 | [diff] [blame] | 870 | def execute_control(self, timeout=None, client_disconnect_timeout=None): |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 871 | if not self.background: |
| 872 | collector = log_collector(self.host, self.tag, self.results_dir) |
| 873 | hostname = self.host.hostname |
| 874 | remote_results = collector.client_results_dir |
| 875 | local_results = collector.server_results_dir |
| 876 | self.host.job.add_client_log(hostname, remote_results, |
| 877 | local_results) |
jadmanski | 5205363 | 2010-06-11 21:08:10 +0000 | [diff] [blame] | 878 | job_record_context = self.host.job.get_record_context() |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 879 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 880 | section = 0 |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 881 | start_time = time.time() |
| 882 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 883 | logger = client_logger(self.host, self.tag, self.results_dir) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 884 | try: |
| 885 | while not timeout or time.time() < start_time + timeout: |
| 886 | if timeout: |
| 887 | section_timeout = start_time + timeout - time.time() |
| 888 | else: |
| 889 | section_timeout = None |
jadmanski | c035491 | 2010-01-12 15:57:29 +0000 | [diff] [blame] | 890 | boot_id = self.host.get_boot_id() |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 891 | last = self.execute_section(section, section_timeout, |
Dale Curtis | 9285ddf | 2011-01-05 11:47:24 -0800 | [diff] [blame] | 892 | logger, client_disconnect_timeout) |
mbligh | b3c0c91 | 2008-11-27 00:32:45 +0000 | [diff] [blame] | 893 | if self.background: |
| 894 | return |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 895 | section += 1 |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 896 | if self.is_client_job_finished(last): |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 897 | logging.info("Client complete") |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 898 | return |
jadmanski | b264ed0 | 2009-01-12 23:54:27 +0000 | [diff] [blame] | 899 | elif self.is_client_job_rebooting(last): |
jadmanski | 79ab928 | 2008-11-11 17:53:12 +0000 | [diff] [blame] | 900 | try: |
jadmanski | c035491 | 2010-01-12 15:57:29 +0000 | [diff] [blame] | 901 | self._wait_for_reboot(boot_id) |
jadmanski | 79ab928 | 2008-11-11 17:53:12 +0000 | [diff] [blame] | 902 | except error.AutotestRunError, e: |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 903 | self.host.job.record("ABORT", None, "reboot", str(e)) |
| 904 | self.host.job.record("END ABORT", None, None, str(e)) |
jadmanski | 79ab928 | 2008-11-11 17:53:12 +0000 | [diff] [blame] | 905 | raise |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 906 | continue |
| 907 | |
beeps | 607428b | 2013-03-25 16:43:20 -0700 | [diff] [blame] | 908 | # If a test fails without probable cause we try to bucket it's |
| 909 | # failure into one of 2 categories. If we can determine the |
| 910 | # current state of the device and it is suspicious, we close the |
| 911 | # status lines indicating a failure. If we either cannot |
| 912 | # determine the state of the device, or it appears totally |
| 913 | # healthy, we give up and abort. |
| 914 | try: |
| 915 | self._diagnose_dut(boot_id) |
| 916 | except AutotestDeviceError as e: |
| 917 | # The status lines of the test are pretty much tailed to |
| 918 | # our log, with indentation, from the client job on the DUT. |
| 919 | # So if the DUT goes down unexpectedly we'll end up with a |
| 920 | # malformed status log unless we manually unwind the status |
| 921 | # stack. Ideally we would want to write a nice wrapper like |
| 922 | # server_job methods run_reboot, run_group but they expect |
| 923 | # reboots and we don't. |
| 924 | self.host.job.record('FAIL', None, None, str(e)) |
| 925 | self.host.job.record('END FAIL', None, None) |
| 926 | self.host.job.record('END GOOD', None, None) |
Dan Shi | b03ea9d | 2013-08-15 17:13:27 -0700 | [diff] [blame] | 927 | self.host.job.failed_with_device_error = True |
beeps | 607428b | 2013-03-25 16:43:20 -0700 | [diff] [blame] | 928 | return |
| 929 | except AutotestAbort as e: |
| 930 | self.host.job.record('ABORT', None, None, str(e)) |
| 931 | self.host.job.record('END ABORT', None, None) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 932 | |
beeps | 607428b | 2013-03-25 16:43:20 -0700 | [diff] [blame] | 933 | # give the client machine a chance to recover from a crash |
| 934 | self.host.wait_up( |
| 935 | self.host.HOURS_TO_WAIT_FOR_RECOVERY * 3600) |
| 936 | msg = ("Aborting - unexpected final status message from " |
| 937 | "client on %s: %s\n") % (self.host.hostname, last) |
| 938 | raise error.AutotestRunError(msg) |
jadmanski | 4600e34 | 2008-10-29 22:54:00 +0000 | [diff] [blame] | 939 | finally: |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 940 | logger.close() |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 941 | if not self.background: |
| 942 | collector.collect_client_job_results() |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 943 | collector.remove_redundant_client_logs() |
mbligh | fc3da5b | 2010-01-06 18:37:22 +0000 | [diff] [blame] | 944 | state_file = os.path.basename(self.remote_control_file |
| 945 | + '.state') |
| 946 | state_path = os.path.join(self.results_dir, state_file) |
| 947 | self.host.job.postprocess_client_state(state_path) |
jadmanski | 6bb32d7 | 2009-03-19 20:25:24 +0000 | [diff] [blame] | 948 | self.host.job.remove_client_log(hostname, remote_results, |
| 949 | local_results) |
jadmanski | 5205363 | 2010-06-11 21:08:10 +0000 | [diff] [blame] | 950 | job_record_context.restore() |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 951 | |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 952 | # should only get here if we timed out |
| 953 | assert timeout |
| 954 | raise error.AutotestTimeoutError() |
mbligh | 0e4613b | 2007-10-29 16:55:07 +0000 | [diff] [blame] | 955 | |
mbligh | dcd57a8 | 2007-07-11 23:06:47 +0000 | [diff] [blame] | 956 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 957 | class log_collector(object): |
| 958 | def __init__(self, host, client_tag, results_dir): |
| 959 | self.host = host |
| 960 | if not client_tag: |
| 961 | client_tag = "default" |
| 962 | self.client_results_dir = os.path.join(host.get_autodir(), "results", |
| 963 | client_tag) |
| 964 | self.server_results_dir = results_dir |
| 965 | |
| 966 | |
| 967 | def collect_client_job_results(self): |
| 968 | """ A method that collects all the current results of a running |
| 969 | client job into the results dir. By default does nothing as no |
| 970 | client job is running, but when running a client job you can override |
| 971 | this with something that will actually do something. """ |
| 972 | |
| 973 | # make an effort to wait for the machine to come up |
| 974 | try: |
| 975 | self.host.wait_up(timeout=30) |
| 976 | except error.AutoservError: |
| 977 | # don't worry about any errors, we'll try and |
| 978 | # get the results anyway |
| 979 | pass |
| 980 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 981 | # Copy all dirs in default to results_dir |
Gabe Black | 1e1c41b | 2015-02-04 23:55:15 -0800 | [diff] [blame] | 982 | timer = autotest_stats.Timer('collect_client_job_results') |
Dan Shi | 209eefd | 2013-05-30 13:17:48 -0700 | [diff] [blame] | 983 | timer.start() |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 984 | try: |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 985 | self.host.get_file(self.client_results_dir + '/', |
mbligh | 4556178 | 2009-05-11 21:14:34 +0000 | [diff] [blame] | 986 | self.server_results_dir, preserve_symlinks=True) |
Dan Shi | b03ea9d | 2013-08-15 17:13:27 -0700 | [diff] [blame] | 987 | |
Dan Shi | 209eefd | 2013-05-30 13:17:48 -0700 | [diff] [blame] | 988 | # Only report time used for successful get_file calls. |
| 989 | timer.stop(); |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 990 | except Exception: |
| 991 | # well, don't stop running just because we couldn't get logs |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 992 | e_msg = "Unexpected error copying test result logs, continuing ..." |
| 993 | logging.error(e_msg) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 994 | traceback.print_exc(file=sys.stdout) |
| 995 | |
| 996 | |
jadmanski | 4d03cf6 | 2010-03-04 18:32:28 +0000 | [diff] [blame] | 997 | def remove_redundant_client_logs(self): |
| 998 | """Remove client.*.log files in favour of client.*.DEBUG files.""" |
| 999 | debug_dir = os.path.join(self.server_results_dir, 'debug') |
| 1000 | debug_files = [f for f in os.listdir(debug_dir) |
| 1001 | if re.search(r'^client\.\d+\.DEBUG$', f)] |
| 1002 | for debug_file in debug_files: |
| 1003 | log_file = debug_file.replace('DEBUG', 'log') |
| 1004 | log_file = os.path.join(debug_dir, log_file) |
| 1005 | if os.path.exists(log_file): |
| 1006 | os.remove(log_file) |
| 1007 | |
| 1008 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1009 | # a file-like object for catching stderr from an autotest client and |
| 1010 | # extracting status logs from it |
Chris Sosa | 3ee5d5c | 2012-02-23 11:18:41 -0800 | [diff] [blame] | 1011 | class BaseClientLogger(object): |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1012 | """Partial file object to write to both stdout and |
| 1013 | the status log file. We only implement those methods |
| 1014 | utils.run() actually calls. |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1015 | """ |
| 1016 | status_parser = re.compile(r"^AUTOTEST_STATUS:([^:]*):(.*)$") |
| 1017 | test_complete_parser = re.compile(r"^AUTOTEST_TEST_COMPLETE:(.*)$") |
jadmanski | b1a5113 | 2009-08-07 16:45:50 +0000 | [diff] [blame] | 1018 | fetch_package_parser = re.compile( |
| 1019 | r"^AUTOTEST_FETCH_PACKAGE:([^:]*):([^:]*):(.*)$") |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1020 | extract_indent = re.compile(r"^(\t*).*$") |
jadmanski | efe4ebf | 2009-05-21 22:12:30 +0000 | [diff] [blame] | 1021 | extract_timestamp = re.compile(r".*\ttimestamp=(\d+)\t.*$") |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1022 | |
| 1023 | def __init__(self, host, tag, server_results_dir): |
| 1024 | self.host = host |
| 1025 | self.job = host.job |
| 1026 | self.log_collector = log_collector(host, tag, server_results_dir) |
| 1027 | self.leftover = "" |
| 1028 | self.last_line = "" |
| 1029 | self.logs = {} |
jadmanski | efe4ebf | 2009-05-21 22:12:30 +0000 | [diff] [blame] | 1030 | |
| 1031 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1032 | def _process_log_dict(self, log_dict): |
| 1033 | log_list = log_dict.pop("logs", []) |
| 1034 | for key in sorted(log_dict.iterkeys()): |
| 1035 | log_list += self._process_log_dict(log_dict.pop(key)) |
| 1036 | return log_list |
| 1037 | |
| 1038 | |
| 1039 | def _process_logs(self): |
| 1040 | """Go through the accumulated logs in self.log and print them |
| 1041 | out to stdout and the status log. Note that this processes |
| 1042 | logs in an ordering where: |
| 1043 | |
| 1044 | 1) logs to different tags are never interleaved |
| 1045 | 2) logs to x.y come before logs to x.y.z for all z |
| 1046 | 3) logs to x.y come before x.z whenever y < z |
| 1047 | |
| 1048 | Note that this will in general not be the same as the |
| 1049 | chronological ordering of the logs. However, if a chronological |
| 1050 | ordering is desired that one can be reconstructed from the |
| 1051 | status log by looking at timestamp lines.""" |
| 1052 | log_list = self._process_log_dict(self.logs) |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 1053 | for entry in log_list: |
| 1054 | self.job.record_entry(entry, log_in_subdir=False) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1055 | if log_list: |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 1056 | self.last_line = log_list[-1].render() |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1057 | |
| 1058 | |
| 1059 | def _process_quoted_line(self, tag, line): |
| 1060 | """Process a line quoted with an AUTOTEST_STATUS flag. If the |
| 1061 | tag is blank then we want to push out all the data we've been |
| 1062 | building up in self.logs, and then the newest line. If the |
| 1063 | tag is not blank, then push the line into the logs for handling |
| 1064 | later.""" |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 1065 | entry = base_job.status_log_entry.parse(line) |
| 1066 | if entry is None: |
| 1067 | return # the line contains no status lines |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1068 | if tag == "": |
| 1069 | self._process_logs() |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 1070 | self.job.record_entry(entry, log_in_subdir=False) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1071 | self.last_line = line |
| 1072 | else: |
| 1073 | tag_parts = [int(x) for x in tag.split(".")] |
| 1074 | log_dict = self.logs |
| 1075 | for part in tag_parts: |
| 1076 | log_dict = log_dict.setdefault(part, {}) |
| 1077 | log_list = log_dict.setdefault("logs", []) |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 1078 | log_list.append(entry) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1079 | |
| 1080 | |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 1081 | def _process_info_line(self, line): |
| 1082 | """Check if line is an INFO line, and if it is, interpret any control |
| 1083 | messages (e.g. enabling/disabling warnings) that it may contain.""" |
| 1084 | match = re.search(r"^\t*INFO\t----\t----(.*)\t[^\t]*$", line) |
| 1085 | if not match: |
| 1086 | return # not an INFO line |
| 1087 | for field in match.group(1).split('\t'): |
| 1088 | if field.startswith("warnings.enable="): |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 1089 | func = self.job.warning_manager.enable_warnings |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 1090 | elif field.startswith("warnings.disable="): |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 1091 | func = self.job.warning_manager.disable_warnings |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 1092 | else: |
| 1093 | continue |
| 1094 | warning_type = field.split("=", 1)[1] |
jadmanski | 16a7ff7 | 2009-04-01 18:19:53 +0000 | [diff] [blame] | 1095 | func(warning_type) |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 1096 | |
| 1097 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1098 | def _process_line(self, line): |
| 1099 | """Write out a line of data to the appropriate stream. Status |
| 1100 | lines sent by autotest will be prepended with |
| 1101 | "AUTOTEST_STATUS", and all other lines are ssh error |
| 1102 | messages.""" |
| 1103 | status_match = self.status_parser.search(line) |
| 1104 | test_complete_match = self.test_complete_parser.search(line) |
jadmanski | b1a5113 | 2009-08-07 16:45:50 +0000 | [diff] [blame] | 1105 | fetch_package_match = self.fetch_package_parser.search(line) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1106 | if status_match: |
| 1107 | tag, line = status_match.groups() |
jadmanski | f37df84 | 2009-02-11 00:03:26 +0000 | [diff] [blame] | 1108 | self._process_info_line(line) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1109 | self._process_quoted_line(tag, line) |
| 1110 | elif test_complete_match: |
jadmanski | fcc0d5d | 2009-02-12 21:52:54 +0000 | [diff] [blame] | 1111 | self._process_logs() |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1112 | fifo_path, = test_complete_match.groups() |
mbligh | 060c471 | 2009-12-29 02:43:35 +0000 | [diff] [blame] | 1113 | try: |
| 1114 | self.log_collector.collect_client_job_results() |
| 1115 | self.host.run("echo A > %s" % fifo_path) |
| 1116 | except Exception: |
| 1117 | msg = "Post-test log collection failed, continuing anyway" |
| 1118 | logging.exception(msg) |
jadmanski | b1a5113 | 2009-08-07 16:45:50 +0000 | [diff] [blame] | 1119 | elif fetch_package_match: |
| 1120 | pkg_name, dest_path, fifo_path = fetch_package_match.groups() |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 1121 | serve_packages = global_config.global_config.get_config_value( |
| 1122 | "PACKAGES", "serve_packages_from_autoserv", type=bool) |
| 1123 | if serve_packages and pkg_name.endswith(".tar.bz2"): |
| 1124 | try: |
| 1125 | self._send_tarball(pkg_name, dest_path) |
| 1126 | except Exception: |
| 1127 | msg = "Package tarball creation failed, continuing anyway" |
| 1128 | logging.exception(msg) |
mbligh | 060c471 | 2009-12-29 02:43:35 +0000 | [diff] [blame] | 1129 | try: |
| 1130 | self.host.run("echo B > %s" % fifo_path) |
| 1131 | except Exception: |
| 1132 | msg = "Package tarball installation failed, continuing anyway" |
| 1133 | logging.exception(msg) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1134 | else: |
showard | b18134f | 2009-03-20 20:52:18 +0000 | [diff] [blame] | 1135 | logging.info(line) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1136 | |
| 1137 | |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 1138 | def _send_tarball(self, pkg_name, remote_dest): |
| 1139 | name, pkg_type = self.job.pkgmgr.parse_tarball_name(pkg_name) |
| 1140 | src_dirs = [] |
| 1141 | if pkg_type == 'test': |
mbligh | 1f572e5 | 2010-04-01 17:15:53 +0000 | [diff] [blame] | 1142 | for test_dir in ['site_tests', 'tests']: |
| 1143 | src_dir = os.path.join(self.job.clientdir, test_dir, name) |
| 1144 | if os.path.exists(src_dir): |
| 1145 | src_dirs += [src_dir] |
| 1146 | if autoserv_prebuild: |
| 1147 | prebuild.setup(self.job.clientdir, src_dir) |
| 1148 | break |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 1149 | elif pkg_type == 'profiler': |
| 1150 | src_dirs += [os.path.join(self.job.clientdir, 'profilers', name)] |
mbligh | 1f572e5 | 2010-04-01 17:15:53 +0000 | [diff] [blame] | 1151 | if autoserv_prebuild: |
| 1152 | prebuild.setup(self.job.clientdir, src_dir) |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 1153 | elif pkg_type == 'dep': |
| 1154 | src_dirs += [os.path.join(self.job.clientdir, 'deps', name)] |
| 1155 | elif pkg_type == 'client': |
| 1156 | return # you must already have a client to hit this anyway |
| 1157 | else: |
| 1158 | return # no other types are supported |
| 1159 | |
| 1160 | # iterate over src_dirs until we find one that exists, then tar it |
| 1161 | for src_dir in src_dirs: |
| 1162 | if os.path.exists(src_dir): |
| 1163 | try: |
| 1164 | logging.info('Bundling %s into %s', src_dir, pkg_name) |
| 1165 | temp_dir = autotemp.tempdir(unique_id='autoserv-packager', |
| 1166 | dir=self.job.tmpdir) |
| 1167 | tarball_path = self.job.pkgmgr.tar_package( |
mbligh | bccad48 | 2009-08-24 22:08:31 +0000 | [diff] [blame] | 1168 | pkg_name, src_dir, temp_dir.name, " .") |
jadmanski | ede7e24 | 2009-08-10 15:43:33 +0000 | [diff] [blame] | 1169 | self.host.send_file(tarball_path, remote_dest) |
| 1170 | finally: |
| 1171 | temp_dir.clean() |
| 1172 | return |
| 1173 | |
| 1174 | |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 1175 | def log_warning(self, msg, warning_type): |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 1176 | """Injects a WARN message into the current status logging stream.""" |
jadmanski | 91d56a9 | 2009-04-01 15:20:40 +0000 | [diff] [blame] | 1177 | timestamp = int(time.time()) |
| 1178 | if self.job.warning_manager.is_valid(timestamp, warning_type): |
Eric Li | d656d56 | 2011-04-20 11:48:29 -0700 | [diff] [blame] | 1179 | self.job.record('WARN', None, None, msg) |
jadmanski | 6dadd83 | 2009-02-05 23:39:27 +0000 | [diff] [blame] | 1180 | |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1181 | |
| 1182 | def write(self, data): |
jadmanski | 2a89dac | 2010-06-11 14:32:58 +0000 | [diff] [blame] | 1183 | # now start processing the existing buffer and the new data |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1184 | data = self.leftover + data |
mbligh | 060c471 | 2009-12-29 02:43:35 +0000 | [diff] [blame] | 1185 | lines = data.split('\n') |
| 1186 | processed_lines = 0 |
| 1187 | try: |
| 1188 | # process all the buffered data except the last line |
| 1189 | # ignore the last line since we may not have all of it yet |
| 1190 | for line in lines[:-1]: |
mbligh | 060c471 | 2009-12-29 02:43:35 +0000 | [diff] [blame] | 1191 | self._process_line(line) |
| 1192 | processed_lines += 1 |
| 1193 | finally: |
| 1194 | # save any unprocessed lines for future processing |
| 1195 | self.leftover = '\n'.join(lines[processed_lines:]) |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1196 | |
| 1197 | |
| 1198 | def flush(self): |
| 1199 | sys.stdout.flush() |
| 1200 | |
| 1201 | |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 1202 | def flush_all_buffers(self): |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1203 | if self.leftover: |
| 1204 | self._process_line(self.leftover) |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 1205 | self.leftover = "" |
jadmanski | 043e113 | 2008-11-19 17:10:32 +0000 | [diff] [blame] | 1206 | self._process_logs() |
| 1207 | self.flush() |
| 1208 | |
| 1209 | |
jadmanski | a61edad | 2009-05-21 22:17:49 +0000 | [diff] [blame] | 1210 | def close(self): |
| 1211 | self.flush_all_buffers() |
| 1212 | |
| 1213 | |
mbligh | a700772 | 2009-01-13 00:37:11 +0000 | [diff] [blame] | 1214 | SiteAutotest = client_utils.import_site_class( |
| 1215 | __file__, "autotest_lib.server.site_autotest", "SiteAutotest", |
| 1216 | BaseAutotest) |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 1217 | |
showard | ad812bf | 2009-10-20 23:49:56 +0000 | [diff] [blame] | 1218 | |
jadmanski | 69bdaac | 2010-07-28 16:27:20 +0000 | [diff] [blame] | 1219 | _SiteRun = client_utils.import_site_class( |
| 1220 | __file__, "autotest_lib.server.site_autotest", "_SiteRun", _BaseRun) |
| 1221 | |
| 1222 | |
Chris Sosa | 3ee5d5c | 2012-02-23 11:18:41 -0800 | [diff] [blame] | 1223 | SiteClientLogger = client_utils.import_site_class( |
| 1224 | __file__, "autotest_lib.server.site_autotest", "SiteClientLogger", |
| 1225 | BaseClientLogger) |
| 1226 | |
| 1227 | |
mbligh | d8b3925 | 2008-03-20 21:15:03 +0000 | [diff] [blame] | 1228 | class Autotest(SiteAutotest): |
jadmanski | 0afbb63 | 2008-06-06 21:10:57 +0000 | [diff] [blame] | 1229 | pass |
jadmanski | c6136e9 | 2010-07-19 16:41:49 +0000 | [diff] [blame] | 1230 | |
| 1231 | |
Chris Sosa | 3ee5d5c | 2012-02-23 11:18:41 -0800 | [diff] [blame] | 1232 | class client_logger(SiteClientLogger): |
| 1233 | pass |
| 1234 | |
| 1235 | |
jadmanski | 69bdaac | 2010-07-28 16:27:20 +0000 | [diff] [blame] | 1236 | class _Run(_SiteRun): |
| 1237 | pass |