blob: ae1101a241e20efc261fde91abb8ab6f4cd2ddc1 [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 = [
56 "gs://cr-prebuilt/board/amd64-generic/paladin-R66-17.0.0-rc2/packages/",
57 "gs://cr-prebuilt/board/eve/paladin-R66-17.0.0-rc2/packages/",
58 ]
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
Alex Klein1699fab2022-09-08 08:46:06 -0600472class PrepareDevInstallerBinhostUploadsTest(
473 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
474):
475 """Tests for the UploadDevInstallerPrebuilts stage."""
LaMont Jonesc64ae212019-04-15 15:41:28 -0600476
Alex Klein1699fab2022-09-08 08:46:06 -0600477 def setUp(self):
478 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
479 # target packages dir
Alex Klein1699fab2022-09-08 08:46:06 -0600480 self.sysroot_path = "/build/target"
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700481 self.chroot = chroot_lib.Chroot(
482 path=self.tempdir / "chroot",
483 out_path=self.tempdir / "out",
Alex Klein1699fab2022-09-08 08:46:06 -0600484 )
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700485 full_sysroot_path = self.chroot.full_path(self.sysroot_path)
Alex Klein1699fab2022-09-08 08:46:06 -0600486 self.full_sysroot_package_path = os.path.join(
487 full_sysroot_path, "packages"
488 )
489 osutils.SafeMakedirs(self.full_sysroot_package_path)
490 self.uploads_dir = os.path.join(self.tempdir, "uploads_dir")
491 osutils.SafeMakedirs(self.uploads_dir)
492 # Create packages/Packages file
493 packages_file = os.path.join(self.full_sysroot_package_path, "Packages")
494 packages_content = """\
Michael Mortensenfc823882019-08-27 14:38:07 -0600495USE: test
496
497CPV: app-arch/brotli-1.0.6
498
499CPV: app-arch/zip-3.0-r3
500
501CPV: chromeos-base/shill-0.0.1-r1
502
503CPV: chromeos-base/test-0.0.1-r1
504
505CPV: virtual/chromium-os-printing-1-r4
506
507CPV: virtual/python-enum34-1
508
509"""
Alex Klein1699fab2022-09-08 08:46:06 -0600510 osutils.WriteFile(packages_file, packages_content)
Michael Mortensenfc823882019-08-27 14:38:07 -0600511
Alex Klein1699fab2022-09-08 08:46:06 -0600512 # Create package.installable file
513 self.dev_install_packages = [
514 "app-arch/zip-3.0-r3",
515 "virtual/chromium-os-printing-1-r4",
516 "virtual/python-enum34-1",
517 ]
518 package_installable_dir = os.path.join(
519 full_sysroot_path, "build/dev-install"
520 )
521 osutils.SafeMakedirs(package_installable_dir)
522 package_installable_filename = os.path.join(
523 package_installable_dir, "package.installable"
524 )
Michael Mortensenfc823882019-08-27 14:38:07 -0600525
Alex Klein1699fab2022-09-08 08:46:06 -0600526 # Create path to the dev_install_packages
527 packages_dir = os.path.join(full_sysroot_path, "packages")
528 osutils.SafeMakedirs(packages_dir)
529 for package in self.dev_install_packages:
Alex Kleinab87ceb2023-01-24 12:00:51 -0700530 # Since a package has a category, such as app-arch/zip-3.0-r3, we
531 # need to create the packages_dir / category dir as needed.
Trent Apted593c0742023-05-05 03:50:20 +0000532 # TODO(b/236161656): Fix.
533 # pylint: disable-next=use-maxsplit-arg
Alex Klein1699fab2022-09-08 08:46:06 -0600534 category = package.split(os.sep)[0]
535 osutils.SafeMakedirs(os.path.join(packages_dir, category))
536 package_tbz2_file = os.path.join(packages_dir, package) + ".tbz2"
537 osutils.Touch(package_tbz2_file)
538 with open(
Mike Frysingerd97829b2023-02-24 16:09:20 -0500539 package_installable_filename, "w", encoding="utf-8"
Alex Klein1699fab2022-09-08 08:46:06 -0600540 ) as package_installable_file:
541 for package in self.dev_install_packages:
542 package_installable_file.write(package + "\n")
543 self.response = binhost_pb2.PrepareDevInstallBinhostUploadsResponse()
Michael Mortensenfc823882019-08-27 14:38:07 -0600544
Alex Klein1699fab2022-09-08 08:46:06 -0600545 def testValidateOnly(self):
546 """Check that a validate only call does not execute any logic."""
547 patch = self.PatchObject(
548 binhost_service, "ReadDevInstallFilesToCreatePackageIndex"
549 )
Michael Mortensenfc823882019-08-27 14:38:07 -0600550
Alex Klein1699fab2022-09-08 08:46:06 -0600551 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
552 input_proto.uri = "gs://chromeos-prebuilt/target"
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700553 input_proto.chroot.path = self.chroot.path
554 input_proto.chroot.out_path = str(self.chroot.out_path)
Alex Klein1699fab2022-09-08 08:46:06 -0600555 input_proto.sysroot.path = self.sysroot_path
556 input_proto.uploads_dir = self.uploads_dir
557 binhost.PrepareDevInstallBinhostUploads(
558 input_proto, self.response, self.validate_only_config
559 )
560 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700561
Alex Klein1699fab2022-09-08 08:46:06 -0600562 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700563 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600564 patch = self.PatchObject(
565 binhost_service, "ReadDevInstallFilesToCreatePackageIndex"
566 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700567
Alex Klein1699fab2022-09-08 08:46:06 -0600568 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
569 input_proto.uri = "gs://chromeos-prebuilt/target"
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700570 input_proto.chroot.path = self.chroot.path
571 input_proto.chroot.out_path = str(self.chroot.out_path)
Alex Klein1699fab2022-09-08 08:46:06 -0600572 input_proto.sysroot.path = self.sysroot_path
573 input_proto.uploads_dir = self.uploads_dir
574 binhost.PrepareDevInstallBinhostUploads(
575 input_proto, self.response, self.mock_call_config
576 )
577 self.assertEqual(len(self.response.upload_targets), 3)
578 self.assertEqual(self.response.upload_targets[2].path, "Packages")
579 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700580
Alex Klein1699fab2022-09-08 08:46:06 -0600581 def testDevInstallerUpload(self):
582 """Test uploads of dev installer prebuilts."""
583 # self.RunStage()
584 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
585 input_proto.uri = "gs://chromeos-prebuilt/target"
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700586 input_proto.chroot.path = self.chroot.path
587 input_proto.chroot.out_path = str(self.chroot.out_path)
Alex Klein1699fab2022-09-08 08:46:06 -0600588 input_proto.sysroot.path = self.sysroot_path
589 input_proto.uploads_dir = self.uploads_dir
590 # Call method under test
591 binhost.PrepareDevInstallBinhostUploads(
592 input_proto, self.response, self.api_config
593 )
594 # Verify results
595 expected_upload_targets = [
596 "app-arch/zip-3.0-r3.tbz2",
597 "virtual/chromium-os-printing-1-r4.tbz2",
598 "virtual/python-enum34-1.tbz2",
599 "Packages",
600 ]
601 self.assertCountEqual(
602 [ut.path for ut in self.response.upload_targets],
603 expected_upload_targets,
604 )
605 # All of the upload_targets should also be in the uploads_directory
606 for target in self.response.upload_targets:
607 self.assertExists(
608 os.path.join(input_proto.uploads_dir, target.path)
609 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700610
Alex Klein1699fab2022-09-08 08:46:06 -0600611 def testPrepareBinhostUploadsNonGsUri(self):
612 """PrepareBinhostUploads dies when URI does not point to GS."""
613 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700614 input_proto.chroot.path = self.chroot.path
615 input_proto.chroot.out_path = str(self.chroot.out_path)
Alex Klein1699fab2022-09-08 08:46:06 -0600616 input_proto.sysroot.path = self.sysroot_path
617 input_proto.uploads_dir = self.uploads_dir
618 input_proto.uri = "https://foo.bar"
619 with self.assertRaises(ValueError):
620 binhost.PrepareDevInstallBinhostUploads(
621 input_proto, self.response, self.api_config
622 )
Cindy Lind4c122a2023-04-13 19:09:31 +0000623
624
625class PrepareChromeBinhostUploadsTest(
626 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
627):
628 """Tests for BinhostService/PrepareChromeBinhostUploads."""
629
630 def setUp(self):
631 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
632 self.create_chrome_package_index_mock = self.PatchObject(
633 binhost_service, "CreateChromePackageIndex"
634 )
635
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700636 self.chroot = chroot_lib.Chroot(
637 path=self.tempdir / "chroot",
638 out_path=self.tempdir / "out",
639 )
Cindy Lind4c122a2023-04-13 19:09:31 +0000640 self.sysroot_path = "build/target"
641 self.uploads_dir = self.tempdir / "uploads_dir"
642 self.input_proto = binhost_pb2.PrepareChromeBinhostUploadsRequest()
643 self.input_proto.uri = "gs://chromeos-prebuilt/target"
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700644 self.input_proto.chroot.path = str(self.chroot.path)
645 self.input_proto.chroot.out_path = str(self.chroot.out_path)
Cindy Lind4c122a2023-04-13 19:09:31 +0000646 self.input_proto.sysroot.path = self.sysroot_path
647 self.input_proto.uploads_dir = str(self.uploads_dir)
648 self.response = binhost_pb2.PrepareChromeBinhostUploadsResponse()
649
Brian Norrisa9cc6b32023-05-10 13:43:45 -0700650 self.packages_path = Path(
651 self.chroot.full_path(self.sysroot_path, "packages")
652 )
Cindy Lind4c122a2023-04-13 19:09:31 +0000653 self.chrome_packages_path = self.packages_path / constants.CHROME_CN
654 osutils.Touch(
655 self.chrome_packages_path / "chromeos-chrome-100-r1.tbz2",
656 makedirs=True,
657 )
658 osutils.Touch(
659 self.chrome_packages_path / "chrome-icu-100-r1.tbz2",
660 makedirs=True,
661 )
662 osutils.Touch(
663 self.chrome_packages_path / "chromeos-lacros-100-r1.tbz2",
664 makedirs=True,
665 )
666
667 def testValidateOnly(self):
668 """Check that a validate only call does not execute any logic."""
669 binhost.PrepareChromeBinhostUploads(
670 self.input_proto, self.response, self.validate_only_config
671 )
672
673 self.create_chrome_package_index_mock.assert_not_called()
674
675 def testMockCall(self):
676 """Test a mock call does not execute logic, returns mocked value."""
677 binhost.PrepareChromeBinhostUploads(
678 self.input_proto, self.response, self.mock_call_config
679 )
680
681 self.assertEqual(len(self.response.upload_targets), 4)
682 self.assertEqual(self.response.upload_targets[3].path, "Packages")
683 self.create_chrome_package_index_mock.assert_not_called()
684
685 def testChromeUpload(self):
686 """Test uploads of Chrome prebuilts."""
687 expected_upload_targets = [
688 "chromeos-base/chromeos-chrome-100-r1.tbz2",
689 "chromeos-base/chrome-icu-100-r1.tbz2",
690 "chromeos-base/chromeos-lacros-100-r1.tbz2",
691 ]
692 self.create_chrome_package_index_mock.return_value = (
693 expected_upload_targets
694 )
695
696 binhost.PrepareChromeBinhostUploads(
697 self.input_proto, self.response, self.api_config
698 )
699
700 self.assertCountEqual(
701 [target.path for target in self.response.upload_targets],
702 expected_upload_targets + ["Packages"],
703 )
704
705 def testPrepareBinhostUploadsNonGsUri(self):
706 """PrepareBinhostUploads dies when URI does not point to GS."""
707 self.input_proto.uri = "https://foo.bar"
708
709 with self.assertRaises(ValueError):
710 binhost.PrepareChromeBinhostUploads(
711 self.input_proto, self.response, self.api_config
712 )