blob: 48a151b42bdff1517a89b1a4ff0a4cef71e82d10 [file] [log] [blame]
Alex Kleina2e42c42019-04-17 16:13:19 -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"""The test controller tests."""
7
8from __future__ import print_function
9
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060010import mock
11
Alex Kleina2e42c42019-04-17 16:13:19 -060012from chromite.api.controller import test as test_controller
Evan Hernandez4e388a52019-05-01 12:16:33 -060013from chromite.api.gen.chromiumos import common_pb2
Alex Klein4f0eb432019-05-02 13:56:04 -060014from chromite.api.gen.chromite.api import image_pb2
Alex Kleina2e42c42019-04-17 16:13:19 -060015from chromite.api.gen.chromite.api import test_pb2
16from chromite.cbuildbot import commands
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060017from chromite.lib import constants
Alex Kleina2e42c42019-04-17 16:13:19 -060018from chromite.lib import cros_build_lib
19from chromite.lib import cros_test_lib
20from chromite.lib import failures_lib
21from chromite.lib import osutils
22from chromite.lib import portage_util
23
24
Evan Hernandez4e388a52019-05-01 12:16:33 -060025class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase):
Alex Kleina2e42c42019-04-17 16:13:19 -060026 """Tests for the UnitTest function."""
27
28 def _GetInput(self, board=None, result_path=None, chroot_path=None,
Alex Kleinf2674462019-05-16 16:47:24 -060029 cache_dir=None, empty_sysroot=None, blacklist=None):
Alex Kleina2e42c42019-04-17 16:13:19 -060030 """Helper to build an input message instance."""
Alex Kleinf2674462019-05-16 16:47:24 -060031 formatted_blacklist = []
32 for pkg in blacklist or []:
33 formatted_blacklist.append({'category': pkg.category,
34 'package_name': pkg.package})
35
Alex Kleina2e42c42019-04-17 16:13:19 -060036 return test_pb2.BuildTargetUnitTestRequest(
37 build_target={'name': board}, result_path=result_path,
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060038 chroot={'path': chroot_path, 'cache_dir': cache_dir},
Alex Kleinf2674462019-05-16 16:47:24 -060039 flags={'empty_sysroot': empty_sysroot},
40 package_blacklist=formatted_blacklist,
Alex Kleina2e42c42019-04-17 16:13:19 -060041 )
42
43 def _GetOutput(self):
44 """Helper to get an empty output message instance."""
45 return test_pb2.BuildTargetUnitTestResponse()
46
47 def testNoArgumentFails(self):
48 """Test no arguments fails."""
49 input_msg = self._GetInput()
50 output_msg = self._GetOutput()
51 with self.assertRaises(cros_build_lib.DieSystemExit):
52 test_controller.BuildTargetUnitTest(input_msg, output_msg)
53
54 def testNoBuildTargetFails(self):
55 """Test missing build target name fails."""
56 input_msg = self._GetInput(result_path=self.tempdir)
57 output_msg = self._GetOutput()
58 with self.assertRaises(cros_build_lib.DieSystemExit):
59 test_controller.BuildTargetUnitTest(input_msg, output_msg)
60
61 def testNoResultPathFails(self):
62 """Test missing result path fails."""
63 # Missing result_path.
64 input_msg = self._GetInput(board='board')
65 output_msg = self._GetOutput()
66 with self.assertRaises(cros_build_lib.DieSystemExit):
67 test_controller.BuildTargetUnitTest(input_msg, output_msg)
68
69 def testPackageBuildFailure(self):
70 """Test handling of raised BuildPackageFailure."""
71 tempdir = osutils.TempDir(base_dir=self.tempdir)
72 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
73
74 pkgs = ['cat/pkg', 'foo/bar']
75 expected = [('cat', 'pkg'), ('foo', 'bar')]
76 rce = cros_build_lib.RunCommandError('error',
77 cros_build_lib.CommandResult())
78 error = failures_lib.PackageBuildFailure(rce, 'shortname', pkgs)
79 self.PatchObject(commands, 'RunUnitTests', side_effect=error)
80
81 input_msg = self._GetInput(board='board', result_path=self.tempdir)
82 output_msg = self._GetOutput()
83
84 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg)
85
86 self.assertNotEqual(0, rc)
87 self.assertTrue(output_msg.failed_packages)
88 failed = []
89 for pi in output_msg.failed_packages:
90 failed.append((pi.category, pi.package_name))
91 self.assertItemsEqual(expected, failed)
92
93 def testPopulatedEmergeFile(self):
94 """Test build script failure due to using outside emerge status file."""
95 tempdir = osutils.TempDir(base_dir=self.tempdir)
96 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
97
98 pkgs = ['cat/pkg', 'foo/bar']
99 cpvs = [portage_util.SplitCPV(pkg, strict=False) for pkg in pkgs]
100 expected = [('cat', 'pkg'), ('foo', 'bar')]
101 rce = cros_build_lib.RunCommandError('error',
102 cros_build_lib.CommandResult())
103 error = failures_lib.BuildScriptFailure(rce, 'shortname')
104 self.PatchObject(commands, 'RunUnitTests', side_effect=error)
105 self.PatchObject(portage_util, 'ParseParallelEmergeStatusFile',
106 return_value=cpvs)
107
108 input_msg = self._GetInput(board='board', result_path=self.tempdir)
109 output_msg = self._GetOutput()
110
111 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg)
112
113 self.assertNotEqual(0, rc)
114 self.assertTrue(output_msg.failed_packages)
115 failed = []
116 for pi in output_msg.failed_packages:
117 failed.append((pi.category, pi.package_name))
118 self.assertItemsEqual(expected, failed)
119
120 def testOtherBuildScriptFailure(self):
121 """Test build script failure due to non-package emerge error."""
122 tempdir = osutils.TempDir(base_dir=self.tempdir)
123 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
124
125 rce = cros_build_lib.RunCommandError('error',
126 cros_build_lib.CommandResult())
127 error = failures_lib.BuildScriptFailure(rce, 'shortname')
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600128 patch = self.PatchObject(commands, 'RunUnitTests', side_effect=error)
Alex Kleina2e42c42019-04-17 16:13:19 -0600129 self.PatchObject(portage_util, 'ParseParallelEmergeStatusFile',
130 return_value=[])
131
Alex Kleinf2674462019-05-16 16:47:24 -0600132 pkgs = ['foo/bar', 'cat/pkg']
133 blacklist = [portage_util.SplitCPV(p, strict=False) for p in pkgs]
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600134 input_msg = self._GetInput(board='board', result_path=self.tempdir,
Alex Kleinf2674462019-05-16 16:47:24 -0600135 empty_sysroot=True, blacklist=blacklist)
Alex Kleina2e42c42019-04-17 16:13:19 -0600136 output_msg = self._GetOutput()
137
138 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg)
139
140 self.assertNotEqual(0, rc)
141 self.assertFalse(output_msg.failed_packages)
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600142 patch.assert_called_with(constants.SOURCE_ROOT, 'board', extra_env=mock.ANY,
Alex Kleinf2674462019-05-16 16:47:24 -0600143 chroot_args=mock.ANY, build_stage=False,
144 blacklist=pkgs)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600145
146
147class VmTestTest(cros_test_lib.MockTestCase):
148 """Test the VmTest endpoint."""
149
150 def _GetInput(self, **kwargs):
151 values = dict(
152 build_target=common_pb2.BuildTarget(name='target'),
Alex Klein4f0eb432019-05-02 13:56:04 -0600153 vm_image=image_pb2.VmImage(path='/path/to/image.bin'),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600154 test_harness=test_pb2.VmTestRequest.TAST,
155 vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')],
156 ssh_options=test_pb2.VmTestRequest.SshOptions(
157 port=1234, private_key_path='/path/to/id_rsa'),
158 )
159 values.update(kwargs)
160 return test_pb2.VmTestRequest(**values)
161
162 def setUp(self):
163 self.rc_mock = cros_test_lib.RunCommandMock()
164 self.rc_mock.SetDefaultCmdResult()
165 self.StartPatcher(self.rc_mock)
166
167 def testTastAllOptions(self):
168 """Test VmTest for Tast with all options set."""
169 test_controller.VmTest(self._GetInput(), None)
170 self.rc_mock.assertCommandContains([
171 'cros_run_vm_test', '--debug', '--no-display', '--copy-on-write',
172 '--board', 'target',
173 '--image-path', '/path/to/image.bin',
174 '--tast', 'suite',
175 '--ssh-port', '1234',
176 '--private-key', '/path/to/id_rsa',
177 ])
178
179 def testAutotestAllOptions(self):
180 """Test VmTest for Autotest with all options set."""
181 input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST)
182 test_controller.VmTest(input_proto, None)
183 self.rc_mock.assertCommandContains([
184 'cros_run_vm_test', '--debug', '--no-display', '--copy-on-write',
185 '--board', 'target',
186 '--image-path', '/path/to/image.bin',
187 '--autotest', 'suite',
188 '--ssh-port', '1234',
189 '--private-key', '/path/to/id_rsa',
190 '--test_that-args=--whitelist-chrome-crashes',
191 ])
192
193 def testMissingBuildTarget(self):
194 """Test VmTest dies when build_target not set."""
195 input_proto = self._GetInput(build_target=None)
196 with self.assertRaises(cros_build_lib.DieSystemExit):
197 test_controller.VmTest(input_proto, None)
198
199 def testMissingVmImage(self):
200 """Test VmTest dies when vm_image not set."""
201 input_proto = self._GetInput(vm_image=None)
202 with self.assertRaises(cros_build_lib.DieSystemExit):
203 test_controller.VmTest(input_proto, None)
204
205 def testMissingTestHarness(self):
206 """Test VmTest dies when test_harness not specified."""
207 input_proto = self._GetInput(
208 test_harness=test_pb2.VmTestRequest.UNSPECIFIED)
209 with self.assertRaises(cros_build_lib.DieSystemExit):
210 test_controller.VmTest(input_proto, None)
211
212 def testMissingVmTests(self):
213 """Test VmTest dies when vm_tests not set."""
214 input_proto = self._GetInput(vm_tests=[])
215 with self.assertRaises(cros_build_lib.DieSystemExit):
216 test_controller.VmTest(input_proto, None)