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 | |
Ian Barkley-Yeung | b527444 | 2023-04-28 16:32:20 -0700 | [diff] [blame] | 284 | def testArtifactsBreakpadDebugSymbols(self): |
| 285 | """Tests the extra parameters to BundleBreakpadSymbols""" |
| 286 | proto = common_pb2.ArtifactsByService.Sysroot( |
| 287 | output_artifacts=[ |
| 288 | common_pb2.ArtifactsByService.Sysroot.ArtifactInfo( |
| 289 | artifact_types=[ |
| 290 | # pylint: disable=line-too-long |
| 291 | common_pb2.ArtifactsByService.Sysroot.ArtifactType.BREAKPAD_DEBUG_SYMBOLS |
| 292 | # pylint: enable=line-too-long |
| 293 | ] |
| 294 | ) |
| 295 | ], |
| 296 | ignore_breakpad_symbol_generation_errors=True, |
| 297 | ignore_breakpad_symbol_generation_expected_files=[ |
| 298 | # pylint: disable=line-too-long |
| 299 | common_pb2.ArtifactsByService.Sysroot.BreakpadSymbolGenerationExpectedFile.EXPECTED_FILE_LIBC, |
| 300 | common_pb2.ArtifactsByService.Sysroot.BreakpadSymbolGenerationExpectedFile.EXPECTED_FILE_CRASH_REPORTER, |
| 301 | # pylint: enable=line-too-long |
| 302 | ], |
| 303 | ) |
| 304 | sysroot_controller.GetArtifacts( |
| 305 | proto, None, None, "build_target", "out" |
| 306 | ) |
| 307 | self._mocks[ |
| 308 | # pylint: disable=line-too-long |
| 309 | common_pb2.ArtifactsByService.Sysroot.ArtifactType.BREAKPAD_DEBUG_SYMBOLS |
| 310 | # pylint: enable=line-too-long |
| 311 | ].assert_called_once_with( |
| 312 | None, |
| 313 | None, |
| 314 | "build_target", |
| 315 | "out", |
| 316 | True, |
| 317 | ["LIBC", "CRASH_REPORTER"], |
| 318 | ) |
| 319 | |
| 320 | def testArtifactsExpectedFileNames(self): |
| 321 | """Verify all BreakpadSymbolGenerationExpectedFile have valid names. |
| 322 | |
| 323 | _BundleBreakpadSymbols inside GetArtifacts assumes that all values of |
| 324 | the BreakpadSymbolGenerationExpectedFile enum are named starting with |
| 325 | EXPECTED_FILE_. Confirm that assumption. |
| 326 | """ |
| 327 | for enum in ( |
| 328 | # pylint: disable=line-too-long |
| 329 | common_pb2.ArtifactsByService.Sysroot.BreakpadSymbolGenerationExpectedFile.keys() |
| 330 | # pylint: enable=line-too-long |
| 331 | ): |
| 332 | self.assertTrue(enum.startswith("EXPECTED_FILE_")) |
| 333 | |
Jack Neus | 26b9467 | 2022-10-27 17:33:21 +0000 | [diff] [blame] | 334 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 335 | class GenerateArchiveTest( |
| 336 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 337 | ): |
| 338 | """GenerateArchive function tests.""" |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 339 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 340 | def setUp(self): |
| 341 | self.chroot_path = "/path/to/chroot" |
| 342 | self.board = "board" |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 343 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 344 | def _InputProto(self, build_target=None, chroot_path=None, pkg_list=None): |
| 345 | """Helper to build and input proto instance.""" |
| 346 | # pkg_list will be a list of category/package strings such as |
| 347 | # ['virtual/target-fuzzers']. |
| 348 | if pkg_list: |
| 349 | package_list = [] |
| 350 | for pkg in pkg_list: |
| 351 | pkg_string_parts = pkg.split("/") |
| 352 | package_info_msg = common_pb2.PackageInfo( |
| 353 | category=pkg_string_parts[0], |
| 354 | package_name=pkg_string_parts[1], |
| 355 | ) |
| 356 | package_list.append(package_info_msg) |
| 357 | else: |
| 358 | package_list = [] |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 359 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 360 | return sysroot_pb2.SysrootGenerateArchiveRequest( |
| 361 | build_target={"name": build_target}, |
| 362 | chroot={"path": chroot_path}, |
| 363 | packages=package_list, |
| 364 | ) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 365 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 366 | def _OutputProto(self): |
| 367 | """Helper to build output proto instance.""" |
| 368 | return sysroot_pb2.SysrootGenerateArchiveResponse() |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 369 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 370 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 371 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 372 | patch = self.PatchObject(sysroot_service, "GenerateArchive") |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 373 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 374 | in_proto = self._InputProto( |
| 375 | build_target=self.board, |
| 376 | chroot_path=self.chroot_path, |
| 377 | pkg_list=["virtual/target-fuzzers"], |
| 378 | ) |
| 379 | sysroot_controller.GenerateArchive( |
| 380 | in_proto, self._OutputProto(), self.validate_only_config |
| 381 | ) |
| 382 | patch.assert_not_called() |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 383 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 384 | def testMockCall(self): |
| 385 | """Sanity check that a mock call does not execute any logic.""" |
| 386 | patch = self.PatchObject(sysroot_service, "GenerateArchive") |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 387 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 388 | in_proto = self._InputProto( |
| 389 | build_target=self.board, |
| 390 | chroot_path=self.chroot_path, |
| 391 | pkg_list=["virtual/target-fuzzers"], |
| 392 | ) |
| 393 | sysroot_controller.GenerateArchive( |
| 394 | in_proto, self._OutputProto(), self.mock_call_config |
| 395 | ) |
| 396 | patch.assert_not_called() |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 397 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 398 | def testArgumentValidation(self): |
| 399 | """Test the input argument validation.""" |
| 400 | # Error when no build target provided. |
| 401 | in_proto = self._InputProto() |
| 402 | out_proto = self._OutputProto() |
| 403 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 404 | sysroot_controller.GenerateArchive( |
| 405 | in_proto, out_proto, self.api_config |
| 406 | ) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 407 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 408 | # Error when packages is not specified. |
| 409 | in_proto = self._InputProto( |
| 410 | build_target="board", chroot_path=self.chroot_path |
| 411 | ) |
| 412 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 413 | sysroot_controller.GenerateArchive( |
| 414 | in_proto, out_proto, self.api_config |
| 415 | ) |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 416 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 417 | # Valid when board, chroot path, and package are specified. |
| 418 | patch = self.PatchObject( |
| 419 | sysroot_service, |
| 420 | "GenerateArchive", |
| 421 | return_value="/path/to/sysroot/tar.bz", |
| 422 | ) |
| 423 | in_proto = self._InputProto( |
| 424 | build_target="board", |
| 425 | chroot_path=self.chroot_path, |
| 426 | pkg_list=["virtual/target-fuzzers"], |
| 427 | ) |
| 428 | out_proto = self._OutputProto() |
| 429 | sysroot_controller.GenerateArchive(in_proto, out_proto, self.api_config) |
| 430 | patch.assert_called_once() |
Michael Mortensen | 3f6b4bd | 2020-02-07 14:16:43 -0700 | [diff] [blame] | 431 | |
| 432 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 433 | class InstallToolchainTest( |
| 434 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 435 | ): |
| 436 | """Install toolchain function tests.""" |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 437 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 438 | def setUp(self): |
| 439 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True) |
| 440 | # Avoid running the portageq command. |
| 441 | self.PatchObject(sysroot_controller, "_LogBinhost") |
| 442 | self.board = "board" |
| 443 | self.sysroot = os.path.join(self.tempdir, "board") |
| 444 | self.invalid_sysroot = os.path.join(self.tempdir, "invalid", "sysroot") |
| 445 | osutils.SafeMakedirs(self.sysroot) |
| 446 | # Set up portage log directory. |
| 447 | self.target_sysroot = sysroot_lib.Sysroot(self.sysroot) |
| 448 | self.portage_dir = os.path.join(self.tempdir, "portage_logdir") |
| 449 | self.PatchObject( |
| 450 | sysroot_lib.Sysroot, "portage_logdir", new=self.portage_dir |
| 451 | ) |
| 452 | osutils.SafeMakedirs(self.portage_dir) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 453 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 454 | def _InputProto( |
| 455 | self, build_target=None, sysroot_path=None, compile_source=False |
| 456 | ): |
| 457 | """Helper to build an input proto instance.""" |
| 458 | proto = sysroot_pb2.InstallToolchainRequest() |
| 459 | if build_target: |
| 460 | proto.sysroot.build_target.name = build_target |
| 461 | if sysroot_path: |
| 462 | proto.sysroot.path = sysroot_path |
| 463 | if compile_source: |
| 464 | proto.flags.compile_source = compile_source |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 465 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 466 | return proto |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 467 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | def _OutputProto(self): |
| 469 | """Helper to build output proto instance.""" |
| 470 | return sysroot_pb2.InstallToolchainResponse() |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 471 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 472 | def _CreatePortageLogFile( |
| 473 | self, |
| 474 | log_path: Union[str, os.PathLike], |
| 475 | pkg_info: package_info.PackageInfo, |
| 476 | timestamp: datetime.datetime, |
| 477 | ): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 478 | """Creates a log file to test for individual packages built by Portage. |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 479 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 480 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 481 | log_path: The PORTAGE_LOGDIR path. |
| 482 | pkg_info: Package name used to name the log file. |
| 483 | timestamp: Timestamp used to name the file. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 484 | """ |
| 485 | path = os.path.join( |
| 486 | log_path, |
| 487 | f"{pkg_info.category}:{pkg_info.pvr}:" |
| 488 | f'{timestamp.strftime("%Y%m%d-%H%M%S")}.log', |
| 489 | ) |
| 490 | osutils.WriteFile( |
| 491 | path, |
| 492 | f"Test log file for package {pkg_info.category}/" |
| 493 | f"{pkg_info.package} written to {path}", |
| 494 | ) |
| 495 | return path |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 496 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 497 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 498 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 499 | patch = self.PatchObject(sysroot_service, "InstallToolchain") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 500 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 501 | in_proto = self._InputProto( |
| 502 | build_target=self.board, sysroot_path=self.sysroot |
| 503 | ) |
| 504 | sysroot_controller.InstallToolchain( |
| 505 | in_proto, self._OutputProto(), self.validate_only_config |
| 506 | ) |
| 507 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 508 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 509 | def testMockCall(self): |
| 510 | """Sanity check that a mock call does not execute any logic.""" |
| 511 | patch = self.PatchObject(sysroot_service, "InstallToolchain") |
| 512 | request = self._InputProto() |
| 513 | response = self._OutputProto() |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 514 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 515 | rc = sysroot_controller.InstallToolchain( |
| 516 | request, response, self.mock_call_config |
| 517 | ) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 518 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 519 | patch.assert_not_called() |
| 520 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 521 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 522 | def testMockError(self): |
| 523 | """Sanity check that a mock error does not execute any logic.""" |
| 524 | patch = self.PatchObject(sysroot_service, "InstallToolchain") |
| 525 | request = self._InputProto() |
| 526 | response = self._OutputProto() |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 527 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 528 | rc = sysroot_controller.InstallToolchain( |
| 529 | request, response, self.mock_error_config |
| 530 | ) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 531 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 532 | patch.assert_not_called() |
| 533 | self.assertEqual( |
| 534 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 535 | ) |
| 536 | self.assertTrue(response.failed_package_data) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 537 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 538 | def testArgumentValidation(self): |
| 539 | """Test the argument validation.""" |
| 540 | # Test errors on missing inputs. |
| 541 | out_proto = self._OutputProto() |
| 542 | # Both missing. |
| 543 | in_proto = self._InputProto() |
| 544 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 545 | sysroot_controller.InstallToolchain( |
| 546 | in_proto, out_proto, self.api_config |
| 547 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 548 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 549 | # Sysroot path missing. |
| 550 | in_proto = self._InputProto(build_target=self.board) |
| 551 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 552 | sysroot_controller.InstallToolchain( |
| 553 | in_proto, out_proto, self.api_config |
| 554 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 555 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 556 | # Build target name missing. |
| 557 | in_proto = self._InputProto(sysroot_path=self.sysroot) |
| 558 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 559 | sysroot_controller.InstallToolchain( |
| 560 | in_proto, out_proto, self.api_config |
| 561 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 562 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 563 | # Both provided, but invalid sysroot path. |
| 564 | in_proto = self._InputProto( |
| 565 | build_target=self.board, sysroot_path=self.invalid_sysroot |
| 566 | ) |
| 567 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 568 | sysroot_controller.InstallToolchain( |
| 569 | in_proto, out_proto, self.api_config |
| 570 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 571 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 572 | def testSuccessOutputHandling(self): |
| 573 | """Test the output is processed and recorded correctly.""" |
| 574 | self.PatchObject(sysroot_service, "InstallToolchain") |
| 575 | out_proto = self._OutputProto() |
| 576 | in_proto = self._InputProto( |
| 577 | build_target=self.board, sysroot_path=self.sysroot |
| 578 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 579 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 580 | rc = sysroot_controller.InstallToolchain( |
| 581 | in_proto, out_proto, self.api_config |
| 582 | ) |
| 583 | self.assertFalse(rc) |
| 584 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 585 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 586 | def testErrorOutputHandling(self): |
| 587 | """Test the error output is processed and recorded correctly.""" |
| 588 | out_proto = self._OutputProto() |
| 589 | in_proto = self._InputProto( |
| 590 | build_target=self.board, sysroot_path=self.sysroot |
| 591 | ) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 592 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 593 | err_pkgs = ["cat/pkg-1.0-r1", "cat2/pkg2-1.0-r1"] |
| 594 | err_cpvs = [package_info.parse(pkg) for pkg in err_pkgs] |
| 595 | expected = [("cat", "pkg"), ("cat2", "pkg2")] |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 596 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 597 | new_logs = {} |
| 598 | for i, pkg in enumerate(err_pkgs): |
| 599 | self._CreatePortageLogFile( |
| 600 | self.portage_dir, |
| 601 | err_cpvs[i], |
| 602 | datetime.datetime(2021, 6, 9, 13, 37, 0), |
| 603 | ) |
| 604 | new_logs[pkg] = self._CreatePortageLogFile( |
| 605 | self.portage_dir, |
| 606 | err_cpvs[i], |
| 607 | datetime.datetime(2021, 6, 9, 16, 20, 0), |
| 608 | ) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 609 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 610 | err = sysroot_lib.ToolchainInstallError( |
| 611 | "Error", cros_build_lib.CompletedProcess(), tc_info=err_cpvs |
| 612 | ) |
| 613 | self.PatchObject(sysroot_service, "InstallToolchain", side_effect=err) |
Alex Klein | da35fcf | 2019-03-07 16:01:15 -0700 | [diff] [blame] | 614 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 615 | rc = sysroot_controller.InstallToolchain( |
| 616 | in_proto, out_proto, self.api_config |
| 617 | ) |
| 618 | self.assertEqual( |
| 619 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 620 | ) |
| 621 | self.assertTrue(out_proto.failed_package_data) |
| 622 | # This needs to return 2 to indicate the available error response. |
| 623 | self.assertEqual( |
| 624 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 625 | ) |
| 626 | for data in out_proto.failed_package_data: |
| 627 | package = controller_util.deserialize_package_info(data.name) |
| 628 | cat_pkg = (data.name.category, data.name.package_name) |
| 629 | self.assertIn(cat_pkg, expected) |
| 630 | self.assertEqual(data.log_path.path, new_logs[package.cpvr]) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 631 | |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 632 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 633 | class InstallPackagesTest( |
| 634 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 635 | ): |
| 636 | """InstallPackages tests.""" |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 637 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 638 | def setUp(self): |
| 639 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True) |
| 640 | # Avoid running the portageq command. |
| 641 | self.PatchObject(sysroot_controller, "_LogBinhost") |
| 642 | self.build_target = "board" |
| 643 | self.sysroot = os.path.join(self.tempdir, "build", "board") |
| 644 | osutils.SafeMakedirs(self.sysroot) |
| 645 | # Set up portage log directory. |
| 646 | self.target_sysroot = sysroot_lib.Sysroot(self.sysroot) |
| 647 | self.portage_dir = os.path.join(self.tempdir, "portage_logdir") |
| 648 | self.PatchObject( |
| 649 | sysroot_lib.Sysroot, "portage_logdir", new=self.portage_dir |
| 650 | ) |
| 651 | osutils.SafeMakedirs(self.portage_dir) |
| 652 | # Set up goma directories. |
| 653 | self.goma_dir = os.path.join(self.tempdir, "goma_dir") |
| 654 | osutils.SafeMakedirs(self.goma_dir) |
| 655 | self.goma_out_dir = os.path.join(self.tempdir, "goma_out_dir") |
| 656 | osutils.SafeMakedirs(self.goma_out_dir) |
| 657 | os.environ["GLOG_log_dir"] = self.goma_dir |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 658 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 659 | def _InputProto( |
| 660 | self, |
| 661 | build_target=None, |
| 662 | sysroot_path=None, |
| 663 | build_source=False, |
Yoshiki Iguchi | 2f7f022 | 2023-05-17 19:30:09 +0900 | [diff] [blame] | 664 | use_cq_prebuilts=False, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 665 | goma_dir=None, |
| 666 | goma_log_dir=None, |
| 667 | goma_stats_file=None, |
| 668 | goma_counterz_file=None, |
| 669 | package_indexes=None, |
| 670 | packages=None, |
| 671 | ): |
| 672 | """Helper to build an input proto instance.""" |
| 673 | instance = sysroot_pb2.InstallPackagesRequest() |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 674 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 675 | if build_target: |
| 676 | instance.sysroot.build_target.name = build_target |
| 677 | if sysroot_path: |
| 678 | instance.sysroot.path = sysroot_path |
| 679 | if build_source: |
| 680 | instance.flags.build_source = build_source |
Yoshiki Iguchi | 2f7f022 | 2023-05-17 19:30:09 +0900 | [diff] [blame] | 681 | if use_cq_prebuilts: |
| 682 | instance.flags.use_cq_prebuilts = use_cq_prebuilts |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 683 | if goma_dir: |
| 684 | instance.goma_config.goma_dir = goma_dir |
| 685 | if goma_log_dir: |
| 686 | instance.goma_config.log_dir.dir = goma_log_dir |
| 687 | if goma_stats_file: |
| 688 | instance.goma_config.stats_file = goma_stats_file |
| 689 | if goma_counterz_file: |
| 690 | instance.goma_config.counterz_file = goma_counterz_file |
| 691 | if package_indexes: |
| 692 | instance.package_indexes.extend(package_indexes) |
| 693 | if packages: |
| 694 | for pkg in packages: |
| 695 | pkg_info = package_info.parse(pkg) |
| 696 | pkg_info_msg = instance.packages.add() |
| 697 | controller_util.serialize_package_info(pkg_info, pkg_info_msg) |
| 698 | return instance |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 699 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 700 | def _OutputProto(self): |
| 701 | """Helper to build an empty output proto instance.""" |
| 702 | return sysroot_pb2.InstallPackagesResponse() |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 703 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 704 | def _CreateGomaLogFile( |
| 705 | self, |
| 706 | goma_log_dir: Union[str, os.PathLike], |
| 707 | name: str, |
| 708 | timestamp: datetime.datetime, |
| 709 | ): |
| 710 | """Creates a log file for testing. |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 711 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 712 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 713 | goma_log_dir: Directory where the file will be created. |
| 714 | name: Log file 'base' name that is combined with the timestamp. |
| 715 | timestamp: Timestamp that is written to the file. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 716 | """ |
| 717 | path = os.path.join( |
| 718 | goma_log_dir, |
| 719 | "%s.host.log.INFO.%s" |
| 720 | % (name, timestamp.strftime("%Y%m%d-%H%M%S.%f")), |
| 721 | ) |
| 722 | osutils.WriteFile( |
| 723 | path, |
| 724 | timestamp.strftime("Goma log file created at: %Y/%m/%d %H:%M:%S"), |
| 725 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 726 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 727 | def _CreatePortageLogFile( |
| 728 | self, |
| 729 | log_path: Union[str, os.PathLike], |
| 730 | pkg_info: package_info.PackageInfo, |
| 731 | timestamp: datetime.datetime, |
| 732 | ): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 733 | """Creates a log file to test for individual packages built by Portage. |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 734 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 735 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 736 | log_path: The PORTAGE_LOGDIR path. |
| 737 | pkg_info: Package name used to name the log file. |
| 738 | timestamp: Timestamp used to name the file. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 739 | """ |
| 740 | path = os.path.join( |
| 741 | log_path, |
| 742 | f"{pkg_info.category}:{pkg_info.pvr}:" |
| 743 | f'{timestamp.strftime("%Y%m%d-%H%M%S")}.log', |
| 744 | ) |
| 745 | osutils.WriteFile( |
| 746 | path, |
| 747 | f"Test log file for package {pkg_info.category}/" |
| 748 | f"{pkg_info.package} written to {path}", |
| 749 | ) |
| 750 | return path |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 751 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 752 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 753 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 754 | patch = self.PatchObject(sysroot_service, "BuildPackages") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 755 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 756 | in_proto = self._InputProto( |
| 757 | build_target=self.build_target, sysroot_path=self.sysroot |
| 758 | ) |
| 759 | sysroot_controller.InstallPackages( |
| 760 | in_proto, self._OutputProto(), self.validate_only_config |
| 761 | ) |
| 762 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 763 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 764 | def testMockCall(self): |
| 765 | """Sanity check that a mock call does not execute any logic.""" |
| 766 | patch = self.PatchObject(sysroot_service, "BuildPackages") |
| 767 | request = self._InputProto() |
| 768 | response = self._OutputProto() |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 769 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 770 | rc = sysroot_controller.InstallPackages( |
| 771 | request, response, self.mock_call_config |
| 772 | ) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 773 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 774 | patch.assert_not_called() |
| 775 | self.assertEqual(controller.RETURN_CODE_SUCCESS, rc) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 776 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 777 | def testMockError(self): |
| 778 | """Sanity check that a mock error does not execute any logic.""" |
| 779 | patch = self.PatchObject(sysroot_service, "BuildPackages") |
| 780 | request = self._InputProto() |
| 781 | response = self._OutputProto() |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 782 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 783 | rc = sysroot_controller.InstallPackages( |
| 784 | request, response, self.mock_error_config |
| 785 | ) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 786 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 787 | patch.assert_not_called() |
| 788 | self.assertEqual( |
| 789 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 790 | ) |
| 791 | self.assertTrue(response.failed_package_data) |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 792 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 793 | def testArgumentValidationAllMissing(self): |
| 794 | """Test missing all arguments.""" |
| 795 | out_proto = self._OutputProto() |
| 796 | in_proto = self._InputProto() |
| 797 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 798 | sysroot_controller.InstallPackages( |
| 799 | in_proto, out_proto, self.api_config |
| 800 | ) |
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 testArgumentValidationNoSysroot(self): |
| 803 | """Test missing sysroot path.""" |
| 804 | out_proto = self._OutputProto() |
| 805 | in_proto = self._InputProto(build_target=self.build_target) |
| 806 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 807 | sysroot_controller.InstallPackages( |
| 808 | in_proto, out_proto, self.api_config |
| 809 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 810 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 811 | def testArgumentValidationNoBuildTarget(self): |
| 812 | """Test missing build target name.""" |
| 813 | out_proto = self._OutputProto() |
| 814 | in_proto = self._InputProto(sysroot_path=self.sysroot) |
| 815 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 816 | sysroot_controller.InstallPackages( |
| 817 | in_proto, out_proto, self.api_config |
| 818 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 819 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 820 | def testArgumentValidationInvalidSysroot(self): |
| 821 | """Test sysroot that hasn't had the toolchain installed.""" |
| 822 | out_proto = self._OutputProto() |
| 823 | in_proto = self._InputProto( |
| 824 | build_target=self.build_target, sysroot_path=self.sysroot |
| 825 | ) |
| 826 | self.PatchObject( |
| 827 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=False |
| 828 | ) |
| 829 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 830 | sysroot_controller.InstallPackages( |
| 831 | in_proto, out_proto, self.api_config |
| 832 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 833 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 834 | def testArgumentValidationInvalidPackage(self): |
| 835 | out_proto = self._OutputProto() |
| 836 | in_proto = self._InputProto( |
| 837 | build_target=self.build_target, |
| 838 | sysroot_path=self.sysroot, |
| 839 | packages=["package-1.0.0-r2"], |
| 840 | ) |
| 841 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 842 | sysroot_controller.InstallPackages( |
| 843 | in_proto, out_proto, self.api_config |
| 844 | ) |
Alex Klein | ca572ee | 2020-09-03 10:47:14 -0600 | [diff] [blame] | 845 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 846 | def testSuccessOutputHandling(self): |
| 847 | """Test successful call output handling.""" |
| 848 | # Prevent argument validation error. |
| 849 | self.PatchObject( |
| 850 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 851 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 852 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 853 | in_proto = self._InputProto( |
| 854 | build_target=self.build_target, sysroot_path=self.sysroot |
| 855 | ) |
| 856 | out_proto = self._OutputProto() |
| 857 | self.PatchObject(sysroot_service, "BuildPackages") |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 858 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 859 | rc = sysroot_controller.InstallPackages( |
| 860 | in_proto, out_proto, self.api_config |
| 861 | ) |
| 862 | self.assertFalse(rc) |
| 863 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 864 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 865 | def testSuccessPackageIndexes(self): |
| 866 | """Test successful call with package_indexes.""" |
| 867 | # Prevent argument validation error. |
| 868 | self.PatchObject( |
| 869 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 870 | ) |
| 871 | package_indexes = [ |
| 872 | common_pb2.PackageIndexInfo( |
| 873 | snapshot_sha="SHA", |
| 874 | snapshot_number=5, |
| 875 | build_target=common_pb2.BuildTarget(name="board"), |
| 876 | location="LOCATION", |
| 877 | profile=common_pb2.Profile(name="profile"), |
| 878 | ), |
| 879 | common_pb2.PackageIndexInfo( |
| 880 | snapshot_sha="SHA2", |
| 881 | snapshot_number=4, |
| 882 | build_target=common_pb2.BuildTarget(name="board"), |
| 883 | location="LOCATION2", |
| 884 | profile=common_pb2.Profile(name="profile"), |
| 885 | ), |
| 886 | ] |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 887 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 888 | in_proto = self._InputProto( |
| 889 | build_target=self.build_target, |
| 890 | sysroot_path=self.sysroot, |
| 891 | package_indexes=package_indexes, |
| 892 | ) |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 893 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 894 | out_proto = self._OutputProto() |
| 895 | rc_patch = self.PatchObject(sysroot_service, "BuildPackagesRunConfig") |
| 896 | self.PatchObject(sysroot_service, "BuildPackages") |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 897 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 898 | rc = sysroot_controller.InstallPackages( |
| 899 | in_proto, out_proto, self.api_config |
| 900 | ) |
| 901 | self.assertFalse(rc) |
| 902 | rc_patch.assert_called_with( |
| 903 | use_any_chrome=False, |
| 904 | usepkg=True, |
| 905 | install_debug_symbols=True, |
| 906 | packages=[], |
| 907 | package_indexes=[ |
Alex Klein | 6d718d6 | 2023-01-18 15:55:51 -0700 | [diff] [blame] | 908 | controller_util.deserialize_package_index_info(x) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 909 | for x in package_indexes |
| 910 | ], |
| 911 | use_flags=[], |
| 912 | use_goma=False, |
| 913 | use_remoteexec=False, |
| 914 | incremental_build=False, |
| 915 | dryrun=False, |
| 916 | backtrack=sysroot_controller.DEFAULT_BACKTRACK, |
Alex Klein | 8393dc2 | 2023-03-30 10:54:27 -0600 | [diff] [blame] | 917 | workon=False, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 918 | ) |
LaMont Jones | c0343fa | 2020-08-12 18:58:31 -0600 | [diff] [blame] | 919 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 920 | def testSuccessWithGomaLogs(self): |
| 921 | """Test successful call with goma.""" |
| 922 | self._CreateGomaLogFile( |
| 923 | self.goma_dir, |
| 924 | "compiler_proxy", |
| 925 | datetime.datetime(2018, 9, 21, 12, 0, 0), |
| 926 | ) |
| 927 | self._CreateGomaLogFile( |
| 928 | self.goma_dir, |
| 929 | "compiler_proxy-subproc", |
| 930 | datetime.datetime(2018, 9, 21, 12, 1, 0), |
| 931 | ) |
| 932 | self._CreateGomaLogFile( |
| 933 | self.goma_dir, "gomacc", datetime.datetime(2018, 9, 21, 12, 2, 0) |
| 934 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 935 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 936 | # Prevent argument validation error. |
| 937 | self.PatchObject( |
| 938 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 939 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 940 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 941 | in_proto = self._InputProto( |
| 942 | build_target=self.build_target, |
| 943 | sysroot_path=self.sysroot, |
| 944 | goma_dir=self.goma_dir, |
| 945 | goma_log_dir=self.goma_out_dir, |
| 946 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 947 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 948 | out_proto = self._OutputProto() |
| 949 | self.PatchObject(sysroot_service, "BuildPackages") |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 950 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 951 | rc = sysroot_controller.InstallPackages( |
| 952 | in_proto, out_proto, self.api_config |
| 953 | ) |
| 954 | self.assertFalse(rc) |
| 955 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 956 | |
| 957 | expected = [ |
| 958 | "compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz", |
| 959 | "compiler_proxy.host.log.INFO.20180921-120000.000000.gz", |
| 960 | "gomacc.host.log.INFO.20180921-120200.000000.tar.gz", |
| 961 | ] |
| 962 | self.assertCountEqual(out_proto.goma_artifacts.log_files, expected) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 963 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 964 | def testSuccessWithGomaLogsAndStatsCounterzFiles(self): |
| 965 | """Test successful call with goma including stats and counterz files.""" |
| 966 | self._CreateGomaLogFile( |
| 967 | self.goma_dir, |
| 968 | "compiler_proxy", |
| 969 | datetime.datetime(2018, 9, 21, 12, 0, 0), |
| 970 | ) |
| 971 | self._CreateGomaLogFile( |
| 972 | self.goma_dir, |
| 973 | "compiler_proxy-subproc", |
| 974 | datetime.datetime(2018, 9, 21, 12, 1, 0), |
| 975 | ) |
| 976 | self._CreateGomaLogFile( |
| 977 | self.goma_dir, "gomacc", datetime.datetime(2018, 9, 21, 12, 2, 0) |
| 978 | ) |
| 979 | # Create stats and counterz files. |
| 980 | osutils.WriteFile( |
| 981 | os.path.join(self.goma_dir, "stats.binaryproto"), |
| 982 | "File: stats.binaryproto", |
| 983 | ) |
| 984 | osutils.WriteFile( |
| 985 | os.path.join(self.goma_dir, "counterz.binaryproto"), |
| 986 | "File: counterz.binaryproto", |
| 987 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 988 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 989 | # Prevent argument validation error. |
| 990 | self.PatchObject( |
| 991 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 992 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 993 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 994 | in_proto = self._InputProto( |
| 995 | build_target=self.build_target, |
| 996 | sysroot_path=self.sysroot, |
| 997 | goma_dir=self.goma_dir, |
| 998 | goma_log_dir=self.goma_out_dir, |
| 999 | goma_stats_file="stats.binaryproto", |
| 1000 | goma_counterz_file="counterz.binaryproto", |
| 1001 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1002 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1003 | out_proto = self._OutputProto() |
| 1004 | self.PatchObject(sysroot_service, "BuildPackages") |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1005 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1006 | rc = sysroot_controller.InstallPackages( |
| 1007 | in_proto, out_proto, self.api_config |
| 1008 | ) |
| 1009 | self.assertFalse(rc) |
| 1010 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 1011 | expected_logs = [ |
| 1012 | "compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz", |
| 1013 | "compiler_proxy.host.log.INFO.20180921-120000.000000.gz", |
| 1014 | "gomacc.host.log.INFO.20180921-120200.000000.tar.gz", |
| 1015 | ] |
| 1016 | self.assertCountEqual(out_proto.goma_artifacts.log_files, expected_logs) |
| 1017 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1018 | # Verify that the output dir has 5 files -- since there should be 3 log |
| 1019 | # files, the stats file, and the counterz file. |
| 1020 | output_files = os.listdir(self.goma_out_dir) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 1021 | expected_output = [ |
| 1022 | "stats.binaryproto", |
| 1023 | "counterz.binaryproto", |
| 1024 | "compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz", |
| 1025 | "compiler_proxy.host.log.INFO.20180921-120000.000000.gz", |
| 1026 | "gomacc.host.log.INFO.20180921-120200.000000.tar.gz", |
| 1027 | ] |
| 1028 | self.assertCountEqual(output_files, expected_output) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1029 | self.assertEqual( |
| 1030 | out_proto.goma_artifacts.counterz_file, "counterz.binaryproto" |
| 1031 | ) |
| 1032 | self.assertEqual( |
| 1033 | out_proto.goma_artifacts.stats_file, "stats.binaryproto" |
| 1034 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1035 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1036 | def testFailureMissingGomaStatsCounterzFiles(self): |
| 1037 | """Test successful call with goma including stats and counterz files.""" |
| 1038 | self._CreateGomaLogFile( |
| 1039 | self.goma_dir, |
| 1040 | "compiler_proxy", |
| 1041 | datetime.datetime(2018, 9, 21, 12, 0, 0), |
| 1042 | ) |
| 1043 | self._CreateGomaLogFile( |
| 1044 | self.goma_dir, |
| 1045 | "compiler_proxy-subproc", |
| 1046 | datetime.datetime(2018, 9, 21, 12, 1, 0), |
| 1047 | ) |
| 1048 | self._CreateGomaLogFile( |
| 1049 | self.goma_dir, "gomacc", datetime.datetime(2018, 9, 21, 12, 2, 0) |
| 1050 | ) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 1051 | # Note that stats and counterz files are not created, but are specified |
| 1052 | # in the proto below. |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1053 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1054 | # Prevent argument validation error. |
| 1055 | self.PatchObject( |
| 1056 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 1057 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1058 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1059 | in_proto = self._InputProto( |
| 1060 | build_target=self.build_target, |
| 1061 | sysroot_path=self.sysroot, |
| 1062 | goma_dir=self.goma_dir, |
| 1063 | goma_log_dir=self.goma_out_dir, |
| 1064 | goma_stats_file="stats.binaryproto", |
| 1065 | goma_counterz_file="counterz.binaryproto", |
| 1066 | ) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1067 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1068 | out_proto = self._OutputProto() |
| 1069 | self.PatchObject(sysroot_service, "BuildPackages") |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1070 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1071 | rc = sysroot_controller.InstallPackages( |
| 1072 | in_proto, out_proto, self.api_config |
| 1073 | ) |
| 1074 | self.assertFalse(rc) |
| 1075 | self.assertFalse(out_proto.failed_package_data) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 1076 | expected_logs = [ |
| 1077 | "compiler_proxy-subproc.host.log.INFO.20180921-120100.000000.gz", |
| 1078 | "compiler_proxy.host.log.INFO.20180921-120000.000000.gz", |
| 1079 | "gomacc.host.log.INFO.20180921-120200.000000.tar.gz", |
| 1080 | ] |
| 1081 | self.assertCountEqual(out_proto.goma_artifacts.log_files, expected_logs) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1082 | self.assertFalse(out_proto.goma_artifacts.counterz_file) |
| 1083 | self.assertFalse(out_proto.goma_artifacts.stats_file) |
Michael Mortensen | 798ee19 | 2020-01-17 13:04:43 -0700 | [diff] [blame] | 1084 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1085 | def testFailureOutputHandling(self): |
| 1086 | """Test failed package handling.""" |
| 1087 | # Prevent argument validation error. |
| 1088 | self.PatchObject( |
| 1089 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 1090 | ) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 1091 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1092 | in_proto = self._InputProto( |
| 1093 | build_target=self.build_target, sysroot_path=self.sysroot |
| 1094 | ) |
| 1095 | out_proto = self._OutputProto() |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 1096 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1097 | # Failed package info and expected list for verification. |
| 1098 | err_pkgs = ["cat/pkg-1.0-r3", "cat2/pkg2-1.0-r1"] |
| 1099 | err_cpvs = [package_info.parse(cpv) for cpv in err_pkgs] |
| 1100 | expected = [("cat", "pkg"), ("cat2", "pkg2")] |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 1101 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1102 | new_logs = {} |
| 1103 | for i, pkg in enumerate(err_pkgs): |
| 1104 | self._CreatePortageLogFile( |
| 1105 | self.portage_dir, |
| 1106 | err_cpvs[i], |
| 1107 | datetime.datetime(2021, 6, 9, 13, 37, 0), |
| 1108 | ) |
| 1109 | new_logs[pkg] = self._CreatePortageLogFile( |
| 1110 | self.portage_dir, |
| 1111 | err_cpvs[i], |
| 1112 | datetime.datetime(2021, 6, 9, 16, 20, 0), |
| 1113 | ) |
| 1114 | # Force error to be raised with the packages. |
| 1115 | error = sysroot_lib.PackageInstallError( |
| 1116 | "Error", cros_build_lib.CompletedProcess(), packages=err_cpvs |
| 1117 | ) |
| 1118 | self.PatchObject(sysroot_service, "BuildPackages", side_effect=error) |
Alex Klein | d4e1e42 | 2019-03-18 16:00:41 -0600 | [diff] [blame] | 1119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1120 | rc = sysroot_controller.InstallPackages( |
| 1121 | in_proto, out_proto, self.api_config |
| 1122 | ) |
| 1123 | # This needs to return 2 to indicate the available error response. |
| 1124 | self.assertEqual( |
| 1125 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 1126 | ) |
| 1127 | for data in out_proto.failed_package_data: |
| 1128 | package = controller_util.deserialize_package_info(data.name) |
| 1129 | cat_pkg = (data.name.category, data.name.package_name) |
| 1130 | self.assertIn(cat_pkg, expected) |
| 1131 | self.assertEqual(data.log_path.path, new_logs[package.cpvr]) |
Lizzy Presland | 7e23a61 | 2021-11-09 21:49:42 +0000 | [diff] [blame] | 1132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1133 | def testNoPackageFailureOutputHandling(self): |
| 1134 | """Test failure handling without packages to report.""" |
| 1135 | # Prevent argument validation error. |
| 1136 | self.PatchObject( |
| 1137 | sysroot_lib.Sysroot, "IsToolchainInstalled", return_value=True |
| 1138 | ) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 1139 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1140 | in_proto = self._InputProto( |
| 1141 | build_target=self.build_target, sysroot_path=self.sysroot |
| 1142 | ) |
| 1143 | out_proto = self._OutputProto() |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 1144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1145 | # Force error to be raised with no packages. |
| 1146 | error = sysroot_lib.PackageInstallError( |
| 1147 | "Error", cros_build_lib.CompletedProcess(), packages=[] |
| 1148 | ) |
| 1149 | self.PatchObject(sysroot_service, "BuildPackages", side_effect=error) |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 1150 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 1151 | rc = sysroot_controller.InstallPackages( |
| 1152 | in_proto, out_proto, self.api_config |
| 1153 | ) |
| 1154 | # All we really care about is it's not 0 or 2 (response available), so |
| 1155 | # test for that rather than a specific return code. |
| 1156 | self.assertTrue(rc) |
| 1157 | self.assertNotEqual( |
| 1158 | controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE, rc |
| 1159 | ) |