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