Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -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 | """Unittests for Toolchain-related operations.""" |
| 6 | |
Alex Klein | cd03a5e | 2021-10-18 13:23:47 -0600 | [diff] [blame] | 7 | import os |
| 8 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 9 | from chromite.api import api_config |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 10 | from chromite.api import controller |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 11 | from chromite.api.controller import toolchain |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 12 | from chromite.api.gen.chromite.api import artifacts_pb2 |
LaMont Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame] | 13 | from chromite.api.gen.chromite.api import sysroot_pb2 |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 14 | from chromite.api.gen.chromite.api import toolchain_pb2 |
Trent Apted | 39e74d3 | 2023-09-04 11:24:40 +1000 | [diff] [blame] | 15 | from chromite.api.gen.chromiumos import builder_config_pb2 |
LaMont Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame] | 16 | from chromite.api.gen.chromiumos import common_pb2 |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 17 | from chromite.lib import cros_build_lib |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_test_lib |
Alex Klein | cd03a5e | 2021-10-18 13:23:47 -0600 | [diff] [blame] | 19 | from chromite.lib import osutils |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 20 | from chromite.lib import toolchain as toolchain_lib |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 21 | from chromite.lib import toolchain_util |
Greg Edelston | dae510a | 2023-06-30 15:25:36 -0600 | [diff] [blame] | 22 | from chromite.service import toolchain as toolchain_service |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 23 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 24 | |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 25 | # pylint: disable=protected-access |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 26 | |
Trent Apted | 39e74d3 | 2023-09-04 11:24:40 +1000 | [diff] [blame] | 27 | BuilderConfig = builder_config_pb2.BuilderConfig |
| 28 | |
Tiancong Wang | ba2a1c2 | 2021-01-19 10:45:06 -0800 | [diff] [blame] | 29 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 30 | class UpdateEbuildWithAFDOArtifactsTest( |
| 31 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 32 | ): |
| 33 | """Unittests for UpdateEbuildWithAFDOArtifacts.""" |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 34 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | @staticmethod |
| 36 | def mock_die(message, *args): |
| 37 | raise cros_build_lib.DieSystemExit(message % args) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 38 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | def setUp(self): |
| 40 | self.board = "board" |
| 41 | self.response = toolchain_pb2.VerifyAFDOArtifactsResponse() |
| 42 | self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO |
| 43 | self.PatchObject(cros_build_lib, "Die", new=self.mock_die) |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | def _GetRequest(self, build_target=None, artifact_type=None): |
| 46 | return toolchain_pb2.VerifyAFDOArtifactsRequest( |
| 47 | build_target={"name": build_target}, |
| 48 | artifact_type=artifact_type, |
| 49 | ) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 50 | |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 51 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 52 | class PrepareForBuildTest( |
| 53 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 54 | ): |
| 55 | """Unittests for PrepareForBuild.""" |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 56 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | def setUp(self): |
| 58 | self.response = toolchain_pb2.PrepareForToolchainBuildResponse() |
| 59 | self.prep = self.PatchObject( |
| 60 | toolchain_util, |
| 61 | "PrepareForBuild", |
| 62 | return_value=toolchain_util.PrepareForBuildReturn.NEEDED, |
| 63 | ) |
| 64 | self.bundle = self.PatchObject( |
| 65 | toolchain_util, "BundleArtifacts", return_value=[] |
| 66 | ) |
| 67 | self.PatchObject( |
| 68 | toolchain, |
| 69 | "_TOOLCHAIN_ARTIFACT_HANDLERS", |
| 70 | { |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 71 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE: ( |
| 72 | toolchain._Handlers( |
| 73 | "UnverifiedChromeLlvmOrderfile", self.prep, self.bundle |
| 74 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | ), |
| 76 | }, |
| 77 | ) |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 78 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | def _GetRequest( |
| 80 | self, artifact_types=None, input_artifacts=None, additional_args=None |
| 81 | ): |
| 82 | chroot = common_pb2.Chroot(path=str(self.tempdir)) |
| 83 | sysroot = sysroot_pb2.Sysroot( |
| 84 | path="/build/board", |
| 85 | build_target=common_pb2.BuildTarget(name="board"), |
| 86 | ) |
| 87 | return toolchain_pb2.PrepareForToolchainBuildRequest( |
| 88 | artifact_types=artifact_types, |
| 89 | chroot=chroot, |
| 90 | sysroot=sysroot, |
| 91 | input_artifacts=input_artifacts, |
| 92 | additional_args=additional_args, |
| 93 | ) |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 94 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | def testRaisesForUnknown(self): |
| 96 | request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES]) |
| 97 | self.assertRaises( |
| 98 | KeyError, |
| 99 | toolchain.PrepareForBuild, |
| 100 | request, |
| 101 | self.response, |
| 102 | self.api_config, |
| 103 | ) |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 104 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 105 | def testAcceptsNone(self): |
| 106 | request = toolchain_pb2.PrepareForToolchainBuildRequest( |
| 107 | artifact_types=[ |
| 108 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE |
| 109 | ], |
| 110 | chroot=None, |
| 111 | sysroot=None, |
| 112 | ) |
| 113 | toolchain.PrepareForBuild(request, self.response, self.api_config) |
| 114 | self.prep.assert_called_once_with( |
| 115 | "UnverifiedChromeLlvmOrderfile", None, "", "", {}, {} |
| 116 | ) |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | def testHandlesUnknownInputArtifacts(self): |
| 119 | request = toolchain_pb2.PrepareForToolchainBuildRequest( |
| 120 | artifact_types=[ |
| 121 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE |
| 122 | ], |
| 123 | chroot=None, |
| 124 | sysroot=None, |
| 125 | input_artifacts=[ |
| 126 | BuilderConfig.Artifacts.InputArtifactInfo( |
| 127 | input_artifact_type=BuilderConfig.Artifacts.IMAGE_ZIP, |
| 128 | input_artifact_gs_locations=["path1"], |
| 129 | ), |
| 130 | ], |
| 131 | ) |
| 132 | toolchain.PrepareForBuild(request, self.response, self.api_config) |
| 133 | self.prep.assert_called_once_with( |
| 134 | "UnverifiedChromeLlvmOrderfile", None, "", "", {}, {} |
| 135 | ) |
LaMont Jones | 45ca6c4 | 2020-02-05 09:39:09 -0700 | [diff] [blame] | 136 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 137 | def testPassesProfileInfo(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 138 | # pylint: disable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | request = toolchain_pb2.PrepareForToolchainBuildRequest( |
| 140 | artifact_types=[ |
| 141 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE |
| 142 | ], |
| 143 | chroot=None, |
| 144 | sysroot=None, |
| 145 | input_artifacts=[ |
| 146 | BuilderConfig.Artifacts.InputArtifactInfo( |
| 147 | input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE, |
| 148 | input_artifact_gs_locations=["path1", "path2"], |
| 149 | ), |
| 150 | BuilderConfig.Artifacts.InputArtifactInfo( |
| 151 | input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE, |
| 152 | input_artifact_gs_locations=["path3"], |
| 153 | ), |
| 154 | ], |
| 155 | profile_info=common_pb2.ArtifactProfileInfo( |
Denis Nikitin | 62e6986 | 2023-02-13 23:37:00 -0800 | [diff] [blame] | 156 | chrome_cwp_profile="CWPVERSION", |
| 157 | arch="amd64", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 158 | ), |
| 159 | ) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 160 | # pylint: enable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 161 | toolchain.PrepareForBuild(request, self.response, self.api_config) |
| 162 | self.prep.assert_called_once_with( |
| 163 | "UnverifiedChromeLlvmOrderfile", |
| 164 | None, |
| 165 | "", |
| 166 | "", |
| 167 | { |
| 168 | "UnverifiedChromeLlvmOrderfile": [ |
| 169 | "gs://path1", |
| 170 | "gs://path2", |
| 171 | "gs://path3", |
| 172 | ], |
| 173 | }, |
Denis Nikitin | 62e6986 | 2023-02-13 23:37:00 -0800 | [diff] [blame] | 174 | { |
| 175 | "chrome_cwp_profile": "CWPVERSION", |
| 176 | "arch": "amd64", |
| 177 | }, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | ) |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 179 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 180 | def testPassesProfileInfoAfdoRelease(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 181 | # pylint: disable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 182 | request = toolchain_pb2.PrepareForToolchainBuildRequest( |
| 183 | artifact_types=[ |
| 184 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE |
| 185 | ], |
| 186 | chroot=None, |
| 187 | sysroot=None, |
| 188 | input_artifacts=[ |
| 189 | BuilderConfig.Artifacts.InputArtifactInfo( |
| 190 | input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE, |
| 191 | input_artifact_gs_locations=["path1", "path2"], |
| 192 | ), |
| 193 | BuilderConfig.Artifacts.InputArtifactInfo( |
| 194 | input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE, |
| 195 | input_artifact_gs_locations=["path3"], |
| 196 | ), |
| 197 | ], |
| 198 | profile_info=common_pb2.ArtifactProfileInfo( |
| 199 | afdo_release=common_pb2.AfdoRelease( |
Denis Nikitin | 62e6986 | 2023-02-13 23:37:00 -0800 | [diff] [blame] | 200 | chrome_cwp_profile="CWPVERSION", |
| 201 | image_build_id=1234, |
| 202 | ), |
| 203 | arch="amd64", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | ), |
| 205 | ) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 206 | # pylint: enable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 207 | toolchain.PrepareForBuild(request, self.response, self.api_config) |
| 208 | self.prep.assert_called_once_with( |
| 209 | "UnverifiedChromeLlvmOrderfile", |
| 210 | None, |
| 211 | "", |
| 212 | "", |
| 213 | { |
| 214 | "UnverifiedChromeLlvmOrderfile": [ |
| 215 | "gs://path1", |
| 216 | "gs://path2", |
| 217 | "gs://path3", |
| 218 | ], |
| 219 | }, |
Denis Nikitin | 62e6986 | 2023-02-13 23:37:00 -0800 | [diff] [blame] | 220 | { |
| 221 | "chrome_cwp_profile": "CWPVERSION", |
| 222 | "image_build_id": 1234, |
| 223 | "arch": "amd64", |
| 224 | }, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 225 | ) |
LaMont Jones | e782167 | 2020-04-09 08:56:26 -0600 | [diff] [blame] | 226 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 227 | def testHandlesDuplicateInputArtifacts(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 228 | # pylint: disable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 229 | request = toolchain_pb2.PrepareForToolchainBuildRequest( |
| 230 | artifact_types=[ |
| 231 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE |
| 232 | ], |
| 233 | chroot=None, |
| 234 | sysroot=None, |
| 235 | input_artifacts=[ |
| 236 | BuilderConfig.Artifacts.InputArtifactInfo( |
| 237 | input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE, |
| 238 | input_artifact_gs_locations=["path1", "path2"], |
| 239 | ), |
| 240 | BuilderConfig.Artifacts.InputArtifactInfo( |
| 241 | input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE, |
| 242 | input_artifact_gs_locations=["path3"], |
| 243 | ), |
| 244 | ], |
| 245 | ) |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 246 | # pylint: enable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 247 | toolchain.PrepareForBuild(request, self.response, self.api_config) |
| 248 | self.prep.assert_called_once_with( |
| 249 | "UnverifiedChromeLlvmOrderfile", |
| 250 | None, |
| 251 | "", |
| 252 | "", |
| 253 | { |
| 254 | "UnverifiedChromeLlvmOrderfile": [ |
| 255 | "gs://path1", |
| 256 | "gs://path2", |
| 257 | "gs://path3", |
| 258 | ], |
| 259 | }, |
| 260 | {}, |
| 261 | ) |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 262 | |
| 263 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 264 | class BundleToolchainTest( |
| 265 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 266 | ): |
| 267 | """Unittests for BundleToolchain.""" |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 268 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 269 | def setUp(self): |
| 270 | self.response = toolchain_pb2.BundleToolchainResponse() |
| 271 | self.prep = self.PatchObject( |
| 272 | toolchain_util, |
| 273 | "PrepareForBuild", |
| 274 | return_value=toolchain_util.PrepareForBuildReturn.NEEDED, |
| 275 | ) |
| 276 | self.bundle = self.PatchObject( |
| 277 | toolchain_util, "BundleArtifacts", return_value=[] |
| 278 | ) |
| 279 | self.PatchObject( |
| 280 | toolchain, |
| 281 | "_TOOLCHAIN_ARTIFACT_HANDLERS", |
| 282 | { |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 283 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE: ( |
| 284 | toolchain._Handlers( |
| 285 | "UnverifiedChromeLlvmOrderfile", self.prep, self.bundle |
| 286 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 287 | ), |
| 288 | }, |
| 289 | ) |
| 290 | osutils.WriteFile(os.path.join(self.tempdir, "artifact.txt"), "test") |
| 291 | osutils.Touch(os.path.join(self.tempdir, "empty")) |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 292 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 293 | def _GetRequest(self, artifact_types=None): |
| 294 | chroot = common_pb2.Chroot(path=str(self.tempdir)) |
| 295 | sysroot = sysroot_pb2.Sysroot( |
| 296 | path="/build/board", |
| 297 | build_target=common_pb2.BuildTarget(name="board"), |
| 298 | ) |
| 299 | return toolchain_pb2.BundleToolchainRequest( |
| 300 | chroot=chroot, |
| 301 | sysroot=sysroot, |
| 302 | output_dir=str(self.tempdir), |
| 303 | artifact_types=artifact_types, |
| 304 | ) |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 305 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 306 | def testRaisesForUnknown(self): |
| 307 | request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES]) |
| 308 | self.assertEqual( |
| 309 | controller.RETURN_CODE_UNRECOVERABLE, |
| 310 | toolchain.BundleArtifacts(request, self.response, self.api_config), |
| 311 | ) |
Michael Mortensen | 3232ab3 | 2020-01-05 19:14:36 -0700 | [diff] [blame] | 312 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 313 | def testValidateOnly(self): |
| 314 | """Sanity check that a validate only call does not execute any logic.""" |
| 315 | request = self._GetRequest( |
| 316 | [BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE] |
| 317 | ) |
| 318 | toolchain.BundleArtifacts( |
| 319 | request, self.response, self.validate_only_config |
| 320 | ) |
| 321 | self.bundle.assert_not_called() |
LaMont Jones | 5d2edcb | 2019-12-23 11:32:03 -0700 | [diff] [blame] | 322 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 323 | def testSetsArtifactsInfo(self): |
| 324 | request = self._GetRequest( |
| 325 | [BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE] |
| 326 | ) |
| 327 | self.bundle.return_value = ["artifact.txt", "empty", "does_not_exist"] |
| 328 | toolchain.BundleArtifacts(request, self.response, self.api_config) |
| 329 | self.assertEqual(1, len(self.response.artifacts_info)) |
| 330 | self.assertEqual( |
| 331 | self.response.artifacts_info[0], |
| 332 | toolchain_pb2.ArtifactInfo( |
| 333 | artifact_type=( |
| 334 | BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE |
| 335 | ), |
| 336 | artifacts=[ |
| 337 | artifacts_pb2.Artifact(path=self.bundle.return_value[0]) |
| 338 | ], |
| 339 | ), |
| 340 | ) |
Tiancong Wang | d521413 | 2021-01-12 10:43:57 -0800 | [diff] [blame] | 341 | |
| 342 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 343 | class GetUpdatedFilesTest( |
| 344 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 345 | ): |
| 346 | """Unittests for GetUpdatedFiles.""" |
Tiancong Wang | d521413 | 2021-01-12 10:43:57 -0800 | [diff] [blame] | 347 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 348 | def setUp(self): |
| 349 | self.response = toolchain_pb2.GetUpdatedFilesResponse() |
| 350 | self.artifact_path = "/any/path/to/metadata" |
Denis Nikitin | 62e6986 | 2023-02-13 23:37:00 -0800 | [diff] [blame] | 351 | self.profile_info = common_pb2.ArtifactProfileInfo( |
| 352 | kernel_version="4.4", arch="amd64" |
| 353 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 354 | self.update = self.PatchObject( |
| 355 | toolchain_util, "GetUpdatedFiles", return_value=([], "") |
| 356 | ) |
Tiancong Wang | d521413 | 2021-01-12 10:43:57 -0800 | [diff] [blame] | 357 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 358 | def _GetRequest(self, uploaded_artifacts): |
| 359 | uploaded = [] |
| 360 | for artifact_type, artifact_path, profile_info in uploaded_artifacts: |
| 361 | uploaded.append( |
| 362 | toolchain_pb2.GetUpdatedFilesRequest.UploadedArtifacts( |
| 363 | artifact_info=toolchain_pb2.ArtifactInfo( |
| 364 | artifact_type=artifact_type, |
| 365 | artifacts=[artifacts_pb2.Artifact(path=artifact_path)], |
| 366 | ), |
| 367 | profile_info=profile_info, |
| 368 | ) |
| 369 | ) |
| 370 | return toolchain_pb2.GetUpdatedFilesRequest(uploaded_artifacts=uploaded) |
Tiancong Wang | d521413 | 2021-01-12 10:43:57 -0800 | [diff] [blame] | 371 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 372 | def testRaisesForUnknown(self): |
| 373 | request = self._GetRequest( |
| 374 | [ |
| 375 | ( |
| 376 | BuilderConfig.Artifacts.UNVERIFIED_KERNEL_CWP_AFDO_FILE, |
| 377 | self.artifact_path, |
| 378 | self.profile_info, |
| 379 | ) |
| 380 | ] |
| 381 | ) |
| 382 | self.assertEqual( |
| 383 | controller.RETURN_CODE_UNRECOVERABLE, |
| 384 | toolchain.GetUpdatedFiles(request, self.response, self.api_config), |
| 385 | ) |
Tiancong Wang | d521413 | 2021-01-12 10:43:57 -0800 | [diff] [blame] | 386 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 387 | def testValidateOnly(self): |
| 388 | """Sanity check that a validate only call does not execute any logic.""" |
| 389 | request = self._GetRequest( |
| 390 | [ |
| 391 | ( |
| 392 | BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE, |
| 393 | self.artifact_path, |
| 394 | self.profile_info, |
| 395 | ) |
| 396 | ] |
| 397 | ) |
| 398 | toolchain.GetUpdatedFiles( |
| 399 | request, self.response, self.validate_only_config |
| 400 | ) |
Tiancong Wang | d521413 | 2021-01-12 10:43:57 -0800 | [diff] [blame] | 401 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 402 | def testUpdateSuccess(self): |
| 403 | updated_file = "/path/to/updated_file" |
| 404 | self.update.return_value = ([updated_file], "Commit Message") |
| 405 | request = self._GetRequest( |
| 406 | [ |
| 407 | ( |
| 408 | BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE, |
| 409 | self.artifact_path, |
| 410 | self.profile_info, |
| 411 | ) |
| 412 | ] |
| 413 | ) |
| 414 | toolchain.GetUpdatedFiles(request, self.response, self.api_config) |
| 415 | print(self.response.updated_files) |
| 416 | self.assertEqual(len(self.response.updated_files), 1) |
| 417 | self.assertEqual( |
| 418 | self.response.updated_files[0], |
| 419 | toolchain_pb2.GetUpdatedFilesResponse.UpdatedFile( |
| 420 | path=updated_file |
| 421 | ), |
| 422 | ) |
| 423 | self.assertIn("Commit Message", self.response.commit_message) |
| 424 | self.assertEqual(len(self.response.commit_footer), 0) |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 425 | |
| 426 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 427 | class GetToolchainsForBoardTest( |
| 428 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 429 | ): |
| 430 | """Unittests for GetToolchainsForBoard.""" |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 431 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 432 | def setUp(self): |
| 433 | self.response = toolchain_pb2.ToolchainsResponse() |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 434 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 435 | def _GetRequest(self, board="betty-pi-arc"): |
| 436 | return toolchain_pb2.ToolchainsRequest(board=board) |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 437 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 438 | def testValidateOnly(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 439 | """Verify a validate-only call does not execute any logic.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 440 | request = self._GetRequest() |
| 441 | toolchain.GetToolchainsForBoard( |
| 442 | request, self.response, self.validate_only_config |
| 443 | ) |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 444 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 445 | def testUpdateSuccess(self): |
| 446 | toolchain_info = { |
| 447 | "default-a": {"default": True}, |
| 448 | "default-b": {"default": True}, |
| 449 | "nondefault-a": {"default": False}, |
| 450 | "nondefault-b": {"default": False}, |
| 451 | } |
| 452 | self.PatchObject( |
| 453 | toolchain_lib, "GetToolchainsForBoard", return_value=toolchain_info |
| 454 | ) |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 455 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 456 | request = self._GetRequest() |
| 457 | toolchain.GetToolchainsForBoard(request, self.response, self.api_config) |
Jack Neus | 4ee7b1d | 2022-06-27 19:54:18 +0000 | [diff] [blame] | 458 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 459 | self.assertEqual( |
| 460 | self.response.default_toolchains, ["default-a", "default-b"] |
| 461 | ) |
| 462 | self.assertEqual( |
| 463 | self.response.nondefault_toolchains, |
| 464 | ["nondefault-a", "nondefault-b"], |
| 465 | ) |
Greg Edelston | dae510a | 2023-06-30 15:25:36 -0600 | [diff] [blame] | 466 | |
| 467 | |
Trent Apted | 39e74d3 | 2023-09-04 11:24:40 +1000 | [diff] [blame] | 468 | class SetupToolchainsTest( |
| 469 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 470 | ): |
Greg Edelston | dae510a | 2023-06-30 15:25:36 -0600 | [diff] [blame] | 471 | """Unit tests for ToolchainService.SetupToolchains.""" |
Trent Apted | 39e74d3 | 2023-09-04 11:24:40 +1000 | [diff] [blame] | 472 | |
Greg Edelston | dae510a | 2023-06-30 15:25:36 -0600 | [diff] [blame] | 473 | def setUp(self) -> None: |
| 474 | self.response = toolchain_pb2.SetupToolchainsResponse() |
Trent Apted | 39e74d3 | 2023-09-04 11:24:40 +1000 | [diff] [blame] | 475 | self.chroot = common_pb2.Chroot(path="/path/to/chroot") |
Greg Edelston | dae510a | 2023-06-30 15:25:36 -0600 | [diff] [blame] | 476 | |
| 477 | def test_outside_chroot(self) -> None: |
| 478 | """Test the behavior if run from outside the chroot.""" |
| 479 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 480 | self.PatchObject(toolchain_service, "setup_toolchains") |
| 481 | request = toolchain_pb2.SetupToolchainsRequest(chroot=self.chroot) |
| 482 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 483 | toolchain.SetupToolchains(request, self.response, self.api_config) |
| 484 | |
| 485 | def test_with_empty_request(self) -> None: |
| 486 | """Test the behavior if the request object is empty.""" |
| 487 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True) |
| 488 | service_mock = self.PatchObject(toolchain_service, "setup_toolchains") |
| 489 | request = toolchain_pb2.SetupToolchainsRequest(chroot=self.chroot) |
| 490 | toolchain.SetupToolchains( |
| 491 | request, |
| 492 | self.response, |
| 493 | self.api_config, |
| 494 | ) |
| 495 | service_mock.assert_called_once_with(include_boards=[]) |
| 496 | |
| 497 | def test_with_boards(self) -> None: |
| 498 | """Test the behavior if the request object specifies boards.""" |
| 499 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True) |
| 500 | service_mock = self.PatchObject(toolchain_service, "setup_toolchains") |
| 501 | request = toolchain_pb2.SetupToolchainsRequest( |
| 502 | chroot=self.chroot, |
| 503 | boards=[ |
| 504 | common_pb2.BuildTarget(name="amd64-generic"), |
| 505 | common_pb2.BuildTarget(name="arm-generic"), |
Trent Apted | 39e74d3 | 2023-09-04 11:24:40 +1000 | [diff] [blame] | 506 | ], |
| 507 | ) |
Greg Edelston | dae510a | 2023-06-30 15:25:36 -0600 | [diff] [blame] | 508 | toolchain.SetupToolchains( |
| 509 | request, |
| 510 | self.response, |
| 511 | self.api_config, |
| 512 | ) |
| 513 | service_mock.assert_called_once_with( |
Trent Apted | 39e74d3 | 2023-09-04 11:24:40 +1000 | [diff] [blame] | 514 | include_boards=["amd64-generic", "arm-generic"], |
| 515 | ) |