xref: /aosp_15_r20/external/bazelbuild-rules_python/tests/integration/integration_test.bzl (revision 60517a1edbc8ecf509223e9af94a7adec7d736b8)
1*60517a1eSAndroid Build Coastguard Worker# Copyright 2023 The Bazel Authors. All rights reserved.
2*60517a1eSAndroid Build Coastguard Worker#
3*60517a1eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*60517a1eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*60517a1eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*60517a1eSAndroid Build Coastguard Worker#
7*60517a1eSAndroid Build Coastguard Worker#    http://www.apache.org/licenses/LICENSE-2.0
8*60517a1eSAndroid Build Coastguard Worker#
9*60517a1eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*60517a1eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*60517a1eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*60517a1eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*60517a1eSAndroid Build Coastguard Worker# limitations under the License.
14*60517a1eSAndroid Build Coastguard Worker"""Helpers for running bazel-in-bazel integration tests."""
15*60517a1eSAndroid Build Coastguard Worker
16*60517a1eSAndroid Build Coastguard Workerload("@bazel_binaries//:defs.bzl", "bazel_binaries")
17*60517a1eSAndroid Build Coastguard Workerload(
18*60517a1eSAndroid Build Coastguard Worker    "@rules_bazel_integration_test//bazel_integration_test:defs.bzl",
19*60517a1eSAndroid Build Coastguard Worker    "bazel_integration_test",
20*60517a1eSAndroid Build Coastguard Worker    "integration_test_utils",
21*60517a1eSAndroid Build Coastguard Worker)
22*60517a1eSAndroid Build Coastguard Workerload("//python:py_test.bzl", "py_test")
23*60517a1eSAndroid Build Coastguard Worker
24*60517a1eSAndroid Build Coastguard Workerdef _test_runner(*, name, bazel_version, py_main, bzlmod, gazelle_plugin):
25*60517a1eSAndroid Build Coastguard Worker    if py_main:
26*60517a1eSAndroid Build Coastguard Worker        test_runner = "{}_bazel_{}_py_runner".format(name, bazel_version)
27*60517a1eSAndroid Build Coastguard Worker        py_test(
28*60517a1eSAndroid Build Coastguard Worker            name = test_runner,
29*60517a1eSAndroid Build Coastguard Worker            srcs = [py_main],
30*60517a1eSAndroid Build Coastguard Worker            main = py_main,
31*60517a1eSAndroid Build Coastguard Worker            deps = [":runner_lib"],
32*60517a1eSAndroid Build Coastguard Worker            # Hide from ... patterns; should only be run as part
33*60517a1eSAndroid Build Coastguard Worker            # of the bazel integration test
34*60517a1eSAndroid Build Coastguard Worker            tags = ["manual"],
35*60517a1eSAndroid Build Coastguard Worker        )
36*60517a1eSAndroid Build Coastguard Worker        return test_runner
37*60517a1eSAndroid Build Coastguard Worker
38*60517a1eSAndroid Build Coastguard Worker    if bazel_version.startswith("6") and not bzlmod:
39*60517a1eSAndroid Build Coastguard Worker        if gazelle_plugin:
40*60517a1eSAndroid Build Coastguard Worker            return "//tests/integration:bazel_6_4_workspace_test_runner_gazelle_plugin"
41*60517a1eSAndroid Build Coastguard Worker        else:
42*60517a1eSAndroid Build Coastguard Worker            return "//tests/integration:bazel_6_4_workspace_test_runner"
43*60517a1eSAndroid Build Coastguard Worker
44*60517a1eSAndroid Build Coastguard Worker    if bzlmod and gazelle_plugin:
45*60517a1eSAndroid Build Coastguard Worker        return "//tests/integration:test_runner_gazelle_plugin"
46*60517a1eSAndroid Build Coastguard Worker    elif bzlmod:
47*60517a1eSAndroid Build Coastguard Worker        return "//tests/integration:test_runner"
48*60517a1eSAndroid Build Coastguard Worker    elif gazelle_plugin:
49*60517a1eSAndroid Build Coastguard Worker        return "//tests/integration:workspace_test_runner_gazelle_plugin"
50*60517a1eSAndroid Build Coastguard Worker    else:
51*60517a1eSAndroid Build Coastguard Worker        return "//tests/integration:workspace_test_runner"
52*60517a1eSAndroid Build Coastguard Worker
53*60517a1eSAndroid Build Coastguard Workerdef rules_python_integration_test(
54*60517a1eSAndroid Build Coastguard Worker        name,
55*60517a1eSAndroid Build Coastguard Worker        workspace_path = None,
56*60517a1eSAndroid Build Coastguard Worker        bzlmod = True,
57*60517a1eSAndroid Build Coastguard Worker        gazelle_plugin = False,
58*60517a1eSAndroid Build Coastguard Worker        tags = None,
59*60517a1eSAndroid Build Coastguard Worker        py_main = None,
60*60517a1eSAndroid Build Coastguard Worker        bazel_versions = None,
61*60517a1eSAndroid Build Coastguard Worker        **kwargs):
62*60517a1eSAndroid Build Coastguard Worker    """Runs a bazel-in-bazel integration test.
63*60517a1eSAndroid Build Coastguard Worker
64*60517a1eSAndroid Build Coastguard Worker    Args:
65*60517a1eSAndroid Build Coastguard Worker        name: Name of the test. This gets appended by the bazel version.
66*60517a1eSAndroid Build Coastguard Worker        workspace_path: The directory name. Defaults to `name` without the
67*60517a1eSAndroid Build Coastguard Worker            `_test` suffix.
68*60517a1eSAndroid Build Coastguard Worker        bzlmod: bool, default True. If true, run with bzlmod enabled, otherwise
69*60517a1eSAndroid Build Coastguard Worker            disable bzlmod.
70*60517a1eSAndroid Build Coastguard Worker        gazelle_plugin: Whether the test uses the gazelle plugin.
71*60517a1eSAndroid Build Coastguard Worker        tags: Test tags.
72*60517a1eSAndroid Build Coastguard Worker        py_main: Optional `.py` file to run tests using. When specified, a
73*60517a1eSAndroid Build Coastguard Worker            python based test runner is used, and this source file is the main
74*60517a1eSAndroid Build Coastguard Worker            entry point and responsible for executing tests.
75*60517a1eSAndroid Build Coastguard Worker        bazel_versions: `list[str] | None`, the bazel versions to test. I
76*60517a1eSAndroid Build Coastguard Worker            not specified, defaults to all configured bazel versions.
77*60517a1eSAndroid Build Coastguard Worker        **kwargs: Passed to the upstream `bazel_integration_tests` rule.
78*60517a1eSAndroid Build Coastguard Worker    """
79*60517a1eSAndroid Build Coastguard Worker    workspace_path = workspace_path or name.removesuffix("_test")
80*60517a1eSAndroid Build Coastguard Worker
81*60517a1eSAndroid Build Coastguard Worker    # Because glob expansion happens at loading time, the bazel-* symlinks
82*60517a1eSAndroid Build Coastguard Worker    # in the workspaces can recursively expand to tens-of-thousands of entries,
83*60517a1eSAndroid Build Coastguard Worker    # which consumes lots of CPU and RAM and can render the system unusable.
84*60517a1eSAndroid Build Coastguard Worker    # To help prevent that, cap the size of the glob expansion.
85*60517a1eSAndroid Build Coastguard Worker    workspace_files = integration_test_utils.glob_workspace_files(workspace_path)
86*60517a1eSAndroid Build Coastguard Worker    if len(workspace_files) > 1000:
87*60517a1eSAndroid Build Coastguard Worker        fail("Workspace {} has too many files. This likely means a bazel-* " +
88*60517a1eSAndroid Build Coastguard Worker             "symlink is being followed when it should be ignored.")
89*60517a1eSAndroid Build Coastguard Worker
90*60517a1eSAndroid Build Coastguard Worker    # bazel_integration_tests creates a separate file group target of the workspace
91*60517a1eSAndroid Build Coastguard Worker    # files for each bazel version, even though the file groups are the same
92*60517a1eSAndroid Build Coastguard Worker    # for each one.
93*60517a1eSAndroid Build Coastguard Worker    # To avoid that, manually create a single filegroup once and re-use it.
94*60517a1eSAndroid Build Coastguard Worker    native.filegroup(
95*60517a1eSAndroid Build Coastguard Worker        name = name + "_workspace_files",
96*60517a1eSAndroid Build Coastguard Worker        srcs = workspace_files + [
97*60517a1eSAndroid Build Coastguard Worker            "//:distribution",
98*60517a1eSAndroid Build Coastguard Worker        ],
99*60517a1eSAndroid Build Coastguard Worker    )
100*60517a1eSAndroid Build Coastguard Worker    kwargs.setdefault("size", "enormous")
101*60517a1eSAndroid Build Coastguard Worker    for bazel_version in bazel_versions or bazel_binaries.versions.all:
102*60517a1eSAndroid Build Coastguard Worker        test_runner = _test_runner(
103*60517a1eSAndroid Build Coastguard Worker            name = name,
104*60517a1eSAndroid Build Coastguard Worker            bazel_version = bazel_version,
105*60517a1eSAndroid Build Coastguard Worker            py_main = py_main,
106*60517a1eSAndroid Build Coastguard Worker            bzlmod = bzlmod,
107*60517a1eSAndroid Build Coastguard Worker            gazelle_plugin = gazelle_plugin,
108*60517a1eSAndroid Build Coastguard Worker        )
109*60517a1eSAndroid Build Coastguard Worker        bazel_integration_test(
110*60517a1eSAndroid Build Coastguard Worker            name = "{}_bazel_{}".format(name, bazel_version),
111*60517a1eSAndroid Build Coastguard Worker            workspace_path = workspace_path,
112*60517a1eSAndroid Build Coastguard Worker            test_runner = test_runner,
113*60517a1eSAndroid Build Coastguard Worker            bazel_version = bazel_version,
114*60517a1eSAndroid Build Coastguard Worker            workspace_files = [name + "_workspace_files"],
115*60517a1eSAndroid Build Coastguard Worker            # Override the tags so that the `manual` tag isn't applied.
116*60517a1eSAndroid Build Coastguard Worker            tags = (tags or []) + [
117*60517a1eSAndroid Build Coastguard Worker                # These tests are very heavy weight, so much so that only a couple
118*60517a1eSAndroid Build Coastguard Worker                # can be run in parallel without harming their reliability,
119*60517a1eSAndroid Build Coastguard Worker                # overall runtime, and the system's stability. Unfortunately,
120*60517a1eSAndroid Build Coastguard Worker                # there doesn't appear to be a way to tell Bazel to limit their
121*60517a1eSAndroid Build Coastguard Worker                # concurrency, only disable it entirely with exclusive.
122*60517a1eSAndroid Build Coastguard Worker                "exclusive",
123*60517a1eSAndroid Build Coastguard Worker                # The default_test_runner() assumes it can write to the user's home
124*60517a1eSAndroid Build Coastguard Worker                # directory for caching purposes. Give it access.
125*60517a1eSAndroid Build Coastguard Worker                "no-sandbox",
126*60517a1eSAndroid Build Coastguard Worker                # The CI RBE setup can't successfully run these tests remotely.
127*60517a1eSAndroid Build Coastguard Worker                "no-remote-exec",
128*60517a1eSAndroid Build Coastguard Worker                # A special tag is used so CI can run them as a separate job.
129*60517a1eSAndroid Build Coastguard Worker                "integration-test",
130*60517a1eSAndroid Build Coastguard Worker            ],
131*60517a1eSAndroid Build Coastguard Worker            **kwargs
132*60517a1eSAndroid Build Coastguard Worker        )
133