Bertrand SIMONNET | e025a0e | 2015-05-06 14:17:43 -0700 | [diff] [blame] | 1 | # Copyright 2015 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 | """Tool to run ebuild unittests.""" |
| 6 | |
| 7 | from __future__ import print_function |
| 8 | |
Aviv Keshet | 01fcca4 | 2016-07-25 16:34:59 -0700 | [diff] [blame^] | 9 | import multiprocessing |
Bertrand SIMONNET | e025a0e | 2015-05-06 14:17:43 -0700 | [diff] [blame] | 10 | import os |
| 11 | |
| 12 | from chromite.lib import commandline |
| 13 | from chromite.lib import chroot_util |
| 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.lib import cros_logging as logging |
| 16 | from chromite.lib import osutils |
| 17 | from chromite.lib import workon_helper |
| 18 | from chromite.lib import portage_util |
| 19 | |
| 20 | |
| 21 | def ParseArgs(argv): |
| 22 | """Parse arguments. |
| 23 | |
| 24 | Args: |
| 25 | argv: array of arguments passed to the script. |
| 26 | """ |
| 27 | parser = commandline.ArgumentParser(description=__doc__) |
| 28 | |
| 29 | target = parser.add_mutually_exclusive_group(required=True) |
| 30 | target.add_argument('--sysroot', type='path', help='Path to the sysroot.') |
| 31 | target.add_argument('--board', help='Board name.') |
| 32 | |
| 33 | parser.add_argument('--pretend', default=False, action='store_true', |
| 34 | help='Show the list of packages to be tested and return.') |
| 35 | parser.add_argument('--noinstalled_only', dest='installed', default=True, |
| 36 | action='store_false', |
| 37 | help='Test all testable packages, even if they are not ' |
| 38 | 'currently installed.') |
| 39 | parser.add_argument('--package_file', type='path', |
| 40 | help='Path to a file containing the list of packages ' |
| 41 | 'that should be tested.') |
| 42 | parser.add_argument('--blacklist_packages', dest='package_blacklist', |
| 43 | help='Space-separated list of blacklisted packages.') |
| 44 | parser.add_argument('--packages', |
| 45 | help='Space-separated list of packages to test.') |
| 46 | parser.add_argument('--nowithdebug', action='store_true', |
| 47 | help="Don't build the tests with USE=cros-debug") |
| 48 | |
| 49 | options = parser.parse_args(argv) |
| 50 | options.Freeze() |
| 51 | return options |
| 52 | |
| 53 | |
| 54 | def main(argv): |
| 55 | opts = ParseArgs(argv) |
| 56 | |
| 57 | cros_build_lib.AssertInsideChroot() |
| 58 | |
| 59 | sysroot = opts.sysroot or cros_build_lib.GetSysroot(opts.board) |
| 60 | package_blacklist = portage_util.UNITTEST_PACKAGE_BLACKLIST |
| 61 | if opts.package_blacklist: |
| 62 | package_blacklist |= set(opts.package_blacklist.split()) |
| 63 | |
| 64 | packages = set() |
| 65 | # The list of packages to test can be passed as a file containing a |
| 66 | # space-separated list of package names. |
| 67 | # This is used by the builder to test only the packages that were upreved. |
| 68 | if opts.package_file and os.path.exists(opts.package_file): |
| 69 | packages = set(osutils.ReadFile(opts.package_file).split()) |
| 70 | |
| 71 | if opts.packages: |
| 72 | packages |= set(opts.packages.split()) |
| 73 | |
| 74 | # If no packages were specified, use all testable packages. |
| 75 | if not (opts.packages or opts.package_file): |
| 76 | workon = workon_helper.WorkonHelper(sysroot) |
| 77 | packages = (workon.InstalledWorkonAtoms() if opts.installed |
| 78 | else workon.ListAtoms(use_all=True)) |
| 79 | |
| 80 | for cp in packages & package_blacklist: |
| 81 | logging.info('Skipping blacklisted package %s.', cp) |
| 82 | |
| 83 | packages = packages - package_blacklist |
| 84 | pkg_with_test = portage_util.PackagesWithTest(sysroot, packages) |
| 85 | |
| 86 | if packages - pkg_with_test: |
| 87 | logging.warning('The following packages do not have tests:') |
| 88 | logging.warning('\n'.join(sorted(packages - pkg_with_test))) |
| 89 | |
| 90 | if opts.pretend: |
| 91 | print('\n'.join(sorted(pkg_with_test))) |
| 92 | return |
| 93 | |
| 94 | env = None |
| 95 | if opts.nowithdebug: |
| 96 | use_flags = os.environ.get('USE', '') |
| 97 | use_flags += ' -cros-debug' |
| 98 | env = {'USE': use_flags} |
| 99 | |
| 100 | try: |
Aviv Keshet | 01fcca4 | 2016-07-25 16:34:59 -0700 | [diff] [blame^] | 101 | chroot_util.RunUnittests(sysroot, pkg_with_test, extra_env=env, |
| 102 | jobs=min(10, multiprocessing.cpu_count())) |
Bertrand SIMONNET | e025a0e | 2015-05-06 14:17:43 -0700 | [diff] [blame] | 103 | except cros_build_lib.RunCommandError: |
| 104 | logging.error('Unittests failed.') |
| 105 | raise |