blob: 9a885f3013fcbeeb3adb34eac846a9a4e60528e8 [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
Brian Norris41f247b2023-06-30 11:09:40 -07008from pathlib import Path
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
Alex Klein19c4cc42019-02-27 14:47:57 -070013from chromite.api.controller import sdk as sdk_controller
Alex Klein7107bdd2019-03-14 17:14:31 -060014from chromite.api.gen.chromite.api import sdk_pb2
Greg Edelston9dcdc8a2023-01-11 17:07:10 -070015from chromite.api.gen.chromiumos import common_pb2
Brian Norris41f247b2023-06-30 11:09:40 -070016from chromite.lib import chroot_lib
Greg Edelston9dcdc8a2023-01-11 17:07:10 -070017from 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,
George Burgess IV6e04d602023-08-16 15:08:32 -060040 ccache_disable=False,
Alex Klein1699fab2022-09-08 08:46:06 -060041 ):
42 """Helper to build a create request message."""
43 request = sdk_pb2.CreateRequest()
44 request.flags.no_replace = no_replace
45 request.flags.bootstrap = bootstrap
Alex Klein231d2da2019-07-22 16:44:45 -060046
Alex Klein1699fab2022-09-08 08:46:06 -060047 if cache_path:
48 request.chroot.cache_dir = cache_path
49 if chroot_path:
50 request.chroot.path = chroot_path
51 if sdk_version:
52 request.sdk_version = sdk_version
53 if skip_chroot_upgrade:
54 request.skip_chroot_upgrade = skip_chroot_upgrade
George Burgess IV6e04d602023-08-16 15:08:32 -060055 if ccache_disable:
56 request.ccache_disable = ccache_disable
Alex Klein19c4cc42019-02-27 14:47:57 -070057
Alex Klein1699fab2022-09-08 08:46:06 -060058 return request
59
60 def testValidateOnly(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070061 """Verify a validate-only call does not execute any logic."""
Alex Klein1699fab2022-09-08 08:46:06 -060062 patch = self.PatchObject(sdk_service, "Create")
63
64 sdk_controller.Create(
65 self._GetRequest(), self.response, self.validate_only_config
66 )
67 patch.assert_not_called()
68
69 def testMockCall(self):
70 """Sanity check that a mock call does not execute any logic."""
71 patch = self.PatchObject(sdk_service, "Create")
72
73 rc = sdk_controller.Create(
74 self._GetRequest(), self.response, self.mock_call_config
75 )
76 patch.assert_not_called()
77 self.assertFalse(rc)
78 self.assertTrue(self.response.version.version)
79
80 def testSuccess(self):
81 """Test the successful call output handling."""
82 self.PatchObject(sdk_service, "Create", return_value=1)
83
84 request = self._GetRequest()
85
86 sdk_controller.Create(request, self.response, self.api_config)
87
88 self.assertEqual(1, self.response.version.version)
89
90 def testFalseArguments(self):
91 """Test False argument handling."""
92 # Create the patches.
93 self.PatchObject(sdk_service, "Create", return_value=1)
94 args_patch = self.PatchObject(sdk_service, "CreateArguments")
95
96 # Flag translation tests.
97 # Test all false values in the message.
98 request = self._GetRequest(
Brian Norris35a7ed02023-02-23 12:50:14 -080099 no_replace=False,
100 bootstrap=False,
Alex Klein1699fab2022-09-08 08:46:06 -0600101 )
102 sdk_controller.Create(request, self.response, self.api_config)
103 args_patch.assert_called_with(
104 replace=True,
105 bootstrap=False,
Brian Norris67374d82023-05-01 12:31:12 -0700106 chroot=mock.ANY,
Alex Klein1699fab2022-09-08 08:46:06 -0600107 sdk_version=mock.ANY,
108 skip_chroot_upgrade=mock.ANY,
George Burgess IV6e04d602023-08-16 15:08:32 -0600109 ccache_disable=mock.ANY,
Alex Klein1699fab2022-09-08 08:46:06 -0600110 )
111
112 def testTrueArguments(self):
113 """Test True arguments handling."""
114 # Create the patches.
115 self.PatchObject(sdk_service, "Create", return_value=1)
116 args_patch = self.PatchObject(sdk_service, "CreateArguments")
117
118 # Test all True values in the message.
119 request = self._GetRequest(
120 no_replace=True,
121 bootstrap=True,
Alex Klein1699fab2022-09-08 08:46:06 -0600122 sdk_version="foo",
123 skip_chroot_upgrade=True,
George Burgess IV6e04d602023-08-16 15:08:32 -0600124 ccache_disable=True,
Alex Klein1699fab2022-09-08 08:46:06 -0600125 )
126 sdk_controller.Create(request, self.response, self.api_config)
127 args_patch.assert_called_with(
128 replace=False,
129 bootstrap=True,
Brian Norris67374d82023-05-01 12:31:12 -0700130 chroot=mock.ANY,
Alex Klein1699fab2022-09-08 08:46:06 -0600131 sdk_version="foo",
132 skip_chroot_upgrade=True,
George Burgess IV6e04d602023-08-16 15:08:32 -0600133 ccache_disable=True,
Alex Klein1699fab2022-09-08 08:46:06 -0600134 )
Mike Frysingercb8992a2020-02-11 05:13:13 +0000135
Alex Kleinaa5c4172019-02-27 17:12:20 -0700136
George Engelbrecht86cfae62023-04-05 10:57:41 -0600137class SdkCleanTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
138 """Clean tests."""
139
140 def setUp(self):
141 """Setup method."""
142 # We need to run the command outside the chroot.
143 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
144 self.response = sdk_pb2.CleanResponse()
145
146 def _GetRequest(self, chroot_path=None, incrementals=False):
147 """Helper to build a clean request message."""
148 request = sdk_pb2.CleanRequest()
149 if chroot_path:
150 request.chroot.path = chroot_path
151
152 request.incrementals = incrementals
153
154 return request
155
156 def testMockCall(self):
157 """Sanity check that a mock call does not execute any logic."""
158 patch = self.PatchObject(sdk_service, "Clean")
159
160 rc = sdk_controller.Clean(
161 self._GetRequest(), self.response, self.mock_call_config
162 )
163 patch.assert_not_called()
164 self.assertFalse(rc)
165
166 def testSuccess(self):
167 """Test the successful call by verifying service invocation."""
168 patch = self.PatchObject(sdk_service, "Clean", return_value=0)
169
170 request = self._GetRequest(incrementals=True)
171
172 sdk_controller.Clean(request, self.response, self.api_config)
173 patch.assert_called_once_with(
174 mock.ANY,
175 safe=False,
176 images=False,
177 sysroots=False,
178 tmp=False,
179 cache=False,
180 logs=False,
181 workdirs=False,
182 incrementals=True,
183 )
184
185 def testDefaults(self):
186 """Test the successful call by verifying service invocation."""
187 patch = self.PatchObject(sdk_service, "Clean", return_value=0)
188
189 request = self._GetRequest()
190
191 sdk_controller.Clean(request, self.response, self.api_config)
192 patch.assert_called_once_with(mock.ANY, safe=True, sysroots=True)
193
194
Michael Mortensene87d8a62020-07-06 11:44:35 -0600195class SdkDeleteTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
George Engelbrecht86cfae62023-04-05 10:57:41 -0600196 """Delete tests."""
Michael Mortensene87d8a62020-07-06 11:44:35 -0600197
Alex Klein1699fab2022-09-08 08:46:06 -0600198 def setUp(self):
199 """Setup method."""
200 # We need to run the command outside the chroot.
201 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
202 self.response = sdk_pb2.DeleteResponse()
Michael Mortensene87d8a62020-07-06 11:44:35 -0600203
Alex Klein1699fab2022-09-08 08:46:06 -0600204 def _GetRequest(self, chroot_path=None):
205 """Helper to build a delete request message."""
206 request = sdk_pb2.DeleteRequest()
207 if chroot_path:
208 request.chroot.path = chroot_path
Michael Mortensene87d8a62020-07-06 11:44:35 -0600209
Alex Klein1699fab2022-09-08 08:46:06 -0600210 return request
Michael Mortensene87d8a62020-07-06 11:44:35 -0600211
Alex Klein1699fab2022-09-08 08:46:06 -0600212 def testValidateOnly(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700213 """Verify a validate-only call does not execute any logic."""
Alex Klein1699fab2022-09-08 08:46:06 -0600214 patch = self.PatchObject(sdk_service, "Delete")
Michael Mortensene87d8a62020-07-06 11:44:35 -0600215
Alex Klein1699fab2022-09-08 08:46:06 -0600216 sdk_controller.Delete(
217 self._GetRequest(), self.response, self.validate_only_config
218 )
219 patch.assert_not_called()
Michael Mortensene87d8a62020-07-06 11:44:35 -0600220
Alex Klein1699fab2022-09-08 08:46:06 -0600221 def testMockCall(self):
222 """Sanity check that a mock call does not execute any logic."""
223 patch = self.PatchObject(sdk_service, "Delete")
Michael Mortensene87d8a62020-07-06 11:44:35 -0600224
Alex Klein1699fab2022-09-08 08:46:06 -0600225 rc = sdk_controller.Delete(
226 self._GetRequest(), self.response, self.mock_call_config
227 )
228 patch.assert_not_called()
229 self.assertFalse(rc)
Michael Mortensene87d8a62020-07-06 11:44:35 -0600230
Alex Klein1699fab2022-09-08 08:46:06 -0600231 def testSuccess(self):
232 """Test the successful call by verifying service invocation."""
233 patch = self.PatchObject(sdk_service, "Delete", return_value=1)
Michael Mortensene87d8a62020-07-06 11:44:35 -0600234
Alex Klein1699fab2022-09-08 08:46:06 -0600235 request = self._GetRequest()
Michael Mortensene87d8a62020-07-06 11:44:35 -0600236
Alex Klein1699fab2022-09-08 08:46:06 -0600237 sdk_controller.Delete(request, self.response, self.api_config)
238 # Verify that by default sdk_service.Delete is called with force=True.
239 patch.assert_called_once_with(mock.ANY, force=True)
Michael Mortensene87d8a62020-07-06 11:44:35 -0600240
241
Brian Norrisf624bf42023-03-02 12:57:49 -0800242class SdkUnmountTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
243 """SDK Unmount tests."""
244
245 def testNoop(self):
246 """Unmount is a deprecated noop."""
247 request = sdk_pb2.UnmountRequest()
248 response = sdk_pb2.UnmountResponse()
249 rc = sdk_controller.Unmount(request, response, self.api_config)
250 self.assertFalse(rc)
251
252
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600253class SdkUnmountPathTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600254 """Update tests."""
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600255
Alex Klein1699fab2022-09-08 08:46:06 -0600256 def setUp(self):
257 """Setup method."""
258 self.response = sdk_pb2.UnmountPathResponse()
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600259
Alex Klein1699fab2022-09-08 08:46:06 -0600260 def _UnmountPathRequest(self, path=None):
261 """Helper to build a delete request message."""
262 request = sdk_pb2.UnmountPathRequest()
263 if path:
264 request.path.path = path
265 return request
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600266
Alex Klein1699fab2022-09-08 08:46:06 -0600267 def testValidateOnly(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700268 """Verify a validate-only call does not execute any logic."""
Alex Klein1699fab2022-09-08 08:46:06 -0600269 patch = self.PatchObject(sdk_service, "UnmountPath")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600270
Alex Klein1699fab2022-09-08 08:46:06 -0600271 sdk_controller.UnmountPath(
272 self._UnmountPathRequest("/test/path"),
273 self.response,
274 self.validate_only_config,
275 )
276 patch.assert_not_called()
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600277
Alex Klein1699fab2022-09-08 08:46:06 -0600278 def testMockCall(self):
279 """Sanity check that a mock call does not execute any logic."""
280 patch = self.PatchObject(sdk_service, "UnmountPath")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600281
Alex Klein1699fab2022-09-08 08:46:06 -0600282 rc = sdk_controller.UnmountPath(
283 self._UnmountPathRequest(), self.response, self.mock_call_config
284 )
285 patch.assert_not_called()
286 self.assertFalse(rc)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600287
Alex Klein1699fab2022-09-08 08:46:06 -0600288 def testSuccess(self):
289 """Test the successful call by verifying service invocation."""
290 patch = self.PatchObject(sdk_service, "UnmountPath", return_value=1)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600291
Alex Klein1699fab2022-09-08 08:46:06 -0600292 request = self._UnmountPathRequest("/test/path")
293 sdk_controller.UnmountPath(request, self.response, self.api_config)
294 patch.assert_called_once_with("/test/path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600295
296
Alex Klein231d2da2019-07-22 16:44:45 -0600297class SdkUpdateTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600298 """Update tests."""
Alex Kleinaa5c4172019-02-27 17:12:20 -0700299
Alex Klein1699fab2022-09-08 08:46:06 -0600300 def setUp(self):
301 """Setup method."""
302 # We need to run the command inside the chroot.
303 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=True)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700304
Alex Klein1699fab2022-09-08 08:46:06 -0600305 self.response = sdk_pb2.UpdateResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600306
Alex Klein1699fab2022-09-08 08:46:06 -0600307 def _GetRequest(self, build_source=False, targets=None):
308 """Helper to simplify building a request instance."""
309 request = sdk_pb2.UpdateRequest()
310 request.flags.build_source = build_source
Alex Kleinaa5c4172019-02-27 17:12:20 -0700311
Alex Klein1699fab2022-09-08 08:46:06 -0600312 for target in targets or []:
313 added = request.toolchain_targets.add()
314 added.name = target
Alex Kleinaa5c4172019-02-27 17:12:20 -0700315
Alex Klein1699fab2022-09-08 08:46:06 -0600316 return request
Alex Kleinaa5c4172019-02-27 17:12:20 -0700317
Alex Klein1699fab2022-09-08 08:46:06 -0600318 def testValidateOnly(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700319 """Verify a validate-only call does not execute any logic."""
Alex Klein1699fab2022-09-08 08:46:06 -0600320 patch = self.PatchObject(sdk_service, "Update")
Alex Klein231d2da2019-07-22 16:44:45 -0600321
Alex Klein1699fab2022-09-08 08:46:06 -0600322 sdk_controller.Update(
323 self._GetRequest(), self.response, self.validate_only_config
324 )
325 patch.assert_not_called()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700326
Alex Klein1699fab2022-09-08 08:46:06 -0600327 def testMockCall(self):
328 """Sanity check that a mock call does not execute any logic."""
329 patch = self.PatchObject(sdk_service, "Update")
Alex Klein076841b2019-08-29 15:19:39 -0600330
Alex Klein1699fab2022-09-08 08:46:06 -0600331 rc = sdk_controller.Create(
332 self._GetRequest(), self.response, self.mock_call_config
333 )
334 patch.assert_not_called()
335 self.assertFalse(rc)
336 self.assertTrue(self.response.version.version)
Alex Klein076841b2019-08-29 15:19:39 -0600337
Alex Klein1699fab2022-09-08 08:46:06 -0600338 def testSuccess(self):
339 """Successful call output handling test."""
340 expected_version = 1
341 self.PatchObject(sdk_service, "Update", return_value=expected_version)
342 request = self._GetRequest()
Alex Kleinaa5c4172019-02-27 17:12:20 -0700343
Alex Klein1699fab2022-09-08 08:46:06 -0600344 sdk_controller.Update(request, self.response, self.api_config)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700345
Alex Klein1699fab2022-09-08 08:46:06 -0600346 self.assertEqual(expected_version, self.response.version.version)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700347
Alex Klein1699fab2022-09-08 08:46:06 -0600348 def testArgumentHandling(self):
349 """Test the proto argument handling."""
350 args = sdk_service.UpdateArguments()
351 self.PatchObject(sdk_service, "Update", return_value=1)
352 args_patch = self.PatchObject(
353 sdk_service, "UpdateArguments", return_value=args
354 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700355
Alex Klein1699fab2022-09-08 08:46:06 -0600356 # No boards and flags False.
357 request = self._GetRequest(build_source=False)
358 sdk_controller.Update(request, self.response, self.api_config)
359 args_patch.assert_called_with(
360 build_source=False, toolchain_targets=[], toolchain_changed=False
361 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700362
Alex Klein1699fab2022-09-08 08:46:06 -0600363 # Multiple boards and flags True.
364 targets = ["board1", "board2"]
365 request = self._GetRequest(build_source=True, targets=targets)
366 sdk_controller.Update(request, self.response, self.api_config)
367 args_patch.assert_called_with(
368 build_source=True,
369 toolchain_targets=targets,
370 toolchain_changed=False,
371 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700372
373
Greg Edelston01ae5942023-01-30 16:26:54 -0700374class CreateManifestFromSdkTest(
375 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
376):
377 """Test the SdkService/CreateManifestFromSdk endpoint."""
378
Brian Norris41f247b2023-06-30 11:09:40 -0700379 _sdk_path = "/build/my_sdk"
Greg Edelston01ae5942023-01-30 16:26:54 -0700380 _dest_dir = "/build"
381 _manifest_path = "/build/my_sdk.Manifest"
382
383 def _NewRequest(self, inside: bool) -> sdk_pb2.CreateManifestFromSdkRequest:
384 return sdk_pb2.CreateManifestFromSdkRequest(
Brian Norris41f247b2023-06-30 11:09:40 -0700385 chroot=common_pb2.Chroot(
386 path=self.chroot.path, out_path=str(self.chroot.out_path)
387 ),
Greg Edelston01ae5942023-01-30 16:26:54 -0700388 sdk_path=common_pb2.Path(
Brian Norris41f247b2023-06-30 11:09:40 -0700389 path=self._sdk_path,
Greg Edelston01ae5942023-01-30 16:26:54 -0700390 location=common_pb2.Path.Location.INSIDE
391 if inside
392 else common_pb2.Path.Location.OUTSIDE,
393 ),
394 dest_dir=common_pb2.Path(
395 path=self._dest_dir,
396 location=common_pb2.Path.Location.OUTSIDE,
397 ),
398 )
399
400 def _NewResponse(self) -> sdk_pb2.CreateManifestFromSdkResponse:
401 return sdk_pb2.CreateManifestFromSdkResponse()
402
Brian Norris41f247b2023-06-30 11:09:40 -0700403 def setUp(self):
404 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
405
406 self.chroot = chroot_lib.Chroot(
407 path=Path("/path/to/chroot"),
408 out_path=Path("/path/to/out"),
409 )
410
Greg Edelston01ae5942023-01-30 16:26:54 -0700411 def testValidateOnly(self):
412 """Check that a validate only call does not execute any logic."""
413 impl_patch = self.PatchObject(sdk_service, "CreateManifestFromSdk")
Greg Edelstone265a172023-06-26 17:20:05 -0600414 sdk_controller.CreateManifestFromSdk(
Greg Edelston01ae5942023-01-30 16:26:54 -0700415 self._NewRequest(False),
416 self._NewResponse(),
417 self.validate_only_config,
418 )
419 impl_patch.assert_not_called()
420
421 def testOutside(self):
422 """Check that a call with an outside path succeeds."""
423 impl_patch = self.PatchObject(
424 sdk_service,
425 "CreateManifestFromSdk",
Brian Norris41f247b2023-06-30 11:09:40 -0700426 return_value=Path(self._manifest_path),
Greg Edelston01ae5942023-01-30 16:26:54 -0700427 )
428 request = self._NewRequest(inside=False)
429 response = self._NewResponse()
430 sdk_controller.CreateManifestFromSdk(
431 request,
432 response,
433 self.api_config,
434 )
435 impl_patch.assert_called_with(
Brian Norris41f247b2023-06-30 11:09:40 -0700436 Path(self._sdk_path),
437 Path(self._dest_dir),
Greg Edelston01ae5942023-01-30 16:26:54 -0700438 )
439 self.assertEqual(
440 response.manifest_path.location, common_pb2.Path.Location.OUTSIDE
441 )
442 self.assertEqual(response.manifest_path.path, self._manifest_path)
443
444 def testInside(self):
445 """Check that an inside path parses correctly and the call succeeds."""
446 impl_patch = self.PatchObject(
447 sdk_service,
448 "CreateManifestFromSdk",
Brian Norris41f247b2023-06-30 11:09:40 -0700449 return_value=Path(self._manifest_path),
Greg Edelston01ae5942023-01-30 16:26:54 -0700450 )
451 request = self._NewRequest(inside=True)
452 response = self._NewResponse()
453 sdk_controller.CreateManifestFromSdk(
454 request,
455 response,
456 self.api_config,
457 )
458 impl_patch.assert_called_with(
Brian Norris41f247b2023-06-30 11:09:40 -0700459 Path(self.chroot.full_path(self._sdk_path)),
460 Path(self._dest_dir),
Greg Edelston01ae5942023-01-30 16:26:54 -0700461 )
462 self.assertEqual(
463 response.manifest_path.location, common_pb2.Path.Location.OUTSIDE
464 )
465 self.assertEqual(response.manifest_path.path, self._manifest_path)
466
467
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700468class BuildSdkToolchainTest(
469 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
470):
471 """Test the SdkService/BuildSdkToolchain endpoint."""
472
473 def setUp(self):
474 """Set up the test case."""
475 self._chroot_path = "/path/to/chroot"
Greg Edelstone265a172023-06-26 17:20:05 -0600476 self._result_dir = "/out/toolchain-pkgs/"
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700477 self._response = sdk_pb2.BuildSdkToolchainResponse()
478 self._generated_filenames = (
479 "armv7a-cros-linux-gnueabihf.tar.xz",
480 "x86_64-cros-linux-gnu.tar.xz",
481 )
482 self._paths_for_generated_files = [
483 common_pb2.Path(
484 path=os.path.join(constants.SDK_TOOLCHAINS_OUTPUT, fname),
485 location=common_pb2.Path.Location.INSIDE,
486 )
487 for fname in self._generated_filenames
488 ]
489
490 def _NewRequest(
491 self,
492 chroot_path: Optional[str] = None,
493 use_flags: Optional[List[str]] = None,
494 ) -> sdk_pb2.BuildSdkToolchainRequest:
495 """Return a new BuildSdkToolchainRequest message."""
Greg Edelstone265a172023-06-26 17:20:05 -0600496 request = sdk_pb2.BuildSdkToolchainRequest(
497 result_path=common_pb2.ResultPath(
498 path=common_pb2.Path(
499 path=self._result_dir,
500 location=common_pb2.Path.Location.OUTSIDE,
501 )
502 )
503 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700504 if chroot_path:
505 request.chroot.path = chroot_path
506 if use_flags:
507 request.use_flags.extend(
508 common_pb2.UseFlag(flag=flag) for flag in use_flags
509 )
510 return request
511
512 def _NewResponse(
513 self, generated_filenames: Optional[List[str]] = None
514 ) -> sdk_pb2.BuildSdkToolchainResponse:
515 """Return a new BuildSdkToolchainResponse message."""
516 response = sdk_pb2.BuildSdkToolchainResponse()
517 if generated_filenames:
518 response.generated_files.extend(
519 common_pb2.Path(
Greg Edelstone265a172023-06-26 17:20:05 -0600520 path=os.path.join(self._result_dir, fname),
521 location=common_pb2.Path.Location.OUTSIDE,
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700522 )
523 for fname in generated_filenames
524 )
525 return response
526
527 def testValidateOnly(self):
528 """Check that a validate only call does not execute any logic."""
529 impl_patch = self.PatchObject(sdk_service, "BuildSdkToolchain")
530 sdk_controller.BuildSdkToolchain(
531 self._NewRequest(), self._NewResponse(), self.validate_only_config
532 )
533 impl_patch.assert_not_called()
534
535 def testSuccess(self):
536 """Check that a normal call defers to the SDK service as expected."""
537 impl_patch = self.PatchObject(sdk_service, "BuildSdkToolchain")
538 request = self._NewRequest(use_flags=[])
539 response = self._NewResponse()
540 sdk_controller.BuildSdkToolchain(
541 request,
542 response,
543 self.api_config,
544 )
545 # Can't use assert_called_with, since the chroot objects are equal but
546 # not identical.
547 impl_patch.assert_called_once()
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700548 self.assertEqual(impl_patch.call_args.kwargs["extra_env"], {})
549
550 def testSuccessWithUseFlags(self):
551 """Check that a call with USE flags works as expected."""
552 impl_patch = self.PatchObject(sdk_service, "BuildSdkToolchain")
553 request = self._NewRequest(use_flags=["llvm-next", "another-flag"])
554 response = self._NewResponse()
555 sdk_controller.BuildSdkToolchain(
556 request,
557 response,
558 self.api_config,
559 )
560 # Can't use assert_called_with, since the chroot objects are equal but
561 # not identical.
562 impl_patch.assert_called_once()
563 self.assertEqual(
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700564 impl_patch.call_args.kwargs["extra_env"],
565 {"USE": "llvm-next another-flag"},
566 )
Greg Edelston6733dc52023-02-15 15:20:07 -0700567
568
Greg Edelstond401e5a2023-04-28 15:29:11 -0600569class uprev_test(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Greg Edelston6733dc52023-02-15 15:20:07 -0700570 """Test case for SdkService/Uprev() endpoint."""
571
Greg Edelston6733dc52023-02-15 15:20:07 -0700572 _binhost_gs_bucket = "gs://chromiumos-prebuilts/"
Greg Edelston40aea812023-03-27 16:34:35 -0600573 _latest_uprev_target_version = "2023.02.19.112358"
Greg Edelston6733dc52023-02-15 15:20:07 -0700574
575 def setUp(self):
576 """Set up the test case."""
Greg Edelston6733dc52023-02-15 15:20:07 -0700577 self.PatchObject(
578 sdk_service,
Greg Edelstond401e5a2023-04-28 15:29:11 -0600579 "get_latest_uprev_target_version",
Greg Edelston40aea812023-03-27 16:34:35 -0600580 return_value=self._latest_uprev_target_version,
Greg Edelston6733dc52023-02-15 15:20:07 -0700581 )
582 self._uprev_patch = self.PatchObject(
583 sdk_service,
Greg Edelstond401e5a2023-04-28 15:29:11 -0600584 "uprev_sdk_and_prebuilts",
Greg Edelston6733dc52023-02-15 15:20:07 -0700585 )
586
Greg Edelston0382ca42023-05-04 14:00:15 -0600587 def NewRequest(
588 self, version: str = "", toolchain_tarball_template: str = ""
589 ):
Greg Edelston6733dc52023-02-15 15:20:07 -0700590 """Return a new UprevRequest with standard inputs."""
591 return sdk_pb2.UprevRequest(
Greg Edelston6733dc52023-02-15 15:20:07 -0700592 binhost_gs_bucket=self._binhost_gs_bucket,
593 version=version,
Greg Edelston0382ca42023-05-04 14:00:15 -0600594 toolchain_tarball_template=toolchain_tarball_template,
Greg Edelston6733dc52023-02-15 15:20:07 -0700595 )
596
597 @staticmethod
598 def NewResponse() -> sdk_pb2.UprevResponse:
599 """Return a new empty UprevResponse."""
600 return sdk_pb2.UprevResponse()
601
602 def testWithVersion(self):
603 """Test the endpoint with `version` specified.
604
605 In this case, we expect that sdk_controller.Uprev is called with the
606 version specified in the UprevRequest.
607 """
608 specified_version = "1970.01.01.000000"
Greg Edelston0382ca42023-05-04 14:00:15 -0600609 toolchain_tarball_template = "path/to/%(version)s/toolchain"
610 request = self.NewRequest(
611 version=specified_version,
612 toolchain_tarball_template=toolchain_tarball_template,
613 )
Greg Edelston6733dc52023-02-15 15:20:07 -0700614 response = self.NewResponse()
615 sdk_controller.Uprev(request, response, self.api_config)
616 self._uprev_patch.assert_called_with(
Greg Edelston6733dc52023-02-15 15:20:07 -0700617 binhost_gs_bucket=self._binhost_gs_bucket,
Greg Edelston8bf6d4c2023-06-30 17:14:39 -0600618 sdk_version=specified_version,
Greg Edelston0382ca42023-05-04 14:00:15 -0600619 toolchain_tarball_template=toolchain_tarball_template,
Greg Edelston6733dc52023-02-15 15:20:07 -0700620 )
621
622 def testWithoutVersion(self):
623 """Test the endpoint with `version` not specified.
624
625 In this case, we expect that sdk_controller.Uprev is called with the
Greg Edelston40aea812023-03-27 16:34:35 -0600626 latest uprev target version, based on the remote file in gs://. This is
627 fetched via sdk_controller.GetLatestUprevTargetVersionVersion
628 (mocked here in setUp()).
Greg Edelston6733dc52023-02-15 15:20:07 -0700629 """
Greg Edelston0382ca42023-05-04 14:00:15 -0600630 toolchain_tarball_template = "path/to/%(version)s/toolchain"
631 request = self.NewRequest(
632 toolchain_tarball_template=toolchain_tarball_template
633 )
Greg Edelston6733dc52023-02-15 15:20:07 -0700634 response = self.NewResponse()
635 sdk_controller.Uprev(request, response, self.api_config)
636 self._uprev_patch.assert_called_with(
Greg Edelston6733dc52023-02-15 15:20:07 -0700637 binhost_gs_bucket=self._binhost_gs_bucket,
Greg Edelston8bf6d4c2023-06-30 17:14:39 -0600638 sdk_version=self._latest_uprev_target_version,
Greg Edelston0382ca42023-05-04 14:00:15 -0600639 toolchain_tarball_template=toolchain_tarball_template,
Greg Edelston6733dc52023-02-15 15:20:07 -0700640 )
Greg Edelston0382ca42023-05-04 14:00:15 -0600641
642 def testWithoutToolchainTarballTemplate(self):
643 """Test the endpoint with `toolchain_tarball_template` not specified."""
644 request = self.NewRequest(version="1234")
645 response = self.NewResponse()
646 with self.assertRaises(cros_build_lib.DieSystemExit):
647 sdk_controller.Uprev(request, response, self.api_config)