Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [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 | """Sysroot controller tests.""" |
| 6 | |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 7 | import datetime |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 8 | import os |
Lizzy Presland | fc1db00 | 2022-05-06 18:19:49 +0000 | [diff] [blame] | 9 | from typing import Union |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [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 | ca572ee | 2020-09-03 10:47:14 -0600 | [diff] [blame] | 13 | from chromite.api.controller import controller_util |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 14 | from chromite.api.controller import sysroot as sysroot_controller |
| 15 | from chromite.api.gen.chromite.api import sysroot_pb2 |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 16 | from chromite.api.gen.chromiumos import common_pb2 |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 17 | from chromite.lib import binpkg |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_build_lib |
| 19 | from chromite.lib import cros_test_lib |
| 20 | from chromite.lib import osutils |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [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 |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 23 | from chromite.service import sysroot as sysroot_service |
| 24 | |
| 25 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 26 | class CreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 27 | """Create function tests.""" |
| 28 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 29 | def _InputProto(self, |
| 30 | build_target=None, |
| 31 | profile=None, |
| 32 | replace=False, |
| 33 | current=False, |
| 34 | package_indexes=None): |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 35 | """Helper to build and input proto instance.""" |
| 36 | proto = sysroot_pb2.SysrootCreateRequest() |
| 37 | if build_target: |
| 38 | proto.build_target.name = build_target |
| 39 | if profile: |
| 40 | proto.profile.name = profile |
| 41 | if replace: |
| 42 | proto.flags.replace = replace |
| 43 | if current: |
| 44 | proto.flags.chroot_current = current |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 45 | if package_indexes: |
| 46 | proto.package_indexes.extend(package_indexes) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 47 | |
| 48 | return proto |
| 49 | |
| 50 | def _OutputProto(self): |
| 51 | """Helper to build output proto instance.""" |
| 52 | return sysroot_pb2.SysrootCreateResponse() |
| 53 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 54 | def testValidateOnly(self): |
| 55 | """Sanity check that a validate only call does not execute any logic.""" |
| 56 | patch = self.PatchObject(sysroot_service, 'Create') |
| 57 | |
| 58 | board = 'board' |
| 59 | profile = None |
| 60 | force = False |
| 61 | upgrade_chroot = True |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 62 | in_proto = self._InputProto( |
| 63 | build_target=board, |
| 64 | profile=profile, |
| 65 | replace=force, |
| 66 | current=not upgrade_chroot) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 67 | sysroot_controller.Create(in_proto, self._OutputProto(), |
| 68 | self.validate_only_config) |
| 69 | patch.assert_not_called() |
| 70 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 71 | def testMockCall(self): |
| 72 | """Sanity check that a mock call does not execute any logic.""" |
| 73 | patch = self.PatchObject(sysroot_service, 'Create') |
| 74 | request = self._InputProto() |
| 75 | response = self._OutputProto() |
| 76 | |
| 77 | rc = sysroot_controller.Create(request, response, self.mock_call_config) |
| 78 | |
| 79 | patch.assert_not_called() |
| 80 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 81 | |
| 82 | def testMockError(self): |
| 83 | """Sanity check that a mock error does not execute any logic.""" |
| 84 | patch = self.PatchObject(sysroot_service, 'Create') |
| 85 | request = self._InputProto() |
| 86 | response = self._OutputProto() |
| 87 | |
| 88 | rc = sysroot_controller.Create(request, response, self.mock_error_config) |
| 89 | |
| 90 | patch.assert_not_called() |
| 91 | self.assertEqual(controller.RETURN_CODE_UNRECOVERABLE, rc) |
| 92 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 93 | def testArgumentValidation(self): |
| 94 | """Test the input argument validation.""" |
| 95 | # Error when no name provided. |
| 96 | in_proto = self._InputProto() |
| 97 | out_proto = self._OutputProto() |
| 98 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 99 | sysroot_controller.Create(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 100 | |
| 101 | # Valid when board passed. |
| 102 | result = sysroot_lib.Sysroot('/sysroot/path') |
| 103 | patch = self.PatchObject(sysroot_service, 'Create', return_value=result) |
| 104 | in_proto = self._InputProto('board') |
| 105 | out_proto = self._OutputProto() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 106 | sysroot_controller.Create(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 107 | patch.assert_called_once() |
| 108 | |
| 109 | def testArgumentHandling(self): |
| 110 | """Test the arguments get processed and passed correctly.""" |
| 111 | sysroot_path = '/sysroot/path' |
| 112 | |
| 113 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 114 | create_patch = self.PatchObject( |
| 115 | sysroot_service, 'Create', return_value=sysroot) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 116 | rc_patch = self.PatchObject(sysroot_service, 'SetupBoardRunConfig') |
| 117 | |
| 118 | # Default values. |
| 119 | board = 'board' |
| 120 | profile = None |
| 121 | force = False |
| 122 | upgrade_chroot = True |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 123 | in_proto = self._InputProto( |
| 124 | build_target=board, |
| 125 | profile=profile, |
| 126 | replace=force, |
| 127 | current=not upgrade_chroot) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 128 | out_proto = self._OutputProto() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 129 | sysroot_controller.Create(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 130 | |
| 131 | # Default value checks. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 132 | rc_patch.assert_called_with( |
| 133 | force=force, upgrade_chroot=upgrade_chroot, package_indexes=[]) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 134 | self.assertEqual(board, out_proto.sysroot.build_target.name) |
| 135 | self.assertEqual(sysroot_path, out_proto.sysroot.path) |
| 136 | |
| 137 | # Not default values. |
| 138 | create_patch.reset_mock() |
| 139 | board = 'board' |
| 140 | profile = 'profile' |
| 141 | force = True |
| 142 | upgrade_chroot = False |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 143 | package_indexes = [ |
| 144 | common_pb2.PackageIndexInfo( |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 145 | snapshot_sha='SHA', |
| 146 | snapshot_number=5, |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 147 | build_target=common_pb2.BuildTarget(name=board), |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 148 | location='LOCATION', |
| 149 | profile=common_pb2.Profile(name=profile)), |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 150 | common_pb2.PackageIndexInfo( |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 151 | snapshot_sha='SHA2', |
| 152 | snapshot_number=4, |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 153 | build_target=common_pb2.BuildTarget(name=board), |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 154 | location='LOCATION2', |
| 155 | profile=common_pb2.Profile(name=profile)) |
| 156 | ] |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 157 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 158 | in_proto = self._InputProto( |
| 159 | build_target=board, |
| 160 | profile=profile, |
| 161 | replace=force, |
| 162 | current=not upgrade_chroot, |
| 163 | package_indexes=package_indexes) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 164 | out_proto = self._OutputProto() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 165 | sysroot_controller.Create(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 166 | |
| 167 | # Not default value checks. |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 168 | rc_patch.assert_called_with( |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 169 | force=force, |
| 170 | package_indexes=[ |
| 171 | binpkg.PackageIndexInfo.from_protobuf(x) for x in package_indexes |
| 172 | ], |
| 173 | upgrade_chroot=upgrade_chroot) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 174 | self.assertEqual(board, out_proto.sysroot.build_target.name) |
| 175 | self.assertEqual(sysroot_path, out_proto.sysroot.path) |
| 176 | |
| 177 | |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 178 | class GenerateArchiveTest(cros_test_lib.MockTempDirTestCase, |
| 179 | api_config.ApiConfigMixin): |
| 180 | """GenerateArchive function tests.""" |
| 181 | |
| 182 | def setUp(self): |
| 183 | self.chroot_path = '/path/to/chroot' |
| 184 | self.board = 'board' |
| 185 | |
| 186 | def _InputProto(self, build_target=None, chroot_path=None, pkg_list=None): |
| 187 | """Helper to build and input proto instance.""" |
| 188 | # pkg_list will be a list of category/package strings such as |
| 189 | # ['virtual/target-fuzzers']. |
| 190 | if pkg_list: |
| 191 | package_list = [] |
| 192 | for pkg in pkg_list: |
| 193 | pkg_string_parts = pkg.split('/') |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 194 | package_info_msg = common_pb2.PackageInfo( |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 195 | category=pkg_string_parts[0], package_name=pkg_string_parts[1]) |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 196 | package_list.append(package_info_msg) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 197 | else: |
| 198 | package_list = [] |
| 199 | |
| 200 | return sysroot_pb2.SysrootGenerateArchiveRequest( |
| 201 | build_target={'name': build_target}, |
| 202 | chroot={'path': chroot_path}, |
| 203 | packages=package_list) |
| 204 | |
| 205 | def _OutputProto(self): |
| 206 | """Helper to build output proto instance.""" |
| 207 | return sysroot_pb2.SysrootGenerateArchiveResponse() |
| 208 | |
| 209 | def testValidateOnly(self): |
| 210 | """Sanity check that a validate only call does not execute any logic.""" |
| 211 | patch = self.PatchObject(sysroot_service, 'GenerateArchive') |
| 212 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 213 | in_proto = self._InputProto( |
| 214 | build_target=self.board, |
| 215 | chroot_path=self.chroot_path, |
| 216 | pkg_list=['virtual/target-fuzzers']) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 217 | sysroot_controller.GenerateArchive(in_proto, self._OutputProto(), |
| 218 | self.validate_only_config) |
| 219 | patch.assert_not_called() |
| 220 | |
| 221 | def testMockCall(self): |
| 222 | """Sanity check that a mock call does not execute any logic.""" |
| 223 | patch = self.PatchObject(sysroot_service, 'GenerateArchive') |
| 224 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 225 | in_proto = self._InputProto( |
| 226 | build_target=self.board, |
| 227 | chroot_path=self.chroot_path, |
| 228 | pkg_list=['virtual/target-fuzzers']) |
| 229 | sysroot_controller.GenerateArchive(in_proto, self._OutputProto(), |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 230 | self.mock_call_config) |
| 231 | patch.assert_not_called() |
| 232 | |
| 233 | def testArgumentValidation(self): |
| 234 | """Test the input argument validation.""" |
| 235 | # Error when no build target provided. |
| 236 | in_proto = self._InputProto() |
| 237 | out_proto = self._OutputProto() |
| 238 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 239 | sysroot_controller.GenerateArchive(in_proto, out_proto, self.api_config) |
| 240 | |
| 241 | # Error when packages is not specified. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 242 | in_proto = self._InputProto( |
| 243 | build_target='board', chroot_path=self.chroot_path) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 244 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 245 | sysroot_controller.GenerateArchive(in_proto, out_proto, self.api_config) |
| 246 | |
| 247 | # Valid when board, chroot path, and package are specified. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 248 | patch = self.PatchObject( |
| 249 | sysroot_service, |
| 250 | 'GenerateArchive', |
| 251 | return_value='/path/to/sysroot/tar.bz') |
| 252 | in_proto = self._InputProto( |
| 253 | build_target='board', |
| 254 | chroot_path=self.chroot_path, |
| 255 | pkg_list=['virtual/target-fuzzers']) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 256 | out_proto = self._OutputProto() |
| 257 | sysroot_controller.GenerateArchive(in_proto, out_proto, self.api_config) |
| 258 | patch.assert_called_once() |
| 259 | |
| 260 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 261 | class InstallToolchainTest(cros_test_lib.MockTempDirTestCase, |
| 262 | api_config.ApiConfigMixin): |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 263 | """Install toolchain function tests.""" |
| 264 | |
| 265 | def setUp(self): |
| 266 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 267 | # Avoid running the portageq command. |
| 268 | self.PatchObject(sysroot_controller, '_LogBinhost') |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 269 | self.board = 'board' |
| 270 | self.sysroot = os.path.join(self.tempdir, 'board') |
| 271 | self.invalid_sysroot = os.path.join(self.tempdir, 'invalid', 'sysroot') |
| 272 | osutils.SafeMakedirs(self.sysroot) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 273 | # Set up portage log directory. |
Lizzy Presland | 4c27983 | 2021-11-19 20:27:43 +0000 | [diff] [blame] | 274 | self.target_sysroot = sysroot_lib.Sysroot(self.sysroot) |
Alex Klein | e1c4d4b | 2022-01-05 14:51:04 -0700 | [diff] [blame] | 275 | self.portage_dir = os.path.join(self.tempdir, 'portage_logdir') |
| 276 | self.PatchObject( |
| 277 | sysroot_lib.Sysroot, 'portage_logdir', new=self.portage_dir) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 278 | osutils.SafeMakedirs(self.portage_dir) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 279 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 280 | def _InputProto(self, |
| 281 | build_target=None, |
| 282 | sysroot_path=None, |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 283 | compile_source=False): |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 284 | """Helper to build an input proto instance.""" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 285 | proto = sysroot_pb2.InstallToolchainRequest() |
| 286 | if build_target: |
| 287 | proto.sysroot.build_target.name = build_target |
| 288 | if sysroot_path: |
| 289 | proto.sysroot.path = sysroot_path |
| 290 | if compile_source: |
| 291 | proto.flags.compile_source = compile_source |
| 292 | |
| 293 | return proto |
| 294 | |
| 295 | def _OutputProto(self): |
| 296 | """Helper to build output proto instance.""" |
| 297 | return sysroot_pb2.InstallToolchainResponse() |
| 298 | |
Lizzy Presland | fc1db00 | 2022-05-06 18:19:49 +0000 | [diff] [blame] | 299 | def _CreatePortageLogFile(self, log_path: Union[str, os.PathLike], |
| 300 | pkg_info: package_info.PackageInfo, |
| 301 | timestamp: datetime.datetime): |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 302 | """Creates a log file for testing for individual packages built by Portage. |
| 303 | |
| 304 | Args: |
Lizzy Presland | fc1db00 | 2022-05-06 18:19:49 +0000 | [diff] [blame] | 305 | log_path: The PORTAGE_LOGDIR path. |
| 306 | pkg_info: Package name used to name the log file. |
| 307 | timestamp: Timestamp used to name the file. |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 308 | """ |
Lizzy Presland | 4c27983 | 2021-11-19 20:27:43 +0000 | [diff] [blame] | 309 | path = os.path.join(log_path, |
Lizzy Presland | 7774178 | 2021-12-13 19:46:42 +0000 | [diff] [blame] | 310 | f'{pkg_info.category}:{pkg_info.pvr}:' \ |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 311 | f'{timestamp.strftime("%Y%m%d-%H%M%S")}.log') |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 312 | osutils.WriteFile( |
| 313 | path, f'Test log file for package {pkg_info.category}/' |
| 314 | f'{pkg_info.package} written to {path}') |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 315 | return path |
| 316 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 317 | def testValidateOnly(self): |
| 318 | """Sanity check that a validate only call does not execute any logic.""" |
| 319 | patch = self.PatchObject(sysroot_service, 'InstallToolchain') |
| 320 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 321 | in_proto = self._InputProto( |
| 322 | build_target=self.board, sysroot_path=self.sysroot) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 323 | sysroot_controller.InstallToolchain(in_proto, self._OutputProto(), |
| 324 | self.validate_only_config) |
| 325 | patch.assert_not_called() |
| 326 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 327 | def testMockCall(self): |
| 328 | """Sanity check that a mock call does not execute any logic.""" |
| 329 | patch = self.PatchObject(sysroot_service, 'InstallToolchain') |
| 330 | request = self._InputProto() |
| 331 | response = self._OutputProto() |
| 332 | |
| 333 | rc = sysroot_controller.InstallToolchain(request, response, |
| 334 | self.mock_call_config) |
| 335 | |
| 336 | patch.assert_not_called() |
| 337 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 338 | |
| 339 | def testMockError(self): |
| 340 | """Sanity check that a mock error does not execute any logic.""" |
| 341 | patch = self.PatchObject(sysroot_service, 'InstallToolchain') |
| 342 | request = self._InputProto() |
| 343 | response = self._OutputProto() |
| 344 | |
| 345 | rc = sysroot_controller.InstallToolchain(request, response, |
| 346 | self.mock_error_config) |
| 347 | |
| 348 | patch.assert_not_called() |
| 349 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Lizzy Presland | 239459a | 2022-05-05 22:03:19 +0000 | [diff] [blame] | 350 | self.assertTrue(response.failed_package_data) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 351 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 352 | def testArgumentValidation(self): |
| 353 | """Test the argument validation.""" |
| 354 | # Test errors on missing inputs. |
| 355 | out_proto = self._OutputProto() |
| 356 | # Both missing. |
| 357 | in_proto = self._InputProto() |
| 358 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 359 | sysroot_controller.InstallToolchain(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 360 | |
| 361 | # Sysroot path missing. |
| 362 | in_proto = self._InputProto(build_target=self.board) |
| 363 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 364 | sysroot_controller.InstallToolchain(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 365 | |
| 366 | # Build target name missing. |
| 367 | in_proto = self._InputProto(sysroot_path=self.sysroot) |
| 368 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 369 | sysroot_controller.InstallToolchain(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 370 | |
| 371 | # Both provided, but invalid sysroot path. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 372 | in_proto = self._InputProto( |
| 373 | build_target=self.board, sysroot_path=self.invalid_sysroot) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 374 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 375 | sysroot_controller.InstallToolchain(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 376 | |
| 377 | def testSuccessOutputHandling(self): |
| 378 | """Test the output is processed and recorded correctly.""" |
| 379 | self.PatchObject(sysroot_service, 'InstallToolchain') |
| 380 | out_proto = self._OutputProto() |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 381 | in_proto = self._InputProto( |
| 382 | build_target=self.board, sysroot_path=self.sysroot) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 383 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 384 | rc = sysroot_controller.InstallToolchain(in_proto, out_proto, |
| 385 | self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 386 | self.assertFalse(rc) |
Lizzy Presland | 239459a | 2022-05-05 22:03:19 +0000 | [diff] [blame] | 387 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 388 | |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 389 | def testErrorOutputHandling(self): |
| 390 | """Test the error output is processed and recorded correctly.""" |
| 391 | out_proto = self._OutputProto() |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 392 | in_proto = self._InputProto( |
| 393 | build_target=self.board, sysroot_path=self.sysroot) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 394 | |
Lizzy Presland | 7774178 | 2021-12-13 19:46:42 +0000 | [diff] [blame] | 395 | err_pkgs = ['cat/pkg-1.0-r1', 'cat2/pkg2-1.0-r1'] |
Alex Klein | ea0c89e | 2021-09-09 15:17:35 -0600 | [diff] [blame] | 396 | err_cpvs = [package_info.parse(pkg) for pkg in err_pkgs] |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 397 | expected = [('cat', 'pkg'), ('cat2', 'pkg2')] |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 398 | |
| 399 | new_logs = {} |
| 400 | for i, pkg in enumerate(err_pkgs): |
Lizzy Presland | 4c27983 | 2021-11-19 20:27:43 +0000 | [diff] [blame] | 401 | self._CreatePortageLogFile(self.portage_dir, err_cpvs[i], |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 402 | datetime.datetime(2021, 6, 9, 13, 37, 0)) |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 403 | new_logs[pkg] = self._CreatePortageLogFile( |
| 404 | self.portage_dir, err_cpvs[i], |
| 405 | datetime.datetime(2021, 6, 9, 16, 20, 0)) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 406 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 407 | err = sysroot_lib.ToolchainInstallError( |
| 408 | 'Error', cros_build_lib.CommandResult(), tc_info=err_cpvs) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 409 | self.PatchObject(sysroot_service, 'InstallToolchain', side_effect=err) |
| 410 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 411 | rc = sysroot_controller.InstallToolchain(in_proto, out_proto, |
| 412 | self.api_config) |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 413 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 414 | self.assertTrue(out_proto.failed_package_data) |
| 415 | # This needs to return 2 to indicate the available error response. |
| 416 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
| 417 | for data in out_proto.failed_package_data: |
| 418 | package = controller_util.deserialize_package_info(data.name) |
| 419 | cat_pkg = (data.name.category, data.name.package_name) |
| 420 | self.assertIn(cat_pkg, expected) |
Lizzy Presland | 7774178 | 2021-12-13 19:46:42 +0000 | [diff] [blame] | 421 | self.assertEqual(data.log_path.path, new_logs[package.cpvr]) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 422 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 423 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 424 | class InstallPackagesTest(cros_test_lib.MockTempDirTestCase, |
| 425 | api_config.ApiConfigMixin): |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 426 | """InstallPackages tests.""" |
| 427 | |
| 428 | def setUp(self): |
| 429 | self.PatchObject(cros_build_lib, 'IsInsideChroot', return_value=True) |
Alex Klein | a9d6460 | 2019-05-17 14:55:37 -0600 | [diff] [blame] | 430 | # Avoid running the portageq command. |
| 431 | self.PatchObject(sysroot_controller, '_LogBinhost') |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 432 | self.build_target = 'board' |
| 433 | self.sysroot = os.path.join(self.tempdir, 'build', 'board') |
| 434 | osutils.SafeMakedirs(self.sysroot) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 435 | # Set up portage log directory. |
Lizzy Presland | 4c27983 | 2021-11-19 20:27:43 +0000 | [diff] [blame] | 436 | self.target_sysroot = sysroot_lib.Sysroot(self.sysroot) |
Alex Klein | e1c4d4b | 2022-01-05 14:51:04 -0700 | [diff] [blame] | 437 | self.portage_dir = os.path.join(self.tempdir, 'portage_logdir') |
| 438 | self.PatchObject( |
| 439 | sysroot_lib.Sysroot, 'portage_logdir', new=self.portage_dir) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 440 | osutils.SafeMakedirs(self.portage_dir) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 441 | # Set up goma directories. |
| 442 | self.goma_dir = os.path.join(self.tempdir, 'goma_dir') |
| 443 | osutils.SafeMakedirs(self.goma_dir) |
| 444 | self.goma_out_dir = os.path.join(self.tempdir, 'goma_out_dir') |
| 445 | osutils.SafeMakedirs(self.goma_out_dir) |
Michael Mortensen | 4ccfb08 | 2020-01-22 16:24:03 -0700 | [diff] [blame] | 446 | os.environ['GLOG_log_dir'] = self.goma_dir |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 447 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 448 | def _InputProto(self, |
| 449 | build_target=None, |
| 450 | sysroot_path=None, |
| 451 | build_source=False, |
| 452 | goma_dir=None, |
| 453 | goma_log_dir=None, |
| 454 | goma_stats_file=None, |
| 455 | goma_counterz_file=None, |
| 456 | package_indexes=None, |
| 457 | packages=None): |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 458 | """Helper to build an input proto instance.""" |
| 459 | instance = sysroot_pb2.InstallPackagesRequest() |
| 460 | |
| 461 | if build_target: |
| 462 | instance.sysroot.build_target.name = build_target |
| 463 | if sysroot_path: |
| 464 | instance.sysroot.path = sysroot_path |
| 465 | if build_source: |
| 466 | instance.flags.build_source = build_source |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 467 | if goma_dir: |
| 468 | instance.goma_config.goma_dir = goma_dir |
| 469 | if goma_log_dir: |
| 470 | instance.goma_config.log_dir.dir = goma_log_dir |
| 471 | if goma_stats_file: |
| 472 | instance.goma_config.stats_file = goma_stats_file |
| 473 | if goma_counterz_file: |
| 474 | instance.goma_config.counterz_file = goma_counterz_file |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 475 | if package_indexes: |
| 476 | instance.package_indexes.extend(package_indexes) |
Alex Klein | ca572ee | 2020-09-03 10:47:14 -0600 | [diff] [blame] | 477 | if packages: |
| 478 | for pkg in packages: |
Alex Klein | b397b79 | 2021-09-09 15:55:45 -0600 | [diff] [blame] | 479 | pkg_info = package_info.parse(pkg) |
| 480 | pkg_info_msg = instance.packages.add() |
| 481 | controller_util.serialize_package_info(pkg_info, pkg_info_msg) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 482 | return instance |
| 483 | |
| 484 | def _OutputProto(self): |
| 485 | """Helper to build an empty output proto instance.""" |
| 486 | return sysroot_pb2.InstallPackagesResponse() |
| 487 | |
Lizzy Presland | fc1db00 | 2022-05-06 18:19:49 +0000 | [diff] [blame] | 488 | def _CreateGomaLogFile(self, goma_log_dir: Union[str, os.PathLike], name: str, |
| 489 | timestamp: datetime.datetime): |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 490 | """Creates a log file for testing. |
| 491 | |
| 492 | Args: |
Lizzy Presland | fc1db00 | 2022-05-06 18:19:49 +0000 | [diff] [blame] | 493 | goma_log_dir: Directory where the file will be created. |
| 494 | name: Log file 'base' name that is combined with the timestamp. |
| 495 | timestamp: Timestamp that is written to the file. |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 496 | """ |
| 497 | path = os.path.join( |
| 498 | goma_log_dir, |
| 499 | '%s.host.log.INFO.%s' % (name, timestamp.strftime('%Y%m%d-%H%M%S.%f'))) |
| 500 | osutils.WriteFile( |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 501 | path, timestamp.strftime('Goma log file created at: %Y/%m/%d %H:%M:%S')) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 502 | |
Lizzy Presland | fc1db00 | 2022-05-06 18:19:49 +0000 | [diff] [blame] | 503 | def _CreatePortageLogFile(self, log_path: Union[str, os.PathLike], |
| 504 | pkg_info: package_info.PackageInfo, |
| 505 | timestamp: datetime.datetime): |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 506 | """Creates a log file for testing for individual packages built by Portage. |
| 507 | |
| 508 | Args: |
Lizzy Presland | fc1db00 | 2022-05-06 18:19:49 +0000 | [diff] [blame] | 509 | log_path: The PORTAGE_LOGDIR path. |
| 510 | pkg_info: Package name used to name the log file. |
| 511 | timestamp: Timestamp used to name the file. |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 512 | """ |
Lizzy Presland | 4c27983 | 2021-11-19 20:27:43 +0000 | [diff] [blame] | 513 | path = os.path.join(log_path, |
Lizzy Presland | 7774178 | 2021-12-13 19:46:42 +0000 | [diff] [blame] | 514 | f'{pkg_info.category}:{pkg_info.pvr}:' \ |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 515 | f'{timestamp.strftime("%Y%m%d-%H%M%S")}.log') |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 516 | osutils.WriteFile( |
| 517 | path, f'Test log file for package {pkg_info.category}/' |
| 518 | f'{pkg_info.package} written to {path}') |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 519 | return path |
| 520 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 521 | def testValidateOnly(self): |
| 522 | """Sanity check that a validate only call does not execute any logic.""" |
| 523 | patch = self.PatchObject(sysroot_service, 'BuildPackages') |
| 524 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 525 | in_proto = self._InputProto( |
| 526 | build_target=self.build_target, sysroot_path=self.sysroot) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 527 | sysroot_controller.InstallPackages(in_proto, self._OutputProto(), |
| 528 | self.validate_only_config) |
| 529 | patch.assert_not_called() |
| 530 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 531 | def testMockCall(self): |
| 532 | """Sanity check that a mock call does not execute any logic.""" |
| 533 | patch = self.PatchObject(sysroot_service, 'BuildPackages') |
| 534 | request = self._InputProto() |
| 535 | response = self._OutputProto() |
| 536 | |
| 537 | rc = sysroot_controller.InstallPackages(request, response, |
| 538 | self.mock_call_config) |
| 539 | |
| 540 | patch.assert_not_called() |
| 541 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
| 542 | |
| 543 | def testMockError(self): |
| 544 | """Sanity check that a mock error does not execute any logic.""" |
| 545 | patch = self.PatchObject(sysroot_service, 'BuildPackages') |
| 546 | request = self._InputProto() |
| 547 | response = self._OutputProto() |
| 548 | |
| 549 | rc = sysroot_controller.InstallPackages(request, response, |
| 550 | self.mock_error_config) |
| 551 | |
| 552 | patch.assert_not_called() |
| 553 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Lizzy Presland | 239459a | 2022-05-05 22:03:19 +0000 | [diff] [blame] | 554 | self.assertTrue(response.failed_package_data) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 555 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 556 | def testArgumentValidationAllMissing(self): |
| 557 | """Test missing all arguments.""" |
| 558 | out_proto = self._OutputProto() |
| 559 | in_proto = self._InputProto() |
| 560 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 561 | sysroot_controller.InstallPackages(in_proto, out_proto, self.api_config) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 562 | |
| 563 | def testArgumentValidationNoSysroot(self): |
| 564 | """Test missing sysroot path.""" |
| 565 | out_proto = self._OutputProto() |
| 566 | in_proto = self._InputProto(build_target=self.build_target) |
| 567 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 568 | sysroot_controller.InstallPackages(in_proto, out_proto, self.api_config) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 569 | |
| 570 | def testArgumentValidationNoBuildTarget(self): |
| 571 | """Test missing build target name.""" |
| 572 | out_proto = self._OutputProto() |
| 573 | in_proto = self._InputProto(sysroot_path=self.sysroot) |
| 574 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 575 | sysroot_controller.InstallPackages(in_proto, out_proto, self.api_config) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 576 | |
| 577 | def testArgumentValidationInvalidSysroot(self): |
| 578 | """Test sysroot that hasn't had the toolchain installed.""" |
| 579 | out_proto = self._OutputProto() |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 580 | in_proto = self._InputProto( |
| 581 | build_target=self.build_target, sysroot_path=self.sysroot) |
| 582 | self.PatchObject( |
| 583 | sysroot_lib.Sysroot, 'IsToolchainInstalled', return_value=False) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 584 | with self.assertRaises(cros_build_lib.DieSystemExit): |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 585 | sysroot_controller.InstallPackages(in_proto, out_proto, self.api_config) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 586 | |
Alex Klein | ca572ee | 2020-09-03 10:47:14 -0600 | [diff] [blame] | 587 | def testArgumentValidationInvalidPackage(self): |
| 588 | out_proto = self._OutputProto() |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 589 | in_proto = self._InputProto( |
| 590 | build_target=self.build_target, |
| 591 | sysroot_path=self.sysroot, |
| 592 | packages=['package-1.0.0-r2']) |
Alex Klein | ca572ee | 2020-09-03 10:47:14 -0600 | [diff] [blame] | 593 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 594 | sysroot_controller.InstallPackages(in_proto, out_proto, self.api_config) |
| 595 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 596 | def testSuccessOutputHandling(self): |
| 597 | """Test successful call output handling.""" |
| 598 | # Prevent argument validation error. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 599 | self.PatchObject( |
| 600 | sysroot_lib.Sysroot, 'IsToolchainInstalled', return_value=True) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 601 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 602 | in_proto = self._InputProto( |
| 603 | build_target=self.build_target, sysroot_path=self.sysroot) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 604 | out_proto = self._OutputProto() |
| 605 | self.PatchObject(sysroot_service, 'BuildPackages') |
| 606 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 607 | rc = sysroot_controller.InstallPackages(in_proto, out_proto, |
| 608 | self.api_config) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 609 | self.assertFalse(rc) |
Lizzy Presland | 239459a | 2022-05-05 22:03:19 +0000 | [diff] [blame] | 610 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 611 | |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 612 | def testSuccessPackageIndexes(self): |
| 613 | """Test successful call with package_indexes.""" |
| 614 | # Prevent argument validation error. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 615 | self.PatchObject( |
| 616 | sysroot_lib.Sysroot, 'IsToolchainInstalled', return_value=True) |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 617 | package_indexes = [ |
| 618 | common_pb2.PackageIndexInfo( |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 619 | snapshot_sha='SHA', |
| 620 | snapshot_number=5, |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 621 | build_target=common_pb2.BuildTarget(name='board'), |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 622 | location='LOCATION', |
| 623 | profile=common_pb2.Profile(name='profile')), |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 624 | common_pb2.PackageIndexInfo( |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 625 | snapshot_sha='SHA2', |
| 626 | snapshot_number=4, |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 627 | build_target=common_pb2.BuildTarget(name='board'), |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 628 | location='LOCATION2', |
| 629 | profile=common_pb2.Profile(name='profile')) |
| 630 | ] |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 631 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 632 | in_proto = self._InputProto( |
| 633 | build_target=self.build_target, |
| 634 | sysroot_path=self.sysroot, |
| 635 | package_indexes=package_indexes) |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 636 | |
| 637 | out_proto = self._OutputProto() |
| 638 | rc_patch = self.PatchObject(sysroot_service, 'BuildPackagesRunConfig') |
| 639 | self.PatchObject(sysroot_service, 'BuildPackages') |
| 640 | |
| 641 | rc = sysroot_controller.InstallPackages(in_proto, out_proto, |
| 642 | self.api_config) |
| 643 | self.assertFalse(rc) |
Alex Klein | eb76da7 | 2021-03-19 10:43:09 -0600 | [diff] [blame] | 644 | rc_patch.assert_called_with( |
Cindy Lin | 7487daa | 2022-02-23 04:14:10 +0000 | [diff] [blame] | 645 | use_any_chrome=False, |
Alex Klein | eb76da7 | 2021-03-19 10:43:09 -0600 | [diff] [blame] | 646 | usepkg=True, |
| 647 | install_debug_symbols=True, |
| 648 | packages=[], |
| 649 | package_indexes=[ |
| 650 | binpkg.PackageIndexInfo.from_protobuf(x) for x in package_indexes |
| 651 | ], |
| 652 | use_flags=[], |
| 653 | use_goma=False, |
Joanna Wang | 1ec0c81 | 2021-11-17 17:41:27 -0800 | [diff] [blame] | 654 | use_remoteexec=False, |
Alex Klein | eb76da7 | 2021-03-19 10:43:09 -0600 | [diff] [blame] | 655 | incremental_build=False, |
Navil Perez | 5766d1b | 2021-05-26 17:38:15 +0000 | [diff] [blame] | 656 | dryrun=False) |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 657 | |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 658 | def testSuccessWithGomaLogs(self): |
| 659 | """Test successful call with goma.""" |
| 660 | self._CreateGomaLogFile(self.goma_dir, 'compiler_proxy', |
| 661 | datetime.datetime(2018, 9, 21, 12, 0, 0)) |
| 662 | self._CreateGomaLogFile(self.goma_dir, 'compiler_proxy-subproc', |
| 663 | datetime.datetime(2018, 9, 21, 12, 1, 0)) |
| 664 | self._CreateGomaLogFile(self.goma_dir, 'gomacc', |
| 665 | datetime.datetime(2018, 9, 21, 12, 2, 0)) |
| 666 | |
| 667 | # Prevent argument validation error. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 668 | self.PatchObject( |
| 669 | sysroot_lib.Sysroot, 'IsToolchainInstalled', return_value=True) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 670 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 671 | in_proto = self._InputProto( |
| 672 | build_target=self.build_target, |
| 673 | sysroot_path=self.sysroot, |
| 674 | goma_dir=self.goma_dir, |
| 675 | goma_log_dir=self.goma_out_dir) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 676 | |
| 677 | out_proto = self._OutputProto() |
| 678 | self.PatchObject(sysroot_service, 'BuildPackages') |
| 679 | |
| 680 | rc = sysroot_controller.InstallPackages(in_proto, out_proto, |
| 681 | self.api_config) |
| 682 | self.assertFalse(rc) |
Lizzy Presland | 239459a | 2022-05-05 22:03:19 +0000 | [diff] [blame] | 683 | self.assertFalse(out_proto.failed_package_data) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 684 | self.assertCountEqual(out_proto.goma_artifacts.log_files, [ |
| 685 | 'compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz', |
| 686 | 'compiler_proxy.host.log.INFO.20180921-120000.000000.gz', |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 687 | 'gomacc.host.log.INFO.20180921-120200.000000.tar.gz' |
| 688 | ]) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 689 | |
| 690 | def testSuccessWithGomaLogsAndStatsCounterzFiles(self): |
| 691 | """Test successful call with goma including stats and counterz files.""" |
| 692 | self._CreateGomaLogFile(self.goma_dir, 'compiler_proxy', |
| 693 | datetime.datetime(2018, 9, 21, 12, 0, 0)) |
| 694 | self._CreateGomaLogFile(self.goma_dir, 'compiler_proxy-subproc', |
| 695 | datetime.datetime(2018, 9, 21, 12, 1, 0)) |
| 696 | self._CreateGomaLogFile(self.goma_dir, 'gomacc', |
| 697 | datetime.datetime(2018, 9, 21, 12, 2, 0)) |
| 698 | # Create stats and counterz files. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 699 | osutils.WriteFile( |
| 700 | os.path.join(self.goma_dir, 'stats.binaryproto'), |
| 701 | 'File: stats.binaryproto') |
| 702 | osutils.WriteFile( |
| 703 | os.path.join(self.goma_dir, 'counterz.binaryproto'), |
| 704 | 'File: counterz.binaryproto') |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 705 | |
| 706 | # Prevent argument validation error. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 707 | self.PatchObject( |
| 708 | sysroot_lib.Sysroot, 'IsToolchainInstalled', return_value=True) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 709 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 710 | in_proto = self._InputProto( |
| 711 | build_target=self.build_target, |
| 712 | sysroot_path=self.sysroot, |
| 713 | goma_dir=self.goma_dir, |
| 714 | goma_log_dir=self.goma_out_dir, |
| 715 | goma_stats_file='stats.binaryproto', |
| 716 | goma_counterz_file='counterz.binaryproto') |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 717 | |
| 718 | out_proto = self._OutputProto() |
| 719 | self.PatchObject(sysroot_service, 'BuildPackages') |
| 720 | |
| 721 | rc = sysroot_controller.InstallPackages(in_proto, out_proto, |
| 722 | self.api_config) |
| 723 | self.assertFalse(rc) |
Lizzy Presland | 239459a | 2022-05-05 22:03:19 +0000 | [diff] [blame] | 724 | self.assertFalse(out_proto.failed_package_data) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 725 | self.assertCountEqual(out_proto.goma_artifacts.log_files, [ |
| 726 | 'compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz', |
| 727 | 'compiler_proxy.host.log.INFO.20180921-120000.000000.gz', |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 728 | 'gomacc.host.log.INFO.20180921-120200.000000.tar.gz' |
| 729 | ]) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 730 | # Verify that the output dir has 5 files -- since there should be 3 log |
| 731 | # files, the stats file, and the counterz file. |
| 732 | output_files = os.listdir(self.goma_out_dir) |
| 733 | self.assertCountEqual(output_files, [ |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 734 | 'stats.binaryproto', 'counterz.binaryproto', |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 735 | 'compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz', |
| 736 | 'compiler_proxy.host.log.INFO.20180921-120000.000000.gz', |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 737 | 'gomacc.host.log.INFO.20180921-120200.000000.tar.gz' |
| 738 | ]) |
Michael Mortensen | 1c7439c | 2020-01-24 14:43:19 -0700 | [diff] [blame] | 739 | self.assertEqual(out_proto.goma_artifacts.counterz_file, |
| 740 | 'counterz.binaryproto') |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 741 | self.assertEqual(out_proto.goma_artifacts.stats_file, 'stats.binaryproto') |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 742 | |
| 743 | def testFailureMissingGomaStatsCounterzFiles(self): |
| 744 | """Test successful call with goma including stats and counterz files.""" |
| 745 | self._CreateGomaLogFile(self.goma_dir, 'compiler_proxy', |
| 746 | datetime.datetime(2018, 9, 21, 12, 0, 0)) |
| 747 | self._CreateGomaLogFile(self.goma_dir, 'compiler_proxy-subproc', |
| 748 | datetime.datetime(2018, 9, 21, 12, 1, 0)) |
| 749 | self._CreateGomaLogFile(self.goma_dir, 'gomacc', |
| 750 | datetime.datetime(2018, 9, 21, 12, 2, 0)) |
| 751 | # Note that stats and counterz files are not created, but are specified in |
| 752 | # the proto below. |
| 753 | |
| 754 | # Prevent argument validation error. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 755 | self.PatchObject( |
| 756 | sysroot_lib.Sysroot, 'IsToolchainInstalled', return_value=True) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 757 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 758 | in_proto = self._InputProto( |
| 759 | build_target=self.build_target, |
| 760 | sysroot_path=self.sysroot, |
| 761 | goma_dir=self.goma_dir, |
| 762 | goma_log_dir=self.goma_out_dir, |
| 763 | goma_stats_file='stats.binaryproto', |
| 764 | goma_counterz_file='counterz.binaryproto') |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 765 | |
| 766 | out_proto = self._OutputProto() |
| 767 | self.PatchObject(sysroot_service, 'BuildPackages') |
| 768 | |
Michael Mortensen | 1d6d5b0 | 2020-01-22 07:33:50 -0700 | [diff] [blame] | 769 | rc = sysroot_controller.InstallPackages(in_proto, out_proto, |
| 770 | self.api_config) |
| 771 | self.assertFalse(rc) |
Lizzy Presland | 239459a | 2022-05-05 22:03:19 +0000 | [diff] [blame] | 772 | self.assertFalse(out_proto.failed_package_data) |
Michael Mortensen | 1d6d5b0 | 2020-01-22 07:33:50 -0700 | [diff] [blame] | 773 | self.assertCountEqual(out_proto.goma_artifacts.log_files, [ |
| 774 | 'compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz', |
| 775 | 'compiler_proxy.host.log.INFO.20180921-120000.000000.gz', |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 776 | 'gomacc.host.log.INFO.20180921-120200.000000.tar.gz' |
| 777 | ]) |
Michael Mortensen | 1d6d5b0 | 2020-01-22 07:33:50 -0700 | [diff] [blame] | 778 | self.assertFalse(out_proto.goma_artifacts.counterz_file) |
| 779 | self.assertFalse(out_proto.goma_artifacts.stats_file) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 780 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 781 | def testFailureOutputHandling(self): |
| 782 | """Test failed package handling.""" |
| 783 | # Prevent argument validation error. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 784 | self.PatchObject( |
| 785 | sysroot_lib.Sysroot, 'IsToolchainInstalled', return_value=True) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 786 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 787 | in_proto = self._InputProto( |
| 788 | build_target=self.build_target, sysroot_path=self.sysroot) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 789 | out_proto = self._OutputProto() |
| 790 | |
| 791 | # Failed package info and expected list for verification. |
Lizzy Presland | 7774178 | 2021-12-13 19:46:42 +0000 | [diff] [blame] | 792 | err_pkgs = ['cat/pkg-1.0-r3', 'cat2/pkg2-1.0-r1'] |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 793 | err_cpvs = [package_info.parse(cpv) for cpv in err_pkgs] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 794 | expected = [('cat', 'pkg'), ('cat2', 'pkg2')] |
| 795 | |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 796 | new_logs = {} |
| 797 | for i, pkg in enumerate(err_pkgs): |
Lizzy Presland | 4c27983 | 2021-11-19 20:27:43 +0000 | [diff] [blame] | 798 | self._CreatePortageLogFile(self.portage_dir, err_cpvs[i], |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 799 | datetime.datetime(2021, 6, 9, 13, 37, 0)) |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 800 | new_logs[pkg] = self._CreatePortageLogFile( |
| 801 | self.portage_dir, err_cpvs[i], |
| 802 | datetime.datetime(2021, 6, 9, 16, 20, 0)) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 803 | # Force error to be raised with the packages. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 804 | error = sysroot_lib.PackageInstallError( |
| 805 | 'Error', cros_build_lib.CommandResult(), packages=err_cpvs) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 806 | self.PatchObject(sysroot_service, 'BuildPackages', side_effect=error) |
| 807 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 808 | rc = sysroot_controller.InstallPackages(in_proto, out_proto, |
| 809 | self.api_config) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 810 | # This needs to return 2 to indicate the available error response. |
| 811 | self.assertEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 812 | for data in out_proto.failed_package_data: |
| 813 | package = controller_util.deserialize_package_info(data.name) |
| 814 | cat_pkg = (data.name.category, data.name.package_name) |
| 815 | self.assertIn(cat_pkg, expected) |
Lizzy Presland | 7774178 | 2021-12-13 19:46:42 +0000 | [diff] [blame] | 816 | self.assertEqual(data.log_path.path, new_logs[package.cpvr]) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 817 | |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 818 | def testNoPackageFailureOutputHandling(self): |
| 819 | """Test failure handling without packages to report.""" |
| 820 | # Prevent argument validation error. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 821 | self.PatchObject( |
| 822 | sysroot_lib.Sysroot, 'IsToolchainInstalled', return_value=True) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 823 | |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 824 | in_proto = self._InputProto( |
| 825 | build_target=self.build_target, sysroot_path=self.sysroot) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 826 | out_proto = self._OutputProto() |
| 827 | |
| 828 | # Force error to be raised with no packages. |
Alex Klein | d066b0f | 2022-07-28 08:47:35 -0600 | [diff] [blame^] | 829 | error = sysroot_lib.PackageInstallError( |
| 830 | 'Error', cros_build_lib.CommandResult(), packages=[]) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 831 | self.PatchObject(sysroot_service, 'BuildPackages', side_effect=error) |
| 832 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 833 | rc = sysroot_controller.InstallPackages(in_proto, out_proto, |
| 834 | self.api_config) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 835 | # All we really care about is it's not 0 or 2 (response available), so |
| 836 | # test for that rather than a specific return code. |
| 837 | self.assertTrue(rc) |
| 838 | self.assertNotEqual(controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, |
| 839 | rc) |