blob: 2289895e71596b8562f2f4ad9f4fbe54e331bd6a [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 Klein231d2da2019-07-22 16:44:45 -060013from chromite.api import api_config
Alex Klein8cb365a2019-05-15 16:24:53 -060014from chromite.api import controller
Alex Kleina2e42c42019-04-17 16:13:19 -060015from chromite.api.controller import test as test_controller
Evan Hernandez4e388a52019-05-01 12:16:33 -060016from chromite.api.gen.chromiumos import common_pb2
Alex Kleina2e42c42019-04-17 16:13:19 -060017from chromite.api.gen.chromite.api import test_pb2
Evan Hernandeze1e05d32019-07-19 12:32:18 -060018from chromite.lib import chroot_lib
Alex Kleina2e42c42019-04-17 16:13:19 -060019from chromite.lib import cros_build_lib
20from chromite.lib import cros_test_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
23from chromite.lib import portage_util
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -060024from chromite.scripts import cros_set_lsb_release
25from chromite.service import test as test_service
Mike Frysingere652ba12019-09-08 00:57:43 -040026from chromite.utils import key_value_store
Alex Kleina2e42c42019-04-17 16:13:19 -060027
28
Alex Klein231d2da2019-07-22 16:44:45 -060029class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase,
30 api_config.ApiConfigMixin):
Alex Kleina2e42c42019-04-17 16:13:19 -060031 """Tests for the UnitTest function."""
32
33 def _GetInput(self, board=None, result_path=None, chroot_path=None,
Alex Kleinf2674462019-05-16 16:47:24 -060034 cache_dir=None, empty_sysroot=None, blacklist=None):
Alex Kleina2e42c42019-04-17 16:13:19 -060035 """Helper to build an input message instance."""
Alex Kleinf2674462019-05-16 16:47:24 -060036 formatted_blacklist = []
37 for pkg in blacklist or []:
38 formatted_blacklist.append({'category': pkg.category,
39 'package_name': pkg.package})
40
Alex Kleina2e42c42019-04-17 16:13:19 -060041 return test_pb2.BuildTargetUnitTestRequest(
42 build_target={'name': board}, result_path=result_path,
Alex Kleinfa6ebdc2019-05-10 10:57:31 -060043 chroot={'path': chroot_path, 'cache_dir': cache_dir},
Alex Kleinf2674462019-05-16 16:47:24 -060044 flags={'empty_sysroot': empty_sysroot},
45 package_blacklist=formatted_blacklist,
Alex Kleina2e42c42019-04-17 16:13:19 -060046 )
47
48 def _GetOutput(self):
49 """Helper to get an empty output message instance."""
50 return test_pb2.BuildTargetUnitTestResponse()
51
Alex Klein231d2da2019-07-22 16:44:45 -060052 def testValidateOnly(self):
53 """Sanity check that a validate only call does not execute any logic."""
54 patch = self.PatchObject(test_service, 'BuildTargetUnitTest')
55
56 input_msg = self._GetInput(board='board', result_path=self.tempdir)
57 test_controller.BuildTargetUnitTest(input_msg, self._GetOutput(),
58 self.validate_only_config)
59 patch.assert_not_called()
60
Alex Kleina2e42c42019-04-17 16:13:19 -060061 def testNoArgumentFails(self):
62 """Test no arguments fails."""
63 input_msg = self._GetInput()
64 output_msg = self._GetOutput()
65 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060066 test_controller.BuildTargetUnitTest(input_msg, output_msg,
67 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -060068
69 def testNoBuildTargetFails(self):
70 """Test missing build target name fails."""
71 input_msg = self._GetInput(result_path=self.tempdir)
72 output_msg = self._GetOutput()
73 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060074 test_controller.BuildTargetUnitTest(input_msg, output_msg,
75 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -060076
77 def testNoResultPathFails(self):
78 """Test missing result path fails."""
79 # Missing result_path.
80 input_msg = self._GetInput(board='board')
81 output_msg = self._GetOutput()
82 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060083 test_controller.BuildTargetUnitTest(input_msg, output_msg,
84 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -060085
86 def testPackageBuildFailure(self):
87 """Test handling of raised BuildPackageFailure."""
88 tempdir = osutils.TempDir(base_dir=self.tempdir)
89 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
90
91 pkgs = ['cat/pkg', 'foo/bar']
92 expected = [('cat', 'pkg'), ('foo', 'bar')]
Alex Kleina2e42c42019-04-17 16:13:19 -060093
Alex Klein38c7d9e2019-05-08 09:31:19 -060094 result = test_service.BuildTargetUnitTestResult(1, None)
95 result.failed_cpvs = [portage_util.SplitCPV(p, strict=False) for p in pkgs]
96 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -060097
98 input_msg = self._GetInput(board='board', result_path=self.tempdir)
99 output_msg = self._GetOutput()
100
Alex Klein231d2da2019-07-22 16:44:45 -0600101 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg,
102 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600103
Alex Klein8cb365a2019-05-15 16:24:53 -0600104 self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600105 self.assertTrue(output_msg.failed_packages)
106 failed = []
107 for pi in output_msg.failed_packages:
108 failed.append((pi.category, pi.package_name))
Mike Frysinger678735c2019-09-28 18:23:28 -0400109 self.assertCountEqual(expected, failed)
Alex Kleina2e42c42019-04-17 16:13:19 -0600110
111 def testOtherBuildScriptFailure(self):
112 """Test build script failure due to non-package emerge error."""
113 tempdir = osutils.TempDir(base_dir=self.tempdir)
114 self.PatchObject(osutils, 'TempDir', return_value=tempdir)
115
Alex Klein38c7d9e2019-05-08 09:31:19 -0600116 result = test_service.BuildTargetUnitTestResult(1, None)
117 self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result)
Alex Kleina2e42c42019-04-17 16:13:19 -0600118
Alex Kleinf2674462019-05-16 16:47:24 -0600119 pkgs = ['foo/bar', 'cat/pkg']
120 blacklist = [portage_util.SplitCPV(p, strict=False) for p in pkgs]
Alex Kleinfa6ebdc2019-05-10 10:57:31 -0600121 input_msg = self._GetInput(board='board', result_path=self.tempdir,
Alex Kleinf2674462019-05-16 16:47:24 -0600122 empty_sysroot=True, blacklist=blacklist)
Alex Kleina2e42c42019-04-17 16:13:19 -0600123 output_msg = self._GetOutput()
124
Alex Klein231d2da2019-07-22 16:44:45 -0600125 rc = test_controller.BuildTargetUnitTest(input_msg, output_msg,
126 self.api_config)
Alex Kleina2e42c42019-04-17 16:13:19 -0600127
Alex Klein8cb365a2019-05-15 16:24:53 -0600128 self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc)
Alex Kleina2e42c42019-04-17 16:13:19 -0600129 self.assertFalse(output_msg.failed_packages)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600130
131
Alex Klein4bc8f4f2019-08-16 14:53:30 -0600132class CrosSigningTestTest(cros_test_lib.RunCommandTestCase,
133 api_config.ApiConfigMixin):
134 """CrosSigningTest tests."""
135
136 def testValidateOnly(self):
137 """Sanity check that a validate only call does not execute any logic."""
138 test_controller.CrosSigningTest(None, None, self.validate_only_config)
139 self.assertFalse(self.rc.call_count)
140
141
Alex Klein231d2da2019-07-22 16:44:45 -0600142class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin):
Evan Hernandez4e388a52019-05-01 12:16:33 -0600143 """Test the VmTest endpoint."""
144
145 def _GetInput(self, **kwargs):
146 values = dict(
147 build_target=common_pb2.BuildTarget(name='target'),
Alex Klein311b8022019-06-05 16:00:07 -0600148 vm_path=common_pb2.Path(path='/path/to/image.bin',
149 location=common_pb2.Path.INSIDE),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600150 test_harness=test_pb2.VmTestRequest.TAST,
151 vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')],
152 ssh_options=test_pb2.VmTestRequest.SshOptions(
Alex Klein231d2da2019-07-22 16:44:45 -0600153 port=1234, private_key_path={'path': '/path/to/id_rsa',
Alex Kleinaa705412019-06-04 15:00:30 -0600154 'location': common_pb2.Path.INSIDE}),
Evan Hernandez4e388a52019-05-01 12:16:33 -0600155 )
156 values.update(kwargs)
157 return test_pb2.VmTestRequest(**values)
158
Alex Klein231d2da2019-07-22 16:44:45 -0600159 def testValidateOnly(self):
160 """Sanity check that a validate only call does not execute any logic."""
161 test_controller.VmTest(self._GetInput(), None, self.validate_only_config)
162 self.assertEqual(0, self.rc.call_count)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600163
164 def testTastAllOptions(self):
165 """Test VmTest for Tast with all options set."""
Alex Klein231d2da2019-07-22 16:44:45 -0600166 test_controller.VmTest(self._GetInput(), None, self.api_config)
167 self.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700168 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600169 '--board', 'target',
170 '--image-path', '/path/to/image.bin',
171 '--tast', 'suite',
172 '--ssh-port', '1234',
173 '--private-key', '/path/to/id_rsa',
174 ])
175
176 def testAutotestAllOptions(self):
177 """Test VmTest for Autotest with all options set."""
178 input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST)
Alex Klein231d2da2019-07-22 16:44:45 -0600179 test_controller.VmTest(input_proto, None, self.api_config)
180 self.assertCommandContains([
Achuith Bhandarkara9e9c3d2019-05-22 13:56:11 -0700181 'cros_run_test', '--debug', '--no-display', '--copy-on-write',
Evan Hernandez4e388a52019-05-01 12:16:33 -0600182 '--board', 'target',
183 '--image-path', '/path/to/image.bin',
184 '--autotest', 'suite',
185 '--ssh-port', '1234',
186 '--private-key', '/path/to/id_rsa',
187 '--test_that-args=--whitelist-chrome-crashes',
188 ])
189
190 def testMissingBuildTarget(self):
191 """Test VmTest dies when build_target not set."""
192 input_proto = self._GetInput(build_target=None)
193 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600194 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600195
196 def testMissingVmImage(self):
197 """Test VmTest dies when vm_image not set."""
Alex Klein311b8022019-06-05 16:00:07 -0600198 input_proto = self._GetInput(vm_path=None)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600199 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600200 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600201
202 def testMissingTestHarness(self):
203 """Test VmTest dies when test_harness not specified."""
204 input_proto = self._GetInput(
205 test_harness=test_pb2.VmTestRequest.UNSPECIFIED)
206 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600207 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandez4e388a52019-05-01 12:16:33 -0600208
209 def testMissingVmTests(self):
210 """Test VmTest dies when vm_tests not set."""
211 input_proto = self._GetInput(vm_tests=[])
212 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600213 test_controller.VmTest(input_proto, None, self.api_config)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600214
215
Alex Klein231d2da2019-07-22 16:44:45 -0600216class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600217 """Test the MoblabVmTest endpoint."""
218
219 @staticmethod
220 def _Payload(path):
221 return test_pb2.MoblabVmTestRequest.Payload(
222 path=common_pb2.Path(path=path))
223
224 @staticmethod
225 def _Output():
226 return test_pb2.MoblabVmTestResponse()
227
228 def _Input(self):
229 return test_pb2.MoblabVmTestRequest(
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600230 chroot=common_pb2.Chroot(path=self.chroot_dir),
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600231 image_payload=self._Payload(self.image_payload_dir),
232 cache_payloads=[self._Payload(self.autotest_payload_dir)])
233
234 def setUp(self):
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600235 self.chroot_dir = '/chroot'
236 self.chroot_tmp_dir = '/chroot/tmp'
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600237 self.image_payload_dir = '/payloads/image'
238 self.autotest_payload_dir = '/payloads/autotest'
239 self.builder = 'moblab-generic-vm/R12-3.4.5-67.890'
240 self.image_cache_dir = '/mnt/moblab/cache'
241 self.image_mount_dir = '/mnt/image'
242
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600243 self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir)
Evan Hernandez655e8042019-06-13 12:50:44 -0600244
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600245 self.mock_create_moblab_vms = self.PatchObject(
246 test_service, 'CreateMoblabVm')
247 self.mock_prepare_moblab_vm_image_cache = self.PatchObject(
248 test_service, 'PrepareMoblabVmImageCache',
249 return_value=self.image_cache_dir)
250 self.mock_run_moblab_vm_tests = self.PatchObject(
251 test_service, 'RunMoblabVmTest')
252 self.mock_validate_moblab_vm_tests = self.PatchObject(
253 test_service, 'ValidateMoblabVmTest')
254
255 @contextlib.contextmanager
Alex Klein38c7d9e2019-05-08 09:31:19 -0600256 def MockLoopbackPartitions(*_args, **_kwargs):
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600257 mount = mock.MagicMock()
Evan Hernandez40ee7452019-06-13 12:51:43 -0600258 mount.Mount.return_value = [self.image_mount_dir]
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600259 yield mount
Alex Klein231d2da2019-07-22 16:44:45 -0600260
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600261 self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions)
262
Alex Klein231d2da2019-07-22 16:44:45 -0600263 def testValidateOnly(self):
264 """Sanity check that a validate only call does not execute any logic."""
265 test_controller.MoblabVmTest(self._Input(), self._Output(),
266 self.validate_only_config)
267 self.mock_create_moblab_vms.assert_not_called()
268
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600269 def testImageContainsBuilder(self):
270 """MoblabVmTest calls service with correct args."""
271 request = self._Input()
272 response = self._Output()
273
274 self.PatchObject(
Mike Frysingere652ba12019-09-08 00:57:43 -0400275 key_value_store, 'LoadFile',
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600276 return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder})
277
Alex Klein231d2da2019-07-22 16:44:45 -0600278 test_controller.MoblabVmTest(request, response, self.api_config)
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600279
280 self.assertEqual(
281 self.mock_create_moblab_vms.call_args_list,
Evan Hernandeze1e05d32019-07-19 12:32:18 -0600282 [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)])
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600283 self.assertEqual(
284 self.mock_prepare_moblab_vm_image_cache.call_args_list,
285 [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])])
286 self.assertEqual(
287 self.mock_run_moblab_vm_tests.call_args_list,
Evan Hernandez655e8042019-06-13 12:50:44 -0600288 [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir,
289 mock.ANY)])
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600290 self.assertEqual(
291 self.mock_validate_moblab_vm_tests.call_args_list,
292 [mock.call(mock.ANY)])
293
294 def testImageMissingBuilder(self):
295 """MoblabVmTest dies when builder path not found in lsb-release."""
296 request = self._Input()
297 response = self._Output()
298
Mike Frysingere652ba12019-09-08 00:57:43 -0400299 self.PatchObject(key_value_store, 'LoadFile', return_value={})
Evan Hernandezdc3f0bb2019-06-06 12:46:52 -0600300
301 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600302 test_controller.MoblabVmTest(request, response, self.api_config)