[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
msw | 11ac9a2 | 2015-07-14 23:36:04 | [diff] [blame] | 6 | """Runs tests with Xvfb and Openbox on Linux and normally on other platforms.""" |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 7 | |
| 8 | import os |
carlosk | cac17251a | 2017-03-15 02:21:07 | [diff] [blame] | 9 | import os.path |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 10 | import platform |
| 11 | import signal |
| 12 | import subprocess |
| 13 | import sys |
msw | 76cf5fe1 | 2015-07-16 23:48:57 | [diff] [blame] | 14 | import threading |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 15 | |
| 16 | import test_env |
| 17 | |
| 18 | |
msw | 76cf5fe1 | 2015-07-16 23:48:57 | [diff] [blame] | 19 | def _kill(proc, send_signal): |
msw | 11ac9a2 | 2015-07-14 23:36:04 | [diff] [blame] | 20 | """Kills |proc| and ignores exceptions thrown for non-existent processes.""" |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 21 | try: |
msw | 76cf5fe1 | 2015-07-16 23:48:57 | [diff] [blame] | 22 | os.kill(proc.pid, send_signal) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 23 | except OSError: |
| 24 | pass |
| 25 | |
| 26 | |
msw | 76cf5fe1 | 2015-07-16 23:48:57 | [diff] [blame] | 27 | def kill(proc, timeout_in_seconds=10): |
| 28 | """Tries to kill |proc| gracefully with a timeout for each signal.""" |
| 29 | if not proc or not proc.pid: |
| 30 | return |
| 31 | |
| 32 | _kill(proc, signal.SIGTERM) |
| 33 | thread = threading.Thread(target=proc.wait) |
| 34 | thread.start() |
| 35 | |
| 36 | thread.join(timeout_in_seconds) |
| 37 | if thread.is_alive(): |
| 38 | print >> sys.stderr, 'Xvfb running after SIGTERM, trying SIGKILL.' |
| 39 | _kill(proc, signal.SIGKILL) |
| 40 | |
| 41 | thread.join(timeout_in_seconds) |
| 42 | if thread.is_alive(): |
| 43 | print >> sys.stderr, 'Xvfb running after SIGTERM and SIGKILL; good luck!' |
| 44 | |
| 45 | |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 46 | def run_executable(cmd, env, stdoutfile=None): |
msw | 11ac9a2 | 2015-07-14 23:36:04 | [diff] [blame] | 47 | """Runs an executable within Xvfb on Linux or normally on other platforms. |
| 48 | |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 49 | If |stdoutfile| is provided, symbolization via script is disabled and stdout |
| 50 | is written to this file as well as to stdout. |
| 51 | |
msw | 11ac9a2 | 2015-07-14 23:36:04 | [diff] [blame] | 52 | Returns the exit code of the specified commandline, or 1 on failure. |
| 53 | """ |
dpranke | 7dad468 | 2017-04-26 23:14:55 | [diff] [blame] | 54 | |
| 55 | # It might seem counterintuitive to support a --no-xvfb flag in a script |
| 56 | # whose only job is to start xvfb, but doing so allows us to consolidate |
| 57 | # the logic in the layers of buildbot scripts so that we *always* use |
| 58 | # xvfb by default and don't have to worry about the distinction, it |
| 59 | # can remain solely under the control of the test invocation itself. |
| 60 | use_xvfb = True |
| 61 | if '--no-xvfb' in cmd: |
| 62 | use_xvfb = False |
| 63 | cmd.remove('--no-xvfb') |
| 64 | |
| 65 | if sys.platform == 'linux2' and use_xvfb: |
thomasanderson | 8c5f703 | 2016-11-28 22:59:16 | [diff] [blame] | 66 | if env.get('_CHROMIUM_INSIDE_XVFB') == '1': |
| 67 | openbox_proc = None |
| 68 | xcompmgr_proc = None |
| 69 | try: |
| 70 | # Some ChromeOS tests need a window manager. |
| 71 | openbox_proc = subprocess.Popen('openbox', stdout=subprocess.PIPE, |
| 72 | stderr=subprocess.STDOUT, env=env) |
| 73 | |
| 74 | # Some tests need a compositing wm to make use of transparent visuals. |
| 75 | xcompmgr_proc = subprocess.Popen('xcompmgr', stdout=subprocess.PIPE, |
| 76 | stderr=subprocess.STDOUT, env=env) |
| 77 | |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 78 | return test_env.run_executable(cmd, env, stdoutfile) |
thomasanderson | 8c5f703 | 2016-11-28 22:59:16 | [diff] [blame] | 79 | except OSError as e: |
| 80 | print >> sys.stderr, 'Failed to start Xvfb or Openbox: %s' % str(e) |
| 81 | return 1 |
| 82 | finally: |
| 83 | kill(openbox_proc) |
| 84 | kill(xcompmgr_proc) |
| 85 | else: |
| 86 | env['_CHROMIUM_INSIDE_XVFB'] = '1' |
jochen | 0426747e | 2017-02-09 17:01:19 | [diff] [blame] | 87 | xvfb_script = __file__ |
| 88 | if xvfb_script.endswith('.pyc'): |
| 89 | xvfb_script = xvfb_script[:-1] |
thomasanderson | 8c5f703 | 2016-11-28 22:59:16 | [diff] [blame] | 90 | return subprocess.call(['xvfb-run', '-a', "--server-args=-screen 0 " |
Tom Anderson | d08c1d04 | 2017-12-01 21:40:01 | [diff] [blame] | 91 | "1280x800x24 -ac -nolisten tcp -dpi 96 " |
| 92 | "+extension RANDR", |
jochen | 0426747e | 2017-02-09 17:01:19 | [diff] [blame] | 93 | xvfb_script] + cmd, env=env) |
thomasanderson | 8c5f703 | 2016-11-28 22:59:16 | [diff] [blame] | 94 | else: |
Trent Apted | b185243 | 2018-01-25 02:15:10 | [diff] [blame] | 95 | return test_env.run_executable(cmd, env, stdoutfile) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 96 | |
| 97 | |
| 98 | def main(): |
dpranke | 7dad468 | 2017-04-26 23:14:55 | [diff] [blame] | 99 | USAGE = 'Usage: xvfb.py [command [--no-xvfb] args...]' |
thomasanderson | 3d07428 | 2016-12-06 18:21:12 | [diff] [blame] | 100 | if len(sys.argv) < 2: |
carlosk | cac17251a | 2017-03-15 02:21:07 | [diff] [blame] | 101 | print >> sys.stderr, USAGE |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 102 | return 2 |
carlosk | cac17251a | 2017-03-15 02:21:07 | [diff] [blame] | 103 | |
| 104 | # If the user still thinks the first argument is the execution directory then |
| 105 | # print a friendly error message and quit. |
| 106 | if os.path.isdir(sys.argv[1]): |
| 107 | print >> sys.stderr, ( |
| 108 | 'Invalid command: \"%s\" is a directory' % sys.argv[1]) |
| 109 | print >> sys.stderr, USAGE |
| 110 | return 3 |
| 111 | |
thomasanderson | 3d07428 | 2016-12-06 18:21:12 | [diff] [blame] | 112 | return run_executable(sys.argv[1:], os.environ.copy()) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 113 | |
| 114 | |
| 115 | if __name__ == "__main__": |
| 116 | sys.exit(main()) |