blob: fe008355edab6bc716f5fee3bc142696fb4f4cb0 [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
Scott Zawalskieadbf702013-03-14 09:23:06 -040013from autotest_lib.server.cros.dynamic_suite import tools
Chris Masone44e4d6c2012-08-15 14:25:53 -070014from autotest_lib.server.cros.dynamic_suite.constants import JOB_REPO_URL
Eric Li2eb800f2011-04-21 16:52:58 -070015
16
Chris Masone44e4d6c2012-08-15 14:25:53 -070017_CONFIG = global_config.global_config
18_PARSER = autoserv_parser.autoserv_parser
Eric Li2eb800f2011-04-21 16:52:58 -070019
Eric Lia11a0452010-01-07 22:01:58 -080020
21class SiteAutotest(installable_object.InstallableObject):
Dan Shib669cbd2013-09-13 11:17:17 -070022 """Site implementation of Autotest."""
Eric Lia11a0452010-01-07 22:01:58 -080023
Chris Sosa3ee5d5c2012-02-23 11:18:41 -080024 def get(self, location=None):
Eric Lia11a0452010-01-07 22:01:58 -080025 if not location:
26 location = os.path.join(self.serverdir, '../client')
27 location = os.path.abspath(location)
28 installable_object.InstallableObject.get(self, location)
29 self.got = True
30
Eric Lic7444bd2011-02-15 17:12:10 -080031
Chris Masone8abb6fc2012-01-31 09:27:36 -080032 def _get_fetch_location_from_host_attribute(self):
33 """Get repo to use for packages from host attribute, if possible.
34
35 Hosts are tagged with an attribute containing the URL
36 from which to source packages when running a test on that host.
37 If self.host is set, attempt to look this attribute up by calling out
38 to the AFE.
39
40 @returns value of the 'job_repo_url' host attribute, if present.
41 """
42 try:
43 from autotest_lib.server import frontend
44 if self.host:
45 afe = frontend.AFE(debug=False)
46 hosts = afe.get_hosts(hostname=self.host.hostname)
Chris Masone44e4d6c2012-08-15 14:25:53 -070047 if hosts and JOB_REPO_URL in hosts[0].attributes:
48 return hosts[0].attributes[JOB_REPO_URL]
49 logging.warning("No %s for %s", JOB_REPO_URL, self.host)
Alex Miller4f341702013-03-25 12:39:12 -070050 except (ImportError, urllib2.URLError):
Chris Masone44e4d6c2012-08-15 14:25:53 -070051 logging.warning('Not attempting to look for %s', JOB_REPO_URL)
Chris Masone8abb6fc2012-01-31 09:27:36 -080052 pass
53 return None
54
55
Eric Li2eb800f2011-04-21 16:52:58 -070056 def get_fetch_location(self):
Chris Masone8abb6fc2012-01-31 09:27:36 -080057 """Generate list of locations where autotest can look for packages.
58
59 Old n' busted: Autotest packages are always stored at a URL that can
60 be derived from the one passed via the voodoo magic --image argument.
61 New hotness: Hosts are tagged with an attribute containing the URL
62 from which to source packages when running a test on that host.
63
64 @returns the list of candidate locations to check for packages.
65 """
Dale Curtis386eea72011-09-21 18:43:04 -070066 repos = super(SiteAutotest, self).get_fetch_location()
Chris Masone8abb6fc2012-01-31 09:27:36 -080067
Chris Masone44e4d6c2012-08-15 14:25:53 -070068 if _PARSER.options.image:
Scott Zawalskieadbf702013-03-14 09:23:06 -040069 image_opt = _PARSER.options.image
70 if image_opt.startswith('http://'):
71 # A devserver HTTP url was specified, set that as the repo_url.
72 repos.append(image_opt.replace(
joychen03eaad92013-06-26 09:55:21 -070073 'update', 'static').rstrip('/') + '/autotest')
Scott Zawalskieadbf702013-03-14 09:23:06 -040074 else:
75 # An image_name like stumpy-release/R27-3437.0.0 was specified,
76 # set this as the repo_url for the host. If an AFE is not being
77 # run, this will ensure that the installed build uses the
78 # associated artifacts for the test specified when running
79 # autoserv with --image. However, any subsequent tests run on
80 # the host will no longer have the context of the image option
81 # and will revert back to utilizing test code/artifacts that are
82 # currently present in the users source checkout.
83 devserver_url = dev_server.ImageServer.resolve(image_opt).url()
84 repo_url = tools.get_package_url(devserver_url, image_opt)
85 repos.append(repo_url)
Chris Masonebe78eb02012-02-23 14:28:19 -080086 else:
Scott Zawalskieadbf702013-03-14 09:23:06 -040087 # No --image option was specified, look for the repo url via
88 # the host attribute. If we are not running with a full AFE
89 # autoserv will fall back to serving packages itself from whatever
90 # source version it is sync'd to rather than using the proper
91 # artifacts for the build on the host.
Chris Masonebe78eb02012-02-23 14:28:19 -080092 found_repo = self._get_fetch_location_from_host_attribute()
93 if found_repo is not None:
94 # Add our new repo to the end, the package manager will
95 # later reverse the list of repositories resulting in ours
96 # being first
97 repos.append(found_repo)
Eric Li2eb800f2011-04-21 16:52:58 -070098
Dale Curtis386eea72011-09-21 18:43:04 -070099 return repos
Eric Li2eb800f2011-04-21 16:52:58 -0700100
101
Dan Shib669cbd2013-09-13 11:17:17 -0700102 def install(self, host=None, autodir=None, use_packaging=True):
Chris Masone8abb6fc2012-01-31 09:27:36 -0800103 """Install autotest. If |host| is not None, stores it in |self.host|.
104
105 @param host A Host instance on which autotest will be installed
106 @param autodir Location on the remote host to install to
Dan Shib669cbd2013-09-13 11:17:17 -0700107 @param use_packaging Enable install modes that use the packaging system.
108
Chris Masone8abb6fc2012-01-31 09:27:36 -0800109 """
110 if host:
111 self.host = host
112
Dan Shib669cbd2013-09-13 11:17:17 -0700113 super(SiteAutotest, self).install(host=host, autodir=autodir,
114 use_packaging=use_packaging)
Chris Masone8abb6fc2012-01-31 09:27:36 -0800115
116
Christopher Wiley809301c2013-07-02 18:00:31 -0700117 def _install(self, host=None, autodir=None, use_autoserv=True,
118 use_packaging=True):
119 """
120 Install autotest. If get() was not called previously, an
121 attempt will be made to install from the autotest svn
122 repository.
123
124 @param host A Host instance on which autotest will be installed
125 @param autodir Location on the remote host to install to
126 @param use_autoserv Enable install modes that depend on the client
127 running with the autoserv harness
128 @param use_packaging Enable install modes that use the packaging system
129
130 @exception AutoservError if a tarball was not specified and
131 the target host does not have svn installed in its path
132 """
133 # TODO(milleral): http://crbug.com/258161
134 super(SiteAutotest, self)._install(host, autodir, use_autoserv,
135 use_packaging)
136 # Send over the most recent global_config.ini after installation if one
137 # is available.
138 # This code is a bit duplicated from
139 # _BaseRun._create_client_config_file, but oh well.
140 if self.installed and self.source_material:
141 logging.info('Installing updated global_config.ini.')
142 destination = os.path.join(self.host.get_autodir(),
143 'global_config.ini')
144 with tempfile.NamedTemporaryFile() as client_config:
145 config = global_config.global_config
146 client_section = config.get_section_values('CLIENT')
147 client_section.write(client_config)
148 client_config.flush()
149 self.host.send_file(client_config.name, destination)
150
151
Chris Sosaf4d43ff2012-10-30 11:21:05 -0700152 def run_static_method(self, module, method, results_dir='.', host=None,
153 *args):
154 """Runs a non-instance method with |args| from |module| on the client.
155
156 This method runs a static/class/module autotest method on the client.
157 For example:
158 run_static_method("autotest_lib.client.cros.cros_ui", "reboot")
159
160 Will run autotest_lib.client.cros.cros_ui.reboot() on the client.
161
162 @param module: module name as you would refer to it when importing in a
163 control file. e.g. autotest_lib.client.common_lib.module_name.
164 @param method: the method you want to call.
165 @param results_dir: A str path where the results should be stored
166 on the local filesystem.
167 @param host: A Host instance on which the control file should
168 be run.
169 @param args: args to pass to the method.
170 """
171 control = "\n".join(["import %s" % module,
172 "%s.%s(%s)\n" % (module, method,
173 ','.join(map(repr, args)))])
174 self.run(control, results_dir=results_dir, host=host)
175
176
Chris Sosa3ee5d5c2012-02-23 11:18:41 -0800177class SiteClientLogger(object):
178 """Overrides default client logger to allow for using a local package cache.
179 """
180
181 def _process_line(self, line):
182 """Returns the package checksum file if it exists."""
183 logging.debug(line)
184 fetch_package_match = self.fetch_package_parser.search(line)
185 if fetch_package_match:
186 pkg_name, dest_path, fifo_path = fetch_package_match.groups()
Chris Masone44e4d6c2012-08-15 14:25:53 -0700187 serve_packages = _CONFIG.get_config_value(
Chris Sosa3ee5d5c2012-02-23 11:18:41 -0800188 "PACKAGES", "serve_packages_from_autoserv", type=bool)
189 if serve_packages and pkg_name == 'packages.checksum':
Chris Sosa3ee5d5c2012-02-23 11:18:41 -0800190 try:
191 checksum_file = os.path.join(
192 self.job.pkgmgr.pkgmgr_dir, 'packages', pkg_name)
193 if os.path.exists(checksum_file):
194 self.host.send_file(checksum_file, dest_path)
Chris Sosa3ee5d5c2012-02-23 11:18:41 -0800195 except error.AutoservRunError:
196 msg = "Package checksum file not found, continuing anyway"
197 logging.exception(msg)
198
Chris Sosa2cc4da02012-07-09 16:18:24 -0700199 try:
200 # When fetching a package, the client expects to be
201 # notified when the fetching is complete. Autotest
202 # does this pushing a B to a fifo queue to the client.
203 self.host.run("echo B > %s" % fifo_path)
204 except error.AutoservRunError:
205 msg = "Checksum installation failed, continuing anyway"
206 logging.exception(msg)
207 finally:
208 return
Chris Sosa3ee5d5c2012-02-23 11:18:41 -0800209
210 # Fall through to process the line using the default method.
211 super(SiteClientLogger, self)._process_line(line)
212
213
214 def _send_tarball(self, pkg_name, remote_dest):
215 """Uses tarballs in package manager by default."""
216 try:
217 server_package = os.path.join(self.job.pkgmgr.pkgmgr_dir,
218 'packages', pkg_name)
219 if os.path.exists(server_package):
220 self.host.send_file(server_package, remote_dest)
221 return
222
223 except error.AutoservRunError:
224 msg = ("Package %s could not be sent from the package cache." %
225 pkg_name)
226 logging.exception(msg)
227
228 # Fall through to send tarball the default method.
229 super(SiteClientLogger, self)._send_tarball(pkg_name, remote_dest)
230
231
Eric Lic7444bd2011-02-15 17:12:10 -0800232class _SiteRun(object):
233 pass