Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """The test controller tests.""" |
| 6 | |
Alex Klein | 9f91578 | 2020-02-14 23:15:09 +0000 | [diff] [blame] | 7 | import contextlib |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 8 | import os |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 9 | from unittest import mock |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 10 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 11 | from chromite.api import api_config |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 12 | from chromite.api import controller |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 13 | from chromite.api.controller import test as test_controller |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 14 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 15 | from chromite.api.gen.chromite.api import test_pb2 |
Andrew Lamb | 763e3be | 2021-07-27 17:22:02 -0600 | [diff] [blame] | 16 | from chromite.api.gen.chromiumos.build.api import system_image_pb2 |
| 17 | from chromite.api.gen.chromiumos.build.api import portage_pb2 |
Andrew Lamb | d814afa | 2021-08-11 11:04:20 -0600 | [diff] [blame] | 18 | from chromite.api.gen.chromiumos.config.payload import flat_config_pb2 |
| 19 | from chromite.api.gen.chromiumos.config.api import design_pb2 |
| 20 | from chromite.api.gen.chromiumos.config.api import design_id_pb2 |
Andrew Lamb | 763e3be | 2021-07-27 17:22:02 -0600 | [diff] [blame] | 21 | from chromite.api.gen.chromiumos.test.api import coverage_rule_pb2 |
| 22 | from chromite.api.gen.chromiumos.test.api import dut_attribute_pb2 |
| 23 | from chromite.api.gen.chromiumos.test.api import test_suite_pb2 |
| 24 | from chromite.api.gen.chromiumos.test.plan import source_test_plan_pb2 |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 25 | from chromite.lib import build_target_lib |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 26 | from chromite.lib import chroot_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 27 | from chromite.lib import cros_build_lib |
| 28 | from chromite.lib import cros_test_lib |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 29 | from chromite.lib import image_lib |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 30 | from chromite.lib import osutils |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 31 | from chromite.lib import sysroot_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 32 | from chromite.lib.parser import package_info |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 33 | from chromite.scripts import cros_set_lsb_release |
| 34 | from chromite.service import test as test_service |
Andrew Lamb | 763e3be | 2021-07-27 17:22:02 -0600 | [diff] [blame] | 35 | from chromite.third_party.google.protobuf import json_format |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 36 | from chromite.utils import key_value_store |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 37 | |
| 38 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 39 | class DebugInfoTestTest(cros_test_lib.MockTempDirTestCase, |
| 40 | api_config.ApiConfigMixin): |
| 41 | """Tests for the DebugInfoTest function.""" |
| 42 | |
| 43 | def setUp(self): |
| 44 | self.board = 'board' |
| 45 | self.chroot_path = os.path.join(self.tempdir, 'chroot') |
| 46 | self.sysroot_path = '/build/board' |
| 47 | self.full_sysroot_path = os.path.join(self.chroot_path, |
| 48 | self.sysroot_path.lstrip(os.sep)) |
| 49 | osutils.SafeMakedirs(self.full_sysroot_path) |
| 50 | |
| 51 | def _GetInput(self, sysroot_path=None, build_target=None): |
| 52 | """Helper to build an input message instance.""" |
| 53 | proto = test_pb2.DebugInfoTestRequest() |
| 54 | if sysroot_path: |
| 55 | proto.sysroot.path = sysroot_path |
| 56 | if build_target: |
| 57 | proto.sysroot.build_target.name = build_target |
| 58 | return proto |
| 59 | |
| 60 | def _GetOutput(self): |
| 61 | """Helper to get an empty output message instance.""" |
| 62 | return test_pb2.DebugInfoTestResponse() |
| 63 | |
| 64 | def testValidateOnly(self): |
| 65 | """Sanity check that a validate only call does not execute any logic.""" |
| 66 | patch = self.PatchObject(test_service, 'DebugInfoTest') |
| 67 | input_msg = self._GetInput(sysroot_path=self.full_sysroot_path) |
| 68 | test_controller.DebugInfoTest(input_msg, self._GetOutput(), |
| 69 | self.validate_only_config) |
| 70 | patch.assert_not_called() |
| 71 | |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 72 | def testMockError(self): |
| 73 | """Test mock error call does not execute any logic, returns error.""" |
| 74 | patch = self.PatchObject(test_service, 'DebugInfoTest') |
| 75 | |
| 76 | input_msg = self._GetInput(sysroot_path=self.full_sysroot_path) |
| 77 | rc = test_controller.DebugInfoTest(input_msg, self._GetOutput(), |
| 78 | self.mock_error_config) |
| 79 | patch.assert_not_called() |
| 80 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 81 | |
| 82 | def testMockCall(self): |
| 83 | """Test mock call does not execute any logic, returns success.""" |
| 84 | patch = self.PatchObject(test_service, 'DebugInfoTest') |
| 85 | |
| 86 | input_msg = self._GetInput(sysroot_path=self.full_sysroot_path) |
| 87 | rc = test_controller.DebugInfoTest(input_msg, self._GetOutput(), |
| 88 | self.mock_call_config) |
| 89 | patch.assert_not_called() |
| 90 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 91 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 92 | def testNoBuildTargetNoSysrootFails(self): |
| 93 | """Test missing build target name and sysroot path fails.""" |
| 94 | input_msg = self._GetInput() |
| 95 | output_msg = self._GetOutput() |
| 96 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 97 | test_controller.DebugInfoTest(input_msg, output_msg, self.api_config) |
| 98 | |
| 99 | def testDebugInfoTest(self): |
| 100 | """Call DebugInfoTest with valid sysroot_path.""" |
| 101 | request = self._GetInput(sysroot_path=self.full_sysroot_path) |
| 102 | |
| 103 | test_controller.DebugInfoTest(request, self._GetOutput(), self.api_config) |
| 104 | |
| 105 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 106 | class BuildTargetUnitTestTest(cros_test_lib.MockTempDirTestCase, |
| 107 | api_config.ApiConfigMixin): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 108 | """Tests for the UnitTest function.""" |
| 109 | |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 110 | def _GetInput(self, |
| 111 | board=None, |
| 112 | result_path=None, |
| 113 | chroot_path=None, |
| 114 | cache_dir=None, |
| 115 | empty_sysroot=None, |
| 116 | packages=None, |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 117 | blocklist=None): |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 118 | """Helper to build an input message instance.""" |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 119 | formatted_packages = [] |
| 120 | for pkg in packages or []: |
| 121 | formatted_packages.append({ |
| 122 | 'category': pkg.category, |
| 123 | 'package_name': pkg.package |
| 124 | }) |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 125 | formatted_blocklist = [] |
| 126 | for pkg in blocklist or []: |
| 127 | formatted_blocklist.append({'category': pkg.category, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 128 | 'package_name': pkg.package}) |
| 129 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 130 | return test_pb2.BuildTargetUnitTestRequest( |
| 131 | build_target={'name': board}, result_path=result_path, |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 132 | chroot={'path': chroot_path, 'cache_dir': cache_dir}, |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 133 | flags={'empty_sysroot': empty_sysroot}, |
Alex Klein | 64ac34c | 2020-09-23 10:21:33 -0600 | [diff] [blame] | 134 | packages=formatted_packages, |
Alex Klein | 157caf4 | 2021-07-01 14:36:43 -0600 | [diff] [blame] | 135 | package_blocklist=formatted_blocklist, |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 136 | ) |
| 137 | |
| 138 | def _GetOutput(self): |
| 139 | """Helper to get an empty output message instance.""" |
| 140 | return test_pb2.BuildTargetUnitTestResponse() |
| 141 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 142 | def testValidateOnly(self): |
| 143 | """Sanity check that a validate only call does not execute any logic.""" |
| 144 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 145 | |
| 146 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 147 | test_controller.BuildTargetUnitTest(input_msg, self._GetOutput(), |
| 148 | self.validate_only_config) |
| 149 | patch.assert_not_called() |
| 150 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 151 | def testMockCall(self): |
| 152 | """Test that a mock call does not execute logic, returns mocked value.""" |
| 153 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 154 | |
| 155 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 156 | response = self._GetOutput() |
| 157 | test_controller.BuildTargetUnitTest(input_msg, response, |
| 158 | self.mock_call_config) |
| 159 | patch.assert_not_called() |
| 160 | self.assertEqual(response.tarball_path, |
| 161 | os.path.join(input_msg.result_path, 'unit_tests.tar')) |
| 162 | |
| 163 | def testMockError(self): |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 164 | """Test that a mock error does not execute logic, returns error.""" |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 165 | patch = self.PatchObject(test_service, 'BuildTargetUnitTest') |
| 166 | |
| 167 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 168 | response = self._GetOutput() |
| 169 | rc = test_controller.BuildTargetUnitTest(input_msg, response, |
| 170 | self.mock_error_config) |
| 171 | patch.assert_not_called() |
| 172 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
| 173 | self.assertTrue(response.failed_packages) |
| 174 | self.assertEqual(response.failed_packages[0].category, 'foo') |
| 175 | self.assertEqual(response.failed_packages[0].package_name, 'bar') |
| 176 | self.assertEqual(response.failed_packages[1].category, 'cat') |
| 177 | self.assertEqual(response.failed_packages[1].package_name, 'pkg') |
| 178 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 179 | def testNoArgumentFails(self): |
| 180 | """Test no arguments fails.""" |
| 181 | input_msg = self._GetInput() |
| 182 | output_msg = self._GetOutput() |
| 183 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 184 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 185 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 186 | |
| 187 | def testNoBuildTargetFails(self): |
| 188 | """Test missing build target name fails.""" |
| 189 | input_msg = self._GetInput(result_path=self.tempdir) |
| 190 | output_msg = self._GetOutput() |
| 191 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 192 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 193 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 194 | |
| 195 | def testNoResultPathFails(self): |
| 196 | """Test missing result path fails.""" |
| 197 | # Missing result_path. |
| 198 | input_msg = self._GetInput(board='board') |
| 199 | output_msg = self._GetOutput() |
| 200 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 201 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 202 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 203 | |
Alex Klein | 64ac34c | 2020-09-23 10:21:33 -0600 | [diff] [blame] | 204 | def testInvalidPackageFails(self): |
| 205 | """Test missing result path fails.""" |
| 206 | # Missing result_path. |
| 207 | pkg = package_info.PackageInfo(package='bar') |
| 208 | input_msg = self._GetInput(board='board', result_path=self.tempdir, |
| 209 | packages=[pkg]) |
| 210 | output_msg = self._GetOutput() |
| 211 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 212 | test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 213 | self.api_config) |
| 214 | |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 215 | def testPackageBuildFailure(self): |
| 216 | """Test handling of raised BuildPackageFailure.""" |
| 217 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 218 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 219 | |
| 220 | pkgs = ['cat/pkg', 'foo/bar'] |
| 221 | expected = [('cat', 'pkg'), ('foo', 'bar')] |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 222 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 223 | result = test_service.BuildTargetUnitTestResult(1, None) |
Alex Klein | ea0c89e | 2021-09-09 15:17:35 -0600 | [diff] [blame] | 224 | result.failed_pkgs = [package_info.parse(p) for p in pkgs] |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 225 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 226 | |
| 227 | input_msg = self._GetInput(board='board', result_path=self.tempdir) |
| 228 | output_msg = self._GetOutput() |
| 229 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 230 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 231 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 232 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 233 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 234 | self.assertTrue(output_msg.failed_packages) |
| 235 | failed = [] |
| 236 | for pi in output_msg.failed_packages: |
| 237 | failed.append((pi.category, pi.package_name)) |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 238 | self.assertCountEqual(expected, failed) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 239 | |
| 240 | def testOtherBuildScriptFailure(self): |
| 241 | """Test build script failure due to non-package emerge error.""" |
| 242 | tempdir = osutils.TempDir(base_dir=self.tempdir) |
| 243 | self.PatchObject(osutils, 'TempDir', return_value=tempdir) |
| 244 | |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 245 | result = test_service.BuildTargetUnitTestResult(1, None) |
| 246 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 247 | |
Alex Klein | f267446 | 2019-05-16 16:47:24 -0600 | [diff] [blame] | 248 | pkgs = ['foo/bar', 'cat/pkg'] |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 249 | blocklist = [package_info.SplitCPV(p, strict=False) for p in pkgs] |
Alex Klein | fa6ebdc | 2019-05-10 10:57:31 -0600 | [diff] [blame] | 250 | input_msg = self._GetInput(board='board', result_path=self.tempdir, |
Alex Klein | b64e5f8 | 2020-09-23 10:55:31 -0600 | [diff] [blame] | 251 | empty_sysroot=True, blocklist=blocklist) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 252 | output_msg = self._GetOutput() |
| 253 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 254 | rc = test_controller.BuildTargetUnitTest(input_msg, output_msg, |
| 255 | self.api_config) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 256 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 257 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
Alex Klein | a2e42c4 | 2019-04-17 16:13:19 -0600 | [diff] [blame] | 258 | self.assertFalse(output_msg.failed_packages) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 259 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 260 | def testBuildTargetUnitTest(self): |
| 261 | """Test BuildTargetUnitTest successful call.""" |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 262 | pkgs = ['foo/bar', 'cat/pkg'] |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 263 | packages = [package_info.SplitCPV(p, strict=False) for p in pkgs] |
Navil Perez | c0b29a8 | 2020-07-07 14:17:48 +0000 | [diff] [blame] | 264 | input_msg = self._GetInput( |
| 265 | board='board', result_path=self.tempdir, packages=packages) |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 266 | |
| 267 | result = test_service.BuildTargetUnitTestResult(0, None) |
| 268 | self.PatchObject(test_service, 'BuildTargetUnitTest', return_value=result) |
| 269 | |
| 270 | tarball_result = os.path.join(input_msg.result_path, 'unit_tests.tar') |
| 271 | self.PatchObject(test_service, 'BuildTargetUnitTestTarball', |
| 272 | return_value=tarball_result) |
| 273 | |
| 274 | response = self._GetOutput() |
| 275 | test_controller.BuildTargetUnitTest(input_msg, response, |
| 276 | self.api_config) |
| 277 | self.assertEqual(response.tarball_path, |
| 278 | os.path.join(input_msg.result_path, 'unit_tests.tar')) |
| 279 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 280 | |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 281 | class BuildTestServiceContainers(cros_test_lib.MockTestCase, |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 282 | api_config.ApiConfigMixin): |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 283 | """Tests for the BuildTestServiceContainers function.""" |
| 284 | |
| 285 | def setUp(self): |
| 286 | self.request = test_pb2.BuildTestServiceContainersRequest( |
| 287 | chroot={'path': '/path/to/chroot'}, |
| 288 | build_target={'name': 'build_target'}, |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 289 | version='R93-14033.0.0', |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 290 | ) |
| 291 | |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 292 | def testSuccess(self): |
| 293 | """Check passing case with mocked cros_build_lib.run.""" |
| 294 | patch = self.PatchObject( |
| 295 | cros_build_lib, 'run', |
| 296 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 297 | |
| 298 | response = test_pb2.BuildTestServiceContainersResponse() |
| 299 | test_controller.BuildTestServiceContainers( |
| 300 | self.request, |
| 301 | response, |
| 302 | self.api_config) |
| 303 | patch.assert_called() |
| 304 | for result in response.results: |
| 305 | self.assertEqual(result.WhichOneof('result'), 'success') |
| 306 | |
C Shapiro | 91af1ce | 2021-06-17 12:42:09 -0500 | [diff] [blame] | 307 | def testFailure(self): |
| 308 | """Check failure case with mocked cros_build_lib.run.""" |
| 309 | patch = self.PatchObject( |
| 310 | cros_build_lib, 'run', |
| 311 | return_value=cros_build_lib.CommandResult(returncode=1)) |
| 312 | |
| 313 | response = test_pb2.BuildTestServiceContainersResponse() |
| 314 | test_controller.BuildTestServiceContainers( |
| 315 | self.request, |
| 316 | response, |
| 317 | self.api_config) |
| 318 | patch.assert_called() |
| 319 | for result in response.results: |
| 320 | self.assertEqual(result.WhichOneof('result'), 'failure') |
| 321 | |
| 322 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 323 | class ChromiteUnitTestTest(cros_test_lib.MockTestCase, |
| 324 | api_config.ApiConfigMixin): |
| 325 | """Tests for the ChromiteInfoTest function.""" |
| 326 | |
| 327 | def setUp(self): |
| 328 | self.board = 'board' |
| 329 | self.chroot_path = '/path/to/chroot' |
| 330 | |
| 331 | def _GetInput(self, chroot_path=None): |
| 332 | """Helper to build an input message instance.""" |
| 333 | proto = test_pb2.ChromiteUnitTestRequest( |
| 334 | chroot={'path': chroot_path}, |
| 335 | ) |
| 336 | return proto |
| 337 | |
| 338 | def _GetOutput(self): |
| 339 | """Helper to get an empty output message instance.""" |
| 340 | return test_pb2.ChromiteUnitTestResponse() |
| 341 | |
| 342 | def testValidateOnly(self): |
| 343 | """Sanity check that a validate only call does not execute any logic.""" |
| 344 | patch = self.PatchObject(cros_build_lib, 'run') |
| 345 | |
| 346 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 347 | test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 348 | self.validate_only_config) |
| 349 | patch.assert_not_called() |
| 350 | |
Michael Mortensen | 7a860eb | 2019-12-03 20:25:15 -0700 | [diff] [blame] | 351 | def testMockError(self): |
| 352 | """Test mock error call does not execute any logic, returns error.""" |
| 353 | patch = self.PatchObject(cros_build_lib, 'run') |
| 354 | |
| 355 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 356 | rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 357 | self.mock_error_config) |
| 358 | patch.assert_not_called() |
| 359 | self.assertEqual(controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY, rc) |
| 360 | |
| 361 | def testMockCall(self): |
| 362 | """Test mock call does not execute any logic, returns success.""" |
| 363 | patch = self.PatchObject(cros_build_lib, 'run') |
| 364 | |
| 365 | input_msg = self._GetInput(chroot_path=self.chroot_path) |
| 366 | rc = test_controller.ChromiteUnitTest(input_msg, self._GetOutput(), |
| 367 | self.mock_call_config) |
| 368 | patch.assert_not_called() |
| 369 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 370 | |
Michael Mortensen | 8ca4d3b | 2019-11-27 09:35:22 -0700 | [diff] [blame] | 371 | def testChromiteUnitTest(self): |
| 372 | """Call ChromiteUnitTest with mocked cros_build_lib.run.""" |
| 373 | request = self._GetInput(chroot_path=self.chroot_path) |
| 374 | patch = self.PatchObject( |
| 375 | cros_build_lib, 'run', |
| 376 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 377 | |
| 378 | test_controller.ChromiteUnitTest(request, self._GetOutput(), |
| 379 | self.api_config) |
| 380 | patch.assert_called_once() |
| 381 | |
| 382 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 383 | class CrosSigningTestTest(cros_test_lib.RunCommandTestCase, |
| 384 | api_config.ApiConfigMixin): |
| 385 | """CrosSigningTest tests.""" |
| 386 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 387 | def setUp(self): |
| 388 | self.chroot_path = '/path/to/chroot' |
| 389 | |
| 390 | def _GetInput(self, chroot_path=None): |
| 391 | """Helper to build an input message instance.""" |
| 392 | proto = test_pb2.CrosSigningTestRequest( |
| 393 | chroot={'path': chroot_path}, |
| 394 | ) |
| 395 | return proto |
| 396 | |
| 397 | def _GetOutput(self): |
| 398 | """Helper to get an empty output message instance.""" |
| 399 | return test_pb2.CrosSigningTestResponse() |
| 400 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 401 | def testValidateOnly(self): |
| 402 | """Sanity check that a validate only call does not execute any logic.""" |
| 403 | test_controller.CrosSigningTest(None, None, self.validate_only_config) |
| 404 | self.assertFalse(self.rc.call_count) |
| 405 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 406 | def testMockCall(self): |
| 407 | """Test mock call does not execute any logic, returns success.""" |
| 408 | rc = test_controller.CrosSigningTest(None, None, self.mock_call_config) |
| 409 | self.assertFalse(self.rc.call_count) |
| 410 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 411 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 412 | def testCrosSigningTest(self): |
| 413 | """Call CrosSigningTest with mocked cros_build_lib.run.""" |
| 414 | request = self._GetInput(chroot_path=self.chroot_path) |
| 415 | patch = self.PatchObject( |
| 416 | cros_build_lib, 'run', |
| 417 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 418 | |
| 419 | test_controller.CrosSigningTest(request, self._GetOutput(), |
| 420 | self.api_config) |
| 421 | patch.assert_called_once() |
| 422 | |
Alex Klein | 4bc8f4f | 2019-08-16 14:53:30 -0600 | [diff] [blame] | 423 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 424 | class SimpleChromeWorkflowTestTest(cros_test_lib.MockTestCase, |
| 425 | api_config.ApiConfigMixin): |
| 426 | """Test the SimpleChromeWorkflowTest endpoint.""" |
| 427 | |
| 428 | @staticmethod |
| 429 | def _Output(): |
| 430 | return test_pb2.SimpleChromeWorkflowTestResponse() |
| 431 | |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 432 | def _Input(self, |
| 433 | sysroot_path=None, |
| 434 | build_target=None, |
| 435 | chrome_root=None, |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 436 | goma_config=None): |
| 437 | proto = test_pb2.SimpleChromeWorkflowTestRequest() |
| 438 | if sysroot_path: |
| 439 | proto.sysroot.path = sysroot_path |
| 440 | if build_target: |
| 441 | proto.sysroot.build_target.name = build_target |
| 442 | if chrome_root: |
| 443 | proto.chrome_root = chrome_root |
| 444 | if goma_config: |
| 445 | proto.goma_config = goma_config |
| 446 | return proto |
| 447 | |
| 448 | def setUp(self): |
| 449 | self.chrome_path = 'path/to/chrome' |
| 450 | self.sysroot_dir = 'build/board' |
| 451 | self.build_target = 'amd64' |
| 452 | self.mock_simple_chrome_workflow_test = self.PatchObject( |
| 453 | test_service, 'SimpleChromeWorkflowTest') |
| 454 | |
| 455 | def testMissingBuildTarget(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 456 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 457 | input_proto = self._Input(build_target=None, sysroot_path='/sysroot/dir', |
| 458 | chrome_root='/chrome/path') |
| 459 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 460 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 461 | self.api_config) |
| 462 | |
| 463 | def testMissingSysrootPath(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 464 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 465 | input_proto = self._Input(build_target='board', sysroot_path=None, |
| 466 | chrome_root='/chrome/path') |
| 467 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 468 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 469 | self.api_config) |
| 470 | |
| 471 | def testMissingChromeRoot(self): |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 472 | """Test SimpleChromeWorkflowTest dies when build_target not set.""" |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 473 | input_proto = self._Input(build_target='board', sysroot_path='/sysroot/dir', |
| 474 | chrome_root=None) |
| 475 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 476 | test_controller.SimpleChromeWorkflowTest(input_proto, None, |
| 477 | self.api_config) |
| 478 | |
| 479 | def testSimpleChromeWorkflowTest(self): |
| 480 | """Call SimpleChromeWorkflowTest with valid args and temp dir.""" |
| 481 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 482 | chrome_root='/path/to/chrome') |
| 483 | response = self._Output() |
| 484 | |
| 485 | test_controller.SimpleChromeWorkflowTest(request, response, self.api_config) |
| 486 | self.mock_simple_chrome_workflow_test.assert_called() |
| 487 | |
| 488 | def testValidateOnly(self): |
| 489 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 490 | chrome_root='/path/to/chrome') |
| 491 | test_controller.SimpleChromeWorkflowTest(request, self._Output(), |
| 492 | self.validate_only_config) |
| 493 | self.mock_simple_chrome_workflow_test.assert_not_called() |
| 494 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 495 | def testMockCall(self): |
| 496 | """Test mock call does not execute any logic, returns success.""" |
| 497 | patch = self.mock_simple_chrome_workflow_test = self.PatchObject( |
| 498 | test_service, 'SimpleChromeWorkflowTest') |
| 499 | |
| 500 | request = self._Input(sysroot_path='sysroot_path', build_target='board', |
| 501 | chrome_root='/path/to/chrome') |
| 502 | rc = test_controller.SimpleChromeWorkflowTest(request, self._Output(), |
| 503 | self.mock_call_config) |
| 504 | patch.assert_not_called() |
| 505 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 506 | |
Michael Mortensen | c28d6f1 | 2019-10-03 13:34:51 -0600 | [diff] [blame] | 507 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 508 | class VmTestTest(cros_test_lib.RunCommandTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 509 | """Test the VmTest endpoint.""" |
| 510 | |
| 511 | def _GetInput(self, **kwargs): |
| 512 | values = dict( |
| 513 | build_target=common_pb2.BuildTarget(name='target'), |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 514 | vm_path=common_pb2.Path(path='/path/to/image.bin', |
| 515 | location=common_pb2.Path.INSIDE), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 516 | test_harness=test_pb2.VmTestRequest.TAST, |
| 517 | vm_tests=[test_pb2.VmTestRequest.VmTest(pattern='suite')], |
| 518 | ssh_options=test_pb2.VmTestRequest.SshOptions( |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 519 | port=1234, private_key_path={'path': '/path/to/id_rsa', |
Alex Klein | aa70541 | 2019-06-04 15:00:30 -0600 | [diff] [blame] | 520 | 'location': common_pb2.Path.INSIDE}), |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 521 | ) |
| 522 | values.update(kwargs) |
| 523 | return test_pb2.VmTestRequest(**values) |
| 524 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 525 | def _Output(self): |
| 526 | return test_pb2.VmTestResponse() |
| 527 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 528 | def testValidateOnly(self): |
| 529 | """Sanity check that a validate only call does not execute any logic.""" |
| 530 | test_controller.VmTest(self._GetInput(), None, self.validate_only_config) |
| 531 | self.assertEqual(0, self.rc.call_count) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 532 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 533 | def testMockCall(self): |
| 534 | """Test mock call does not execute any logic.""" |
| 535 | patch = self.PatchObject(cros_build_lib, 'run') |
| 536 | |
| 537 | request = self._GetInput() |
| 538 | response = self._Output() |
| 539 | # VmTest does not return a value, checking mocked value is flagged by lint. |
| 540 | test_controller.VmTest(request, response, self.mock_call_config) |
| 541 | patch.assert_not_called() |
| 542 | |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 543 | def testTastAllOptions(self): |
| 544 | """Test VmTest for Tast with all options set.""" |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 545 | test_controller.VmTest(self._GetInput(), None, self.api_config) |
| 546 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 547 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 548 | '--board', 'target', |
| 549 | '--image-path', '/path/to/image.bin', |
| 550 | '--tast', 'suite', |
| 551 | '--ssh-port', '1234', |
| 552 | '--private-key', '/path/to/id_rsa', |
| 553 | ]) |
| 554 | |
| 555 | def testAutotestAllOptions(self): |
| 556 | """Test VmTest for Autotest with all options set.""" |
| 557 | input_proto = self._GetInput(test_harness=test_pb2.VmTestRequest.AUTOTEST) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 558 | test_controller.VmTest(input_proto, None, self.api_config) |
| 559 | self.assertCommandContains([ |
Achuith Bhandarkar | a9e9c3d | 2019-05-22 13:56:11 -0700 | [diff] [blame] | 560 | 'cros_run_test', '--debug', '--no-display', '--copy-on-write', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 561 | '--board', 'target', |
| 562 | '--image-path', '/path/to/image.bin', |
| 563 | '--autotest', 'suite', |
| 564 | '--ssh-port', '1234', |
| 565 | '--private-key', '/path/to/id_rsa', |
Greg Edelston | dcb0e91 | 2020-08-31 11:09:40 -0600 | [diff] [blame] | 566 | '--test_that-args=--allow-chrome-crashes', |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 567 | ]) |
| 568 | |
| 569 | def testMissingBuildTarget(self): |
| 570 | """Test VmTest dies when build_target not set.""" |
| 571 | input_proto = self._GetInput(build_target=None) |
| 572 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 573 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 574 | |
| 575 | def testMissingVmImage(self): |
| 576 | """Test VmTest dies when vm_image not set.""" |
Alex Klein | 311b802 | 2019-06-05 16:00:07 -0600 | [diff] [blame] | 577 | input_proto = self._GetInput(vm_path=None) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 578 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 579 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 580 | |
| 581 | def testMissingTestHarness(self): |
| 582 | """Test VmTest dies when test_harness not specified.""" |
| 583 | input_proto = self._GetInput( |
| 584 | test_harness=test_pb2.VmTestRequest.UNSPECIFIED) |
| 585 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 586 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | 4e388a5 | 2019-05-01 12:16:33 -0600 | [diff] [blame] | 587 | |
| 588 | def testMissingVmTests(self): |
| 589 | """Test VmTest dies when vm_tests not set.""" |
| 590 | input_proto = self._GetInput(vm_tests=[]) |
| 591 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 592 | test_controller.VmTest(input_proto, None, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 593 | |
Michael Mortensen | 82cd62d | 2019-12-01 14:58:54 -0700 | [diff] [blame] | 594 | def testVmTest(self): |
| 595 | """Call VmTest with valid args and temp dir.""" |
| 596 | request = self._GetInput() |
| 597 | response = self._Output() |
| 598 | patch = self.PatchObject( |
| 599 | cros_build_lib, 'run', |
| 600 | return_value=cros_build_lib.CommandResult(returncode=0)) |
| 601 | |
| 602 | test_controller.VmTest(request, response, self.api_config) |
| 603 | patch.assert_called() |
| 604 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 605 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 606 | class MoblabVmTestTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 607 | """Test the MoblabVmTest endpoint.""" |
| 608 | |
| 609 | @staticmethod |
| 610 | def _Payload(path): |
| 611 | return test_pb2.MoblabVmTestRequest.Payload( |
| 612 | path=common_pb2.Path(path=path)) |
| 613 | |
| 614 | @staticmethod |
| 615 | def _Output(): |
| 616 | return test_pb2.MoblabVmTestResponse() |
| 617 | |
| 618 | def _Input(self): |
| 619 | return test_pb2.MoblabVmTestRequest( |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 620 | chroot=common_pb2.Chroot(path=self.chroot_dir), |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 621 | image_payload=self._Payload(self.image_payload_dir), |
| 622 | cache_payloads=[self._Payload(self.autotest_payload_dir)]) |
| 623 | |
| 624 | def setUp(self): |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 625 | self.chroot_dir = '/chroot' |
| 626 | self.chroot_tmp_dir = '/chroot/tmp' |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 627 | self.image_payload_dir = '/payloads/image' |
| 628 | self.autotest_payload_dir = '/payloads/autotest' |
| 629 | self.builder = 'moblab-generic-vm/R12-3.4.5-67.890' |
| 630 | self.image_cache_dir = '/mnt/moblab/cache' |
| 631 | self.image_mount_dir = '/mnt/image' |
| 632 | |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 633 | self.PatchObject(chroot_lib.Chroot, 'tempdir', osutils.TempDir) |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 634 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 635 | self.mock_create_moblab_vms = self.PatchObject( |
| 636 | test_service, 'CreateMoblabVm') |
| 637 | self.mock_prepare_moblab_vm_image_cache = self.PatchObject( |
| 638 | test_service, 'PrepareMoblabVmImageCache', |
| 639 | return_value=self.image_cache_dir) |
| 640 | self.mock_run_moblab_vm_tests = self.PatchObject( |
| 641 | test_service, 'RunMoblabVmTest') |
| 642 | self.mock_validate_moblab_vm_tests = self.PatchObject( |
| 643 | test_service, 'ValidateMoblabVmTest') |
| 644 | |
| 645 | @contextlib.contextmanager |
Alex Klein | 38c7d9e | 2019-05-08 09:31:19 -0600 | [diff] [blame] | 646 | def MockLoopbackPartitions(*_args, **_kwargs): |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 647 | mount = mock.MagicMock() |
Evan Hernandez | 40ee745 | 2019-06-13 12:51:43 -0600 | [diff] [blame] | 648 | mount.Mount.return_value = [self.image_mount_dir] |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 649 | yield mount |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 650 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 651 | self.PatchObject(image_lib, 'LoopbackPartitions', MockLoopbackPartitions) |
| 652 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 653 | def testValidateOnly(self): |
| 654 | """Sanity check that a validate only call does not execute any logic.""" |
| 655 | test_controller.MoblabVmTest(self._Input(), self._Output(), |
| 656 | self.validate_only_config) |
| 657 | self.mock_create_moblab_vms.assert_not_called() |
| 658 | |
Michael Mortensen | 7a7646d | 2019-12-12 15:36:14 -0700 | [diff] [blame] | 659 | def testMockCall(self): |
| 660 | """Test mock call does not execute any logic.""" |
| 661 | patch = self.PatchObject(key_value_store, 'LoadFile') |
| 662 | |
| 663 | # MoblabVmTest does not return a value, checking mocked value is flagged by |
| 664 | # lint. |
| 665 | test_controller.MoblabVmTest(self._Input(), self._Output(), |
| 666 | self.mock_call_config) |
| 667 | patch.assert_not_called() |
| 668 | |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 669 | def testImageContainsBuilder(self): |
| 670 | """MoblabVmTest calls service with correct args.""" |
| 671 | request = self._Input() |
| 672 | response = self._Output() |
| 673 | |
| 674 | self.PatchObject( |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 675 | key_value_store, 'LoadFile', |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 676 | return_value={cros_set_lsb_release.LSB_KEY_BUILDER_PATH: self.builder}) |
| 677 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 678 | test_controller.MoblabVmTest(request, response, self.api_config) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 679 | |
| 680 | self.assertEqual( |
| 681 | self.mock_create_moblab_vms.call_args_list, |
Evan Hernandez | e1e05d3 | 2019-07-19 12:32:18 -0600 | [diff] [blame] | 682 | [mock.call(mock.ANY, self.chroot_dir, self.image_payload_dir)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 683 | self.assertEqual( |
| 684 | self.mock_prepare_moblab_vm_image_cache.call_args_list, |
| 685 | [mock.call(mock.ANY, self.builder, [self.autotest_payload_dir])]) |
| 686 | self.assertEqual( |
| 687 | self.mock_run_moblab_vm_tests.call_args_list, |
Evan Hernandez | 655e804 | 2019-06-13 12:50:44 -0600 | [diff] [blame] | 688 | [mock.call(mock.ANY, mock.ANY, self.builder, self.image_cache_dir, |
| 689 | mock.ANY)]) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 690 | self.assertEqual( |
| 691 | self.mock_validate_moblab_vm_tests.call_args_list, |
| 692 | [mock.call(mock.ANY)]) |
| 693 | |
| 694 | def testImageMissingBuilder(self): |
| 695 | """MoblabVmTest dies when builder path not found in lsb-release.""" |
| 696 | request = self._Input() |
| 697 | response = self._Output() |
| 698 | |
Mike Frysinger | e652ba1 | 2019-09-08 00:57:43 -0400 | [diff] [blame] | 699 | self.PatchObject(key_value_store, 'LoadFile', return_value={}) |
Evan Hernandez | dc3f0bb | 2019-06-06 12:46:52 -0600 | [diff] [blame] | 700 | |
| 701 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 702 | test_controller.MoblabVmTest(request, response, self.api_config) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 703 | |
| 704 | |
| 705 | class GetArtifactsTest(cros_test_lib.MockTempDirTestCase): |
| 706 | """Test GetArtifacts.""" |
| 707 | |
| 708 | CODE_COVERAGE_LLVM_ARTIFACT_TYPE = ( |
| 709 | common_pb2.ArtifactsByService.Test.ArtifactType.CODE_COVERAGE_LLVM_JSON |
| 710 | ) |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 711 | UNIT_TEST_ARTIFACT_TYPE = ( |
| 712 | common_pb2.ArtifactsByService.Test.ArtifactType.UNIT_TESTS |
| 713 | ) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 714 | |
| 715 | def setUp(self): |
| 716 | """Set up the class for tests.""" |
| 717 | chroot_dir = os.path.join(self.tempdir, 'chroot') |
| 718 | osutils.SafeMakedirs(chroot_dir) |
| 719 | osutils.SafeMakedirs(os.path.join(chroot_dir, 'tmp')) |
| 720 | self.chroot = chroot_lib.Chroot(chroot_dir) |
| 721 | |
| 722 | sysroot_path = os.path.join(chroot_dir, 'build', 'board') |
| 723 | osutils.SafeMakedirs(sysroot_path) |
| 724 | self.sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 725 | |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 726 | self.build_target = build_target_lib.BuildTarget('board') |
| 727 | |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 728 | def testReturnsEmptyListWhenNoOutputArtifactsProvided(self): |
| 729 | """Test empty list is returned when there are no output_artifacts.""" |
| 730 | result = test_controller.GetArtifacts( |
| 731 | common_pb2.ArtifactsByService.Test(output_artifacts=[]), |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 732 | self.chroot, self.sysroot, self.build_target, self.tempdir) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 733 | |
| 734 | self.assertEqual(len(result), 0) |
| 735 | |
| 736 | def testShouldCallBundleCodeCoverageLlvmJsonForEachValidArtifact(self): |
| 737 | """Test BundleCodeCoverageLlvmJson is called on each valid artifact.""" |
| 738 | BundleCodeCoverageLlvmJson_mock = self.PatchObject( |
| 739 | test_service, 'BundleCodeCoverageLlvmJson', return_value='test') |
| 740 | |
| 741 | test_controller.GetArtifacts( |
| 742 | common_pb2.ArtifactsByService.Test(output_artifacts=[ |
| 743 | # Valid |
| 744 | common_pb2.ArtifactsByService.Test.ArtifactInfo( |
| 745 | artifact_types=[ |
| 746 | self.CODE_COVERAGE_LLVM_ARTIFACT_TYPE |
| 747 | ] |
| 748 | ), |
| 749 | |
| 750 | # Invalid |
| 751 | common_pb2.ArtifactsByService.Test.ArtifactInfo( |
| 752 | artifact_types=[ |
| 753 | common_pb2.ArtifactsByService.Test.ArtifactType.UNIT_TESTS |
| 754 | ] |
| 755 | ), |
| 756 | ]), |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 757 | self.chroot, self.sysroot, self.build_target, self.tempdir) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 758 | |
| 759 | BundleCodeCoverageLlvmJson_mock.assert_called_once() |
| 760 | |
| 761 | def testShouldReturnValidResult(self): |
| 762 | """Test result contains paths and code_coverage_llvm_json type.""" |
| 763 | self.PatchObject(test_service, 'BundleCodeCoverageLlvmJson', |
| 764 | return_value='test') |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 765 | self.PatchObject(test_service, 'BuildTargetUnitTestTarball', |
| 766 | return_value='unit_tests.tar') |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 767 | |
| 768 | result = test_controller.GetArtifacts( |
| 769 | common_pb2.ArtifactsByService.Test(output_artifacts=[ |
| 770 | # Valid |
| 771 | common_pb2.ArtifactsByService.Test.ArtifactInfo( |
| 772 | artifact_types=[ |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 773 | self.UNIT_TEST_ARTIFACT_TYPE |
| 774 | ] |
| 775 | ), |
| 776 | common_pb2.ArtifactsByService.Test.ArtifactInfo( |
| 777 | artifact_types=[ |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 778 | self.CODE_COVERAGE_LLVM_ARTIFACT_TYPE |
| 779 | ] |
| 780 | ), |
| 781 | ]), |
Jack Neus | c9707c3 | 2021-07-23 21:48:54 +0000 | [diff] [blame] | 782 | self.chroot, self.sysroot, self.build_target, self.tempdir) |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 783 | |
George Engelbrecht | 764b1cd | 2021-06-18 17:01:07 -0600 | [diff] [blame] | 784 | self.assertEqual(result[0]['paths'], ['unit_tests.tar']) |
| 785 | self.assertEqual(result[0]['type'], self.UNIT_TEST_ARTIFACT_TYPE) |
| 786 | self.assertEqual(result[1]['paths'], ['test']) |
| 787 | self.assertEqual(result[1]['type'], self.CODE_COVERAGE_LLVM_ARTIFACT_TYPE) |
Andrew Lamb | 763e3be | 2021-07-27 17:22:02 -0600 | [diff] [blame] | 788 | |
| 789 | |
| 790 | class GetCoverageRulesTest(cros_test_lib.RunCommandTempDirTestCase, |
| 791 | api_config.ApiConfigMixin): |
| 792 | """Tests for GetCoverageRules.""" |
| 793 | |
Andrew Lamb | d814afa | 2021-08-11 11:04:20 -0600 | [diff] [blame] | 794 | def _Input(self): |
Andrew Lamb | 763e3be | 2021-07-27 17:22:02 -0600 | [diff] [blame] | 795 | """Returns a sample GetCoverageRulesRequest for testing.""" |
Andrew Lamb | d814afa | 2021-08-11 11:04:20 -0600 | [diff] [blame] | 796 | build_metadata_list_path = os.path.join(self.tempdir, |
| 797 | 'build_metadata_list.jsonproto') |
| 798 | build_metadata_list = system_image_pb2.SystemImage.BuildMetadataList( |
| 799 | values=[ |
Andrew Lamb | 763e3be | 2021-07-27 17:22:02 -0600 | [diff] [blame] | 800 | system_image_pb2.SystemImage.BuildMetadata( |
| 801 | build_target=system_image_pb2.SystemImage.BuildTarget( |
| 802 | portage_build_target=portage_pb2.Portage.BuildTarget( |
| 803 | overlay_name='overlayA')), |
| 804 | package_summary=system_image_pb2.SystemImage.BuildMetadata |
| 805 | .PackageSummary( |
| 806 | kernel=system_image_pb2.SystemImage.BuildMetadata.Kernel( |
| 807 | version='4.4'))) |
Andrew Lamb | d814afa | 2021-08-11 11:04:20 -0600 | [diff] [blame] | 808 | ]) |
| 809 | osutils.WriteFile(build_metadata_list_path, |
| 810 | json_format.MessageToJson(build_metadata_list)) |
| 811 | |
| 812 | dut_attribute_list_path = os.path.join(self.tempdir, |
| 813 | 'dut_attribute_list.jsonproto') |
| 814 | dut_attribute_list = dut_attribute_pb2.DutAttributeList(dut_attributes=[ |
| 815 | dut_attribute_pb2.DutAttribute( |
| 816 | id=dut_attribute_pb2.DutAttribute.Id(value='system_build_target')) |
| 817 | ]) |
| 818 | osutils.WriteFile(dut_attribute_list_path, |
| 819 | json_format.MessageToJson(dut_attribute_list)) |
| 820 | |
| 821 | flat_config_list_path = os.path.join(self.tempdir, |
| 822 | 'flat_config_list.jsonproto') |
| 823 | flat_config_list = flat_config_pb2.FlatConfigList(values=[ |
| 824 | flat_config_pb2.FlatConfig( |
| 825 | hw_design=design_pb2.Design( |
| 826 | id=design_id_pb2.DesignId(value='design1') |
| 827 | ) |
| 828 | ) |
| 829 | ]) |
| 830 | osutils.WriteFile(flat_config_list_path, |
| 831 | json_format.MessageToJson(flat_config_list)) |
| 832 | |
| 833 | return test_pb2.GetCoverageRulesRequest( |
| 834 | source_test_plans=[ |
| 835 | source_test_plan_pb2.SourceTestPlan( |
| 836 | requirements=source_test_plan_pb2.SourceTestPlan.Requirements( |
| 837 | kernel_versions=source_test_plan_pb2.SourceTestPlan |
| 838 | .Requirements.KernelVersions()), |
| 839 | test_tags=['kernel']), |
| 840 | ], |
| 841 | build_metadata_list=common_pb2.Path( |
| 842 | path=build_metadata_list_path, location=common_pb2.Path.OUTSIDE), |
| 843 | dut_attribute_list=common_pb2.Path( |
| 844 | path=dut_attribute_list_path, location=common_pb2.Path.OUTSIDE), |
| 845 | flat_config_list=common_pb2.Path( |
| 846 | path=flat_config_list_path, location=common_pb2.Path.OUTSIDE), |
| 847 | ) |
Andrew Lamb | 763e3be | 2021-07-27 17:22:02 -0600 | [diff] [blame] | 848 | |
| 849 | @staticmethod |
| 850 | def _Output(): |
| 851 | """Returns a sample GetCoverageRulesResponse for testing.""" |
| 852 | return test_pb2.GetCoverageRulesResponse(coverage_rules=[ |
| 853 | coverage_rule_pb2.CoverageRule( |
| 854 | name='kernel:4.4', |
| 855 | test_suites=[ |
| 856 | test_suite_pb2.TestSuite( |
| 857 | test_case_tag_criteria=test_suite_pb2.TestSuite |
| 858 | .TestCaseTagCriteria(tags=['kernel'])) |
| 859 | ], |
| 860 | dut_criteria=[ |
| 861 | dut_attribute_pb2.DutCriterion( |
| 862 | attribute_id=dut_attribute_pb2.DutAttribute.Id( |
| 863 | value='system_build_target'), |
| 864 | values=['overlayA'], |
| 865 | ) |
| 866 | ]) |
| 867 | ]) |
| 868 | |
| 869 | @staticmethod |
| 870 | def _write_coverage_rules(path, coverage_rules): |
| 871 | """Write a list of CoverageRules in the same format as testplan.""" |
| 872 | osutils.WriteFile( |
| 873 | path, '\n'.join( |
| 874 | json_format.MessageToJson(rule).replace('\n', '') |
| 875 | for rule in coverage_rules)) |
| 876 | |
| 877 | def testWritesInputsAndReturnsCoverageRules(self): |
| 878 | """Test inputs are written, and output of testplan is parsed.""" |
| 879 | output_proto = test_pb2.GetCoverageRulesResponse() |
| 880 | |
| 881 | self.rc.SetDefaultCmdResult( |
| 882 | side_effect=lambda _: self._write_coverage_rules( |
| 883 | os.path.join(self.tempdir, 'out.jsonpb'), |
| 884 | self._Output().coverage_rules)) |
| 885 | self.PatchObject(osutils.TempDir, '__enter__', return_value=self.tempdir) |
| 886 | |
| 887 | test_controller.GetCoverageRules(self._Input(), output_proto, |
| 888 | self.api_config) |
| 889 | |
Andrew Lamb | 763e3be | 2021-07-27 17:22:02 -0600 | [diff] [blame] | 890 | self.assertEqual(output_proto, self._Output()) |