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