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