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