blob: 4b50564d8edcc3ebd493dca3cd3e4e23eb26e274 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Tiancong Wangaf050172019-07-10 11:52:03 -07002# 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 Kleincd03a5e2021-10-18 13:23:47 -06007import os
8
Alex Klein231d2da2019-07-22 16:44:45 -06009from chromite.api import api_config
LaMont Jones5d2edcb2019-12-23 11:32:03 -070010from chromite.api import controller
Tiancong Wangaf050172019-07-10 11:52:03 -070011from chromite.api.controller import toolchain
LaMont Jones5d2edcb2019-12-23 11:32:03 -070012from chromite.api.gen.chromite.api import artifacts_pb2
LaMont Jones4579e8c2019-12-06 14:20:37 -070013from chromite.api.gen.chromite.api import sysroot_pb2
Tiancong Wangaf050172019-07-10 11:52:03 -070014from chromite.api.gen.chromite.api import toolchain_pb2
Trent Apted39e74d32023-09-04 11:24:40 +100015from chromite.api.gen.chromiumos import builder_config_pb2
LaMont Jones4579e8c2019-12-06 14:20:37 -070016from chromite.api.gen.chromiumos import common_pb2
Tiancong Wang24a3df72019-08-20 15:48:51 -070017from chromite.lib import cros_build_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070018from chromite.lib import cros_test_lib
Alex Kleincd03a5e2021-10-18 13:23:47 -060019from chromite.lib import osutils
Jack Neus4ee7b1d2022-06-27 19:54:18 +000020from chromite.lib import toolchain as toolchain_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070021from chromite.lib import toolchain_util
Greg Edelstondae510a2023-06-30 15:25:36 -060022from chromite.service import toolchain as toolchain_service
Tiancong Wangaf050172019-07-10 11:52:03 -070023
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040024
LaMont Jones5d2edcb2019-12-23 11:32:03 -070025# pylint: disable=protected-access
Tiancong Wangaf050172019-07-10 11:52:03 -070026
Trent Apted39e74d32023-09-04 11:24:40 +100027BuilderConfig = builder_config_pb2.BuilderConfig
28
Tiancong Wangba2a1c22021-01-19 10:45:06 -080029
Alex Klein1699fab2022-09-08 08:46:06 -060030class UpdateEbuildWithAFDOArtifactsTest(
31 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
32):
33 """Unittests for UpdateEbuildWithAFDOArtifacts."""
Tiancong Wang24a3df72019-08-20 15:48:51 -070034
Alex Klein1699fab2022-09-08 08:46:06 -060035 @staticmethod
36 def mock_die(message, *args):
37 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -070038
Alex Klein1699fab2022-09-08 08:46:06 -060039 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 Wang24a3df72019-08-20 15:48:51 -070044
Alex Klein1699fab2022-09-08 08:46:06 -060045 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 Wangaf050172019-07-10 11:52:03 -070050
LaMont Jonesb20b3d92019-11-23 11:47:48 -070051
Alex Klein1699fab2022-09-08 08:46:06 -060052class PrepareForBuildTest(
53 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
54):
55 """Unittests for PrepareForBuild."""
LaMont Jonesb20b3d92019-11-23 11:47:48 -070056
Alex Klein1699fab2022-09-08 08:46:06 -060057 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 {
George Burgess IV1e584862023-09-12 13:28:05 -060071 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE: (
Alex Kleinab87ceb2023-01-24 12:00:51 -070072 toolchain._Handlers(
George Burgess IV1e584862023-09-12 13:28:05 -060073 "UnverifiedChromeBenchmarkAfdoFile",
74 self.prep,
75 self.bundle,
Alex Kleinab87ceb2023-01-24 12:00:51 -070076 )
Alex Klein1699fab2022-09-08 08:46:06 -060077 ),
78 },
79 )
LaMont Jonesb20b3d92019-11-23 11:47:48 -070080
Alex Klein1699fab2022-09-08 08:46:06 -060081 def _GetRequest(
82 self, artifact_types=None, input_artifacts=None, additional_args=None
83 ):
84 chroot = common_pb2.Chroot(path=str(self.tempdir))
85 sysroot = sysroot_pb2.Sysroot(
86 path="/build/board",
87 build_target=common_pb2.BuildTarget(name="board"),
88 )
89 return toolchain_pb2.PrepareForToolchainBuildRequest(
90 artifact_types=artifact_types,
91 chroot=chroot,
92 sysroot=sysroot,
93 input_artifacts=input_artifacts,
94 additional_args=additional_args,
95 )
LaMont Jonesb20b3d92019-11-23 11:47:48 -070096
Alex Klein1699fab2022-09-08 08:46:06 -060097 def testRaisesForUnknown(self):
98 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
99 self.assertRaises(
100 KeyError,
101 toolchain.PrepareForBuild,
102 request,
103 self.response,
104 self.api_config,
105 )
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700106
Alex Klein1699fab2022-09-08 08:46:06 -0600107 def testAcceptsNone(self):
108 request = toolchain_pb2.PrepareForToolchainBuildRequest(
109 artifact_types=[
George Burgess IV1e584862023-09-12 13:28:05 -0600110 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE
Alex Klein1699fab2022-09-08 08:46:06 -0600111 ],
112 chroot=None,
113 sysroot=None,
114 )
115 toolchain.PrepareForBuild(request, self.response, self.api_config)
116 self.prep.assert_called_once_with(
George Burgess IV1e584862023-09-12 13:28:05 -0600117 "UnverifiedChromeBenchmarkAfdoFile", None, "", "", {}, {}
Alex Klein1699fab2022-09-08 08:46:06 -0600118 )
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 def testHandlesUnknownInputArtifacts(self):
121 request = toolchain_pb2.PrepareForToolchainBuildRequest(
122 artifact_types=[
George Burgess IV1e584862023-09-12 13:28:05 -0600123 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE
Alex Klein1699fab2022-09-08 08:46:06 -0600124 ],
125 chroot=None,
126 sysroot=None,
127 input_artifacts=[
128 BuilderConfig.Artifacts.InputArtifactInfo(
129 input_artifact_type=BuilderConfig.Artifacts.IMAGE_ZIP,
130 input_artifact_gs_locations=["path1"],
131 ),
132 ],
133 )
134 toolchain.PrepareForBuild(request, self.response, self.api_config)
135 self.prep.assert_called_once_with(
George Burgess IV1e584862023-09-12 13:28:05 -0600136 "UnverifiedChromeBenchmarkAfdoFile", None, "", "", {}, {}
Alex Klein1699fab2022-09-08 08:46:06 -0600137 )
LaMont Jones45ca6c42020-02-05 09:39:09 -0700138
Alex Klein1699fab2022-09-08 08:46:06 -0600139 def testPassesProfileInfo(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700140 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -0600141 request = toolchain_pb2.PrepareForToolchainBuildRequest(
142 artifact_types=[
George Burgess IV1e584862023-09-12 13:28:05 -0600143 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE
Alex Klein1699fab2022-09-08 08:46:06 -0600144 ],
145 chroot=None,
146 sysroot=None,
147 input_artifacts=[
148 BuilderConfig.Artifacts.InputArtifactInfo(
George Burgess IV1e584862023-09-12 13:28:05 -0600149 input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE,
Alex Klein1699fab2022-09-08 08:46:06 -0600150 input_artifact_gs_locations=["path1", "path2"],
151 ),
152 BuilderConfig.Artifacts.InputArtifactInfo(
George Burgess IV1e584862023-09-12 13:28:05 -0600153 input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE,
Alex Klein1699fab2022-09-08 08:46:06 -0600154 input_artifact_gs_locations=["path3"],
155 ),
156 ],
157 profile_info=common_pb2.ArtifactProfileInfo(
Denis Nikitin62e69862023-02-13 23:37:00 -0800158 chrome_cwp_profile="CWPVERSION",
159 arch="amd64",
Alex Klein1699fab2022-09-08 08:46:06 -0600160 ),
161 )
Alex Kleinab87ceb2023-01-24 12:00:51 -0700162 # pylint: enable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -0600163 toolchain.PrepareForBuild(request, self.response, self.api_config)
164 self.prep.assert_called_once_with(
George Burgess IV1e584862023-09-12 13:28:05 -0600165 "UnverifiedChromeBenchmarkAfdoFile",
Alex Klein1699fab2022-09-08 08:46:06 -0600166 None,
167 "",
168 "",
169 {
George Burgess IV1e584862023-09-12 13:28:05 -0600170 "UnverifiedChromeBenchmarkAfdoFile": [
Alex Klein1699fab2022-09-08 08:46:06 -0600171 "gs://path1",
172 "gs://path2",
173 "gs://path3",
174 ],
175 },
Denis Nikitin62e69862023-02-13 23:37:00 -0800176 {
177 "chrome_cwp_profile": "CWPVERSION",
178 "arch": "amd64",
179 },
Alex Klein1699fab2022-09-08 08:46:06 -0600180 )
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700181
Alex Klein1699fab2022-09-08 08:46:06 -0600182 def testPassesProfileInfoAfdoRelease(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700183 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -0600184 request = toolchain_pb2.PrepareForToolchainBuildRequest(
185 artifact_types=[
George Burgess IV1e584862023-09-12 13:28:05 -0600186 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE
Alex Klein1699fab2022-09-08 08:46:06 -0600187 ],
188 chroot=None,
189 sysroot=None,
190 input_artifacts=[
191 BuilderConfig.Artifacts.InputArtifactInfo(
George Burgess IV1e584862023-09-12 13:28:05 -0600192 input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE,
Alex Klein1699fab2022-09-08 08:46:06 -0600193 input_artifact_gs_locations=["path1", "path2"],
194 ),
195 BuilderConfig.Artifacts.InputArtifactInfo(
George Burgess IV1e584862023-09-12 13:28:05 -0600196 input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE,
Alex Klein1699fab2022-09-08 08:46:06 -0600197 input_artifact_gs_locations=["path3"],
198 ),
199 ],
200 profile_info=common_pb2.ArtifactProfileInfo(
201 afdo_release=common_pb2.AfdoRelease(
Denis Nikitin62e69862023-02-13 23:37:00 -0800202 chrome_cwp_profile="CWPVERSION",
203 image_build_id=1234,
204 ),
205 arch="amd64",
Alex Klein1699fab2022-09-08 08:46:06 -0600206 ),
207 )
Alex Kleinab87ceb2023-01-24 12:00:51 -0700208 # pylint: enable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -0600209 toolchain.PrepareForBuild(request, self.response, self.api_config)
210 self.prep.assert_called_once_with(
George Burgess IV1e584862023-09-12 13:28:05 -0600211 "UnverifiedChromeBenchmarkAfdoFile",
Alex Klein1699fab2022-09-08 08:46:06 -0600212 None,
213 "",
214 "",
215 {
George Burgess IV1e584862023-09-12 13:28:05 -0600216 "UnverifiedChromeBenchmarkAfdoFile": [
Alex Klein1699fab2022-09-08 08:46:06 -0600217 "gs://path1",
218 "gs://path2",
219 "gs://path3",
220 ],
221 },
Denis Nikitin62e69862023-02-13 23:37:00 -0800222 {
223 "chrome_cwp_profile": "CWPVERSION",
224 "image_build_id": 1234,
225 "arch": "amd64",
226 },
Alex Klein1699fab2022-09-08 08:46:06 -0600227 )
LaMont Jonese7821672020-04-09 08:56:26 -0600228
Alex Klein1699fab2022-09-08 08:46:06 -0600229 def testHandlesDuplicateInputArtifacts(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700230 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -0600231 request = toolchain_pb2.PrepareForToolchainBuildRequest(
232 artifact_types=[
George Burgess IV1e584862023-09-12 13:28:05 -0600233 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE
Alex Klein1699fab2022-09-08 08:46:06 -0600234 ],
235 chroot=None,
236 sysroot=None,
237 input_artifacts=[
238 BuilderConfig.Artifacts.InputArtifactInfo(
George Burgess IV1e584862023-09-12 13:28:05 -0600239 input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE,
Alex Klein1699fab2022-09-08 08:46:06 -0600240 input_artifact_gs_locations=["path1", "path2"],
241 ),
242 BuilderConfig.Artifacts.InputArtifactInfo(
George Burgess IV1e584862023-09-12 13:28:05 -0600243 input_artifact_type=BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE,
Alex Klein1699fab2022-09-08 08:46:06 -0600244 input_artifact_gs_locations=["path3"],
245 ),
246 ],
247 )
Alex Kleinab87ceb2023-01-24 12:00:51 -0700248 # pylint: enable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -0600249 toolchain.PrepareForBuild(request, self.response, self.api_config)
250 self.prep.assert_called_once_with(
George Burgess IV1e584862023-09-12 13:28:05 -0600251 "UnverifiedChromeBenchmarkAfdoFile",
Alex Klein1699fab2022-09-08 08:46:06 -0600252 None,
253 "",
254 "",
255 {
George Burgess IV1e584862023-09-12 13:28:05 -0600256 "UnverifiedChromeBenchmarkAfdoFile": [
Alex Klein1699fab2022-09-08 08:46:06 -0600257 "gs://path1",
258 "gs://path2",
259 "gs://path3",
260 ],
261 },
262 {},
263 )
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700264
265
Alex Klein1699fab2022-09-08 08:46:06 -0600266class BundleToolchainTest(
267 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
268):
269 """Unittests for BundleToolchain."""
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700270
Alex Klein1699fab2022-09-08 08:46:06 -0600271 def setUp(self):
272 self.response = toolchain_pb2.BundleToolchainResponse()
273 self.prep = self.PatchObject(
274 toolchain_util,
275 "PrepareForBuild",
276 return_value=toolchain_util.PrepareForBuildReturn.NEEDED,
277 )
278 self.bundle = self.PatchObject(
279 toolchain_util, "BundleArtifacts", return_value=[]
280 )
281 self.PatchObject(
282 toolchain,
283 "_TOOLCHAIN_ARTIFACT_HANDLERS",
284 {
George Burgess IV1e584862023-09-12 13:28:05 -0600285 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE: (
Alex Kleinab87ceb2023-01-24 12:00:51 -0700286 toolchain._Handlers(
George Burgess IV1e584862023-09-12 13:28:05 -0600287 "UnverifiedChromeBenchmarkAfdoFile",
288 self.prep,
289 self.bundle,
Alex Kleinab87ceb2023-01-24 12:00:51 -0700290 )
Alex Klein1699fab2022-09-08 08:46:06 -0600291 ),
292 },
293 )
294 osutils.WriteFile(os.path.join(self.tempdir, "artifact.txt"), "test")
295 osutils.Touch(os.path.join(self.tempdir, "empty"))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700296
Alex Klein1699fab2022-09-08 08:46:06 -0600297 def _GetRequest(self, artifact_types=None):
298 chroot = common_pb2.Chroot(path=str(self.tempdir))
299 sysroot = sysroot_pb2.Sysroot(
300 path="/build/board",
301 build_target=common_pb2.BuildTarget(name="board"),
302 )
303 return toolchain_pb2.BundleToolchainRequest(
304 chroot=chroot,
305 sysroot=sysroot,
306 output_dir=str(self.tempdir),
307 artifact_types=artifact_types,
308 )
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700309
Alex Klein1699fab2022-09-08 08:46:06 -0600310 def testRaisesForUnknown(self):
311 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
312 self.assertEqual(
313 controller.RETURN_CODE_UNRECOVERABLE,
314 toolchain.BundleArtifacts(request, self.response, self.api_config),
315 )
Michael Mortensen3232ab32020-01-05 19:14:36 -0700316
Alex Klein1699fab2022-09-08 08:46:06 -0600317 def testValidateOnly(self):
318 """Sanity check that a validate only call does not execute any logic."""
319 request = self._GetRequest(
George Burgess IV1e584862023-09-12 13:28:05 -0600320 [BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE]
Alex Klein1699fab2022-09-08 08:46:06 -0600321 )
322 toolchain.BundleArtifacts(
323 request, self.response, self.validate_only_config
324 )
325 self.bundle.assert_not_called()
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700326
Alex Klein1699fab2022-09-08 08:46:06 -0600327 def testSetsArtifactsInfo(self):
George Burgess IV1e584862023-09-12 13:28:05 -0600328 artifact_type = (
329 BuilderConfig.Artifacts.UNVERIFIED_CHROME_BENCHMARK_AFDO_FILE
Alex Klein1699fab2022-09-08 08:46:06 -0600330 )
George Burgess IV1e584862023-09-12 13:28:05 -0600331 request = self._GetRequest([artifact_type])
Alex Klein1699fab2022-09-08 08:46:06 -0600332 self.bundle.return_value = ["artifact.txt", "empty", "does_not_exist"]
333 toolchain.BundleArtifacts(request, self.response, self.api_config)
334 self.assertEqual(1, len(self.response.artifacts_info))
335 self.assertEqual(
336 self.response.artifacts_info[0],
337 toolchain_pb2.ArtifactInfo(
George Burgess IV1e584862023-09-12 13:28:05 -0600338 artifact_type=artifact_type,
Alex Klein1699fab2022-09-08 08:46:06 -0600339 artifacts=[
340 artifacts_pb2.Artifact(path=self.bundle.return_value[0])
341 ],
342 ),
343 )
Tiancong Wangd5214132021-01-12 10:43:57 -0800344
345
Alex Klein1699fab2022-09-08 08:46:06 -0600346class GetUpdatedFilesTest(
347 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
348):
349 """Unittests for GetUpdatedFiles."""
Tiancong Wangd5214132021-01-12 10:43:57 -0800350
Alex Klein1699fab2022-09-08 08:46:06 -0600351 def setUp(self):
352 self.response = toolchain_pb2.GetUpdatedFilesResponse()
353 self.artifact_path = "/any/path/to/metadata"
Denis Nikitin62e69862023-02-13 23:37:00 -0800354 self.profile_info = common_pb2.ArtifactProfileInfo(
355 kernel_version="4.4", arch="amd64"
356 )
Alex Klein1699fab2022-09-08 08:46:06 -0600357 self.update = self.PatchObject(
358 toolchain_util, "GetUpdatedFiles", return_value=([], "")
359 )
Tiancong Wangd5214132021-01-12 10:43:57 -0800360
Alex Klein1699fab2022-09-08 08:46:06 -0600361 def _GetRequest(self, uploaded_artifacts):
362 uploaded = []
363 for artifact_type, artifact_path, profile_info in uploaded_artifacts:
364 uploaded.append(
365 toolchain_pb2.GetUpdatedFilesRequest.UploadedArtifacts(
366 artifact_info=toolchain_pb2.ArtifactInfo(
367 artifact_type=artifact_type,
368 artifacts=[artifacts_pb2.Artifact(path=artifact_path)],
369 ),
370 profile_info=profile_info,
371 )
372 )
373 return toolchain_pb2.GetUpdatedFilesRequest(uploaded_artifacts=uploaded)
Tiancong Wangd5214132021-01-12 10:43:57 -0800374
Alex Klein1699fab2022-09-08 08:46:06 -0600375 def testRaisesForUnknown(self):
376 request = self._GetRequest(
377 [
378 (
379 BuilderConfig.Artifacts.UNVERIFIED_KERNEL_CWP_AFDO_FILE,
380 self.artifact_path,
381 self.profile_info,
382 )
383 ]
384 )
385 self.assertEqual(
386 controller.RETURN_CODE_UNRECOVERABLE,
387 toolchain.GetUpdatedFiles(request, self.response, self.api_config),
388 )
Tiancong Wangd5214132021-01-12 10:43:57 -0800389
Alex Klein1699fab2022-09-08 08:46:06 -0600390 def testValidateOnly(self):
391 """Sanity check that a validate only call does not execute any logic."""
392 request = self._GetRequest(
393 [
394 (
395 BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE,
396 self.artifact_path,
397 self.profile_info,
398 )
399 ]
400 )
401 toolchain.GetUpdatedFiles(
402 request, self.response, self.validate_only_config
403 )
Tiancong Wangd5214132021-01-12 10:43:57 -0800404
Alex Klein1699fab2022-09-08 08:46:06 -0600405 def testUpdateSuccess(self):
406 updated_file = "/path/to/updated_file"
407 self.update.return_value = ([updated_file], "Commit Message")
408 request = self._GetRequest(
409 [
410 (
411 BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE,
412 self.artifact_path,
413 self.profile_info,
414 )
415 ]
416 )
417 toolchain.GetUpdatedFiles(request, self.response, self.api_config)
418 print(self.response.updated_files)
419 self.assertEqual(len(self.response.updated_files), 1)
420 self.assertEqual(
421 self.response.updated_files[0],
422 toolchain_pb2.GetUpdatedFilesResponse.UpdatedFile(
423 path=updated_file
424 ),
425 )
426 self.assertIn("Commit Message", self.response.commit_message)
427 self.assertEqual(len(self.response.commit_footer), 0)
Jack Neus4ee7b1d2022-06-27 19:54:18 +0000428
429
Alex Klein1699fab2022-09-08 08:46:06 -0600430class GetToolchainsForBoardTest(
431 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
432):
433 """Unittests for GetToolchainsForBoard."""
Jack Neus4ee7b1d2022-06-27 19:54:18 +0000434
Alex Klein1699fab2022-09-08 08:46:06 -0600435 def setUp(self):
436 self.response = toolchain_pb2.ToolchainsResponse()
Jack Neus4ee7b1d2022-06-27 19:54:18 +0000437
Alex Klein1699fab2022-09-08 08:46:06 -0600438 def _GetRequest(self, board="betty-pi-arc"):
439 return toolchain_pb2.ToolchainsRequest(board=board)
Jack Neus4ee7b1d2022-06-27 19:54:18 +0000440
Alex Klein1699fab2022-09-08 08:46:06 -0600441 def testValidateOnly(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700442 """Verify a validate-only call does not execute any logic."""
Alex Klein1699fab2022-09-08 08:46:06 -0600443 request = self._GetRequest()
444 toolchain.GetToolchainsForBoard(
445 request, self.response, self.validate_only_config
446 )
Jack Neus4ee7b1d2022-06-27 19:54:18 +0000447
Alex Klein1699fab2022-09-08 08:46:06 -0600448 def testUpdateSuccess(self):
449 toolchain_info = {
450 "default-a": {"default": True},
451 "default-b": {"default": True},
452 "nondefault-a": {"default": False},
453 "nondefault-b": {"default": False},
454 }
455 self.PatchObject(
456 toolchain_lib, "GetToolchainsForBoard", return_value=toolchain_info
457 )
Jack Neus4ee7b1d2022-06-27 19:54:18 +0000458
Alex Klein1699fab2022-09-08 08:46:06 -0600459 request = self._GetRequest()
460 toolchain.GetToolchainsForBoard(request, self.response, self.api_config)
Jack Neus4ee7b1d2022-06-27 19:54:18 +0000461
Alex Klein1699fab2022-09-08 08:46:06 -0600462 self.assertEqual(
463 self.response.default_toolchains, ["default-a", "default-b"]
464 )
465 self.assertEqual(
466 self.response.nondefault_toolchains,
467 ["nondefault-a", "nondefault-b"],
468 )
Greg Edelstondae510a2023-06-30 15:25:36 -0600469
470
Trent Apted39e74d32023-09-04 11:24:40 +1000471class SetupToolchainsTest(
472 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
473):
Greg Edelstondae510a2023-06-30 15:25:36 -0600474 """Unit tests for ToolchainService.SetupToolchains."""
Trent Apted39e74d32023-09-04 11:24:40 +1000475
Greg Edelstondae510a2023-06-30 15:25:36 -0600476 def setUp(self) -> None:
477 self.response = toolchain_pb2.SetupToolchainsResponse()
Trent Apted39e74d32023-09-04 11:24:40 +1000478 self.chroot = common_pb2.Chroot(path="/path/to/chroot")
Greg Edelstondae510a2023-06-30 15:25:36 -0600479
480 def test_outside_chroot(self) -> None:
481 """Test the behavior if run from outside the chroot."""
482 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
483 self.PatchObject(toolchain_service, "setup_toolchains")
484 request = toolchain_pb2.SetupToolchainsRequest(chroot=self.chroot)
485 with self.assertRaises(cros_build_lib.DieSystemExit):
486 toolchain.SetupToolchains(request, self.response, self.api_config)
487
488 def test_with_empty_request(self) -> None:
489 """Test the behavior if the request object is empty."""
490 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True)
491 service_mock = self.PatchObject(toolchain_service, "setup_toolchains")
492 request = toolchain_pb2.SetupToolchainsRequest(chroot=self.chroot)
493 toolchain.SetupToolchains(
494 request,
495 self.response,
496 self.api_config,
497 )
498 service_mock.assert_called_once_with(include_boards=[])
499
500 def test_with_boards(self) -> None:
501 """Test the behavior if the request object specifies boards."""
502 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True)
503 service_mock = self.PatchObject(toolchain_service, "setup_toolchains")
504 request = toolchain_pb2.SetupToolchainsRequest(
505 chroot=self.chroot,
506 boards=[
507 common_pb2.BuildTarget(name="amd64-generic"),
508 common_pb2.BuildTarget(name="arm-generic"),
Trent Apted39e74d32023-09-04 11:24:40 +1000509 ],
510 )
Greg Edelstondae510a2023-06-30 15:25:36 -0600511 toolchain.SetupToolchains(
512 request,
513 self.response,
514 self.api_config,
515 )
516 service_mock.assert_called_once_with(
Trent Apted39e74d32023-09-04 11:24:40 +1000517 include_boards=["amd64-generic", "arm-generic"],
518 )