blob: baef497409952c9430b3b0aea92a289fef69c6a5 [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
Mike Frysinger166fea02021-02-12 05:30:33 -05008from unittest import mock
Mike Frysingeref94e4c2020-02-10 23:59:54 -05009
Alex Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import api_config
Evan Hernandezd437b4e2019-03-25 13:48:30 -060011from chromite.api.controller import binhost
12from chromite.api.gen.chromite.api import binhost_pb2
Greg Edelston724c13d2023-04-07 16:19:24 -060013from chromite.api.gen.chromiumos import common_pb2
14from chromite.lib import binpkg
Cindy Lind4c122a2023-04-13 19:09:31 +000015from chromite.lib import constants
LaMont Jonesc64ae212019-04-15 15:41:28 -060016from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060017from chromite.lib import cros_test_lib
Michael Mortensenfc823882019-08-27 14:38:07 -060018from chromite.lib import osutils
Evan Hernandezd437b4e2019-03-25 13:48:30 -060019from chromite.service import binhost as binhost_service
20
Alex Klein231d2da2019-07-22 16:44:45 -060021
Michael Mortensena0af77b2019-11-13 11:15:15 -070022class GetBinhostsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -060023 """Unittests for GetBinhosts."""
Michael Mortensena0af77b2019-11-13 11:15:15 -070024
Alex Klein1699fab2022-09-08 08:46:06 -060025 def setUp(self):
26 self.response = binhost_pb2.BinhostGetResponse()
Michael Mortensena0af77b2019-11-13 11:15:15 -070027
Alex Klein1699fab2022-09-08 08:46:06 -060028 def testValidateOnly(self):
29 """Check that a validate only call does not execute any logic."""
30 patch = self.PatchObject(binhost_service, "GetBinhosts")
Michael Mortensena0af77b2019-11-13 11:15:15 -070031
Alex Klein1699fab2022-09-08 08:46:06 -060032 request = binhost_pb2.BinhostGetRequest()
33 request.build_target.name = "target"
34 binhost.GetBinhosts(request, self.response, self.validate_only_config)
35 patch.assert_not_called()
Michael Mortensena0af77b2019-11-13 11:15:15 -070036
Alex Klein1699fab2022-09-08 08:46:06 -060037 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070038 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -060039 patch = self.PatchObject(binhost_service, "GetBinhosts")
Michael Mortensena0af77b2019-11-13 11:15:15 -070040
Alex Klein1699fab2022-09-08 08:46:06 -060041 input_proto = binhost_pb2.BinhostGetRequest()
42 input_proto.build_target.name = "target"
Michael Mortensena0af77b2019-11-13 11:15:15 -070043
Alex Klein1699fab2022-09-08 08:46:06 -060044 binhost.GetBinhosts(input_proto, self.response, self.mock_call_config)
Michael Mortensena0af77b2019-11-13 11:15:15 -070045
Alex Klein1699fab2022-09-08 08:46:06 -060046 self.assertEqual(len(self.response.binhosts), 1)
47 self.assertEqual(self.response.binhosts[0].package_index, "Packages")
48 patch.assert_not_called()
Michael Mortensena0af77b2019-11-13 11:15:15 -070049
Alex Klein1699fab2022-09-08 08:46:06 -060050 def testGetBinhosts(self):
51 """GetBinhosts calls service with correct args."""
Alex Kleinab87ceb2023-01-24 12:00:51 -070052 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -060053 binhost_list = [
54 "gs://cr-prebuilt/board/amd64-generic/paladin-R66-17.0.0-rc2/packages/",
55 "gs://cr-prebuilt/board/eve/paladin-R66-17.0.0-rc2/packages/",
56 ]
Alex Kleinab87ceb2023-01-24 12:00:51 -070057 # pylint: enable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -060058 get_binhost = self.PatchObject(
59 binhost_service, "GetBinhosts", return_value=binhost_list
60 )
Michael Mortensena0af77b2019-11-13 11:15:15 -070061
Alex Klein1699fab2022-09-08 08:46:06 -060062 input_proto = binhost_pb2.BinhostGetRequest()
63 input_proto.build_target.name = "target"
Michael Mortensena0af77b2019-11-13 11:15:15 -070064
Alex Klein1699fab2022-09-08 08:46:06 -060065 binhost.GetBinhosts(input_proto, self.response, self.api_config)
Michael Mortensena0af77b2019-11-13 11:15:15 -070066
Alex Klein1699fab2022-09-08 08:46:06 -060067 self.assertEqual(len(self.response.binhosts), 2)
68 self.assertEqual(self.response.binhosts[0].package_index, "Packages")
69 get_binhost.assert_called_once_with(mock.ANY)
Michael Mortensena0af77b2019-11-13 11:15:15 -070070
71
Alex Klein1699fab2022-09-08 08:46:06 -060072class GetPrivatePrebuiltAclArgsTest(
73 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
74):
75 """Unittests for GetPrivatePrebuiltAclArgs."""
Michael Mortensen42251f92019-11-14 11:01:43 -070076
Alex Klein1699fab2022-09-08 08:46:06 -060077 def setUp(self):
78 self.response = binhost_pb2.AclArgsResponse()
Michael Mortensen42251f92019-11-14 11:01:43 -070079
Alex Klein1699fab2022-09-08 08:46:06 -060080 def testValidateOnly(self):
81 """Check that a validate only call does not execute any logic."""
82 patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs")
Michael Mortensen42251f92019-11-14 11:01:43 -070083
Alex Klein1699fab2022-09-08 08:46:06 -060084 request = binhost_pb2.AclArgsRequest()
85 request.build_target.name = "target"
86 binhost.GetPrivatePrebuiltAclArgs(
87 request, self.response, self.validate_only_config
88 )
89 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -070090
Alex Klein1699fab2022-09-08 08:46:06 -060091 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070092 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -060093 patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs")
Michael Mortensen42251f92019-11-14 11:01:43 -070094
Alex Klein1699fab2022-09-08 08:46:06 -060095 input_proto = binhost_pb2.AclArgsRequest()
96 input_proto.build_target.name = "target"
Michael Mortensen42251f92019-11-14 11:01:43 -070097
Alex Klein1699fab2022-09-08 08:46:06 -060098 binhost.GetPrivatePrebuiltAclArgs(
99 input_proto, self.response, self.mock_call_config
100 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 self.assertEqual(len(self.response.args), 1)
103 self.assertEqual(self.response.args[0].arg, "-g")
104 self.assertEqual(self.response.args[0].value, "group1:READ")
105 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700106
Alex Klein1699fab2022-09-08 08:46:06 -0600107 def testGetPrivatePrebuiltAclArgs(self):
108 """GetPrivatePrebuildAclsArgs calls service with correct args."""
109 argvalue_list = [["-g", "group1:READ"]]
110 get_binhost = self.PatchObject(
111 binhost_service, "GetPrebuiltAclArgs", return_value=argvalue_list
112 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 input_proto = binhost_pb2.AclArgsRequest()
115 input_proto.build_target.name = "target"
Michael Mortensen42251f92019-11-14 11:01:43 -0700116
Alex Klein1699fab2022-09-08 08:46:06 -0600117 binhost.GetPrivatePrebuiltAclArgs(
118 input_proto, self.response, self.api_config
119 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700120
Alex Klein1699fab2022-09-08 08:46:06 -0600121 self.assertEqual(len(self.response.args), 1)
122 self.assertEqual(self.response.args[0].arg, "-g")
123 self.assertEqual(self.response.args[0].value, "group1:READ")
124 get_binhost.assert_called_once_with(mock.ANY)
Michael Mortensen42251f92019-11-14 11:01:43 -0700125
126
Alex Klein1699fab2022-09-08 08:46:06 -0600127class PrepareBinhostUploadsTest(
128 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
129):
130 """Unittests for PrepareBinhostUploads."""
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600131
Alex Klein1699fab2022-09-08 08:46:06 -0600132 def setUp(self):
133 self.PatchObject(
134 binhost_service,
135 "GetPrebuiltsRoot",
136 return_value="/build/target/packages",
137 )
138 self.PatchObject(
139 binhost_service,
140 "GetPrebuiltsFiles",
141 return_value=["foo.tbz2", "bar.tbz2"],
142 )
143 self.PatchObject(
144 binhost_service,
145 "UpdatePackageIndex",
146 return_value="/build/target/packages/Packages",
147 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600148
Alex Klein1699fab2022-09-08 08:46:06 -0600149 self.response = binhost_pb2.PrepareBinhostUploadsResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600150
Alex Klein1699fab2022-09-08 08:46:06 -0600151 def testValidateOnly(self):
152 """Check that a validate only call does not execute any logic."""
153 patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot")
Alex Klein231d2da2019-07-22 16:44:45 -0600154
Alex Klein1699fab2022-09-08 08:46:06 -0600155 request = binhost_pb2.PrepareBinhostUploadsRequest()
156 request.build_target.name = "target"
157 request.uri = "gs://chromeos-prebuilt/target"
158 rc = binhost.PrepareBinhostUploads(
159 request, self.response, self.validate_only_config
160 )
161 patch.assert_not_called()
162 self.assertEqual(rc, 0)
Alex Klein231d2da2019-07-22 16:44:45 -0600163
Alex Klein1699fab2022-09-08 08:46:06 -0600164 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700165 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600166 patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot")
Michael Mortensen42251f92019-11-14 11:01:43 -0700167
Alex Klein1699fab2022-09-08 08:46:06 -0600168 request = binhost_pb2.PrepareBinhostUploadsRequest()
169 request.build_target.name = "target"
170 request.uri = "gs://chromeos-prebuilt/target"
171 rc = binhost.PrepareBinhostUploads(
172 request, self.response, self.mock_call_config
173 )
174 self.assertEqual(self.response.uploads_dir, "/upload/directory")
175 self.assertEqual(self.response.upload_targets[0].path, "upload_target")
176 patch.assert_not_called()
177 self.assertEqual(rc, 0)
Michael Mortensen42251f92019-11-14 11:01:43 -0700178
Alex Klein1699fab2022-09-08 08:46:06 -0600179 def testPrepareBinhostUploads(self):
180 """PrepareBinhostUploads returns Packages and tar files."""
181 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
182 input_proto.build_target.name = "target"
183 input_proto.uri = "gs://chromeos-prebuilt/target"
184 binhost.PrepareBinhostUploads(
185 input_proto, self.response, self.api_config
186 )
187 self.assertEqual(self.response.uploads_dir, "/build/target/packages")
188 self.assertCountEqual(
189 [ut.path for ut in self.response.upload_targets],
190 ["Packages", "foo.tbz2", "bar.tbz2"],
191 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600192
Alex Klein1699fab2022-09-08 08:46:06 -0600193 def testPrepareBinhostUploadsNonGsUri(self):
194 """PrepareBinhostUploads dies when URI does not point to GS."""
195 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
196 input_proto.build_target.name = "target"
197 input_proto.uri = "https://foo.bar"
198 with self.assertRaises(ValueError):
199 binhost.PrepareBinhostUploads(
200 input_proto, self.response, self.api_config
201 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600202
203
Greg Edelston724c13d2023-04-07 16:19:24 -0600204class UpdatePackageIndexTest(
205 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
206):
207 """Unit tests for BinhostService/UpdatePackageIndex."""
208
209 def setUp(self):
210 self._original_pkg_index = binpkg.PackageIndex()
211 self._original_pkg_index.header["A"] = "B"
212 self._original_pkg_index.packages = [
213 {
214 "CPV": "foo/bar",
215 "KEY": "value",
216 },
217 {
218 "CPV": "cat/pkg",
219 "KEY": "also_value",
220 },
221 ]
222 self._pkg_index_fp = os.path.join(
223 self.tempdir,
224 "path/to/packages/Packages",
225 )
226
227 def _write_original_package_index(self):
228 """Write the package index to the tempdir.
229
230 Note that if an input_proto specifies location=INSIDE, then they will
231 not be able to find the written file, since the tempdir isn't actually
232 inside a chroot.
233 """
234 osutils.Touch(self._pkg_index_fp, makedirs=True)
235 self._original_pkg_index.WriteFile(self._pkg_index_fp)
236
237 def testValidateOnly(self):
238 """Check that a validate only call does not execute any logic."""
239 self._write_original_package_index()
240 patch = self.PatchObject(binpkg.PackageIndex, "ReadFilePath")
241 request = binhost_pb2.UpdatePackageIndexRequest(
242 package_index_file=common_pb2.Path(
243 path=self._pkg_index_fp,
244 location=common_pb2.Path.Location.OUTSIDE,
245 ),
246 set_upload_location=True,
247 )
248 response = binhost_pb2.UpdatePackageIndexResponse()
249 binhost.UpdatePackageIndex(request, response, self.validate_only_config)
250 patch.assert_not_called()
251
252 def testMustProvideSomeCommand(self):
253 """Test that an error is raised if no update types are specified."""
254 self._write_original_package_index()
255 request = binhost_pb2.UpdatePackageIndexRequest(
256 package_index_file=common_pb2.Path(
257 path=self._pkg_index_fp,
258 location=common_pb2.Path.OUTSIDE,
259 ),
260 uri="gs://chromeos-prebuilt/board/amd64-host/packages",
261 )
262 response = binhost_pb2.UpdatePackageIndexResponse()
263 with self.assertRaises(cros_build_lib.DieSystemExit):
264 binhost.UpdatePackageIndex(request, response, self.api_config)
265
266 def testSetUploadLocation(self):
267 """Test setting the package upload location in the index file.
268
269 This test includes correctly parsing the input uri.
270 """
271 # Arrange
272 self._write_original_package_index()
273
274 # Act
275 request = binhost_pb2.UpdatePackageIndexRequest(
276 package_index_file=common_pb2.Path(
277 path=self._pkg_index_fp,
278 location=common_pb2.Path.Location.OUTSIDE,
279 ),
280 set_upload_location=True,
281 uri="gs://chromeos-prebuilt/board/amd64-host/packages/",
282 )
283 response = binhost_pb2.UpdatePackageIndexResponse()
284 binhost.UpdatePackageIndex(request, response, self.api_config)
285
286 # Assert
287 new_pkg_index = binpkg.PackageIndex()
288 new_pkg_index.ReadFilePath(self._pkg_index_fp)
289 self.assertEqual(new_pkg_index.header["URI"], "gs://chromeos-prebuilt")
290 self.assertDictEqual(
291 new_pkg_index.packages[0],
292 {
293 "CPV": "cat/pkg",
294 "KEY": "also_value",
295 "PATH": "board/amd64-host/packages/cat/pkg.tbz2",
296 },
297 )
298 self.assertDictEqual(
299 new_pkg_index.packages[1],
300 {
301 "CPV": "foo/bar",
302 "KEY": "value",
303 "PATH": "board/amd64-host/packages/foo/bar.tbz2",
304 },
305 )
306
307
Alex Klein231d2da2019-07-22 16:44:45 -0600308class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600309 """Unittests for SetBinhost."""
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600310
Alex Klein1699fab2022-09-08 08:46:06 -0600311 def setUp(self):
312 self.response = binhost_pb2.SetBinhostResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600313
Alex Klein1699fab2022-09-08 08:46:06 -0600314 def testValidateOnly(self):
315 """Check that a validate only call does not execute any logic."""
316 patch = self.PatchObject(binhost_service, "SetBinhost")
Alex Klein231d2da2019-07-22 16:44:45 -0600317
Alex Klein1699fab2022-09-08 08:46:06 -0600318 request = binhost_pb2.SetBinhostRequest()
319 request.build_target.name = "target"
320 request.key = binhost_pb2.POSTSUBMIT_BINHOST
321 request.uri = "gs://chromeos-prebuilt/target"
322 binhost.SetBinhost(request, self.response, self.validate_only_config)
323 patch.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600324
Alex Klein1699fab2022-09-08 08:46:06 -0600325 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700326 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600327 patch = self.PatchObject(binhost_service, "SetBinhost")
Michael Mortensen42251f92019-11-14 11:01:43 -0700328
Alex Klein1699fab2022-09-08 08:46:06 -0600329 request = binhost_pb2.SetBinhostRequest()
330 request.build_target.name = "target"
331 request.key = binhost_pb2.POSTSUBMIT_BINHOST
332 request.uri = "gs://chromeos-prebuilt/target"
Arif Kasim6242cdd2022-10-19 17:51:00 +0000333 request.max_uris = 4
Alex Klein1699fab2022-09-08 08:46:06 -0600334 binhost.SetBinhost(request, self.response, self.mock_call_config)
335 patch.assert_not_called()
336 self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf")
Michael Mortensen42251f92019-11-14 11:01:43 -0700337
Alex Klein1699fab2022-09-08 08:46:06 -0600338 def testSetBinhost(self):
339 """SetBinhost calls service with correct args."""
340 set_binhost = self.PatchObject(
341 binhost_service, "SetBinhost", return_value="/path/to/BINHOST.conf"
342 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600343
Alex Klein1699fab2022-09-08 08:46:06 -0600344 input_proto = binhost_pb2.SetBinhostRequest()
345 input_proto.build_target.name = "target"
346 input_proto.private = True
347 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
348 input_proto.uri = "gs://chromeos-prebuilt/target"
Arif Kasim6242cdd2022-10-19 17:51:00 +0000349 input_proto.max_uris = 4
Alex Klein1699fab2022-09-08 08:46:06 -0600350 binhost.SetBinhost(input_proto, self.response, self.api_config)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600351
Alex Klein1699fab2022-09-08 08:46:06 -0600352 self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf")
353 set_binhost.assert_called_once_with(
354 "target",
355 "POSTSUBMIT_BINHOST",
356 "gs://chromeos-prebuilt/target",
357 private=True,
Arif Kasim6242cdd2022-10-19 17:51:00 +0000358 max_uris=4,
Alex Klein1699fab2022-09-08 08:46:06 -0600359 )
LaMont Jonesc64ae212019-04-15 15:41:28 -0600360
Alex Klein231d2da2019-07-22 16:44:45 -0600361
Arif Kasima0467262022-11-11 17:08:14 +0000362class GetBinhostConfPathTest(
363 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
364):
365 """Unittests for GetBinhostConfPath."""
366
367 def setUp(self):
368 self.response = binhost_pb2.GetBinhostConfPathResponse()
369
370 def testValidateOnly(self):
371 """Check that a validate only call does not execute any logic."""
372 patch = self.PatchObject(binhost_service, "GetBinhostConfPath")
373
374 request = binhost_pb2.GetBinhostConfPathRequest()
375 request.build_target.name = "target"
376 request.key = binhost_pb2.POSTSUBMIT_BINHOST
377 binhost.GetBinhostConfPath(
378 request, self.response, self.validate_only_config
379 )
380 patch.assert_not_called()
381
382 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700383 """Test a mock call does not execute logic, returns mocked value."""
Arif Kasima0467262022-11-11 17:08:14 +0000384 patch = self.PatchObject(binhost_service, "GetBinhostConfPath")
385
386 request = binhost_pb2.GetBinhostConfPathRequest()
387 request.build_target.name = "target"
388 request.key = binhost_pb2.POSTSUBMIT_BINHOST
389 binhost.GetBinhostConfPath(
390 request, self.response, self.mock_call_config
391 )
392 patch.assert_not_called()
393 self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf")
394
395 def testGetBinhostConfPath(self):
396 """GetBinhostConfPath calls service with correct args."""
397 get_binhost_conf_path = self.PatchObject(
398 binhost_service,
399 "GetBinhostConfPath",
400 return_value="/path/to/BINHOST.conf",
401 )
402 input_proto = binhost_pb2.GetBinhostConfPathRequest()
403 input_proto.build_target.name = "target"
404 input_proto.private = True
405 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
406 binhost.GetBinhostConfPath(input_proto, self.response, self.api_config)
407
408 self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf")
409 get_binhost_conf_path.assert_called_once_with(
410 "target",
411 "POSTSUBMIT_BINHOST",
412 True,
413 )
414
415
Alex Klein1699fab2022-09-08 08:46:06 -0600416class RegenBuildCacheTest(
417 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
418):
419 """Unittests for RegenBuildCache."""
LaMont Jonesc64ae212019-04-15 15:41:28 -0600420
Alex Klein1699fab2022-09-08 08:46:06 -0600421 def setUp(self):
422 self.response = binhost_pb2.RegenBuildCacheResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600423
Alex Klein1699fab2022-09-08 08:46:06 -0600424 def testValidateOnly(self):
425 """Check that a validate only call does not execute any logic."""
426 patch = self.PatchObject(binhost_service, "RegenBuildCache")
Alex Klein231d2da2019-07-22 16:44:45 -0600427
Alex Klein1699fab2022-09-08 08:46:06 -0600428 request = binhost_pb2.RegenBuildCacheRequest()
429 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
430 binhost.RegenBuildCache(
431 request, self.response, self.validate_only_config
432 )
433 patch.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600434
Alex Klein1699fab2022-09-08 08:46:06 -0600435 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700436 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600437 patch = self.PatchObject(binhost_service, "RegenBuildCache")
Michael Mortensen42251f92019-11-14 11:01:43 -0700438
Alex Klein1699fab2022-09-08 08:46:06 -0600439 request = binhost_pb2.RegenBuildCacheRequest()
440 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
441 binhost.RegenBuildCache(request, self.response, self.mock_call_config)
442 patch.assert_not_called()
443 self.assertEqual(len(self.response.modified_overlays), 1)
444 self.assertEqual(
445 self.response.modified_overlays[0].path, "/path/to/BuildCache"
446 )
447
448 def testRegenBuildCache(self):
449 """RegenBuildCache calls service with the correct args."""
450 regen_cache = self.PatchObject(binhost_service, "RegenBuildCache")
451
452 input_proto = binhost_pb2.RegenBuildCacheRequest()
453 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
454
455 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
456 regen_cache.assert_called_once_with(mock.ANY, "both")
457
458 def testRequiresOverlayType(self):
459 """RegenBuildCache dies if overlay_type not specified."""
460 regen_cache = self.PatchObject(binhost_service, "RegenBuildCache")
461
462 input_proto = binhost_pb2.RegenBuildCacheRequest()
463 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED
464
465 with self.assertRaises(cros_build_lib.DieSystemExit):
466 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
467 regen_cache.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700468
469
Alex Klein1699fab2022-09-08 08:46:06 -0600470class PrepareDevInstallerBinhostUploadsTest(
471 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
472):
473 """Tests for the UploadDevInstallerPrebuilts stage."""
LaMont Jonesc64ae212019-04-15 15:41:28 -0600474
Alex Klein1699fab2022-09-08 08:46:06 -0600475 def setUp(self):
476 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
477 # target packages dir
478 self.chroot_path = os.path.join(self.tempdir, "chroot")
479 self.sysroot_path = "/build/target"
480 self.chroot_path = os.path.join(self.tempdir, "chroot")
481 full_sysroot_path = os.path.join(
482 self.chroot_path, self.sysroot_path.lstrip(os.sep)
483 )
484 self.full_sysroot_package_path = os.path.join(
485 full_sysroot_path, "packages"
486 )
487 osutils.SafeMakedirs(self.full_sysroot_package_path)
488 self.uploads_dir = os.path.join(self.tempdir, "uploads_dir")
489 osutils.SafeMakedirs(self.uploads_dir)
490 # Create packages/Packages file
491 packages_file = os.path.join(self.full_sysroot_package_path, "Packages")
492 packages_content = """\
Michael Mortensenfc823882019-08-27 14:38:07 -0600493USE: test
494
495CPV: app-arch/brotli-1.0.6
496
497CPV: app-arch/zip-3.0-r3
498
499CPV: chromeos-base/shill-0.0.1-r1
500
501CPV: chromeos-base/test-0.0.1-r1
502
503CPV: virtual/chromium-os-printing-1-r4
504
505CPV: virtual/python-enum34-1
506
507"""
Alex Klein1699fab2022-09-08 08:46:06 -0600508 osutils.WriteFile(packages_file, packages_content)
Michael Mortensenfc823882019-08-27 14:38:07 -0600509
Alex Klein1699fab2022-09-08 08:46:06 -0600510 # Create package.installable file
511 self.dev_install_packages = [
512 "app-arch/zip-3.0-r3",
513 "virtual/chromium-os-printing-1-r4",
514 "virtual/python-enum34-1",
515 ]
516 package_installable_dir = os.path.join(
517 full_sysroot_path, "build/dev-install"
518 )
519 osutils.SafeMakedirs(package_installable_dir)
520 package_installable_filename = os.path.join(
521 package_installable_dir, "package.installable"
522 )
Michael Mortensenfc823882019-08-27 14:38:07 -0600523
Alex Klein1699fab2022-09-08 08:46:06 -0600524 # Create path to the dev_install_packages
525 packages_dir = os.path.join(full_sysroot_path, "packages")
526 osutils.SafeMakedirs(packages_dir)
527 for package in self.dev_install_packages:
Alex Kleinab87ceb2023-01-24 12:00:51 -0700528 # Since a package has a category, such as app-arch/zip-3.0-r3, we
529 # need to create the packages_dir / category dir as needed.
Alex Klein1699fab2022-09-08 08:46:06 -0600530 category = package.split(os.sep)[0]
531 osutils.SafeMakedirs(os.path.join(packages_dir, category))
532 package_tbz2_file = os.path.join(packages_dir, package) + ".tbz2"
533 osutils.Touch(package_tbz2_file)
534 with open(
Mike Frysingerd97829b2023-02-24 16:09:20 -0500535 package_installable_filename, "w", encoding="utf-8"
Alex Klein1699fab2022-09-08 08:46:06 -0600536 ) as package_installable_file:
537 for package in self.dev_install_packages:
538 package_installable_file.write(package + "\n")
539 self.response = binhost_pb2.PrepareDevInstallBinhostUploadsResponse()
Michael Mortensenfc823882019-08-27 14:38:07 -0600540
Alex Klein1699fab2022-09-08 08:46:06 -0600541 def testValidateOnly(self):
542 """Check that a validate only call does not execute any logic."""
543 patch = self.PatchObject(
544 binhost_service, "ReadDevInstallFilesToCreatePackageIndex"
545 )
Michael Mortensenfc823882019-08-27 14:38:07 -0600546
Alex Klein1699fab2022-09-08 08:46:06 -0600547 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
548 input_proto.uri = "gs://chromeos-prebuilt/target"
549 input_proto.chroot.path = self.chroot_path
550 input_proto.sysroot.path = self.sysroot_path
551 input_proto.uploads_dir = self.uploads_dir
552 binhost.PrepareDevInstallBinhostUploads(
553 input_proto, self.response, self.validate_only_config
554 )
555 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700556
Alex Klein1699fab2022-09-08 08:46:06 -0600557 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700558 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600559 patch = self.PatchObject(
560 binhost_service, "ReadDevInstallFilesToCreatePackageIndex"
561 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700562
Alex Klein1699fab2022-09-08 08:46:06 -0600563 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
564 input_proto.uri = "gs://chromeos-prebuilt/target"
565 input_proto.chroot.path = self.chroot_path
566 input_proto.sysroot.path = self.sysroot_path
567 input_proto.uploads_dir = self.uploads_dir
568 binhost.PrepareDevInstallBinhostUploads(
569 input_proto, self.response, self.mock_call_config
570 )
571 self.assertEqual(len(self.response.upload_targets), 3)
572 self.assertEqual(self.response.upload_targets[2].path, "Packages")
573 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700574
Alex Klein1699fab2022-09-08 08:46:06 -0600575 def testDevInstallerUpload(self):
576 """Test uploads of dev installer prebuilts."""
577 # self.RunStage()
578 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
579 input_proto.uri = "gs://chromeos-prebuilt/target"
580 input_proto.chroot.path = self.chroot_path
581 input_proto.sysroot.path = self.sysroot_path
582 input_proto.uploads_dir = self.uploads_dir
583 # Call method under test
584 binhost.PrepareDevInstallBinhostUploads(
585 input_proto, self.response, self.api_config
586 )
587 # Verify results
588 expected_upload_targets = [
589 "app-arch/zip-3.0-r3.tbz2",
590 "virtual/chromium-os-printing-1-r4.tbz2",
591 "virtual/python-enum34-1.tbz2",
592 "Packages",
593 ]
594 self.assertCountEqual(
595 [ut.path for ut in self.response.upload_targets],
596 expected_upload_targets,
597 )
598 # All of the upload_targets should also be in the uploads_directory
599 for target in self.response.upload_targets:
600 self.assertExists(
601 os.path.join(input_proto.uploads_dir, target.path)
602 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700603
Alex Klein1699fab2022-09-08 08:46:06 -0600604 def testPrepareBinhostUploadsNonGsUri(self):
605 """PrepareBinhostUploads dies when URI does not point to GS."""
606 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
607 input_proto.chroot.path = self.chroot_path
608 input_proto.sysroot.path = self.sysroot_path
609 input_proto.uploads_dir = self.uploads_dir
610 input_proto.uri = "https://foo.bar"
611 with self.assertRaises(ValueError):
612 binhost.PrepareDevInstallBinhostUploads(
613 input_proto, self.response, self.api_config
614 )
Cindy Lind4c122a2023-04-13 19:09:31 +0000615
616
617class PrepareChromeBinhostUploadsTest(
618 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
619):
620 """Tests for BinhostService/PrepareChromeBinhostUploads."""
621
622 def setUp(self):
623 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
624 self.create_chrome_package_index_mock = self.PatchObject(
625 binhost_service, "CreateChromePackageIndex"
626 )
627
628 self.chroot_path = self.tempdir / "chroot"
629 self.sysroot_path = "build/target"
630 self.uploads_dir = self.tempdir / "uploads_dir"
631 self.input_proto = binhost_pb2.PrepareChromeBinhostUploadsRequest()
632 self.input_proto.uri = "gs://chromeos-prebuilt/target"
633 self.input_proto.chroot.path = str(self.chroot_path)
634 self.input_proto.sysroot.path = self.sysroot_path
635 self.input_proto.uploads_dir = str(self.uploads_dir)
636 self.response = binhost_pb2.PrepareChromeBinhostUploadsResponse()
637
638 self.packages_path = self.chroot_path / self.sysroot_path / "packages"
639 self.chrome_packages_path = self.packages_path / constants.CHROME_CN
640 osutils.Touch(
641 self.chrome_packages_path / "chromeos-chrome-100-r1.tbz2",
642 makedirs=True,
643 )
644 osutils.Touch(
645 self.chrome_packages_path / "chrome-icu-100-r1.tbz2",
646 makedirs=True,
647 )
648 osutils.Touch(
649 self.chrome_packages_path / "chromeos-lacros-100-r1.tbz2",
650 makedirs=True,
651 )
652
653 def testValidateOnly(self):
654 """Check that a validate only call does not execute any logic."""
655 binhost.PrepareChromeBinhostUploads(
656 self.input_proto, self.response, self.validate_only_config
657 )
658
659 self.create_chrome_package_index_mock.assert_not_called()
660
661 def testMockCall(self):
662 """Test a mock call does not execute logic, returns mocked value."""
663 binhost.PrepareChromeBinhostUploads(
664 self.input_proto, self.response, self.mock_call_config
665 )
666
667 self.assertEqual(len(self.response.upload_targets), 4)
668 self.assertEqual(self.response.upload_targets[3].path, "Packages")
669 self.create_chrome_package_index_mock.assert_not_called()
670
671 def testChromeUpload(self):
672 """Test uploads of Chrome prebuilts."""
673 expected_upload_targets = [
674 "chromeos-base/chromeos-chrome-100-r1.tbz2",
675 "chromeos-base/chrome-icu-100-r1.tbz2",
676 "chromeos-base/chromeos-lacros-100-r1.tbz2",
677 ]
678 self.create_chrome_package_index_mock.return_value = (
679 expected_upload_targets
680 )
681
682 binhost.PrepareChromeBinhostUploads(
683 self.input_proto, self.response, self.api_config
684 )
685
686 self.assertCountEqual(
687 [target.path for target in self.response.upload_targets],
688 expected_upload_targets + ["Packages"],
689 )
690
691 def testPrepareBinhostUploadsNonGsUri(self):
692 """PrepareBinhostUploads dies when URI does not point to GS."""
693 self.input_proto.uri = "https://foo.bar"
694
695 with self.assertRaises(ValueError):
696 binhost.PrepareChromeBinhostUploads(
697 self.input_proto, self.response, self.api_config
698 )