blob: 6b06f86dd9e749a4c21f80b1a3c8c61eab52108a [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Kleinc5403d62019-04-03 09:34:59 -06002# 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
7Handles all testing related functionality, it is not itself a test.
8"""
9
Jack Neusc9707c32021-07-23 21:48:54 +000010import functools
Derek Beckett963901e2022-04-19 11:38:36 -070011import logging
Alex Kleina2e42c42019-04-17 16:13:19 -060012import os
Sean McAllister17eed8d2021-09-21 10:41:16 -060013import string
Sean McAllister3834fef2021-10-08 15:45:18 -060014import subprocess
Josiah Hounyode113e52022-11-30 06:30:33 +000015import traceback
Alex Kleina2e42c42019-04-17 16:13:19 -060016
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040017from chromite.third_party.google.protobuf import json_format
18
Alex Klein8cb365a2019-05-15 16:24:53 -060019from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060020from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060021from chromite.api import validate
Alex Kleina2e42c42019-04-17 16:13:19 -060022from chromite.api.controller import controller_util
Evan Hernandez4e388a52019-05-01 12:16:33 -060023from chromite.api.gen.chromite.api import test_pb2
David Wellingc1433c22021-06-25 16:29:48 +000024from chromite.api.gen.chromiumos import common_pb2
Sean McAllister3834fef2021-10-08 15:45:18 -060025from chromite.api.gen.chromiumos.build.api import container_metadata_pb2
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040026from chromite.api.metrics import deserialize_metrics_log
Mike Frysinger06a51c82021-04-06 11:39:17 -040027from chromite.lib import build_target_lib
George Engelbrecht764b1cd2021-06-18 17:01:07 -060028from chromite.lib import chroot_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060029from chromite.lib import constants
Alex Kleinc5403d62019-04-03 09:34:59 -060030from chromite.lib import cros_build_lib
Ram Chandrasekare08e3ba2022-04-04 21:42:27 +000031from chromite.lib import goma_lib
Alex Kleinaef41942022-04-19 14:13:17 -060032from chromite.lib import metrics_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060033from chromite.lib import osutils
Alex Kleinc5403d62019-04-03 09:34:59 -060034from chromite.lib import sysroot_lib
Alex Klein18a60af2020-06-11 12:08:47 -060035from chromite.lib.parser import package_info
Jack Neusc9707c32021-07-23 21:48:54 +000036from chromite.service import packages as packages_service
Alex Kleinc5403d62019-04-03 09:34:59 -060037from chromite.service import test
38
39
Michael Mortensen85d38402019-12-12 09:50:29 -070040@faux.empty_success
41@faux.empty_completed_unsuccessfully_error
Alex Klein231d2da2019-07-22 16:44:45 -060042def DebugInfoTest(input_proto, _output_proto, config):
Alex Klein1699fab2022-09-08 08:46:06 -060043 """Run the debug info tests."""
44 sysroot_path = input_proto.sysroot.path
45 target_name = input_proto.sysroot.build_target.name
Alex Kleinc5403d62019-04-03 09:34:59 -060046
Alex Klein1699fab2022-09-08 08:46:06 -060047 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 Kleinc5403d62019-04-03 09:34:59 -060068 else:
Alex Klein1699fab2022-09-08 08:46:06 -060069 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -060070
71
Michael Mortensen82cd62d2019-12-01 14:58:54 -070072def _BuildTargetUnitTestFailedResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060073 """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 Klein1699fab2022-09-08 08:46:06 -060077 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 Mortensen82cd62d2019-12-01 14:58:54 -070082
83
Alex Klein0aecf472022-05-23 10:48:45 -060084@faux.empty_success
Michael Mortensen82cd62d2019-12-01 14:58:54 -070085@faux.error(_BuildTargetUnitTestFailedResponse)
Alex Klein1699fab2022-09-08 08:46:06 -060086@validate.require_each("packages", ["category", "package_name"])
Alex Klein231d2da2019-07-22 16:44:45 -060087@validate.validation_complete
Alex Kleinaef41942022-04-19 14:13:17 -060088@metrics_lib.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -060089def BuildTargetUnitTest(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060090 """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 Kleinfa6ebdc2019-05-10 10:57:31 -060095
Alex Klein1699fab2022-09-08 08:46:06 -060096 # Packages to be tested.
97 packages_package_info = input_proto.packages
98 packages = []
99 for package_info_msg in packages_package_info:
100 cpv = controller_util.PackageInfoToCPV(package_info_msg)
101 packages.append(cpv.cp)
Navil Perezc0b29a82020-07-07 14:17:48 +0000102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 # Skipped tests.
104 blocklisted_package_info = input_proto.package_blocklist
105 blocklist = []
106 for package_info_msg in blocklisted_package_info:
107 blocklist.append(controller_util.PackageInfoToString(package_info_msg))
Alex Kleinf2674462019-05-16 16:47:24 -0600108
Alex Klein1699fab2022-09-08 08:46:06 -0600109 # Allow call to filter out non-cros_workon packages from the input packages.
110 filter_only_cros_workon = input_proto.flags.filter_only_cros_workon
Navil Perez43fb45d2021-05-14 20:34:24 +0000111
Alex Klein1699fab2022-09-08 08:46:06 -0600112 # Allow call to succeed if no tests were found.
113 testable_packages_optional = input_proto.flags.testable_packages_optional
Navil Perez19dc4792020-09-10 19:06:17 +0000114
Alex Klein1699fab2022-09-08 08:46:06 -0600115 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
Alex Kleina2e42c42019-04-17 16:13:19 -0600116
Alex Klein1699fab2022-09-08 08:46:06 -0600117 code_coverage = input_proto.flags.code_coverage
Srinivas Hegde0058b8b2022-09-12 22:31:10 +0000118 rust_code_coverage = input_proto.flags.rust_code_coverage
David Burgera9c11872020-07-29 13:32:02 -0600119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 sysroot = sysroot_lib.Sysroot(build_target.root)
Lizzy Presland4feb2372022-01-20 05:16:30 +0000121
Alex Klein1699fab2022-09-08 08:46:06 -0600122 result = test.BuildTargetUnitTest(
123 build_target,
124 packages=packages,
125 blocklist=blocklist,
126 was_built=was_built,
127 code_coverage=code_coverage,
Srinivas Hegde0058b8b2022-09-12 22:31:10 +0000128 rust_code_coverage=rust_code_coverage,
Alex Klein1699fab2022-09-08 08:46:06 -0600129 testable_packages_optional=testable_packages_optional,
130 filter_only_cros_workon=filter_only_cros_workon,
131 )
Alex Kleina2e42c42019-04-17 16:13:19 -0600132
Alex Klein1699fab2022-09-08 08:46:06 -0600133 if not result.success:
134 # Record all failed packages and retrieve log locations.
135 controller_util.retrieve_package_log_paths(
136 result.failed_pkgs, output_proto, sysroot
137 )
138 if result.failed_pkgs:
139 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
140 else:
141 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -0600142
Alex Klein1699fab2022-09-08 08:46:06 -0600143 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Alex Kleine3fc3ca2019-04-30 16:20:55 -0600144
145
Alex Klein1699fab2022-09-08 08:46:06 -0600146SRC_DIR = os.path.join(constants.SOURCE_ROOT, "src")
147PLATFORM_DEV_DIR = os.path.join(SRC_DIR, "platform/dev")
148TEST_SERVICE_DIR = os.path.join(PLATFORM_DEV_DIR, "src/chromiumos/test")
C Shapiro91af1ce2021-06-17 12:42:09 -0500149
150
C Shapiroe15660b2021-06-18 12:49:37 -0500151def _BuildTestServiceContainersResponse(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600152 """Fake success response"""
153 # pylint: disable=unused-argument
154 output_proto.results.append(
155 test_pb2.TestServiceContainerBuildResult(
156 success=test_pb2.TestServiceContainerBuildResult.Success()
157 )
158 )
C Shapiroe15660b2021-06-18 12:49:37 -0500159
160
161def _BuildTestServiceContainersFailedResponse(
Alex Klein1699fab2022-09-08 08:46:06 -0600162 _input_proto, output_proto, _config
163):
164 """Fake failure response"""
C Shapiroe15660b2021-06-18 12:49:37 -0500165
Alex Klein1699fab2022-09-08 08:46:06 -0600166 # pylint: disable=unused-argument
167 output_proto.results.append(
168 test_pb2.TestServiceContainerBuildResult(
169 failure=test_pb2.TestServiceContainerBuildResult.Failure(
170 error_message="fake error"
171 )
172 )
173 )
C Shapiroe15660b2021-06-18 12:49:37 -0500174
175
Alex Klein1699fab2022-09-08 08:46:06 -0600176@validate.constraint("valid docker tag")
Sean McAllister17eed8d2021-09-21 10:41:16 -0600177def _ValidDockerTag(tag):
Alex Klein1699fab2022-09-08 08:46:06 -0600178 """Check that a string meets requirements for Docker tag naming."""
179 # Tags can't start with period or dash
180 if tag[0] in ".-":
181 return "tag can't begin with '.' or '-'"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600182
Alex Klein1699fab2022-09-08 08:46:06 -0600183 # Tags can only consist of [a-zA-Z0-9-_.]
184 allowed_chars = set(string.ascii_letters + string.digits + "-_.")
185 invalid_chars = set(tag) - allowed_chars
186 if invalid_chars:
187 return f'saw one or more invalid characters: [{"".join(invalid_chars)}]'
Sean McAllister17eed8d2021-09-21 10:41:16 -0600188
Alex Klein1699fab2022-09-08 08:46:06 -0600189 # Finally, max tag length is 128 characters
190 if len(tag) > 128:
191 return "maximum tag length is 128 characters"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600192
193
Alex Klein1699fab2022-09-08 08:46:06 -0600194@validate.constraint("valid docker label key")
Sean McAllister17eed8d2021-09-21 10:41:16 -0600195def _ValidDockerLabelKey(key):
Alex Klein1699fab2022-09-08 08:46:06 -0600196 """Check that a string meets requirements for Docker tag naming."""
Sean McAllister17eed8d2021-09-21 10:41:16 -0600197
Alex Klein1699fab2022-09-08 08:46:06 -0600198 # Label keys should start and end with a lowercase letter
199 lowercase = set(string.ascii_lowercase)
200 if not (key[0] in lowercase and key[-1] in lowercase):
201 return "label key doesn't start and end with lowercase letter"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600202
Alex Klein1699fab2022-09-08 08:46:06 -0600203 # Label keys can have lower-case alphanumeric characters, period and dash
204 allowed_chars = set(string.ascii_lowercase + string.digits + "-.")
205 invalid_chars = set(key) - allowed_chars
206 if invalid_chars:
207 return f'saw one or more invalid characters: [{"".join(invalid_chars)}]'
Sean McAllister17eed8d2021-09-21 10:41:16 -0600208
Alex Klein1699fab2022-09-08 08:46:06 -0600209 # Repeated . and - aren't allowed
210 for char in ".-":
211 if char * 2 in key:
212 return f"'{char}' can't be repeated in label key"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600213
214
C Shapiroe15660b2021-06-18 12:49:37 -0500215@faux.success(_BuildTestServiceContainersResponse)
216@faux.error(_BuildTestServiceContainersFailedResponse)
Alex Klein1699fab2022-09-08 08:46:06 -0600217@validate.require("build_target.name")
218@validate.require("chroot.path")
219@validate.check_constraint("tags", _ValidDockerTag)
220@validate.check_constraint("labels", _ValidDockerLabelKey)
C Shapiro91af1ce2021-06-17 12:42:09 -0500221@validate.validation_complete
Sean McAllister15fa8332021-09-27 12:24:12 -0600222def BuildTestServiceContainers(
223 input_proto: test_pb2.BuildTestServiceContainersRequest,
Alex Klein1699fab2022-09-08 08:46:06 -0600224 output_proto: test_pb2.BuildTestServiceContainersResponse,
225 _config,
226):
227 """Builds docker containers for all test services and pushes them to gcr.io"""
228 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
229 chroot = controller_util.ParseChroot(input_proto.chroot)
230 sysroot = sysroot_lib.Sysroot(build_target.root)
C Shapiro91af1ce2021-06-17 12:42:09 -0500231
Alex Klein1699fab2022-09-08 08:46:06 -0600232 tags = ",".join(input_proto.tags)
233 labels = (f"{key}={value}" for key, value in input_proto.labels.items())
Sean McAllister15fa8332021-09-27 12:24:12 -0600234
Alex Klein1699fab2022-09-08 08:46:06 -0600235 build_script = os.path.join(
236 TEST_SERVICE_DIR, "python/src/docker_libs/cli/build-dockerimages.py"
237 )
238 human_name = "Service Builder"
Sean McAllister15fa8332021-09-27 12:24:12 -0600239
Alex Klein1699fab2022-09-08 08:46:06 -0600240 with osutils.TempDir(prefix="test_container") as tempdir:
241 result_file = "metadata.jsonpb"
242 output_path = os.path.join(tempdir, result_file)
243 # Note that we use an output file instead of stdout to avoid any issues
244 # with maintaining stdout hygiene. Stdout and stderr are combined to
245 # form the error log in response to any errors.
246 cmd = [build_script, chroot.path, sysroot.path]
Sean McAllistere6a4ae22021-10-21 13:17:08 -0600247
Alex Klein1699fab2022-09-08 08:46:06 -0600248 if input_proto.HasField("repository"):
249 cmd += ["--host", input_proto.repository.hostname]
250 cmd += ["--project", input_proto.repository.project]
Sean McAllistere6a4ae22021-10-21 13:17:08 -0600251
Alex Klein1699fab2022-09-08 08:46:06 -0600252 cmd += ["--tags", tags]
253 cmd += ["--output", output_path]
Derek Beckettb28b5372022-04-15 13:08:32 -0700254
Alex Klein1699fab2022-09-08 08:46:06 -0600255 # Translate generator to comma separated string.
256 ct_labels = ",".join(labels)
257 cmd += ["--labels", ct_labels]
258 cmd += ["--build_all"]
259 cmd += ["--upload"]
Derek Beckettcbe64c82022-05-05 15:00:18 -0700260
Alex Klein1699fab2022-09-08 08:46:06 -0600261 cmd_result = cros_build_lib.run(
262 cmd, check=False, stderr=subprocess.STDOUT, stdout=True
263 )
Derek Becketta8d9fd22022-04-27 10:44:52 -0700264
Alex Klein1699fab2022-09-08 08:46:06 -0600265 if cmd_result.returncode != 0:
266 # When failing, just record a fail response with the builder name.
267 logging.debug(
268 "%s build failed.\nStdout:\n%s\nStderr:\n%s",
269 human_name,
270 cmd_result.stdout,
271 cmd_result.stderr,
272 )
273 result = test_pb2.TestServiceContainerBuildResult()
274 result.name = human_name
275 image_info = container_metadata_pb2.ContainerImageInfo()
276 result.failure.CopyFrom(
277 test_pb2.TestServiceContainerBuildResult.Failure(
278 error_message=cmd_result.stdout
279 )
280 )
281 output_proto.results.append(result)
C Shapiro91af1ce2021-06-17 12:42:09 -0500282
Alex Klein1699fab2022-09-08 08:46:06 -0600283 else:
284 logging.debug(
285 "%s build succeeded.\nStdout:\n%s\nStderr:\n%s",
286 human_name,
287 cmd_result.stdout,
288 cmd_result.stderr,
289 )
290 files = os.listdir(tempdir)
291 # Iterate through the tempdir to output metadata files.
292 for file in files:
293 if result_file in file:
294 output_path = os.path.join(tempdir, file)
Derek Beckett344b5a82022-05-09 17:21:45 -0700295
Alex Klein1699fab2022-09-08 08:46:06 -0600296 # build-dockerimages.py will append the service name to outputfile
297 # with an underscore.
298 human_name = file.split("_")[-1]
Derek Beckett344b5a82022-05-09 17:21:45 -0700299
Alex Klein1699fab2022-09-08 08:46:06 -0600300 result = test_pb2.TestServiceContainerBuildResult()
301 result.name = human_name
302 image_info = container_metadata_pb2.ContainerImageInfo()
303 json_format.Parse(osutils.ReadFile(output_path), image_info)
304 result.success.CopyFrom(
305 test_pb2.TestServiceContainerBuildResult.Success(
306 image_info=image_info
307 )
308 )
309 output_proto.results.append(result)
Derek Beckett344b5a82022-05-09 17:21:45 -0700310
C Shapiro91af1ce2021-06-17 12:42:09 -0500311
Michael Mortensen7a860eb2019-12-03 20:25:15 -0700312@faux.empty_success
313@faux.empty_completed_unsuccessfully_error
Alex Klein231d2da2019-07-22 16:44:45 -0600314@validate.validation_complete
315def ChromiteUnitTest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600316 """Run the chromite unit tests."""
317 if test.ChromiteUnitTest():
318 return controller.RETURN_CODE_SUCCESS
319 else:
320 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Evan Hernandez4e388a52019-05-01 12:16:33 -0600321
322
Greg Edelstonf3fc8b62020-03-17 14:20:24 -0600323@faux.empty_success
324@faux.empty_completed_unsuccessfully_error
325@validate.validation_complete
326def ChromitePytest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600327 """Run the chromite unit tests."""
328 # TODO(vapier): Delete this stub.
329 return controller.RETURN_CODE_SUCCESS
Greg Edelstonf3fc8b62020-03-17 14:20:24 -0600330
331
Sloan Johnson9fdd5312022-03-02 00:55:26 +0000332@faux.empty_success
333@faux.empty_completed_unsuccessfully_error
334@validate.validation_complete
335def RulesCrosUnitTest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600336 """Run the rules_cros unit tests."""
337 if test.RulesCrosUnitTest():
338 return controller.RETURN_CODE_SUCCESS
339 else:
340 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Sloan Johnson9fdd5312022-03-02 00:55:26 +0000341
342
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600343@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600344@validate.require("sysroot.path", "sysroot.build_target.name", "chrome_root")
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600345@validate.validation_complete
346def SimpleChromeWorkflowTest(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600347 """Run SimpleChromeWorkflow tests."""
348 if input_proto.goma_config.goma_dir:
349 chromeos_goma_dir = input_proto.goma_config.chromeos_goma_dir or None
350 goma = goma_lib.Goma(
351 input_proto.goma_config.goma_dir,
352 input_proto.goma_config.goma_client_json,
353 stage_name="BuildApiTestSimpleChrome",
354 chromeos_goma_dir=chromeos_goma_dir,
355 )
356 else:
357 goma = None
358 return test.SimpleChromeWorkflowTest(
359 input_proto.sysroot.path,
360 input_proto.sysroot.build_target.name,
361 input_proto.chrome_root,
362 goma,
363 )
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600364
365
Alex Klein076841b2019-08-29 15:19:39 -0600366@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600367@validate.require(
368 "build_target.name", "vm_path.path", "test_harness", "vm_tests"
369)
Alex Klein231d2da2019-07-22 16:44:45 -0600370@validate.validation_complete
371def VmTest(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600372 """Run VM tests."""
373 build_target_name = input_proto.build_target.name
374 vm_path = input_proto.vm_path.path
Alex Kleinc05f3d12019-05-29 14:16:21 -0600375
Alex Klein1699fab2022-09-08 08:46:06 -0600376 test_harness = input_proto.test_harness
Evan Hernandez4e388a52019-05-01 12:16:33 -0600377
Alex Klein1699fab2022-09-08 08:46:06 -0600378 vm_tests = input_proto.vm_tests
Evan Hernandez4e388a52019-05-01 12:16:33 -0600379
Alex Klein1699fab2022-09-08 08:46:06 -0600380 cmd = [
381 "cros_run_test",
382 "--debug",
383 "--no-display",
384 "--copy-on-write",
385 "--board",
386 build_target_name,
387 "--image-path",
388 vm_path,
389 "--%s" % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower(),
390 ]
391 cmd.extend(vm_test.pattern for vm_test in vm_tests)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600392
Alex Klein1699fab2022-09-08 08:46:06 -0600393 if input_proto.ssh_options.port:
394 cmd.extend(["--ssh-port", str(input_proto.ssh_options.port)])
Evan Hernandez4e388a52019-05-01 12:16:33 -0600395
Alex Klein1699fab2022-09-08 08:46:06 -0600396 if input_proto.ssh_options.private_key_path:
397 cmd.extend(
398 ["--private-key", input_proto.ssh_options.private_key_path.path]
399 )
Evan Hernandez4e388a52019-05-01 12:16:33 -0600400
Alex Klein1699fab2022-09-08 08:46:06 -0600401 # TODO(evanhernandez): Find a nice way to pass test_that-args through
402 # the build API. Or obviate them.
403 if test_harness == test_pb2.VmTestRequest.AUTOTEST:
404 cmd.append("--test_that-args=--allow-chrome-crashes")
Evan Hernandez4e388a52019-05-01 12:16:33 -0600405
Alex Klein1699fab2022-09-08 08:46:06 -0600406 with osutils.TempDir(prefix="vm-test-results.") as results_dir:
407 cmd.extend(["--results-dir", results_dir])
408 cros_build_lib.run(cmd, kill_timeout=10 * 60)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600409
410
Alex Klein076841b2019-08-29 15:19:39 -0600411@faux.all_empty
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600412@validate.validation_complete
413def CrosSigningTest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600414 """Run the cros-signing unit tests."""
415 test_runner = os.path.join(
416 constants.SOURCE_ROOT, "cros-signing", "signer", "run_tests.py"
417 )
418 result = cros_build_lib.run([test_runner], check=False)
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600419
Alex Klein1699fab2022-09-08 08:46:06 -0600420 return result.returncode
David Wellingc1433c22021-06-25 16:29:48 +0000421
422
Alex Klein1699fab2022-09-08 08:46:06 -0600423def GetArtifacts(
424 in_proto: common_pb2.ArtifactsByService.Test,
425 chroot: chroot_lib.Chroot,
426 sysroot_class: sysroot_lib.Sysroot,
427 build_target: build_target_lib.BuildTarget,
428 output_dir: str,
429) -> list:
430 """Builds and copies test artifacts to specified output_dir.
David Wellingc1433c22021-06-25 16:29:48 +0000431
Alex Klein1699fab2022-09-08 08:46:06 -0600432 Copies test artifacts to output_dir, returning a list of (output_dir: str)
433 paths to the desired files.
David Wellingc1433c22021-06-25 16:29:48 +0000434
Alex Klein1699fab2022-09-08 08:46:06 -0600435 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600436 in_proto: Proto request defining reqs.
437 chroot: The chroot class used for these artifacts.
438 sysroot_class: The sysroot class used for these artifacts.
439 build_target: The build target used for these artifacts.
440 output_dir: The path to write artifacts to.
David Wellingc1433c22021-06-25 16:29:48 +0000441
Alex Klein1699fab2022-09-08 08:46:06 -0600442 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -0600443 A list of dictionary mappings of ArtifactType to list of paths.
Alex Klein1699fab2022-09-08 08:46:06 -0600444 """
445 generated = []
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600446
Alex Klein1699fab2022-09-08 08:46:06 -0600447 artifact_types = {
448 in_proto.ArtifactType.CODE_COVERAGE_LLVM_JSON: functools.partial(
449 test.BundleCodeCoverageLlvmJson, build_target.name
450 ),
Srinivas Hegdeb6fe4ac2022-09-22 00:04:40 +0000451 in_proto.ArtifactType.CODE_COVERAGE_RUST_LLVM_JSON: functools.partial(
452 test.BundleCodeCoverageRustLlvmJson, build_target.name
453 ),
Alex Klein1699fab2022-09-08 08:46:06 -0600454 in_proto.ArtifactType.HWQUAL: functools.partial(
455 test.BundleHwqualTarball,
456 build_target.name,
457 packages_service.determine_full_version(),
458 ),
Charles Liuc71379c2022-10-28 04:02:34 +0000459 in_proto.ArtifactType.CODE_COVERAGE_GOLANG: functools.partial(
460 test.BundleCodeCoverageGolang
461 ),
Alex Klein1699fab2022-09-08 08:46:06 -0600462 }
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600463
Alex Klein1699fab2022-09-08 08:46:06 -0600464 for output_artifact in in_proto.output_artifacts:
465 for artifact_type, func in artifact_types.items():
466 if artifact_type in output_artifact.artifact_types:
Josiah Hounyode113e52022-11-30 06:30:33 +0000467 try:
468 if (
469 artifact_type
470 == in_proto.ArtifactType.CODE_COVERAGE_GOLANG
471 ):
472 paths = func(chroot, output_dir)
473 else:
474 paths = func(chroot, sysroot_class, output_dir)
475 except Exception as e:
476 generated.append(
477 {
478 "type": artifact_type,
479 "failed": True,
480 "failure_reason": str(e),
481 }
482 )
483 artifact_name = (
484 common_pb2.ArtifactsByService.Test.ArtifactType.Name(
485 artifact_type
486 )
487 )
488 logging.warning(
489 "%s artifact generation failed with exception %s",
490 artifact_name,
491 e,
492 )
493 logging.warning("traceback:\n%s", traceback.format_exc())
494 continue
Alex Klein1699fab2022-09-08 08:46:06 -0600495 if paths:
496 generated.append(
497 {
498 "paths": [paths]
499 if isinstance(paths, str)
500 else paths,
501 "type": artifact_type,
502 }
503 )
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600504
Alex Klein1699fab2022-09-08 08:46:06 -0600505 return generated