blob: d1df6d58481ecafd5b5a15faec1effcedb54d335 [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 Klein38c7d9e2019-05-08 09:31:19 -060018from chromite.lib import build_target_util
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
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060021from chromite.lib import image_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060022from chromite.lib import osutils
Alex Kleinc5403d62019-04-03 09:34:59 -060023from chromite.lib import sysroot_lib
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060024from chromite.scripts import cros_set_lsb_release
Alex Kleinc5403d62019-04-03 09:34:59 -060025from 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
Alex Klein8cb365a2019-05-15 16:24:53 -060045 if test.DebugInfoTest(sysroot_path):
46 return controller.RETURN_CODE_SUCCESS
47 else:
48 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -060049
50
51def BuildTargetUnitTest(input_proto, output_proto):
52 """Run a build target's ebuild unit tests."""
53 # Required args.
54 board = input_proto.build_target.name
55 result_path = input_proto.result_path
56
57 if not board:
58 cros_build_lib.Die('build_target.name is required.')
59 if not result_path:
60 cros_build_lib.Die('result_path is required.')
61
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060062 # Method flags.
Alex Klein38c7d9e2019-05-08 09:31:19 -060063 # An empty sysroot means build packages was not run. This is used for
64 # certain boards that need to use prebuilts (e.g. grunt's unittest-only).
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060065 was_built = not input_proto.flags.empty_sysroot
66
Alex Kleinf2674462019-05-16 16:47:24 -060067 # Skipped tests.
68 blacklisted_package_info = input_proto.package_blacklist
69 blacklist = []
70 for package_info in blacklisted_package_info:
71 blacklist.append(controller_util.PackageInfoToString(package_info))
72
Alex Klein38c7d9e2019-05-08 09:31:19 -060073 build_target = build_target_util.BuildTarget(board)
74 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Kleina2e42c42019-04-17 16:13:19 -060075
Alex Klein38c7d9e2019-05-08 09:31:19 -060076 result = test.BuildTargetUnitTest(build_target, chroot, blacklist=blacklist,
77 was_built=was_built)
Alex Kleina2e42c42019-04-17 16:13:19 -060078
Alex Klein38c7d9e2019-05-08 09:31:19 -060079 if not result.success:
80 # Failed to run tests or some tests failed.
81 # Record all failed packages.
82 for cpv in result.failed_cpvs:
83 package_info = output_proto.failed_packages.add()
84 controller_util.CPVToPackageInfo(cpv, package_info)
85 if result.failed_cpvs:
86 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
87 else:
88 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Alex Kleina2e42c42019-04-17 16:13:19 -060089
Alex Klein38c7d9e2019-05-08 09:31:19 -060090 sysroot = sysroot_lib.Sysroot(build_target.root)
91 tarball = test.BuildTargetUnitTestTarball(chroot, sysroot, result_path)
92 if tarball:
93 output_proto.tarball_path = tarball
Alex Kleine3fc3ca2019-04-30 16:20:55 -060094
95
96def ChromiteUnitTest(_input_proto, _output_proto):
97 """Run the chromite unit tests."""
98 cmd = [os.path.join(constants.CHROMITE_DIR, 'scripts', 'run_tests')]
Evan Hernandez9dfbe162019-06-11 17:22:07 -060099 result = cros_build_lib.RunCommand(cmd, error_code_ok=True,
100 combine_stdout_stderr=True)
Alex Klein8cb365a2019-05-15 16:24:53 -0600101 if result.returncode == 0:
102 return controller.RETURN_CODE_SUCCESS
103 else:
104 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Evan Hernandez4e388a52019-05-01 12:16:33 -0600105
106
107def VmTest(input_proto, _output_proto):
108 """Run VM tests."""
109 if not input_proto.HasField('build_target'):
110 cros_build_lib.Die('build_target is required')
111 build_target = input_proto.build_target
112
Alex Kleinc05f3d12019-05-29 14:16:21 -0600113 vm_path = input_proto.vm_path
Alex Klein311b8022019-06-05 16:00:07 -0600114 if not vm_path.path:
115 cros_build_lib.Die('vm_path.path is required.')
Alex Kleinc05f3d12019-05-29 14:16:21 -0600116
Evan Hernandez4e388a52019-05-01 12:16:33 -0600117 test_harness = input_proto.test_harness
118 if test_harness == test_pb2.VmTestRequest.UNSPECIFIED:
119 cros_build_lib.Die('test_harness is required')
120
121 vm_tests = input_proto.vm_tests
122 if not vm_tests:
123 cros_build_lib.Die('vm_tests must contain at least one element')
124
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700125 cmd = ['cros_run_test', '--debug', '--no-display', '--copy-on-write',
Alex Klein311b8022019-06-05 16:00:07 -0600126 '--board', build_target.name, '--image-path', vm_path.path,
Evan Hernandez4e388a52019-05-01 12:16:33 -0600127 '--%s' % test_pb2.VmTestRequest.TestHarness.Name(test_harness).lower()]
128 cmd.extend(vm_test.pattern for vm_test in vm_tests)
129
130 if input_proto.ssh_options.port:
131 cmd.extend(['--ssh-port', str(input_proto.ssh_options.port)])
132
133 if input_proto.ssh_options.private_key_path:
Alex Kleinaa705412019-06-04 15:00:30 -0600134 cmd.extend(['--private-key', input_proto.ssh_options.private_key_path.path])
Evan Hernandez4e388a52019-05-01 12:16:33 -0600135
136 # TODO(evanhernandez): Find a nice way to pass test_that-args through
137 # the build API. Or obviate them.
138 if test_harness == test_pb2.VmTestRequest.AUTOTEST:
139 cmd.append('--test_that-args=--whitelist-chrome-crashes')
140
141 with osutils.TempDir(prefix='vm-test-results.') as results_dir:
142 cmd.extend(['--results-dir', results_dir])
Alex Klein8cb365a2019-05-15 16:24:53 -0600143 cros_build_lib.RunCommand(cmd, kill_timeout=10 * 60)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600144
145
146def MoblabVmTest(input_proto, _output_proto):
147 """Run Moblab VM tests."""
Evan Hernandez655e8042019-06-13 12:50:44 -0600148 chroot = controller_util.ParseChroot(input_proto.chroot)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600149 image_payload_dir = input_proto.image_payload.path.path
150 cache_payload_dirs = [cp.path.path for cp in input_proto.cache_payloads]
151
152 # Autotest and Moblab depend on the builder path, so we must read it from
153 # the image.
154 image_file = os.path.join(image_payload_dir, constants.TEST_IMAGE_BIN)
155 with image_lib.LoopbackPartitions(image_file) as image_mount:
156 lsb_release_file = os.path.join(image_mount.destination,
157 constants.LSB_RELEASE_PATH.strip('/'))
158 lsb_release_kvs = cros_build_lib.LoadKeyValueFile(lsb_release_file)
159 builder = lsb_release_kvs.get(cros_set_lsb_release.LSB_KEY_BUILDER_PATH)
160
161 if not builder:
162 cros_build_lib.Die('Image did not contain key %s in %s',
163 cros_set_lsb_release.LSB_KEY_BUILDER_PATH,
164 constants.LSB_RELEASE_PATH)
165
166 # Now we can run the tests.
Evan Hernandez655e8042019-06-13 12:50:44 -0600167 with chroot.tempdir() as workspace_dir, chroot.tempdir() as results_dir:
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600168 vms = test.CreateMoblabVm(workspace_dir, image_payload_dir)
169 cache_dir = test.PrepareMoblabVmImageCache(vms, builder, cache_payload_dirs)
Evan Hernandez655e8042019-06-13 12:50:44 -0600170 test.RunMoblabVmTest(chroot, vms, builder, cache_dir, results_dir)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600171 test.ValidateMoblabVmTest(results_dir)