blob: 482aaa5240c0ed6317ad8d21241cbd885ae0a79b [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Klein19c4cc42019-02-27 14:47:57 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""SDK tests."""
6
Greg Edelston9dcdc8a2023-01-11 17:07:10 -07007import os
Greg Edelston01ae5942023-01-30 16:26:54 -07008import pathlib
Greg Edelston9dcdc8a2023-01-11 17:07:10 -07009from typing import List, Optional
Mike Frysinger166fea02021-02-12 05:30:33 -050010from unittest import mock
11
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
Greg Edelston9dcdc8a2023-01-11 17:07:10 -070013from chromite.api.controller import controller_util
Alex Klein19c4cc42019-02-27 14:47:57 -070014from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060015from chromite.api.gen.chromite.api import sdk_pb2
Greg Edelston9dcdc8a2023-01-11 17:07:10 -070016from chromite.api.gen.chromiumos import common_pb2
17from chromite.lib import constants
Alex Klein19c4cc42019-02-27 14:47:57 -070018from chromite.lib import cros_build_lib
Alex Klein231d2da2019-07-22 16:44:45 -060019from chromite.lib import cros_test_lib
Alex Klein19c4cc42019-02-27 14:47:57 -070020from chromite.service import sdk as sdk_service
21
22
Alex Klein231d2da2019-07-22 16:44:45 -060023class SdkCreateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -060024 """Create tests."""
Alex Klein19c4cc42019-02-27 14:47:57 -070025
Alex Klein1699fab2022-09-08 08:46:06 -060026 def setUp(self):
27 """Setup method."""
28 # We need to run the command outside the chroot.
29 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
30 self.response = sdk_pb2.CreateResponse()
Alex Klein19c4cc42019-02-27 14:47:57 -070031
Alex Klein1699fab2022-09-08 08:46:06 -060032 def _GetRequest(
33 self,
34 no_replace=False,
Chris McDonald5dcdb892020-02-07 15:10:46 -070035 bootstrap=False,
Alex Klein1699fab2022-09-08 08:46:06 -060036 cache_path=None,
37 chroot_path=None,
38 sdk_version=None,
39 skip_chroot_upgrade=False,
40 ):
41 """Helper to build a create request message."""
42 request = sdk_pb2.CreateRequest()
43 request.flags.no_replace = no_replace
44 request.flags.bootstrap = bootstrap
Alex Klein231d2da2019-07-22 16:44:45 -060045
Alex Klein1699fab2022-09-08 08:46:06 -060046 if cache_path:
47 request.chroot.cache_dir = cache_path
48 if chroot_path:
49 request.chroot.path = chroot_path
50 if sdk_version:
51 request.sdk_version = sdk_version
52 if skip_chroot_upgrade:
53 request.skip_chroot_upgrade = skip_chroot_upgrade
Alex Klein19c4cc42019-02-27 14:47:57 -070054
Alex Klein1699fab2022-09-08 08:46:06 -060055 return request
56
57 def testValidateOnly(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070058 """Verify a validate-only call does not execute any logic."""
Alex Klein1699fab2022-09-08 08:46:06 -060059 patch = self.PatchObject(sdk_service, "Create")
60
61 sdk_controller.Create(
62 self._GetRequest(), self.response, self.validate_only_config
63 )
64 patch.assert_not_called()
65
66 def testMockCall(self):
67 """Sanity check that a mock call does not execute any logic."""
68 patch = self.PatchObject(sdk_service, "Create")
69
70 rc = sdk_controller.Create(
71 self._GetRequest(), self.response, self.mock_call_config
72 )
73 patch.assert_not_called()
74 self.assertFalse(rc)
75 self.assertTrue(self.response.version.version)
76
77 def testSuccess(self):
78 """Test the successful call output handling."""
79 self.PatchObject(sdk_service, "Create", return_value=1)
80
81 request = self._GetRequest()
82
83 sdk_controller.Create(request, self.response, self.api_config)
84
85 self.assertEqual(1, self.response.version.version)
86
87 def testFalseArguments(self):
88 """Test False argument handling."""
89 # Create the patches.
90 self.PatchObject(sdk_service, "Create", return_value=1)
91 args_patch = self.PatchObject(sdk_service, "CreateArguments")
92
93 # Flag translation tests.
94 # Test all false values in the message.
95 request = self._GetRequest(
Brian Norris35a7ed02023-02-23 12:50:14 -080096 no_replace=False,
97 bootstrap=False,
Alex Klein1699fab2022-09-08 08:46:06 -060098 )
99 sdk_controller.Create(request, self.response, self.api_config)
100 args_patch.assert_called_with(
101 replace=True,
102 bootstrap=False,
Alex Klein1699fab2022-09-08 08:46:06 -0600103 chroot_path=mock.ANY,
104 cache_dir=mock.ANY,
105 sdk_version=mock.ANY,
106 skip_chroot_upgrade=mock.ANY,
107 )
108
109 def testTrueArguments(self):
110 """Test True arguments handling."""
111 # Create the patches.
112 self.PatchObject(sdk_service, "Create", return_value=1)
113 args_patch = self.PatchObject(sdk_service, "CreateArguments")
114
115 # Test all True values in the message.
116 request = self._GetRequest(
117 no_replace=True,
118 bootstrap=True,
Alex Klein1699fab2022-09-08 08:46:06 -0600119 sdk_version="foo",
120 skip_chroot_upgrade=True,
121 )
122 sdk_controller.Create(request, self.response, self.api_config)
123 args_patch.assert_called_with(
124 replace=False,
125 bootstrap=True,
Alex Klein1699fab2022-09-08 08:46:06 -0600126 chroot_path=mock.ANY,
127 cache_dir=mock.ANY,
128 sdk_version="foo",
129 skip_chroot_upgrade=True,
130 )
Mike Frysingercb8992a2020-02-11 05:13:13 +0000131
Alex Kleinaa5c4172019-02-27 17:12:20 -0700132
Michael Mortensene87d8a62020-07-06 11:44:35 -0600133class SdkDeleteTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600134 """Create tests."""
Michael Mortensene87d8a62020-07-06 11:44:35 -0600135
Alex Klein1699fab2022-09-08 08:46:06 -0600136 def setUp(self):
137 """Setup method."""
138 # We need to run the command outside the chroot.
139 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
140 self.response = sdk_pb2.DeleteResponse()
Michael Mortensene87d8a62020-07-06 11:44:35 -0600141
Alex Klein1699fab2022-09-08 08:46:06 -0600142 def _GetRequest(self, chroot_path=None):
143 """Helper to build a delete request message."""
144 request = sdk_pb2.DeleteRequest()
145 if chroot_path:
146 request.chroot.path = chroot_path
Michael Mortensene87d8a62020-07-06 11:44:35 -0600147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 return request
Michael Mortensene87d8a62020-07-06 11:44:35 -0600149
Alex Klein1699fab2022-09-08 08:46:06 -0600150 def testValidateOnly(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700151 """Verify a validate-only call does not execute any logic."""
Alex Klein1699fab2022-09-08 08:46:06 -0600152 patch = self.PatchObject(sdk_service, "Delete")
Michael Mortensene87d8a62020-07-06 11:44:35 -0600153
Alex Klein1699fab2022-09-08 08:46:06 -0600154 sdk_controller.Delete(
155 self._GetRequest(), self.response, self.validate_only_config
156 )
157 patch.assert_not_called()
Michael Mortensene87d8a62020-07-06 11:44:35 -0600158
Alex Klein1699fab2022-09-08 08:46:06 -0600159 def testMockCall(self):
160 """Sanity check that a mock call does not execute any logic."""
161 patch = self.PatchObject(sdk_service, "Delete")
Michael Mortensene87d8a62020-07-06 11:44:35 -0600162
Alex Klein1699fab2022-09-08 08:46:06 -0600163 rc = sdk_controller.Delete(
164 self._GetRequest(), self.response, self.mock_call_config
165 )
166 patch.assert_not_called()
167 self.assertFalse(rc)
Michael Mortensene87d8a62020-07-06 11:44:35 -0600168
Alex Klein1699fab2022-09-08 08:46:06 -0600169 def testSuccess(self):
170 """Test the successful call by verifying service invocation."""
171 patch = self.PatchObject(sdk_service, "Delete", return_value=1)
Michael Mortensene87d8a62020-07-06 11:44:35 -0600172
Alex Klein1699fab2022-09-08 08:46:06 -0600173 request = self._GetRequest()
Michael Mortensene87d8a62020-07-06 11:44:35 -0600174
Alex Klein1699fab2022-09-08 08:46:06 -0600175 sdk_controller.Delete(request, self.response, self.api_config)
176 # Verify that by default sdk_service.Delete is called with force=True.
177 patch.assert_called_once_with(mock.ANY, force=True)
Michael Mortensene87d8a62020-07-06 11:44:35 -0600178
179
Brian Norrisf624bf42023-03-02 12:57:49 -0800180class SdkUnmountTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
181 """SDK Unmount tests."""
182
183 def testNoop(self):
184 """Unmount is a deprecated noop."""
185 request = sdk_pb2.UnmountRequest()
186 response = sdk_pb2.UnmountResponse()
187 rc = sdk_controller.Unmount(request, response, self.api_config)
188 self.assertFalse(rc)
189
190
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600191class SdkUnmountPathTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600192 """Update tests."""
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600193
Alex Klein1699fab2022-09-08 08:46:06 -0600194 def setUp(self):
195 """Setup method."""
196 self.response = sdk_pb2.UnmountPathResponse()
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600197
Alex Klein1699fab2022-09-08 08:46:06 -0600198 def _UnmountPathRequest(self, path=None):
199 """Helper to build a delete request message."""
200 request = sdk_pb2.UnmountPathRequest()
201 if path:
202 request.path.path = path
203 return request
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600204
Alex Klein1699fab2022-09-08 08:46:06 -0600205 def testValidateOnly(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700206 """Verify a validate-only call does not execute any logic."""
Alex Klein1699fab2022-09-08 08:46:06 -0600207 patch = self.PatchObject(sdk_service, "UnmountPath")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600208
Alex Klein1699fab2022-09-08 08:46:06 -0600209 sdk_controller.UnmountPath(
210 self._UnmountPathRequest("/test/path"),
211 self.response,
212 self.validate_only_config,
213 )
214 patch.assert_not_called()
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600215
Alex Klein1699fab2022-09-08 08:46:06 -0600216 def testMockCall(self):
217 """Sanity check that a mock call does not execute any logic."""
218 patch = self.PatchObject(sdk_service, "UnmountPath")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600219
Alex Klein1699fab2022-09-08 08:46:06 -0600220 rc = sdk_controller.UnmountPath(
221 self._UnmountPathRequest(), self.response, self.mock_call_config
222 )
223 patch.assert_not_called()
224 self.assertFalse(rc)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600225
Alex Klein1699fab2022-09-08 08:46:06 -0600226 def testSuccess(self):
227 """Test the successful call by verifying service invocation."""
228 patch = self.PatchObject(sdk_service, "UnmountPath", return_value=1)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600229
Alex Klein1699fab2022-09-08 08:46:06 -0600230 request = self._UnmountPathRequest("/test/path")
231 sdk_controller.UnmountPath(request, self.response, self.api_config)
232 patch.assert_called_once_with("/test/path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600233
234
Alex Klein231d2da2019-07-22 16:44:45 -0600235class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600236 """Update tests."""
Alex Kleinaa5c4172019-02-27 17:12:20 -0700237
Alex Klein1699fab2022-09-08 08:46:06 -0600238 def setUp(self):
239 """Setup method."""
240 # We need to run the command inside the chroot.
241 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700242
Alex Klein1699fab2022-09-08 08:46:06 -0600243 self.response = sdk_pb2.UpdateResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600244
Alex Klein1699fab2022-09-08 08:46:06 -0600245 def _GetRequest(self, build_source=False, targets=None):
246 """Helper to simplify building a request instance."""
247 request = sdk_pb2.UpdateRequest()
248 request.flags.build_source = build_source
Alex Kleinaa5c4172019-02-27 17:12:20 -0700249
Alex Klein1699fab2022-09-08 08:46:06 -0600250 for target in targets or []:
251 added = request.toolchain_targets.add()
252 added.name = target
Alex Kleinaa5c4172019-02-27 17:12:20 -0700253
Alex Klein1699fab2022-09-08 08:46:06 -0600254 return request
Alex Kleinaa5c4172019-02-27 17:12:20 -0700255
Alex Klein1699fab2022-09-08 08:46:06 -0600256 def testValidateOnly(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700257 """Verify a validate-only call does not execute any logic."""
Alex Klein1699fab2022-09-08 08:46:06 -0600258 patch = self.PatchObject(sdk_service, "Update")
Alex Klein231d2da2019-07-22 16:44:45 -0600259
Alex Klein1699fab2022-09-08 08:46:06 -0600260 sdk_controller.Update(
261 self._GetRequest(), self.response, self.validate_only_config
262 )
263 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700264
Alex Klein1699fab2022-09-08 08:46:06 -0600265 def testMockCall(self):
266 """Sanity check that a mock call does not execute any logic."""
267 patch = self.PatchObject(sdk_service, "Update")
Alex Klein076841b2019-08-29 15:19:39 -0600268
Alex Klein1699fab2022-09-08 08:46:06 -0600269 rc = sdk_controller.Create(
270 self._GetRequest(), self.response, self.mock_call_config
271 )
272 patch.assert_not_called()
273 self.assertFalse(rc)
274 self.assertTrue(self.response.version.version)
Alex Klein076841b2019-08-29 15:19:39 -0600275
Alex Klein1699fab2022-09-08 08:46:06 -0600276 def testSuccess(self):
277 """Successful call output handling test."""
278 expected_version = 1
279 self.PatchObject(sdk_service, "Update", return_value=expected_version)
280 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700281
Alex Klein1699fab2022-09-08 08:46:06 -0600282 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700283
Alex Klein1699fab2022-09-08 08:46:06 -0600284 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700285
Alex Klein1699fab2022-09-08 08:46:06 -0600286 def testArgumentHandling(self):
287 """Test the proto argument handling."""
288 args = sdk_service.UpdateArguments()
289 self.PatchObject(sdk_service, "Update", return_value=1)
290 args_patch = self.PatchObject(
291 sdk_service, "UpdateArguments", return_value=args
292 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700293
Alex Klein1699fab2022-09-08 08:46:06 -0600294 # No boards and flags False.
295 request = self._GetRequest(build_source=False)
296 sdk_controller.Update(request, self.response, self.api_config)
297 args_patch.assert_called_with(
298 build_source=False, toolchain_targets=[], toolchain_changed=False
299 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700300
Alex Klein1699fab2022-09-08 08:46:06 -0600301 # Multiple boards and flags True.
302 targets = ["board1", "board2"]
303 request = self._GetRequest(build_source=True, targets=targets)
304 sdk_controller.Update(request, self.response, self.api_config)
305 args_patch.assert_called_with(
306 build_source=True,
307 toolchain_targets=targets,
308 toolchain_changed=False,
309 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700310
311
Greg Edelston01ae5942023-01-30 16:26:54 -0700312class CreateManifestFromSdkTest(
313 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
314):
315 """Test the SdkService/CreateManifestFromSdk endpoint."""
316
317 _chroot_path = "/path/to/chroot"
318 _sdk_path_relative = "build/my_sdk"
319 _dest_dir = "/build"
320 _manifest_path = "/build/my_sdk.Manifest"
321
322 def _NewRequest(self, inside: bool) -> sdk_pb2.CreateManifestFromSdkRequest:
323 return sdk_pb2.CreateManifestFromSdkRequest(
324 chroot=common_pb2.Chroot(path=self._chroot_path),
325 sdk_path=common_pb2.Path(
326 path="/%s" % self._sdk_path_relative,
327 location=common_pb2.Path.Location.INSIDE
328 if inside
329 else common_pb2.Path.Location.OUTSIDE,
330 ),
331 dest_dir=common_pb2.Path(
332 path=self._dest_dir,
333 location=common_pb2.Path.Location.OUTSIDE,
334 ),
335 )
336
337 def _NewResponse(self) -> sdk_pb2.CreateManifestFromSdkResponse:
338 return sdk_pb2.CreateManifestFromSdkResponse()
339
340 def testValidateOnly(self):
341 """Check that a validate only call does not execute any logic."""
342 impl_patch = self.PatchObject(sdk_service, "CreateManifestFromSdk")
343 sdk_controller.BuildSdkToolchain(
344 self._NewRequest(False),
345 self._NewResponse(),
346 self.validate_only_config,
347 )
348 impl_patch.assert_not_called()
349
350 def testOutside(self):
351 """Check that a call with an outside path succeeds."""
352 impl_patch = self.PatchObject(
353 sdk_service,
354 "CreateManifestFromSdk",
355 return_value=pathlib.Path(self._manifest_path),
356 )
357 request = self._NewRequest(inside=False)
358 response = self._NewResponse()
359 sdk_controller.CreateManifestFromSdk(
360 request,
361 response,
362 self.api_config,
363 )
364 impl_patch.assert_called_with(
365 pathlib.Path("/", self._sdk_path_relative),
366 pathlib.Path(self._dest_dir),
367 )
368 self.assertEqual(
369 response.manifest_path.location, common_pb2.Path.Location.OUTSIDE
370 )
371 self.assertEqual(response.manifest_path.path, self._manifest_path)
372
373 def testInside(self):
374 """Check that an inside path parses correctly and the call succeeds."""
375 impl_patch = self.PatchObject(
376 sdk_service,
377 "CreateManifestFromSdk",
378 return_value=pathlib.Path(self._manifest_path),
379 )
380 request = self._NewRequest(inside=True)
381 response = self._NewResponse()
382 sdk_controller.CreateManifestFromSdk(
383 request,
384 response,
385 self.api_config,
386 )
387 impl_patch.assert_called_with(
388 pathlib.Path(self._chroot_path, self._sdk_path_relative),
389 pathlib.Path(self._dest_dir),
390 )
391 self.assertEqual(
392 response.manifest_path.location, common_pb2.Path.Location.OUTSIDE
393 )
394 self.assertEqual(response.manifest_path.path, self._manifest_path)
395
396
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700397class BuildSdkToolchainTest(
398 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
399):
400 """Test the SdkService/BuildSdkToolchain endpoint."""
401
402 def setUp(self):
403 """Set up the test case."""
404 self._chroot_path = "/path/to/chroot"
405 self._response = sdk_pb2.BuildSdkToolchainResponse()
406 self._generated_filenames = (
407 "armv7a-cros-linux-gnueabihf.tar.xz",
408 "x86_64-cros-linux-gnu.tar.xz",
409 )
410 self._paths_for_generated_files = [
411 common_pb2.Path(
412 path=os.path.join(constants.SDK_TOOLCHAINS_OUTPUT, fname),
413 location=common_pb2.Path.Location.INSIDE,
414 )
415 for fname in self._generated_filenames
416 ]
417
418 def _NewRequest(
419 self,
420 chroot_path: Optional[str] = None,
421 use_flags: Optional[List[str]] = None,
422 ) -> sdk_pb2.BuildSdkToolchainRequest:
423 """Return a new BuildSdkToolchainRequest message."""
424 request = sdk_pb2.BuildSdkToolchainRequest()
425 if chroot_path:
426 request.chroot.path = chroot_path
427 if use_flags:
428 request.use_flags.extend(
429 common_pb2.UseFlag(flag=flag) for flag in use_flags
430 )
431 return request
432
433 def _NewResponse(
434 self, generated_filenames: Optional[List[str]] = None
435 ) -> sdk_pb2.BuildSdkToolchainResponse:
436 """Return a new BuildSdkToolchainResponse message."""
437 response = sdk_pb2.BuildSdkToolchainResponse()
438 if generated_filenames:
439 response.generated_files.extend(
440 common_pb2.Path(
441 path=os.path.join(constants.SDK_TOOLCHAINS_OUTPUT, fname),
442 location=common_pb2.Path.Location.INSIDE,
443 )
444 for fname in generated_filenames
445 )
446 return response
447
448 def testValidateOnly(self):
449 """Check that a validate only call does not execute any logic."""
450 impl_patch = self.PatchObject(sdk_service, "BuildSdkToolchain")
451 sdk_controller.BuildSdkToolchain(
452 self._NewRequest(), self._NewResponse(), self.validate_only_config
453 )
454 impl_patch.assert_not_called()
455
456 def testSuccess(self):
457 """Check that a normal call defers to the SDK service as expected."""
458 impl_patch = self.PatchObject(sdk_service, "BuildSdkToolchain")
459 request = self._NewRequest(use_flags=[])
460 response = self._NewResponse()
461 sdk_controller.BuildSdkToolchain(
462 request,
463 response,
464 self.api_config,
465 )
466 # Can't use assert_called_with, since the chroot objects are equal but
467 # not identical.
468 impl_patch.assert_called_once()
469 self.assertEqual(
470 impl_patch.call_args.args[0],
471 controller_util.ParseChroot(request.chroot),
472 )
473 self.assertEqual(impl_patch.call_args.kwargs["extra_env"], {})
474
475 def testSuccessWithUseFlags(self):
476 """Check that a call with USE flags works as expected."""
477 impl_patch = self.PatchObject(sdk_service, "BuildSdkToolchain")
478 request = self._NewRequest(use_flags=["llvm-next", "another-flag"])
479 response = self._NewResponse()
480 sdk_controller.BuildSdkToolchain(
481 request,
482 response,
483 self.api_config,
484 )
485 # Can't use assert_called_with, since the chroot objects are equal but
486 # not identical.
487 impl_patch.assert_called_once()
488 self.assertEqual(
489 impl_patch.call_args.args[0],
490 controller_util.ParseChroot(request.chroot),
491 )
492 self.assertEqual(
493 impl_patch.call_args.kwargs["extra_env"],
494 {"USE": "llvm-next another-flag"},
495 )
Greg Edelston6733dc52023-02-15 15:20:07 -0700496
497
498class UprevTestCase(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
499 """Test case for SdkService/Uprev() endpoint."""
500
Greg Edelston4907c8c2023-03-27 14:53:31 -0600501 _source_root = pathlib.Path("/path/to/checkout/")
Greg Edelston6733dc52023-02-15 15:20:07 -0700502 _binhost_gs_bucket = "gs://chromiumos-prebuilts/"
503 _latest_version = "2023.02.15.115707"
504
505 def setUp(self):
506 """Set up the test case."""
Greg Edelston4907c8c2023-03-27 14:53:31 -0600507 self._source_root_pb2 = common_pb2.Path(
508 path=str(self._source_root),
509 location=common_pb2.Path.Location.OUTSIDE,
510 )
Greg Edelston6733dc52023-02-15 15:20:07 -0700511 self.PatchObject(
512 sdk_service,
513 "GetLatestVersion",
514 return_value=self._latest_version,
515 )
516 self._uprev_patch = self.PatchObject(
517 sdk_service,
518 "UprevSdkAndPrebuilts",
519 )
520
521 def NewRequest(self, version: str = ""):
522 """Return a new UprevRequest with standard inputs."""
523 return sdk_pb2.UprevRequest(
Greg Edelston4907c8c2023-03-27 14:53:31 -0600524 source_root=self._source_root_pb2,
Greg Edelston6733dc52023-02-15 15:20:07 -0700525 binhost_gs_bucket=self._binhost_gs_bucket,
526 version=version,
527 )
528
529 @staticmethod
530 def NewResponse() -> sdk_pb2.UprevResponse:
531 """Return a new empty UprevResponse."""
532 return sdk_pb2.UprevResponse()
533
534 def testWithVersion(self):
535 """Test the endpoint with `version` specified.
536
537 In this case, we expect that sdk_controller.Uprev is called with the
538 version specified in the UprevRequest.
539 """
540 specified_version = "1970.01.01.000000"
541 request = self.NewRequest(version=specified_version)
542 response = self.NewResponse()
543 sdk_controller.Uprev(request, response, self.api_config)
544 self._uprev_patch.assert_called_with(
Greg Edelston4907c8c2023-03-27 14:53:31 -0600545 self._source_root,
Greg Edelston6733dc52023-02-15 15:20:07 -0700546 binhost_gs_bucket=self._binhost_gs_bucket,
547 version=specified_version,
548 )
549
550 def testWithoutVersion(self):
551 """Test the endpoint with `version` not specified.
552
553 In this case, we expect that sdk_controller.Uprev is called with the
554 latest version available on gs://. This is fetched via
555 sdk_controller.GetLatestVersion (mocked here in setUp()).
556 """
557 request = self.NewRequest()
558 response = self.NewResponse()
559 sdk_controller.Uprev(request, response, self.api_config)
560 self._uprev_patch.assert_called_with(
Greg Edelston4907c8c2023-03-27 14:53:31 -0600561 self._source_root,
Greg Edelston6733dc52023-02-15 15:20:07 -0700562 binhost_gs_bucket=self._binhost_gs_bucket,
563 version=self._latest_version,
564 )