Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 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 |
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 | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 26 | """Create function tests.""" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 27 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | def _InputProto( |
| 29 | self, |
| 30 | build_target=None, |
| 31 | profile=None, |
| 32 | replace=False, |
| 33 | current=False, |
| 34 | package_indexes=None, |
| 35 | ): |
| 36 | """Helper to build and input proto instance.""" |
| 37 | proto = sysroot_pb2.SysrootCreateRequest() |
| 38 | if build_target: |
| 39 | proto.build_target.name = build_target |
| 40 | if profile: |
| 41 | proto.profile.name = profile |
| 42 | if replace: |
| 43 | proto.flags.replace = replace |
| 44 | if current: |
| 45 | proto.flags.chroot_current = current |
| 46 | if package_indexes: |
| 47 | proto.package_indexes.extend(package_indexes) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 48 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 49 | return proto |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 50 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | def _OutputProto(self): |
| 52 | """Helper to build output proto instance.""" |
| 53 | return sysroot_pb2.SysrootCreateResponse() |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 54 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 56 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | patch = self.PatchObject(sysroot_service, "Create") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 58 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | board = "board" |
| 60 | profile = None |
| 61 | force = False |
| 62 | upgrade_chroot = True |
| 63 | in_proto = self._InputProto( |
| 64 | build_target=board, |
| 65 | profile=profile, |
| 66 | replace=force, |
| 67 | current=not upgrade_chroot, |
| 68 | ) |
| 69 | sysroot_controller.Create( |
| 70 | in_proto, self._OutputProto(), self.validate_only_config |
| 71 | ) |
| 72 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 75 | """Verify a mock call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | patch = self.PatchObject(sysroot_service, "Create") |
| 77 | request = self._InputProto() |
| 78 | response = self._OutputProto() |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 79 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 80 | rc = sysroot_controller.Create(request, response, self.mock_call_config) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 81 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | patch.assert_not_called() |
| 83 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 84 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 85 | def testMockError(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 86 | """Verify a mock error does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | patch = self.PatchObject(sysroot_service, "Create") |
| 88 | request = self._InputProto() |
| 89 | response = self._OutputProto() |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 90 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 91 | rc = sysroot_controller.Create( |
| 92 | request, response, self.mock_error_config |
| 93 | ) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 94 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | patch.assert_not_called() |
| 96 | self.assertEqual(controller.RETURN_CODE_UNRECOVERABLE, rc) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 97 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 98 | def testArgumentValidation(self): |
| 99 | """Test the input argument validation.""" |
| 100 | # Error when no name provided. |
| 101 | in_proto = self._InputProto() |
| 102 | out_proto = self._OutputProto() |
| 103 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 104 | sysroot_controller.Create(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 105 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | # Valid when board passed. |
| 107 | result = sysroot_lib.Sysroot("/sysroot/path") |
| 108 | patch = self.PatchObject(sysroot_service, "Create", return_value=result) |
| 109 | in_proto = self._InputProto("board") |
| 110 | out_proto = self._OutputProto() |
| 111 | sysroot_controller.Create(in_proto, out_proto, self.api_config) |
| 112 | patch.assert_called_once() |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | def testArgumentHandling(self): |
| 115 | """Test the arguments get processed and passed correctly.""" |
| 116 | sysroot_path = "/sysroot/path" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 119 | create_patch = self.PatchObject( |
| 120 | sysroot_service, "Create", return_value=sysroot |
| 121 | ) |
| 122 | rc_patch = self.PatchObject(sysroot_service, "SetupBoardRunConfig") |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 123 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 124 | # Default values. |
| 125 | board = "board" |
| 126 | profile = None |
| 127 | force = False |
| 128 | upgrade_chroot = True |
| 129 | in_proto = self._InputProto( |
| 130 | build_target=board, |
| 131 | profile=profile, |
| 132 | replace=force, |
| 133 | current=not upgrade_chroot, |
| 134 | ) |
| 135 | out_proto = self._OutputProto() |
| 136 | sysroot_controller.Create(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 137 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 138 | # Default value checks. |
| 139 | rc_patch.assert_called_with( |
| 140 | force=force, |
| 141 | upgrade_chroot=upgrade_chroot, |
| 142 | package_indexes=[], |
| 143 | backtrack=sysroot_controller.DEFAULT_BACKTRACK, |
| 144 | ) |
| 145 | self.assertEqual(board, out_proto.sysroot.build_target.name) |
| 146 | self.assertEqual(sysroot_path, out_proto.sysroot.path) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 147 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 148 | # Not default values. |
| 149 | create_patch.reset_mock() |
| 150 | board = "board" |
| 151 | profile = "profile" |
| 152 | force = True |
| 153 | upgrade_chroot = False |
| 154 | package_indexes = [ |
| 155 | common_pb2.PackageIndexInfo( |
| 156 | snapshot_sha="SHA", |
| 157 | snapshot_number=5, |
| 158 | build_target=common_pb2.BuildTarget(name=board), |
| 159 | location="LOCATION", |
| 160 | profile=common_pb2.Profile(name=profile), |
| 161 | ), |
| 162 | common_pb2.PackageIndexInfo( |
| 163 | snapshot_sha="SHA2", |
| 164 | snapshot_number=4, |
| 165 | build_target=common_pb2.BuildTarget(name=board), |
| 166 | location="LOCATION2", |
| 167 | profile=common_pb2.Profile(name=profile), |
| 168 | ), |
| 169 | ] |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 170 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 171 | in_proto = self._InputProto( |
| 172 | build_target=board, |
| 173 | profile=profile, |
| 174 | replace=force, |
| 175 | current=not upgrade_chroot, |
| 176 | package_indexes=package_indexes, |
| 177 | ) |
| 178 | out_proto = self._OutputProto() |
| 179 | sysroot_controller.Create(in_proto, out_proto, self.api_config) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 180 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 181 | # Not default value checks. |
| 182 | rc_patch.assert_called_with( |
| 183 | force=force, |
| 184 | package_indexes=[ |
Alex Klein | 6d718d6 | 2023-01-18 15:55:51 -0700 | [diff] [blame] | 185 | controller_util.deserialize_package_index_info(x) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | for x in package_indexes |
| 187 | ], |
| 188 | upgrade_chroot=upgrade_chroot, |
| 189 | backtrack=sysroot_controller.DEFAULT_BACKTRACK, |
| 190 | ) |
| 191 | self.assertEqual(board, out_proto.sysroot.build_target.name) |
| 192 | self.assertEqual(sysroot_path, out_proto.sysroot.path) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 193 | |
| 194 | |
Jack Neus | 26b9467 | 2022-10-27 17:33:21 +0000 | [diff] [blame] | 195 | class GetArtifactsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
| 196 | """GetArtifacts function tests.""" |
| 197 | |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 198 | # pylint: disable=line-too-long |
Jack Neus | 26b9467 | 2022-10-27 17:33:21 +0000 | [diff] [blame] | 199 | _artifact_funcs = { |
| 200 | common_pb2.ArtifactsByService.Sysroot.ArtifactType.SIMPLE_CHROME_SYSROOT: sysroot_service.CreateSimpleChromeSysroot, |
| 201 | common_pb2.ArtifactsByService.Sysroot.ArtifactType.CHROME_EBUILD_ENV: sysroot_service.CreateChromeEbuildEnv, |
| 202 | common_pb2.ArtifactsByService.Sysroot.ArtifactType.BREAKPAD_DEBUG_SYMBOLS: sysroot_service.BundleBreakpadSymbols, |
| 203 | common_pb2.ArtifactsByService.Sysroot.ArtifactType.DEBUG_SYMBOLS: sysroot_service.BundleDebugSymbols, |
| 204 | common_pb2.ArtifactsByService.Sysroot.ArtifactType.FUZZER_SYSROOT: sysroot_service.CreateFuzzerSysroot, |
| 205 | } |
| 206 | |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 207 | # pylint: enable=line-too-long |
| 208 | |
Jack Neus | 26b9467 | 2022-10-27 17:33:21 +0000 | [diff] [blame] | 209 | def setUp(self): |
| 210 | self._mocks = {} |
| 211 | for artifact, func in self._artifact_funcs.items(): |
| 212 | self._mocks[artifact] = self.PatchObject( |
| 213 | sysroot_service, func.__name__ |
| 214 | ) |
| 215 | |
| 216 | def _InputProto( |
| 217 | self, |
| 218 | artifact_types=_artifact_funcs.keys(), |
| 219 | ): |
| 220 | """Helper to build an input proto instance.""" |
| 221 | return common_pb2.ArtifactsByService.Sysroot( |
| 222 | output_artifacts=[ |
| 223 | common_pb2.ArtifactsByService.Sysroot.ArtifactInfo( |
| 224 | artifact_types=artifact_types |
| 225 | ) |
| 226 | ] |
| 227 | ) |
| 228 | |
| 229 | def testNoArtifacts(self): |
| 230 | """Test GetArtifacts with no artifact types.""" |
| 231 | in_proto = self._InputProto(artifact_types=[]) |
| 232 | sysroot_controller.GetArtifacts( |
| 233 | in_proto, None, None, "build_target", "" |
| 234 | ) |
| 235 | |
| 236 | for _, patch in self._mocks.items(): |
| 237 | patch.assert_not_called() |
| 238 | |
| 239 | def testArtifactsSuccess(self): |
| 240 | """Test GetArtifacts with all artifact types.""" |
| 241 | sysroot_controller.GetArtifacts( |
| 242 | self._InputProto(), None, None, "build_target", "" |
| 243 | ) |
| 244 | |
| 245 | for _, patch in self._mocks.items(): |
| 246 | patch.assert_called_once() |
| 247 | |
| 248 | def testArtifactsException(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 249 | """Test with all artifact types when one type throws an exception.""" |
Jack Neus | 26b9467 | 2022-10-27 17:33:21 +0000 | [diff] [blame] | 250 | |
| 251 | self._mocks[ |
| 252 | common_pb2.ArtifactsByService.Sysroot.ArtifactType.FUZZER_SYSROOT |
| 253 | ].side_effect = Exception("foo bar") |
| 254 | generated = sysroot_controller.GetArtifacts( |
| 255 | self._InputProto(), None, None, "build_target", "" |
| 256 | ) |
| 257 | |
| 258 | for _, patch in self._mocks.items(): |
| 259 | patch.assert_called_once() |
| 260 | |
| 261 | found_artifact = False |
| 262 | for data in generated: |
| 263 | artifact_type = ( |
| 264 | common_pb2.ArtifactsByService.Sysroot.ArtifactType.Name( |
| 265 | data["type"] |
| 266 | ) |
| 267 | ) |
| 268 | if artifact_type == "FUZZER_SYSROOT": |
| 269 | found_artifact = True |
| 270 | self.assertTrue(data["failed"]) |
| 271 | self.assertEqual(data["failure_reason"], "foo bar") |
| 272 | self.assertTrue(found_artifact) |
| 273 | |
| 274 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 275 | class GenerateArchiveTest( |
| 276 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 277 | ): |
| 278 | """GenerateArchive function tests.""" |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 279 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 280 | def setUp(self): |
| 281 | self.chroot_path = "/path/to/chroot" |
| 282 | self.board = "board" |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 283 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 284 | def _InputProto(self, build_target=None, chroot_path=None, pkg_list=None): |
| 285 | """Helper to build and input proto instance.""" |
| 286 | # pkg_list will be a list of category/package strings such as |
| 287 | # ['virtual/target-fuzzers']. |
| 288 | if pkg_list: |
| 289 | package_list = [] |
| 290 | for pkg in pkg_list: |
| 291 | pkg_string_parts = pkg.split("/") |
| 292 | package_info_msg = common_pb2.PackageInfo( |
| 293 | category=pkg_string_parts[0], |
| 294 | package_name=pkg_string_parts[1], |
| 295 | ) |
| 296 | package_list.append(package_info_msg) |
| 297 | else: |
| 298 | package_list = [] |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 299 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 300 | return sysroot_pb2.SysrootGenerateArchiveRequest( |
| 301 | build_target={"name": build_target}, |
| 302 | chroot={"path": chroot_path}, |
| 303 | packages=package_list, |
| 304 | ) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 305 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 306 | def _OutputProto(self): |
| 307 | """Helper to build output proto instance.""" |
| 308 | return sysroot_pb2.SysrootGenerateArchiveResponse() |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 309 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 310 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 311 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 312 | patch = self.PatchObject(sysroot_service, "GenerateArchive") |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 313 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 314 | in_proto = self._InputProto( |
| 315 | build_target=self.board, |
| 316 | chroot_path=self.chroot_path, |
| 317 | pkg_list=["virtual/target-fuzzers"], |
| 318 | ) |
| 319 | sysroot_controller.GenerateArchive( |
| 320 | in_proto, self._OutputProto(), self.validate_only_config |
| 321 | ) |
| 322 | patch.assert_not_called() |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 323 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 324 | def testMockCall(self): |
| 325 | """Sanity check that a mock call does not execute any logic.""" |
| 326 | patch = self.PatchObject(sysroot_service, "GenerateArchive") |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 327 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | in_proto = self._InputProto( |
| 329 | build_target=self.board, |
| 330 | chroot_path=self.chroot_path, |
| 331 | pkg_list=["virtual/target-fuzzers"], |
| 332 | ) |
| 333 | sysroot_controller.GenerateArchive( |
| 334 | in_proto, self._OutputProto(), self.mock_call_config |
| 335 | ) |
| 336 | patch.assert_not_called() |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 337 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 338 | def testArgumentValidation(self): |
| 339 | """Test the input argument validation.""" |
| 340 | # Error when no build target provided. |
| 341 | in_proto = self._InputProto() |
| 342 | out_proto = self._OutputProto() |
| 343 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 344 | sysroot_controller.GenerateArchive( |
| 345 | in_proto, out_proto, self.api_config |
| 346 | ) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 347 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 348 | # Error when packages is not specified. |
| 349 | in_proto = self._InputProto( |
| 350 | build_target="board", chroot_path=self.chroot_path |
| 351 | ) |
| 352 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 353 | sysroot_controller.GenerateArchive( |
| 354 | in_proto, out_proto, self.api_config |
| 355 | ) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 356 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 357 | # Valid when board, chroot path, and package are specified. |
| 358 | patch = self.PatchObject( |
| 359 | sysroot_service, |
| 360 | "GenerateArchive", |
| 361 | return_value="/path/to/sysroot/tar.bz", |
| 362 | ) |
| 363 | in_proto = self._InputProto( |
| 364 | build_target="board", |
| 365 | chroot_path=self.chroot_path, |
| 366 | pkg_list=["virtual/target-fuzzers"], |
| 367 | ) |
| 368 | out_proto = self._OutputProto() |
| 369 | sysroot_controller.GenerateArchive(in_proto, out_proto, self.api_config) |
| 370 | patch.assert_called_once() |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 371 | |
| 372 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 373 | class InstallToolchainTest( |
| 374 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 375 | ): |
| 376 | """Install toolchain function tests.""" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 377 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 378 | def setUp(self): |
| 379 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True) |
| 380 | # Avoid running the portageq command. |
| 381 | self.PatchObject(sysroot_controller, "_LogBinhost") |
| 382 | self.board = "board" |
| 383 | self.sysroot = os.path.join(self.tempdir, "board") |
| 384 | self.invalid_sysroot = os.path.join(self.tempdir, "invalid", "sysroot") |
| 385 | osutils.SafeMakedirs(self.sysroot) |
| 386 | # Set up portage log directory. |
| 387 | self.target_sysroot = sysroot_lib.Sysroot(self.sysroot) |
| 388 | self.portage_dir = os.path.join(self.tempdir, "portage_logdir") |
| 389 | self.PatchObject( |
| 390 | sysroot_lib.Sysroot, "portage_logdir", new=self.portage_dir |
| 391 | ) |
| 392 | osutils.SafeMakedirs(self.portage_dir) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 393 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 394 | def _InputProto( |
| 395 | self, build_target=None, sysroot_path=None, compile_source=False |
| 396 | ): |
| 397 | """Helper to build an input proto instance.""" |
| 398 | proto = sysroot_pb2.InstallToolchainRequest() |
| 399 | if build_target: |
| 400 | proto.sysroot.build_target.name = build_target |
| 401 | if sysroot_path: |
| 402 | proto.sysroot.path = sysroot_path |
| 403 | if compile_source: |
| 404 | proto.flags.compile_source = compile_source |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 405 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 406 | return proto |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 407 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 408 | def _OutputProto(self): |
| 409 | """Helper to build output proto instance.""" |
| 410 | return sysroot_pb2.InstallToolchainResponse() |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 411 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 412 | def _CreatePortageLogFile( |
| 413 | self, |
| 414 | log_path: Union[str, os.PathLike], |
| 415 | pkg_info: package_info.PackageInfo, |
| 416 | timestamp: datetime.datetime, |
| 417 | ): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 418 | """Creates a log file to test for individual packages built by Portage. |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 419 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 421 | log_path: The PORTAGE_LOGDIR path. |
| 422 | pkg_info: Package name used to name the log file. |
| 423 | timestamp: Timestamp used to name the file. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 424 | """ |
| 425 | path = os.path.join( |
| 426 | log_path, |
| 427 | f"{pkg_info.category}:{pkg_info.pvr}:" |
| 428 | f'{timestamp.strftime("%Y%m%d-%H%M%S")}.log', |
| 429 | ) |
| 430 | osutils.WriteFile( |
| 431 | path, |
| 432 | f"Test log file for package {pkg_info.category}/" |
| 433 | f"{pkg_info.package} written to {path}", |
| 434 | ) |
| 435 | return path |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 436 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 437 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 438 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 439 | patch = self.PatchObject(sysroot_service, "InstallToolchain") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 440 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 441 | in_proto = self._InputProto( |
| 442 | build_target=self.board, sysroot_path=self.sysroot |
| 443 | ) |
| 444 | sysroot_controller.InstallToolchain( |
| 445 | in_proto, self._OutputProto(), self.validate_only_config |
| 446 | ) |
| 447 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 448 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 449 | def testMockCall(self): |
| 450 | """Sanity check that a mock call does not execute any logic.""" |
| 451 | patch = self.PatchObject(sysroot_service, "InstallToolchain") |
| 452 | request = self._InputProto() |
| 453 | response = self._OutputProto() |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 454 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 455 | rc = sysroot_controller.InstallToolchain( |
| 456 | request, response, self.mock_call_config |
| 457 | ) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 458 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 459 | patch.assert_not_called() |
| 460 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 461 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 462 | def testMockError(self): |
| 463 | """Sanity check that a mock error does not execute any logic.""" |
| 464 | patch = self.PatchObject(sysroot_service, "InstallToolchain") |
| 465 | request = self._InputProto() |
| 466 | response = self._OutputProto() |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 467 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | rc = sysroot_controller.InstallToolchain( |
| 469 | request, response, self.mock_error_config |
| 470 | ) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 471 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 472 | patch.assert_not_called() |
| 473 | self.assertEqual( |
| 474 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 475 | ) |
| 476 | self.assertTrue(response.failed_package_data) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 477 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 478 | def testArgumentValidation(self): |
| 479 | """Test the argument validation.""" |
| 480 | # Test errors on missing inputs. |
| 481 | out_proto = self._OutputProto() |
| 482 | # Both missing. |
| 483 | in_proto = self._InputProto() |
| 484 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 485 | sysroot_controller.InstallToolchain( |
| 486 | in_proto, out_proto, self.api_config |
| 487 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 488 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 489 | # Sysroot path missing. |
| 490 | in_proto = self._InputProto(build_target=self.board) |
| 491 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 492 | sysroot_controller.InstallToolchain( |
| 493 | in_proto, out_proto, self.api_config |
| 494 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 495 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 496 | # Build target name missing. |
| 497 | in_proto = self._InputProto(sysroot_path=self.sysroot) |
| 498 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 499 | sysroot_controller.InstallToolchain( |
| 500 | in_proto, out_proto, self.api_config |
| 501 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 502 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 503 | # Both provided, but invalid sysroot path. |
| 504 | in_proto = self._InputProto( |
| 505 | build_target=self.board, sysroot_path=self.invalid_sysroot |
| 506 | ) |
| 507 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 508 | sysroot_controller.InstallToolchain( |
| 509 | in_proto, out_proto, self.api_config |
| 510 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 511 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 512 | def testSuccessOutputHandling(self): |
| 513 | """Test the output is processed and recorded correctly.""" |
| 514 | self.PatchObject(sysroot_service, "InstallToolchain") |
| 515 | out_proto = self._OutputProto() |
| 516 | in_proto = self._InputProto( |
| 517 | build_target=self.board, sysroot_path=self.sysroot |
| 518 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 519 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 520 | rc = sysroot_controller.InstallToolchain( |
| 521 | in_proto, out_proto, self.api_config |
| 522 | ) |
| 523 | self.assertFalse(rc) |
| 524 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 525 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 526 | def testErrorOutputHandling(self): |
| 527 | """Test the error output is processed and recorded correctly.""" |
| 528 | out_proto = self._OutputProto() |
| 529 | in_proto = self._InputProto( |
| 530 | build_target=self.board, sysroot_path=self.sysroot |
| 531 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 532 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 533 | err_pkgs = ["cat/pkg-1.0-r1", "cat2/pkg2-1.0-r1"] |
| 534 | err_cpvs = [package_info.parse(pkg) for pkg in err_pkgs] |
| 535 | expected = [("cat", "pkg"), ("cat2", "pkg2")] |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 536 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 537 | new_logs = {} |
| 538 | for i, pkg in enumerate(err_pkgs): |
| 539 | self._CreatePortageLogFile( |
| 540 | self.portage_dir, |
| 541 | err_cpvs[i], |
| 542 | datetime.datetime(2021, 6, 9, 13, 37, 0), |
| 543 | ) |
| 544 | new_logs[pkg] = self._CreatePortageLogFile( |
| 545 | self.portage_dir, |
| 546 | err_cpvs[i], |
| 547 | datetime.datetime(2021, 6, 9, 16, 20, 0), |
| 548 | ) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 549 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 550 | err = sysroot_lib.ToolchainInstallError( |
| 551 | "Error", cros_build_lib.CompletedProcess(), tc_info=err_cpvs |
| 552 | ) |
| 553 | self.PatchObject(sysroot_service, "InstallToolchain", side_effect=err) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 554 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 555 | rc = sysroot_controller.InstallToolchain( |
| 556 | in_proto, out_proto, self.api_config |
| 557 | ) |
| 558 | self.assertEqual( |
| 559 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 560 | ) |
| 561 | self.assertTrue(out_proto.failed_package_data) |
| 562 | # This needs to return 2 to indicate the available error response. |
| 563 | self.assertEqual( |
| 564 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 565 | ) |
| 566 | for data in out_proto.failed_package_data: |
| 567 | package = controller_util.deserialize_package_info(data.name) |
| 568 | cat_pkg = (data.name.category, data.name.package_name) |
| 569 | self.assertIn(cat_pkg, expected) |
| 570 | self.assertEqual(data.log_path.path, new_logs[package.cpvr]) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 571 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 572 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 573 | class InstallPackagesTest( |
| 574 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 575 | ): |
| 576 | """InstallPackages tests.""" |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 577 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 578 | def setUp(self): |
| 579 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True) |
| 580 | # Avoid running the portageq command. |
| 581 | self.PatchObject(sysroot_controller, "_LogBinhost") |
| 582 | self.build_target = "board" |
| 583 | self.sysroot = os.path.join(self.tempdir, "build", "board") |
| 584 | osutils.SafeMakedirs(self.sysroot) |
| 585 | # Set up portage log directory. |
| 586 | self.target_sysroot = sysroot_lib.Sysroot(self.sysroot) |
| 587 | self.portage_dir = os.path.join(self.tempdir, "portage_logdir") |
| 588 | self.PatchObject( |
| 589 | sysroot_lib.Sysroot, "portage_logdir", new=self.portage_dir |
| 590 | ) |
| 591 | osutils.SafeMakedirs(self.portage_dir) |
| 592 | # Set up goma directories. |
| 593 | self.goma_dir = os.path.join(self.tempdir, "goma_dir") |
| 594 | osutils.SafeMakedirs(self.goma_dir) |
| 595 | self.goma_out_dir = os.path.join(self.tempdir, "goma_out_dir") |
| 596 | osutils.SafeMakedirs(self.goma_out_dir) |
| 597 | os.environ["GLOG_log_dir"] = self.goma_dir |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 598 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 599 | def _InputProto( |
| 600 | self, |
| 601 | build_target=None, |
| 602 | sysroot_path=None, |
| 603 | build_source=False, |
| 604 | goma_dir=None, |
| 605 | goma_log_dir=None, |
| 606 | goma_stats_file=None, |
| 607 | goma_counterz_file=None, |
| 608 | package_indexes=None, |
| 609 | packages=None, |
| 610 | ): |
| 611 | """Helper to build an input proto instance.""" |
| 612 | instance = sysroot_pb2.InstallPackagesRequest() |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 613 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 614 | if build_target: |
| 615 | instance.sysroot.build_target.name = build_target |
| 616 | if sysroot_path: |
| 617 | instance.sysroot.path = sysroot_path |
| 618 | if build_source: |
| 619 | instance.flags.build_source = build_source |
| 620 | if goma_dir: |
| 621 | instance.goma_config.goma_dir = goma_dir |
| 622 | if goma_log_dir: |
| 623 | instance.goma_config.log_dir.dir = goma_log_dir |
| 624 | if goma_stats_file: |
| 625 | instance.goma_config.stats_file = goma_stats_file |
| 626 | if goma_counterz_file: |
| 627 | instance.goma_config.counterz_file = goma_counterz_file |
| 628 | if package_indexes: |
| 629 | instance.package_indexes.extend(package_indexes) |
| 630 | if packages: |
| 631 | for pkg in packages: |
| 632 | pkg_info = package_info.parse(pkg) |
| 633 | pkg_info_msg = instance.packages.add() |
| 634 | controller_util.serialize_package_info(pkg_info, pkg_info_msg) |
| 635 | return instance |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 636 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 637 | def _OutputProto(self): |
| 638 | """Helper to build an empty output proto instance.""" |
| 639 | return sysroot_pb2.InstallPackagesResponse() |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 640 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 641 | def _CreateGomaLogFile( |
| 642 | self, |
| 643 | goma_log_dir: Union[str, os.PathLike], |
| 644 | name: str, |
| 645 | timestamp: datetime.datetime, |
| 646 | ): |
| 647 | """Creates a log file for testing. |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 648 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 649 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 650 | goma_log_dir: Directory where the file will be created. |
| 651 | name: Log file 'base' name that is combined with the timestamp. |
| 652 | timestamp: Timestamp that is written to the file. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 653 | """ |
| 654 | path = os.path.join( |
| 655 | goma_log_dir, |
| 656 | "%s.host.log.INFO.%s" |
| 657 | % (name, timestamp.strftime("%Y%m%d-%H%M%S.%f")), |
| 658 | ) |
| 659 | osutils.WriteFile( |
| 660 | path, |
| 661 | timestamp.strftime("Goma log file created at: %Y/%m/%d %H:%M:%S"), |
| 662 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 663 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 664 | def _CreatePortageLogFile( |
| 665 | self, |
| 666 | log_path: Union[str, os.PathLike], |
| 667 | pkg_info: package_info.PackageInfo, |
| 668 | timestamp: datetime.datetime, |
| 669 | ): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 670 | """Creates a log file to test for individual packages built by Portage. |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 671 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 672 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 673 | log_path: The PORTAGE_LOGDIR path. |
| 674 | pkg_info: Package name used to name the log file. |
| 675 | timestamp: Timestamp used to name the file. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 676 | """ |
| 677 | path = os.path.join( |
| 678 | log_path, |
| 679 | f"{pkg_info.category}:{pkg_info.pvr}:" |
| 680 | f'{timestamp.strftime("%Y%m%d-%H%M%S")}.log', |
| 681 | ) |
| 682 | osutils.WriteFile( |
| 683 | path, |
| 684 | f"Test log file for package {pkg_info.category}/" |
| 685 | f"{pkg_info.package} written to {path}", |
| 686 | ) |
| 687 | return path |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 688 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 689 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 690 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 691 | patch = self.PatchObject(sysroot_service, "BuildPackages") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 692 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 693 | in_proto = self._InputProto( |
| 694 | build_target=self.build_target, sysroot_path=self.sysroot |
| 695 | ) |
| 696 | sysroot_controller.InstallPackages( |
| 697 | in_proto, self._OutputProto(), self.validate_only_config |
| 698 | ) |
| 699 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 700 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 701 | def testMockCall(self): |
| 702 | """Sanity check that a mock call does not execute any logic.""" |
| 703 | patch = self.PatchObject(sysroot_service, "BuildPackages") |
| 704 | request = self._InputProto() |
| 705 | response = self._OutputProto() |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 706 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 707 | rc = sysroot_controller.InstallPackages( |
| 708 | request, response, self.mock_call_config |
| 709 | ) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 710 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 711 | patch.assert_not_called() |
| 712 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 713 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 714 | def testMockError(self): |
| 715 | """Sanity check that a mock error does not execute any logic.""" |
| 716 | patch = self.PatchObject(sysroot_service, "BuildPackages") |
| 717 | request = self._InputProto() |
| 718 | response = self._OutputProto() |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 719 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 720 | rc = sysroot_controller.InstallPackages( |
| 721 | request, response, self.mock_error_config |
| 722 | ) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 723 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 724 | patch.assert_not_called() |
| 725 | self.assertEqual( |
| 726 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 727 | ) |
| 728 | self.assertTrue(response.failed_package_data) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 729 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 730 | def testArgumentValidationAllMissing(self): |
| 731 | """Test missing all arguments.""" |
| 732 | out_proto = self._OutputProto() |
| 733 | in_proto = self._InputProto() |
| 734 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 735 | sysroot_controller.InstallPackages( |
| 736 | in_proto, out_proto, self.api_config |
| 737 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 738 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 739 | def testArgumentValidationNoSysroot(self): |
| 740 | """Test missing sysroot path.""" |
| 741 | out_proto = self._OutputProto() |
| 742 | in_proto = self._InputProto(build_target=self.build_target) |
| 743 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 744 | sysroot_controller.InstallPackages( |
| 745 | in_proto, out_proto, self.api_config |
| 746 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 747 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 748 | def testArgumentValidationNoBuildTarget(self): |
| 749 | """Test missing build target name.""" |
| 750 | out_proto = self._OutputProto() |
| 751 | in_proto = self._InputProto(sysroot_path=self.sysroot) |
| 752 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 753 | sysroot_controller.InstallPackages( |
| 754 | in_proto, out_proto, self.api_config |
| 755 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 756 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 757 | def testArgumentValidationInvalidSysroot(self): |
| 758 | """Test sysroot that hasn't had the toolchain installed.""" |
| 759 | out_proto = self._OutputProto() |
| 760 | in_proto = self._InputProto( |
| 761 | build_target=self.build_target, sysroot_path=self.sysroot |
| 762 | ) |
| 763 | self.PatchObject( |
| 764 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=False |
| 765 | ) |
| 766 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 767 | sysroot_controller.InstallPackages( |
| 768 | in_proto, out_proto, self.api_config |
| 769 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 770 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 771 | def testArgumentValidationInvalidPackage(self): |
| 772 | out_proto = self._OutputProto() |
| 773 | in_proto = self._InputProto( |
| 774 | build_target=self.build_target, |
| 775 | sysroot_path=self.sysroot, |
| 776 | packages=["package-1.0.0-r2"], |
| 777 | ) |
| 778 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 779 | sysroot_controller.InstallPackages( |
| 780 | in_proto, out_proto, self.api_config |
| 781 | ) |
Alex Klein | ca572ee | 2020-09-03 10:47:14 -0600 | [diff] [blame] | 782 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 783 | def testSuccessOutputHandling(self): |
| 784 | """Test successful call output handling.""" |
| 785 | # Prevent argument validation error. |
| 786 | self.PatchObject( |
| 787 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 788 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 789 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 790 | in_proto = self._InputProto( |
| 791 | build_target=self.build_target, sysroot_path=self.sysroot |
| 792 | ) |
| 793 | out_proto = self._OutputProto() |
| 794 | self.PatchObject(sysroot_service, "BuildPackages") |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 795 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 796 | rc = sysroot_controller.InstallPackages( |
| 797 | in_proto, out_proto, self.api_config |
| 798 | ) |
| 799 | self.assertFalse(rc) |
| 800 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 801 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 802 | def testSuccessPackageIndexes(self): |
| 803 | """Test successful call with package_indexes.""" |
| 804 | # Prevent argument validation error. |
| 805 | self.PatchObject( |
| 806 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 807 | ) |
| 808 | package_indexes = [ |
| 809 | common_pb2.PackageIndexInfo( |
| 810 | snapshot_sha="SHA", |
| 811 | snapshot_number=5, |
| 812 | build_target=common_pb2.BuildTarget(name="board"), |
| 813 | location="LOCATION", |
| 814 | profile=common_pb2.Profile(name="profile"), |
| 815 | ), |
| 816 | common_pb2.PackageIndexInfo( |
| 817 | snapshot_sha="SHA2", |
| 818 | snapshot_number=4, |
| 819 | build_target=common_pb2.BuildTarget(name="board"), |
| 820 | location="LOCATION2", |
| 821 | profile=common_pb2.Profile(name="profile"), |
| 822 | ), |
| 823 | ] |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 824 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 825 | in_proto = self._InputProto( |
| 826 | build_target=self.build_target, |
| 827 | sysroot_path=self.sysroot, |
| 828 | package_indexes=package_indexes, |
| 829 | ) |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 830 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 831 | out_proto = self._OutputProto() |
| 832 | rc_patch = self.PatchObject(sysroot_service, "BuildPackagesRunConfig") |
| 833 | self.PatchObject(sysroot_service, "BuildPackages") |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 834 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 835 | rc = sysroot_controller.InstallPackages( |
| 836 | in_proto, out_proto, self.api_config |
| 837 | ) |
| 838 | self.assertFalse(rc) |
| 839 | rc_patch.assert_called_with( |
| 840 | use_any_chrome=False, |
| 841 | usepkg=True, |
| 842 | install_debug_symbols=True, |
| 843 | packages=[], |
| 844 | package_indexes=[ |
Alex Klein | 6d718d6 | 2023-01-18 15:55:51 -0700 | [diff] [blame] | 845 | controller_util.deserialize_package_index_info(x) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 846 | for x in package_indexes |
| 847 | ], |
| 848 | use_flags=[], |
| 849 | use_goma=False, |
| 850 | use_remoteexec=False, |
| 851 | incremental_build=False, |
| 852 | dryrun=False, |
| 853 | backtrack=sysroot_controller.DEFAULT_BACKTRACK, |
| 854 | ) |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 855 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 856 | def testSuccessWithGomaLogs(self): |
| 857 | """Test successful call with goma.""" |
| 858 | self._CreateGomaLogFile( |
| 859 | self.goma_dir, |
| 860 | "compiler_proxy", |
| 861 | datetime.datetime(2018, 9, 21, 12, 0, 0), |
| 862 | ) |
| 863 | self._CreateGomaLogFile( |
| 864 | self.goma_dir, |
| 865 | "compiler_proxy-subproc", |
| 866 | datetime.datetime(2018, 9, 21, 12, 1, 0), |
| 867 | ) |
| 868 | self._CreateGomaLogFile( |
| 869 | self.goma_dir, "gomacc", datetime.datetime(2018, 9, 21, 12, 2, 0) |
| 870 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 871 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 872 | # Prevent argument validation error. |
| 873 | self.PatchObject( |
| 874 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 875 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 876 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 877 | in_proto = self._InputProto( |
| 878 | build_target=self.build_target, |
| 879 | sysroot_path=self.sysroot, |
| 880 | goma_dir=self.goma_dir, |
| 881 | goma_log_dir=self.goma_out_dir, |
| 882 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 883 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 884 | out_proto = self._OutputProto() |
| 885 | self.PatchObject(sysroot_service, "BuildPackages") |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 886 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 887 | rc = sysroot_controller.InstallPackages( |
| 888 | in_proto, out_proto, self.api_config |
| 889 | ) |
| 890 | self.assertFalse(rc) |
| 891 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 892 | |
| 893 | expected = [ |
| 894 | "compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz", |
| 895 | "compiler_proxy.host.log.INFO.20180921-120000.000000.gz", |
| 896 | "gomacc.host.log.INFO.20180921-120200.000000.tar.gz", |
| 897 | ] |
| 898 | self.assertCountEqual(out_proto.goma_artifacts.log_files, expected) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 899 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 900 | def testSuccessWithGomaLogsAndStatsCounterzFiles(self): |
| 901 | """Test successful call with goma including stats and counterz files.""" |
| 902 | self._CreateGomaLogFile( |
| 903 | self.goma_dir, |
| 904 | "compiler_proxy", |
| 905 | datetime.datetime(2018, 9, 21, 12, 0, 0), |
| 906 | ) |
| 907 | self._CreateGomaLogFile( |
| 908 | self.goma_dir, |
| 909 | "compiler_proxy-subproc", |
| 910 | datetime.datetime(2018, 9, 21, 12, 1, 0), |
| 911 | ) |
| 912 | self._CreateGomaLogFile( |
| 913 | self.goma_dir, "gomacc", datetime.datetime(2018, 9, 21, 12, 2, 0) |
| 914 | ) |
| 915 | # Create stats and counterz files. |
| 916 | osutils.WriteFile( |
| 917 | os.path.join(self.goma_dir, "stats.binaryproto"), |
| 918 | "File: stats.binaryproto", |
| 919 | ) |
| 920 | osutils.WriteFile( |
| 921 | os.path.join(self.goma_dir, "counterz.binaryproto"), |
| 922 | "File: counterz.binaryproto", |
| 923 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 924 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 925 | # Prevent argument validation error. |
| 926 | self.PatchObject( |
| 927 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 928 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 929 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 930 | in_proto = self._InputProto( |
| 931 | build_target=self.build_target, |
| 932 | sysroot_path=self.sysroot, |
| 933 | goma_dir=self.goma_dir, |
| 934 | goma_log_dir=self.goma_out_dir, |
| 935 | goma_stats_file="stats.binaryproto", |
| 936 | goma_counterz_file="counterz.binaryproto", |
| 937 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 938 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 939 | out_proto = self._OutputProto() |
| 940 | self.PatchObject(sysroot_service, "BuildPackages") |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 941 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 942 | rc = sysroot_controller.InstallPackages( |
| 943 | in_proto, out_proto, self.api_config |
| 944 | ) |
| 945 | self.assertFalse(rc) |
| 946 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 947 | expected_logs = [ |
| 948 | "compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz", |
| 949 | "compiler_proxy.host.log.INFO.20180921-120000.000000.gz", |
| 950 | "gomacc.host.log.INFO.20180921-120200.000000.tar.gz", |
| 951 | ] |
| 952 | self.assertCountEqual(out_proto.goma_artifacts.log_files, expected_logs) |
| 953 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 954 | # Verify that the output dir has 5 files -- since there should be 3 log |
| 955 | # files, the stats file, and the counterz file. |
| 956 | output_files = os.listdir(self.goma_out_dir) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 957 | expected_output = [ |
| 958 | "stats.binaryproto", |
| 959 | "counterz.binaryproto", |
| 960 | "compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz", |
| 961 | "compiler_proxy.host.log.INFO.20180921-120000.000000.gz", |
| 962 | "gomacc.host.log.INFO.20180921-120200.000000.tar.gz", |
| 963 | ] |
| 964 | self.assertCountEqual(output_files, expected_output) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 965 | self.assertEqual( |
| 966 | out_proto.goma_artifacts.counterz_file, "counterz.binaryproto" |
| 967 | ) |
| 968 | self.assertEqual( |
| 969 | out_proto.goma_artifacts.stats_file, "stats.binaryproto" |
| 970 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 971 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 972 | def testFailureMissingGomaStatsCounterzFiles(self): |
| 973 | """Test successful call with goma including stats and counterz files.""" |
| 974 | self._CreateGomaLogFile( |
| 975 | self.goma_dir, |
| 976 | "compiler_proxy", |
| 977 | datetime.datetime(2018, 9, 21, 12, 0, 0), |
| 978 | ) |
| 979 | self._CreateGomaLogFile( |
| 980 | self.goma_dir, |
| 981 | "compiler_proxy-subproc", |
| 982 | datetime.datetime(2018, 9, 21, 12, 1, 0), |
| 983 | ) |
| 984 | self._CreateGomaLogFile( |
| 985 | self.goma_dir, "gomacc", datetime.datetime(2018, 9, 21, 12, 2, 0) |
| 986 | ) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 987 | # Note that stats and counterz files are not created, but are specified |
| 988 | # in the proto below. |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 989 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 990 | # Prevent argument validation error. |
| 991 | self.PatchObject( |
| 992 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 993 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 994 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 995 | in_proto = self._InputProto( |
| 996 | build_target=self.build_target, |
| 997 | sysroot_path=self.sysroot, |
| 998 | goma_dir=self.goma_dir, |
| 999 | goma_log_dir=self.goma_out_dir, |
| 1000 | goma_stats_file="stats.binaryproto", |
| 1001 | goma_counterz_file="counterz.binaryproto", |
| 1002 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1003 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1004 | out_proto = self._OutputProto() |
| 1005 | self.PatchObject(sysroot_service, "BuildPackages") |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1006 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1007 | rc = sysroot_controller.InstallPackages( |
| 1008 | in_proto, out_proto, self.api_config |
| 1009 | ) |
| 1010 | self.assertFalse(rc) |
| 1011 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 1012 | expected_logs = [ |
| 1013 | "compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz", |
| 1014 | "compiler_proxy.host.log.INFO.20180921-120000.000000.gz", |
| 1015 | "gomacc.host.log.INFO.20180921-120200.000000.tar.gz", |
| 1016 | ] |
| 1017 | self.assertCountEqual(out_proto.goma_artifacts.log_files, expected_logs) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1018 | self.assertFalse(out_proto.goma_artifacts.counterz_file) |
| 1019 | self.assertFalse(out_proto.goma_artifacts.stats_file) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1020 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1021 | def testFailureOutputHandling(self): |
| 1022 | """Test failed package handling.""" |
| 1023 | # Prevent argument validation error. |
| 1024 | self.PatchObject( |
| 1025 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 1026 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 1027 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1028 | in_proto = self._InputProto( |
| 1029 | build_target=self.build_target, sysroot_path=self.sysroot |
| 1030 | ) |
| 1031 | out_proto = self._OutputProto() |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 1032 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1033 | # Failed package info and expected list for verification. |
| 1034 | err_pkgs = ["cat/pkg-1.0-r3", "cat2/pkg2-1.0-r1"] |
| 1035 | err_cpvs = [package_info.parse(cpv) for cpv in err_pkgs] |
| 1036 | expected = [("cat", "pkg"), ("cat2", "pkg2")] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 1037 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1038 | new_logs = {} |
| 1039 | for i, pkg in enumerate(err_pkgs): |
| 1040 | self._CreatePortageLogFile( |
| 1041 | self.portage_dir, |
| 1042 | err_cpvs[i], |
| 1043 | datetime.datetime(2021, 6, 9, 13, 37, 0), |
| 1044 | ) |
| 1045 | new_logs[pkg] = self._CreatePortageLogFile( |
| 1046 | self.portage_dir, |
| 1047 | err_cpvs[i], |
| 1048 | datetime.datetime(2021, 6, 9, 16, 20, 0), |
| 1049 | ) |
| 1050 | # Force error to be raised with the packages. |
| 1051 | error = sysroot_lib.PackageInstallError( |
| 1052 | "Error", cros_build_lib.CompletedProcess(), packages=err_cpvs |
| 1053 | ) |
| 1054 | self.PatchObject(sysroot_service, "BuildPackages", side_effect=error) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 1055 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1056 | rc = sysroot_controller.InstallPackages( |
| 1057 | in_proto, out_proto, self.api_config |
| 1058 | ) |
| 1059 | # This needs to return 2 to indicate the available error response. |
| 1060 | self.assertEqual( |
| 1061 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 1062 | ) |
| 1063 | for data in out_proto.failed_package_data: |
| 1064 | package = controller_util.deserialize_package_info(data.name) |
| 1065 | cat_pkg = (data.name.category, data.name.package_name) |
| 1066 | self.assertIn(cat_pkg, expected) |
| 1067 | self.assertEqual(data.log_path.path, new_logs[package.cpvr]) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 1068 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1069 | def testNoPackageFailureOutputHandling(self): |
| 1070 | """Test failure handling without packages to report.""" |
| 1071 | # Prevent argument validation error. |
| 1072 | self.PatchObject( |
| 1073 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 1074 | ) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 1075 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1076 | in_proto = self._InputProto( |
| 1077 | build_target=self.build_target, sysroot_path=self.sysroot |
| 1078 | ) |
| 1079 | out_proto = self._OutputProto() |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 1080 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1081 | # Force error to be raised with no packages. |
| 1082 | error = sysroot_lib.PackageInstallError( |
| 1083 | "Error", cros_build_lib.CompletedProcess(), packages=[] |
| 1084 | ) |
| 1085 | self.PatchObject(sysroot_service, "BuildPackages", side_effect=error) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 1086 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1087 | rc = sysroot_controller.InstallPackages( |
| 1088 | in_proto, out_proto, self.api_config |
| 1089 | ) |
| 1090 | # All we really care about is it's not 0 or 2 (response available), so |
| 1091 | # test for that rather than a specific return code. |
| 1092 | self.assertTrue(rc) |
| 1093 | self.assertNotEqual( |
| 1094 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 1095 | ) |