Home | History | Annotate | Download | only in cros
      1 # 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 
      5 import logging
      6 import os
      7 import shutil
      8 import tempfile
      9 
     10 import common
     11 from autotest_lib.client.bin import test, utils
     12 from autotest_lib.client.common_lib import error
     13 from autotest_lib.client.cros import constants
     14 from autotest_lib.client.cros.graphics import graphics_utils
     15 
     16 
     17 class ChromeBinaryTest(test.test):
     18     """
     19     Base class for tests to run chrome test binaries without signing in and
     20     running Chrome.
     21     """
     22 
     23     CHROME_TEST_DEP = 'chrome_test'
     24     CHROME_SANDBOX = '/opt/google/chrome/chrome-sandbox'
     25     COMPONENT_LIB = '/opt/google/chrome/lib'
     26     home_dir = None
     27     cr_source_dir = None
     28     test_binary_dir = None
     29 
     30     def setup(self):
     31         self.job.setup_dep([self.CHROME_TEST_DEP])
     32 
     33     def initialize(self):
     34         test_dep_dir = os.path.join(self.autodir, 'deps', self.CHROME_TEST_DEP)
     35         self.job.install_pkg(self.CHROME_TEST_DEP, 'dep', test_dep_dir)
     36 
     37         self.cr_source_dir = '%s/test_src' % test_dep_dir
     38         self.test_binary_dir = '%s/out/Release' % self.cr_source_dir
     39         # If chrome is a component build then need to create a symlink such
     40         # that the _unittest binaries can find the chrome component libraries.
     41         Release_lib = os.path.join(self.test_binary_dir, 'lib')
     42         if os.path.isdir(self.COMPONENT_LIB):
     43             logging.info('Detected component build. This assumes binary '
     44                          'compatibility between chrome and *unittest.')
     45             if not os.path.islink(Release_lib):
     46                 os.symlink(self.COMPONENT_LIB, Release_lib)
     47         self.home_dir = tempfile.mkdtemp()
     48 
     49     def cleanup(self):
     50         if self.home_dir:
     51             shutil.rmtree(self.home_dir, ignore_errors=True)
     52 
     53     def get_chrome_binary_path(self, binary_to_run):
     54         return os.path.join(self.test_binary_dir, binary_to_run)
     55 
     56     def run_chrome_test_binary(self,
     57                                binary_to_run,
     58                                extra_params='',
     59                                prefix='',
     60                                as_chronos=True):
     61         """
     62         Run chrome test binary.
     63 
     64         @param binary_to_run: The name of the browser test binary.
     65         @param extra_params: Arguments for the browser test binary.
     66         @param prefix: Prefix to the command that invokes the test binary.
     67         @param as_chronos: Boolean indicating if the tests should run in a
     68             chronos shell.
     69 
     70         @raises: error.TestFail if there is error running the command.
     71         """
     72         cmd = '%s/%s %s' % (self.test_binary_dir, binary_to_run, extra_params)
     73         env_vars = 'HOME=%s CR_SOURCE_ROOT=%s CHROME_DEVEL_SANDBOX=%s' % (
     74             self.home_dir, self.cr_source_dir, self.CHROME_SANDBOX)
     75         cmd = '%s %s' % (env_vars, prefix + cmd)
     76 
     77         try:
     78             if utils.is_freon():
     79                 if as_chronos:
     80                     utils.system('su %s -c \'%s\'' % ('chronos', cmd))
     81                 else:
     82                     utils.system(cmd)
     83             else:
     84                 if as_chronos:
     85                     graphics_utils.xsystem(cmd, user='chronos')
     86                 else:
     87                     graphics_utils.xsystem(cmd)
     88         except error.CmdError as e:
     89             raise error.TestFail('%s failed! %s' % (binary_to_run, e))
     90 
     91 
     92 def nuke_chrome(func):
     93     """Decorator to nuke the Chrome browser processes."""
     94 
     95     def wrapper(*args, **kargs):
     96         open(constants.DISABLE_BROWSER_RESTART_MAGIC_FILE, 'w').close()
     97         try:
     98             try:
     99                 utils.nuke_process_by_name(name=constants.BROWSER,
    100                                            with_prejudice=True)
    101             except error.AutoservPidAlreadyDeadError:
    102                 pass
    103             return func(*args, **kargs)
    104         finally:
    105             # Allow chrome to be restarted again later.
    106             os.unlink(constants.DISABLE_BROWSER_RESTART_MAGIC_FILE)
    107 
    108     return wrapper
    109