blob: ff29b9d5c09b8d7c41d903a3d83607d51c69f12f [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,
Chris McDonaldb0c021c2019-01-07 15:04:22 -070025 constants.TARGET_OS_TEST_PKG,
26 constants.TARGET_OS_FACTORY_PKG)
Chris McDonalde69db662018-11-15 12:50:18 -070027IMPLICIT_TEST_DEPS = ('virtual/implicit-system',)
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070028
29
30def ParseArgs(argv):
31 """Parse arguments.
32
33 Args:
34 argv: array of arguments passed to the script.
35 """
36 parser = commandline.ArgumentParser(description=__doc__)
37
38 target = parser.add_mutually_exclusive_group(required=True)
39 target.add_argument('--sysroot', type='path', help='Path to the sysroot.')
40 target.add_argument('--board', help='Board name.')
41
42 parser.add_argument('--pretend', default=False, action='store_true',
43 help='Show the list of packages to be tested and return.')
44 parser.add_argument('--noinstalled_only', dest='installed', default=True,
45 action='store_false',
46 help='Test all testable packages, even if they are not '
47 'currently installed.')
48 parser.add_argument('--package_file', type='path',
49 help='Path to a file containing the list of packages '
50 'that should be tested.')
51 parser.add_argument('--blacklist_packages', dest='package_blacklist',
52 help='Space-separated list of blacklisted packages.')
53 parser.add_argument('--packages',
54 help='Space-separated list of packages to test.')
55 parser.add_argument('--nowithdebug', action='store_true',
56 help="Don't build the tests with USE=cros-debug")
Chris McDonalde69db662018-11-15 12:50:18 -070057 parser.add_argument('--assume-empty-sysroot', default=False,
58 action='store_true', dest='empty_sysroot',
59 help='Set up dependencies and run unit tests for all '
60 'packages that could be installed on target board '
61 'without assuming that any packages have actually '
62 'been merged yet.')
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070063
64 options = parser.parse_args(argv)
65 options.Freeze()
66 return options
67
68
Chris McDonalde69db662018-11-15 12:50:18 -070069def determine_board_packages(sysroot, virtual_packages):
70 """Returns a set of the dependencies for the given packages"""
71 deps = cros_extract_deps.ExtractDeps(sysroot, virtual_packages)
72 return set('%s/%s' % (atom['category'], atom['name'])
73 for atom in deps.values())
74
75
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070076def main(argv):
77 opts = ParseArgs(argv)
78
79 cros_build_lib.AssertInsideChroot()
80
81 sysroot = opts.sysroot or cros_build_lib.GetSysroot(opts.board)
82 package_blacklist = portage_util.UNITTEST_PACKAGE_BLACKLIST
83 if opts.package_blacklist:
84 package_blacklist |= set(opts.package_blacklist.split())
85
86 packages = set()
87 # The list of packages to test can be passed as a file containing a
88 # space-separated list of package names.
89 # This is used by the builder to test only the packages that were upreved.
90 if opts.package_file and os.path.exists(opts.package_file):
91 packages = set(osutils.ReadFile(opts.package_file).split())
92
93 if opts.packages:
94 packages |= set(opts.packages.split())
95
96 # If no packages were specified, use all testable packages.
Chris McDonalde69db662018-11-15 12:50:18 -070097 if not (opts.packages or opts.package_file) and not opts.empty_sysroot:
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -070098 workon = workon_helper.WorkonHelper(sysroot)
99 packages = (workon.InstalledWorkonAtoms() if opts.installed
Chris McDonalde69db662018-11-15 12:50:18 -0700100 else set(workon.ListAtoms(use_all=True)))
101
102 if opts.empty_sysroot:
103 packages |= determine_board_packages(sysroot, BOARD_VIRTUAL_PACKAGES)
104 workon = workon_helper.WorkonHelper(sysroot)
105 workon_packages = set(workon.ListAtoms(use_all=True))
106 packages &= workon_packages
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700107
108 for cp in packages & package_blacklist:
109 logging.info('Skipping blacklisted package %s.', cp)
110
111 packages = packages - package_blacklist
112 pkg_with_test = portage_util.PackagesWithTest(sysroot, packages)
113
114 if packages - pkg_with_test:
Mike Frysinger80aa9322019-09-01 15:38:56 -0400115 logging.warning('The following packages do not have tests:\n %s',
116 '\n '.join(sorted(packages - pkg_with_test)))
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700117
Mike Frysingerdcb680f2019-09-02 00:21:44 -0400118 if not pkg_with_test:
119 logging.error('No testable packages found!')
120 return 1
121
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700122 if opts.pretend:
123 print('\n'.join(sorted(pkg_with_test)))
Mike Frysingerdcb680f2019-09-02 00:21:44 -0400124 return 0
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700125
126 env = None
127 if opts.nowithdebug:
128 use_flags = os.environ.get('USE', '')
129 use_flags += ' -cros-debug'
130 env = {'USE': use_flags}
131
Chris McDonalde69db662018-11-15 12:50:18 -0700132 if opts.empty_sysroot:
133 try:
134 chroot_util.Emerge(list(IMPLICIT_TEST_DEPS), sysroot, rebuild_deps=False,
135 use_binary=False)
136 chroot_util.Emerge(list(pkg_with_test), sysroot, rebuild_deps=False,
137 use_binary=False)
138 except cros_build_lib.RunCommandError:
139 logging.error('Failed building dependencies for unittests.')
Mike Frysingerd56bb812019-09-01 15:42:24 -0400140 return 1
Chris McDonalde69db662018-11-15 12:50:18 -0700141
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700142 try:
Aviv Keshet01fcca42016-07-25 16:34:59 -0700143 chroot_util.RunUnittests(sysroot, pkg_with_test, extra_env=env,
144 jobs=min(10, multiprocessing.cpu_count()))
Bertrand SIMONNETe025a0e2015-05-06 14:17:43 -0700145 except cros_build_lib.RunCommandError:
146 logging.error('Unittests failed.')
Mike Frysingerd56bb812019-09-01 15:42:24 -0400147 return 1