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