blob: 616b487e618135cdeb601420bfbe49e1d0589ae1 [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
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060010import contextlib
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060011import mock
12
Alex Klein8cb365a2019-05-15 16:24:53 -060013from chromite.api import controller
Alex Kleina2e42c42019-04-17 16:13:19 -060014from chromite.api.controller import test as test_controller
Evan Hernandez4e388a52019-05-01 12:16:33 -060015from chromite.api.gen.chromiumos import common_pb2
Alex Kleina2e42c42019-04-17 16:13:19 -060016from chromite.api.gen.chromite.api import test_pb2
Alex Kleina2e42c42019-04-17 16:13:19 -060017from chromite.lib import cros_build_lib
18from chromite.lib import cros_test_lib
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060019from chromite.lib import image_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060020from chromite.lib import osutils
21from chromite.lib import portage_util
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060022from chromite.scripts import cros_set_lsb_release
23from chromite.service import test as test_service
Alex Kleina2e42c42019-04-17 16:13:19 -060024
25
Evan Hernandez4e388a52019-05-01 12:16:33 -060026class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase):
Alex Kleina2e42c42019-04-17 16:13:19 -060027 """Tests for the UnitTest function."""
28
29 def _GetInput(self, board=None, result_path=None, chroot_path=None,
Alex Kleinf2674462019-05-16 16:47:24 -060030 cache_dir=None, empty_sysroot=None, blacklist=None):
Alex Kleina2e42c42019-04-17 16:13:19 -060031 """Helper to build an input message instance."""
Alex Kleinf2674462019-05-16 16:47:24 -060032 formatted_blacklist = []
33 for pkg in blacklist or []:
34 formatted_blacklist.append({'category': pkg.category,
35 'package_name': pkg.package})
36
Alex Kleina2e42c42019-04-17 16:13:19 -060037 return test_pb2.BuildTargetUnitTestRequest(
38 build_target={'name': board}, result_path=result_path,
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060039 chroot={'path': chroot_path, 'cache_dir': cache_dir},
Alex Kleinf2674462019-05-16 16:47:24 -060040 flags={'empty_sysroot': empty_sysroot},
41 package_blacklist=formatted_blacklist,
Alex Kleina2e42c42019-04-17 16:13:19 -060042 )
43
44 def _GetOutput(self):
45 """Helper to get an empty output message instance."""
46 return test_pb2.BuildTargetUnitTestResponse()
47
48 def testNoArgumentFails(self):
49 """Test no arguments fails."""
50 input_msg = self._GetInput()
51 output_msg = self._GetOutput()
52 with self.assertRaises(cros_build_lib.DieSystemExit):
53 test_controller.BuildTargetUnitTest(input_msg, output_msg)
54
55 def testNoBuildTargetFails(self):
56 """Test missing build target name fails."""
57 input_msg = self._GetInput(result_path=self.tempdir)
58 output_msg = self._GetOutput()
59 with self.assertRaises(cros_build_lib.DieSystemExit):
60 test_controller.BuildTargetUnitTest(input_msg, output_msg)
61
62 def testNoResultPathFails(self):
63 """Test missing result path fails."""
64 # Missing result_path.
65 input_msg = self._GetInput(board='board')
66 output_msg = self._GetOutput()
67 with self.assertRaises(cros_build_lib.DieSystemExit):
68 test_controller.BuildTargetUnitTest(input_msg, output_msg)
69
70 def testPackageBuildFailure(self):
71 """Test handling of raised BuildPackageFailure."""
72 tempdir = osutils.TempDir(base_dir=self.tempdir)
73 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
74
75 pkgs = ['cat/pkg', 'foo/bar']
76 expected = [('cat', 'pkg'), ('foo', 'bar')]
Alex Kleina2e42c42019-04-17 16:13:19 -060077
Alex Klein38c7d9e2019-05-08 09:31:19 -060078 result = test_service.BuildTargetUnitTestResult(1, None)
79 result.failed_cpvs = [portage_util.SplitCPV(p, strict=False) for p in pkgs]
80 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -060081
82 input_msg = self._GetInput(board='board', result_path=self.tempdir)
83 output_msg = self._GetOutput()
84
85 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg)
86
Alex Klein8cb365a2019-05-15 16:24:53 -060087 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -060088 self.assertTrue(output_msg.failed_packages)
89 failed = []
90 for pi in output_msg.failed_packages:
91 failed.append((pi.category, pi.package_name))
92 self.assertItemsEqual(expected, failed)
93
94 def testOtherBuildScriptFailure(self):
95 """Test build script failure due to non-package emerge error."""
96 tempdir = osutils.TempDir(base_dir=self.tempdir)
97 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
98
Alex Klein38c7d9e2019-05-08 09:31:19 -060099 result = test_service.BuildTargetUnitTestResult(1, None)
100 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -0600101
Alex Kleinf2674462019-05-16 16:47:24 -0600102 pkgs = ['foo/bar', 'cat/pkg']
103 blacklist = [portage_util.SplitCPV(p, strict=False) for p in pkgs]
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600104 input_msg = self._GetInput(board='board', result_path=self.tempdir,
Alex Kleinf2674462019-05-16 16:47:24 -0600105 empty_sysroot=True, blacklist=blacklist)
Alex Kleina2e42c42019-04-17 16:13:19 -0600106 output_msg = self._GetOutput()
107
108 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg)
109
Alex Klein8cb365a2019-05-15 16:24:53 -0600110 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600111 self.assertFalse(output_msg.failed_packages)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600112
113
114class VmTestTest(cros_test_lib.MockTestCase):
115 """Test the VmTest endpoint."""
116
117 def _GetInput(self, **kwargs):
118 values = dict(
119 build_target=common_pb2.BuildTarget(name='target'),
Alex Klein311b8022019-06-05 16:00:07 -0600120 vm_path=common_pb2.Path(path='/path/to/image.bin',
121 location=common_pb2.Path.INSIDE),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600122 test_harness=test_pb2.VmTestRequest.TAST,
123 vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')],
124 ssh_options=test_pb2.VmTestRequest.SshOptions(
Alex Kleinaa705412019-06-04 15:00:30 -0600125 port=1234, private_key_path={'path':'/path/to/id_rsa',
126 'location': common_pb2.Path.INSIDE}),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600127 )
128 values.update(kwargs)
129 return test_pb2.VmTestRequest(**values)
130
131 def setUp(self):
132 self.rc_mock = cros_test_lib.RunCommandMock()
133 self.rc_mock.SetDefaultCmdResult()
134 self.StartPatcher(self.rc_mock)
135
136 def testTastAllOptions(self):
137 """Test VmTest for Tast with all options set."""
138 test_controller.VmTest(self._GetInput(), None)
139 self.rc_mock.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700140 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600141 '--board', 'target',
142 '--image-path', '/path/to/image.bin',
143 '--tast', 'suite',
144 '--ssh-port', '1234',
145 '--private-key', '/path/to/id_rsa',
146 ])
147
148 def testAutotestAllOptions(self):
149 """Test VmTest for Autotest with all options set."""
150 input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST)
151 test_controller.VmTest(input_proto, None)
152 self.rc_mock.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700153 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600154 '--board', 'target',
155 '--image-path', '/path/to/image.bin',
156 '--autotest', 'suite',
157 '--ssh-port', '1234',
158 '--private-key', '/path/to/id_rsa',
159 '--test_that-args=--whitelist-chrome-crashes',
160 ])
161
162 def testMissingBuildTarget(self):
163 """Test VmTest dies when build_target not set."""
164 input_proto = self._GetInput(build_target=None)
165 with self.assertRaises(cros_build_lib.DieSystemExit):
166 test_controller.VmTest(input_proto, None)
167
168 def testMissingVmImage(self):
169 """Test VmTest dies when vm_image not set."""
Alex Klein311b8022019-06-05 16:00:07 -0600170 input_proto = self._GetInput(vm_path=None)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600171 with self.assertRaises(cros_build_lib.DieSystemExit):
172 test_controller.VmTest(input_proto, None)
173
174 def testMissingTestHarness(self):
175 """Test VmTest dies when test_harness not specified."""
176 input_proto = self._GetInput(
177 test_harness=test_pb2.VmTestRequest.UNSPECIFIED)
178 with self.assertRaises(cros_build_lib.DieSystemExit):
179 test_controller.VmTest(input_proto, None)
180
181 def testMissingVmTests(self):
182 """Test VmTest dies when vm_tests not set."""
183 input_proto = self._GetInput(vm_tests=[])
184 with self.assertRaises(cros_build_lib.DieSystemExit):
185 test_controller.VmTest(input_proto, None)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600186
187
188class MoblabVmTestTest(cros_test_lib.MockTestCase):
189 """Test the MoblabVmTest endpoint."""
190
191 @staticmethod
192 def _Payload(path):
193 return test_pb2.MoblabVmTestRequest.Payload(
194 path=common_pb2.Path(path=path))
195
196 @staticmethod
197 def _Output():
198 return test_pb2.MoblabVmTestResponse()
199
200 def _Input(self):
201 return test_pb2.MoblabVmTestRequest(
202 image_payload=self._Payload(self.image_payload_dir),
203 cache_payloads=[self._Payload(self.autotest_payload_dir)])
204
205 def setUp(self):
206 self.image_payload_dir = '/payloads/image'
207 self.autotest_payload_dir = '/payloads/autotest'
208 self.builder = 'moblab-generic-vm/R12-3.4.5-67.890'
209 self.image_cache_dir = '/mnt/moblab/cache'
210 self.image_mount_dir = '/mnt/image'
211
212 self.mock_create_moblab_vms = self.PatchObject(
213 test_service, 'CreateMoblabVm')
214 self.mock_prepare_moblab_vm_image_cache = self.PatchObject(
215 test_service, 'PrepareMoblabVmImageCache',
216 return_value=self.image_cache_dir)
217 self.mock_run_moblab_vm_tests = self.PatchObject(
218 test_service, 'RunMoblabVmTest')
219 self.mock_validate_moblab_vm_tests = self.PatchObject(
220 test_service, 'ValidateMoblabVmTest')
221
222 @contextlib.contextmanager
Alex Klein38c7d9e2019-05-08 09:31:19 -0600223 def MockLoopbackPartitions(*_args, **_kwargs):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600224 mount = mock.MagicMock()
225 mount.destination = self.image_mount_dir
226 yield mount
227 self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions)
228
229 def testImageContainsBuilder(self):
230 """MoblabVmTest calls service with correct args."""
231 request = self._Input()
232 response = self._Output()
233
234 self.PatchObject(
235 cros_build_lib, 'LoadKeyValueFile',
236 return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder})
237
238 test_controller.MoblabVmTest(request, response)
239
240 self.assertEqual(
241 self.mock_create_moblab_vms.call_args_list,
242 [mock.call(mock.ANY, self.image_payload_dir)])
243 self.assertEqual(
244 self.mock_prepare_moblab_vm_image_cache.call_args_list,
245 [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])])
246 self.assertEqual(
247 self.mock_run_moblab_vm_tests.call_args_list,
248 [mock.call(mock.ANY, self.builder, self.image_cache_dir, mock.ANY)])
249 self.assertEqual(
250 self.mock_validate_moblab_vm_tests.call_args_list,
251 [mock.call(mock.ANY)])
252
253 def testImageMissingBuilder(self):
254 """MoblabVmTest dies when builder path not found in lsb-release."""
255 request = self._Input()
256 response = self._Output()
257
258 self.PatchObject(cros_build_lib, 'LoadKeyValueFile', return_value={})
259
260 with self.assertRaises(cros_build_lib.DieSystemExit):
261 test_controller.MoblabVmTest(request, response)