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