blob: c70d8039b74cd482f380e253e1c660deb6bd7060 [file] [log] [blame]
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00001#!/usr/bin/env python
Marc-Antoine Ruel8add1242013-11-05 17:28:27 -05002# Copyright 2012 The Swarming Authors. All rights reserved.
Marc-Antoine Ruele98b1122013-11-05 20:27:57 -05003# Use of this source code is governed under the Apache License, Version 2.0 that
4# can be found in the LICENSE file.
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00005
maruel@chromium.org0cd0b182012-10-22 13:34:15 +00006"""Reads a .isolated, creates a tree of hardlinks and runs the test.
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +00007
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -05008To improve performance, it keeps a local cache. The local cache can safely be
9deleted.
10
11Any ${ISOLATED_OUTDIR} on the command line will be replaced by the location of a
12temporary directory upon execution of the command specified in the .isolated
13file. All content written to this directory will be uploaded upon termination
14and the .isolated file describing this directory will be printed to stdout.
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000015"""
16
Marc-Antoine Ruel185ded42015-01-28 20:49:18 -050017__version__ = '0.4.1'
maruel@chromium.orgdedbf492013-09-12 20:42:11 +000018
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000019import logging
20import optparse
21import os
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000022import sys
23import tempfile
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000024
vadimsh@chromium.orga4326472013-08-24 02:05:41 +000025from third_party.depot_tools import fix_encoding
26
Vadim Shtayura6b555c12014-07-23 16:22:18 -070027from utils import file_path
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -040028from utils import logging_utils
Marc-Antoine Ruelcfb60852014-07-02 15:22:00 -040029from utils import on_error
Marc-Antoine Ruelc44f5722015-01-08 16:10:01 -050030from utils import subprocess42
vadimsh@chromium.orga4326472013-08-24 02:05:41 +000031from utils import tools
vadimsh@chromium.org3e97deb2013-08-24 00:56:44 +000032from utils import zip_package
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000033
Vadim Shtayurae34e13a2014-02-02 11:23:26 -080034import auth
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -040035import isolated_format
maruel@chromium.orgdedbf492013-09-12 20:42:11 +000036import isolateserver
maruel@chromium.orgdedbf492013-09-12 20:42:11 +000037
vadimsh@chromium.orga4326472013-08-24 02:05:41 +000038
vadimsh@chromium.org85071062013-08-21 23:37:45 +000039# Absolute path to this file (can be None if running from zip on Mac).
40THIS_FILE_PATH = os.path.abspath(__file__) if __file__ else None
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000041
42# Directory that contains this file (might be inside zip package).
vadimsh@chromium.org85071062013-08-21 23:37:45 +000043BASE_DIR = os.path.dirname(THIS_FILE_PATH) if __file__ else None
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000044
45# Directory that contains currently running script file.
maruel@chromium.org814d23f2013-10-01 19:08:00 +000046if zip_package.get_main_script_path():
47 MAIN_DIR = os.path.dirname(
48 os.path.abspath(zip_package.get_main_script_path()))
49else:
50 # This happens when 'import run_isolated' is executed at the python
51 # interactive prompt, in that case __file__ is undefined.
52 MAIN_DIR = None
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000053
csharp@chromium.orgff2a4662012-11-21 20:49:32 +000054# The name of the log file to use.
55RUN_ISOLATED_LOG_FILE = 'run_isolated.log'
56
csharp@chromium.orge217f302012-11-22 16:51:53 +000057# The name of the log to use for the run_test_cases.py command
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000058RUN_TEST_CASES_LOG = 'run_test_cases.log'
csharp@chromium.orge217f302012-11-22 16:51:53 +000059
vadimsh@chromium.org87d63262013-04-04 19:34:21 +000060
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000061def get_as_zip_package(executable=True):
62 """Returns ZipPackage with this module and all its dependencies.
63
64 If |executable| is True will store run_isolated.py as __main__.py so that
65 zip package is directly executable be python.
66 """
67 # Building a zip package when running from another zip package is
68 # unsupported and probably unneeded.
69 assert not zip_package.is_zipped_module(sys.modules[__name__])
vadimsh@chromium.org85071062013-08-21 23:37:45 +000070 assert THIS_FILE_PATH
71 assert BASE_DIR
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000072 package = zip_package.ZipPackage(root=BASE_DIR)
73 package.add_python_file(THIS_FILE_PATH, '__main__.py' if executable else None)
Marc-Antoine Ruel8bee66d2014-08-28 19:02:07 -040074 package.add_python_file(os.path.join(BASE_DIR, 'isolated_format.py'))
maruel@chromium.orgdedbf492013-09-12 20:42:11 +000075 package.add_python_file(os.path.join(BASE_DIR, 'isolateserver.py'))
Vadim Shtayurae34e13a2014-02-02 11:23:26 -080076 package.add_python_file(os.path.join(BASE_DIR, 'auth.py'))
vadimsh@chromium.org8b9d56b2013-08-21 22:24:35 +000077 package.add_directory(os.path.join(BASE_DIR, 'third_party'))
78 package.add_directory(os.path.join(BASE_DIR, 'utils'))
79 return package
80
81
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000082def make_temp_dir(prefix, root_dir):
83 """Returns a temporary directory on the same file system as root_dir."""
84 base_temp_dir = None
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -040085 if (root_dir and
86 not file_path.is_same_filesystem(root_dir, tempfile.gettempdir())):
Paweł Hajdan, Jrf7d58722015-04-27 14:54:42 +020087 base_temp_dir = root_dir
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +000088 return tempfile.mkdtemp(prefix=prefix, dir=base_temp_dir)
89
90
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -050091def change_tree_read_only(rootdir, read_only):
92 """Changes the tree read-only bits according to the read_only specification.
93
94 The flag can be 0, 1 or 2, which will affect the possibility to modify files
95 and create or delete files.
96 """
97 if read_only == 2:
98 # Files and directories (except on Windows) are marked read only. This
99 # inhibits modifying, creating or deleting files in the test directory,
100 # except on Windows where creating and deleting files is still possible.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400101 file_path.make_tree_read_only(rootdir)
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500102 elif read_only == 1:
103 # Files are marked read only but not the directories. This inhibits
104 # modifying files but creating or deleting files is still possible.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400105 file_path.make_tree_files_read_only(rootdir)
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500106 elif read_only in (0, None):
Marc-Antoine Ruelf1d827c2014-11-24 15:22:25 -0500107 # Anything can be modified.
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500108 # TODO(maruel): This is currently dangerous as long as DiskCache.touch()
109 # is not yet changed to verify the hash of the content of the files it is
110 # looking at, so that if a test modifies an input file, the file must be
111 # deleted.
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400112 file_path.make_tree_writeable(rootdir)
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500113 else:
114 raise ValueError(
115 'change_tree_read_only(%s, %s): Unknown flag %s' %
116 (rootdir, read_only, read_only))
117
118
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500119def process_command(command, out_dir):
120 """Replaces isolated specific variables in a command line."""
Vadim Shtayura51aba362014-05-14 15:39:23 -0700121 filtered = []
122 for arg in command:
123 if '${ISOLATED_OUTDIR}' in arg:
124 arg = arg.replace('${ISOLATED_OUTDIR}', out_dir).replace('/', os.sep)
125 filtered.append(arg)
126 return filtered
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500127
128
Kenneth Russell61d42352014-09-15 11:41:16 -0700129def run_tha_test(isolated_hash, storage, cache, leak_temp_dir, extra_args):
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500130 """Downloads the dependencies in the cache, hardlinks them into a temporary
131 directory and runs the executable from there.
132
133 A temporary directory is created to hold the output files. The content inside
134 this directory will be uploaded back to |storage| packaged as a .isolated
135 file.
136
137 Arguments:
Marc-Antoine Ruel35b58432014-12-08 17:40:40 -0500138 isolated_hash: the SHA-1 of the .isolated file that must be retrieved to
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500139 recreate the tree of files to run the target executable.
140 storage: an isolateserver.Storage object to retrieve remote objects. This
141 object has a reference to an isolateserver.StorageApi, which does
142 the actual I/O.
143 cache: an isolateserver.LocalCache to keep from retrieving the same objects
144 constantly by caching the objects retrieved. Can be on-disk or
145 in-memory.
Kenneth Russell61d42352014-09-15 11:41:16 -0700146 leak_temp_dir: if true, the temporary directory will be deliberately leaked
147 for later examination.
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500148 extra_args: optional arguments to add to the command stated in the .isolate
149 file.
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000150 """
Marc-Antoine Ruel3c979cb2015-03-11 13:43:28 -0400151 run_dir = make_temp_dir(u'run_tha_test', cache.cache_dir)
152 out_dir = unicode(make_temp_dir(u'isolated_out', cache.cache_dir))
Marc-Antoine Ruel3a963792013-12-11 11:33:49 -0500153 result = 0
maruel@chromium.org781ccf62013-09-17 19:39:47 +0000154 try:
maruel@chromium.org4f2ebe42013-09-19 13:09:08 +0000155 try:
Vadim Shtayura7f7459c2014-09-04 13:25:10 -0700156 bundle = isolateserver.fetch_isolated(
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000157 isolated_hash=isolated_hash,
158 storage=storage,
159 cache=cache,
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500160 outdir=run_dir,
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000161 require_command=True)
Marc-Antoine Ruel1e7658c2014-08-28 19:46:39 -0400162 except isolated_format.IsolatedError:
Marc-Antoine Ruelcfb60852014-07-02 15:22:00 -0400163 on_error.report(None)
Vadim Shtayura51aba362014-05-14 15:39:23 -0700164 return 1
maruel@chromium.org781ccf62013-09-17 19:39:47 +0000165
Vadim Shtayura7f7459c2014-09-04 13:25:10 -0700166 change_tree_read_only(run_dir, bundle.read_only)
167 cwd = os.path.normpath(os.path.join(run_dir, bundle.relative_cwd))
168 command = bundle.command + extra_args
Vadim Shtayurae4a780b2014-01-17 13:18:53 -0800169
John Abd-El-Malek3f998682014-09-17 17:48:09 -0700170 file_path.ensure_command_has_abs_path(command, cwd)
Marc-Antoine Ruel2283ad12014-02-09 11:14:57 -0500171 command = process_command(command, out_dir)
Marc-Antoine Rueldef5b802014-01-08 20:57:12 -0500172 logging.info('Running %s, cwd=%s' % (command, cwd))
maruel@chromium.org781ccf62013-09-17 19:39:47 +0000173
174 # TODO(csharp): This should be specified somewhere else.
175 # TODO(vadimsh): Pass it via 'env_vars' in manifest.
176 # Add a rotating log file if one doesn't already exist.
177 env = os.environ.copy()
maruel@chromium.org814d23f2013-10-01 19:08:00 +0000178 if MAIN_DIR:
179 env.setdefault('RUN_TEST_CASES_LOG_FILE',
vadimsh@chromium.org7b5dae32013-10-03 16:59:59 +0000180 os.path.join(MAIN_DIR, RUN_TEST_CASES_LOG))
Marc-Antoine Ruelc44f5722015-01-08 16:10:01 -0500181 sys.stdout.flush()
182 with tools.Profiler('RunTest'):
183 try:
184 with subprocess42.Popen_with_handler(command, cwd=cwd, env=env) as p:
185 p.communicate()
186 result = p.returncode
187 except OSError:
188 on_error.report('Failed to run %s; cwd=%s' % (command, cwd))
189 result = 1
190 logging.info(
191 'Command finished with exit code %d (%s)',
192 result, hex(0xffffffff & result))
maruel@chromium.org781ccf62013-09-17 19:39:47 +0000193 finally:
Marc-Antoine Ruel7124e392014-01-09 11:49:21 -0500194 try:
Kenneth Russell61d42352014-09-15 11:41:16 -0700195 if leak_temp_dir:
196 logging.warning('Deliberately leaking %s for later examination',
197 run_dir)
198 else:
199 try:
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400200 if not file_path.rmtree(run_dir):
Kenneth Russell61d42352014-09-15 11:41:16 -0700201 print >> sys.stderr, (
202 'Failed to delete the temporary directory, forcibly failing\n'
203 'the task because of it. No zombie process can outlive a\n'
204 'successful task run and still be marked as successful.\n'
205 'Fix your stuff.')
206 result = result or 1
207 except OSError:
208 logging.warning('Leaking %s', run_dir)
209 result = 1
Vadim Shtayura51aba362014-05-14 15:39:23 -0700210
211 # HACK(vadimsh): On Windows rmtree(run_dir) call above has
212 # a synchronization effect: it finishes only when all task child processes
213 # terminate (since a running process locks *.exe file). Examine out_dir
214 # only after that call completes (since child processes may
215 # write to out_dir too and we need to wait for them to finish).
216
217 # Upload out_dir and generate a .isolated file out of this directory.
218 # It is only done if files were written in the directory.
Marc-Antoine Ruel7eb35c82015-01-16 20:30:45 -0500219 if os.path.isdir(out_dir) and os.listdir(out_dir):
Vadim Shtayura51aba362014-05-14 15:39:23 -0700220 with tools.Profiler('ArchiveOutput'):
Marc-Antoine Ruel933027e2015-05-04 14:20:18 -0400221 try:
222 results = isolateserver.archive_files_to_storage(
223 storage, [out_dir], None)
224 except isolateserver.Aborted:
225 # This happens when a signal SIGTERM was received while uploading
226 # data. There is 2 causes:
227 # - The task was too slow and was about to be killed anyway due to
228 # exceeding the hard timeout.
229 # - The amount of data uploaded back is very large and took too much
230 # time to archive.
231 #
232 # There's 3 options to handle this:
233 # - Ignore the upload failure as a silent failure. This can be
234 # detected client side by the fact no result file exists.
235 # - Return as if the task failed. This is not factually correct.
236 # - Return an internal failure. Sadly, it's impossible at this level
237 # at the moment.
238 #
239 # For now, silently drop the upload.
240 #
241 # In any case, the process only has a very short grace period so it
242 # needs to exit right away.
243 sys.stderr.write('Received SIGTERM while uploading')
244 results = None
245
246 if results:
247 # TODO(maruel): Implement side-channel to publish this information.
248 output_data = {
249 'hash': results[0][0],
250 'namespace': storage.namespace,
251 'storage': storage.location,
252 }
253 sys.stdout.flush()
254 print(
255 '[run_isolated_out_hack]%s[/run_isolated_out_hack]' %
256 tools.format_json(output_data, dense=True))
Vadim Shtayura51aba362014-05-14 15:39:23 -0700257
258 finally:
Marc-Antoine Ruelaf9d8372014-07-21 19:50:57 -0400259 try:
Marc-Antoine Ruele4ad07e2014-10-15 20:22:29 -0400260 if os.path.isdir(out_dir) and not file_path.rmtree(out_dir):
Marc-Antoine Ruelaf9d8372014-07-21 19:50:57 -0400261 result = result or 1
262 except OSError:
Marc-Antoine Ruelfc059e22015-02-26 14:02:38 -0500263 # The error was already printed out. Report it but that's it. Only
264 # report on non-Windows or on Windows when the process had succeeded.
265 # Due to the way file sharing works on Windows, it's sadly expected that
266 # file deletion may fail when a test failed.
267 if sys.platform != 'win32' or not result:
268 on_error.report(None)
Marc-Antoine Ruelaf9d8372014-07-21 19:50:57 -0400269 result = 1
Vadim Shtayura51aba362014-05-14 15:39:23 -0700270
Marc-Antoine Ruel3a963792013-12-11 11:33:49 -0500271 return result
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000272
273
Marc-Antoine Ruel90c98162013-12-18 15:11:57 -0500274def main(args):
vadimsh@chromium.orga4326472013-08-24 02:05:41 +0000275 tools.disable_buffering()
Marc-Antoine Ruelf74cffe2015-07-15 15:21:34 -0400276 parser = logging_utils.OptionParserWithLogging(
maruel@chromium.orgdedbf492013-09-12 20:42:11 +0000277 usage='%prog <options>',
278 version=__version__,
279 log_file=RUN_ISOLATED_LOG_FILE)
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000280
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -0500281 data_group = optparse.OptionGroup(parser, 'Data source')
282 data_group.add_option(
Marc-Antoine Ruel185ded42015-01-28 20:49:18 -0500283 '-s', '--isolated',
284 help='Hash of the .isolated to grab from the isolate server')
Marc-Antoine Ruelc698ea22015-01-30 14:03:26 -0800285 data_group.add_option(
286 '-H', dest='isolated', help=optparse.SUPPRESS_HELP)
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -0500287 isolateserver.add_isolate_server_options(data_group)
Marc-Antoine Ruel1687b5e2014-02-06 17:47:53 -0500288 parser.add_option_group(data_group)
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000289
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -0400290 isolateserver.add_cache_options(parser)
291 parser.set_defaults(cache='cache')
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000292
Kenneth Russell61d42352014-09-15 11:41:16 -0700293 debug_group = optparse.OptionGroup(parser, 'Debugging')
294 debug_group.add_option(
295 '--leak-temp-dir',
296 action='store_true',
297 help='Deliberately leak isolate\'s temp dir for later examination '
298 '[default: %default]')
299 parser.add_option_group(debug_group)
300
Vadim Shtayurae34e13a2014-02-02 11:23:26 -0800301 auth.add_auth_options(parser)
Marc-Antoine Ruel90c98162013-12-18 15:11:57 -0500302 options, args = parser.parse_args(args)
Marc-Antoine Ruel185ded42015-01-28 20:49:18 -0500303 if not options.isolated:
304 parser.error('--isolated is required.')
Vadim Shtayura5d1efce2014-02-04 10:55:43 -0800305 auth.process_auth_options(parser, options)
Marc-Antoine Ruele290ada2014-12-10 19:48:49 -0500306 isolateserver.process_isolate_server_options(parser, options, True)
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000307
Marc-Antoine Ruela57d7db2014-10-15 20:31:19 -0400308 cache = isolateserver.process_cache_options(options)
Marc-Antoine Ruelf7d737d2014-12-10 15:36:29 -0500309 with isolateserver.get_storage(
310 options.isolate_server, options.namespace) as storage:
Marc-Antoine Ruelcfb60852014-07-02 15:22:00 -0400311 # Hashing schemes used by |storage| and |cache| MUST match.
312 assert storage.hash_algo == cache.hash_algo
313 return run_tha_test(
Marc-Antoine Ruel185ded42015-01-28 20:49:18 -0500314 options.isolated, storage, cache, options.leak_temp_dir, args)
maruel@chromium.org9c72d4e2012-09-28 19:20:25 +0000315
316
317if __name__ == '__main__':
csharp@chromium.orgbfb98742013-03-26 20:28:36 +0000318 # Ensure that we are always running with the correct encoding.
vadimsh@chromium.orga4326472013-08-24 02:05:41 +0000319 fix_encoding.fix_encoding()
Marc-Antoine Ruel90c98162013-12-18 15:11:57 -0500320 sys.exit(main(sys.argv[1:]))