blob: ac49f128a93715c244e5a611e6f43ce448a2a5fc [file] [log] [blame]
Yuli Huang678435f2014-02-28 01:03:37 +08001# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os, shutil, tempfile
6
Owen Lin8293df32014-05-30 15:14:15 +08007from autotest_lib.client.bin import test, utils
Yuli Huang678435f2014-02-28 01:03:37 +08008from autotest_lib.client.common_lib import error
Owen Lin8293df32014-05-30 15:14:15 +08009from autotest_lib.client.cros import constants, cros_ui
Yuli Huang678435f2014-02-28 01:03:37 +080010
11
12class ChromeBinaryTest(test.test):
13 """
14 Base class for tests to run chrome test binaries without signing in and
15 running Chrome.
16 """
17
18 CHROME_TEST_DEP = 'chrome_test'
19 CHROME_SANDBOX = '/opt/google/chrome/chrome-sandbox'
20 home_dir = None
21
22 def setup(self):
23 self.job.setup_dep([self.CHROME_TEST_DEP])
24
25
26 def initialize(self):
27 test_dep_dir = os.path.join(self.autodir, 'deps',
28 self.CHROME_TEST_DEP)
29 self.job.install_pkg(self.CHROME_TEST_DEP, 'dep', test_dep_dir)
30
31 self.cr_source_dir = '%s/test_src' % test_dep_dir
32 self.test_binary_dir = '%s/out/Release' % self.cr_source_dir
33 self.home_dir = tempfile.mkdtemp()
34
35
36 def cleanup(self):
37 if self.home_dir:
38 shutil.rmtree(self.home_dir, ignore_errors=True)
39
40
Ilja Friedelef925372014-07-09 16:47:01 -070041 def get_chrome_binary_path(self, binary_to_run):
42 return os.path.join(self.test_binary_dir, binary_to_run)
43
44
Yuli Huang678435f2014-02-28 01:03:37 +080045 def run_chrome_test_binary(self, binary_to_run, extra_params='', prefix='',
46 as_chronos=True):
47 """
48 Run chrome test binary.
49
50 @param binary_to_run: The name of the browser test binary.
51 @param extra_params: Arguments for the browser test binary.
52 @param prefix: Prefix to the command that invokes the test binary.
53 @param as_chronos: Boolean indicating if the tests should run in a
54 chronos shell.
55
56 @raises: error.TestFail if there is error running the command.
57 """
Ilja Friedelef925372014-07-09 16:47:01 -070058 binary = self.get_chrome_binary_path(binary_to_run)
Yuli Huang678435f2014-02-28 01:03:37 +080059 cmd = '%s/%s %s' % (self.test_binary_dir, binary_to_run, extra_params)
60 env_vars = 'HOME=%s CR_SOURCE_ROOT=%s CHROME_DEVEL_SANDBOX=%s' % (
61 self.home_dir, self.cr_source_dir, self.CHROME_SANDBOX)
62 cmd = '%s %s' % (env_vars, prefix + cmd)
63
64 try:
65 if as_chronos:
66 cros_ui.xsystem_as(cmd, user='chronos')
67 else:
68 cros_ui.xsystem(cmd)
69 except error.CmdError as e:
70 raise error.TestFail('%s failed! %s' % (binary_to_run, e))
Owen Lin8293df32014-05-30 15:14:15 +080071
72
73def nuke_chrome(func):
74 """Decorator to nuke the Chrome browser processes."""
75
76 def wrapper(*args, **kargs):
77 open(constants.DISABLE_BROWSER_RESTART_MAGIC_FILE, 'w').close()
78 try:
79 try:
80 utils.nuke_process_by_name(
81 name=constants.BROWSER, with_prejudice=True)
82 except error.AutoservPidAlreadyDeadError:
83 pass
84 return func(*args, **kargs)
85 finally:
86 # Allow chrome to be restarted again later.
87 os.unlink(constants.DISABLE_BROWSER_RESTART_MAGIC_FILE)
88 return wrapper