blob: f2846173cda090910e54f729fe4886d646d6a976 [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
Evan Hernandez4e388a52019-05-01 12:16:33 -060016from chromite.api.gen.chromite.api import test_pb2
Alex Klein21b95022019-05-09 14:14:46 -060017from chromite.api.gen.chromiumos import common_pb2
18from 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
45 return 0 if test.DebugInfoTest(sysroot_path) else 1
Alex Kleina2e42c42019-04-17 16:13:19 -060046
47
48def 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 Kleinfa6ebdc2019-05-10 10:57:31 -060059 # Method flags.
60 # An empty sysroot means build packages was not run.
61 was_built = not input_proto.flags.empty_sysroot
62
Alex Kleinf2674462019-05-16 16:47:24 -060063 # 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 Kleina2e42c42019-04-17 16:13:19 -060069 # 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 Kleinf2674462019-05-16 16:47:24 -060094 chroot_args=chroot_args, build_stage=was_built,
95 blacklist=blacklist)
Alex Kleina2e42c42019-04-17 16:13:19 -060096 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
118def _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 Kleine3fc3ca2019-04-30 16:20:55 -0600130
131
132def 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 Hernandez4e388a52019-05-01 12:16:33 -0600137
138
139def 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 Hernandez4e388a52019-05-01 12:16:33 -0600145 vm_image = input_proto.vm_image
Alex Klein21b95022019-05-09 14:14:46 -0600146 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 Hernandez4e388a52019-05-01 12:16:33 -0600153
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