blob: f36a1625b32b96885de1a0bd5395d42c71d9a6bd [file] [log] [blame]
Chris Masone8abb6fc2012-01-31 09:27:36 -08001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Dale Curtis386eea72011-09-21 18:43:04 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
Chris Masone8abb6fc2012-01-31 09:27:36 -08005import logging
Eric Lia11a0452010-01-07 22:01:58 -08006import os
Christopher Wiley809301c2013-07-02 18:00:31 -07007import tempfile
Alex Miller4f341702013-03-25 12:39:12 -07008import urllib2
Christopher Wiley809301c2013-07-02 18:00:31 -07009
Chris Sosa3ee5d5c2012-02-23 11:18:41 -080010from autotest_lib.client.common_lib import error, global_config
Scott Zawalskieadbf702013-03-14 09:23:06 -040011from autotest_lib.client.common_lib.cros import dev_server
Chris Masone8abb6fc2012-01-31 09:27:36 -080012from autotest_lib.server import installable_object, autoserv_parser
Dan Shi82997b92015-05-06 12:08:02 -070013from autotest_lib.server import utils as server_utils
Scott Zawalskieadbf702013-03-14 09:23:06 -040014from autotest_lib.server.cros.dynamic_suite import tools
Chris Masone44e4d6c2012-08-15 14:25:53 -070015from autotest_lib.server.cros.dynamic_suite.constants import JOB_REPO_URL
Eric Li2eb800f2011-04-21 16:52:58 -070016
17
Chris Masone44e4d6c2012-08-15 14:25:53 -070018_CONFIG = global_config.global_config
19_PARSER = autoserv_parser.autoserv_parser
Eric Li2eb800f2011-04-21 16:52:58 -070020
Eric Lia11a0452010-01-07 22:01:58 -080021
22class SiteAutotest(installable_object.InstallableObject):
Dan Shib669cbd2013-09-13 11:17:17 -070023 """Site implementation of Autotest."""
Eric Lia11a0452010-01-07 22:01:58 -080024
Chris Sosa3ee5d5c2012-02-23 11:18:41 -080025 def get(self, location=None):
Eric Lia11a0452010-01-07 22:01:58 -080026 if not location:
27 location = os.path.join(self.serverdir, '../client')
28 location = os.path.abspath(location)
29 installable_object.InstallableObject.get(self, location)
30 self.got = True
31
Eric Lic7444bd2011-02-15 17:12:10 -080032
Chris Masone8abb6fc2012-01-31 09:27:36 -080033 def _get_fetch_location_from_host_attribute(self):
34 """Get repo to use for packages from host attribute, if possible.
35
36 Hosts are tagged with an attribute containing the URL
37 from which to source packages when running a test on that host.
38 If self.host is set, attempt to look this attribute up by calling out
39 to the AFE.
40
41 @returns value of the 'job_repo_url' host attribute, if present.
42 """
43 try:
Dan Shi349a33c2016-03-16 23:57:21 -070044 from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
Chris Masone8abb6fc2012-01-31 09:27:36 -080045 if self.host:
Dan Shi349a33c2016-03-16 23:57:21 -070046 afe = frontend_wrappers.RetryingAFE(timeout_min=5, delay_sec=10)
Chris Masone8abb6fc2012-01-31 09:27:36 -080047 hosts = afe.get_hosts(hostname=self.host.hostname)
Chris Masone44e4d6c2012-08-15 14:25:53 -070048 if hosts and JOB_REPO_URL in hosts[0].attributes:
Dan Shi349a33c2016-03-16 23:57:21 -070049 logging.info('Get job repo url from host attributes: %s',
50 hosts[0].attributes[JOB_REPO_URL])
Chris Masone44e4d6c2012-08-15 14:25:53 -070051 return hosts[0].attributes[JOB_REPO_URL]
52 logging.warning("No %s for %s", JOB_REPO_URL, self.host)
Alex Miller4f341702013-03-25 12:39:12 -070053 except (ImportError, urllib2.URLError):
Chris Masone44e4d6c2012-08-15 14:25:53 -070054 logging.warning('Not attempting to look for %s', JOB_REPO_URL)
Chris Masone8abb6fc2012-01-31 09:27:36 -080055 pass
56 return None
57
58
Eric Li2eb800f2011-04-21 16:52:58 -070059 def get_fetch_location(self):
Chris Masone8abb6fc2012-01-31 09:27:36 -080060 """Generate list of locations where autotest can look for packages.
61
62 Old n' busted: Autotest packages are always stored at a URL that can
63 be derived from the one passed via the voodoo magic --image argument.
64 New hotness: Hosts are tagged with an attribute containing the URL
65 from which to source packages when running a test on that host.
66
67 @returns the list of candidate locations to check for packages.
68 """
Dale Curtis386eea72011-09-21 18:43:04 -070069 repos = super(SiteAutotest, self).get_fetch_location()
Chris Masone8abb6fc2012-01-31 09:27:36 -080070
Chris Masone44e4d6c2012-08-15 14:25:53 -070071 if _PARSER.options.image:
Scott Zawalskieadbf702013-03-14 09:23:06 -040072 image_opt = _PARSER.options.image
73 if image_opt.startswith('http://'):
74 # A devserver HTTP url was specified, set that as the repo_url.
75 repos.append(image_opt.replace(
joychen03eaad92013-06-26 09:55:21 -070076 'update', 'static').rstrip('/') + '/autotest')
Scott Zawalskieadbf702013-03-14 09:23:06 -040077 else:
78 # An image_name like stumpy-release/R27-3437.0.0 was specified,
79 # set this as the repo_url for the host. If an AFE is not being
80 # run, this will ensure that the installed build uses the
81 # associated artifacts for the test specified when running
82 # autoserv with --image. However, any subsequent tests run on
83 # the host will no longer have the context of the image option
84 # and will revert back to utilizing test code/artifacts that are
85 # currently present in the users source checkout.
86 devserver_url = dev_server.ImageServer.resolve(image_opt).url()
87 repo_url = tools.get_package_url(devserver_url, image_opt)
88 repos.append(repo_url)
Dan Shi82997b92015-05-06 12:08:02 -070089 elif not server_utils.is_inside_chroot():
90 # Only try to get fetch location from host attribute if the test
91 # is not running inside chroot.
Scott Zawalskieadbf702013-03-14 09:23:06 -040092 # No --image option was specified, look for the repo url via
93 # the host attribute. If we are not running with a full AFE
94 # autoserv will fall back to serving packages itself from whatever
95 # source version it is sync'd to rather than using the proper
96 # artifacts for the build on the host.
Chris Masonebe78eb02012-02-23 14:28:19 -080097 found_repo = self._get_fetch_location_from_host_attribute()
98 if found_repo is not None:
99 # Add our new repo to the end, the package manager will
100 # later reverse the list of repositories resulting in ours
101 # being first
102 repos.append(found_repo)
Eric Li2eb800f2011-04-21 16:52:58 -0700103
Dale Curtis386eea72011-09-21 18:43:04 -0700104 return repos
Eric Li2eb800f2011-04-21 16:52:58 -0700105
106
Dan Shib669cbd2013-09-13 11:17:17 -0700107 def install(self, host=None, autodir=None, use_packaging=True):
Chris Masone8abb6fc2012-01-31 09:27:36 -0800108 """Install autotest. If |host| is not None, stores it in |self.host|.
109
110 @param host A Host instance on which autotest will be installed
111 @param autodir Location on the remote host to install to
Dan Shib669cbd2013-09-13 11:17:17 -0700112 @param use_packaging Enable install modes that use the packaging system.
113
Chris Masone8abb6fc2012-01-31 09:27:36 -0800114 """
115 if host:
116 self.host = host
117
Dan Shib669cbd2013-09-13 11:17:17 -0700118 super(SiteAutotest, self).install(host=host, autodir=autodir,
119 use_packaging=use_packaging)
Chris Masone8abb6fc2012-01-31 09:27:36 -0800120
121
Christopher Wiley809301c2013-07-02 18:00:31 -0700122 def _install(self, host=None, autodir=None, use_autoserv=True,
123 use_packaging=True):
124 """
125 Install autotest. If get() was not called previously, an
126 attempt will be made to install from the autotest svn
127 repository.
128
129 @param host A Host instance on which autotest will be installed
130 @param autodir Location on the remote host to install to
131 @param use_autoserv Enable install modes that depend on the client
132 running with the autoserv harness
133 @param use_packaging Enable install modes that use the packaging system
134
135 @exception AutoservError if a tarball was not specified and
136 the target host does not have svn installed in its path
137 """
138 # TODO(milleral): http://crbug.com/258161
139 super(SiteAutotest, self)._install(host, autodir, use_autoserv,
140 use_packaging)
141 # Send over the most recent global_config.ini after installation if one
142 # is available.
143 # This code is a bit duplicated from
144 # _BaseRun._create_client_config_file, but oh well.
145 if self.installed and self.source_material:
146 logging.info('Installing updated global_config.ini.')
147 destination = os.path.join(self.host.get_autodir(),
148 'global_config.ini')
149 with tempfile.NamedTemporaryFile() as client_config:
150 config = global_config.global_config
151 client_section = config.get_section_values('CLIENT')
152 client_section.write(client_config)
153 client_config.flush()
154 self.host.send_file(client_config.name, destination)
155
156
Chris Sosaf4d43ff2012-10-30 11:21:05 -0700157 def run_static_method(self, module, method, results_dir='.', host=None,
158 *args):
159 """Runs a non-instance method with |args| from |module| on the client.
160
161 This method runs a static/class/module autotest method on the client.
162 For example:
163 run_static_method("autotest_lib.client.cros.cros_ui", "reboot")
164
165 Will run autotest_lib.client.cros.cros_ui.reboot() on the client.
166
167 @param module: module name as you would refer to it when importing in a
168 control file. e.g. autotest_lib.client.common_lib.module_name.
169 @param method: the method you want to call.
170 @param results_dir: A str path where the results should be stored
171 on the local filesystem.
172 @param host: A Host instance on which the control file should
173 be run.
174 @param args: args to pass to the method.
175 """
176 control = "\n".join(["import %s" % module,
177 "%s.%s(%s)\n" % (module, method,
178 ','.join(map(repr, args)))])
179 self.run(control, results_dir=results_dir, host=host)
180
181
Chris Sosa3ee5d5c2012-02-23 11:18:41 -0800182class SiteClientLogger(object):
183 """Overrides default client logger to allow for using a local package cache.
184 """
185
186 def _process_line(self, line):
187 """Returns the package checksum file if it exists."""
188 logging.debug(line)
189 fetch_package_match = self.fetch_package_parser.search(line)
190 if fetch_package_match:
191 pkg_name, dest_path, fifo_path = fetch_package_match.groups()
Chris Masone44e4d6c2012-08-15 14:25:53 -0700192 serve_packages = _CONFIG.get_config_value(
Chris Sosa3ee5d5c2012-02-23 11:18:41 -0800193 "PACKAGES", "serve_packages_from_autoserv", type=bool)
194 if serve_packages and pkg_name == 'packages.checksum':
Chris Sosa3ee5d5c2012-02-23 11:18:41 -0800195 try:
196 checksum_file = os.path.join(
197 self.job.pkgmgr.pkgmgr_dir, 'packages', pkg_name)
198 if os.path.exists(checksum_file):
199 self.host.send_file(checksum_file, dest_path)
Chris Sosa3ee5d5c2012-02-23 11:18:41 -0800200 except error.AutoservRunError:
201 msg = "Package checksum file not found, continuing anyway"
202 logging.exception(msg)
203
Chris Sosa2cc4da02012-07-09 16:18:24 -0700204 try:
205 # When fetching a package, the client expects to be
206 # notified when the fetching is complete. Autotest
207 # does this pushing a B to a fifo queue to the client.
208 self.host.run("echo B > %s" % fifo_path)
209 except error.AutoservRunError:
210 msg = "Checksum installation failed, continuing anyway"
211 logging.exception(msg)
212 finally:
213 return
Chris Sosa3ee5d5c2012-02-23 11:18:41 -0800214
215 # Fall through to process the line using the default method.
216 super(SiteClientLogger, self)._process_line(line)
217
218
219 def _send_tarball(self, pkg_name, remote_dest):
220 """Uses tarballs in package manager by default."""
221 try:
222 server_package = os.path.join(self.job.pkgmgr.pkgmgr_dir,
223 'packages', pkg_name)
224 if os.path.exists(server_package):
225 self.host.send_file(server_package, remote_dest)
226 return
227
228 except error.AutoservRunError:
229 msg = ("Package %s could not be sent from the package cache." %
230 pkg_name)
231 logging.exception(msg)
232
233 # Fall through to send tarball the default method.
234 super(SiteClientLogger, self)._send_tarball(pkg_name, remote_dest)
235
236
Eric Lic7444bd2011-02-15 17:12:10 -0800237class _SiteRun(object):
238 pass