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