blob: 640501dc4d8a685e0663a7c2413f1719bdf1f4ac [file] [log] [blame]
[email protected]0a88a652012-03-09 00:34:451#!/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
msw11ac9a22015-07-14 23:36:046"""Runs tests with Xvfb and Openbox on Linux and normally on other platforms."""
[email protected]0a88a652012-03-09 00:34:457
8import os
9import platform
10import signal
11import subprocess
12import sys
msw76cf5fe12015-07-16 23:48:5713import threading
[email protected]0a88a652012-03-09 00:34:4514
15import test_env
16
17
msw76cf5fe12015-07-16 23:48:5718def _kill(proc, send_signal):
msw11ac9a22015-07-14 23:36:0419 """Kills |proc| and ignores exceptions thrown for non-existent processes."""
[email protected]0a88a652012-03-09 00:34:4520 try:
msw76cf5fe12015-07-16 23:48:5721 os.kill(proc.pid, send_signal)
[email protected]0a88a652012-03-09 00:34:4522 except OSError:
23 pass
24
25
msw76cf5fe12015-07-16 23:48:5726def 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
msw11ac9a22015-07-14 23:36:0445def 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 """
thomasanderson8c5f7032016-11-28 22:59:1650 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]0a88a652012-03-09 00:34:4576 return test_env.run_executable(cmd, env)
[email protected]0a88a652012-03-09 00:34:4577
78
79def 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
87if __name__ == "__main__":
88 sys.exit(main())