Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 10 | import contextlib |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 11 | import mock |
| 12 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 13 | from chromite.api import controller |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 14 | from chromite.api.controller import test as test_controller |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 15 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 16 | from chromite.api.gen.chromite.api import test_pb2 |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 17 | from chromite.lib import cros_build_lib |
| 18 | from chromite.lib import cros_test_lib |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 19 | from chromite.lib import image_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 20 | from chromite.lib import osutils |
| 21 | from chromite.lib import portage_util |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 22 | from chromite.scripts import cros_set_lsb_release |
| 23 | from chromite.service import test as test_service |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 24 | |
| 25 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 26 | class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 27 | """Tests for the UnitTest function.""" |
| 28 | |
| 29 | def _GetInput(self, board=None, result_path=None, chroot_path=None, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 30 | cache_dir=None, empty_sysroot=None, blacklist=None): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 31 | """Helper to build an input message instance.""" |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 32 | formatted_blacklist = [] |
| 33 | for pkg in blacklist or []: |
| 34 | formatted_blacklist.append({'category': pkg.category, |
| 35 | 'package_name': pkg.package}) |
| 36 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 37 | return test_pb2.BuildTargetUnitTestRequest( |
| 38 | build_target={'name': board}, result_path=result_path, |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 39 | chroot={'path': chroot_path, 'cache_dir': cache_dir}, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 40 | flags={'empty_sysroot': empty_sysroot}, |
| 41 | package_blacklist=formatted_blacklist, |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 42 | ) |
| 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 Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 77 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 78 | 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 Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 81 | |
| 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 Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 87 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 88 | 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 Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 99 | result = test_service.BuildTargetUnitTestResult(1, None) |
| 100 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 101 | |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 102 | pkgs = ['foo/bar', 'cat/pkg'] |
| 103 | blacklist = [portage_util.SplitCPV(p, strict=False) for p in pkgs] |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 104 | input_msg = self._GetInput(board='board', result_path=self.tempdir, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 105 | empty_sysroot=True, blacklist=blacklist) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 106 | output_msg = self._GetOutput() |
| 107 | |
| 108 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg) |
| 109 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 110 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 111 | self.assertFalse(output_msg.failed_packages) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 112 | |
| 113 | |
| 114 | class 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 Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 120 | vm_path=common_pb2.Path(path='/path/to/image.bin', |
| 121 | location=common_pb2.Path.INSIDE), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 122 | test_harness=test_pb2.VmTestRequest.TAST, |
| 123 | vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')], |
| 124 | ssh_options=test_pb2.VmTestRequest.SshOptions( |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 125 | port=1234, private_key_path={'path':'/path/to/id_rsa', |
| 126 | 'location': common_pb2.Path.INSIDE}), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 127 | ) |
| 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 Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 140 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 141 | '--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 Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 153 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 154 | '--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 Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 170 | input_proto = self._GetInput(vm_path=None) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 171 | 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 Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 186 | |
| 187 | |
| 188 | class 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 Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 223 | def MockLoopbackPartitions(*_args, **_kwargs): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 224 | 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) |