blob: 43f4e41bfb34f3ddec820de773f258a99c68aa75 [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
14
Alex Klein8cb365a2019-05-15 16:24:53 -060015from chromite.api import controller
Alex Kleina2e42c42019-04-17 16:13:19 -060016from chromite.api.controller import controller_util
Evan Hernandez4e388a52019-05-01 12:16:33 -060017from chromite.api.gen.chromite.api import test_pb2
Alex Klein21b95022019-05-09 14:14:46 -060018from chromite.api.gen.chromiumos import common_pb2
19from chromite.cbuildbot import commands
Alex Kleina2e42c42019-04-17 16:13:19 -060020from chromite.lib import constants
Alex Kleinc5403d62019-04-03 09:34:59 -060021from chromite.lib import cros_build_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060022from chromite.lib import failures_lib
23from chromite.lib import osutils
24from chromite.lib import portage_util
Alex Kleinc5403d62019-04-03 09:34:59 -060025from chromite.lib import sysroot_lib
26from chromite.service import test
27
28
29def 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 Klein8cb365a2019-05-15 16:24:53 -060046 if test.DebugInfoTest(sysroot_path):
47 return controller.RETURN_CODE_SUCCESS
48 else:
49 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -060050
51
52def 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 Kleinfa6ebdc2019-05-10 10:57:31 -060063 # Method flags.
64 # An empty sysroot means build packages was not run.
65 was_built = not input_proto.flags.empty_sysroot
66
Alex Kleinf2674462019-05-16 16:47:24 -060067 # 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 Kleina2e42c42019-04-17 16:13:19 -060073 # 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 Kleinf2674462019-05-16 16:47:24 -060098 chroot_args=chroot_args, build_stage=was_built,
99 blacklist=blacklist)
Alex Kleina2e42c42019-04-17 16:13:19 -0600100 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 Klein8cb365a2019-05-15 16:24:53 -0600107 if e.failed_packages:
108 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
109 else:
110 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -0600111 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 Klein8cb365a2019-05-15 16:24:53 -0600114 failed_packages = portage_util.ParseParallelEmergeStatusFile(full_sf_path)
115 for cpv in failed_packages:
Alex Kleina2e42c42019-04-17 16:13:19 -0600116 package_info = output_proto.failed_packages.add()
117 controller_util.CPVToPackageInfo(cpv, package_info)
118
Alex Klein8cb365a2019-05-15 16:24:53 -0600119 if failed_packages:
120 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
121 else:
122 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -0600123
124 tarball = _BuildUnittestTarball(chroot, board, result_path)
125 if tarball:
126 output_proto.tarball_path = tarball
127
128
129def _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 Kleine3fc3ca2019-04-30 16:20:55 -0600141
142
143def 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 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
153def 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
Alex Kleinc05f3d12019-05-29 14:16:21 -0600159 vm_path = input_proto.vm_path
Evan Hernandez4e388a52019-05-01 12:16:33 -0600160 vm_image = input_proto.vm_image
Alex Klein21b95022019-05-09 14:14:46 -0600161 test_vm_image = input_proto.test_vm_image
Alex Kleinc05f3d12019-05-29 14:16:21 -0600162 if not vm_image.path and not test_vm_image.path and not vm_path.path:
163 cros_build_lib.Die('A VM path must be provided.')
Alex Klein21b95022019-05-09 14:14:46 -0600164 if test_vm_image.path:
165 if test_vm_image.type != common_pb2.TEST_VM:
166 cros_build_lib.Die('Must provide a test VM image.')
167 vm_image = test_vm_image
Evan Hernandez4e388a52019-05-01 12:16:33 -0600168
Alex Kleinc05f3d12019-05-29 14:16:21 -0600169 vm_image_path = vm_path.path or vm_image.path
170
Evan Hernandez4e388a52019-05-01 12:16:33 -0600171 test_harness = input_proto.test_harness
172 if test_harness == test_pb2.VmTestRequest.UNSPECIFIED:
173 cros_build_lib.Die('test_harness is required')
174
175 vm_tests = input_proto.vm_tests
176 if not vm_tests:
177 cros_build_lib.Die('vm_tests must contain at least one element')
178
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700179 cmd = ['cros_run_test', '--debug', '--no-display', '--copy-on-write',
Alex Kleinc05f3d12019-05-29 14:16:21 -0600180 '--board', build_target.name, '--image-path', vm_image_path,
Evan Hernandez4e388a52019-05-01 12:16:33 -0600181 '--%s' % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower()]
182 cmd.extend(vm_test.pattern for vm_test in vm_tests)
183
184 if input_proto.ssh_options.port:
185 cmd.extend(['--ssh-port', str(input_proto.ssh_options.port)])
186
187 if input_proto.ssh_options.private_key_path:
188 cmd.extend(['--private-key', input_proto.ssh_options.private_key_path])
189
190 # TODO(evanhernandez): Find a nice way to pass test_that-args through
191 # the build API. Or obviate them.
192 if test_harness == test_pb2.VmTestRequest.AUTOTEST:
193 cmd.append('--test_that-args=--whitelist-chrome-crashes')
194
195 with osutils.TempDir(prefix='vm-test-results.') as results_dir:
196 cmd.extend(['--results-dir', results_dir])
Alex Klein8cb365a2019-05-15 16:24:53 -0600197 cros_build_lib.RunCommand(cmd, kill_timeout=10 * 60)