blob: bb37ea24667b91f96a2963b4c65d39f583dbd0f3 [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
Alex Kleina2e42c42019-04-17 16:13:19 -060015
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040016from chromite.third_party.google.protobuf import json_format
17
Alex Klein8cb365a2019-05-15 16:24:53 -060018from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060019from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060020from chromite.api import validate
Alex Kleina2e42c42019-04-17 16:13:19 -060021from chromite.api.controller import controller_util
Evan Hernandez4e388a52019-05-01 12:16:33 -060022from chromite.api.gen.chromite.api import test_pb2
David Wellingc1433c22021-06-25 16:29:48 +000023from chromite.api.gen.chromiumos import common_pb2
Sean McAllister3834fef2021-10-08 15:45:18 -060024from chromite.api.gen.chromiumos.build.api import container_metadata_pb2
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040025from chromite.api.metrics import deserialize_metrics_log
Mike Frysinger06a51c82021-04-06 11:39:17 -040026from chromite.lib import build_target_lib
George Engelbrecht764b1cd2021-06-18 17:01:07 -060027from chromite.lib import chroot_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060028from chromite.lib import constants
Alex Kleinc5403d62019-04-03 09:34:59 -060029from chromite.lib import cros_build_lib
Ram Chandrasekare08e3ba2022-04-04 21:42:27 +000030from chromite.lib import goma_lib
Alex Kleinaef41942022-04-19 14:13:17 -060031from chromite.lib import metrics_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060032from chromite.lib import osutils
Alex Kleinc5403d62019-04-03 09:34:59 -060033from chromite.lib import sysroot_lib
Alex Klein18a60af2020-06-11 12:08:47 -060034from chromite.lib.parser import package_info
Jack Neusc9707c32021-07-23 21:48:54 +000035from chromite.service import packages as packages_service
Alex Kleinc5403d62019-04-03 09:34:59 -060036from chromite.service import test
37
38
Michael Mortensen85d38402019-12-12 09:50:29 -070039@faux.empty_success
40@faux.empty_completed_unsuccessfully_error
Alex Klein231d2da2019-07-22 16:44:45 -060041def DebugInfoTest(input_proto, _output_proto, config):
Alex Klein1699fab2022-09-08 08:46:06 -060042 """Run the debug info tests."""
43 sysroot_path = input_proto.sysroot.path
44 target_name = input_proto.sysroot.build_target.name
Alex Kleinc5403d62019-04-03 09:34:59 -060045
Alex Klein1699fab2022-09-08 08:46:06 -060046 if not sysroot_path:
47 if target_name:
48 sysroot_path = build_target_lib.get_default_sysroot_path(
49 target_name
50 )
51 else:
52 cros_build_lib.Die(
53 "The sysroot path or the sysroot's build target name "
54 "must be provided."
55 )
56
57 # We could get away with out this, but it's a cheap check.
58 sysroot = sysroot_lib.Sysroot(sysroot_path)
59 if not sysroot.Exists():
60 cros_build_lib.Die("The provided sysroot does not exist.")
61
62 if config.validate_only:
63 return controller.RETURN_CODE_VALID_INPUT
64
65 if test.DebugInfoTest(sysroot_path):
66 return controller.RETURN_CODE_SUCCESS
Alex Kleinc5403d62019-04-03 09:34:59 -060067 else:
Alex Klein1699fab2022-09-08 08:46:06 -060068 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -060069
70
Michael Mortensen82cd62d2019-12-01 14:58:54 -070071def _BuildTargetUnitTestFailedResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060072 """Add failed packages to a failed response."""
73 packages = ["foo/bar", "cat/pkg"]
74 for pkg in packages:
75 pkg_info = package_info.parse(pkg)
76 pkg_info_msg = output_proto.failed_packages.add()
77 controller_util.serialize_package_info(pkg_info, pkg_info_msg)
78 failed_pkg_data_msg = output_proto.failed_package_data.add()
79 controller_util.serialize_package_info(
80 pkg_info, failed_pkg_data_msg.name
81 )
82 failed_pkg_data_msg.log_path.path = "/path/to/%s/log" % pkg
Michael Mortensen82cd62d2019-12-01 14:58:54 -070083
84
Alex Klein0aecf472022-05-23 10:48:45 -060085@faux.empty_success
Michael Mortensen82cd62d2019-12-01 14:58:54 -070086@faux.error(_BuildTargetUnitTestFailedResponse)
Alex Klein1699fab2022-09-08 08:46:06 -060087@validate.require_each("packages", ["category", "package_name"])
Alex Klein231d2da2019-07-22 16:44:45 -060088@validate.validation_complete
Alex Kleinaef41942022-04-19 14:13:17 -060089@metrics_lib.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -060090def BuildTargetUnitTest(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060091 """Run a build target's ebuild unit tests."""
92 # Method flags.
93 # An empty sysroot means build packages was not run. This is used for
94 # certain boards that need to use prebuilts (e.g. grunt's unittest-only).
95 was_built = not input_proto.flags.empty_sysroot
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060096
Alex Klein1699fab2022-09-08 08:46:06 -060097 # Packages to be tested.
98 packages_package_info = input_proto.packages
99 packages = []
100 for package_info_msg in packages_package_info:
101 cpv = controller_util.PackageInfoToCPV(package_info_msg)
102 packages.append(cpv.cp)
Navil Perezc0b29a82020-07-07 14:17:48 +0000103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 # Skipped tests.
105 blocklisted_package_info = input_proto.package_blocklist
106 blocklist = []
107 for package_info_msg in blocklisted_package_info:
108 blocklist.append(controller_util.PackageInfoToString(package_info_msg))
Alex Kleinf2674462019-05-16 16:47:24 -0600109
Alex Klein1699fab2022-09-08 08:46:06 -0600110 # Allow call to filter out non-cros_workon packages from the input packages.
111 filter_only_cros_workon = input_proto.flags.filter_only_cros_workon
Navil Perez43fb45d2021-05-14 20:34:24 +0000112
Alex Klein1699fab2022-09-08 08:46:06 -0600113 # Allow call to succeed if no tests were found.
114 testable_packages_optional = input_proto.flags.testable_packages_optional
Navil Perez19dc4792020-09-10 19:06:17 +0000115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
Alex Kleina2e42c42019-04-17 16:13:19 -0600117
Alex Klein1699fab2022-09-08 08:46:06 -0600118 code_coverage = input_proto.flags.code_coverage
Srinivas Hegde0058b8b2022-09-12 22:31:10 +0000119 rust_code_coverage = input_proto.flags.rust_code_coverage
David Burgera9c11872020-07-29 13:32:02 -0600120
Alex Klein1699fab2022-09-08 08:46:06 -0600121 sysroot = sysroot_lib.Sysroot(build_target.root)
Lizzy Presland4feb2372022-01-20 05:16:30 +0000122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 result = test.BuildTargetUnitTest(
124 build_target,
125 packages=packages,
126 blocklist=blocklist,
127 was_built=was_built,
128 code_coverage=code_coverage,
Srinivas Hegde0058b8b2022-09-12 22:31:10 +0000129 rust_code_coverage=rust_code_coverage,
Alex Klein1699fab2022-09-08 08:46:06 -0600130 testable_packages_optional=testable_packages_optional,
131 filter_only_cros_workon=filter_only_cros_workon,
132 )
Alex Kleina2e42c42019-04-17 16:13:19 -0600133
Alex Klein1699fab2022-09-08 08:46:06 -0600134 if not result.success:
135 # Record all failed packages and retrieve log locations.
136 controller_util.retrieve_package_log_paths(
137 result.failed_pkgs, output_proto, sysroot
138 )
139 if result.failed_pkgs:
140 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
141 else:
142 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -0600143
Alex Klein1699fab2022-09-08 08:46:06 -0600144 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Alex Kleine3fc3ca2019-04-30 16:20:55 -0600145
146
Alex Klein1699fab2022-09-08 08:46:06 -0600147SRC_DIR = os.path.join(constants.SOURCE_ROOT, "src")
148PLATFORM_DEV_DIR = os.path.join(SRC_DIR, "platform/dev")
149TEST_SERVICE_DIR = os.path.join(PLATFORM_DEV_DIR, "src/chromiumos/test")
C Shapiro91af1ce2021-06-17 12:42:09 -0500150
151
C Shapiroe15660b2021-06-18 12:49:37 -0500152def _BuildTestServiceContainersResponse(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600153 """Fake success response"""
154 # pylint: disable=unused-argument
155 output_proto.results.append(
156 test_pb2.TestServiceContainerBuildResult(
157 success=test_pb2.TestServiceContainerBuildResult.Success()
158 )
159 )
C Shapiroe15660b2021-06-18 12:49:37 -0500160
161
162def _BuildTestServiceContainersFailedResponse(
Alex Klein1699fab2022-09-08 08:46:06 -0600163 _input_proto, output_proto, _config
164):
165 """Fake failure response"""
C Shapiroe15660b2021-06-18 12:49:37 -0500166
Alex Klein1699fab2022-09-08 08:46:06 -0600167 # pylint: disable=unused-argument
168 output_proto.results.append(
169 test_pb2.TestServiceContainerBuildResult(
170 failure=test_pb2.TestServiceContainerBuildResult.Failure(
171 error_message="fake error"
172 )
173 )
174 )
C Shapiroe15660b2021-06-18 12:49:37 -0500175
176
Alex Klein1699fab2022-09-08 08:46:06 -0600177@validate.constraint("valid docker tag")
Sean McAllister17eed8d2021-09-21 10:41:16 -0600178def _ValidDockerTag(tag):
Alex Klein1699fab2022-09-08 08:46:06 -0600179 """Check that a string meets requirements for Docker tag naming."""
180 # Tags can't start with period or dash
181 if tag[0] in ".-":
182 return "tag can't begin with '.' or '-'"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600183
Alex Klein1699fab2022-09-08 08:46:06 -0600184 # Tags can only consist of [a-zA-Z0-9-_.]
185 allowed_chars = set(string.ascii_letters + string.digits + "-_.")
186 invalid_chars = set(tag) - allowed_chars
187 if invalid_chars:
188 return f'saw one or more invalid characters: [{"".join(invalid_chars)}]'
Sean McAllister17eed8d2021-09-21 10:41:16 -0600189
Alex Klein1699fab2022-09-08 08:46:06 -0600190 # Finally, max tag length is 128 characters
191 if len(tag) > 128:
192 return "maximum tag length is 128 characters"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600193
194
Alex Klein1699fab2022-09-08 08:46:06 -0600195@validate.constraint("valid docker label key")
Sean McAllister17eed8d2021-09-21 10:41:16 -0600196def _ValidDockerLabelKey(key):
Alex Klein1699fab2022-09-08 08:46:06 -0600197 """Check that a string meets requirements for Docker tag naming."""
Sean McAllister17eed8d2021-09-21 10:41:16 -0600198
Alex Klein1699fab2022-09-08 08:46:06 -0600199 # Label keys should start and end with a lowercase letter
200 lowercase = set(string.ascii_lowercase)
201 if not (key[0] in lowercase and key[-1] in lowercase):
202 return "label key doesn't start and end with lowercase letter"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600203
Alex Klein1699fab2022-09-08 08:46:06 -0600204 # Label keys can have lower-case alphanumeric characters, period and dash
205 allowed_chars = set(string.ascii_lowercase + string.digits + "-.")
206 invalid_chars = set(key) - allowed_chars
207 if invalid_chars:
208 return f'saw one or more invalid characters: [{"".join(invalid_chars)}]'
Sean McAllister17eed8d2021-09-21 10:41:16 -0600209
Alex Klein1699fab2022-09-08 08:46:06 -0600210 # Repeated . and - aren't allowed
211 for char in ".-":
212 if char * 2 in key:
213 return f"'{char}' can't be repeated in label key"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600214
215
C Shapiroe15660b2021-06-18 12:49:37 -0500216@faux.success(_BuildTestServiceContainersResponse)
217@faux.error(_BuildTestServiceContainersFailedResponse)
Alex Klein1699fab2022-09-08 08:46:06 -0600218@validate.require("build_target.name")
219@validate.require("chroot.path")
220@validate.check_constraint("tags", _ValidDockerTag)
221@validate.check_constraint("labels", _ValidDockerLabelKey)
C Shapiro91af1ce2021-06-17 12:42:09 -0500222@validate.validation_complete
Sean McAllister15fa8332021-09-27 12:24:12 -0600223def BuildTestServiceContainers(
224 input_proto: test_pb2.BuildTestServiceContainersRequest,
Alex Klein1699fab2022-09-08 08:46:06 -0600225 output_proto: test_pb2.BuildTestServiceContainersResponse,
226 _config,
227):
228 """Builds docker containers for all test services and pushes them to gcr.io"""
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 Shapiro91af1ce2021-06-17 12:42:09 -0500232
Alex Klein1699fab2022-09-08 08:46:06 -0600233 tags = ",".join(input_proto.tags)
234 labels = (f"{key}={value}" for key, value in input_proto.labels.items())
Sean McAllister15fa8332021-09-27 12:24:12 -0600235
Alex Klein1699fab2022-09-08 08:46:06 -0600236 build_script = os.path.join(
237 TEST_SERVICE_DIR, "python/src/docker_libs/cli/build-dockerimages.py"
238 )
239 human_name = "Service Builder"
Sean McAllister15fa8332021-09-27 12:24:12 -0600240
Alex Klein1699fab2022-09-08 08:46:06 -0600241 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.
247 cmd = [build_script, chroot.path, sysroot.path]
Sean McAllistere6a4ae22021-10-21 13:17:08 -0600248
Alex Klein1699fab2022-09-08 08:46:06 -0600249 if input_proto.HasField("repository"):
250 cmd += ["--host", input_proto.repository.hostname]
251 cmd += ["--project", input_proto.repository.project]
Sean McAllistere6a4ae22021-10-21 13:17:08 -0600252
Alex Klein1699fab2022-09-08 08:46:06 -0600253 cmd += ["--tags", tags]
254 cmd += ["--output", output_path]
Derek Beckettb28b5372022-04-15 13:08:32 -0700255
Alex Klein1699fab2022-09-08 08:46:06 -0600256 # Translate generator to comma separated string.
257 ct_labels = ",".join(labels)
258 cmd += ["--labels", ct_labels]
259 cmd += ["--build_all"]
260 cmd += ["--upload"]
Derek Beckettcbe64c82022-05-05 15:00:18 -0700261
Alex Klein1699fab2022-09-08 08:46:06 -0600262 cmd_result = cros_build_lib.run(
263 cmd, check=False, stderr=subprocess.STDOUT, stdout=True
264 )
Derek Becketta8d9fd22022-04-27 10:44:52 -0700265
Alex Klein1699fab2022-09-08 08:46:06 -0600266 if cmd_result.returncode != 0:
267 # When failing, just record a fail response with the builder name.
268 logging.debug(
269 "%s build failed.\nStdout:\n%s\nStderr:\n%s",
270 human_name,
271 cmd_result.stdout,
272 cmd_result.stderr,
273 )
274 result = test_pb2.TestServiceContainerBuildResult()
275 result.name = human_name
276 image_info = container_metadata_pb2.ContainerImageInfo()
277 result.failure.CopyFrom(
278 test_pb2.TestServiceContainerBuildResult.Failure(
279 error_message=cmd_result.stdout
280 )
281 )
282 output_proto.results.append(result)
C Shapiro91af1ce2021-06-17 12:42:09 -0500283
Alex Klein1699fab2022-09-08 08:46:06 -0600284 else:
285 logging.debug(
286 "%s build succeeded.\nStdout:\n%s\nStderr:\n%s",
287 human_name,
288 cmd_result.stdout,
289 cmd_result.stderr,
290 )
291 files = os.listdir(tempdir)
292 # Iterate through the tempdir to output metadata files.
293 for file in files:
294 if result_file in file:
295 output_path = os.path.join(tempdir, file)
Derek Beckett344b5a82022-05-09 17:21:45 -0700296
Alex Klein1699fab2022-09-08 08:46:06 -0600297 # build-dockerimages.py will append the service name to outputfile
298 # with an underscore.
299 human_name = file.split("_")[-1]
Derek Beckett344b5a82022-05-09 17:21:45 -0700300
Alex Klein1699fab2022-09-08 08:46:06 -0600301 result = test_pb2.TestServiceContainerBuildResult()
302 result.name = human_name
303 image_info = container_metadata_pb2.ContainerImageInfo()
304 json_format.Parse(osutils.ReadFile(output_path), image_info)
305 result.success.CopyFrom(
306 test_pb2.TestServiceContainerBuildResult.Success(
307 image_info=image_info
308 )
309 )
310 output_proto.results.append(result)
Derek Beckett344b5a82022-05-09 17:21:45 -0700311
C Shapiro91af1ce2021-06-17 12:42:09 -0500312
Michael Mortensen7a860eb2019-12-03 20:25:15 -0700313@faux.empty_success
314@faux.empty_completed_unsuccessfully_error
Alex Klein231d2da2019-07-22 16:44:45 -0600315@validate.validation_complete
316def ChromiteUnitTest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600317 """Run the chromite unit tests."""
318 if test.ChromiteUnitTest():
319 return controller.RETURN_CODE_SUCCESS
320 else:
321 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Evan Hernandez4e388a52019-05-01 12:16:33 -0600322
323
Greg Edelstonf3fc8b62020-03-17 14:20:24 -0600324@faux.empty_success
325@faux.empty_completed_unsuccessfully_error
326@validate.validation_complete
327def ChromitePytest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600328 """Run the chromite unit tests."""
329 # TODO(vapier): Delete this stub.
330 return controller.RETURN_CODE_SUCCESS
Greg Edelstonf3fc8b62020-03-17 14:20:24 -0600331
332
Sloan Johnson9fdd5312022-03-02 00:55:26 +0000333@faux.empty_success
334@faux.empty_completed_unsuccessfully_error
335@validate.validation_complete
336def RulesCrosUnitTest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600337 """Run the rules_cros unit tests."""
338 if test.RulesCrosUnitTest():
339 return controller.RETURN_CODE_SUCCESS
340 else:
341 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Sloan Johnson9fdd5312022-03-02 00:55:26 +0000342
343
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600344@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600345@validate.require("sysroot.path", "sysroot.build_target.name", "chrome_root")
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600346@validate.validation_complete
347def SimpleChromeWorkflowTest(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600348 """Run SimpleChromeWorkflow tests."""
349 if input_proto.goma_config.goma_dir:
350 chromeos_goma_dir = input_proto.goma_config.chromeos_goma_dir or None
351 goma = goma_lib.Goma(
352 input_proto.goma_config.goma_dir,
353 input_proto.goma_config.goma_client_json,
354 stage_name="BuildApiTestSimpleChrome",
355 chromeos_goma_dir=chromeos_goma_dir,
356 )
357 else:
358 goma = None
359 return test.SimpleChromeWorkflowTest(
360 input_proto.sysroot.path,
361 input_proto.sysroot.build_target.name,
362 input_proto.chrome_root,
363 goma,
364 )
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600365
366
Alex Klein076841b2019-08-29 15:19:39 -0600367@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600368@validate.require(
369 "build_target.name", "vm_path.path", "test_harness", "vm_tests"
370)
Alex Klein231d2da2019-07-22 16:44:45 -0600371@validate.validation_complete
372def VmTest(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600373 """Run VM tests."""
374 build_target_name = input_proto.build_target.name
375 vm_path = input_proto.vm_path.path
Alex Kleinc05f3d12019-05-29 14:16:21 -0600376
Alex Klein1699fab2022-09-08 08:46:06 -0600377 test_harness = input_proto.test_harness
Evan Hernandez4e388a52019-05-01 12:16:33 -0600378
Alex Klein1699fab2022-09-08 08:46:06 -0600379 vm_tests = input_proto.vm_tests
Evan Hernandez4e388a52019-05-01 12:16:33 -0600380
Alex Klein1699fab2022-09-08 08:46:06 -0600381 cmd = [
382 "cros_run_test",
383 "--debug",
384 "--no-display",
385 "--copy-on-write",
386 "--board",
387 build_target_name,
388 "--image-path",
389 vm_path,
390 "--%s" % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower(),
391 ]
392 cmd.extend(vm_test.pattern for vm_test in vm_tests)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600393
Alex Klein1699fab2022-09-08 08:46:06 -0600394 if input_proto.ssh_options.port:
395 cmd.extend(["--ssh-port", str(input_proto.ssh_options.port)])
Evan Hernandez4e388a52019-05-01 12:16:33 -0600396
Alex Klein1699fab2022-09-08 08:46:06 -0600397 if input_proto.ssh_options.private_key_path:
398 cmd.extend(
399 ["--private-key", input_proto.ssh_options.private_key_path.path]
400 )
Evan Hernandez4e388a52019-05-01 12:16:33 -0600401
Alex Klein1699fab2022-09-08 08:46:06 -0600402 # TODO(evanhernandez): Find a nice way to pass test_that-args through
403 # the build API. Or obviate them.
404 if test_harness == test_pb2.VmTestRequest.AUTOTEST:
405 cmd.append("--test_that-args=--allow-chrome-crashes")
Evan Hernandez4e388a52019-05-01 12:16:33 -0600406
Alex Klein1699fab2022-09-08 08:46:06 -0600407 with osutils.TempDir(prefix="vm-test-results.") as results_dir:
408 cmd.extend(["--results-dir", results_dir])
409 cros_build_lib.run(cmd, kill_timeout=10 * 60)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600410
411
Alex Klein076841b2019-08-29 15:19:39 -0600412@faux.all_empty
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600413@validate.validation_complete
414def CrosSigningTest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600415 """Run the cros-signing unit tests."""
416 test_runner = os.path.join(
417 constants.SOURCE_ROOT, "cros-signing", "signer", "run_tests.py"
418 )
419 result = cros_build_lib.run([test_runner], check=False)
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600420
Alex Klein1699fab2022-09-08 08:46:06 -0600421 return result.returncode
David Wellingc1433c22021-06-25 16:29:48 +0000422
423
Alex Klein1699fab2022-09-08 08:46:06 -0600424def GetArtifacts(
425 in_proto: common_pb2.ArtifactsByService.Test,
426 chroot: chroot_lib.Chroot,
427 sysroot_class: sysroot_lib.Sysroot,
428 build_target: build_target_lib.BuildTarget,
429 output_dir: str,
430) -> list:
431 """Builds and copies test artifacts to specified output_dir.
David Wellingc1433c22021-06-25 16:29:48 +0000432
Alex Klein1699fab2022-09-08 08:46:06 -0600433 Copies test artifacts to output_dir, returning a list of (output_dir: str)
434 paths to the desired files.
David Wellingc1433c22021-06-25 16:29:48 +0000435
Alex Klein1699fab2022-09-08 08:46:06 -0600436 Args:
437 in_proto: Proto request defining reqs.
438 chroot: The chroot class used for these artifacts.
439 sysroot_class: The sysroot class used for these artifacts.
440 build_target: The build target used for these artifacts.
441 output_dir: The path to write artifacts to.
David Wellingc1433c22021-06-25 16:29:48 +0000442
Alex Klein1699fab2022-09-08 08:46:06 -0600443 Returns:
444 A list of dictionary mappings of ArtifactType to list of paths.
445 """
446 generated = []
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600447
Alex Klein1699fab2022-09-08 08:46:06 -0600448 artifact_types = {
449 in_proto.ArtifactType.CODE_COVERAGE_LLVM_JSON: functools.partial(
450 test.BundleCodeCoverageLlvmJson, build_target.name
451 ),
452 in_proto.ArtifactType.HWQUAL: functools.partial(
453 test.BundleHwqualTarball,
454 build_target.name,
455 packages_service.determine_full_version(),
456 ),
457 }
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600458
Alex Klein1699fab2022-09-08 08:46:06 -0600459 for output_artifact in in_proto.output_artifacts:
460 for artifact_type, func in artifact_types.items():
461 if artifact_type in output_artifact.artifact_types:
462 paths = func(chroot, sysroot_class, output_dir)
463 if paths:
464 generated.append(
465 {
466 "paths": [paths]
467 if isinstance(paths, str)
468 else paths,
469 "type": artifact_type,
470 }
471 )
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600472
Alex Klein1699fab2022-09-08 08:46:06 -0600473 return generated