blob: b132efc1387ecee155c98b02096dbbf4461a627c [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
6"""Runs the test with xvfb on linux. Runs the test normally on other platforms.
7
8For simplicity in gyp targets, this script just runs the test normal on
9non-linux platforms.
10"""
11
12import os
13import platform
14import signal
15import subprocess
16import sys
17
18import test_env
19
20
21def kill(pid):
22 """Kills a process and traps exception if the process doesn't exist anymore.
23 """
24 # If the process doesn't exist, it raises an exception that we can ignore.
25 try:
26 os.kill(pid, signal.SIGKILL)
27 except OSError:
28 pass
29
30
31def get_xvfb_path(server_dir):
32 """Figures out which X server to use."""
33 xvfb_path = os.path.join(server_dir, 'Xvfb.' + platform.architecture()[0])
34 if not os.path.exists(xvfb_path):
35 xvfb_path = os.path.join(server_dir, 'Xvfb')
36 if not os.path.exists(xvfb_path):
37 print >> sys.stderr, (
38 'No Xvfb found in designated server path: %s' % server_dir)
39 raise Exception('No virtual server')
40 return xvfb_path
41
42
43def start_xvfb(xvfb_path, display):
44 """Starts a virtual X server that we run the tests in.
45
46 This makes it so we can run the tests even if we didn't start the tests from
47 an X session.
48
49 Args:
50 xvfb_path: Path to Xvfb.
51 """
52 proc = subprocess.Popen(
53 [xvfb_path, display, '-screen', '0', '1024x768x24', '-ac'],
54 stdout=subprocess.PIPE,
55 stderr=subprocess.STDOUT)
56 return proc.pid
57
58
59def wait_for_xvfb(xdisplaycheck, env):
60 """Waits for xvfb to be fully initialized by using xdisplaycheck."""
61 try:
62 subprocess.check_call(
63 [xdisplaycheck],
64 stdout=subprocess.PIPE,
65 stderr=subprocess.STDOUT,
66 env=env)
[email protected]40bcbbd12012-03-20 01:17:2467 except OSError:
68 print >> sys.stderr, 'Failed to load %s with cwd=%s' % (
69 xdisplaycheck, os.getcwd())
70 return False
[email protected]0a88a652012-03-09 00:34:4571 except subprocess.CalledProcessError:
[email protected]40bcbbd12012-03-20 01:17:2472 print >> sys.stderr, (
73 'Xvfb failed to load properly while trying to run %s' % xdisplaycheck)
[email protected]0a88a652012-03-09 00:34:4574 return False
75 return True
76
77
78def run_executable(cmd, build_dir, env):
79 """Runs an executable within a xvfb buffer on linux or normally on other
80 platforms.
81
82 Requires that both xvfb and icewm are installed on linux.
[email protected]5934ca72012-07-05 18:24:5283
84 Detects recursion with an environment variable and do not create a recursive X
85 buffer if present.
[email protected]0a88a652012-03-09 00:34:4586 """
[email protected]5934ca72012-07-05 18:24:5287 # First look if we are inside a display.
88 if env.get('_CHROMIUM_INSIDE_XVFB') == '1':
89 # No need to recurse.
90 return test_env.run_executable(cmd, env)
91
[email protected]0a88a652012-03-09 00:34:4592 pid = None
93 xvfb = 'Xvfb'
94 try:
95 if sys.platform == 'linux2':
96 # Defaults to X display 9.
97 display = ':9'
98 pid = start_xvfb(xvfb, display)
99 env['DISPLAY'] = display
100 if not wait_for_xvfb(os.path.join(build_dir, 'xdisplaycheck'), env):
101 return 3
[email protected]5934ca72012-07-05 18:24:52102 # Inhibit recursion.
103 env['_CHROMIUM_INSIDE_XVFB'] = '1'
[email protected]0a88a652012-03-09 00:34:45104 # Some ChromeOS tests need a window manager. Technically, it could be
105 # another script but that would be overkill.
106 subprocess.Popen(
107 'icewm', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
108 return test_env.run_executable(cmd, env)
109 finally:
110 if pid:
111 kill(pid)
112
113
114def main():
115 if len(sys.argv) < 3:
116 print >> sys.stderr, (
117 'Usage: xvfb.py [path to build_dir] [command args...]')
118 return 2
119 return run_executable(sys.argv[2:], sys.argv[1], os.environ.copy())
120
121
122if __name__ == "__main__":
123 sys.exit(main())