blob: 4bc0f6590d14bf1868fca5c5600268aca8ff556a [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
Chris McDonalde69db662018-11-15 12:50:18 -070014from chromite.lib import constants
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070015from chromite.lib import chroot_util
16from chromite.lib import cros_build_lib
17from chromite.lib import cros_logging as logging
18from chromite.lib import osutils
19from chromite.lib import workon_helper
20from chromite.lib import portage_util
Chris McDonalde69db662018-11-15 12:50:18 -070021from chromite.scripts import cros_extract_deps
22
23BOARD_VIRTUAL_PACKAGES = (constants.TARGET_OS_PKG,
24 constants.TARGET_OS_DEV_PKG,
25 constants.TARGET_OS_TEST_PKG)
26IMPLICIT_TEST_DEPS = ('virtual/implicit-system',)
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070027
28
29def ParseArgs(argv):
30 """Parse arguments.
31
32 Args:
33 argv: array of arguments passed to the script.
34 """
35 parser = commandline.ArgumentParser(description=__doc__)
36
37 target = parser.add_mutually_exclusive_group(required=True)
38 target.add_argument('--sysroot', type='path', help='Path to the sysroot.')
39 target.add_argument('--board', help='Board name.')
40
41 parser.add_argument('--pretend', default=False, action='store_true',
42 help='Show the list of packages to be tested and return.')
43 parser.add_argument('--noinstalled_only', dest='installed', default=True,
44 action='store_false',
45 help='Test all testable packages, even if they are not '
46 'currently installed.')
47 parser.add_argument('--package_file', type='path',
48 help='Path to a file containing the list of packages '
49 'that should be tested.')
50 parser.add_argument('--blacklist_packages', dest='package_blacklist',
51 help='Space-separated list of blacklisted packages.')
52 parser.add_argument('--packages',
53 help='Space-separated list of packages to test.')
54 parser.add_argument('--nowithdebug', action='store_true',
55 help="Don't build the tests with USE=cros-debug")
Chris McDonalde69db662018-11-15 12:50:18 -070056 parser.add_argument('--assume-empty-sysroot', default=False,
57 action='store_true', dest='empty_sysroot',
58 help='Set up dependencies and run unit tests for all '
59 'packages that could be installed on target board '
60 'without assuming that any packages have actually '
61 'been merged yet.')
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070062
63 options = parser.parse_args(argv)
64 options.Freeze()
65 return options
66
67
Chris McDonalde69db662018-11-15 12:50:18 -070068def determine_board_packages(sysroot, virtual_packages):
69 """Returns a set of the dependencies for the given packages"""
70 deps = cros_extract_deps.ExtractDeps(sysroot, virtual_packages)
71 return set('%s/%s' % (atom['category'], atom['name'])
72 for atom in deps.values())
73
74
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070075def main(argv):
76 opts = ParseArgs(argv)
77
78 cros_build_lib.AssertInsideChroot()
79
80 sysroot = opts.sysroot or cros_build_lib.GetSysroot(opts.board)
81 package_blacklist = portage_util.UNITTEST_PACKAGE_BLACKLIST
82 if opts.package_blacklist:
83 package_blacklist |= set(opts.package_blacklist.split())
84
85 packages = set()
86 # The list of packages to test can be passed as a file containing a
87 # space-separated list of package names.
88 # This is used by the builder to test only the packages that were upreved.
89 if opts.package_file and os.path.exists(opts.package_file):
90 packages = set(osutils.ReadFile(opts.package_file).split())
91
92 if opts.packages:
93 packages |= set(opts.packages.split())
94
95 # If no packages were specified, use all testable packages.
Chris McDonalde69db662018-11-15 12:50:18 -070096 if not (opts.packages or opts.package_file) and not opts.empty_sysroot:
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070097 workon = workon_helper.WorkonHelper(sysroot)
98 packages = (workon.InstalledWorkonAtoms() if opts.installed
Chris McDonalde69db662018-11-15 12:50:18 -070099 else set(workon.ListAtoms(use_all=True)))
100
101 if opts.empty_sysroot:
102 packages |= determine_board_packages(sysroot, BOARD_VIRTUAL_PACKAGES)
103 workon = workon_helper.WorkonHelper(sysroot)
104 workon_packages = set(workon.ListAtoms(use_all=True))
105 packages &= workon_packages
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700106
107 for cp in packages & package_blacklist:
108 logging.info('Skipping blacklisted package %s.', cp)
109
110 packages = packages - package_blacklist
111 pkg_with_test = portage_util.PackagesWithTest(sysroot, packages)
112
113 if packages - pkg_with_test:
114 logging.warning('The following packages do not have tests:')
115 logging.warning('\n'.join(sorted(packages - pkg_with_test)))
116
117 if opts.pretend:
118 print('\n'.join(sorted(pkg_with_test)))
119 return
120
121 env = None
122 if opts.nowithdebug:
123 use_flags = os.environ.get('USE', '')
124 use_flags += ' -cros-debug'
125 env = {'USE': use_flags}
126
Chris McDonalde69db662018-11-15 12:50:18 -0700127 if opts.empty_sysroot:
128 try:
129 chroot_util.Emerge(list(IMPLICIT_TEST_DEPS), sysroot, rebuild_deps=False,
130 use_binary=False)
131 chroot_util.Emerge(list(pkg_with_test), sysroot, rebuild_deps=False,
132 use_binary=False)
133 except cros_build_lib.RunCommandError:
134 logging.error('Failed building dependencies for unittests.')
135 raise
136
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700137 try:
Aviv Keshet01fcca42016-07-25 16:34:59 -0700138 chroot_util.RunUnittests(sysroot, pkg_with_test, extra_env=env,
139 jobs=min(10, multiprocessing.cpu_count()))
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700140 except cros_build_lib.RunCommandError:
141 logging.error('Unittests failed.')
142 raise