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