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 = [ |
Mike Frysinger | d668a22 | 2023-09-11 23:51:55 -0400 | [diff] [blame^] | 56 | f"{constants.TRASH_BUCKET}/board/amd64-generic/paladin-R66-17.0.0-rc2/packages/", |
| 57 | f"{constants.TRASH_BUCKET}/board/eve/paladin-R66-17.0.0-rc2/packages/", |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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 | |
Cindy Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 472 | class PrepareChromeBinhostUploadsTest( |
| 473 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 474 | ): |
| 475 | """Tests for BinhostService/PrepareChromeBinhostUploads.""" |
| 476 | |
| 477 | def setUp(self): |
| 478 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 479 | self.create_chrome_package_index_mock = self.PatchObject( |
| 480 | binhost_service, "CreateChromePackageIndex" |
| 481 | ) |
| 482 | |
Brian Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame] | 483 | self.chroot = chroot_lib.Chroot( |
| 484 | path=self.tempdir / "chroot", |
| 485 | out_path=self.tempdir / "out", |
| 486 | ) |
Cindy Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 487 | self.sysroot_path = "build/target" |
| 488 | self.uploads_dir = self.tempdir / "uploads_dir" |
| 489 | self.input_proto = binhost_pb2.PrepareChromeBinhostUploadsRequest() |
| 490 | self.input_proto.uri = "gs://chromeos-prebuilt/target" |
Brian Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame] | 491 | self.input_proto.chroot.path = str(self.chroot.path) |
| 492 | self.input_proto.chroot.out_path = str(self.chroot.out_path) |
Cindy Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 493 | self.input_proto.sysroot.path = self.sysroot_path |
| 494 | self.input_proto.uploads_dir = str(self.uploads_dir) |
| 495 | self.response = binhost_pb2.PrepareChromeBinhostUploadsResponse() |
| 496 | |
Brian Norris | a9cc6b3 | 2023-05-10 13:43:45 -0700 | [diff] [blame] | 497 | self.packages_path = Path( |
| 498 | self.chroot.full_path(self.sysroot_path, "packages") |
| 499 | ) |
Cindy Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 500 | self.chrome_packages_path = self.packages_path / constants.CHROME_CN |
| 501 | osutils.Touch( |
| 502 | self.chrome_packages_path / "chromeos-chrome-100-r1.tbz2", |
| 503 | makedirs=True, |
| 504 | ) |
| 505 | osutils.Touch( |
| 506 | self.chrome_packages_path / "chrome-icu-100-r1.tbz2", |
| 507 | makedirs=True, |
| 508 | ) |
| 509 | osutils.Touch( |
| 510 | self.chrome_packages_path / "chromeos-lacros-100-r1.tbz2", |
| 511 | makedirs=True, |
| 512 | ) |
| 513 | |
| 514 | def testValidateOnly(self): |
| 515 | """Check that a validate only call does not execute any logic.""" |
| 516 | binhost.PrepareChromeBinhostUploads( |
| 517 | self.input_proto, self.response, self.validate_only_config |
| 518 | ) |
| 519 | |
| 520 | self.create_chrome_package_index_mock.assert_not_called() |
| 521 | |
| 522 | def testMockCall(self): |
| 523 | """Test a mock call does not execute logic, returns mocked value.""" |
| 524 | binhost.PrepareChromeBinhostUploads( |
| 525 | self.input_proto, self.response, self.mock_call_config |
| 526 | ) |
| 527 | |
| 528 | self.assertEqual(len(self.response.upload_targets), 4) |
| 529 | self.assertEqual(self.response.upload_targets[3].path, "Packages") |
| 530 | self.create_chrome_package_index_mock.assert_not_called() |
| 531 | |
| 532 | def testChromeUpload(self): |
| 533 | """Test uploads of Chrome prebuilts.""" |
| 534 | expected_upload_targets = [ |
| 535 | "chromeos-base/chromeos-chrome-100-r1.tbz2", |
| 536 | "chromeos-base/chrome-icu-100-r1.tbz2", |
| 537 | "chromeos-base/chromeos-lacros-100-r1.tbz2", |
| 538 | ] |
| 539 | self.create_chrome_package_index_mock.return_value = ( |
| 540 | expected_upload_targets |
| 541 | ) |
| 542 | |
| 543 | binhost.PrepareChromeBinhostUploads( |
| 544 | self.input_proto, self.response, self.api_config |
| 545 | ) |
| 546 | |
| 547 | self.assertCountEqual( |
| 548 | [target.path for target in self.response.upload_targets], |
| 549 | expected_upload_targets + ["Packages"], |
| 550 | ) |
| 551 | |
| 552 | def testPrepareBinhostUploadsNonGsUri(self): |
| 553 | """PrepareBinhostUploads dies when URI does not point to GS.""" |
| 554 | self.input_proto.uri = "https://foo.bar" |
| 555 | |
| 556 | with self.assertRaises(ValueError): |
| 557 | binhost.PrepareChromeBinhostUploads( |
| 558 | self.input_proto, self.response, self.api_config |
| 559 | ) |