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