[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 | |
msw | 11ac9a2 | 2015-07-14 23:36:04 | [diff] [blame] | 45 | def run_executable(cmd, build_dir, env): |
| 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' |
| 72 | return subprocess.call(['xvfb-run', '-a', "--server-args=-screen 0 " |
| 73 | "1280x800x24 -ac -nolisten tcp -dpi 96", |
| 74 | __file__, build_dir] + cmd, env=env) |
| 75 | else: |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 76 | return test_env.run_executable(cmd, env) |
[email protected] | 0a88a65 | 2012-03-09 00:34:45 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def main(): |
| 80 | if len(sys.argv) < 3: |
| 81 | print >> sys.stderr, ( |
| 82 | 'Usage: xvfb.py [path to build_dir] [command args...]') |
| 83 | return 2 |
| 84 | return run_executable(sys.argv[2:], sys.argv[1], os.environ.copy()) |
| 85 | |
| 86 | |
| 87 | if __name__ == "__main__": |
| 88 | sys.exit(main()) |