blob: 343ed33f4e4a16b17444e2c25f022295c138dfec [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Evan Hernandezd437b4e2019-03-25 13:48:30 -06002# 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 Binhost operations."""
6
Michael Mortensenfc823882019-08-27 14:38:07 -06007import os
Brian Norrisa9cc6b32023-05-10 13:43:45 -07008from pathlib import Path
Mike Frysinger166fea02021-02-12 05:30:33 -05009from unittest import mock
Mike Frysingeref94e4c2020-02-10 23:59:54 -050010
Alex Klein231d2da2019-07-22 16:44:45 -060011from chromite.api import api_config
Evan Hernandezd437b4e2019-03-25 13:48:30 -060012from chromite.api.controller import binhost
13from chromite.api.gen.chromite.api import binhost_pb2
Greg Edelston724c13d2023-04-07 16:19:24 -060014from chromite.api.gen.chromiumos import common_pb2
15from chromite.lib import binpkg
Brian Norrisa9cc6b32023-05-10 13:43:45 -070016from chromite.lib import chroot_lib
Cindy Lind4c122a2023-04-13 19:09:31 +000017from chromite.lib import constants
LaMont Jonesc64ae212019-04-15 15:41:28 -060018from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060019from chromite.lib import cros_test_lib
Michael Mortensenfc823882019-08-27 14:38:07 -060020from chromite.lib import osutils
Evan Hernandezd437b4e2019-03-25 13:48:30 -060021from chromite.service import binhost as binhost_service
22
Alex Klein231d2da2019-07-22 16:44:45 -060023
Michael Mortensena0af77b2019-11-13 11:15:15 -070024class GetBinhostsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -060025 """Unittests for GetBinhosts."""
Michael Mortensena0af77b2019-11-13 11:15:15 -070026
Alex Klein1699fab2022-09-08 08:46:06 -060027 def setUp(self):
28 self.response = binhost_pb2.BinhostGetResponse()
Michael Mortensena0af77b2019-11-13 11:15:15 -070029
Alex Klein1699fab2022-09-08 08:46:06 -060030 def testValidateOnly(self):
31 """Check that a validate only call does not execute any logic."""
32 patch = self.PatchObject(binhost_service, "GetBinhosts")
Michael Mortensena0af77b2019-11-13 11:15:15 -070033
Alex Klein1699fab2022-09-08 08:46:06 -060034 request = binhost_pb2.BinhostGetRequest()
35 request.build_target.name = "target"
36 binhost.GetBinhosts(request, self.response, self.validate_only_config)
37 patch.assert_not_called()
Michael Mortensena0af77b2019-11-13 11:15:15 -070038
Alex Klein1699fab2022-09-08 08:46:06 -060039 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070040 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -060041 patch = self.PatchObject(binhost_service, "GetBinhosts")
Michael Mortensena0af77b2019-11-13 11:15:15 -070042
Alex Klein1699fab2022-09-08 08:46:06 -060043 input_proto = binhost_pb2.BinhostGetRequest()
44 input_proto.build_target.name = "target"
Michael Mortensena0af77b2019-11-13 11:15:15 -070045
Alex Klein1699fab2022-09-08 08:46:06 -060046 binhost.GetBinhosts(input_proto, self.response, self.mock_call_config)
Michael Mortensena0af77b2019-11-13 11:15:15 -070047
Alex Klein1699fab2022-09-08 08:46:06 -060048 self.assertEqual(len(self.response.binhosts), 1)
49 self.assertEqual(self.response.binhosts[0].package_index, "Packages")
50 patch.assert_not_called()
Michael Mortensena0af77b2019-11-13 11:15:15 -070051
Alex Klein1699fab2022-09-08 08:46:06 -060052 def testGetBinhosts(self):
53 """GetBinhosts calls service with correct args."""
Alex Kleinab87ceb2023-01-24 12:00:51 -070054 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -060055 binhost_list = [
Mike Frysingerd668a222023-09-11 23:51:55 -040056 f"{constants.TRASH_BUCKET}/board/amd64-generic/paladin-R66-17.0.0-rc2/packages/",
57 f"{constants.TRASH_BUCKET}/board/eve/paladin-R66-17.0.0-rc2/packages/",
Alex Klein1699fab2022-09-08 08:46:06 -060058 ]
Alex Kleinab87ceb2023-01-24 12:00:51 -070059 # pylint: enable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -060060 get_binhost = self.PatchObject(
61 binhost_service, "GetBinhosts", return_value=binhost_list
62 )
Michael Mortensena0af77b2019-11-13 11:15:15 -070063
Alex Klein1699fab2022-09-08 08:46:06 -060064 input_proto = binhost_pb2.BinhostGetRequest()
65 input_proto.build_target.name = "target"
Michael Mortensena0af77b2019-11-13 11:15:15 -070066
Alex Klein1699fab2022-09-08 08:46:06 -060067 binhost.GetBinhosts(input_proto, self.response, self.api_config)
Michael Mortensena0af77b2019-11-13 11:15:15 -070068
Alex Klein1699fab2022-09-08 08:46:06 -060069 self.assertEqual(len(self.response.binhosts), 2)
70 self.assertEqual(self.response.binhosts[0].package_index, "Packages")
71 get_binhost.assert_called_once_with(mock.ANY)
Michael Mortensena0af77b2019-11-13 11:15:15 -070072
73
Alex Klein1699fab2022-09-08 08:46:06 -060074class GetPrivatePrebuiltAclArgsTest(
75 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
76):
77 """Unittests for GetPrivatePrebuiltAclArgs."""
Michael Mortensen42251f92019-11-14 11:01:43 -070078
Alex Klein1699fab2022-09-08 08:46:06 -060079 def setUp(self):
80 self.response = binhost_pb2.AclArgsResponse()
Michael Mortensen42251f92019-11-14 11:01:43 -070081
Alex Klein1699fab2022-09-08 08:46:06 -060082 def testValidateOnly(self):
83 """Check that a validate only call does not execute any logic."""
84 patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs")
Michael Mortensen42251f92019-11-14 11:01:43 -070085
Alex Klein1699fab2022-09-08 08:46:06 -060086 request = binhost_pb2.AclArgsRequest()
87 request.build_target.name = "target"
88 binhost.GetPrivatePrebuiltAclArgs(
89 request, self.response, self.validate_only_config
90 )
91 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -070092
Alex Klein1699fab2022-09-08 08:46:06 -060093 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070094 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -060095 patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs")
Michael Mortensen42251f92019-11-14 11:01:43 -070096
Alex Klein1699fab2022-09-08 08:46:06 -060097 input_proto = binhost_pb2.AclArgsRequest()
98 input_proto.build_target.name = "target"
Michael Mortensen42251f92019-11-14 11:01:43 -070099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 binhost.GetPrivatePrebuiltAclArgs(
101 input_proto, self.response, self.mock_call_config
102 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 self.assertEqual(len(self.response.args), 1)
105 self.assertEqual(self.response.args[0].arg, "-g")
106 self.assertEqual(self.response.args[0].value, "group1:READ")
107 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700108
Alex Klein1699fab2022-09-08 08:46:06 -0600109 def testGetPrivatePrebuiltAclArgs(self):
110 """GetPrivatePrebuildAclsArgs calls service with correct args."""
111 argvalue_list = [["-g", "group1:READ"]]
112 get_binhost = self.PatchObject(
113 binhost_service, "GetPrebuiltAclArgs", return_value=argvalue_list
114 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 input_proto = binhost_pb2.AclArgsRequest()
117 input_proto.build_target.name = "target"
Michael Mortensen42251f92019-11-14 11:01:43 -0700118
Alex Klein1699fab2022-09-08 08:46:06 -0600119 binhost.GetPrivatePrebuiltAclArgs(
120 input_proto, self.response, self.api_config
121 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 self.assertEqual(len(self.response.args), 1)
124 self.assertEqual(self.response.args[0].arg, "-g")
125 self.assertEqual(self.response.args[0].value, "group1:READ")
126 get_binhost.assert_called_once_with(mock.ANY)
Michael Mortensen42251f92019-11-14 11:01:43 -0700127
128
Alex Klein1699fab2022-09-08 08:46:06 -0600129class PrepareBinhostUploadsTest(
130 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
131):
132 """Unittests for PrepareBinhostUploads."""
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600133
Alex Klein1699fab2022-09-08 08:46:06 -0600134 def setUp(self):
135 self.PatchObject(
136 binhost_service,
137 "GetPrebuiltsRoot",
138 return_value="/build/target/packages",
139 )
140 self.PatchObject(
141 binhost_service,
142 "GetPrebuiltsFiles",
143 return_value=["foo.tbz2", "bar.tbz2"],
144 )
145 self.PatchObject(
146 binhost_service,
147 "UpdatePackageIndex",
148 return_value="/build/target/packages/Packages",
149 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600150
Alex Klein1699fab2022-09-08 08:46:06 -0600151 self.response = binhost_pb2.PrepareBinhostUploadsResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600152
Alex Klein1699fab2022-09-08 08:46:06 -0600153 def testValidateOnly(self):
154 """Check that a validate only call does not execute any logic."""
155 patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot")
Alex Klein231d2da2019-07-22 16:44:45 -0600156
Alex Klein1699fab2022-09-08 08:46:06 -0600157 request = binhost_pb2.PrepareBinhostUploadsRequest()
158 request.build_target.name = "target"
159 request.uri = "gs://chromeos-prebuilt/target"
160 rc = binhost.PrepareBinhostUploads(
161 request, self.response, self.validate_only_config
162 )
163 patch.assert_not_called()
164 self.assertEqual(rc, 0)
Alex Klein231d2da2019-07-22 16:44:45 -0600165
Alex Klein1699fab2022-09-08 08:46:06 -0600166 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700167 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600168 patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot")
Michael Mortensen42251f92019-11-14 11:01:43 -0700169
Alex Klein1699fab2022-09-08 08:46:06 -0600170 request = binhost_pb2.PrepareBinhostUploadsRequest()
171 request.build_target.name = "target"
172 request.uri = "gs://chromeos-prebuilt/target"
173 rc = binhost.PrepareBinhostUploads(
174 request, self.response, self.mock_call_config
175 )
176 self.assertEqual(self.response.uploads_dir, "/upload/directory")
177 self.assertEqual(self.response.upload_targets[0].path, "upload_target")
178 patch.assert_not_called()
179 self.assertEqual(rc, 0)
Michael Mortensen42251f92019-11-14 11:01:43 -0700180
Alex Klein1699fab2022-09-08 08:46:06 -0600181 def testPrepareBinhostUploads(self):
182 """PrepareBinhostUploads returns Packages and tar files."""
183 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
184 input_proto.build_target.name = "target"
185 input_proto.uri = "gs://chromeos-prebuilt/target"
186 binhost.PrepareBinhostUploads(
187 input_proto, self.response, self.api_config
188 )
189 self.assertEqual(self.response.uploads_dir, "/build/target/packages")
190 self.assertCountEqual(
191 [ut.path for ut in self.response.upload_targets],
192 ["Packages", "foo.tbz2", "bar.tbz2"],
193 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600194
Alex Klein1699fab2022-09-08 08:46:06 -0600195 def testPrepareBinhostUploadsNonGsUri(self):
196 """PrepareBinhostUploads dies when URI does not point to GS."""
197 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
198 input_proto.build_target.name = "target"
199 input_proto.uri = "https://foo.bar"
200 with self.assertRaises(ValueError):
201 binhost.PrepareBinhostUploads(
202 input_proto, self.response, self.api_config
203 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600204
205
Greg Edelston724c13d2023-04-07 16:19:24 -0600206class UpdatePackageIndexTest(
207 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
208):
209 """Unit tests for BinhostService/UpdatePackageIndex."""
210
211 def setUp(self):
212 self._original_pkg_index = binpkg.PackageIndex()
213 self._original_pkg_index.header["A"] = "B"
214 self._original_pkg_index.packages = [
215 {
216 "CPV": "foo/bar",
217 "KEY": "value",
218 },
219 {
220 "CPV": "cat/pkg",
221 "KEY": "also_value",
222 },
223 ]
224 self._pkg_index_fp = os.path.join(
225 self.tempdir,
226 "path/to/packages/Packages",
227 )
228
229 def _write_original_package_index(self):
230 """Write the package index to the tempdir.
231
232 Note that if an input_proto specifies location=INSIDE, then they will
233 not be able to find the written file, since the tempdir isn't actually
234 inside a chroot.
235 """
236 osutils.Touch(self._pkg_index_fp, makedirs=True)
237 self._original_pkg_index.WriteFile(self._pkg_index_fp)
238
239 def testValidateOnly(self):
240 """Check that a validate only call does not execute any logic."""
241 self._write_original_package_index()
242 patch = self.PatchObject(binpkg.PackageIndex, "ReadFilePath")
243 request = binhost_pb2.UpdatePackageIndexRequest(
244 package_index_file=common_pb2.Path(
245 path=self._pkg_index_fp,
246 location=common_pb2.Path.Location.OUTSIDE,
247 ),
248 set_upload_location=True,
249 )
250 response = binhost_pb2.UpdatePackageIndexResponse()
251 binhost.UpdatePackageIndex(request, response, self.validate_only_config)
252 patch.assert_not_called()
253
254 def testMustProvideSomeCommand(self):
255 """Test that an error is raised if no update types are specified."""
256 self._write_original_package_index()
257 request = binhost_pb2.UpdatePackageIndexRequest(
258 package_index_file=common_pb2.Path(
259 path=self._pkg_index_fp,
260 location=common_pb2.Path.OUTSIDE,
261 ),
262 uri="gs://chromeos-prebuilt/board/amd64-host/packages",
263 )
264 response = binhost_pb2.UpdatePackageIndexResponse()
265 with self.assertRaises(cros_build_lib.DieSystemExit):
266 binhost.UpdatePackageIndex(request, response, self.api_config)
267
268 def testSetUploadLocation(self):
269 """Test setting the package upload location in the index file.
270
271 This test includes correctly parsing the input uri.
272 """
273 # Arrange
274 self._write_original_package_index()
275
276 # Act
277 request = binhost_pb2.UpdatePackageIndexRequest(
278 package_index_file=common_pb2.Path(
279 path=self._pkg_index_fp,
280 location=common_pb2.Path.Location.OUTSIDE,
281 ),
282 set_upload_location=True,
283 uri="gs://chromeos-prebuilt/board/amd64-host/packages/",
284 )
285 response = binhost_pb2.UpdatePackageIndexResponse()
286 binhost.UpdatePackageIndex(request, response, self.api_config)
287
288 # Assert
289 new_pkg_index = binpkg.PackageIndex()
290 new_pkg_index.ReadFilePath(self._pkg_index_fp)
291 self.assertEqual(new_pkg_index.header["URI"], "gs://chromeos-prebuilt")
292 self.assertDictEqual(
293 new_pkg_index.packages[0],
294 {
295 "CPV": "cat/pkg",
296 "KEY": "also_value",
297 "PATH": "board/amd64-host/packages/cat/pkg.tbz2",
298 },
299 )
300 self.assertDictEqual(
301 new_pkg_index.packages[1],
302 {
303 "CPV": "foo/bar",
304 "KEY": "value",
305 "PATH": "board/amd64-host/packages/foo/bar.tbz2",
306 },
307 )
308
309
Alex Klein231d2da2019-07-22 16:44:45 -0600310class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600311 """Unittests for SetBinhost."""
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600312
Alex Klein1699fab2022-09-08 08:46:06 -0600313 def setUp(self):
314 self.response = binhost_pb2.SetBinhostResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600315
Alex Klein1699fab2022-09-08 08:46:06 -0600316 def testValidateOnly(self):
317 """Check that a validate only call does not execute any logic."""
318 patch = self.PatchObject(binhost_service, "SetBinhost")
Alex Klein231d2da2019-07-22 16:44:45 -0600319
Alex Klein1699fab2022-09-08 08:46:06 -0600320 request = binhost_pb2.SetBinhostRequest()
321 request.build_target.name = "target"
322 request.key = binhost_pb2.POSTSUBMIT_BINHOST
323 request.uri = "gs://chromeos-prebuilt/target"
324 binhost.SetBinhost(request, self.response, self.validate_only_config)
325 patch.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600326
Alex Klein1699fab2022-09-08 08:46:06 -0600327 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700328 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600329 patch = self.PatchObject(binhost_service, "SetBinhost")
Michael Mortensen42251f92019-11-14 11:01:43 -0700330
Alex Klein1699fab2022-09-08 08:46:06 -0600331 request = binhost_pb2.SetBinhostRequest()
332 request.build_target.name = "target"
333 request.key = binhost_pb2.POSTSUBMIT_BINHOST
334 request.uri = "gs://chromeos-prebuilt/target"
Arif Kasim6242cdd2022-10-19 17:51:00 +0000335 request.max_uris = 4
Alex Klein1699fab2022-09-08 08:46:06 -0600336 binhost.SetBinhost(request, self.response, self.mock_call_config)
337 patch.assert_not_called()
338 self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf")
Michael Mortensen42251f92019-11-14 11:01:43 -0700339
Alex Klein1699fab2022-09-08 08:46:06 -0600340 def testSetBinhost(self):
341 """SetBinhost calls service with correct args."""
342 set_binhost = self.PatchObject(
343 binhost_service, "SetBinhost", return_value="/path/to/BINHOST.conf"
344 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600345
Alex Klein1699fab2022-09-08 08:46:06 -0600346 input_proto = binhost_pb2.SetBinhostRequest()
347 input_proto.build_target.name = "target"
348 input_proto.private = True
349 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
350 input_proto.uri = "gs://chromeos-prebuilt/target"
Arif Kasim6242cdd2022-10-19 17:51:00 +0000351 input_proto.max_uris = 4
Alex Klein1699fab2022-09-08 08:46:06 -0600352 binhost.SetBinhost(input_proto, self.response, self.api_config)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600353
Alex Klein1699fab2022-09-08 08:46:06 -0600354 self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf")
355 set_binhost.assert_called_once_with(
356 "target",
357 "POSTSUBMIT_BINHOST",
358 "gs://chromeos-prebuilt/target",
359 private=True,
Arif Kasim6242cdd2022-10-19 17:51:00 +0000360 max_uris=4,
Alex Klein1699fab2022-09-08 08:46:06 -0600361 )
LaMont Jonesc64ae212019-04-15 15:41:28 -0600362
Alex Klein231d2da2019-07-22 16:44:45 -0600363
Arif Kasima0467262022-11-11 17:08:14 +0000364class GetBinhostConfPathTest(
365 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
366):
367 """Unittests for GetBinhostConfPath."""
368
369 def setUp(self):
370 self.response = binhost_pb2.GetBinhostConfPathResponse()
371
372 def testValidateOnly(self):
373 """Check that a validate only call does not execute any logic."""
374 patch = self.PatchObject(binhost_service, "GetBinhostConfPath")
375
376 request = binhost_pb2.GetBinhostConfPathRequest()
377 request.build_target.name = "target"
378 request.key = binhost_pb2.POSTSUBMIT_BINHOST
379 binhost.GetBinhostConfPath(
380 request, self.response, self.validate_only_config
381 )
382 patch.assert_not_called()
383
384 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700385 """Test a mock call does not execute logic, returns mocked value."""
Arif Kasima0467262022-11-11 17:08:14 +0000386 patch = self.PatchObject(binhost_service, "GetBinhostConfPath")
387
388 request = binhost_pb2.GetBinhostConfPathRequest()
389 request.build_target.name = "target"
390 request.key = binhost_pb2.POSTSUBMIT_BINHOST
391 binhost.GetBinhostConfPath(
392 request, self.response, self.mock_call_config
393 )
394 patch.assert_not_called()
395 self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf")
396
397 def testGetBinhostConfPath(self):
398 """GetBinhostConfPath calls service with correct args."""
399 get_binhost_conf_path = self.PatchObject(
400 binhost_service,
401 "GetBinhostConfPath",
402 return_value="/path/to/BINHOST.conf",
403 )
404 input_proto = binhost_pb2.GetBinhostConfPathRequest()
405 input_proto.build_target.name = "target"
406 input_proto.private = True
407 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
408 binhost.GetBinhostConfPath(input_proto, self.response, self.api_config)
409
410 self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf")
411 get_binhost_conf_path.assert_called_once_with(
412 "target",
413 "POSTSUBMIT_BINHOST",
414 True,
415 )
416
417
Alex Klein1699fab2022-09-08 08:46:06 -0600418class RegenBuildCacheTest(
419 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
420):
421 """Unittests for RegenBuildCache."""
LaMont Jonesc64ae212019-04-15 15:41:28 -0600422
Alex Klein1699fab2022-09-08 08:46:06 -0600423 def setUp(self):
424 self.response = binhost_pb2.RegenBuildCacheResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600425
Alex Klein1699fab2022-09-08 08:46:06 -0600426 def testValidateOnly(self):
427 """Check that a validate only call does not execute any logic."""
428 patch = self.PatchObject(binhost_service, "RegenBuildCache")
Alex Klein231d2da2019-07-22 16:44:45 -0600429
Alex Klein1699fab2022-09-08 08:46:06 -0600430 request = binhost_pb2.RegenBuildCacheRequest()
431 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
432 binhost.RegenBuildCache(
433 request, self.response, self.validate_only_config
434 )
435 patch.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600436
Alex Klein1699fab2022-09-08 08:46:06 -0600437 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700438 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600439 patch = self.PatchObject(binhost_service, "RegenBuildCache")
Michael Mortensen42251f92019-11-14 11:01:43 -0700440
Alex Klein1699fab2022-09-08 08:46:06 -0600441 request = binhost_pb2.RegenBuildCacheRequest()
442 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
443 binhost.RegenBuildCache(request, self.response, self.mock_call_config)
444 patch.assert_not_called()
445 self.assertEqual(len(self.response.modified_overlays), 1)
446 self.assertEqual(
447 self.response.modified_overlays[0].path, "/path/to/BuildCache"
448 )
449
450 def testRegenBuildCache(self):
451 """RegenBuildCache calls service with the correct args."""
452 regen_cache = self.PatchObject(binhost_service, "RegenBuildCache")
453
454 input_proto = binhost_pb2.RegenBuildCacheRequest()
455 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
456
457 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
458 regen_cache.assert_called_once_with(mock.ANY, "both")
459
460 def testRequiresOverlayType(self):
461 """RegenBuildCache dies if overlay_type not specified."""
462 regen_cache = self.PatchObject(binhost_service, "RegenBuildCache")
463
464 input_proto = binhost_pb2.RegenBuildCacheRequest()
465 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED
466
467 with self.assertRaises(cros_build_lib.DieSystemExit):
468 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
469 regen_cache.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700470
471
Cindy Lind4c122a2023-04-13 19:09:31 +0000472class PrepareChromeBinhostUploadsTest(
473 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
474):
475 """Tests for BinhostService/PrepareChromeBinhostUploads."""
476
477 def setUp(self):
478 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
479 self.create_chrome_package_index_mock = self.PatchObject(
480 binhost_service, "CreateChromePackageIndex"
481 )
482
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700483 self.chroot = chroot_lib.Chroot(
484 path=self.tempdir / "chroot",
485 out_path=self.tempdir / "out",
486 )
Cindy Lind4c122a2023-04-13 19:09:31 +0000487 self.sysroot_path = "build/target"
488 self.uploads_dir = self.tempdir / "uploads_dir"
489 self.input_proto = binhost_pb2.PrepareChromeBinhostUploadsRequest()
490 self.input_proto.uri = "gs://chromeos-prebuilt/target"
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700491 self.input_proto.chroot.path = str(self.chroot.path)
492 self.input_proto.chroot.out_path = str(self.chroot.out_path)
Cindy Lind4c122a2023-04-13 19:09:31 +0000493 self.input_proto.sysroot.path = self.sysroot_path
494 self.input_proto.uploads_dir = str(self.uploads_dir)
495 self.response = binhost_pb2.PrepareChromeBinhostUploadsResponse()
496
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700497 self.packages_path = Path(
498 self.chroot.full_path(self.sysroot_path, "packages")
499 )
Cindy Lind4c122a2023-04-13 19:09:31 +0000500 self.chrome_packages_path = self.packages_path / constants.CHROME_CN
501 osutils.Touch(
502 self.chrome_packages_path / "chromeos-chrome-100-r1.tbz2",
503 makedirs=True,
504 )
505 osutils.Touch(
506 self.chrome_packages_path / "chrome-icu-100-r1.tbz2",
507 makedirs=True,
508 )
509 osutils.Touch(
510 self.chrome_packages_path / "chromeos-lacros-100-r1.tbz2",
511 makedirs=True,
512 )
513
514 def testValidateOnly(self):
515 """Check that a validate only call does not execute any logic."""
516 binhost.PrepareChromeBinhostUploads(
517 self.input_proto, self.response, self.validate_only_config
518 )
519
520 self.create_chrome_package_index_mock.assert_not_called()
521
522 def testMockCall(self):
523 """Test a mock call does not execute logic, returns mocked value."""
524 binhost.PrepareChromeBinhostUploads(
525 self.input_proto, self.response, self.mock_call_config
526 )
527
528 self.assertEqual(len(self.response.upload_targets), 4)
529 self.assertEqual(self.response.upload_targets[3].path, "Packages")
530 self.create_chrome_package_index_mock.assert_not_called()
531
532 def testChromeUpload(self):
533 """Test uploads of Chrome prebuilts."""
534 expected_upload_targets = [
535 "chromeos-base/chromeos-chrome-100-r1.tbz2",
536 "chromeos-base/chrome-icu-100-r1.tbz2",
537 "chromeos-base/chromeos-lacros-100-r1.tbz2",
538 ]
539 self.create_chrome_package_index_mock.return_value = (
540 expected_upload_targets
541 )
542
543 binhost.PrepareChromeBinhostUploads(
544 self.input_proto, self.response, self.api_config
545 )
546
547 self.assertCountEqual(
548 [target.path for target in self.response.upload_targets],
549 expected_upload_targets + ["Packages"],
550 )
551
552 def testPrepareBinhostUploadsNonGsUri(self):
553 """PrepareBinhostUploads dies when URI does not point to GS."""
554 self.input_proto.uri = "https://foo.bar"
555
556 with self.assertRaises(ValueError):
557 binhost.PrepareChromeBinhostUploads(
558 self.input_proto, self.response, self.api_config
559 )