blob: 6b5888053f8ac07f00ca6c45c18a558b571ced39 [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
LaMont Jonesc64ae212019-04-15 15:41:28 -060013from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060014from chromite.lib import cros_test_lib
Michael Mortensenfc823882019-08-27 14:38:07 -060015from chromite.lib import osutils
Evan Hernandezd437b4e2019-03-25 13:48:30 -060016from chromite.service import binhost as binhost_service
17
Alex Klein231d2da2019-07-22 16:44:45 -060018
Michael Mortensena0af77b2019-11-13 11:15:15 -070019class GetBinhostsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -060020 """Unittests for GetBinhosts."""
Michael Mortensena0af77b2019-11-13 11:15:15 -070021
Alex Klein1699fab2022-09-08 08:46:06 -060022 def setUp(self):
23 self.response = binhost_pb2.BinhostGetResponse()
Michael Mortensena0af77b2019-11-13 11:15:15 -070024
Alex Klein1699fab2022-09-08 08:46:06 -060025 def testValidateOnly(self):
26 """Check that a validate only call does not execute any logic."""
27 patch = self.PatchObject(binhost_service, "GetBinhosts")
Michael Mortensena0af77b2019-11-13 11:15:15 -070028
Alex Klein1699fab2022-09-08 08:46:06 -060029 request = binhost_pb2.BinhostGetRequest()
30 request.build_target.name = "target"
31 binhost.GetBinhosts(request, self.response, self.validate_only_config)
32 patch.assert_not_called()
Michael Mortensena0af77b2019-11-13 11:15:15 -070033
Alex Klein1699fab2022-09-08 08:46:06 -060034 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070035 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -060036 patch = self.PatchObject(binhost_service, "GetBinhosts")
Michael Mortensena0af77b2019-11-13 11:15:15 -070037
Alex Klein1699fab2022-09-08 08:46:06 -060038 input_proto = binhost_pb2.BinhostGetRequest()
39 input_proto.build_target.name = "target"
Michael Mortensena0af77b2019-11-13 11:15:15 -070040
Alex Klein1699fab2022-09-08 08:46:06 -060041 binhost.GetBinhosts(input_proto, self.response, self.mock_call_config)
Michael Mortensena0af77b2019-11-13 11:15:15 -070042
Alex Klein1699fab2022-09-08 08:46:06 -060043 self.assertEqual(len(self.response.binhosts), 1)
44 self.assertEqual(self.response.binhosts[0].package_index, "Packages")
45 patch.assert_not_called()
Michael Mortensena0af77b2019-11-13 11:15:15 -070046
Alex Klein1699fab2022-09-08 08:46:06 -060047 def testGetBinhosts(self):
48 """GetBinhosts calls service with correct args."""
Alex Kleinab87ceb2023-01-24 12:00:51 -070049 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -060050 binhost_list = [
51 "gs://cr-prebuilt/board/amd64-generic/paladin-R66-17.0.0-rc2/packages/",
52 "gs://cr-prebuilt/board/eve/paladin-R66-17.0.0-rc2/packages/",
53 ]
Alex Kleinab87ceb2023-01-24 12:00:51 -070054 # pylint: enable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -060055 get_binhost = self.PatchObject(
56 binhost_service, "GetBinhosts", return_value=binhost_list
57 )
Michael Mortensena0af77b2019-11-13 11:15:15 -070058
Alex Klein1699fab2022-09-08 08:46:06 -060059 input_proto = binhost_pb2.BinhostGetRequest()
60 input_proto.build_target.name = "target"
Michael Mortensena0af77b2019-11-13 11:15:15 -070061
Alex Klein1699fab2022-09-08 08:46:06 -060062 binhost.GetBinhosts(input_proto, self.response, self.api_config)
Michael Mortensena0af77b2019-11-13 11:15:15 -070063
Alex Klein1699fab2022-09-08 08:46:06 -060064 self.assertEqual(len(self.response.binhosts), 2)
65 self.assertEqual(self.response.binhosts[0].package_index, "Packages")
66 get_binhost.assert_called_once_with(mock.ANY)
Michael Mortensena0af77b2019-11-13 11:15:15 -070067
68
Alex Klein1699fab2022-09-08 08:46:06 -060069class GetPrivatePrebuiltAclArgsTest(
70 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
71):
72 """Unittests for GetPrivatePrebuiltAclArgs."""
Michael Mortensen42251f92019-11-14 11:01:43 -070073
Alex Klein1699fab2022-09-08 08:46:06 -060074 def setUp(self):
75 self.response = binhost_pb2.AclArgsResponse()
Michael Mortensen42251f92019-11-14 11:01:43 -070076
Alex Klein1699fab2022-09-08 08:46:06 -060077 def testValidateOnly(self):
78 """Check that a validate only call does not execute any logic."""
79 patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs")
Michael Mortensen42251f92019-11-14 11:01:43 -070080
Alex Klein1699fab2022-09-08 08:46:06 -060081 request = binhost_pb2.AclArgsRequest()
82 request.build_target.name = "target"
83 binhost.GetPrivatePrebuiltAclArgs(
84 request, self.response, self.validate_only_config
85 )
86 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -070087
Alex Klein1699fab2022-09-08 08:46:06 -060088 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -070089 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -060090 patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs")
Michael Mortensen42251f92019-11-14 11:01:43 -070091
Alex Klein1699fab2022-09-08 08:46:06 -060092 input_proto = binhost_pb2.AclArgsRequest()
93 input_proto.build_target.name = "target"
Michael Mortensen42251f92019-11-14 11:01:43 -070094
Alex Klein1699fab2022-09-08 08:46:06 -060095 binhost.GetPrivatePrebuiltAclArgs(
96 input_proto, self.response, self.mock_call_config
97 )
Michael Mortensen42251f92019-11-14 11:01:43 -070098
Alex Klein1699fab2022-09-08 08:46:06 -060099 self.assertEqual(len(self.response.args), 1)
100 self.assertEqual(self.response.args[0].arg, "-g")
101 self.assertEqual(self.response.args[0].value, "group1:READ")
102 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 def testGetPrivatePrebuiltAclArgs(self):
105 """GetPrivatePrebuildAclsArgs calls service with correct args."""
106 argvalue_list = [["-g", "group1:READ"]]
107 get_binhost = self.PatchObject(
108 binhost_service, "GetPrebuiltAclArgs", return_value=argvalue_list
109 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 input_proto = binhost_pb2.AclArgsRequest()
112 input_proto.build_target.name = "target"
Michael Mortensen42251f92019-11-14 11:01:43 -0700113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 binhost.GetPrivatePrebuiltAclArgs(
115 input_proto, self.response, self.api_config
116 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700117
Alex Klein1699fab2022-09-08 08:46:06 -0600118 self.assertEqual(len(self.response.args), 1)
119 self.assertEqual(self.response.args[0].arg, "-g")
120 self.assertEqual(self.response.args[0].value, "group1:READ")
121 get_binhost.assert_called_once_with(mock.ANY)
Michael Mortensen42251f92019-11-14 11:01:43 -0700122
123
Alex Klein1699fab2022-09-08 08:46:06 -0600124class PrepareBinhostUploadsTest(
125 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
126):
127 """Unittests for PrepareBinhostUploads."""
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600128
Alex Klein1699fab2022-09-08 08:46:06 -0600129 def setUp(self):
130 self.PatchObject(
131 binhost_service,
132 "GetPrebuiltsRoot",
133 return_value="/build/target/packages",
134 )
135 self.PatchObject(
136 binhost_service,
137 "GetPrebuiltsFiles",
138 return_value=["foo.tbz2", "bar.tbz2"],
139 )
140 self.PatchObject(
141 binhost_service,
142 "UpdatePackageIndex",
143 return_value="/build/target/packages/Packages",
144 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600145
Alex Klein1699fab2022-09-08 08:46:06 -0600146 self.response = binhost_pb2.PrepareBinhostUploadsResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 def testValidateOnly(self):
149 """Check that a validate only call does not execute any logic."""
150 patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot")
Alex Klein231d2da2019-07-22 16:44:45 -0600151
Alex Klein1699fab2022-09-08 08:46:06 -0600152 request = binhost_pb2.PrepareBinhostUploadsRequest()
153 request.build_target.name = "target"
154 request.uri = "gs://chromeos-prebuilt/target"
155 rc = binhost.PrepareBinhostUploads(
156 request, self.response, self.validate_only_config
157 )
158 patch.assert_not_called()
159 self.assertEqual(rc, 0)
Alex Klein231d2da2019-07-22 16:44:45 -0600160
Alex Klein1699fab2022-09-08 08:46:06 -0600161 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700162 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600163 patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot")
Michael Mortensen42251f92019-11-14 11:01:43 -0700164
Alex Klein1699fab2022-09-08 08:46:06 -0600165 request = binhost_pb2.PrepareBinhostUploadsRequest()
166 request.build_target.name = "target"
167 request.uri = "gs://chromeos-prebuilt/target"
168 rc = binhost.PrepareBinhostUploads(
169 request, self.response, self.mock_call_config
170 )
171 self.assertEqual(self.response.uploads_dir, "/upload/directory")
172 self.assertEqual(self.response.upload_targets[0].path, "upload_target")
173 patch.assert_not_called()
174 self.assertEqual(rc, 0)
Michael Mortensen42251f92019-11-14 11:01:43 -0700175
Alex Klein1699fab2022-09-08 08:46:06 -0600176 def testPrepareBinhostUploads(self):
177 """PrepareBinhostUploads returns Packages and tar files."""
178 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
179 input_proto.build_target.name = "target"
180 input_proto.uri = "gs://chromeos-prebuilt/target"
181 binhost.PrepareBinhostUploads(
182 input_proto, self.response, self.api_config
183 )
184 self.assertEqual(self.response.uploads_dir, "/build/target/packages")
185 self.assertCountEqual(
186 [ut.path for ut in self.response.upload_targets],
187 ["Packages", "foo.tbz2", "bar.tbz2"],
188 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600189
Alex Klein1699fab2022-09-08 08:46:06 -0600190 def testPrepareBinhostUploadsNonGsUri(self):
191 """PrepareBinhostUploads dies when URI does not point to GS."""
192 input_proto = binhost_pb2.PrepareBinhostUploadsRequest()
193 input_proto.build_target.name = "target"
194 input_proto.uri = "https://foo.bar"
195 with self.assertRaises(ValueError):
196 binhost.PrepareBinhostUploads(
197 input_proto, self.response, self.api_config
198 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600199
200
Alex Klein231d2da2019-07-22 16:44:45 -0600201class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
Alex Klein1699fab2022-09-08 08:46:06 -0600202 """Unittests for SetBinhost."""
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600203
Alex Klein1699fab2022-09-08 08:46:06 -0600204 def setUp(self):
205 self.response = binhost_pb2.SetBinhostResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600206
Alex Klein1699fab2022-09-08 08:46:06 -0600207 def testValidateOnly(self):
208 """Check that a validate only call does not execute any logic."""
209 patch = self.PatchObject(binhost_service, "SetBinhost")
Alex Klein231d2da2019-07-22 16:44:45 -0600210
Alex Klein1699fab2022-09-08 08:46:06 -0600211 request = binhost_pb2.SetBinhostRequest()
212 request.build_target.name = "target"
213 request.key = binhost_pb2.POSTSUBMIT_BINHOST
214 request.uri = "gs://chromeos-prebuilt/target"
215 binhost.SetBinhost(request, self.response, self.validate_only_config)
216 patch.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600217
Alex Klein1699fab2022-09-08 08:46:06 -0600218 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700219 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600220 patch = self.PatchObject(binhost_service, "SetBinhost")
Michael Mortensen42251f92019-11-14 11:01:43 -0700221
Alex Klein1699fab2022-09-08 08:46:06 -0600222 request = binhost_pb2.SetBinhostRequest()
223 request.build_target.name = "target"
224 request.key = binhost_pb2.POSTSUBMIT_BINHOST
225 request.uri = "gs://chromeos-prebuilt/target"
Arif Kasim6242cdd2022-10-19 17:51:00 +0000226 request.max_uris = 4
Alex Klein1699fab2022-09-08 08:46:06 -0600227 binhost.SetBinhost(request, self.response, self.mock_call_config)
228 patch.assert_not_called()
229 self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf")
Michael Mortensen42251f92019-11-14 11:01:43 -0700230
Alex Klein1699fab2022-09-08 08:46:06 -0600231 def testSetBinhost(self):
232 """SetBinhost calls service with correct args."""
233 set_binhost = self.PatchObject(
234 binhost_service, "SetBinhost", return_value="/path/to/BINHOST.conf"
235 )
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600236
Alex Klein1699fab2022-09-08 08:46:06 -0600237 input_proto = binhost_pb2.SetBinhostRequest()
238 input_proto.build_target.name = "target"
239 input_proto.private = True
240 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
241 input_proto.uri = "gs://chromeos-prebuilt/target"
Arif Kasim6242cdd2022-10-19 17:51:00 +0000242 input_proto.max_uris = 4
Alex Klein1699fab2022-09-08 08:46:06 -0600243 binhost.SetBinhost(input_proto, self.response, self.api_config)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600244
Alex Klein1699fab2022-09-08 08:46:06 -0600245 self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf")
246 set_binhost.assert_called_once_with(
247 "target",
248 "POSTSUBMIT_BINHOST",
249 "gs://chromeos-prebuilt/target",
250 private=True,
Arif Kasim6242cdd2022-10-19 17:51:00 +0000251 max_uris=4,
Alex Klein1699fab2022-09-08 08:46:06 -0600252 )
LaMont Jonesc64ae212019-04-15 15:41:28 -0600253
Alex Klein231d2da2019-07-22 16:44:45 -0600254
Arif Kasima0467262022-11-11 17:08:14 +0000255class GetBinhostConfPathTest(
256 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
257):
258 """Unittests for GetBinhostConfPath."""
259
260 def setUp(self):
261 self.response = binhost_pb2.GetBinhostConfPathResponse()
262
263 def testValidateOnly(self):
264 """Check that a validate only call does not execute any logic."""
265 patch = self.PatchObject(binhost_service, "GetBinhostConfPath")
266
267 request = binhost_pb2.GetBinhostConfPathRequest()
268 request.build_target.name = "target"
269 request.key = binhost_pb2.POSTSUBMIT_BINHOST
270 binhost.GetBinhostConfPath(
271 request, self.response, self.validate_only_config
272 )
273 patch.assert_not_called()
274
275 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700276 """Test a mock call does not execute logic, returns mocked value."""
Arif Kasima0467262022-11-11 17:08:14 +0000277 patch = self.PatchObject(binhost_service, "GetBinhostConfPath")
278
279 request = binhost_pb2.GetBinhostConfPathRequest()
280 request.build_target.name = "target"
281 request.key = binhost_pb2.POSTSUBMIT_BINHOST
282 binhost.GetBinhostConfPath(
283 request, self.response, self.mock_call_config
284 )
285 patch.assert_not_called()
286 self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf")
287
288 def testGetBinhostConfPath(self):
289 """GetBinhostConfPath calls service with correct args."""
290 get_binhost_conf_path = self.PatchObject(
291 binhost_service,
292 "GetBinhostConfPath",
293 return_value="/path/to/BINHOST.conf",
294 )
295 input_proto = binhost_pb2.GetBinhostConfPathRequest()
296 input_proto.build_target.name = "target"
297 input_proto.private = True
298 input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST
299 binhost.GetBinhostConfPath(input_proto, self.response, self.api_config)
300
301 self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf")
302 get_binhost_conf_path.assert_called_once_with(
303 "target",
304 "POSTSUBMIT_BINHOST",
305 True,
306 )
307
308
Alex Klein1699fab2022-09-08 08:46:06 -0600309class RegenBuildCacheTest(
310 cros_test_lib.MockTestCase, api_config.ApiConfigMixin
311):
312 """Unittests for RegenBuildCache."""
LaMont Jonesc64ae212019-04-15 15:41:28 -0600313
Alex Klein1699fab2022-09-08 08:46:06 -0600314 def setUp(self):
315 self.response = binhost_pb2.RegenBuildCacheResponse()
Alex Klein231d2da2019-07-22 16:44:45 -0600316
Alex Klein1699fab2022-09-08 08:46:06 -0600317 def testValidateOnly(self):
318 """Check that a validate only call does not execute any logic."""
319 patch = self.PatchObject(binhost_service, "RegenBuildCache")
Alex Klein231d2da2019-07-22 16:44:45 -0600320
Alex Klein1699fab2022-09-08 08:46:06 -0600321 request = binhost_pb2.RegenBuildCacheRequest()
322 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
323 binhost.RegenBuildCache(
324 request, self.response, self.validate_only_config
325 )
326 patch.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600327
Alex Klein1699fab2022-09-08 08:46:06 -0600328 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700329 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600330 patch = self.PatchObject(binhost_service, "RegenBuildCache")
Michael Mortensen42251f92019-11-14 11:01:43 -0700331
Alex Klein1699fab2022-09-08 08:46:06 -0600332 request = binhost_pb2.RegenBuildCacheRequest()
333 request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
334 binhost.RegenBuildCache(request, self.response, self.mock_call_config)
335 patch.assert_not_called()
336 self.assertEqual(len(self.response.modified_overlays), 1)
337 self.assertEqual(
338 self.response.modified_overlays[0].path, "/path/to/BuildCache"
339 )
340
341 def testRegenBuildCache(self):
342 """RegenBuildCache calls service with the correct args."""
343 regen_cache = self.PatchObject(binhost_service, "RegenBuildCache")
344
345 input_proto = binhost_pb2.RegenBuildCacheRequest()
346 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
347
348 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
349 regen_cache.assert_called_once_with(mock.ANY, "both")
350
351 def testRequiresOverlayType(self):
352 """RegenBuildCache dies if overlay_type not specified."""
353 regen_cache = self.PatchObject(binhost_service, "RegenBuildCache")
354
355 input_proto = binhost_pb2.RegenBuildCacheRequest()
356 input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED
357
358 with self.assertRaises(cros_build_lib.DieSystemExit):
359 binhost.RegenBuildCache(input_proto, self.response, self.api_config)
360 regen_cache.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700361
362
Alex Klein1699fab2022-09-08 08:46:06 -0600363class PrepareDevInstallerBinhostUploadsTest(
364 cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin
365):
366 """Tests for the UploadDevInstallerPrebuilts stage."""
LaMont Jonesc64ae212019-04-15 15:41:28 -0600367
Alex Klein1699fab2022-09-08 08:46:06 -0600368 def setUp(self):
369 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
370 # target packages dir
371 self.chroot_path = os.path.join(self.tempdir, "chroot")
372 self.sysroot_path = "/build/target"
373 self.chroot_path = os.path.join(self.tempdir, "chroot")
374 full_sysroot_path = os.path.join(
375 self.chroot_path, self.sysroot_path.lstrip(os.sep)
376 )
377 self.full_sysroot_package_path = os.path.join(
378 full_sysroot_path, "packages"
379 )
380 osutils.SafeMakedirs(self.full_sysroot_package_path)
381 self.uploads_dir = os.path.join(self.tempdir, "uploads_dir")
382 osutils.SafeMakedirs(self.uploads_dir)
383 # Create packages/Packages file
384 packages_file = os.path.join(self.full_sysroot_package_path, "Packages")
385 packages_content = """\
Michael Mortensenfc823882019-08-27 14:38:07 -0600386USE: test
387
388CPV: app-arch/brotli-1.0.6
389
390CPV: app-arch/zip-3.0-r3
391
392CPV: chromeos-base/shill-0.0.1-r1
393
394CPV: chromeos-base/test-0.0.1-r1
395
396CPV: virtual/chromium-os-printing-1-r4
397
398CPV: virtual/python-enum34-1
399
400"""
Alex Klein1699fab2022-09-08 08:46:06 -0600401 osutils.WriteFile(packages_file, packages_content)
Michael Mortensenfc823882019-08-27 14:38:07 -0600402
Alex Klein1699fab2022-09-08 08:46:06 -0600403 # Create package.installable file
404 self.dev_install_packages = [
405 "app-arch/zip-3.0-r3",
406 "virtual/chromium-os-printing-1-r4",
407 "virtual/python-enum34-1",
408 ]
409 package_installable_dir = os.path.join(
410 full_sysroot_path, "build/dev-install"
411 )
412 osutils.SafeMakedirs(package_installable_dir)
413 package_installable_filename = os.path.join(
414 package_installable_dir, "package.installable"
415 )
Michael Mortensenfc823882019-08-27 14:38:07 -0600416
Alex Klein1699fab2022-09-08 08:46:06 -0600417 # Create path to the dev_install_packages
418 packages_dir = os.path.join(full_sysroot_path, "packages")
419 osutils.SafeMakedirs(packages_dir)
420 for package in self.dev_install_packages:
Alex Kleinab87ceb2023-01-24 12:00:51 -0700421 # Since a package has a category, such as app-arch/zip-3.0-r3, we
422 # need to create the packages_dir / category dir as needed.
Alex Klein1699fab2022-09-08 08:46:06 -0600423 category = package.split(os.sep)[0]
424 osutils.SafeMakedirs(os.path.join(packages_dir, category))
425 package_tbz2_file = os.path.join(packages_dir, package) + ".tbz2"
426 osutils.Touch(package_tbz2_file)
427 with open(
428 package_installable_filename, "w"
429 ) as package_installable_file:
430 for package in self.dev_install_packages:
431 package_installable_file.write(package + "\n")
432 self.response = binhost_pb2.PrepareDevInstallBinhostUploadsResponse()
Michael Mortensenfc823882019-08-27 14:38:07 -0600433
Alex Klein1699fab2022-09-08 08:46:06 -0600434 def testValidateOnly(self):
435 """Check that a validate only call does not execute any logic."""
436 patch = self.PatchObject(
437 binhost_service, "ReadDevInstallFilesToCreatePackageIndex"
438 )
Michael Mortensenfc823882019-08-27 14:38:07 -0600439
Alex Klein1699fab2022-09-08 08:46:06 -0600440 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
441 input_proto.uri = "gs://chromeos-prebuilt/target"
442 input_proto.chroot.path = self.chroot_path
443 input_proto.sysroot.path = self.sysroot_path
444 input_proto.uploads_dir = self.uploads_dir
445 binhost.PrepareDevInstallBinhostUploads(
446 input_proto, self.response, self.validate_only_config
447 )
448 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700449
Alex Klein1699fab2022-09-08 08:46:06 -0600450 def testMockCall(self):
Alex Kleinab87ceb2023-01-24 12:00:51 -0700451 """Test a mock call does not execute logic, returns mocked value."""
Alex Klein1699fab2022-09-08 08:46:06 -0600452 patch = self.PatchObject(
453 binhost_service, "ReadDevInstallFilesToCreatePackageIndex"
454 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700455
Alex Klein1699fab2022-09-08 08:46:06 -0600456 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
457 input_proto.uri = "gs://chromeos-prebuilt/target"
458 input_proto.chroot.path = self.chroot_path
459 input_proto.sysroot.path = self.sysroot_path
460 input_proto.uploads_dir = self.uploads_dir
461 binhost.PrepareDevInstallBinhostUploads(
462 input_proto, self.response, self.mock_call_config
463 )
464 self.assertEqual(len(self.response.upload_targets), 3)
465 self.assertEqual(self.response.upload_targets[2].path, "Packages")
466 patch.assert_not_called()
Michael Mortensen42251f92019-11-14 11:01:43 -0700467
Alex Klein1699fab2022-09-08 08:46:06 -0600468 def testDevInstallerUpload(self):
469 """Test uploads of dev installer prebuilts."""
470 # self.RunStage()
471 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
472 input_proto.uri = "gs://chromeos-prebuilt/target"
473 input_proto.chroot.path = self.chroot_path
474 input_proto.sysroot.path = self.sysroot_path
475 input_proto.uploads_dir = self.uploads_dir
476 # Call method under test
477 binhost.PrepareDevInstallBinhostUploads(
478 input_proto, self.response, self.api_config
479 )
480 # Verify results
481 expected_upload_targets = [
482 "app-arch/zip-3.0-r3.tbz2",
483 "virtual/chromium-os-printing-1-r4.tbz2",
484 "virtual/python-enum34-1.tbz2",
485 "Packages",
486 ]
487 self.assertCountEqual(
488 [ut.path for ut in self.response.upload_targets],
489 expected_upload_targets,
490 )
491 # All of the upload_targets should also be in the uploads_directory
492 for target in self.response.upload_targets:
493 self.assertExists(
494 os.path.join(input_proto.uploads_dir, target.path)
495 )
Michael Mortensen42251f92019-11-14 11:01:43 -0700496
Alex Klein1699fab2022-09-08 08:46:06 -0600497 def testPrepareBinhostUploadsNonGsUri(self):
498 """PrepareBinhostUploads dies when URI does not point to GS."""
499 input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest()
500 input_proto.chroot.path = self.chroot_path
501 input_proto.sysroot.path = self.sysroot_path
502 input_proto.uploads_dir = self.uploads_dir
503 input_proto.uri = "https://foo.bar"
504 with self.assertRaises(ValueError):
505 binhost.PrepareDevInstallBinhostUploads(
506 input_proto, self.response, self.api_config
507 )