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