blob: 87087da4c8dd82dfaa10d9b0cc3125849e972c24 [file] [log] [blame]
Alex Kleinc5403d62019-04-03 09:34:59 -06001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Test controller.
7
8Handles all testing related functionality, it is not itself a test.
9"""
10
11from __future__ import print_function
12
Alex Kleina2e42c42019-04-17 16:13:19 -060013import os
Mike Frysingeref94e4c2020-02-10 23:59:54 -050014import sys
Alex Kleina2e42c42019-04-17 16:13:19 -060015
Alex Klein8cb365a2019-05-15 16:24:53 -060016from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060017from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060018from chromite.api import validate
Will Bradley59e0a152019-11-12 12:59:17 -070019from chromite.api.metrics import deserialize_metrics_log
Alex Kleina2e42c42019-04-17 16:13:19 -060020from chromite.api.controller import controller_util
Evan Hernandez4e388a52019-05-01 12:16:33 -060021from chromite.api.gen.chromite.api import test_pb2
Michael Mortensenc28d6f12019-10-03 13:34:51 -060022from chromite.cbuildbot import goma_util
Alex Kleina2e42c42019-04-17 16:13:19 -060023from chromite.lib import constants
Alex Kleinc5403d62019-04-03 09:34:59 -060024from chromite.lib import cros_build_lib
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060025from chromite.lib import image_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060026from chromite.lib import osutils
Michael Mortensen82cd62d2019-12-01 14:58:54 -070027from chromite.lib import portage_util
Alex Kleinc5403d62019-04-03 09:34:59 -060028from chromite.lib import sysroot_lib
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060029from chromite.scripts import cros_set_lsb_release
Alex Kleinc5403d62019-04-03 09:34:59 -060030from chromite.service import test
Mike Frysingere652ba12019-09-08 00:57:43 -040031from chromite.utils import key_value_store
Will Bradley59e0a152019-11-12 12:59:17 -070032from chromite.utils import metrics
Alex Kleinc5403d62019-04-03 09:34:59 -060033
34
Mike Frysingeref94e4c2020-02-10 23:59:54 -050035assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
36
37
Michael Mortensen85d38402019-12-12 09:50:29 -070038@faux.empty_success
39@faux.empty_completed_unsuccessfully_error
Alex Klein231d2da2019-07-22 16:44:45 -060040def DebugInfoTest(input_proto, _output_proto, config):
Alex Kleinc5403d62019-04-03 09:34:59 -060041 """Run the debug info tests."""
42 sysroot_path = input_proto.sysroot.path
43 target_name = input_proto.sysroot.build_target.name
44
45 if not sysroot_path:
46 if target_name:
47 sysroot_path = cros_build_lib.GetSysroot(target_name)
48 else:
49 cros_build_lib.Die("The sysroot path or the sysroot's build target name "
50 'must be provided.')
51
52 # We could get away with out this, but it's a cheap check.
53 sysroot = sysroot_lib.Sysroot(sysroot_path)
54 if not sysroot.Exists():
55 cros_build_lib.Die('The provided sysroot does not exist.')
56
Alex Klein231d2da2019-07-22 16:44:45 -060057 if config.validate_only:
58 return controller.RETURN_CODE_VALID_INPUT
59
Alex Klein8cb365a2019-05-15 16:24:53 -060060 if test.DebugInfoTest(sysroot_path):
61 return controller.RETURN_CODE_SUCCESS
62 else:
63 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -060064
65
Michael Mortensen82cd62d2019-12-01 14:58:54 -070066def _BuildTargetUnitTestResponse(input_proto, output_proto, _config):
67 """Add tarball path to a successful response."""
68 output_proto.tarball_path = os.path.join(input_proto.result_path,
69 'unit_tests.tar')
70
71
72def _BuildTargetUnitTestFailedResponse(_input_proto, output_proto, _config):
73 """Add failed packages to a failed response."""
74 packages = ['foo/bar', 'cat/pkg']
75 failed_cpvs = [portage_util.SplitCPV(p, strict=False) for p in packages]
76 for cpv in failed_cpvs:
77 package_info = output_proto.failed_packages.add()
78 controller_util.CPVToPackageInfo(cpv, package_info)
79
80
81@faux.success(_BuildTargetUnitTestResponse)
82@faux.error(_BuildTargetUnitTestFailedResponse)
Alex Klein2b236722019-06-19 15:44:26 -060083@validate.require('build_target.name', 'result_path')
Alex Klein231d2da2019-07-22 16:44:45 -060084@validate.exists('result_path')
85@validate.validation_complete
Will Bradley59e0a152019-11-12 12:59:17 -070086@metrics.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -060087def BuildTargetUnitTest(input_proto, output_proto, _config):
Alex Kleina2e42c42019-04-17 16:13:19 -060088 """Run a build target's ebuild unit tests."""
89 # Required args.
Alex Kleina2e42c42019-04-17 16:13:19 -060090 result_path = input_proto.result_path
91
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060092 # Method flags.
Alex Klein38c7d9e2019-05-08 09:31:19 -060093 # 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).
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060095 was_built = not input_proto.flags.empty_sysroot
96
Navil Perezc0b29a82020-07-07 14:17:48 +000097 # Packages to be tested.
98 packages_package_info = input_proto.packages
99 packages = []
100 for package_info in packages_package_info:
101 packages.append(controller_util.PackageInfoToString(package_info))
102
Alex Kleinf2674462019-05-16 16:47:24 -0600103 # Skipped tests.
104 blacklisted_package_info = input_proto.package_blacklist
105 blacklist = []
106 for package_info in blacklisted_package_info:
107 blacklist.append(controller_util.PackageInfoToString(package_info))
108
Alex Klein26e472b2020-03-10 14:35:01 -0600109 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
Alex Klein38c7d9e2019-05-08 09:31:19 -0600110 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Kleina2e42c42019-04-17 16:13:19 -0600111
David Burgera9c11872020-07-29 13:32:02 -0600112 code_coverage = input_proto.flags.code_coverage
113
Navil Perezc0b29a82020-07-07 14:17:48 +0000114 result = test.BuildTargetUnitTest(
115 build_target,
116 chroot,
117 packages=packages,
118 blacklist=blacklist,
David Burgera9c11872020-07-29 13:32:02 -0600119 was_built=was_built,
120 code_coverage=code_coverage)
Alex Kleina2e42c42019-04-17 16:13:19 -0600121
Alex Klein38c7d9e2019-05-08 09:31:19 -0600122 if not result.success:
123 # Failed to run tests or some tests failed.
124 # Record all failed packages.
125 for cpv in result.failed_cpvs:
126 package_info = output_proto.failed_packages.add()
127 controller_util.CPVToPackageInfo(cpv, package_info)
128 if result.failed_cpvs:
129 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
130 else:
131 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -0600132
Alex Klein38c7d9e2019-05-08 09:31:19 -0600133 sysroot = sysroot_lib.Sysroot(build_target.root)
134 tarball = test.BuildTargetUnitTestTarball(chroot, sysroot, result_path)
135 if tarball:
136 output_proto.tarball_path = tarball
Will Bradley59e0a152019-11-12 12:59:17 -0700137 deserialize_metrics_log(output_proto.events, prefix=build_target.name)
Alex Kleine3fc3ca2019-04-30 16:20:55 -0600138
139
Michael Mortensen7a860eb2019-12-03 20:25:15 -0700140@faux.empty_success
141@faux.empty_completed_unsuccessfully_error
Alex Klein231d2da2019-07-22 16:44:45 -0600142@validate.validation_complete
143def ChromiteUnitTest(_input_proto, _output_proto, _config):
Alex Kleine3fc3ca2019-04-30 16:20:55 -0600144 """Run the chromite unit tests."""
Chris McDonald1be467d2020-04-07 16:08:56 -0600145 cmd = [os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests'), '--py2']
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500146 result = cros_build_lib.run(cmd, check=False)
Alex Klein8cb365a2019-05-15 16:24:53 -0600147 if result.returncode == 0:
148 return controller.RETURN_CODE_SUCCESS
149 else:
150 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Evan Hernandez4e388a52019-05-01 12:16:33 -0600151
152
Greg Edelstonf3fc8b62020-03-17 14:20:24 -0600153@faux.empty_success
154@faux.empty_completed_unsuccessfully_error
155@validate.validation_complete
156def ChromitePytest(_input_proto, _output_proto, _config):
157 """Run the chromite unit tests."""
158 if test.ChromitePytest():
159 return controller.RETURN_CODE_SUCCESS
160 else:
161 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
162
163
Michael Mortensenc28d6f12019-10-03 13:34:51 -0600164@faux.all_empty
165@validate.require('sysroot.path', 'sysroot.build_target.name', 'chrome_root')
166@validate.validation_complete
167def SimpleChromeWorkflowTest(input_proto, _output_proto, _config):
168 """Run SimpleChromeWorkflow tests."""
169 if input_proto.goma_config.goma_dir:
170 chromeos_goma_dir = input_proto.goma_config.chromeos_goma_dir or None
171 goma = goma_util.Goma(
172 input_proto.goma_config.goma_dir,
173 input_proto.goma_config.goma_client_json,
174 stage_name='BuildApiTestSimpleChrome',
175 chromeos_goma_dir=chromeos_goma_dir)
176 else:
177 goma = None
178 return test.SimpleChromeWorkflowTest(input_proto.sysroot.path,
179 input_proto.sysroot.build_target.name,
180 input_proto.chrome_root,
181 goma)
182
183
Alex Klein076841b2019-08-29 15:19:39 -0600184@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -0600185@validate.require('build_target.name', 'vm_path.path', 'test_harness',
186 'vm_tests')
Alex Klein231d2da2019-07-22 16:44:45 -0600187@validate.validation_complete
188def VmTest(input_proto, _output_proto, _config):
Evan Hernandez4e388a52019-05-01 12:16:33 -0600189 """Run VM tests."""
Alex Klein2b236722019-06-19 15:44:26 -0600190 build_target_name = input_proto.build_target.name
191 vm_path = input_proto.vm_path.path
Alex Kleinc05f3d12019-05-29 14:16:21 -0600192
Evan Hernandez4e388a52019-05-01 12:16:33 -0600193 test_harness = input_proto.test_harness
Evan Hernandez4e388a52019-05-01 12:16:33 -0600194
195 vm_tests = input_proto.vm_tests
Evan Hernandez4e388a52019-05-01 12:16:33 -0600196
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700197 cmd = ['cros_run_test', '--debug', '--no-display', '--copy-on-write',
Alex Klein2b236722019-06-19 15:44:26 -0600198 '--board', build_target_name, '--image-path', vm_path,
Evan Hernandez4e388a52019-05-01 12:16:33 -0600199 '--%s' % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower()]
200 cmd.extend(vm_test.pattern for vm_test in vm_tests)
201
202 if input_proto.ssh_options.port:
203 cmd.extend(['--ssh-port', str(input_proto.ssh_options.port)])
204
205 if input_proto.ssh_options.private_key_path:
Alex Kleinaa705412019-06-04 15:00:30 -0600206 cmd.extend(['--private-key', input_proto.ssh_options.private_key_path.path])
Evan Hernandez4e388a52019-05-01 12:16:33 -0600207
208 # TODO(evanhernandez): Find a nice way to pass test_that-args through
209 # the build API. Or obviate them.
210 if test_harness == test_pb2.VmTestRequest.AUTOTEST:
211 cmd.append('--test_that-args=--whitelist-chrome-crashes')
212
213 with osutils.TempDir(prefix='vm-test-results.') as results_dir:
214 cmd.extend(['--results-dir', results_dir])
Mike Frysinger45602c72019-09-22 02:15:11 -0400215 cros_build_lib.run(cmd, kill_timeout=10 * 60)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600216
217
Alex Klein076841b2019-08-29 15:19:39 -0600218@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600219@validate.require('image_payload.path.path', 'cache_payloads')
220@validate.validation_complete
221def MoblabVmTest(input_proto, _output_proto, _config):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600222 """Run Moblab VM tests."""
Evan Hernandez655e8042019-06-13 12:50:44 -0600223 chroot = controller_util.ParseChroot(input_proto.chroot)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600224 image_payload_dir = input_proto.image_payload.path.path
225 cache_payload_dirs = [cp.path.path for cp in input_proto.cache_payloads]
226
227 # Autotest and Moblab depend on the builder path, so we must read it from
228 # the image.
229 image_file = os.path.join(image_payload_dir, constants.TEST_IMAGE_BIN)
Evan Hernandez40ee7452019-06-13 12:51:43 -0600230 with osutils.TempDir() as mount_dir:
231 with image_lib.LoopbackPartitions(image_file, destination=mount_dir) as lp:
232 # The file we want is /etc/lsb-release, which lives in the ROOT-A
233 # disk partition.
234 partition_paths = lp.Mount([constants.PART_ROOT_A])
235 assert len(partition_paths) == 1, (
236 'expected one partition path, got: %r' % partition_paths)
237 partition_path = partition_paths[0]
238 lsb_release_file = os.path.join(partition_path,
239 constants.LSB_RELEASE_PATH.strip('/'))
Mike Frysingere652ba12019-09-08 00:57:43 -0400240 lsb_release_kvs = key_value_store.LoadFile(lsb_release_file)
Evan Hernandez40ee7452019-06-13 12:51:43 -0600241 builder = lsb_release_kvs.get(cros_set_lsb_release.LSB_KEY_BUILDER_PATH)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600242
243 if not builder:
244 cros_build_lib.Die('Image did not contain key %s in %s',
245 cros_set_lsb_release.LSB_KEY_BUILDER_PATH,
246 constants.LSB_RELEASE_PATH)
247
248 # Now we can run the tests.
Evan Hernandez655e8042019-06-13 12:50:44 -0600249 with chroot.tempdir() as workspace_dir, chroot.tempdir() as results_dir:
Alex Kleine3c0df62019-11-13 13:32:13 -0700250 # Convert the results directory to an absolute chroot directory.
251 chroot_results_dir = '/%s' % os.path.relpath(results_dir, chroot.path)
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600252 vms = test.CreateMoblabVm(workspace_dir, chroot.path, image_payload_dir)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600253 cache_dir = test.PrepareMoblabVmImageCache(vms, builder, cache_payload_dirs)
Alex Kleine3c0df62019-11-13 13:32:13 -0700254 test.RunMoblabVmTest(chroot, vms, builder, cache_dir, chroot_results_dir)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600255 test.ValidateMoblabVmTest(results_dir)
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600256
257
Alex Klein076841b2019-08-29 15:19:39 -0600258@faux.all_empty
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600259@validate.validation_complete
260def CrosSigningTest(_input_proto, _output_proto, _config):
261 """Run the cros-signing unit tests."""
262 test_runner = os.path.join(constants.SOURCE_ROOT, 'cros-signing', 'signer',
263 'run_tests.py')
Mike Frysingerf5a3b2d2019-12-12 14:36:17 -0500264 result = cros_build_lib.run([test_runner], check=False)
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600265
266 return result.returncode