Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 2 | # 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 Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 7 | import os |
Brian Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 8 | from pathlib import Path |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 9 | from unittest import mock |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 10 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 11 | from chromite.api import api_config |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 12 | from chromite.api.controller import binhost |
| 13 | from chromite.api.gen.chromite.api import binhost_pb2 |
Greg Edelston | 724c13d | 2023-04-07 16:19:24 -0600 | [diff] [blame] | 14 | from chromite.api.gen.chromiumos import common_pb2 |
| 15 | from chromite.lib import binpkg |
Brian Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 16 | from chromite.lib import chroot_lib |
Cindy Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 17 | from chromite.lib import constants |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 18 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 19 | from chromite.lib import cros_test_lib |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 20 | from chromite.lib import osutils |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 21 | from chromite.service import binhost as binhost_service |
| 22 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 23 | |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 24 | class GetBinhostsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | """Unittests for GetBinhosts.""" |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 26 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 27 | def setUp(self): |
| 28 | self.response = binhost_pb2.BinhostGetResponse() |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 29 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 30 | def testValidateOnly(self): |
| 31 | """Check that a validate only call does not execute any logic.""" |
| 32 | patch = self.PatchObject(binhost_service, "GetBinhosts") |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 33 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 34 | 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 Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 38 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 40 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | patch = self.PatchObject(binhost_service, "GetBinhosts") |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 42 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | input_proto = binhost_pb2.BinhostGetRequest() |
| 44 | input_proto.build_target.name = "target" |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 45 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | binhost.GetBinhosts(input_proto, self.response, self.mock_call_config) |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 47 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | self.assertEqual(len(self.response.binhosts), 1) |
| 49 | self.assertEqual(self.response.binhosts[0].package_index, "Packages") |
| 50 | patch.assert_not_called() |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 51 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 52 | def testGetBinhosts(self): |
| 53 | """GetBinhosts calls service with correct args.""" |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 54 | # pylint: disable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | 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 Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 59 | # pylint: enable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | get_binhost = self.PatchObject( |
| 61 | binhost_service, "GetBinhosts", return_value=binhost_list |
| 62 | ) |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | input_proto = binhost_pb2.BinhostGetRequest() |
| 65 | input_proto.build_target.name = "target" |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 66 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | binhost.GetBinhosts(input_proto, self.response, self.api_config) |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 68 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | 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 Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 72 | |
| 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | class GetPrivatePrebuiltAclArgsTest( |
| 75 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 76 | ): |
| 77 | """Unittests for GetPrivatePrebuiltAclArgs.""" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 78 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | def setUp(self): |
| 80 | self.response = binhost_pb2.AclArgsResponse() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 81 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 82 | def testValidateOnly(self): |
| 83 | """Check that a validate only call does not execute any logic.""" |
| 84 | patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 85 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 86 | 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 92 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 94 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 96 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | input_proto = binhost_pb2.AclArgsRequest() |
| 98 | input_proto.build_target.name = "target" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 99 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | binhost.GetPrivatePrebuiltAclArgs( |
| 101 | input_proto, self.response, self.mock_call_config |
| 102 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 103 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 104 | 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 108 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 109 | 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 115 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | input_proto = binhost_pb2.AclArgsRequest() |
| 117 | input_proto.build_target.name = "target" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 118 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 119 | binhost.GetPrivatePrebuiltAclArgs( |
| 120 | input_proto, self.response, self.api_config |
| 121 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 122 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 123 | 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 127 | |
| 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | class PrepareBinhostUploadsTest( |
| 130 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 131 | ): |
| 132 | """Unittests for PrepareBinhostUploads.""" |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 133 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | 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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 150 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 151 | self.response = binhost_pb2.PrepareBinhostUploadsResponse() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 152 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 153 | def testValidateOnly(self): |
| 154 | """Check that a validate only call does not execute any logic.""" |
| 155 | patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 156 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 165 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 166 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 167 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 168 | patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 169 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 180 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 181 | 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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 194 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | 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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 204 | |
| 205 | |
Greg Edelston | 724c13d | 2023-04-07 16:19:24 -0600 | [diff] [blame] | 206 | class 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 310 | class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 311 | """Unittests for SetBinhost.""" |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 312 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 313 | def setUp(self): |
| 314 | self.response = binhost_pb2.SetBinhostResponse() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 315 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 316 | def testValidateOnly(self): |
| 317 | """Check that a validate only call does not execute any logic.""" |
| 318 | patch = self.PatchObject(binhost_service, "SetBinhost") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 319 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 320 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 326 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 327 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 328 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 329 | patch = self.PatchObject(binhost_service, "SetBinhost") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 330 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 331 | 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 Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 335 | request.max_uris = 4 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 336 | 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 339 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 340 | 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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 345 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 346 | 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 Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 351 | input_proto.max_uris = 4 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 352 | binhost.SetBinhost(input_proto, self.response, self.api_config) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 353 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 354 | 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 Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 360 | max_uris=4, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 361 | ) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 362 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 363 | |
Arif Kasim | a046726 | 2022-11-11 17:08:14 +0000 | [diff] [blame] | 364 | class 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 Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 385 | """Test a mock call does not execute logic, returns mocked value.""" |
Arif Kasim | a046726 | 2022-11-11 17:08:14 +0000 | [diff] [blame] | 386 | 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 Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 418 | class RegenBuildCacheTest( |
| 419 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 420 | ): |
| 421 | """Unittests for RegenBuildCache.""" |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 422 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 423 | def setUp(self): |
| 424 | self.response = binhost_pb2.RegenBuildCacheResponse() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 425 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 426 | def testValidateOnly(self): |
| 427 | """Check that a validate only call does not execute any logic.""" |
| 428 | patch = self.PatchObject(binhost_service, "RegenBuildCache") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 429 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 430 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 436 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 437 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 438 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 439 | patch = self.PatchObject(binhost_service, "RegenBuildCache") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 440 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 441 | 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 470 | |
| 471 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 472 | class PrepareDevInstallerBinhostUploadsTest( |
| 473 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 474 | ): |
| 475 | """Tests for the UploadDevInstallerPrebuilts stage.""" |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 476 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 477 | def setUp(self): |
| 478 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 479 | # target packages dir |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 480 | self.sysroot_path = "/build/target" |
Brian Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 481 | self.chroot = chroot_lib.Chroot( |
| 482 | path=self.tempdir / "chroot", |
| 483 | out_path=self.tempdir / "out", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 484 | ) |
Brian Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 485 | full_sysroot_path = self.chroot.full_path(self.sysroot_path) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 486 | 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 Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 495 | USE: test |
| 496 | |
| 497 | CPV: app-arch/brotli-1.0.6 |
| 498 | |
| 499 | CPV: app-arch/zip-3.0-r3 |
| 500 | |
| 501 | CPV: chromeos-base/shill-0.0.1-r1 |
| 502 | |
| 503 | CPV: chromeos-base/test-0.0.1-r1 |
| 504 | |
| 505 | CPV: virtual/chromium-os-printing-1-r4 |
| 506 | |
| 507 | CPV: virtual/python-enum34-1 |
| 508 | |
| 509 | """ |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 510 | osutils.WriteFile(packages_file, packages_content) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 511 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 512 | # 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 Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 525 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 526 | # 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 Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 530 | # 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 Apted | 593c074 | 2023-05-05 03:50:20 +0000 | [diff] [blame] | 532 | # TODO(b/236161656): Fix. |
| 533 | # pylint: disable-next=use-maxsplit-arg |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 534 | 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 Frysinger | d97829b | 2023-02-24 16:09:20 -0500 | [diff] [blame] | 539 | package_installable_filename, "w", encoding="utf-8" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 540 | ) 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 Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 544 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 545 | 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 Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 550 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 551 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 552 | input_proto.uri = "gs://chromeos-prebuilt/target" |
Brian Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 553 | input_proto.chroot.path = self.chroot.path |
| 554 | input_proto.chroot.out_path = str(self.chroot.out_path) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 555 | 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 561 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 562 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 563 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 564 | patch = self.PatchObject( |
| 565 | binhost_service, "ReadDevInstallFilesToCreatePackageIndex" |
| 566 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 567 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 568 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 569 | input_proto.uri = "gs://chromeos-prebuilt/target" |
Brian Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 570 | input_proto.chroot.path = self.chroot.path |
| 571 | input_proto.chroot.out_path = str(self.chroot.out_path) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 572 | 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 580 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 581 | 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 Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 586 | input_proto.chroot.path = self.chroot.path |
| 587 | input_proto.chroot.out_path = str(self.chroot.out_path) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 588 | 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 610 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 611 | def testPrepareBinhostUploadsNonGsUri(self): |
| 612 | """PrepareBinhostUploads dies when URI does not point to GS.""" |
| 613 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
Brian Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 614 | input_proto.chroot.path = self.chroot.path |
| 615 | input_proto.chroot.out_path = str(self.chroot.out_path) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 616 | 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 Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 623 | |
| 624 | |
| 625 | class 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 Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 636 | self.chroot = chroot_lib.Chroot( |
| 637 | path=self.tempdir / "chroot", |
| 638 | out_path=self.tempdir / "out", |
| 639 | ) |
Cindy Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 640 | 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 Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 644 | self.input_proto.chroot.path = str(self.chroot.path) |
| 645 | self.input_proto.chroot.out_path = str(self.chroot.out_path) |
Cindy Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 646 | 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 Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame^] | 650 | self.packages_path = Path( |
| 651 | self.chroot.full_path(self.sysroot_path, "packages") |
| 652 | ) |
Cindy Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 653 | 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 | ) |