[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 |
| 9 | import platform |
| 10 | import signal |
| 11 | import subprocess |
| 12 | import sys |
msw | 76cf5fe1 | 2015-07-16 23:48:57 | [diff] [blame] | 13 | import threading |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 14 | |
| 15 | import test_env |
| 16 | |
| 17 | |
msw | 76cf5fe1 | 2015-07-16 23:48:57 | [diff] [blame] | 18 | def _kill(proc, send_signal): |
msw | 11ac9a2 | 2015-07-14 23:36:04 | [diff] [blame] | 19 | """Kills |proc| and ignores exceptions thrown for non-existent processes.""" |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 20 | try: |
msw | 76cf5fe1 | 2015-07-16 23:48:57 | [diff] [blame] | 21 | os.kill(proc.pid, send_signal) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 22 | except OSError: |
| 23 | pass |
| 24 | |
| 25 | |
msw | 76cf5fe1 | 2015-07-16 23:48:57 | [diff] [blame] | 26 | def kill(proc, timeout_in_seconds=10): |
| 27 | """Tries to kill |proc| gracefully with a timeout for each signal.""" |
| 28 | if not proc or not proc.pid: |
| 29 | return |
| 30 | |
| 31 | _kill(proc, signal.SIGTERM) |
| 32 | thread = threading.Thread(target=proc.wait) |
| 33 | thread.start() |
| 34 | |
| 35 | thread.join(timeout_in_seconds) |
| 36 | if thread.is_alive(): |
| 37 | print >> sys.stderr, 'Xvfb running after SIGTERM, trying SIGKILL.' |
| 38 | _kill(proc, signal.SIGKILL) |
| 39 | |
| 40 | thread.join(timeout_in_seconds) |
| 41 | if thread.is_alive(): |
| 42 | print >> sys.stderr, 'Xvfb running after SIGTERM and SIGKILL; good luck!' |
| 43 | |
| 44 | |
thomasanderson | 3d07428 | 2016-12-06 18:21:12 | [diff] [blame] | 45 | def run_executable(cmd, env): |
msw | 11ac9a2 | 2015-07-14 23:36:04 | [diff] [blame] | 46 | """Runs an executable within Xvfb on Linux or normally on other platforms. |
| 47 | |
| 48 | Returns the exit code of the specified commandline, or 1 on failure. |
| 49 | """ |
thomasanderson | 8c5f703 | 2016-11-28 22:59:16 | [diff] [blame] | 50 | if sys.platform == 'linux2': |
| 51 | if env.get('_CHROMIUM_INSIDE_XVFB') == '1': |
| 52 | openbox_proc = None |
| 53 | xcompmgr_proc = None |
| 54 | try: |
| 55 | # Some ChromeOS tests need a window manager. |
| 56 | openbox_proc = subprocess.Popen('openbox', stdout=subprocess.PIPE, |
| 57 | stderr=subprocess.STDOUT, env=env) |
| 58 | |
| 59 | # Some tests need a compositing wm to make use of transparent visuals. |
| 60 | xcompmgr_proc = subprocess.Popen('xcompmgr', stdout=subprocess.PIPE, |
| 61 | stderr=subprocess.STDOUT, env=env) |
| 62 | |
| 63 | return test_env.run_executable(cmd, env) |
| 64 | except OSError as e: |
| 65 | print >> sys.stderr, 'Failed to start Xvfb or Openbox: %s' % str(e) |
| 66 | return 1 |
| 67 | finally: |
| 68 | kill(openbox_proc) |
| 69 | kill(xcompmgr_proc) |
| 70 | else: |
| 71 | env['_CHROMIUM_INSIDE_XVFB'] = '1' |
jochen | 0426747e | 2017-02-09 17:01:19 | [diff] [blame^] | 72 | xvfb_script = __file__ |
| 73 | if xvfb_script.endswith('.pyc'): |
| 74 | xvfb_script = xvfb_script[:-1] |
thomasanderson | 8c5f703 | 2016-11-28 22:59:16 | [diff] [blame] | 75 | return subprocess.call(['xvfb-run', '-a', "--server-args=-screen 0 " |
| 76 | "1280x800x24 -ac -nolisten tcp -dpi 96", |
jochen | 0426747e | 2017-02-09 17:01:19 | [diff] [blame^] | 77 | xvfb_script] + cmd, env=env) |
thomasanderson | 8c5f703 | 2016-11-28 22:59:16 | [diff] [blame] | 78 | else: |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 79 | return test_env.run_executable(cmd, env) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 80 | |
| 81 | |
| 82 | def main(): |
thomasanderson | 3d07428 | 2016-12-06 18:21:12 | [diff] [blame] | 83 | if len(sys.argv) < 2: |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 84 | print >> sys.stderr, ( |
thomasanderson | 3d07428 | 2016-12-06 18:21:12 | [diff] [blame] | 85 | 'Usage: xvfb.py [command args...]') |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 86 | return 2 |
thomasanderson | 3d07428 | 2016-12-06 18:21:12 | [diff] [blame] | 87 | return run_executable(sys.argv[1:], os.environ.copy()) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 88 | |
| 89 | |
| 90 | if __name__ == "__main__": |
| 91 | sys.exit(main()) |