Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | Handles all testing related functionality, it is not itself a test. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import print_function |
| 12 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 13 | import os |
| 14 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 15 | from chromite.api import controller |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 16 | from chromite.api.controller import controller_util |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 17 | from chromite.api.gen.chromite.api import test_pb2 |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 18 | from chromite.api.gen.chromiumos import common_pb2 |
| 19 | from chromite.cbuildbot import commands |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 20 | from chromite.lib import constants |
Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 21 | from chromite.lib import cros_build_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 22 | from chromite.lib import failures_lib |
| 23 | from chromite.lib import osutils |
| 24 | from chromite.lib import portage_util |
Alex Klein | c5403d6 | 2019-04-03 09:34:59 -0600 | [diff] [blame] | 25 | from chromite.lib import sysroot_lib |
| 26 | from chromite.service import test |
| 27 | |
| 28 | |
| 29 | def DebugInfoTest(input_proto, _output_proto): |
| 30 | """Run the debug info tests.""" |
| 31 | sysroot_path = input_proto.sysroot.path |
| 32 | target_name = input_proto.sysroot.build_target.name |
| 33 | |
| 34 | if not sysroot_path: |
| 35 | if target_name: |
| 36 | sysroot_path = cros_build_lib.GetSysroot(target_name) |
| 37 | else: |
| 38 | cros_build_lib.Die("The sysroot path or the sysroot's build target name " |
| 39 | 'must be provided.') |
| 40 | |
| 41 | # We could get away with out this, but it's a cheap check. |
| 42 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 43 | if not sysroot.Exists(): |
| 44 | cros_build_lib.Die('The provided sysroot does not exist.') |
| 45 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 46 | if test.DebugInfoTest(sysroot_path): |
| 47 | return controller.RETURN_CODE_SUCCESS |
| 48 | else: |
| 49 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 50 | |
| 51 | |
| 52 | def BuildTargetUnitTest(input_proto, output_proto): |
| 53 | """Run a build target's ebuild unit tests.""" |
| 54 | # Required args. |
| 55 | board = input_proto.build_target.name |
| 56 | result_path = input_proto.result_path |
| 57 | |
| 58 | if not board: |
| 59 | cros_build_lib.Die('build_target.name is required.') |
| 60 | if not result_path: |
| 61 | cros_build_lib.Die('result_path is required.') |
| 62 | |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 63 | # Method flags. |
| 64 | # An empty sysroot means build packages was not run. |
| 65 | was_built = not input_proto.flags.empty_sysroot |
| 66 | |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 67 | # Skipped tests. |
| 68 | blacklisted_package_info = input_proto.package_blacklist |
| 69 | blacklist = [] |
| 70 | for package_info in blacklisted_package_info: |
| 71 | blacklist.append(controller_util.PackageInfoToString(package_info)) |
| 72 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 73 | # Chroot handling. |
| 74 | chroot = input_proto.chroot.path |
| 75 | cache_dir = input_proto.chroot.cache_dir |
| 76 | |
| 77 | chroot_args = [] |
| 78 | if chroot: |
| 79 | chroot_args.extend(['--chroot', chroot]) |
| 80 | else: |
| 81 | chroot = constants.DEFAULT_CHROOT_PATH |
| 82 | |
| 83 | if cache_dir: |
| 84 | chroot_args.extend(['--cache_dir', cache_dir]) |
| 85 | |
| 86 | # TODO(crbug.com/954609) Service implementation. |
| 87 | extra_env = {'USE': 'chrome_internal'} |
| 88 | base_dir = os.path.join(chroot, 'tmp') |
| 89 | # The called code also sets the status file in some cases, but the hacky |
| 90 | # call makes it not always work, so set up our own just in case. |
| 91 | with osutils.TempDir(base_dir=base_dir) as tempdir: |
| 92 | full_sf_path = os.path.join(tempdir, 'status_file') |
| 93 | chroot_sf_path = full_sf_path.replace(chroot, '') |
| 94 | extra_env[constants.PARALLEL_EMERGE_STATUS_FILE_ENVVAR] = chroot_sf_path |
| 95 | |
| 96 | try: |
| 97 | commands.RunUnitTests(constants.SOURCE_ROOT, board, extra_env=extra_env, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 98 | chroot_args=chroot_args, build_stage=was_built, |
| 99 | blacklist=blacklist) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 100 | except failures_lib.PackageBuildFailure as e: |
| 101 | # Add the failed packages. |
| 102 | for pkg in e.failed_packages: |
| 103 | cpv = portage_util.SplitCPV(pkg, strict=False) |
| 104 | package_info = output_proto.failed_packages.add() |
| 105 | controller_util.CPVToPackageInfo(cpv, package_info) |
| 106 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 107 | if e.failed_packages: |
| 108 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
| 109 | else: |
| 110 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 111 | except failures_lib.BuildScriptFailure: |
| 112 | # Check our status file for failed packages in case the calling code's |
| 113 | # file didn't get set. |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 114 | failed_packages = portage_util.ParseParallelEmergeStatusFile(full_sf_path) |
| 115 | for cpv in failed_packages: |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 116 | package_info = output_proto.failed_packages.add() |
| 117 | controller_util.CPVToPackageInfo(cpv, package_info) |
| 118 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 119 | if failed_packages: |
| 120 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
| 121 | else: |
| 122 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 123 | |
| 124 | tarball = _BuildUnittestTarball(chroot, board, result_path) |
| 125 | if tarball: |
| 126 | output_proto.tarball_path = tarball |
| 127 | |
| 128 | |
| 129 | def _BuildUnittestTarball(chroot, board, result_path): |
| 130 | """Build the unittest tarball.""" |
| 131 | tarball = 'unit_tests.tar' |
| 132 | tarball_path = os.path.join(result_path, tarball) |
| 133 | |
| 134 | cwd = os.path.join(chroot, 'build', board, constants.UNITTEST_PKG_PATH) |
| 135 | |
| 136 | result = cros_build_lib.CreateTarball(tarball_path, cwd, chroot=chroot, |
| 137 | compression=cros_build_lib.COMP_NONE, |
| 138 | error_code_ok=True) |
| 139 | |
| 140 | return tarball_path if result.returncode == 0 else None |
Alex Klein | e3fc3ca | 2019-04-30 16:20:55 -0600 | [diff] [blame] | 141 | |
| 142 | |
| 143 | def ChromiteUnitTest(_input_proto, _output_proto): |
| 144 | """Run the chromite unit tests.""" |
| 145 | cmd = [os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests')] |
| 146 | result = cros_build_lib.RunCommand(cmd, error_code_ok=True) |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 147 | if result.returncode == 0: |
| 148 | return controller.RETURN_CODE_SUCCESS |
| 149 | else: |
| 150 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 151 | |
| 152 | |
| 153 | def VmTest(input_proto, _output_proto): |
| 154 | """Run VM tests.""" |
| 155 | if not input_proto.HasField('build_target'): |
| 156 | cros_build_lib.Die('build_target is required') |
| 157 | build_target = input_proto.build_target |
| 158 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 159 | vm_image = input_proto.vm_image |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 160 | test_vm_image = input_proto.test_vm_image |
| 161 | if not vm_image.path and not test_vm_image.path: |
| 162 | cros_build_lib.Die('vm_image or test_vm_image is required.') |
| 163 | if test_vm_image.path: |
| 164 | if test_vm_image.type != common_pb2.TEST_VM: |
| 165 | cros_build_lib.Die('Must provide a test VM image.') |
| 166 | vm_image = test_vm_image |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 167 | |
| 168 | test_harness = input_proto.test_harness |
| 169 | if test_harness == test_pb2.VmTestRequest.UNSPECIFIED: |
| 170 | cros_build_lib.Die('test_harness is required') |
| 171 | |
| 172 | vm_tests = input_proto.vm_tests |
| 173 | if not vm_tests: |
| 174 | cros_build_lib.Die('vm_tests must contain at least one element') |
| 175 | |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame^] | 176 | cmd = ['cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 177 | '--board', build_target.name, '--image-path', vm_image.path, |
| 178 | '--%s' % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower()] |
| 179 | cmd.extend(vm_test.pattern for vm_test in vm_tests) |
| 180 | |
| 181 | if input_proto.ssh_options.port: |
| 182 | cmd.extend(['--ssh-port', str(input_proto.ssh_options.port)]) |
| 183 | |
| 184 | if input_proto.ssh_options.private_key_path: |
| 185 | cmd.extend(['--private-key', input_proto.ssh_options.private_key_path]) |
| 186 | |
| 187 | # TODO(evanhernandez): Find a nice way to pass test_that-args through |
| 188 | # the build API. Or obviate them. |
| 189 | if test_harness == test_pb2.VmTestRequest.AUTOTEST: |
| 190 | cmd.append('--test_that-args=--whitelist-chrome-crashes') |
| 191 | |
| 192 | with osutils.TempDir(prefix='vm-test-results.') as results_dir: |
| 193 | cmd.extend(['--results-dir', results_dir]) |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 194 | cros_build_lib.RunCommand(cmd, kill_timeout=10 * 60) |