blob: 6512d37a92edf88a7d2df43d00663d97721bd7b3 [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
LaMont Jonesc64ae212019-04-15 15:41:28 -060015from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060016from chromite.lib import cros_test_lib
Michael Mortensenfc823882019-08-27 14:38:07 -060017from chromite.lib import osutils
Evan Hernandezd437b4e2019-03-25 13:48:30 -060018from chromite.service import binhost as binhost_service
19
Alex Klein231d2da2019-07-22 16:44:45 -060020
Michael Mortensena0af77b2019-11-13 11:15:15 -070021class GetBinhostsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -060022 """Unittests for GetBinhosts."""
Michael Mortensena0af77b2019-11-13 11:15:15 -070023
Alex Klein1699fab2022-09-08 08:46:06 -060024 def setUp(self):
25 self.response = binhost_pb2.BinhostGetResponse()
Michael Mortensena0af77b2019-11-13 11:15:15 -070026
Alex Klein1699fab2022-09-08 08:46:06 -060027 def testValidateOnly(self):
28 """Check that a validate only call does not execute any logic."""
29 patch = self.PatchObject(binhost_service, "GetBinhosts")
Michael Mortensena0af77b2019-11-13 11:15:15 -070030
Alex Klein1699fab2022-09-08 08:46:06 -060031 request = binhost_pb2.BinhostGetRequest()
32 request.build_target.name = "target"
33 binhost.GetBinhosts(request, self.response, self.validate_only_config)
34 patch.assert_not_called()
Michael Mortensena0af77b2019-11-13 11:15:15 -070035
Alex Klein1699fab2022-09-08 08:46:06 -060036 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070037 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -060038 patch = self.PatchObject(binhost_service, "GetBinhosts")
Michael Mortensena0af77b2019-11-13 11:15:15 -070039
Alex Klein1699fab2022-09-08 08:46:06 -060040 input_proto = binhost_pb2.BinhostGetRequest()
41 input_proto.build_target.name = "target"
Michael Mortensena0af77b2019-11-13 11:15:15 -070042
Alex Klein1699fab2022-09-08 08:46:06 -060043 binhost.GetBinhosts(input_proto, self.response, self.mock_call_config)
Michael Mortensena0af77b2019-11-13 11:15:15 -070044
Alex Klein1699fab2022-09-08 08:46:06 -060045 self.assertEqual(len(self.response.binhosts), 1)
46 self.assertEqual(self.response.binhosts[0].package_index, "Packages")
47 patch.assert_not_called()
Michael Mortensena0af77b2019-11-13 11:15:15 -070048
Alex Klein1699fab2022-09-08 08:46:06 -060049 def testGetBinhosts(self):
50 """GetBinhosts calls service with correct args."""
Alex Kleinab87ceb2023-01-24 12:00:51 -070051 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -060052 binhost_list = [
53 "gs://cr-prebuilt/board/amd64-generic/paladin-R66-17.0.0-rc2/packages/",
54 "gs://cr-prebuilt/board/eve/paladin-R66-17.0.0-rc2/packages/",
55 ]
Alex Kleinab87ceb2023-01-24 12:00:51 -070056 # pylint: enable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -060057 get_binhost = self.PatchObject(
58 binhost_service, "GetBinhosts", return_value=binhost_list
59 )
Michael Mortensena0af77b2019-11-13 11:15:15 -070060
Alex Klein1699fab2022-09-08 08:46:06 -060061 input_proto = binhost_pb2.BinhostGetRequest()
62 input_proto.build_target.name = "target"
Michael Mortensena0af77b2019-11-13 11:15:15 -070063
Alex Klein1699fab2022-09-08 08:46:06 -060064 binhost.GetBinhosts(input_proto, self.response, self.api_config)
Michael Mortensena0af77b2019-11-13 11:15:15 -070065
Alex Klein1699fab2022-09-08 08:46:06 -060066 self.assertEqual(len(self.response.binhosts), 2)
67 self.assertEqual(self.response.binhosts[0].package_index, "Packages")
68 get_binhost.assert_called_once_with(mock.ANY)
Michael Mortensena0af77b2019-11-13 11:15:15 -070069
70
Alex Klein1699fab2022-09-08 08:46:06 -060071class GetPrivatePrebuiltAclArgsTest(
72 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
73):
74 """Unittests for GetPrivatePrebuiltAclArgs."""
Michael Mortensen42251f92019-11-14 11:01:43 -070075
Alex Klein1699fab2022-09-08 08:46:06 -060076 def setUp(self):
77 self.response = binhost_pb2.AclArgsResponse()
Michael Mortensen42251f92019-11-14 11:01:43 -070078
Alex Klein1699fab2022-09-08 08:46:06 -060079 def testValidateOnly(self):
80 """Check that a validate only call does not execute any logic."""
81 patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs")
Michael Mortensen42251f92019-11-14 11:01:43 -070082
Alex Klein1699fab2022-09-08 08:46:06 -060083 request = binhost_pb2.AclArgsRequest()
84 request.build_target.name = "target"
85 binhost.GetPrivatePrebuiltAclArgs(
86 request, self.response, self.validate_only_config
87 )
88 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -070089
Alex Klein1699fab2022-09-08 08:46:06 -060090 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070091 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -060092 patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs")
Michael Mortensen42251f92019-11-14 11:01:43 -070093
Alex Klein1699fab2022-09-08 08:46:06 -060094 input_proto = binhost_pb2.AclArgsRequest()
95 input_proto.build_target.name = "target"
Michael Mortensen42251f92019-11-14 11:01:43 -070096
Alex Klein1699fab2022-09-08 08:46:06 -060097 binhost.GetPrivatePrebuiltAclArgs(
98 input_proto, self.response, self.mock_call_config
99 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700100
Alex Klein1699fab2022-09-08 08:46:06 -0600101 self.assertEqual(len(self.response.args), 1)
102 self.assertEqual(self.response.args[0].arg, "-g")
103 self.assertEqual(self.response.args[0].value, "group1:READ")
104 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700105
Alex Klein1699fab2022-09-08 08:46:06 -0600106 def testGetPrivatePrebuiltAclArgs(self):
107 """GetPrivatePrebuildAclsArgs calls service with correct args."""
108 argvalue_list = [["-g", "group1:READ"]]
109 get_binhost = self.PatchObject(
110 binhost_service, "GetPrebuiltAclArgs", return_value=argvalue_list
111 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700112
Alex Klein1699fab2022-09-08 08:46:06 -0600113 input_proto = binhost_pb2.AclArgsRequest()
114 input_proto.build_target.name = "target"
Michael Mortensen42251f92019-11-14 11:01:43 -0700115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 binhost.GetPrivatePrebuiltAclArgs(
117 input_proto, self.response, self.api_config
118 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 self.assertEqual(len(self.response.args), 1)
121 self.assertEqual(self.response.args[0].arg, "-g")
122 self.assertEqual(self.response.args[0].value, "group1:READ")
123 get_binhost.assert_called_once_with(mock.ANY)
Michael Mortensen42251f92019-11-14 11:01:43 -0700124
125
Alex Klein1699fab2022-09-08 08:46:06 -0600126class PrepareBinhostUploadsTest(
127 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
128):
129 """Unittests for PrepareBinhostUploads."""
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600130
Alex Klein1699fab2022-09-08 08:46:06 -0600131 def setUp(self):
132 self.PatchObject(
133 binhost_service,
134 "GetPrebuiltsRoot",
135 return_value="/build/target/packages",
136 )
137 self.PatchObject(
138 binhost_service,
139 "GetPrebuiltsFiles",
140 return_value=["foo.tbz2", "bar.tbz2"],
141 )
142 self.PatchObject(
143 binhost_service,
144 "UpdatePackageIndex",
145 return_value="/build/target/packages/Packages",
146 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 self.response = binhost_pb2.PrepareBinhostUploadsResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600149
Alex Klein1699fab2022-09-08 08:46:06 -0600150 def testValidateOnly(self):
151 """Check that a validate only call does not execute any logic."""
152 patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot")
Alex Klein231d2da2019-07-22 16:44:45 -0600153
Alex Klein1699fab2022-09-08 08:46:06 -0600154 request = binhost_pb2.PrepareBinhostUploadsRequest()
155 request.build_target.name = "target"
156 request.uri = "gs://chromeos-prebuilt/target"
157 rc = binhost.PrepareBinhostUploads(
158 request, self.response, self.validate_only_config
159 )
160 patch.assert_not_called()
161 self.assertEqual(rc, 0)
Alex Klein231d2da2019-07-22 16:44:45 -0600162
Alex Klein1699fab2022-09-08 08:46:06 -0600163 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700164 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600165 patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot")
Michael Mortensen42251f92019-11-14 11:01:43 -0700166
Alex Klein1699fab2022-09-08 08:46:06 -0600167 request = binhost_pb2.PrepareBinhostUploadsRequest()
168 request.build_target.name = "target"
169 request.uri = "gs://chromeos-prebuilt/target"
170 rc = binhost.PrepareBinhostUploads(
171 request, self.response, self.mock_call_config
172 )
173 self.assertEqual(self.response.uploads_dir, "/upload/directory")
174 self.assertEqual(self.response.upload_targets[0].path, "upload_target")
175 patch.assert_not_called()
176 self.assertEqual(rc, 0)
Michael Mortensen42251f92019-11-14 11:01:43 -0700177
Alex Klein1699fab2022-09-08 08:46:06 -0600178 def testPrepareBinhostUploads(self):
179 """PrepareBinhostUploads returns Packages and tar files."""
180 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
181 input_proto.build_target.name = "target"
182 input_proto.uri = "gs://chromeos-prebuilt/target"
183 binhost.PrepareBinhostUploads(
184 input_proto, self.response, self.api_config
185 )
186 self.assertEqual(self.response.uploads_dir, "/build/target/packages")
187 self.assertCountEqual(
188 [ut.path for ut in self.response.upload_targets],
189 ["Packages", "foo.tbz2", "bar.tbz2"],
190 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600191
Alex Klein1699fab2022-09-08 08:46:06 -0600192 def testPrepareBinhostUploadsNonGsUri(self):
193 """PrepareBinhostUploads dies when URI does not point to GS."""
194 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
195 input_proto.build_target.name = "target"
196 input_proto.uri = "https://foo.bar"
197 with self.assertRaises(ValueError):
198 binhost.PrepareBinhostUploads(
199 input_proto, self.response, self.api_config
200 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600201
202
Greg Edelston724c13d2023-04-07 16:19:24 -0600203class UpdatePackageIndexTest(
204 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
205):
206 """Unit tests for BinhostService/UpdatePackageIndex."""
207
208 def setUp(self):
209 self._original_pkg_index = binpkg.PackageIndex()
210 self._original_pkg_index.header["A"] = "B"
211 self._original_pkg_index.packages = [
212 {
213 "CPV": "foo/bar",
214 "KEY": "value",
215 },
216 {
217 "CPV": "cat/pkg",
218 "KEY": "also_value",
219 },
220 ]
221 self._pkg_index_fp = os.path.join(
222 self.tempdir,
223 "path/to/packages/Packages",
224 )
225
226 def _write_original_package_index(self):
227 """Write the package index to the tempdir.
228
229 Note that if an input_proto specifies location=INSIDE, then they will
230 not be able to find the written file, since the tempdir isn't actually
231 inside a chroot.
232 """
233 osutils.Touch(self._pkg_index_fp, makedirs=True)
234 self._original_pkg_index.WriteFile(self._pkg_index_fp)
235
236 def testValidateOnly(self):
237 """Check that a validate only call does not execute any logic."""
238 self._write_original_package_index()
239 patch = self.PatchObject(binpkg.PackageIndex, "ReadFilePath")
240 request = binhost_pb2.UpdatePackageIndexRequest(
241 package_index_file=common_pb2.Path(
242 path=self._pkg_index_fp,
243 location=common_pb2.Path.Location.OUTSIDE,
244 ),
245 set_upload_location=True,
246 )
247 response = binhost_pb2.UpdatePackageIndexResponse()
248 binhost.UpdatePackageIndex(request, response, self.validate_only_config)
249 patch.assert_not_called()
250
251 def testMustProvideSomeCommand(self):
252 """Test that an error is raised if no update types are specified."""
253 self._write_original_package_index()
254 request = binhost_pb2.UpdatePackageIndexRequest(
255 package_index_file=common_pb2.Path(
256 path=self._pkg_index_fp,
257 location=common_pb2.Path.OUTSIDE,
258 ),
259 uri="gs://chromeos-prebuilt/board/amd64-host/packages",
260 )
261 response = binhost_pb2.UpdatePackageIndexResponse()
262 with self.assertRaises(cros_build_lib.DieSystemExit):
263 binhost.UpdatePackageIndex(request, response, self.api_config)
264
265 def testSetUploadLocation(self):
266 """Test setting the package upload location in the index file.
267
268 This test includes correctly parsing the input uri.
269 """
270 # Arrange
271 self._write_original_package_index()
272
273 # Act
274 request = binhost_pb2.UpdatePackageIndexRequest(
275 package_index_file=common_pb2.Path(
276 path=self._pkg_index_fp,
277 location=common_pb2.Path.Location.OUTSIDE,
278 ),
279 set_upload_location=True,
280 uri="gs://chromeos-prebuilt/board/amd64-host/packages/",
281 )
282 response = binhost_pb2.UpdatePackageIndexResponse()
283 binhost.UpdatePackageIndex(request, response, self.api_config)
284
285 # Assert
286 new_pkg_index = binpkg.PackageIndex()
287 new_pkg_index.ReadFilePath(self._pkg_index_fp)
288 self.assertEqual(new_pkg_index.header["URI"], "gs://chromeos-prebuilt")
289 self.assertDictEqual(
290 new_pkg_index.packages[0],
291 {
292 "CPV": "cat/pkg",
293 "KEY": "also_value",
294 "PATH": "board/amd64-host/packages/cat/pkg.tbz2",
295 },
296 )
297 self.assertDictEqual(
298 new_pkg_index.packages[1],
299 {
300 "CPV": "foo/bar",
301 "KEY": "value",
302 "PATH": "board/amd64-host/packages/foo/bar.tbz2",
303 },
304 )
305
306
Alex Klein231d2da2019-07-22 16:44:45 -0600307class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600308 """Unittests for SetBinhost."""
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600309
Alex Klein1699fab2022-09-08 08:46:06 -0600310 def setUp(self):
311 self.response = binhost_pb2.SetBinhostResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600312
Alex Klein1699fab2022-09-08 08:46:06 -0600313 def testValidateOnly(self):
314 """Check that a validate only call does not execute any logic."""
315 patch = self.PatchObject(binhost_service, "SetBinhost")
Alex Klein231d2da2019-07-22 16:44:45 -0600316
Alex Klein1699fab2022-09-08 08:46:06 -0600317 request = binhost_pb2.SetBinhostRequest()
318 request.build_target.name = "target"
319 request.key = binhost_pb2.POSTSUBMIT_BINHOST
320 request.uri = "gs://chromeos-prebuilt/target"
321 binhost.SetBinhost(request, self.response, self.validate_only_config)
322 patch.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600323
Alex Klein1699fab2022-09-08 08:46:06 -0600324 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700325 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600326 patch = self.PatchObject(binhost_service, "SetBinhost")
Michael Mortensen42251f92019-11-14 11:01:43 -0700327
Alex Klein1699fab2022-09-08 08:46:06 -0600328 request = binhost_pb2.SetBinhostRequest()
329 request.build_target.name = "target"
330 request.key = binhost_pb2.POSTSUBMIT_BINHOST
331 request.uri = "gs://chromeos-prebuilt/target"
Arif Kasim6242cdd2022-10-19 17:51:00 +0000332 request.max_uris = 4
Alex Klein1699fab2022-09-08 08:46:06 -0600333 binhost.SetBinhost(request, self.response, self.mock_call_config)
334 patch.assert_not_called()
335 self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf")
Michael Mortensen42251f92019-11-14 11:01:43 -0700336
Alex Klein1699fab2022-09-08 08:46:06 -0600337 def testSetBinhost(self):
338 """SetBinhost calls service with correct args."""
339 set_binhost = self.PatchObject(
340 binhost_service, "SetBinhost", return_value="/path/to/BINHOST.conf"
341 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600342
Alex Klein1699fab2022-09-08 08:46:06 -0600343 input_proto = binhost_pb2.SetBinhostRequest()
344 input_proto.build_target.name = "target"
345 input_proto.private = True
346 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
347 input_proto.uri = "gs://chromeos-prebuilt/target"
Arif Kasim6242cdd2022-10-19 17:51:00 +0000348 input_proto.max_uris = 4
Alex Klein1699fab2022-09-08 08:46:06 -0600349 binhost.SetBinhost(input_proto, self.response, self.api_config)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600350
Alex Klein1699fab2022-09-08 08:46:06 -0600351 self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf")
352 set_binhost.assert_called_once_with(
353 "target",
354 "POSTSUBMIT_BINHOST",
355 "gs://chromeos-prebuilt/target",
356 private=True,
Arif Kasim6242cdd2022-10-19 17:51:00 +0000357 max_uris=4,
Alex Klein1699fab2022-09-08 08:46:06 -0600358 )
LaMont Jonesc64ae212019-04-15 15:41:28 -0600359
Alex Klein231d2da2019-07-22 16:44:45 -0600360
Arif Kasima0467262022-11-11 17:08:14 +0000361class GetBinhostConfPathTest(
362 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
363):
364 """Unittests for GetBinhostConfPath."""
365
366 def setUp(self):
367 self.response = binhost_pb2.GetBinhostConfPathResponse()
368
369 def testValidateOnly(self):
370 """Check that a validate only call does not execute any logic."""
371 patch = self.PatchObject(binhost_service, "GetBinhostConfPath")
372
373 request = binhost_pb2.GetBinhostConfPathRequest()
374 request.build_target.name = "target"
375 request.key = binhost_pb2.POSTSUBMIT_BINHOST
376 binhost.GetBinhostConfPath(
377 request, self.response, self.validate_only_config
378 )
379 patch.assert_not_called()
380
381 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700382 """Test a mock call does not execute logic, returns mocked value."""
Arif Kasima0467262022-11-11 17:08:14 +0000383 patch = self.PatchObject(binhost_service, "GetBinhostConfPath")
384
385 request = binhost_pb2.GetBinhostConfPathRequest()
386 request.build_target.name = "target"
387 request.key = binhost_pb2.POSTSUBMIT_BINHOST
388 binhost.GetBinhostConfPath(
389 request, self.response, self.mock_call_config
390 )
391 patch.assert_not_called()
392 self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf")
393
394 def testGetBinhostConfPath(self):
395 """GetBinhostConfPath calls service with correct args."""
396 get_binhost_conf_path = self.PatchObject(
397 binhost_service,
398 "GetBinhostConfPath",
399 return_value="/path/to/BINHOST.conf",
400 )
401 input_proto = binhost_pb2.GetBinhostConfPathRequest()
402 input_proto.build_target.name = "target"
403 input_proto.private = True
404 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
405 binhost.GetBinhostConfPath(input_proto, self.response, self.api_config)
406
407 self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf")
408 get_binhost_conf_path.assert_called_once_with(
409 "target",
410 "POSTSUBMIT_BINHOST",
411 True,
412 )
413
414
Alex Klein1699fab2022-09-08 08:46:06 -0600415class RegenBuildCacheTest(
416 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
417):
418 """Unittests for RegenBuildCache."""
LaMont Jonesc64ae212019-04-15 15:41:28 -0600419
Alex Klein1699fab2022-09-08 08:46:06 -0600420 def setUp(self):
421 self.response = binhost_pb2.RegenBuildCacheResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600422
Alex Klein1699fab2022-09-08 08:46:06 -0600423 def testValidateOnly(self):
424 """Check that a validate only call does not execute any logic."""
425 patch = self.PatchObject(binhost_service, "RegenBuildCache")
Alex Klein231d2da2019-07-22 16:44:45 -0600426
Alex Klein1699fab2022-09-08 08:46:06 -0600427 request = binhost_pb2.RegenBuildCacheRequest()
428 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
429 binhost.RegenBuildCache(
430 request, self.response, self.validate_only_config
431 )
432 patch.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600433
Alex Klein1699fab2022-09-08 08:46:06 -0600434 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700435 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600436 patch = self.PatchObject(binhost_service, "RegenBuildCache")
Michael Mortensen42251f92019-11-14 11:01:43 -0700437
Alex Klein1699fab2022-09-08 08:46:06 -0600438 request = binhost_pb2.RegenBuildCacheRequest()
439 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
440 binhost.RegenBuildCache(request, self.response, self.mock_call_config)
441 patch.assert_not_called()
442 self.assertEqual(len(self.response.modified_overlays), 1)
443 self.assertEqual(
444 self.response.modified_overlays[0].path, "/path/to/BuildCache"
445 )
446
447 def testRegenBuildCache(self):
448 """RegenBuildCache calls service with the correct args."""
449 regen_cache = self.PatchObject(binhost_service, "RegenBuildCache")
450
451 input_proto = binhost_pb2.RegenBuildCacheRequest()
452 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
453
454 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
455 regen_cache.assert_called_once_with(mock.ANY, "both")
456
457 def testRequiresOverlayType(self):
458 """RegenBuildCache dies if overlay_type not specified."""
459 regen_cache = self.PatchObject(binhost_service, "RegenBuildCache")
460
461 input_proto = binhost_pb2.RegenBuildCacheRequest()
462 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED
463
464 with self.assertRaises(cros_build_lib.DieSystemExit):
465 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
466 regen_cache.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700467
468
Alex Klein1699fab2022-09-08 08:46:06 -0600469class PrepareDevInstallerBinhostUploadsTest(
470 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
471):
472 """Tests for the UploadDevInstallerPrebuilts stage."""
LaMont Jonesc64ae212019-04-15 15:41:28 -0600473
Alex Klein1699fab2022-09-08 08:46:06 -0600474 def setUp(self):
475 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
476 # target packages dir
477 self.chroot_path = os.path.join(self.tempdir, "chroot")
478 self.sysroot_path = "/build/target"
479 self.chroot_path = os.path.join(self.tempdir, "chroot")
480 full_sysroot_path = os.path.join(
481 self.chroot_path, self.sysroot_path.lstrip(os.sep)
482 )
483 self.full_sysroot_package_path = os.path.join(
484 full_sysroot_path, "packages"
485 )
486 osutils.SafeMakedirs(self.full_sysroot_package_path)
487 self.uploads_dir = os.path.join(self.tempdir, "uploads_dir")
488 osutils.SafeMakedirs(self.uploads_dir)
489 # Create packages/Packages file
490 packages_file = os.path.join(self.full_sysroot_package_path, "Packages")
491 packages_content = """\
Michael Mortensenfc823882019-08-27 14:38:07 -0600492USE: test
493
494CPV: app-arch/brotli-1.0.6
495
496CPV: app-arch/zip-3.0-r3
497
498CPV: chromeos-base/shill-0.0.1-r1
499
500CPV: chromeos-base/test-0.0.1-r1
501
502CPV: virtual/chromium-os-printing-1-r4
503
504CPV: virtual/python-enum34-1
505
506"""
Alex Klein1699fab2022-09-08 08:46:06 -0600507 osutils.WriteFile(packages_file, packages_content)
Michael Mortensenfc823882019-08-27 14:38:07 -0600508
Alex Klein1699fab2022-09-08 08:46:06 -0600509 # Create package.installable file
510 self.dev_install_packages = [
511 "app-arch/zip-3.0-r3",
512 "virtual/chromium-os-printing-1-r4",
513 "virtual/python-enum34-1",
514 ]
515 package_installable_dir = os.path.join(
516 full_sysroot_path, "build/dev-install"
517 )
518 osutils.SafeMakedirs(package_installable_dir)
519 package_installable_filename = os.path.join(
520 package_installable_dir, "package.installable"
521 )
Michael Mortensenfc823882019-08-27 14:38:07 -0600522
Alex Klein1699fab2022-09-08 08:46:06 -0600523 # Create path to the dev_install_packages
524 packages_dir = os.path.join(full_sysroot_path, "packages")
525 osutils.SafeMakedirs(packages_dir)
526 for package in self.dev_install_packages:
Alex Kleinab87ceb2023-01-24 12:00:51 -0700527 # Since a package has a category, such as app-arch/zip-3.0-r3, we
528 # need to create the packages_dir / category dir as needed.
Alex Klein1699fab2022-09-08 08:46:06 -0600529 category = package.split(os.sep)[0]
530 osutils.SafeMakedirs(os.path.join(packages_dir, category))
531 package_tbz2_file = os.path.join(packages_dir, package) + ".tbz2"
532 osutils.Touch(package_tbz2_file)
533 with open(
Mike Frysingerd97829b2023-02-24 16:09:20 -0500534 package_installable_filename, "w", encoding="utf-8"
Alex Klein1699fab2022-09-08 08:46:06 -0600535 ) as package_installable_file:
536 for package in self.dev_install_packages:
537 package_installable_file.write(package + "\n")
538 self.response = binhost_pb2.PrepareDevInstallBinhostUploadsResponse()
Michael Mortensenfc823882019-08-27 14:38:07 -0600539
Alex Klein1699fab2022-09-08 08:46:06 -0600540 def testValidateOnly(self):
541 """Check that a validate only call does not execute any logic."""
542 patch = self.PatchObject(
543 binhost_service, "ReadDevInstallFilesToCreatePackageIndex"
544 )
Michael Mortensenfc823882019-08-27 14:38:07 -0600545
Alex Klein1699fab2022-09-08 08:46:06 -0600546 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
547 input_proto.uri = "gs://chromeos-prebuilt/target"
548 input_proto.chroot.path = self.chroot_path
549 input_proto.sysroot.path = self.sysroot_path
550 input_proto.uploads_dir = self.uploads_dir
551 binhost.PrepareDevInstallBinhostUploads(
552 input_proto, self.response, self.validate_only_config
553 )
554 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700555
Alex Klein1699fab2022-09-08 08:46:06 -0600556 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700557 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600558 patch = self.PatchObject(
559 binhost_service, "ReadDevInstallFilesToCreatePackageIndex"
560 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700561
Alex Klein1699fab2022-09-08 08:46:06 -0600562 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
563 input_proto.uri = "gs://chromeos-prebuilt/target"
564 input_proto.chroot.path = self.chroot_path
565 input_proto.sysroot.path = self.sysroot_path
566 input_proto.uploads_dir = self.uploads_dir
567 binhost.PrepareDevInstallBinhostUploads(
568 input_proto, self.response, self.mock_call_config
569 )
570 self.assertEqual(len(self.response.upload_targets), 3)
571 self.assertEqual(self.response.upload_targets[2].path, "Packages")
572 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700573
Alex Klein1699fab2022-09-08 08:46:06 -0600574 def testDevInstallerUpload(self):
575 """Test uploads of dev installer prebuilts."""
576 # self.RunStage()
577 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
578 input_proto.uri = "gs://chromeos-prebuilt/target"
579 input_proto.chroot.path = self.chroot_path
580 input_proto.sysroot.path = self.sysroot_path
581 input_proto.uploads_dir = self.uploads_dir
582 # Call method under test
583 binhost.PrepareDevInstallBinhostUploads(
584 input_proto, self.response, self.api_config
585 )
586 # Verify results
587 expected_upload_targets = [
588 "app-arch/zip-3.0-r3.tbz2",
589 "virtual/chromium-os-printing-1-r4.tbz2",
590 "virtual/python-enum34-1.tbz2",
591 "Packages",
592 ]
593 self.assertCountEqual(
594 [ut.path for ut in self.response.upload_targets],
595 expected_upload_targets,
596 )
597 # All of the upload_targets should also be in the uploads_directory
598 for target in self.response.upload_targets:
599 self.assertExists(
600 os.path.join(input_proto.uploads_dir, target.path)
601 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700602
Alex Klein1699fab2022-09-08 08:46:06 -0600603 def testPrepareBinhostUploadsNonGsUri(self):
604 """PrepareBinhostUploads dies when URI does not point to GS."""
605 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
606 input_proto.chroot.path = self.chroot_path
607 input_proto.sysroot.path = self.sysroot_path
608 input_proto.uploads_dir = self.uploads_dir
609 input_proto.uri = "https://foo.bar"
610 with self.assertRaises(ValueError):
611 binhost.PrepareDevInstallBinhostUploads(
612 input_proto, self.response, self.api_config
613 )