blob: 3a11a2d51152cfe1e0b2931ba85ee1885faad6c5 [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
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060022from chromite.lib import image_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060023from chromite.lib import osutils
24from chromite.lib import portage_util
Alex Kleinc5403d62019-04-03 09:34:59 -060025from chromite.lib import sysroot_lib
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060026from chromite.scripts import cros_set_lsb_release
Alex Kleinc5403d62019-04-03 09:34:59 -060027from chromite.service import test
28
29
30def DebugInfoTest(input_proto, _output_proto):
31 """Run the debug info tests."""
32 sysroot_path = input_proto.sysroot.path
33 target_name = input_proto.sysroot.build_target.name
34
35 if not sysroot_path:
36 if target_name:
37 sysroot_path = cros_build_lib.GetSysroot(target_name)
38 else:
39 cros_build_lib.Die("The sysroot path or the sysroot's build target name "
40 'must be provided.')
41
42 # We could get away with out this, but it's a cheap check.
43 sysroot = sysroot_lib.Sysroot(sysroot_path)
44 if not sysroot.Exists():
45 cros_build_lib.Die('The provided sysroot does not exist.')
46
Alex Klein8cb365a2019-05-15 16:24:53 -060047 if test.DebugInfoTest(sysroot_path):
48 return controller.RETURN_CODE_SUCCESS
49 else:
50 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -060051
52
53def BuildTargetUnitTest(input_proto, output_proto):
54 """Run a build target's ebuild unit tests."""
55 # Required args.
56 board = input_proto.build_target.name
57 result_path = input_proto.result_path
58
59 if not board:
60 cros_build_lib.Die('build_target.name is required.')
61 if not result_path:
62 cros_build_lib.Die('result_path is required.')
63
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060064 # Method flags.
65 # An empty sysroot means build packages was not run.
66 was_built = not input_proto.flags.empty_sysroot
67
Alex Kleinf2674462019-05-16 16:47:24 -060068 # Skipped tests.
69 blacklisted_package_info = input_proto.package_blacklist
70 blacklist = []
71 for package_info in blacklisted_package_info:
72 blacklist.append(controller_util.PackageInfoToString(package_info))
73
Alex Kleina2e42c42019-04-17 16:13:19 -060074 # Chroot handling.
75 chroot = input_proto.chroot.path
76 cache_dir = input_proto.chroot.cache_dir
77
78 chroot_args = []
79 if chroot:
80 chroot_args.extend(['--chroot', chroot])
81 else:
82 chroot = constants.DEFAULT_CHROOT_PATH
83
84 if cache_dir:
85 chroot_args.extend(['--cache_dir', cache_dir])
86
87 # TODO(crbug.com/954609) Service implementation.
88 extra_env = {'USE': 'chrome_internal'}
89 base_dir = os.path.join(chroot, 'tmp')
90 # The called code also sets the status file in some cases, but the hacky
91 # call makes it not always work, so set up our own just in case.
92 with osutils.TempDir(base_dir=base_dir) as tempdir:
93 full_sf_path = os.path.join(tempdir, 'status_file')
94 chroot_sf_path = full_sf_path.replace(chroot, '')
95 extra_env[constants.PARALLEL_EMERGE_STATUS_FILE_ENVVAR] = chroot_sf_path
96
97 try:
98 commands.RunUnitTests(constants.SOURCE_ROOT, board, extra_env=extra_env,
Alex Kleinf2674462019-05-16 16:47:24 -060099 chroot_args=chroot_args, build_stage=was_built,
100 blacklist=blacklist)
Alex Kleina2e42c42019-04-17 16:13:19 -0600101 except failures_lib.PackageBuildFailure as e:
102 # Add the failed packages.
103 for pkg in e.failed_packages:
104 cpv = portage_util.SplitCPV(pkg, strict=False)
105 package_info = output_proto.failed_packages.add()
106 controller_util.CPVToPackageInfo(cpv, package_info)
107
Alex Klein8cb365a2019-05-15 16:24:53 -0600108 if e.failed_packages:
109 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
110 else:
111 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -0600112 except failures_lib.BuildScriptFailure:
113 # Check our status file for failed packages in case the calling code's
114 # file didn't get set.
Alex Klein8cb365a2019-05-15 16:24:53 -0600115 failed_packages = portage_util.ParseParallelEmergeStatusFile(full_sf_path)
116 for cpv in failed_packages:
Alex Kleina2e42c42019-04-17 16:13:19 -0600117 package_info = output_proto.failed_packages.add()
118 controller_util.CPVToPackageInfo(cpv, package_info)
119
Alex Klein8cb365a2019-05-15 16:24:53 -0600120 if failed_packages:
121 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
122 else:
123 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -0600124
125 tarball = _BuildUnittestTarball(chroot, board, result_path)
126 if tarball:
127 output_proto.tarball_path = tarball
128
129
130def _BuildUnittestTarball(chroot, board, result_path):
131 """Build the unittest tarball."""
132 tarball = 'unit_tests.tar'
133 tarball_path = os.path.join(result_path, tarball)
134
135 cwd = os.path.join(chroot, 'build', board, constants.UNITTEST_PKG_PATH)
136
137 result = cros_build_lib.CreateTarball(tarball_path, cwd, chroot=chroot,
138 compression=cros_build_lib.COMP_NONE,
139 error_code_ok=True)
140
141 return tarball_path if result.returncode == 0 else None
Alex Kleine3fc3ca2019-04-30 16:20:55 -0600142
143
144def ChromiteUnitTest(_input_proto, _output_proto):
145 """Run the chromite unit tests."""
146 cmd = [os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests')]
Evan Hernandez9dfbe162019-06-11 17:22:07 -0600147 result = cros_build_lib.RunCommand(cmd, error_code_ok=True,
148 combine_stdout_stderr=True)
Alex Klein8cb365a2019-05-15 16:24:53 -0600149 if result.returncode == 0:
150 return controller.RETURN_CODE_SUCCESS
151 else:
152 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Evan Hernandez4e388a52019-05-01 12:16:33 -0600153
154
155def VmTest(input_proto, _output_proto):
156 """Run VM tests."""
157 if not input_proto.HasField('build_target'):
158 cros_build_lib.Die('build_target is required')
159 build_target = input_proto.build_target
160
Alex Kleinc05f3d12019-05-29 14:16:21 -0600161 vm_path = input_proto.vm_path
Alex Klein311b8022019-06-05 16:00:07 -0600162 if not vm_path.path:
163 cros_build_lib.Die('vm_path.path is required.')
Alex Kleinc05f3d12019-05-29 14:16:21 -0600164
Evan Hernandez4e388a52019-05-01 12:16:33 -0600165 test_harness = input_proto.test_harness
166 if test_harness == test_pb2.VmTestRequest.UNSPECIFIED:
167 cros_build_lib.Die('test_harness is required')
168
169 vm_tests = input_proto.vm_tests
170 if not vm_tests:
171 cros_build_lib.Die('vm_tests must contain at least one element')
172
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700173 cmd = ['cros_run_test', '--debug', '--no-display', '--copy-on-write',
Alex Klein311b8022019-06-05 16:00:07 -0600174 '--board', build_target.name, '--image-path', vm_path.path,
Evan Hernandez4e388a52019-05-01 12:16:33 -0600175 '--%s' % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower()]
176 cmd.extend(vm_test.pattern for vm_test in vm_tests)
177
178 if input_proto.ssh_options.port:
179 cmd.extend(['--ssh-port', str(input_proto.ssh_options.port)])
180
181 if input_proto.ssh_options.private_key_path:
Alex Kleinaa705412019-06-04 15:00:30 -0600182 cmd.extend(['--private-key', input_proto.ssh_options.private_key_path.path])
Evan Hernandez4e388a52019-05-01 12:16:33 -0600183
184 # TODO(evanhernandez): Find a nice way to pass test_that-args through
185 # the build API. Or obviate them.
186 if test_harness == test_pb2.VmTestRequest.AUTOTEST:
187 cmd.append('--test_that-args=--whitelist-chrome-crashes')
188
189 with osutils.TempDir(prefix='vm-test-results.') as results_dir:
190 cmd.extend(['--results-dir', results_dir])
Alex Klein8cb365a2019-05-15 16:24:53 -0600191 cros_build_lib.RunCommand(cmd, kill_timeout=10 * 60)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600192
193
194def MoblabVmTest(input_proto, _output_proto):
195 """Run Moblab VM tests."""
196 image_payload_dir = input_proto.image_payload.path.path
197 cache_payload_dirs = [cp.path.path for cp in input_proto.cache_payloads]
198
199 # Autotest and Moblab depend on the builder path, so we must read it from
200 # the image.
201 image_file = os.path.join(image_payload_dir, constants.TEST_IMAGE_BIN)
202 with image_lib.LoopbackPartitions(image_file) as image_mount:
203 lsb_release_file = os.path.join(image_mount.destination,
204 constants.LSB_RELEASE_PATH.strip('/'))
205 lsb_release_kvs = cros_build_lib.LoadKeyValueFile(lsb_release_file)
206 builder = lsb_release_kvs.get(cros_set_lsb_release.LSB_KEY_BUILDER_PATH)
207
208 if not builder:
209 cros_build_lib.Die('Image did not contain key %s in %s',
210 cros_set_lsb_release.LSB_KEY_BUILDER_PATH,
211 constants.LSB_RELEASE_PATH)
212
213 # Now we can run the tests.
214 with osutils.TempDir() as workspace_dir, osutils.TempDir() as results_dir:
215 vms = test.CreateMoblabVm(workspace_dir, image_payload_dir)
216 cache_dir = test.PrepareMoblabVmImageCache(vms, builder, cache_payload_dirs)
217 test.RunMoblabVmTest(vms, builder, cache_dir, results_dir)
218 test.ValidateMoblabVmTest(results_dir)