blob: 319656cd3e27c585ade9756fc302f702019a2df1 [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
17from chromite.lib import constants
Alex Kleinc5403d62019-04-03 09:34:59 -060018from chromite.lib import cros_build_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060019from chromite.lib import failures_lib
20from chromite.lib import osutils
21from chromite.lib import portage_util
Alex Kleinc5403d62019-04-03 09:34:59 -060022from chromite.lib import sysroot_lib
23from chromite.service import test
24
25
26def DebugInfoTest(input_proto, _output_proto):
27 """Run the debug info tests."""
28 sysroot_path = input_proto.sysroot.path
29 target_name = input_proto.sysroot.build_target.name
30
31 if not sysroot_path:
32 if target_name:
33 sysroot_path = cros_build_lib.GetSysroot(target_name)
34 else:
35 cros_build_lib.Die("The sysroot path or the sysroot's build target name "
36 'must be provided.')
37
38 # We could get away with out this, but it's a cheap check.
39 sysroot = sysroot_lib.Sysroot(sysroot_path)
40 if not sysroot.Exists():
41 cros_build_lib.Die('The provided sysroot does not exist.')
42
43 return 0 if test.DebugInfoTest(sysroot_path) else 1
Alex Kleina2e42c42019-04-17 16:13:19 -060044
45
46def BuildTargetUnitTest(input_proto, output_proto):
47 """Run a build target's ebuild unit tests."""
48 # Required args.
49 board = input_proto.build_target.name
50 result_path = input_proto.result_path
51
52 if not board:
53 cros_build_lib.Die('build_target.name is required.')
54 if not result_path:
55 cros_build_lib.Die('result_path is required.')
56
57 # Chroot handling.
58 chroot = input_proto.chroot.path
59 cache_dir = input_proto.chroot.cache_dir
60
61 chroot_args = []
62 if chroot:
63 chroot_args.extend(['--chroot', chroot])
64 else:
65 chroot = constants.DEFAULT_CHROOT_PATH
66
67 if cache_dir:
68 chroot_args.extend(['--cache_dir', cache_dir])
69
70 # TODO(crbug.com/954609) Service implementation.
71 extra_env = {'USE': 'chrome_internal'}
72 base_dir = os.path.join(chroot, 'tmp')
73 # The called code also sets the status file in some cases, but the hacky
74 # call makes it not always work, so set up our own just in case.
75 with osutils.TempDir(base_dir=base_dir) as tempdir:
76 full_sf_path = os.path.join(tempdir, 'status_file')
77 chroot_sf_path = full_sf_path.replace(chroot, '')
78 extra_env[constants.PARALLEL_EMERGE_STATUS_FILE_ENVVAR] = chroot_sf_path
79
80 try:
81 commands.RunUnitTests(constants.SOURCE_ROOT, board, extra_env=extra_env,
82 chroot_args=chroot_args)
83 except failures_lib.PackageBuildFailure as e:
84 # Add the failed packages.
85 for pkg in e.failed_packages:
86 cpv = portage_util.SplitCPV(pkg, strict=False)
87 package_info = output_proto.failed_packages.add()
88 controller_util.CPVToPackageInfo(cpv, package_info)
89
90 return 1
91 except failures_lib.BuildScriptFailure:
92 # Check our status file for failed packages in case the calling code's
93 # file didn't get set.
94 for cpv in portage_util.ParseParallelEmergeStatusFile(full_sf_path):
95 package_info = output_proto.failed_packages.add()
96 controller_util.CPVToPackageInfo(cpv, package_info)
97
98 return 1
99
100 tarball = _BuildUnittestTarball(chroot, board, result_path)
101 if tarball:
102 output_proto.tarball_path = tarball
103
104
105def _BuildUnittestTarball(chroot, board, result_path):
106 """Build the unittest tarball."""
107 tarball = 'unit_tests.tar'
108 tarball_path = os.path.join(result_path, tarball)
109
110 cwd = os.path.join(chroot, 'build', board, constants.UNITTEST_PKG_PATH)
111
112 result = cros_build_lib.CreateTarball(tarball_path, cwd, chroot=chroot,
113 compression=cros_build_lib.COMP_NONE,
114 error_code_ok=True)
115
116 return tarball_path if result.returncode == 0 else None
Alex Kleine3fc3ca2019-04-30 16:20:55 -0600117
118
119def ChromiteUnitTest(_input_proto, _output_proto):
120 """Run the chromite unit tests."""
121 cmd = [os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests')]
122 result = cros_build_lib.RunCommand(cmd, error_code_ok=True)
123 return result.returncode