xref: /aosp_15_r20/development/scripts/update_crate_tests.py (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1#!/usr/bin/env python3
2#
3# Copyright (C) 2020 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16"""Add or update tests to TEST_MAPPING.
17
18This script uses Bazel to find reverse dependencies on a crate and generates a
19TEST_MAPPING file. It accepts the absolute path to a crate as argument. If no
20argument is provided, it assumes the crate is the current directory.
21
22  Usage:
23  $ . build/envsetup.sh
24  $ lunch aosp_arm64-eng
25  $ update_crate_tests.py $ANDROID_BUILD_TOP/external/rust/crates/libc
26
27This script is automatically called by external_updater.
28
29A test_mapping_config.json file can be defined in the project directory to
30configure the generated TEST_MAPPING file, for example:
31
32    {
33        // Run tests in postsubmit instead of presubmit.
34        "postsubmit_tests":["foo"]
35    }
36
37"""
38
39import argparse
40import glob
41import json
42import os
43import platform
44import re
45import subprocess
46import sys
47from datetime import datetime
48from pathlib import Path
49
50# Some tests requires specific options. Consider fixing the upstream crate
51# before updating this dictionary.
52TEST_OPTIONS = {
53    "ring_test_tests_digest_tests": [{"test-timeout": "600000"}],
54    "ring_test_src_lib": [{"test-timeout": "100000"}],
55}
56
57# Groups to add tests to. "presubmit" runs x86_64 device tests+host tests, and
58# "presubmit-rust" runs arm64 device tests on physical devices.
59TEST_GROUPS = [
60    "presubmit",
61    "presubmit-rust",
62    "postsubmit",
63]
64
65# Excluded tests. These tests will be ignored by this script.
66TEST_EXCLUDE = [
67        "ash_test_src_lib",
68        "ash_test_tests_constant_size_arrays",
69        "ash_test_tests_display",
70        "shared_library_test_src_lib",
71        "vulkano_test_src_lib",
72
73        # These are helper binaries for aidl_integration_test
74        # and aren't actually meant to run as individual tests.
75        "aidl_test_rust_client",
76        "aidl_test_rust_service",
77        "aidl_test_rust_service_async",
78
79        # This is a helper binary for AuthFsHostTest and shouldn't
80        # be run directly.
81        "open_then_run",
82
83        # TODO: Remove when b/198197213 is closed.
84        "diced_client_test",
85
86        "CoverageRustSmokeTest",
87        "libtrusty-rs-tests",
88        "terminal-size_test_src_lib",
89]
90
91# Excluded modules.
92EXCLUDE_PATHS = [
93        "//external/adhd",
94        "//external/crosvm",
95        "//external/libchromeos-rs",
96        "//external/vm_tools"
97]
98
99LABEL_PAT = re.compile('^//(.*):.*$')
100EXTERNAL_PAT = re.compile('^//external/rust/')
101
102
103class UpdaterException(Exception):
104    """Exception generated by this script."""
105
106
107class Env(object):
108    """Env captures the execution environment.
109
110    It ensures this script is executed within an AOSP repository.
111
112    Attributes:
113      ANDROID_BUILD_TOP: A string representing the absolute path to the top
114        of the repository.
115    """
116    def __init__(self):
117        try:
118            self.ANDROID_BUILD_TOP = os.environ['ANDROID_BUILD_TOP']
119        except KeyError:
120            raise UpdaterException('$ANDROID_BUILD_TOP is not defined; you '
121                                   'must first source build/envsetup.sh and '
122                                   'select a target.')
123
124
125class Bazel(object):
126    """Bazel wrapper.
127
128    The wrapper is used to call bazel queryview and generate the list of
129    reverse dependencies.
130
131    Attributes:
132      path: The path to the bazel executable.
133    """
134    def __init__(self, env):
135        """Constructor.
136
137        Note that the current directory is changed to ANDROID_BUILD_TOP.
138
139        Args:
140          env: An instance of Env.
141
142        Raises:
143          UpdaterException: an error occurred while calling soong_ui.
144        """
145        if platform.system() != 'Linux':
146            raise UpdaterException('This script has only been tested on Linux.')
147        self.path = os.path.join(env.ANDROID_BUILD_TOP, "build", "bazel", "bin", "bazel")
148        soong_ui = os.path.join(env.ANDROID_BUILD_TOP, "build", "soong", "soong_ui.bash")
149
150        # soong_ui requires to be at the root of the repository.
151        os.chdir(env.ANDROID_BUILD_TOP)
152
153        print("Building Bazel Queryview. This can take a couple of minutes...")
154        cmd = [soong_ui, "--build-mode", "--all-modules", "--dir=.", "queryview"]
155        try:
156            subprocess.check_output(cmd, stderr=subprocess.STDOUT, text=True)
157        except subprocess.CalledProcessError as e:
158            raise UpdaterException('Unable to update TEST_MAPPING: ' + e.output)
159
160    def query_modules(self, path):
161        """Returns all modules for a given path."""
162        cmd = self.path + " query --config=queryview /" + path + ":all"
163        out = subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, text=True).strip().split("\n")
164        modules = set()
165        for line in out:
166            # speed up by excluding unused modules.
167            if "windows_x86" in line:
168                continue
169            modules.add(line)
170        return modules
171
172    def query_rdeps(self, module):
173        """Returns all reverse dependencies for a single module."""
174        cmd = (self.path + " query --config=queryview \'rdeps(//..., " +
175                module + ")\' --output=label_kind")
176        out = (subprocess.check_output(cmd, shell=True, stderr=subprocess.DEVNULL, text=True)
177                .strip().split("\n"))
178        if '' in out:
179            out.remove('')
180        return out
181
182    def exclude_module(self, module):
183        for path in EXCLUDE_PATHS:
184            if module.startswith(path):
185                return True
186        return False
187
188    # Return all the TEST_MAPPING files within a given path.
189    def find_all_test_mapping_files(self, path):
190        result = []
191        for root, dirs, files in os.walk(path):
192            if "TEST_MAPPING" in files:
193                result.append(os.path.join(root, "TEST_MAPPING"))
194        return result
195
196    # For a given test, return the TEST_MAPPING file where the test is mapped.
197    # This limits the search to the directory specified in "path" along with its subdirs.
198    def test_to_test_mapping(self, env, path, test):
199        test_mapping_files = self.find_all_test_mapping_files(env.ANDROID_BUILD_TOP + path)
200        for file in test_mapping_files:
201            with open(file) as fd:
202                if "\""+ test + "\"" in fd.read():
203                    mapping_path = file.split("/TEST_MAPPING")[0].split("//")[1]
204                    return mapping_path
205
206        return None
207
208    # Returns:
209    # rdep_test: for tests specified locally.
210    # rdep_dirs: for paths to TEST_MAPPING files for reverse dependencies.
211    #
212    # We import directories for non-local tests because including tests directly has proven to be
213    # fragile and burdensome. For example, whenever a project removes or renames a test, all the
214    # TEST_MAPPING files for its reverse dependencies must be updated or we get test breakages.
215    # That can be many tens of projects that must updated to prevent the reported breakage of tests
216    # that no longer exist. Similarly when a test is added, it won't be run when the reverse
217    # dependencies change unless/until update_crate_tests.py is run for its depenencies.
218    # Importing TEST_MAPPING files instead of tests solves both of these problems. When tests are
219    # removed, renamed, or added, only files local to the project need to be modified.
220    # The downside is that we potentially miss some tests. But this seems like a reasonable
221    # tradeoff.
222    def query_rdep_tests_dirs(self, env, modules, path, exclude_dir):
223        """Returns all reverse dependency tests for modules in this package."""
224        rdep_tests = set()
225        rdep_dirs = set()
226        path_pat = re.compile("^/%s:.*$" % path)
227        for module in modules:
228            for rdep in self.query_rdeps(module):
229                rule_type, _, mod = rdep.split(" ")
230                if rule_type == "rust_test_" or rule_type == "rust_test":
231                    if self.exclude_module(mod):
232                        continue
233                    path_match = path_pat.match(mod)
234                    if path_match or not EXTERNAL_PAT.match(mod):
235                        rdep_path = mod.split(":")[0]
236                        rdep_test = mod.split(":")[1].split("--")[0]
237                        mapping_path = self.test_to_test_mapping(env, rdep_path, rdep_test)
238                        # Only include tests directly if they're local to the project.
239                        if (mapping_path is not None) and exclude_dir.endswith(mapping_path):
240                            rdep_tests.add(rdep_test)
241                        # All other tests are included by path.
242                        elif mapping_path is not None:
243                            rdep_dirs.add(mapping_path)
244                    else:
245                        label_match = LABEL_PAT.match(mod)
246                        if label_match:
247                            rdep_dirs.add(label_match.group(1))
248        return (rdep_tests, rdep_dirs)
249
250
251class Package(object):
252    """A Bazel package.
253
254    Attributes:
255      dir: The absolute path to this package.
256      dir_rel: The relative path to this package.
257      rdep_tests: The list of computed reverse dependencies.
258      rdep_dirs: The list of computed reverse dependency directories.
259    """
260    def __init__(self, path, env, bazel):
261        """Constructor.
262
263        Note that the current directory is changed to the package location when
264        called.
265
266        Args:
267          path: Path to the package.
268          env: An instance of Env.
269          bazel: An instance of Bazel.
270
271        Raises:
272          UpdaterException: the package does not appear to belong to the
273            current repository.
274        """
275        self.dir = path
276        try:
277            self.dir_rel = self.dir.split(env.ANDROID_BUILD_TOP)[1]
278        except IndexError:
279            raise UpdaterException('The path ' + self.dir + ' is not under ' +
280                            env.ANDROID_BUILD_TOP + '; You must be in the '
281                            'directory of a crate or pass its absolute path '
282                            'as the argument.')
283
284        # Move to the package_directory.
285        os.chdir(self.dir)
286        modules = bazel.query_modules(self.dir_rel)
287        (self.rdep_tests, self.rdep_dirs) = bazel.query_rdep_tests_dirs(env, modules,
288                                                                        self.dir_rel, self.dir)
289
290    def get_rdep_tests_dirs(self):
291        return (self.rdep_tests, self.rdep_dirs)
292
293
294class TestMapping(object):
295    """A TEST_MAPPING file.
296
297    Attributes:
298      package: The package associated with this TEST_MAPPING file.
299    """
300    def __init__(self, env, bazel, path):
301        """Constructor.
302
303        Args:
304          env: An instance of Env.
305          bazel: An instance of Bazel.
306          path: The absolute path to the package.
307        """
308        self.package = Package(path, env, bazel)
309
310    def create(self):
311        """Generates the TEST_MAPPING file."""
312        (tests, dirs) = self.package.get_rdep_tests_dirs()
313        if not bool(tests) and not bool(dirs):
314            if os.path.isfile('TEST_MAPPING'):
315                os.remove('TEST_MAPPING')
316            return
317        test_mapping = self.tests_dirs_to_mapping(tests, dirs)
318        self.write_test_mapping(test_mapping)
319
320    def tests_dirs_to_mapping(self, tests, dirs):
321        """Translate the test list into a dictionary."""
322        test_mapping = {"imports": []}
323        config = None
324        if os.path.isfile(os.path.join(self.package.dir, "test_mapping_config.json")):
325            with open(os.path.join(self.package.dir, "test_mapping_config.json"), 'r') as fd:
326                config = json.load(fd)
327
328        for test_group in TEST_GROUPS:
329            test_mapping[test_group] = []
330            for test in tests:
331                if test in TEST_EXCLUDE:
332                    continue
333                if config and 'postsubmit_tests' in config:
334                    if test in config['postsubmit_tests'] and 'postsubmit' not in test_group:
335                        continue
336                    if test not in config['postsubmit_tests'] and 'postsubmit' in test_group:
337                        continue
338                else:
339                    if 'postsubmit' in test_group:
340                        # If postsubmit_tests is not configured, do not place
341                        # anything in postsubmit - presubmit groups are
342                        # automatically included in postsubmit in CI.
343                        continue
344                if test in TEST_OPTIONS:
345                    test_mapping[test_group].append({"name": test, "options": TEST_OPTIONS[test]})
346                else:
347                    test_mapping[test_group].append({"name": test})
348            test_mapping[test_group] = sorted(test_mapping[test_group], key=lambda t: t["name"])
349
350        for dir in dirs:
351            test_mapping["imports"].append({"path": dir})
352        test_mapping["imports"] = sorted(test_mapping["imports"], key=lambda t: t["path"])
353        test_mapping = {section: entry for (section, entry) in test_mapping.items() if entry}
354        return test_mapping
355
356    def write_test_mapping(self, test_mapping):
357        """Writes the TEST_MAPPING file."""
358        with open("TEST_MAPPING", "w") as json_file:
359            json_file.write("// Generated by update_crate_tests.py for tests that depend on this crate.\n")
360            json.dump(test_mapping, json_file, indent=2, separators=(',', ': '), sort_keys=True)
361            json_file.write("\n")
362        print("TEST_MAPPING successfully updated for %s!" % self.package.dir_rel)
363
364
365def parse_args():
366    parser = argparse.ArgumentParser('update_crate_tests')
367    parser.add_argument('paths',
368                        nargs='*',
369                        help='Absolute or relative paths of the projects as globs.')
370    parser.add_argument('--branch_and_commit',
371                        action='store_true',
372                        help='Starts a new branch and commit changes.')
373    parser.add_argument('--push_change',
374                        action='store_true',
375                        help='Pushes change to Gerrit.')
376    return parser.parse_args()
377
378def main():
379    args = parse_args()
380    paths = args.paths if len(args.paths) > 0 else [os.getcwd()]
381    # We want to use glob to get all the paths, so we first convert to absolute.
382    paths = [Path(path).resolve() for path in paths]
383    paths = sorted([path for abs_path in paths
384                    for path in glob.glob(str(abs_path))])
385
386    env = Env()
387    bazel = Bazel(env)
388    for path in paths:
389        try:
390            test_mapping = TestMapping(env, bazel, path)
391            test_mapping.create()
392            changed = (subprocess.call(['git', 'diff', '--quiet']) == 1)
393            untracked = (os.path.isfile('TEST_MAPPING') and
394                         (subprocess.run(['git', 'ls-files', '--error-unmatch', 'TEST_MAPPING'],
395                                         stderr=subprocess.DEVNULL,
396                                         stdout=subprocess.DEVNULL).returncode == 1))
397            if args.branch_and_commit and (changed or untracked):
398                subprocess.check_output(['repo', 'start',
399                                         'tmp_auto_test_mapping', '.'])
400                subprocess.check_output(['git', 'add', 'TEST_MAPPING'])
401                # test_mapping_config.json is not always present
402                subprocess.call(['git', 'add', 'test_mapping_config.json'],
403                                stderr=subprocess.DEVNULL,
404                                stdout=subprocess.DEVNULL)
405                subprocess.check_output(['git', 'commit', '-m',
406                                         'Update TEST_MAPPING\n\nTest: None'])
407            if args.push_change and (changed or untracked):
408                date = datetime.today().strftime('%m-%d')
409                subprocess.check_output(['git', 'push', 'aosp', 'HEAD:refs/for/master',
410                                         '-o', 'topic=test-mapping-%s' % date])
411        except (UpdaterException, subprocess.CalledProcessError) as err:
412            sys.exit("Error: " + str(err))
413
414if __name__ == '__main__':
415  main()
416