blob: 88508d8b5ef52afba1fbb2d18076adcf23cd6049 [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.cbuildbot import commands
Alex Kleina2e42c42019-04-17 16:13:19 -060019from chromite.lib import constants
Alex Kleinc5403d62019-04-03 09:34:59 -060020from chromite.lib import cros_build_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060021from chromite.lib import failures_lib
22from chromite.lib import osutils
23from chromite.lib import portage_util
Alex Kleinc5403d62019-04-03 09:34:59 -060024from chromite.lib import sysroot_lib
25from chromite.service import test
26
27
28def 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
Alex Klein8cb365a2019-05-15 16:24:53 -060045 if test.DebugInfoTest(sysroot_path):
46 return controller.RETURN_CODE_SUCCESS
47 else:
48 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -060049
50
51def BuildTargetUnitTest(input_proto, output_proto):
52 """Run a build target's ebuild unit tests."""
53 # Required args.
54 board = input_proto.build_target.name
55 result_path = input_proto.result_path
56
57 if not board:
58 cros_build_lib.Die('build_target.name is required.')
59 if not result_path:
60 cros_build_lib.Die('result_path is required.')
61
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060062 # Method flags.
63 # An empty sysroot means build packages was not run.
64 was_built = not input_proto.flags.empty_sysroot
65
Alex Kleinf2674462019-05-16 16:47:24 -060066 # Skipped tests.
67 blacklisted_package_info = input_proto.package_blacklist
68 blacklist = []
69 for package_info in blacklisted_package_info:
70 blacklist.append(controller_util.PackageInfoToString(package_info))
71
Alex Kleina2e42c42019-04-17 16:13:19 -060072 # Chroot handling.
73 chroot = input_proto.chroot.path
74 cache_dir = input_proto.chroot.cache_dir
75
76 chroot_args = []
77 if chroot:
78 chroot_args.extend(['--chroot', chroot])
79 else:
80 chroot = constants.DEFAULT_CHROOT_PATH
81
82 if cache_dir:
83 chroot_args.extend(['--cache_dir', cache_dir])
84
85 # TODO(crbug.com/954609) Service implementation.
86 extra_env = {'USE': 'chrome_internal'}
87 base_dir = os.path.join(chroot, 'tmp')
88 # The called code also sets the status file in some cases, but the hacky
89 # call makes it not always work, so set up our own just in case.
90 with osutils.TempDir(base_dir=base_dir) as tempdir:
91 full_sf_path = os.path.join(tempdir, 'status_file')
92 chroot_sf_path = full_sf_path.replace(chroot, '')
93 extra_env[constants.PARALLEL_EMERGE_STATUS_FILE_ENVVAR] = chroot_sf_path
94
95 try:
96 commands.RunUnitTests(constants.SOURCE_ROOT, board, extra_env=extra_env,
Alex Kleinf2674462019-05-16 16:47:24 -060097 chroot_args=chroot_args, build_stage=was_built,
98 blacklist=blacklist)
Alex Kleina2e42c42019-04-17 16:13:19 -060099 except failures_lib.PackageBuildFailure as e:
100 # Add the failed packages.
101 for pkg in e.failed_packages:
102 cpv = portage_util.SplitCPV(pkg, strict=False)
103 package_info = output_proto.failed_packages.add()
104 controller_util.CPVToPackageInfo(cpv, package_info)
105
Alex Klein8cb365a2019-05-15 16:24:53 -0600106 if e.failed_packages:
107 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
108 else:
109 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -0600110 except failures_lib.BuildScriptFailure:
111 # Check our status file for failed packages in case the calling code's
112 # file didn't get set.
Alex Klein8cb365a2019-05-15 16:24:53 -0600113 failed_packages = portage_util.ParseParallelEmergeStatusFile(full_sf_path)
114 for cpv in failed_packages:
Alex Kleina2e42c42019-04-17 16:13:19 -0600115 package_info = output_proto.failed_packages.add()
116 controller_util.CPVToPackageInfo(cpv, package_info)
117
Alex Klein8cb365a2019-05-15 16:24:53 -0600118 if failed_packages:
119 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
120 else:
121 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -0600122
123 tarball = _BuildUnittestTarball(chroot, board, result_path)
124 if tarball:
125 output_proto.tarball_path = tarball
126
127
128def _BuildUnittestTarball(chroot, board, result_path):
129 """Build the unittest tarball."""
130 tarball = 'unit_tests.tar'
131 tarball_path = os.path.join(result_path, tarball)
132
133 cwd = os.path.join(chroot, 'build', board, constants.UNITTEST_PKG_PATH)
134
135 result = cros_build_lib.CreateTarball(tarball_path, cwd, chroot=chroot,
136 compression=cros_build_lib.COMP_NONE,
137 error_code_ok=True)
138
139 return tarball_path if result.returncode == 0 else None
Alex Kleine3fc3ca2019-04-30 16:20:55 -0600140
141
142def ChromiteUnitTest(_input_proto, _output_proto):
143 """Run the chromite unit tests."""
144 cmd = [os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests')]
145 result = cros_build_lib.RunCommand(cmd, error_code_ok=True)
Alex Klein8cb365a2019-05-15 16:24:53 -0600146 if result.returncode == 0:
147 return controller.RETURN_CODE_SUCCESS
148 else:
149 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Evan Hernandez4e388a52019-05-01 12:16:33 -0600150
151
152def VmTest(input_proto, _output_proto):
153 """Run VM tests."""
154 if not input_proto.HasField('build_target'):
155 cros_build_lib.Die('build_target is required')
156 build_target = input_proto.build_target
157
Alex Kleinc05f3d12019-05-29 14:16:21 -0600158 vm_path = input_proto.vm_path
Alex Klein311b8022019-06-05 16:00:07 -0600159 if not vm_path.path:
160 cros_build_lib.Die('vm_path.path is required.')
Alex Kleinc05f3d12019-05-29 14:16:21 -0600161
Evan Hernandez4e388a52019-05-01 12:16:33 -0600162 test_harness = input_proto.test_harness
163 if test_harness == test_pb2.VmTestRequest.UNSPECIFIED:
164 cros_build_lib.Die('test_harness is required')
165
166 vm_tests = input_proto.vm_tests
167 if not vm_tests:
168 cros_build_lib.Die('vm_tests must contain at least one element')
169
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700170 cmd = ['cros_run_test', '--debug', '--no-display', '--copy-on-write',
Alex Klein311b8022019-06-05 16:00:07 -0600171 '--board', build_target.name, '--image-path', vm_path.path,
Evan Hernandez4e388a52019-05-01 12:16:33 -0600172 '--%s' % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower()]
173 cmd.extend(vm_test.pattern for vm_test in vm_tests)
174
175 if input_proto.ssh_options.port:
176 cmd.extend(['--ssh-port', str(input_proto.ssh_options.port)])
177
178 if input_proto.ssh_options.private_key_path:
Alex Kleinaa705412019-06-04 15:00:30 -0600179 cmd.extend(['--private-key', input_proto.ssh_options.private_key_path.path])
Evan Hernandez4e388a52019-05-01 12:16:33 -0600180
181 # TODO(evanhernandez): Find a nice way to pass test_that-args through
182 # the build API. Or obviate them.
183 if test_harness == test_pb2.VmTestRequest.AUTOTEST:
184 cmd.append('--test_that-args=--whitelist-chrome-crashes')
185
186 with osutils.TempDir(prefix='vm-test-results.') as results_dir:
187 cmd.extend(['--results-dir', results_dir])
Alex Klein8cb365a2019-05-15 16:24:53 -0600188 cros_build_lib.RunCommand(cmd, kill_timeout=10 * 60)