blob: 07daa6c5c09c09bdfc3df4cb3a2574ce246517ea [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
10from chromite.api.controller import test as test_controller
Evan Hernandez4e388a52019-05-01 12:16:33 -060011from chromite.api.gen.chromiumos import common_pb2
Alex Klein4f0eb432019-05-02 13:56:04 -060012from chromite.api.gen.chromite.api import image_pb2
Alex Kleina2e42c42019-04-17 16:13:19 -060013from chromite.api.gen.chromite.api import test_pb2
14from chromite.cbuildbot import commands
15from chromite.lib import cros_build_lib
16from chromite.lib import cros_test_lib
17from chromite.lib import failures_lib
18from chromite.lib import osutils
19from chromite.lib import portage_util
20
21
Evan Hernandez4e388a52019-05-01 12:16:33 -060022class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase):
Alex Kleina2e42c42019-04-17 16:13:19 -060023 """Tests for the UnitTest function."""
24
25 def _GetInput(self, board=None, result_path=None, chroot_path=None,
26 cache_dir=None):
27 """Helper to build an input message instance."""
28 return test_pb2.BuildTargetUnitTestRequest(
29 build_target={'name': board}, result_path=result_path,
30 chroot={'path': chroot_path, 'cache_dir': cache_dir}
31 )
32
33 def _GetOutput(self):
34 """Helper to get an empty output message instance."""
35 return test_pb2.BuildTargetUnitTestResponse()
36
37 def testNoArgumentFails(self):
38 """Test no arguments fails."""
39 input_msg = self._GetInput()
40 output_msg = self._GetOutput()
41 with self.assertRaises(cros_build_lib.DieSystemExit):
42 test_controller.BuildTargetUnitTest(input_msg, output_msg)
43
44 def testNoBuildTargetFails(self):
45 """Test missing build target name fails."""
46 input_msg = self._GetInput(result_path=self.tempdir)
47 output_msg = self._GetOutput()
48 with self.assertRaises(cros_build_lib.DieSystemExit):
49 test_controller.BuildTargetUnitTest(input_msg, output_msg)
50
51 def testNoResultPathFails(self):
52 """Test missing result path fails."""
53 # Missing result_path.
54 input_msg = self._GetInput(board='board')
55 output_msg = self._GetOutput()
56 with self.assertRaises(cros_build_lib.DieSystemExit):
57 test_controller.BuildTargetUnitTest(input_msg, output_msg)
58
59 def testPackageBuildFailure(self):
60 """Test handling of raised BuildPackageFailure."""
61 tempdir = osutils.TempDir(base_dir=self.tempdir)
62 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
63
64 pkgs = ['cat/pkg', 'foo/bar']
65 expected = [('cat', 'pkg'), ('foo', 'bar')]
66 rce = cros_build_lib.RunCommandError('error',
67 cros_build_lib.CommandResult())
68 error = failures_lib.PackageBuildFailure(rce, 'shortname', pkgs)
69 self.PatchObject(commands, 'RunUnitTests', side_effect=error)
70
71 input_msg = self._GetInput(board='board', result_path=self.tempdir)
72 output_msg = self._GetOutput()
73
74 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg)
75
76 self.assertNotEqual(0, rc)
77 self.assertTrue(output_msg.failed_packages)
78 failed = []
79 for pi in output_msg.failed_packages:
80 failed.append((pi.category, pi.package_name))
81 self.assertItemsEqual(expected, failed)
82
83 def testPopulatedEmergeFile(self):
84 """Test build script failure due to using outside emerge status file."""
85 tempdir = osutils.TempDir(base_dir=self.tempdir)
86 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
87
88 pkgs = ['cat/pkg', 'foo/bar']
89 cpvs = [portage_util.SplitCPV(pkg, strict=False) for pkg in pkgs]
90 expected = [('cat', 'pkg'), ('foo', 'bar')]
91 rce = cros_build_lib.RunCommandError('error',
92 cros_build_lib.CommandResult())
93 error = failures_lib.BuildScriptFailure(rce, 'shortname')
94 self.PatchObject(commands, 'RunUnitTests', side_effect=error)
95 self.PatchObject(portage_util, 'ParseParallelEmergeStatusFile',
96 return_value=cpvs)
97
98 input_msg = self._GetInput(board='board', result_path=self.tempdir)
99 output_msg = self._GetOutput()
100
101 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg)
102
103 self.assertNotEqual(0, rc)
104 self.assertTrue(output_msg.failed_packages)
105 failed = []
106 for pi in output_msg.failed_packages:
107 failed.append((pi.category, pi.package_name))
108 self.assertItemsEqual(expected, failed)
109
110 def testOtherBuildScriptFailure(self):
111 """Test build script failure due to non-package emerge error."""
112 tempdir = osutils.TempDir(base_dir=self.tempdir)
113 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
114
115 rce = cros_build_lib.RunCommandError('error',
116 cros_build_lib.CommandResult())
117 error = failures_lib.BuildScriptFailure(rce, 'shortname')
118 self.PatchObject(commands, 'RunUnitTests', side_effect=error)
119 self.PatchObject(portage_util, 'ParseParallelEmergeStatusFile',
120 return_value=[])
121
122 input_msg = self._GetInput(board='board', result_path=self.tempdir)
123 output_msg = self._GetOutput()
124
125 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg)
126
127 self.assertNotEqual(0, rc)
128 self.assertFalse(output_msg.failed_packages)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600129
130
131class VmTestTest(cros_test_lib.MockTestCase):
132 """Test the VmTest endpoint."""
133
134 def _GetInput(self, **kwargs):
135 values = dict(
136 build_target=common_pb2.BuildTarget(name='target'),
Alex Klein4f0eb432019-05-02 13:56:04 -0600137 vm_image=image_pb2.VmImage(path='/path/to/image.bin'),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600138 test_harness=test_pb2.VmTestRequest.TAST,
139 vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')],
140 ssh_options=test_pb2.VmTestRequest.SshOptions(
141 port=1234, private_key_path='/path/to/id_rsa'),
142 )
143 values.update(kwargs)
144 return test_pb2.VmTestRequest(**values)
145
146 def setUp(self):
147 self.rc_mock = cros_test_lib.RunCommandMock()
148 self.rc_mock.SetDefaultCmdResult()
149 self.StartPatcher(self.rc_mock)
150
151 def testTastAllOptions(self):
152 """Test VmTest for Tast with all options set."""
153 test_controller.VmTest(self._GetInput(), None)
154 self.rc_mock.assertCommandContains([
155 'cros_run_vm_test', '--debug', '--no-display', '--copy-on-write',
156 '--board', 'target',
157 '--image-path', '/path/to/image.bin',
158 '--tast', 'suite',
159 '--ssh-port', '1234',
160 '--private-key', '/path/to/id_rsa',
161 ])
162
163 def testAutotestAllOptions(self):
164 """Test VmTest for Autotest with all options set."""
165 input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST)
166 test_controller.VmTest(input_proto, None)
167 self.rc_mock.assertCommandContains([
168 'cros_run_vm_test', '--debug', '--no-display', '--copy-on-write',
169 '--board', 'target',
170 '--image-path', '/path/to/image.bin',
171 '--autotest', 'suite',
172 '--ssh-port', '1234',
173 '--private-key', '/path/to/id_rsa',
174 '--test_that-args=--whitelist-chrome-crashes',
175 ])
176
177 def testMissingBuildTarget(self):
178 """Test VmTest dies when build_target not set."""
179 input_proto = self._GetInput(build_target=None)
180 with self.assertRaises(cros_build_lib.DieSystemExit):
181 test_controller.VmTest(input_proto, None)
182
183 def testMissingVmImage(self):
184 """Test VmTest dies when vm_image not set."""
185 input_proto = self._GetInput(vm_image=None)
186 with self.assertRaises(cros_build_lib.DieSystemExit):
187 test_controller.VmTest(input_proto, None)
188
189 def testMissingTestHarness(self):
190 """Test VmTest dies when test_harness not specified."""
191 input_proto = self._GetInput(
192 test_harness=test_pb2.VmTestRequest.UNSPECIFIED)
193 with self.assertRaises(cros_build_lib.DieSystemExit):
194 test_controller.VmTest(input_proto, None)
195
196 def testMissingVmTests(self):
197 """Test VmTest dies when vm_tests not set."""
198 input_proto = self._GetInput(vm_tests=[])
199 with self.assertRaises(cros_build_lib.DieSystemExit):
200 test_controller.VmTest(input_proto, None)