xref: /aosp_15_r20/external/grpc-grpc/tools/bazelify_tests/build_defs.bzl (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1*cc02d7e2SAndroid Build Coastguard Worker# Copyright 2023 The gRPC Authors
2*cc02d7e2SAndroid Build Coastguard Worker#
3*cc02d7e2SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*cc02d7e2SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*cc02d7e2SAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*cc02d7e2SAndroid Build Coastguard Worker#
7*cc02d7e2SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*cc02d7e2SAndroid Build Coastguard Worker#
9*cc02d7e2SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*cc02d7e2SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*cc02d7e2SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*cc02d7e2SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*cc02d7e2SAndroid Build Coastguard Worker# limitations under the License.
14*cc02d7e2SAndroid Build Coastguard Worker
15*cc02d7e2SAndroid Build Coastguard Worker"""
16*cc02d7e2SAndroid Build Coastguard WorkerContains macros used for running bazelified tests.
17*cc02d7e2SAndroid Build Coastguard Worker"""
18*cc02d7e2SAndroid Build Coastguard Worker
19*cc02d7e2SAndroid Build Coastguard Workerload(":dockerimage_current_versions.bzl", "DOCKERIMAGE_CURRENT_VERSIONS")
20*cc02d7e2SAndroid Build Coastguard Workerload("@bazel_toolchains//rules/exec_properties:exec_properties.bzl", "create_rbe_exec_properties_dict")
21*cc02d7e2SAndroid Build Coastguard Worker
22*cc02d7e2SAndroid Build Coastguard Workerdef _dockerized_sh_test(name, srcs = [], args = [], data = [], size = "medium", timeout = None, tags = [], exec_compatible_with = [], flaky = None, docker_image_version = None, docker_run_as_root = False, env = {}):
23*cc02d7e2SAndroid Build Coastguard Worker    """Runs sh_test under docker either via RBE or via docker sandbox."""
24*cc02d7e2SAndroid Build Coastguard Worker    if docker_image_version:
25*cc02d7e2SAndroid Build Coastguard Worker        image_spec = DOCKERIMAGE_CURRENT_VERSIONS.get(docker_image_version, None)
26*cc02d7e2SAndroid Build Coastguard Worker        if not image_spec:
27*cc02d7e2SAndroid Build Coastguard Worker            fail("Version info for docker image '%s' not found in dockerimage_current_versions.bzl" % docker_image_version)
28*cc02d7e2SAndroid Build Coastguard Worker    else:
29*cc02d7e2SAndroid Build Coastguard Worker        fail("docker_image_version attribute not set for dockerized test '%s'" % name)
30*cc02d7e2SAndroid Build Coastguard Worker
31*cc02d7e2SAndroid Build Coastguard Worker    exec_properties = create_rbe_exec_properties_dict(
32*cc02d7e2SAndroid Build Coastguard Worker        labels = {
33*cc02d7e2SAndroid Build Coastguard Worker            "workload": "misc",
34*cc02d7e2SAndroid Build Coastguard Worker            "machine_size": "misc_large",
35*cc02d7e2SAndroid Build Coastguard Worker        },
36*cc02d7e2SAndroid Build Coastguard Worker        docker_network = "standard",
37*cc02d7e2SAndroid Build Coastguard Worker        container_image = image_spec,
38*cc02d7e2SAndroid Build Coastguard Worker        # TODO(jtattermusch): note that docker sandbox doesn't currently support "docker_run_as_root"
39*cc02d7e2SAndroid Build Coastguard Worker        docker_run_as_root = docker_run_as_root,
40*cc02d7e2SAndroid Build Coastguard Worker    )
41*cc02d7e2SAndroid Build Coastguard Worker
42*cc02d7e2SAndroid Build Coastguard Worker    # since the tests require special bazel args, only run them when explicitly requested
43*cc02d7e2SAndroid Build Coastguard Worker    tags = ["manual"] + tags
44*cc02d7e2SAndroid Build Coastguard Worker
45*cc02d7e2SAndroid Build Coastguard Worker    # TODO(jtattermusch): find a way to ensure that action can only run under docker sandbox or remotely
46*cc02d7e2SAndroid Build Coastguard Worker    # to avoid running it outside of a docker container by accident.
47*cc02d7e2SAndroid Build Coastguard Worker
48*cc02d7e2SAndroid Build Coastguard Worker    test_args = {
49*cc02d7e2SAndroid Build Coastguard Worker        "name": name,
50*cc02d7e2SAndroid Build Coastguard Worker        "srcs": srcs,
51*cc02d7e2SAndroid Build Coastguard Worker        "tags": tags,
52*cc02d7e2SAndroid Build Coastguard Worker        "args": args,
53*cc02d7e2SAndroid Build Coastguard Worker        "flaky": flaky,
54*cc02d7e2SAndroid Build Coastguard Worker        "data": data,
55*cc02d7e2SAndroid Build Coastguard Worker        "size": size,
56*cc02d7e2SAndroid Build Coastguard Worker        "env": env,
57*cc02d7e2SAndroid Build Coastguard Worker        "timeout": timeout,
58*cc02d7e2SAndroid Build Coastguard Worker        "exec_compatible_with": exec_compatible_with,
59*cc02d7e2SAndroid Build Coastguard Worker        "exec_properties": exec_properties,
60*cc02d7e2SAndroid Build Coastguard Worker    }
61*cc02d7e2SAndroid Build Coastguard Worker
62*cc02d7e2SAndroid Build Coastguard Worker    native.sh_test(
63*cc02d7e2SAndroid Build Coastguard Worker        **test_args
64*cc02d7e2SAndroid Build Coastguard Worker    )
65*cc02d7e2SAndroid Build Coastguard Worker
66*cc02d7e2SAndroid Build Coastguard Workerdef _dockerized_genrule(name, cmd, outs, srcs = [], tags = [], exec_compatible_with = [], docker_image_version = None, docker_run_as_root = False):
67*cc02d7e2SAndroid Build Coastguard Worker    """Runs genrule under docker either via RBE or via docker sandbox."""
68*cc02d7e2SAndroid Build Coastguard Worker    if docker_image_version:
69*cc02d7e2SAndroid Build Coastguard Worker        image_spec = DOCKERIMAGE_CURRENT_VERSIONS.get(docker_image_version, None)
70*cc02d7e2SAndroid Build Coastguard Worker        if not image_spec:
71*cc02d7e2SAndroid Build Coastguard Worker            fail("Version info for docker image '%s' not found in dockerimage_current_versions.bzl" % docker_image_version)
72*cc02d7e2SAndroid Build Coastguard Worker    else:
73*cc02d7e2SAndroid Build Coastguard Worker        fail("docker_image_version attribute not set for dockerized test '%s'" % name)
74*cc02d7e2SAndroid Build Coastguard Worker
75*cc02d7e2SAndroid Build Coastguard Worker    exec_properties = create_rbe_exec_properties_dict(
76*cc02d7e2SAndroid Build Coastguard Worker        labels = {
77*cc02d7e2SAndroid Build Coastguard Worker            "workload": "misc",
78*cc02d7e2SAndroid Build Coastguard Worker            "machine_size": "misc_large",
79*cc02d7e2SAndroid Build Coastguard Worker        },
80*cc02d7e2SAndroid Build Coastguard Worker        docker_network = "standard",
81*cc02d7e2SAndroid Build Coastguard Worker        container_image = image_spec,
82*cc02d7e2SAndroid Build Coastguard Worker        # TODO(jtattermusch): note that docker sandbox doesn't currently support "docker_run_as_root"
83*cc02d7e2SAndroid Build Coastguard Worker        docker_run_as_root = docker_run_as_root,
84*cc02d7e2SAndroid Build Coastguard Worker    )
85*cc02d7e2SAndroid Build Coastguard Worker
86*cc02d7e2SAndroid Build Coastguard Worker    # since the tests require special bazel args, only run them when explicitly requested
87*cc02d7e2SAndroid Build Coastguard Worker    tags = ["manual"] + tags
88*cc02d7e2SAndroid Build Coastguard Worker
89*cc02d7e2SAndroid Build Coastguard Worker    # TODO(jtattermusch): find a way to ensure that action can only run under docker sandbox or remotely
90*cc02d7e2SAndroid Build Coastguard Worker    # to avoid running it outside of a docker container by accident.
91*cc02d7e2SAndroid Build Coastguard Worker
92*cc02d7e2SAndroid Build Coastguard Worker    genrule_args = {
93*cc02d7e2SAndroid Build Coastguard Worker        "name": name,
94*cc02d7e2SAndroid Build Coastguard Worker        "cmd": cmd,
95*cc02d7e2SAndroid Build Coastguard Worker        "srcs": srcs,
96*cc02d7e2SAndroid Build Coastguard Worker        "tags": tags,
97*cc02d7e2SAndroid Build Coastguard Worker        "exec_compatible_with": exec_compatible_with,
98*cc02d7e2SAndroid Build Coastguard Worker        "exec_properties": exec_properties,
99*cc02d7e2SAndroid Build Coastguard Worker        "outs": outs,
100*cc02d7e2SAndroid Build Coastguard Worker    }
101*cc02d7e2SAndroid Build Coastguard Worker
102*cc02d7e2SAndroid Build Coastguard Worker    native.genrule(
103*cc02d7e2SAndroid Build Coastguard Worker        **genrule_args
104*cc02d7e2SAndroid Build Coastguard Worker    )
105*cc02d7e2SAndroid Build Coastguard Worker
106*cc02d7e2SAndroid Build Coastguard Workerdef grpc_run_tests_harness_test(name, args = [], data = [], size = "medium", timeout = None, tags = [], exec_compatible_with = [], flaky = None, docker_image_version = None, use_login_shell = None, prepare_script = None):
107*cc02d7e2SAndroid Build Coastguard Worker    """Execute an run_tests.py-harness style test under bazel.
108*cc02d7e2SAndroid Build Coastguard Worker
109*cc02d7e2SAndroid Build Coastguard Worker    Args:
110*cc02d7e2SAndroid Build Coastguard Worker        name: The name of the test.
111*cc02d7e2SAndroid Build Coastguard Worker        args: The args to supply to the test binary.
112*cc02d7e2SAndroid Build Coastguard Worker        data: Data dependencies.
113*cc02d7e2SAndroid Build Coastguard Worker        size: The size of the test.
114*cc02d7e2SAndroid Build Coastguard Worker        timeout: The test timeout.
115*cc02d7e2SAndroid Build Coastguard Worker        tags: The tags for the test.
116*cc02d7e2SAndroid Build Coastguard Worker        exec_compatible_with: A list of constraint values that must be
117*cc02d7e2SAndroid Build Coastguard Worker            satisifed for the platform.
118*cc02d7e2SAndroid Build Coastguard Worker        flaky: Whether this test is flaky.
119*cc02d7e2SAndroid Build Coastguard Worker        docker_image_version: The docker .current_version file to use for docker containerization.
120*cc02d7e2SAndroid Build Coastguard Worker        use_login_shell: If True, the run_tests.py command will run under a login shell.
121*cc02d7e2SAndroid Build Coastguard Worker        prepare_script: Optional script that will be sourced before run_tests.py runs.
122*cc02d7e2SAndroid Build Coastguard Worker    """
123*cc02d7e2SAndroid Build Coastguard Worker
124*cc02d7e2SAndroid Build Coastguard Worker    data = [
125*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz",
126*cc02d7e2SAndroid Build Coastguard Worker    ] + data
127*cc02d7e2SAndroid Build Coastguard Worker
128*cc02d7e2SAndroid Build Coastguard Worker    args = [
129*cc02d7e2SAndroid Build Coastguard Worker        "$(location //tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz)",
130*cc02d7e2SAndroid Build Coastguard Worker    ] + args
131*cc02d7e2SAndroid Build Coastguard Worker
132*cc02d7e2SAndroid Build Coastguard Worker    srcs = [
133*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_run_tests_harness_test.sh",
134*cc02d7e2SAndroid Build Coastguard Worker    ]
135*cc02d7e2SAndroid Build Coastguard Worker
136*cc02d7e2SAndroid Build Coastguard Worker    env = {}
137*cc02d7e2SAndroid Build Coastguard Worker
138*cc02d7e2SAndroid Build Coastguard Worker    if use_login_shell:
139*cc02d7e2SAndroid Build Coastguard Worker        env["GRPC_RUNTESTS_USE_LOGIN_SHELL"] = "1"
140*cc02d7e2SAndroid Build Coastguard Worker
141*cc02d7e2SAndroid Build Coastguard Worker    if prepare_script:
142*cc02d7e2SAndroid Build Coastguard Worker        data = data + [prepare_script]
143*cc02d7e2SAndroid Build Coastguard Worker        env["GRPC_RUNTESTS_PREPARE_SCRIPT"] = "$(location " + prepare_script + ")"
144*cc02d7e2SAndroid Build Coastguard Worker
145*cc02d7e2SAndroid Build Coastguard Worker    # Enable ccache by default. This is important for speeding up the C++ cmake build,
146*cc02d7e2SAndroid Build Coastguard Worker    # which isn't very efficient and tends to recompile some source files multiple times.
147*cc02d7e2SAndroid Build Coastguard Worker    # Even though only the local disk cache is enabled (local to the docker container,
148*cc02d7e2SAndroid Build Coastguard Worker    # so will be thrown away after the bazel actions finishes), ccache still speeds up
149*cc02d7e2SAndroid Build Coastguard Worker    # the C++ build significantly.
150*cc02d7e2SAndroid Build Coastguard Worker    # TODO(jtattermusch): find a cleaner way to toggle ccache for builds.
151*cc02d7e2SAndroid Build Coastguard Worker    env["GRPC_BUILD_ENABLE_CCACHE"] = "true"
152*cc02d7e2SAndroid Build Coastguard Worker
153*cc02d7e2SAndroid Build Coastguard Worker    _dockerized_sh_test(name = name, srcs = srcs, args = args, data = data, size = size, timeout = timeout, tags = tags, exec_compatible_with = exec_compatible_with, flaky = flaky, docker_image_version = docker_image_version, env = env)
154*cc02d7e2SAndroid Build Coastguard Worker
155*cc02d7e2SAndroid Build Coastguard Workerdef grpc_run_bazel_distribtest_test(name, args = [], data = [], size = "medium", timeout = None, tags = [], exec_compatible_with = [], flaky = None, docker_image_version = None):
156*cc02d7e2SAndroid Build Coastguard Worker    """Execute bazel distribtest under bazel (an entire bazel build/test will run in a container as a single bazel action)
157*cc02d7e2SAndroid Build Coastguard Worker
158*cc02d7e2SAndroid Build Coastguard Worker    Args:
159*cc02d7e2SAndroid Build Coastguard Worker        name: The name of the test.
160*cc02d7e2SAndroid Build Coastguard Worker        args: The args to supply to the test binary.
161*cc02d7e2SAndroid Build Coastguard Worker        data: Data dependencies.
162*cc02d7e2SAndroid Build Coastguard Worker        size: The size of the test.
163*cc02d7e2SAndroid Build Coastguard Worker        timeout: The test timeout.
164*cc02d7e2SAndroid Build Coastguard Worker        tags: The tags for the test.
165*cc02d7e2SAndroid Build Coastguard Worker        exec_compatible_with: A list of constraint values that must be
166*cc02d7e2SAndroid Build Coastguard Worker            satisifed for the platform.
167*cc02d7e2SAndroid Build Coastguard Worker        flaky: Whether this test is flaky.
168*cc02d7e2SAndroid Build Coastguard Worker        docker_image_version: The docker .current_version file to use for docker containerization.
169*cc02d7e2SAndroid Build Coastguard Worker    """
170*cc02d7e2SAndroid Build Coastguard Worker
171*cc02d7e2SAndroid Build Coastguard Worker    data = [
172*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz",
173*cc02d7e2SAndroid Build Coastguard Worker    ] + data
174*cc02d7e2SAndroid Build Coastguard Worker
175*cc02d7e2SAndroid Build Coastguard Worker    args = [
176*cc02d7e2SAndroid Build Coastguard Worker        "$(location //tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz)",
177*cc02d7e2SAndroid Build Coastguard Worker    ] + args
178*cc02d7e2SAndroid Build Coastguard Worker
179*cc02d7e2SAndroid Build Coastguard Worker    srcs = [
180*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_run_bazel_distribtest_test.sh",
181*cc02d7e2SAndroid Build Coastguard Worker    ]
182*cc02d7e2SAndroid Build Coastguard Worker    env = {}
183*cc02d7e2SAndroid Build Coastguard Worker    _dockerized_sh_test(name = name, srcs = srcs, args = args, data = data, size = size, timeout = timeout, tags = tags, exec_compatible_with = exec_compatible_with, flaky = flaky, docker_image_version = docker_image_version, env = env)
184*cc02d7e2SAndroid Build Coastguard Worker
185*cc02d7e2SAndroid Build Coastguard Workerdef grpc_run_cpp_distribtest_test(name, args = [], data = [], size = "medium", timeout = None, tags = [], exec_compatible_with = [], flaky = None, docker_image_version = None):
186*cc02d7e2SAndroid Build Coastguard Worker    """Execute an C++ distribtest under bazel.
187*cc02d7e2SAndroid Build Coastguard Worker
188*cc02d7e2SAndroid Build Coastguard Worker    Args:
189*cc02d7e2SAndroid Build Coastguard Worker        name: The name of the test.
190*cc02d7e2SAndroid Build Coastguard Worker        args: The args to supply to the test binary.
191*cc02d7e2SAndroid Build Coastguard Worker        data: Data dependencies.
192*cc02d7e2SAndroid Build Coastguard Worker        size: The size of the test.
193*cc02d7e2SAndroid Build Coastguard Worker        timeout: The test timeout.
194*cc02d7e2SAndroid Build Coastguard Worker        tags: The tags for the test.
195*cc02d7e2SAndroid Build Coastguard Worker        exec_compatible_with: A list of constraint values that must be
196*cc02d7e2SAndroid Build Coastguard Worker            satisifed for the platform.
197*cc02d7e2SAndroid Build Coastguard Worker        flaky: Whether this test is flaky.
198*cc02d7e2SAndroid Build Coastguard Worker        docker_image_version: The docker .current_version file to use for docker containerization.
199*cc02d7e2SAndroid Build Coastguard Worker    """
200*cc02d7e2SAndroid Build Coastguard Worker
201*cc02d7e2SAndroid Build Coastguard Worker    data = [
202*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz",
203*cc02d7e2SAndroid Build Coastguard Worker    ] + data
204*cc02d7e2SAndroid Build Coastguard Worker
205*cc02d7e2SAndroid Build Coastguard Worker    args = [
206*cc02d7e2SAndroid Build Coastguard Worker        "$(location //tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz)",
207*cc02d7e2SAndroid Build Coastguard Worker    ] + args
208*cc02d7e2SAndroid Build Coastguard Worker
209*cc02d7e2SAndroid Build Coastguard Worker    srcs = [
210*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_run_cpp_distribtest_test.sh",
211*cc02d7e2SAndroid Build Coastguard Worker    ]
212*cc02d7e2SAndroid Build Coastguard Worker
213*cc02d7e2SAndroid Build Coastguard Worker    # TODO(jtattermusch): revisit running docker as root (but currently some distribtests need to install stuff inside the docker container)
214*cc02d7e2SAndroid Build Coastguard Worker    env = {}
215*cc02d7e2SAndroid Build Coastguard Worker    _dockerized_sh_test(name = name, srcs = srcs, args = args, data = data, size = size, timeout = timeout, tags = tags, exec_compatible_with = exec_compatible_with, flaky = flaky, docker_image_version = docker_image_version, env = env, docker_run_as_root = True)
216*cc02d7e2SAndroid Build Coastguard Worker
217*cc02d7e2SAndroid Build Coastguard Workerdef grpc_run_simple_command_test(name, args = [], data = [], size = "medium", timeout = None, tags = [], exec_compatible_with = [], flaky = None, docker_image_version = None):
218*cc02d7e2SAndroid Build Coastguard Worker    """Execute the specified test command under grpc workspace (and under a docker container)
219*cc02d7e2SAndroid Build Coastguard Worker
220*cc02d7e2SAndroid Build Coastguard Worker    Args:
221*cc02d7e2SAndroid Build Coastguard Worker        name: The name of the test.
222*cc02d7e2SAndroid Build Coastguard Worker        args: The command to run.
223*cc02d7e2SAndroid Build Coastguard Worker        data: Data dependencies.
224*cc02d7e2SAndroid Build Coastguard Worker        size: The size of the test.
225*cc02d7e2SAndroid Build Coastguard Worker        timeout: The test timeout.
226*cc02d7e2SAndroid Build Coastguard Worker        tags: The tags for the test.
227*cc02d7e2SAndroid Build Coastguard Worker        exec_compatible_with: A list of constraint values that must be
228*cc02d7e2SAndroid Build Coastguard Worker            satisifed for the platform.
229*cc02d7e2SAndroid Build Coastguard Worker        flaky: Whether this test is flaky.
230*cc02d7e2SAndroid Build Coastguard Worker        docker_image_version: The docker .current_version file to use for docker containerization.
231*cc02d7e2SAndroid Build Coastguard Worker    """
232*cc02d7e2SAndroid Build Coastguard Worker
233*cc02d7e2SAndroid Build Coastguard Worker    data = [
234*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz",
235*cc02d7e2SAndroid Build Coastguard Worker    ] + data
236*cc02d7e2SAndroid Build Coastguard Worker
237*cc02d7e2SAndroid Build Coastguard Worker    args = [
238*cc02d7e2SAndroid Build Coastguard Worker        "$(location //tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz)",
239*cc02d7e2SAndroid Build Coastguard Worker    ] + args
240*cc02d7e2SAndroid Build Coastguard Worker
241*cc02d7e2SAndroid Build Coastguard Worker    srcs = [
242*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_run_simple_command_test.sh",
243*cc02d7e2SAndroid Build Coastguard Worker    ]
244*cc02d7e2SAndroid Build Coastguard Worker
245*cc02d7e2SAndroid Build Coastguard Worker    env = {}
246*cc02d7e2SAndroid Build Coastguard Worker    _dockerized_sh_test(name = name, srcs = srcs, args = args, data = data, size = size, timeout = timeout, tags = tags, exec_compatible_with = exec_compatible_with, flaky = flaky, docker_image_version = docker_image_version, env = env, docker_run_as_root = False)
247*cc02d7e2SAndroid Build Coastguard Worker
248*cc02d7e2SAndroid Build Coastguard Workerdef grpc_build_artifact_task(name, timeout = None, artifact_deps = [], tags = [], exec_compatible_with = [], flaky = None, docker_image_version = None, build_script = None):
249*cc02d7e2SAndroid Build Coastguard Worker    """Execute a build artifact task and a corresponding 'build test'.
250*cc02d7e2SAndroid Build Coastguard Worker
251*cc02d7e2SAndroid Build Coastguard Worker    The artifact is built by a genrule that always succeeds (Even if the underlying build fails)
252*cc02d7e2SAndroid Build Coastguard Worker    and an sh_test (with "_build_test" suffix) that presents the result of the artifact build
253*cc02d7e2SAndroid Build Coastguard Worker    in the result UI (by displaying the the build status, the log, and artifacts produced).
254*cc02d7e2SAndroid Build Coastguard Worker    Such layout helps to easily build artifacts and run distribtests that depend on other artifacts,
255*cc02d7e2SAndroid Build Coastguard Worker    while making the test results well structured and easy to interpret.
256*cc02d7e2SAndroid Build Coastguard Worker
257*cc02d7e2SAndroid Build Coastguard Worker    Args:
258*cc02d7e2SAndroid Build Coastguard Worker        name: The name of the target.
259*cc02d7e2SAndroid Build Coastguard Worker        timeout: The test timeout for the build.
260*cc02d7e2SAndroid Build Coastguard Worker        artifact_deps: List of dependencies on artifacts built by another grpc_build_artifact_task.
261*cc02d7e2SAndroid Build Coastguard Worker        tags: The tags for the target.
262*cc02d7e2SAndroid Build Coastguard Worker        exec_compatible_with: A list of constraint values that must be
263*cc02d7e2SAndroid Build Coastguard Worker            satisifed for the platform.
264*cc02d7e2SAndroid Build Coastguard Worker        flaky: Whether this artifact build is flaky.
265*cc02d7e2SAndroid Build Coastguard Worker        docker_image_version: The docker .current_version file to use for docker containerization.
266*cc02d7e2SAndroid Build Coastguard Worker        build_script: The script that builds the aritfacts.
267*cc02d7e2SAndroid Build Coastguard Worker    """
268*cc02d7e2SAndroid Build Coastguard Worker
269*cc02d7e2SAndroid Build Coastguard Worker    out_exitcode_file = str(name + "_exit_code")
270*cc02d7e2SAndroid Build Coastguard Worker    out_build_log = str(name + "_build_log.txt")
271*cc02d7e2SAndroid Build Coastguard Worker    out_archive_name = str(name + ".tar.gz")
272*cc02d7e2SAndroid Build Coastguard Worker
273*cc02d7e2SAndroid Build Coastguard Worker    genrule_outs = [
274*cc02d7e2SAndroid Build Coastguard Worker        out_exitcode_file,
275*cc02d7e2SAndroid Build Coastguard Worker        out_build_log,
276*cc02d7e2SAndroid Build Coastguard Worker        out_archive_name,
277*cc02d7e2SAndroid Build Coastguard Worker    ]
278*cc02d7e2SAndroid Build Coastguard Worker
279*cc02d7e2SAndroid Build Coastguard Worker    genrule_srcs = [
280*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_build_artifact_task.sh",
281*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz",
282*cc02d7e2SAndroid Build Coastguard Worker        build_script,
283*cc02d7e2SAndroid Build Coastguard Worker    ]
284*cc02d7e2SAndroid Build Coastguard Worker
285*cc02d7e2SAndroid Build Coastguard Worker    cmd = "$(location //tools/bazelify_tests:grpc_build_artifact_task.sh) $(location //tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz) $(location " + build_script + ") $(location " + out_exitcode_file + ") $(location " + out_build_log + ") $(location " + out_archive_name + ")"
286*cc02d7e2SAndroid Build Coastguard Worker
287*cc02d7e2SAndroid Build Coastguard Worker    # for each artifact task we depends on, use the correponding tar.gz as extra src and pass its location as an extra cmdline arg.
288*cc02d7e2SAndroid Build Coastguard Worker    for dep in artifact_deps:
289*cc02d7e2SAndroid Build Coastguard Worker        dep_archive_name = str(dep + ".tar.gz")
290*cc02d7e2SAndroid Build Coastguard Worker        cmd = cmd + " $(location " + dep_archive_name + ")"
291*cc02d7e2SAndroid Build Coastguard Worker        genrule_srcs.append(dep_archive_name)
292*cc02d7e2SAndroid Build Coastguard Worker
293*cc02d7e2SAndroid Build Coastguard Worker    _dockerized_genrule(name = name, cmd = cmd, outs = genrule_outs, srcs = genrule_srcs, tags = tags, exec_compatible_with = exec_compatible_with, docker_image_version = docker_image_version, docker_run_as_root = False)
294*cc02d7e2SAndroid Build Coastguard Worker
295*cc02d7e2SAndroid Build Coastguard Worker    # The genrule above always succeeds (even if the underlying build fails), so that we can create rules that depend
296*cc02d7e2SAndroid Build Coastguard Worker    # on multiple artifact builds (of which some can fail). The actual build status (exitcode) and the log of the build
297*cc02d7e2SAndroid Build Coastguard Worker    # will be reported by an associated sh_test (that gets displayed in the UI in a much nicer way than a genrule).
298*cc02d7e2SAndroid Build Coastguard Worker    # Note that in bazel you cannot declare a test that has declared outputs and you also cannot make other rules
299*cc02d7e2SAndroid Build Coastguard Worker    # depend on a test - which is the reason why we need a separate genrule to represent the build itself.
300*cc02d7e2SAndroid Build Coastguard Worker    test_name = str(name + "_build_test")
301*cc02d7e2SAndroid Build Coastguard Worker    test_srcs = [
302*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_build_artifact_task_build_test.sh",
303*cc02d7e2SAndroid Build Coastguard Worker    ]
304*cc02d7e2SAndroid Build Coastguard Worker    test_data = [
305*cc02d7e2SAndroid Build Coastguard Worker        out_exitcode_file,
306*cc02d7e2SAndroid Build Coastguard Worker        out_build_log,
307*cc02d7e2SAndroid Build Coastguard Worker        out_archive_name,
308*cc02d7e2SAndroid Build Coastguard Worker    ]
309*cc02d7e2SAndroid Build Coastguard Worker    test_env = {}
310*cc02d7e2SAndroid Build Coastguard Worker    test_args = [
311*cc02d7e2SAndroid Build Coastguard Worker        "$(location " + out_exitcode_file + ")",
312*cc02d7e2SAndroid Build Coastguard Worker        "$(location " + out_build_log + ")",
313*cc02d7e2SAndroid Build Coastguard Worker        "$(location " + out_archive_name + ")",
314*cc02d7e2SAndroid Build Coastguard Worker    ]
315*cc02d7e2SAndroid Build Coastguard Worker    _dockerized_sh_test(name = test_name, srcs = test_srcs, args = test_args, data = test_data, size = "small", tags = tags, exec_compatible_with = exec_compatible_with, flaky = flaky, docker_image_version = docker_image_version, env = test_env, docker_run_as_root = False)
316*cc02d7e2SAndroid Build Coastguard Worker
317*cc02d7e2SAndroid Build Coastguard Workerdef grpc_run_distribtest_test(name, artifact_deps = [], size = "medium", timeout = None, tags = [], exec_compatible_with = [], flaky = None, docker_image_version = None, build_script = None, docker_run_as_root = False):
318*cc02d7e2SAndroid Build Coastguard Worker    """Run a distribtest for a previously built artifact/package
319*cc02d7e2SAndroid Build Coastguard Worker
320*cc02d7e2SAndroid Build Coastguard Worker    Args:
321*cc02d7e2SAndroid Build Coastguard Worker        name: The name of the test.
322*cc02d7e2SAndroid Build Coastguard Worker        artifact_deps: List of dependencies on artifacts built by another grpc_build_artifact_task.
323*cc02d7e2SAndroid Build Coastguard Worker        size: The size of the test.
324*cc02d7e2SAndroid Build Coastguard Worker        timeout: The test timeout.
325*cc02d7e2SAndroid Build Coastguard Worker        tags: The tags for the test.
326*cc02d7e2SAndroid Build Coastguard Worker        exec_compatible_with: A list of constraint values that must be
327*cc02d7e2SAndroid Build Coastguard Worker            satisifed for the platform.
328*cc02d7e2SAndroid Build Coastguard Worker        flaky: Whether this test is flaky.
329*cc02d7e2SAndroid Build Coastguard Worker        docker_image_version: The docker .current_version file to use for docker containerization.
330*cc02d7e2SAndroid Build Coastguard Worker        build_script: The script that runs the test.
331*cc02d7e2SAndroid Build Coastguard Worker        docker_run_as_root: If True, the test will run under docker as root.
332*cc02d7e2SAndroid Build Coastguard Worker    """
333*cc02d7e2SAndroid Build Coastguard Worker
334*cc02d7e2SAndroid Build Coastguard Worker    data = [
335*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz",
336*cc02d7e2SAndroid Build Coastguard Worker        build_script,
337*cc02d7e2SAndroid Build Coastguard Worker    ]
338*cc02d7e2SAndroid Build Coastguard Worker
339*cc02d7e2SAndroid Build Coastguard Worker    args = [
340*cc02d7e2SAndroid Build Coastguard Worker        "$(location //tools/bazelify_tests:grpc_repo_archive_with_submodules.tar.gz)",
341*cc02d7e2SAndroid Build Coastguard Worker        "$(location " + build_script + ")",
342*cc02d7e2SAndroid Build Coastguard Worker    ]
343*cc02d7e2SAndroid Build Coastguard Worker
344*cc02d7e2SAndroid Build Coastguard Worker    # for each artifact task we depends on, use the correponding tar.gz as extra data item and pass its location as an extra arg.
345*cc02d7e2SAndroid Build Coastguard Worker    for dep in artifact_deps:
346*cc02d7e2SAndroid Build Coastguard Worker        dep_archive_name = str(dep + ".tar.gz")
347*cc02d7e2SAndroid Build Coastguard Worker        args.append("$(location " + dep_archive_name + ")")
348*cc02d7e2SAndroid Build Coastguard Worker        data.append(dep_archive_name)
349*cc02d7e2SAndroid Build Coastguard Worker
350*cc02d7e2SAndroid Build Coastguard Worker    srcs = [
351*cc02d7e2SAndroid Build Coastguard Worker        "//tools/bazelify_tests:grpc_run_distribtest_test.sh",
352*cc02d7e2SAndroid Build Coastguard Worker    ]
353*cc02d7e2SAndroid Build Coastguard Worker
354*cc02d7e2SAndroid Build Coastguard Worker    env = {}
355*cc02d7e2SAndroid Build Coastguard Worker    _dockerized_sh_test(name = name, srcs = srcs, args = args, data = data, size = size, timeout = timeout, tags = tags, exec_compatible_with = exec_compatible_with, flaky = flaky, docker_image_version = docker_image_version, env = env, docker_run_as_root = docker_run_as_root)
356