blob: 4bfea4209934e10b1cc37b69ffb60cfa9111b364 [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
Mike Frysinger6a2b0f22020-02-20 13:34:07 -050012import sys
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070013
14from chromite.lib import commandline
Chris McDonalde69db662018-11-15 12:50:18 -070015from chromite.lib import constants
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070016from chromite.lib import chroot_util
17from chromite.lib import cros_build_lib
18from chromite.lib import cros_logging as logging
19from chromite.lib import osutils
20from chromite.lib import workon_helper
21from chromite.lib import portage_util
Chris McDonalde69db662018-11-15 12:50:18 -070022from chromite.scripts import cros_extract_deps
23
Mike Frysinger6a2b0f22020-02-20 13:34:07 -050024
25assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
26
27
Chris McDonalde69db662018-11-15 12:50:18 -070028BOARD_VIRTUAL_PACKAGES = (constants.TARGET_OS_PKG,
29 constants.TARGET_OS_DEV_PKG,
Chris McDonaldb0c021c2019-01-07 15:04:22 -070030 constants.TARGET_OS_TEST_PKG,
31 constants.TARGET_OS_FACTORY_PKG)
Chris McDonalde69db662018-11-15 12:50:18 -070032IMPLICIT_TEST_DEPS = ('virtual/implicit-system',)
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070033
34
35def ParseArgs(argv):
36 """Parse arguments.
37
38 Args:
39 argv: array of arguments passed to the script.
40 """
41 parser = commandline.ArgumentParser(description=__doc__)
42
43 target = parser.add_mutually_exclusive_group(required=True)
44 target.add_argument('--sysroot', type='path', help='Path to the sysroot.')
45 target.add_argument('--board', help='Board name.')
46
47 parser.add_argument('--pretend', default=False, action='store_true',
48 help='Show the list of packages to be tested and return.')
49 parser.add_argument('--noinstalled_only', dest='installed', default=True,
50 action='store_false',
51 help='Test all testable packages, even if they are not '
52 'currently installed.')
53 parser.add_argument('--package_file', type='path',
54 help='Path to a file containing the list of packages '
55 'that should be tested.')
56 parser.add_argument('--blacklist_packages', dest='package_blacklist',
57 help='Space-separated list of blacklisted packages.')
58 parser.add_argument('--packages',
59 help='Space-separated list of packages to test.')
60 parser.add_argument('--nowithdebug', action='store_true',
61 help="Don't build the tests with USE=cros-debug")
Chris McDonalde69db662018-11-15 12:50:18 -070062 parser.add_argument('--assume-empty-sysroot', default=False,
63 action='store_true', dest='empty_sysroot',
64 help='Set up dependencies and run unit tests for all '
65 'packages that could be installed on target board '
66 'without assuming that any packages have actually '
67 'been merged yet.')
Alex Kleinfe6081c2020-03-16 10:02:24 -060068 parser.add_argument(
69 '-j',
70 '--jobs',
71 type=int,
72 default=multiprocessing.cpu_count(),
73 help='The limit for the number of possible concurrent jobs.')
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070074
75 options = parser.parse_args(argv)
76 options.Freeze()
77 return options
78
79
Chris McDonalde69db662018-11-15 12:50:18 -070080def determine_board_packages(sysroot, virtual_packages):
81 """Returns a set of the dependencies for the given packages"""
Chris McDonaldd8a7f112019-11-01 10:35:07 -060082 deps, _bdeps = cros_extract_deps.ExtractDeps(
83 sysroot, virtual_packages, include_bdepend=False)
84 return set(
85 '%s/%s' % (atom['category'], atom['name']) for atom in deps.values())
Chris McDonalde69db662018-11-15 12:50:18 -070086
87
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070088def main(argv):
89 opts = ParseArgs(argv)
90
91 cros_build_lib.AssertInsideChroot()
92
93 sysroot = opts.sysroot or cros_build_lib.GetSysroot(opts.board)
94 package_blacklist = portage_util.UNITTEST_PACKAGE_BLACKLIST
95 if opts.package_blacklist:
96 package_blacklist |= set(opts.package_blacklist.split())
97
98 packages = set()
99 # The list of packages to test can be passed as a file containing a
100 # space-separated list of package names.
101 # This is used by the builder to test only the packages that were upreved.
102 if opts.package_file and os.path.exists(opts.package_file):
103 packages = set(osutils.ReadFile(opts.package_file).split())
104
105 if opts.packages:
106 packages |= set(opts.packages.split())
107
108 # If no packages were specified, use all testable packages.
Chris McDonalde69db662018-11-15 12:50:18 -0700109 if not (opts.packages or opts.package_file) and not opts.empty_sysroot:
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700110 workon = workon_helper.WorkonHelper(sysroot)
111 packages = (workon.InstalledWorkonAtoms() if opts.installed
Chris McDonalde69db662018-11-15 12:50:18 -0700112 else set(workon.ListAtoms(use_all=True)))
113
114 if opts.empty_sysroot:
115 packages |= determine_board_packages(sysroot, BOARD_VIRTUAL_PACKAGES)
116 workon = workon_helper.WorkonHelper(sysroot)
117 workon_packages = set(workon.ListAtoms(use_all=True))
118 packages &= workon_packages
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700119
120 for cp in packages & package_blacklist:
121 logging.info('Skipping blacklisted package %s.', cp)
122
123 packages = packages - package_blacklist
124 pkg_with_test = portage_util.PackagesWithTest(sysroot, packages)
125
126 if packages - pkg_with_test:
Mike Frysinger80aa9322019-09-01 15:38:56 -0400127 logging.warning('The following packages do not have tests:\n %s',
128 '\n '.join(sorted(packages - pkg_with_test)))
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700129
Mike Frysingerdcb680f2019-09-02 00:21:44 -0400130 if not pkg_with_test:
131 logging.error('No testable packages found!')
132 return 1
133
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700134 if opts.pretend:
135 print('\n'.join(sorted(pkg_with_test)))
Mike Frysingerdcb680f2019-09-02 00:21:44 -0400136 return 0
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700137
138 env = None
139 if opts.nowithdebug:
140 use_flags = os.environ.get('USE', '')
141 use_flags += ' -cros-debug'
142 env = {'USE': use_flags}
143
Chris McDonalde69db662018-11-15 12:50:18 -0700144 if opts.empty_sysroot:
145 try:
146 chroot_util.Emerge(list(IMPLICIT_TEST_DEPS), sysroot, rebuild_deps=False,
147 use_binary=False)
148 chroot_util.Emerge(list(pkg_with_test), sysroot, rebuild_deps=False,
149 use_binary=False)
150 except cros_build_lib.RunCommandError:
151 logging.error('Failed building dependencies for unittests.')
Mike Frysingerd56bb812019-09-01 15:42:24 -0400152 return 1
Chris McDonalde69db662018-11-15 12:50:18 -0700153
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700154 try:
Alex Kleinfe6081c2020-03-16 10:02:24 -0600155 chroot_util.RunUnittests(
156 sysroot, pkg_with_test, extra_env=env, jobs=opts.jobs)
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700157 except cros_build_lib.RunCommandError:
158 logging.error('Unittests failed.')
Mike Frysingerd56bb812019-09-01 15:42:24 -0400159 return 1