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