blob: aa2e180c1e84f86effd2d33992cf2c7adbea1dd2 [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.
Alex Kleind3b84042023-05-19 14:43:59 -060097 packages = [
98 controller_util.deserialize_package_info(x).atom
99 for x in input_proto.packages
100 ]
Navil Perezc0b29a82020-07-07 14:17:48 +0000101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 # Skipped tests.
Alex Kleind3b84042023-05-19 14:43:59 -0600103 blocklist = [
104 controller_util.deserialize_package_info(x).atom
105 for x in input_proto.package_blocklist
106 ]
Alex Kleinf2674462019-05-16 16:47:24 -0600107
Alex Klein1699fab2022-09-08 08:46:06 -0600108 # 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 Perez43fb45d2021-05-14 20:34:24 +0000110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 # Allow call to succeed if no tests were found.
112 testable_packages_optional = input_proto.flags.testable_packages_optional
Navil Perez19dc4792020-09-10 19:06:17 +0000113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
Alex Kleina2e42c42019-04-17 16:13:19 -0600115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 code_coverage = input_proto.flags.code_coverage
Srinivas Hegde0058b8b2022-09-12 22:31:10 +0000117 rust_code_coverage = input_proto.flags.rust_code_coverage
David Burgera9c11872020-07-29 13:32:02 -0600118
Alex Klein1699fab2022-09-08 08:46:06 -0600119 sysroot = sysroot_lib.Sysroot(build_target.root)
Lizzy Presland4feb2372022-01-20 05:16:30 +0000120
Alex Klein1699fab2022-09-08 08:46:06 -0600121 result = test.BuildTargetUnitTest(
122 build_target,
123 packages=packages,
124 blocklist=blocklist,
125 was_built=was_built,
126 code_coverage=code_coverage,
Srinivas Hegde0058b8b2022-09-12 22:31:10 +0000127 rust_code_coverage=rust_code_coverage,
Alex Klein1699fab2022-09-08 08:46:06 -0600128 testable_packages_optional=testable_packages_optional,
129 filter_only_cros_workon=filter_only_cros_workon,
130 )
Alex Kleina2e42c42019-04-17 16:13:19 -0600131
Alex Klein1699fab2022-09-08 08:46:06 -0600132 if not result.success:
133 # Record all failed packages and retrieve log locations.
134 controller_util.retrieve_package_log_paths(
135 result.failed_pkgs, output_proto, sysroot
136 )
137 if result.failed_pkgs:
138 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
139 else:
140 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -0600141
Alex Klein1699fab2022-09-08 08:46:06 -0600142 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Alex Kleine3fc3ca2019-04-30 16:20:55 -0600143
144
Alex Klein1699fab2022-09-08 08:46:06 -0600145SRC_DIR = os.path.join(constants.SOURCE_ROOT, "src")
146PLATFORM_DEV_DIR = os.path.join(SRC_DIR, "platform/dev")
147TEST_SERVICE_DIR = os.path.join(PLATFORM_DEV_DIR, "src/chromiumos/test")
C Shapiro91af1ce2021-06-17 12:42:09 -0500148
149
C Shapiroe15660b2021-06-18 12:49:37 -0500150def _BuildTestServiceContainersResponse(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600151 """Fake success response"""
152 # pylint: disable=unused-argument
153 output_proto.results.append(
154 test_pb2.TestServiceContainerBuildResult(
155 success=test_pb2.TestServiceContainerBuildResult.Success()
156 )
157 )
C Shapiroe15660b2021-06-18 12:49:37 -0500158
159
160def _BuildTestServiceContainersFailedResponse(
Alex Klein1699fab2022-09-08 08:46:06 -0600161 _input_proto, output_proto, _config
162):
163 """Fake failure response"""
C Shapiroe15660b2021-06-18 12:49:37 -0500164
Alex Klein1699fab2022-09-08 08:46:06 -0600165 # pylint: disable=unused-argument
166 output_proto.results.append(
167 test_pb2.TestServiceContainerBuildResult(
168 failure=test_pb2.TestServiceContainerBuildResult.Failure(
169 error_message="fake error"
170 )
171 )
172 )
C Shapiroe15660b2021-06-18 12:49:37 -0500173
174
Alex Klein1699fab2022-09-08 08:46:06 -0600175@validate.constraint("valid docker tag")
Sean McAllister17eed8d2021-09-21 10:41:16 -0600176def _ValidDockerTag(tag):
Alex Klein1699fab2022-09-08 08:46:06 -0600177 """Check that a string meets requirements for Docker tag naming."""
178 # Tags can't start with period or dash
179 if tag[0] in ".-":
180 return "tag can't begin with '.' or '-'"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600181
Alex Klein1699fab2022-09-08 08:46:06 -0600182 # Tags can only consist of [a-zA-Z0-9-_.]
183 allowed_chars = set(string.ascii_letters + string.digits + "-_.")
184 invalid_chars = set(tag) - allowed_chars
185 if invalid_chars:
186 return f'saw one or more invalid characters: [{"".join(invalid_chars)}]'
Sean McAllister17eed8d2021-09-21 10:41:16 -0600187
Alex Klein1699fab2022-09-08 08:46:06 -0600188 # Finally, max tag length is 128 characters
189 if len(tag) > 128:
190 return "maximum tag length is 128 characters"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600191
192
Alex Klein1699fab2022-09-08 08:46:06 -0600193@validate.constraint("valid docker label key")
Sean McAllister17eed8d2021-09-21 10:41:16 -0600194def _ValidDockerLabelKey(key):
Alex Klein1699fab2022-09-08 08:46:06 -0600195 """Check that a string meets requirements for Docker tag naming."""
Sean McAllister17eed8d2021-09-21 10:41:16 -0600196
Alex Klein1699fab2022-09-08 08:46:06 -0600197 # Label keys should start and end with a lowercase letter
198 lowercase = set(string.ascii_lowercase)
199 if not (key[0] in lowercase and key[-1] in lowercase):
200 return "label key doesn't start and end with lowercase letter"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600201
Alex Klein1699fab2022-09-08 08:46:06 -0600202 # Label keys can have lower-case alphanumeric characters, period and dash
203 allowed_chars = set(string.ascii_lowercase + string.digits + "-.")
204 invalid_chars = set(key) - allowed_chars
205 if invalid_chars:
206 return f'saw one or more invalid characters: [{"".join(invalid_chars)}]'
Sean McAllister17eed8d2021-09-21 10:41:16 -0600207
Alex Klein1699fab2022-09-08 08:46:06 -0600208 # Repeated . and - aren't allowed
209 for char in ".-":
210 if char * 2 in key:
211 return f"'{char}' can't be repeated in label key"
Sean McAllister17eed8d2021-09-21 10:41:16 -0600212
213
C Shapiroe15660b2021-06-18 12:49:37 -0500214@faux.success(_BuildTestServiceContainersResponse)
215@faux.error(_BuildTestServiceContainersFailedResponse)
Brian Norris7ac08e22023-06-15 11:03:01 -0700216@validate.require("build_target.name", "chroot.path", "chroot.out_path")
Alex Klein1699fab2022-09-08 08:46:06 -0600217@validate.check_constraint("tags", _ValidDockerTag)
218@validate.check_constraint("labels", _ValidDockerLabelKey)
C Shapiro91af1ce2021-06-17 12:42:09 -0500219@validate.validation_complete
Sean McAllister15fa8332021-09-27 12:24:12 -0600220def BuildTestServiceContainers(
221 input_proto: test_pb2.BuildTestServiceContainersRequest,
Alex Klein1699fab2022-09-08 08:46:06 -0600222 output_proto: test_pb2.BuildTestServiceContainersResponse,
223 _config,
224):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700225 """Build docker containers for all test services and push them to gcr.io."""
Alex Klein1699fab2022-09-08 08:46:06 -0600226 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
227 chroot = controller_util.ParseChroot(input_proto.chroot)
228 sysroot = sysroot_lib.Sysroot(build_target.root)
C Shapiro91af1ce2021-06-17 12:42:09 -0500229
Alex Klein1699fab2022-09-08 08:46:06 -0600230 tags = ",".join(input_proto.tags)
231 labels = (f"{key}={value}" for key, value in input_proto.labels.items())
Sean McAllister15fa8332021-09-27 12:24:12 -0600232
Alex Klein1699fab2022-09-08 08:46:06 -0600233 build_script = os.path.join(
234 TEST_SERVICE_DIR, "python/src/docker_libs/cli/build-dockerimages.py"
235 )
236 human_name = "Service Builder"
Sean McAllister15fa8332021-09-27 12:24:12 -0600237
Alex Klein1699fab2022-09-08 08:46:06 -0600238 with osutils.TempDir(prefix="test_container") as tempdir:
239 result_file = "metadata.jsonpb"
240 output_path = os.path.join(tempdir, result_file)
241 # Note that we use an output file instead of stdout to avoid any issues
242 # with maintaining stdout hygiene. Stdout and stderr are combined to
243 # form the error log in response to any errors.
Brian Norris7ac08e22023-06-15 11:03:01 -0700244 cmd = [
245 build_script,
246 chroot.path,
247 sysroot.path,
248 "--out-dir",
249 chroot.out_path,
250 ]
Sean McAllistere6a4ae22021-10-21 13:17:08 -0600251
Alex Klein1699fab2022-09-08 08:46:06 -0600252 if input_proto.HasField("repository"):
253 cmd += ["--host", input_proto.repository.hostname]
254 cmd += ["--project", input_proto.repository.project]
Sean McAllistere6a4ae22021-10-21 13:17:08 -0600255
Alex Klein1699fab2022-09-08 08:46:06 -0600256 cmd += ["--tags", tags]
257 cmd += ["--output", output_path]
Derek Beckettb28b5372022-04-15 13:08:32 -0700258
Alex Klein1699fab2022-09-08 08:46:06 -0600259 # Translate generator to comma separated string.
260 ct_labels = ",".join(labels)
261 cmd += ["--labels", ct_labels]
262 cmd += ["--build_all"]
263 cmd += ["--upload"]
Derek Beckettcbe64c82022-05-05 15:00:18 -0700264
Alex Klein1699fab2022-09-08 08:46:06 -0600265 cmd_result = cros_build_lib.run(
266 cmd, check=False, stderr=subprocess.STDOUT, stdout=True
267 )
Derek Becketta8d9fd22022-04-27 10:44:52 -0700268
Alex Klein1699fab2022-09-08 08:46:06 -0600269 if cmd_result.returncode != 0:
270 # When failing, just record a fail response with the builder name.
271 logging.debug(
272 "%s build failed.\nStdout:\n%s\nStderr:\n%s",
273 human_name,
274 cmd_result.stdout,
275 cmd_result.stderr,
276 )
277 result = test_pb2.TestServiceContainerBuildResult()
278 result.name = human_name
279 image_info = container_metadata_pb2.ContainerImageInfo()
280 result.failure.CopyFrom(
281 test_pb2.TestServiceContainerBuildResult.Failure(
282 error_message=cmd_result.stdout
283 )
284 )
285 output_proto.results.append(result)
C Shapiro91af1ce2021-06-17 12:42:09 -0500286
Alex Klein1699fab2022-09-08 08:46:06 -0600287 else:
288 logging.debug(
289 "%s build succeeded.\nStdout:\n%s\nStderr:\n%s",
290 human_name,
291 cmd_result.stdout,
292 cmd_result.stderr,
293 )
294 files = os.listdir(tempdir)
295 # Iterate through the tempdir to output metadata files.
296 for file in files:
297 if result_file in file:
298 output_path = os.path.join(tempdir, file)
Derek Beckett344b5a82022-05-09 17:21:45 -0700299
Alex Kleinab87ceb2023-01-24 12:00:51 -0700300 # build-dockerimages.py will append the service name to
301 # outputfile with an underscore.
Alex Klein1699fab2022-09-08 08:46:06 -0600302 human_name = file.split("_")[-1]
Derek Beckett344b5a82022-05-09 17:21:45 -0700303
Alex Klein1699fab2022-09-08 08:46:06 -0600304 result = test_pb2.TestServiceContainerBuildResult()
305 result.name = human_name
306 image_info = container_metadata_pb2.ContainerImageInfo()
307 json_format.Parse(osutils.ReadFile(output_path), image_info)
308 result.success.CopyFrom(
309 test_pb2.TestServiceContainerBuildResult.Success(
310 image_info=image_info
311 )
312 )
313 output_proto.results.append(result)
Derek Beckett344b5a82022-05-09 17:21:45 -0700314
C Shapiro91af1ce2021-06-17 12:42:09 -0500315
Michael Mortensen7a860eb2019-12-03 20:25:15 -0700316@faux.empty_success
317@faux.empty_completed_unsuccessfully_error
Alex Klein231d2da2019-07-22 16:44:45 -0600318@validate.validation_complete
319def ChromiteUnitTest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600320 """Run the chromite unit tests."""
321 if test.ChromiteUnitTest():
322 return controller.RETURN_CODE_SUCCESS
323 else:
324 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Evan Hernandez4e388a52019-05-01 12:16:33 -0600325
326
Greg Edelstonf3fc8b62020-03-17 14:20:24 -0600327@faux.empty_success
328@faux.empty_completed_unsuccessfully_error
329@validate.validation_complete
330def ChromitePytest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600331 """Run the chromite unit tests."""
332 # TODO(vapier): Delete this stub.
333 return controller.RETURN_CODE_SUCCESS
Greg Edelstonf3fc8b62020-03-17 14:20:24 -0600334
335
Sloan Johnson9fdd5312022-03-02 00:55:26 +0000336@faux.empty_success
337@faux.empty_completed_unsuccessfully_error
338@validate.validation_complete
339def RulesCrosUnitTest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600340 """Run the rules_cros unit tests."""
341 if test.RulesCrosUnitTest():
342 return controller.RETURN_CODE_SUCCESS
343 else:
344 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Sloan Johnson9fdd5312022-03-02 00:55:26 +0000345
346
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600347@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600348@validate.require("sysroot.path", "sysroot.build_target.name", "chrome_root")
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600349@validate.validation_complete
350def SimpleChromeWorkflowTest(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600351 """Run SimpleChromeWorkflow tests."""
352 if input_proto.goma_config.goma_dir:
353 chromeos_goma_dir = input_proto.goma_config.chromeos_goma_dir or None
354 goma = goma_lib.Goma(
355 input_proto.goma_config.goma_dir,
Alex Klein1699fab2022-09-08 08:46:06 -0600356 stage_name="BuildApiTestSimpleChrome",
357 chromeos_goma_dir=chromeos_goma_dir,
358 )
359 else:
360 goma = None
361 return test.SimpleChromeWorkflowTest(
362 input_proto.sysroot.path,
363 input_proto.sysroot.build_target.name,
364 input_proto.chrome_root,
365 goma,
366 )
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600367
368
Alex Klein076841b2019-08-29 15:19:39 -0600369@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600370@validate.require(
371 "build_target.name", "vm_path.path", "test_harness", "vm_tests"
372)
Alex Klein231d2da2019-07-22 16:44:45 -0600373@validate.validation_complete
374def VmTest(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600375 """Run VM tests."""
376 build_target_name = input_proto.build_target.name
377 vm_path = input_proto.vm_path.path
Alex Kleinc05f3d12019-05-29 14:16:21 -0600378
Alex Klein1699fab2022-09-08 08:46:06 -0600379 test_harness = input_proto.test_harness
Evan Hernandez4e388a52019-05-01 12:16:33 -0600380
Alex Klein1699fab2022-09-08 08:46:06 -0600381 vm_tests = input_proto.vm_tests
Evan Hernandez4e388a52019-05-01 12:16:33 -0600382
Alex Klein1699fab2022-09-08 08:46:06 -0600383 cmd = [
384 "cros_run_test",
385 "--debug",
386 "--no-display",
387 "--copy-on-write",
388 "--board",
389 build_target_name,
390 "--image-path",
391 vm_path,
392 "--%s" % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower(),
393 ]
394 cmd.extend(vm_test.pattern for vm_test in vm_tests)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600395
Alex Klein1699fab2022-09-08 08:46:06 -0600396 if input_proto.ssh_options.port:
397 cmd.extend(["--ssh-port", str(input_proto.ssh_options.port)])
Evan Hernandez4e388a52019-05-01 12:16:33 -0600398
Alex Klein1699fab2022-09-08 08:46:06 -0600399 if input_proto.ssh_options.private_key_path:
400 cmd.extend(
401 ["--private-key", input_proto.ssh_options.private_key_path.path]
402 )
Evan Hernandez4e388a52019-05-01 12:16:33 -0600403
Alex Klein1699fab2022-09-08 08:46:06 -0600404 # TODO(evanhernandez): Find a nice way to pass test_that-args through
405 # the build API. Or obviate them.
406 if test_harness == test_pb2.VmTestRequest.AUTOTEST:
407 cmd.append("--test_that-args=--allow-chrome-crashes")
Evan Hernandez4e388a52019-05-01 12:16:33 -0600408
Alex Klein1699fab2022-09-08 08:46:06 -0600409 with osutils.TempDir(prefix="vm-test-results.") as results_dir:
410 cmd.extend(["--results-dir", results_dir])
411 cros_build_lib.run(cmd, kill_timeout=10 * 60)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600412
413
Alex Klein076841b2019-08-29 15:19:39 -0600414@faux.all_empty
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600415@validate.validation_complete
416def CrosSigningTest(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600417 """Run the cros-signing unit tests."""
418 test_runner = os.path.join(
419 constants.SOURCE_ROOT, "cros-signing", "signer", "run_tests.py"
420 )
421 result = cros_build_lib.run([test_runner], check=False)
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600422
Alex Klein1699fab2022-09-08 08:46:06 -0600423 return result.returncode
David Wellingc1433c22021-06-25 16:29:48 +0000424
425
Alex Klein1699fab2022-09-08 08:46:06 -0600426def GetArtifacts(
427 in_proto: common_pb2.ArtifactsByService.Test,
428 chroot: chroot_lib.Chroot,
429 sysroot_class: sysroot_lib.Sysroot,
430 build_target: build_target_lib.BuildTarget,
431 output_dir: str,
432) -> list:
433 """Builds and copies test artifacts to specified output_dir.
David Wellingc1433c22021-06-25 16:29:48 +0000434
Alex Klein1699fab2022-09-08 08:46:06 -0600435 Copies test artifacts to output_dir, returning a list of (output_dir: str)
436 paths to the desired files.
David Wellingc1433c22021-06-25 16:29:48 +0000437
Alex Klein1699fab2022-09-08 08:46:06 -0600438 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600439 in_proto: Proto request defining reqs.
440 chroot: The chroot class used for these artifacts.
441 sysroot_class: The sysroot class used for these artifacts.
442 build_target: The build target used for these artifacts.
443 output_dir: The path to write artifacts to.
David Wellingc1433c22021-06-25 16:29:48 +0000444
Alex Klein1699fab2022-09-08 08:46:06 -0600445 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -0600446 A list of dictionary mappings of ArtifactType to list of paths.
Alex Klein1699fab2022-09-08 08:46:06 -0600447 """
448 generated = []
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600449
Alex Klein1699fab2022-09-08 08:46:06 -0600450 artifact_types = {
451 in_proto.ArtifactType.CODE_COVERAGE_LLVM_JSON: functools.partial(
452 test.BundleCodeCoverageLlvmJson, build_target.name
453 ),
Srinivas Hegdeb6fe4ac2022-09-22 00:04:40 +0000454 in_proto.ArtifactType.CODE_COVERAGE_RUST_LLVM_JSON: functools.partial(
455 test.BundleCodeCoverageRustLlvmJson, build_target.name
456 ),
Alex Klein1699fab2022-09-08 08:46:06 -0600457 in_proto.ArtifactType.HWQUAL: functools.partial(
458 test.BundleHwqualTarball,
459 build_target.name,
460 packages_service.determine_full_version(),
461 ),
Charles Liuc71379c2022-10-28 04:02:34 +0000462 in_proto.ArtifactType.CODE_COVERAGE_GOLANG: functools.partial(
463 test.BundleCodeCoverageGolang
464 ),
Alex Klein1699fab2022-09-08 08:46:06 -0600465 }
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600466
Alex Klein1699fab2022-09-08 08:46:06 -0600467 for output_artifact in in_proto.output_artifacts:
468 for artifact_type, func in artifact_types.items():
469 if artifact_type in output_artifact.artifact_types:
Josiah Hounyode113e52022-11-30 06:30:33 +0000470 try:
471 if (
472 artifact_type
473 == in_proto.ArtifactType.CODE_COVERAGE_GOLANG
474 ):
475 paths = func(chroot, output_dir)
476 else:
477 paths = func(chroot, sysroot_class, output_dir)
478 except Exception as e:
479 generated.append(
480 {
481 "type": artifact_type,
482 "failed": True,
483 "failure_reason": str(e),
484 }
485 )
486 artifact_name = (
487 common_pb2.ArtifactsByService.Test.ArtifactType.Name(
488 artifact_type
489 )
490 )
491 logging.warning(
492 "%s artifact generation failed with exception %s",
493 artifact_name,
494 e,
495 )
496 logging.warning("traceback:\n%s", traceback.format_exc())
497 continue
Alex Klein1699fab2022-09-08 08:46:06 -0600498 if paths:
499 generated.append(
500 {
501 "paths": [paths]
502 if isinstance(paths, str)
503 else paths,
504 "type": artifact_type,
505 }
506 )
George Engelbrecht764b1cd2021-06-18 17:01:07 -0600507
Alex Klein1699fab2022-09-08 08:46:06 -0600508 return generated