blob: aa0d541999057e898c0faf79143e60c299710199 [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 Kleina2e42c42019-04-17 16:13:19 -060062 # Chroot handling.
63 chroot = input_proto.chroot.path
64 cache_dir = input_proto.chroot.cache_dir
65
66 chroot_args = []
67 if chroot:
68 chroot_args.extend(['--chroot', chroot])
69 else:
70 chroot = constants.DEFAULT_CHROOT_PATH
71
72 if cache_dir:
73 chroot_args.extend(['--cache_dir', cache_dir])
74
75 # TODO(crbug.com/954609) Service implementation.
76 extra_env = {'USE': 'chrome_internal'}
77 base_dir = os.path.join(chroot, 'tmp')
78 # The called code also sets the status file in some cases, but the hacky
79 # call makes it not always work, so set up our own just in case.
80 with osutils.TempDir(base_dir=base_dir) as tempdir:
81 full_sf_path = os.path.join(tempdir, 'status_file')
82 chroot_sf_path = full_sf_path.replace(chroot, '')
83 extra_env[constants.PARALLEL_EMERGE_STATUS_FILE_ENVVAR] = chroot_sf_path
84
85 try:
86 commands.RunUnitTests(constants.SOURCE_ROOT, board, extra_env=extra_env,
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060087 chroot_args=chroot_args, build_stage=was_built)
Alex Kleina2e42c42019-04-17 16:13:19 -060088 except failures_lib.PackageBuildFailure as e:
89 # Add the failed packages.
90 for pkg in e.failed_packages:
91 cpv = portage_util.SplitCPV(pkg, strict=False)
92 package_info = output_proto.failed_packages.add()
93 controller_util.CPVToPackageInfo(cpv, package_info)
94
95 return 1
96 except failures_lib.BuildScriptFailure:
97 # Check our status file for failed packages in case the calling code's
98 # file didn't get set.
99 for cpv in portage_util.ParseParallelEmergeStatusFile(full_sf_path):
100 package_info = output_proto.failed_packages.add()
101 controller_util.CPVToPackageInfo(cpv, package_info)
102
103 return 1
104
105 tarball = _BuildUnittestTarball(chroot, board, result_path)
106 if tarball:
107 output_proto.tarball_path = tarball
108
109
110def _BuildUnittestTarball(chroot, board, result_path):
111 """Build the unittest tarball."""
112 tarball = 'unit_tests.tar'
113 tarball_path = os.path.join(result_path, tarball)
114
115 cwd = os.path.join(chroot, 'build', board, constants.UNITTEST_PKG_PATH)
116
117 result = cros_build_lib.CreateTarball(tarball_path, cwd, chroot=chroot,
118 compression=cros_build_lib.COMP_NONE,
119 error_code_ok=True)
120
121 return tarball_path if result.returncode == 0 else None
Alex Kleine3fc3ca2019-04-30 16:20:55 -0600122
123
124def ChromiteUnitTest(_input_proto, _output_proto):
125 """Run the chromite unit tests."""
126 cmd = [os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests')]
127 result = cros_build_lib.RunCommand(cmd, error_code_ok=True)
128 return result.returncode
Evan Hernandez4e388a52019-05-01 12:16:33 -0600129
130
131def VmTest(input_proto, _output_proto):
132 """Run VM tests."""
133 if not input_proto.HasField('build_target'):
134 cros_build_lib.Die('build_target is required')
135 build_target = input_proto.build_target
136
137 if not input_proto.HasField('vm_image'):
138 cros_build_lib.Die('vm_image is required')
139 vm_image = input_proto.vm_image
140
141 test_harness = input_proto.test_harness
142 if test_harness == test_pb2.VmTestRequest.UNSPECIFIED:
143 cros_build_lib.Die('test_harness is required')
144
145 vm_tests = input_proto.vm_tests
146 if not vm_tests:
147 cros_build_lib.Die('vm_tests must contain at least one element')
148
149 cmd = ['cros_run_vm_test', '--debug', '--no-display', '--copy-on-write',
150 '--board', build_target.name, '--image-path', vm_image.path,
151 '--%s' % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower()]
152 cmd.extend(vm_test.pattern for vm_test in vm_tests)
153
154 if input_proto.ssh_options.port:
155 cmd.extend(['--ssh-port', str(input_proto.ssh_options.port)])
156
157 if input_proto.ssh_options.private_key_path:
158 cmd.extend(['--private-key', input_proto.ssh_options.private_key_path])
159
160 # TODO(evanhernandez): Find a nice way to pass test_that-args through
161 # the build API. Or obviate them.
162 if test_harness == test_pb2.VmTestRequest.AUTOTEST:
163 cmd.append('--test_that-args=--whitelist-chrome-crashes')
164
165 with osutils.TempDir(prefix='vm-test-results.') as results_dir:
166 cmd.extend(['--results-dir', results_dir])
167 return cros_build_lib.RunCommand(cmd, kill_timeout=10 * 60).returncode