Home | History | Annotate | Download | only in install_test
      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 
      6 """Runs install and update tests.
      7 
      8 Install tests are performed using a single Chrome build, whereas two or more
      9 builds are needed for Update tests. There are separate command arguments for
     10 the builds that will be used for each of the tests. If a test file contains
     11 both types of tests(install and update), both arguments should be specified.
     12 Otherwise, specify only the command argument that is required for the test.
     13 To run a test with this script, append the module name to the _TEST_MODULES
     14 list. Modules added to the list must be in the same directory or in a sub-
     15 directory that's in the same location as this script.
     16 
     17 Example:
     18     $ python run_install_tests.py --url=<chrome_builds_url> --filter=* \
     19       --install-build=24.0.1290.0 --update-builds=24.0.1289.0,24.0.1290.0
     20 """
     21 
     22 import logging
     23 import optparse
     24 import os
     25 import re
     26 import sys
     27 import unittest
     28 
     29 import chrome_installer_win
     30 from install_test import InstallTest
     31 
     32 _DIRECTORY = os.path.dirname(os.path.abspath(__file__))
     33 sys.path.append(os.path.join(_DIRECTORY, os.path.pardir, os.path.pardir,
     34                              os.path.pardir, 'build', 'util', 'lib'))
     35 
     36 from common import unittest_util
     37 from common import util
     38 
     39 # To run tests from a module, append the module name to this list.
     40 _TEST_MODULES = ['sample_updater', 'theme_updater']
     41 
     42 for module in _TEST_MODULES:
     43   __import__(module)
     44 
     45 
     46 class Main(object):
     47   """Main program for running 'Fresh Install' and 'Updater' tests."""
     48 
     49   def __init__(self):
     50     self._SetLoggingConfiguration()
     51     self._ParseArgs()
     52     self._Run()
     53 
     54   def _ParseArgs(self):
     55     """Parses command line arguments."""
     56     parser = optparse.OptionParser()
     57     parser.add_option(
     58         '-u', '--url', type='string', default='', dest='url',
     59         help='Specifies the build url, without the build number.')
     60     parser.add_option(
     61         '-o', '--options', type='string', default='',
     62         help='Specifies any additional Chrome options (i.e. --system-level).')
     63     parser.add_option(
     64         '--install-build', type='string', default='', dest='install_build',
     65         help='Specifies the build to be used for fresh install testing.')
     66     parser.add_option(
     67         '--update-builds', type='string', default='', dest='update_builds',
     68         help='Specifies the builds to be used for updater testing.')
     69     parser.add_option(
     70         '--install-type', type='string', default='user', dest='install_type',
     71         help='Type of installation (i.e., user, system, or both)')
     72     parser.add_option(
     73         '-f', '--filter', type='string', default='*', dest='filter',
     74         help='Filter that specifies the test or testsuite to run.')
     75     self._opts, self._args = parser.parse_args()
     76     self._ValidateArgs()
     77     if self._opts.install_type == 'system':
     78       InstallTest.SetInstallType(chrome_installer_win.InstallationType.SYSTEM)
     79     update_builds = (self._opts.update_builds.split(',') if
     80                      self._opts.update_builds else [])
     81     options = self._opts.options.split(',') if self._opts.options else []
     82     InstallTest.InitTestFixture(self._opts.install_build, update_builds,
     83                                 self._opts.url, options)
     84 
     85   def _ValidateArgs(self):
     86     """Verifies the sanity of the command arguments.
     87 
     88     Confirms that all specified builds have a valid version number, and the
     89     build urls are valid.
     90     """
     91     builds = []
     92     if self._opts.install_build:
     93       builds.append(self._opts.install_build)
     94     if self._opts.update_builds:
     95       builds.extend(self._opts.update_builds.split(','))
     96     builds = list(frozenset(builds))
     97     for build in builds:
     98       if not re.match('\d+\.\d+\.\d+\.\d+', build):
     99         raise RuntimeError('Invalid build number: %s' % build)
    100       if not util.DoesUrlExist('%s/%s/' % (self._opts.url, build)):
    101         raise RuntimeError('Could not locate build no. %s' % build)
    102 
    103   def _SetLoggingConfiguration(self):
    104     """Sets the basic logging configuration."""
    105     log_format = '%(asctime)s %(levelname)-8s %(message)s'
    106     logging.basicConfig(level=logging.INFO, format=log_format)
    107 
    108   def _Run(self):
    109     """Runs the unit tests."""
    110     all_tests = unittest.defaultTestLoader.loadTestsFromNames(_TEST_MODULES)
    111     tests = unittest_util.FilterTestSuite(all_tests, self._opts.filter)
    112     result = unittest_util.TextTestRunner(verbosity=1).run(tests)
    113     # Run tests again if installation type is 'both'(i.e., user and system).
    114     if self._opts.install_type == 'both':
    115       # Load the tests again so test parameters can be reinitialized.
    116       all_tests = unittest.defaultTestLoader.loadTestsFromNames(_TEST_MODULES)
    117       tests = unittest_util.FilterTestSuite(all_tests, self._opts.filter)
    118       InstallTest.SetInstallType(chrome_installer_win.InstallationType.SYSTEM)
    119       result = unittest_util.TextTestRunner(verbosity=1).run(tests)
    120     del(tests)
    121     if not result.wasSuccessful():
    122       print >>sys.stderr, ('Not all tests were successful.')
    123       sys.exit(1)
    124     sys.exit(0)
    125 
    126 
    127 if __name__ == '__main__':
    128   Main()
    129