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