Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Test controller. |
| 6 | |
| 7 | Handles all testing related functionality, it is not itself a test. |
| 8 | """ |
| 9 | |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 10 | import functools |
Derek Beckett | 963901e | 2022-04-19 11:38:36 -0700 | [diff] [blame] | 11 | import logging |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 12 | import os |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 13 | import string |
Sean McAllister | 3834fef | 2021-10-08 15:45:18 -0600 | [diff] [blame] | 14 | import subprocess |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 15 | import traceback |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 16 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 17 | from chromite.third_party.google.protobuf import json_format |
| 18 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 19 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 20 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 21 | from chromite.api import validate |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 22 | from chromite.api.controller import controller_util |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 23 | from chromite.api.gen.chromite.api import test_pb2 |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 24 | from chromite.api.gen.chromiumos import common_pb2 |
Sean McAllister | 3834fef | 2021-10-08 15:45:18 -0600 | [diff] [blame] | 25 | from chromite.api.gen.chromiumos.build.api import container_metadata_pb2 |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 26 | from chromite.api.metrics import deserialize_metrics_log |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 27 | from chromite.lib import build_target_lib |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 28 | from chromite.lib import chroot_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 29 | from chromite.lib import constants |
Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 30 | from chromite.lib import cros_build_lib |
Ram Chandrasekar | e08e3ba | 2022-04-04 21:42:27 +0000 | [diff] [blame] | 31 | from chromite.lib import goma_lib |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 32 | from chromite.lib import metrics_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 33 | from chromite.lib import osutils |
Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 34 | from chromite.lib import sysroot_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 35 | from chromite.lib.parser import package_info |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 36 | from chromite.service import packages as packages_service |
Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 37 | from chromite.service import test |
| 38 | |
| 39 | |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 40 | @faux.empty_success |
| 41 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 42 | def DebugInfoTest(input_proto, _output_proto, config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | """Run the debug info tests.""" |
| 44 | sysroot_path = input_proto.sysroot.path |
| 45 | target_name = input_proto.sysroot.build_target.name |
Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | if not sysroot_path: |
| 48 | if target_name: |
| 49 | sysroot_path = build_target_lib.get_default_sysroot_path( |
| 50 | target_name |
| 51 | ) |
| 52 | else: |
| 53 | cros_build_lib.Die( |
| 54 | "The sysroot path or the sysroot's build target name " |
| 55 | "must be provided." |
| 56 | ) |
| 57 | |
| 58 | # We could get away with out this, but it's a cheap check. |
| 59 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 60 | if not sysroot.Exists(): |
| 61 | cros_build_lib.Die("The provided sysroot does not exist.") |
| 62 | |
| 63 | if config.validate_only: |
| 64 | return controller.RETURN_CODE_VALID_INPUT |
| 65 | |
| 66 | if test.DebugInfoTest(sysroot_path): |
| 67 | return controller.RETURN_CODE_SUCCESS |
Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 68 | else: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 70 | |
| 71 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 72 | def _BuildTargetUnitTestFailedResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | """Add failed packages to a failed response.""" |
| 74 | packages = ["foo/bar", "cat/pkg"] |
| 75 | for pkg in packages: |
| 76 | pkg_info = package_info.parse(pkg) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | failed_pkg_data_msg = output_proto.failed_package_data.add() |
| 78 | controller_util.serialize_package_info( |
| 79 | pkg_info, failed_pkg_data_msg.name |
| 80 | ) |
| 81 | failed_pkg_data_msg.log_path.path = "/path/to/%s/log" % pkg |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 82 | |
| 83 | |
Alex Klein | 0aecf47 | 2022-05-23 10:48:45 -0600 | [diff] [blame] | 84 | @faux.empty_success |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 85 | @faux.error(_BuildTargetUnitTestFailedResponse) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 86 | @validate.require_each("packages", ["category", "package_name"]) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 87 | @validate.validation_complete |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 88 | @metrics_lib.collect_metrics |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 89 | def BuildTargetUnitTest(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | """Run a build target's ebuild unit tests.""" |
| 91 | # Method flags. |
| 92 | # An empty sysroot means build packages was not run. This is used for |
| 93 | # certain boards that need to use prebuilts (e.g. grunt's unittest-only). |
| 94 | was_built = not input_proto.flags.empty_sysroot |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | # Packages to be tested. |
Alex Klein | d3b8404 | 2023-05-19 14:43:59 -0600 | [diff] [blame] | 97 | packages = [ |
| 98 | controller_util.deserialize_package_info(x).atom |
| 99 | for x in input_proto.packages |
| 100 | ] |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 101 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 102 | # Skipped tests. |
Alex Klein | d3b8404 | 2023-05-19 14:43:59 -0600 | [diff] [blame] | 103 | blocklist = [ |
| 104 | controller_util.deserialize_package_info(x).atom |
| 105 | for x in input_proto.package_blocklist |
| 106 | ] |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 107 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | # Allow call to filter out non-cros_workon packages from the input packages. |
| 109 | filter_only_cros_workon = input_proto.flags.filter_only_cros_workon |
Navil Perez | 43fb45d | 2021-05-14 20:34:24 +0000 | [diff] [blame] | 110 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 111 | # Allow call to succeed if no tests were found. |
| 112 | testable_packages_optional = input_proto.flags.testable_packages_optional |
Navil Perez | 19dc479 | 2020-09-10 19:06:17 +0000 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 115 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | code_coverage = input_proto.flags.code_coverage |
Srinivas Hegde | 0058b8b | 2022-09-12 22:31:10 +0000 | [diff] [blame] | 117 | rust_code_coverage = input_proto.flags.rust_code_coverage |
David Burger | a9c1187 | 2020-07-29 13:32:02 -0600 | [diff] [blame] | 118 | |
Ryo Hashimoto | 009e9dd | 2023-09-01 15:56:23 +0900 | [diff] [blame] | 119 | bazel = input_proto.flags.bazel |
| 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | sysroot = sysroot_lib.Sysroot(build_target.root) |
Lizzy Presland | 4feb237 | 2022-01-20 05:16:30 +0000 | [diff] [blame] | 122 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 123 | result = test.BuildTargetUnitTest( |
| 124 | build_target, |
| 125 | packages=packages, |
| 126 | blocklist=blocklist, |
| 127 | was_built=was_built, |
| 128 | code_coverage=code_coverage, |
Srinivas Hegde | 0058b8b | 2022-09-12 22:31:10 +0000 | [diff] [blame] | 129 | rust_code_coverage=rust_code_coverage, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 130 | testable_packages_optional=testable_packages_optional, |
| 131 | filter_only_cros_workon=filter_only_cros_workon, |
Ryo Hashimoto | 009e9dd | 2023-09-01 15:56:23 +0900 | [diff] [blame] | 132 | bazel=bazel, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | ) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 134 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 135 | if not result.success: |
| 136 | # Record all failed packages and retrieve log locations. |
| 137 | controller_util.retrieve_package_log_paths( |
| 138 | result.failed_pkgs, output_proto, sysroot |
| 139 | ) |
| 140 | if result.failed_pkgs: |
| 141 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
| 142 | else: |
| 143 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | deserialize_metrics_log(output_proto.events, prefix=build_target.name) |
Alex Klein | e3fc3ca | 2019-04-30 16:20:55 -0600 | [diff] [blame] | 146 | |
| 147 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 148 | SRC_DIR = os.path.join(constants.SOURCE_ROOT, "src") |
| 149 | PLATFORM_DEV_DIR = os.path.join(SRC_DIR, "platform/dev") |
| 150 | TEST_SERVICE_DIR = os.path.join(PLATFORM_DEV_DIR, "src/chromiumos/test") |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 151 | |
| 152 | |
C Shapiro | e15660b | 2021-06-18 12:49:37 -0500 | [diff] [blame] | 153 | def _BuildTestServiceContainersResponse(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | """Fake success response""" |
| 155 | # pylint: disable=unused-argument |
| 156 | output_proto.results.append( |
| 157 | test_pb2.TestServiceContainerBuildResult( |
| 158 | success=test_pb2.TestServiceContainerBuildResult.Success() |
| 159 | ) |
| 160 | ) |
C Shapiro | e15660b | 2021-06-18 12:49:37 -0500 | [diff] [blame] | 161 | |
| 162 | |
| 163 | def _BuildTestServiceContainersFailedResponse( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 164 | _input_proto, output_proto, _config |
| 165 | ): |
| 166 | """Fake failure response""" |
C Shapiro | e15660b | 2021-06-18 12:49:37 -0500 | [diff] [blame] | 167 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 168 | # pylint: disable=unused-argument |
| 169 | output_proto.results.append( |
| 170 | test_pb2.TestServiceContainerBuildResult( |
| 171 | failure=test_pb2.TestServiceContainerBuildResult.Failure( |
| 172 | error_message="fake error" |
| 173 | ) |
| 174 | ) |
| 175 | ) |
C Shapiro | e15660b | 2021-06-18 12:49:37 -0500 | [diff] [blame] | 176 | |
| 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | @validate.constraint("valid docker tag") |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 179 | def _ValidDockerTag(tag): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 180 | """Check that a string meets requirements for Docker tag naming.""" |
| 181 | # Tags can't start with period or dash |
| 182 | if tag[0] in ".-": |
| 183 | return "tag can't begin with '.' or '-'" |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 184 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 185 | # Tags can only consist of [a-zA-Z0-9-_.] |
| 186 | allowed_chars = set(string.ascii_letters + string.digits + "-_.") |
| 187 | invalid_chars = set(tag) - allowed_chars |
| 188 | if invalid_chars: |
| 189 | return f'saw one or more invalid characters: [{"".join(invalid_chars)}]' |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 190 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 191 | # Finally, max tag length is 128 characters |
| 192 | if len(tag) > 128: |
| 193 | return "maximum tag length is 128 characters" |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 194 | |
| 195 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 196 | @validate.constraint("valid docker label key") |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 197 | def _ValidDockerLabelKey(key): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 198 | """Check that a string meets requirements for Docker tag naming.""" |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 199 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 200 | # Label keys should start and end with a lowercase letter |
| 201 | lowercase = set(string.ascii_lowercase) |
| 202 | if not (key[0] in lowercase and key[-1] in lowercase): |
| 203 | return "label key doesn't start and end with lowercase letter" |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 204 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 205 | # Label keys can have lower-case alphanumeric characters, period and dash |
| 206 | allowed_chars = set(string.ascii_lowercase + string.digits + "-.") |
| 207 | invalid_chars = set(key) - allowed_chars |
| 208 | if invalid_chars: |
| 209 | return f'saw one or more invalid characters: [{"".join(invalid_chars)}]' |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 210 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 211 | # Repeated . and - aren't allowed |
| 212 | for char in ".-": |
| 213 | if char * 2 in key: |
| 214 | return f"'{char}' can't be repeated in label key" |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 215 | |
| 216 | |
C Shapiro | e15660b | 2021-06-18 12:49:37 -0500 | [diff] [blame] | 217 | @faux.success(_BuildTestServiceContainersResponse) |
| 218 | @faux.error(_BuildTestServiceContainersFailedResponse) |
Brian Norris | 7ac08e2 | 2023-06-15 11:03:01 -0700 | [diff] [blame] | 219 | @validate.require("build_target.name", "chroot.path", "chroot.out_path") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 220 | @validate.check_constraint("tags", _ValidDockerTag) |
| 221 | @validate.check_constraint("labels", _ValidDockerLabelKey) |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 222 | @validate.validation_complete |
Sean McAllister | 15fa833 | 2021-09-27 12:24:12 -0600 | [diff] [blame] | 223 | def BuildTestServiceContainers( |
| 224 | input_proto: test_pb2.BuildTestServiceContainersRequest, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 225 | output_proto: test_pb2.BuildTestServiceContainersResponse, |
| 226 | _config, |
| 227 | ): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 228 | """Build docker containers for all test services and push them to gcr.io.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 229 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
| 230 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 231 | sysroot = sysroot_lib.Sysroot(build_target.root) |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 232 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 233 | tags = ",".join(input_proto.tags) |
| 234 | labels = (f"{key}={value}" for key, value in input_proto.labels.items()) |
Sean McAllister | 15fa833 | 2021-09-27 12:24:12 -0600 | [diff] [blame] | 235 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 236 | build_script = os.path.join( |
| 237 | TEST_SERVICE_DIR, "python/src/docker_libs/cli/build-dockerimages.py" |
| 238 | ) |
| 239 | human_name = "Service Builder" |
Sean McAllister | 15fa833 | 2021-09-27 12:24:12 -0600 | [diff] [blame] | 240 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 241 | with osutils.TempDir(prefix="test_container") as tempdir: |
| 242 | result_file = "metadata.jsonpb" |
| 243 | output_path = os.path.join(tempdir, result_file) |
| 244 | # Note that we use an output file instead of stdout to avoid any issues |
| 245 | # with maintaining stdout hygiene. Stdout and stderr are combined to |
| 246 | # form the error log in response to any errors. |
Brian Norris | 7ac08e2 | 2023-06-15 11:03:01 -0700 | [diff] [blame] | 247 | cmd = [ |
| 248 | build_script, |
| 249 | chroot.path, |
| 250 | sysroot.path, |
| 251 | "--out-dir", |
| 252 | chroot.out_path, |
| 253 | ] |
Sean McAllister | e6a4ae2 | 2021-10-21 13:17:08 -0600 | [diff] [blame] | 254 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | if input_proto.HasField("repository"): |
| 256 | cmd += ["--host", input_proto.repository.hostname] |
| 257 | cmd += ["--project", input_proto.repository.project] |
Sean McAllister | e6a4ae2 | 2021-10-21 13:17:08 -0600 | [diff] [blame] | 258 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 259 | cmd += ["--tags", tags] |
| 260 | cmd += ["--output", output_path] |
Derek Beckett | b28b537 | 2022-04-15 13:08:32 -0700 | [diff] [blame] | 261 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 262 | # Translate generator to comma separated string. |
| 263 | ct_labels = ",".join(labels) |
| 264 | cmd += ["--labels", ct_labels] |
| 265 | cmd += ["--build_all"] |
| 266 | cmd += ["--upload"] |
Derek Beckett | cbe64c8 | 2022-05-05 15:00:18 -0700 | [diff] [blame] | 267 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 268 | cmd_result = cros_build_lib.run( |
| 269 | cmd, check=False, stderr=subprocess.STDOUT, stdout=True |
| 270 | ) |
Derek Beckett | a8d9fd2 | 2022-04-27 10:44:52 -0700 | [diff] [blame] | 271 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 272 | if cmd_result.returncode != 0: |
| 273 | # When failing, just record a fail response with the builder name. |
| 274 | logging.debug( |
| 275 | "%s build failed.\nStdout:\n%s\nStderr:\n%s", |
| 276 | human_name, |
| 277 | cmd_result.stdout, |
| 278 | cmd_result.stderr, |
| 279 | ) |
| 280 | result = test_pb2.TestServiceContainerBuildResult() |
| 281 | result.name = human_name |
| 282 | image_info = container_metadata_pb2.ContainerImageInfo() |
| 283 | result.failure.CopyFrom( |
| 284 | test_pb2.TestServiceContainerBuildResult.Failure( |
| 285 | error_message=cmd_result.stdout |
| 286 | ) |
| 287 | ) |
| 288 | output_proto.results.append(result) |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 289 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 290 | else: |
| 291 | logging.debug( |
| 292 | "%s build succeeded.\nStdout:\n%s\nStderr:\n%s", |
| 293 | human_name, |
| 294 | cmd_result.stdout, |
| 295 | cmd_result.stderr, |
| 296 | ) |
| 297 | files = os.listdir(tempdir) |
| 298 | # Iterate through the tempdir to output metadata files. |
| 299 | for file in files: |
| 300 | if result_file in file: |
| 301 | output_path = os.path.join(tempdir, file) |
Derek Beckett | 344b5a8 | 2022-05-09 17:21:45 -0700 | [diff] [blame] | 302 | |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 303 | # build-dockerimages.py will append the service name to |
| 304 | # outputfile with an underscore. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 305 | human_name = file.split("_")[-1] |
Derek Beckett | 344b5a8 | 2022-05-09 17:21:45 -0700 | [diff] [blame] | 306 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 307 | result = test_pb2.TestServiceContainerBuildResult() |
| 308 | result.name = human_name |
| 309 | image_info = container_metadata_pb2.ContainerImageInfo() |
| 310 | json_format.Parse(osutils.ReadFile(output_path), image_info) |
| 311 | result.success.CopyFrom( |
| 312 | test_pb2.TestServiceContainerBuildResult.Success( |
| 313 | image_info=image_info |
| 314 | ) |
| 315 | ) |
| 316 | output_proto.results.append(result) |
Derek Beckett | 344b5a8 | 2022-05-09 17:21:45 -0700 | [diff] [blame] | 317 | |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 318 | |
Michael Mortensen | 7a860eb | 2019-12-03 20:25:15 -0700 | [diff] [blame] | 319 | @faux.empty_success |
| 320 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 321 | @validate.validation_complete |
| 322 | def ChromiteUnitTest(_input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 323 | """Run the chromite unit tests.""" |
| 324 | if test.ChromiteUnitTest(): |
| 325 | return controller.RETURN_CODE_SUCCESS |
| 326 | else: |
| 327 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 328 | |
| 329 | |
Greg Edelston | f3fc8b6 | 2020-03-17 14:20:24 -0600 | [diff] [blame] | 330 | @faux.empty_success |
| 331 | @faux.empty_completed_unsuccessfully_error |
| 332 | @validate.validation_complete |
| 333 | def ChromitePytest(_input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 334 | """Run the chromite unit tests.""" |
| 335 | # TODO(vapier): Delete this stub. |
| 336 | return controller.RETURN_CODE_SUCCESS |
Greg Edelston | f3fc8b6 | 2020-03-17 14:20:24 -0600 | [diff] [blame] | 337 | |
| 338 | |
Sloan Johnson | 9fdd531 | 2022-03-02 00:55:26 +0000 | [diff] [blame] | 339 | @faux.empty_success |
| 340 | @faux.empty_completed_unsuccessfully_error |
| 341 | @validate.validation_complete |
| 342 | def RulesCrosUnitTest(_input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 343 | """Run the rules_cros unit tests.""" |
| 344 | if test.RulesCrosUnitTest(): |
| 345 | return controller.RETURN_CODE_SUCCESS |
| 346 | else: |
| 347 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Sloan Johnson | 9fdd531 | 2022-03-02 00:55:26 +0000 | [diff] [blame] | 348 | |
| 349 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 350 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 351 | @validate.require("sysroot.path", "sysroot.build_target.name", "chrome_root") |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 352 | @validate.validation_complete |
| 353 | def SimpleChromeWorkflowTest(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 354 | """Run SimpleChromeWorkflow tests.""" |
| 355 | if input_proto.goma_config.goma_dir: |
| 356 | chromeos_goma_dir = input_proto.goma_config.chromeos_goma_dir or None |
| 357 | goma = goma_lib.Goma( |
| 358 | input_proto.goma_config.goma_dir, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 359 | stage_name="BuildApiTestSimpleChrome", |
| 360 | chromeos_goma_dir=chromeos_goma_dir, |
| 361 | ) |
| 362 | else: |
| 363 | goma = None |
| 364 | return test.SimpleChromeWorkflowTest( |
| 365 | input_proto.sysroot.path, |
| 366 | input_proto.sysroot.build_target.name, |
| 367 | input_proto.chrome_root, |
| 368 | goma, |
| 369 | ) |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 370 | |
| 371 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 372 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 373 | @validate.require( |
| 374 | "build_target.name", "vm_path.path", "test_harness", "vm_tests" |
| 375 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 376 | @validate.validation_complete |
| 377 | def VmTest(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 378 | """Run VM tests.""" |
| 379 | build_target_name = input_proto.build_target.name |
| 380 | vm_path = input_proto.vm_path.path |
Alex Klein | c05f3d1 | 2019-05-29 14:16:21 -0600 | [diff] [blame] | 381 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 382 | test_harness = input_proto.test_harness |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 383 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 384 | vm_tests = input_proto.vm_tests |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 385 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 386 | cmd = [ |
| 387 | "cros_run_test", |
| 388 | "--debug", |
| 389 | "--no-display", |
| 390 | "--copy-on-write", |
| 391 | "--board", |
| 392 | build_target_name, |
| 393 | "--image-path", |
| 394 | vm_path, |
| 395 | "--%s" % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower(), |
| 396 | ] |
| 397 | cmd.extend(vm_test.pattern for vm_test in vm_tests) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 398 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 399 | if input_proto.ssh_options.port: |
| 400 | cmd.extend(["--ssh-port", str(input_proto.ssh_options.port)]) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 401 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 402 | if input_proto.ssh_options.private_key_path: |
| 403 | cmd.extend( |
| 404 | ["--private-key", input_proto.ssh_options.private_key_path.path] |
| 405 | ) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 406 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 407 | # TODO(evanhernandez): Find a nice way to pass test_that-args through |
| 408 | # the build API. Or obviate them. |
| 409 | if test_harness == test_pb2.VmTestRequest.AUTOTEST: |
| 410 | cmd.append("--test_that-args=--allow-chrome-crashes") |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 411 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 412 | with osutils.TempDir(prefix="vm-test-results.") as results_dir: |
| 413 | cmd.extend(["--results-dir", results_dir]) |
| 414 | cros_build_lib.run(cmd, kill_timeout=10 * 60) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 415 | |
| 416 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 417 | @faux.all_empty |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 418 | @validate.validation_complete |
| 419 | def CrosSigningTest(_input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | """Run the cros-signing unit tests.""" |
| 421 | test_runner = os.path.join( |
| 422 | constants.SOURCE_ROOT, "cros-signing", "signer", "run_tests.py" |
| 423 | ) |
| 424 | result = cros_build_lib.run([test_runner], check=False) |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 425 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 426 | return result.returncode |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 427 | |
| 428 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 429 | def GetArtifacts( |
| 430 | in_proto: common_pb2.ArtifactsByService.Test, |
| 431 | chroot: chroot_lib.Chroot, |
| 432 | sysroot_class: sysroot_lib.Sysroot, |
| 433 | build_target: build_target_lib.BuildTarget, |
| 434 | output_dir: str, |
| 435 | ) -> list: |
| 436 | """Builds and copies test artifacts to specified output_dir. |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 437 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 438 | Copies test artifacts to output_dir, returning a list of (output_dir: str) |
| 439 | paths to the desired files. |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 440 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 441 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 442 | in_proto: Proto request defining reqs. |
| 443 | chroot: The chroot class used for these artifacts. |
| 444 | sysroot_class: The sysroot class used for these artifacts. |
| 445 | build_target: The build target used for these artifacts. |
| 446 | output_dir: The path to write artifacts to. |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 447 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 448 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 449 | A list of dictionary mappings of ArtifactType to list of paths. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 450 | """ |
| 451 | generated = [] |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 452 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 453 | artifact_types = { |
| 454 | in_proto.ArtifactType.CODE_COVERAGE_LLVM_JSON: functools.partial( |
| 455 | test.BundleCodeCoverageLlvmJson, build_target.name |
| 456 | ), |
Srinivas Hegde | b6fe4ac | 2022-09-22 00:04:40 +0000 | [diff] [blame] | 457 | in_proto.ArtifactType.CODE_COVERAGE_RUST_LLVM_JSON: functools.partial( |
| 458 | test.BundleCodeCoverageRustLlvmJson, build_target.name |
| 459 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 460 | in_proto.ArtifactType.HWQUAL: functools.partial( |
| 461 | test.BundleHwqualTarball, |
| 462 | build_target.name, |
| 463 | packages_service.determine_full_version(), |
| 464 | ), |
Charles Liu | c71379c | 2022-10-28 04:02:34 +0000 | [diff] [blame] | 465 | in_proto.ArtifactType.CODE_COVERAGE_GOLANG: functools.partial( |
| 466 | test.BundleCodeCoverageGolang |
| 467 | ), |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | } |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 469 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 470 | for output_artifact in in_proto.output_artifacts: |
| 471 | for artifact_type, func in artifact_types.items(): |
| 472 | if artifact_type in output_artifact.artifact_types: |
Josiah Hounyo | de113e5 | 2022-11-30 06:30:33 +0000 | [diff] [blame] | 473 | try: |
| 474 | if ( |
| 475 | artifact_type |
| 476 | == in_proto.ArtifactType.CODE_COVERAGE_GOLANG |
| 477 | ): |
| 478 | paths = func(chroot, output_dir) |
| 479 | else: |
| 480 | paths = func(chroot, sysroot_class, output_dir) |
| 481 | except Exception as e: |
| 482 | generated.append( |
| 483 | { |
| 484 | "type": artifact_type, |
| 485 | "failed": True, |
| 486 | "failure_reason": str(e), |
| 487 | } |
| 488 | ) |
| 489 | artifact_name = ( |
| 490 | common_pb2.ArtifactsByService.Test.ArtifactType.Name( |
| 491 | artifact_type |
| 492 | ) |
| 493 | ) |
| 494 | logging.warning( |
| 495 | "%s artifact generation failed with exception %s", |
| 496 | artifact_name, |
| 497 | e, |
| 498 | ) |
| 499 | logging.warning("traceback:\n%s", traceback.format_exc()) |
| 500 | continue |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 501 | if paths: |
| 502 | generated.append( |
| 503 | { |
| 504 | "paths": [paths] |
| 505 | if isinstance(paths, str) |
| 506 | else paths, |
| 507 | "type": artifact_type, |
| 508 | } |
| 509 | ) |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 510 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 511 | return generated |