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 |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 8 | from unittest import mock |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 9 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 10 | from chromite.api import api_config |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 11 | from chromite.api.controller import binhost |
| 12 | from chromite.api.gen.chromite.api import binhost_pb2 |
Greg Edelston | 724c13d | 2023-04-07 16:19:24 -0600 | [diff] [blame^] | 13 | from chromite.api.gen.chromiumos import common_pb2 |
| 14 | from chromite.lib import binpkg |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 15 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 16 | from chromite.lib import cros_test_lib |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 17 | from chromite.lib import osutils |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 18 | from chromite.service import binhost as binhost_service |
| 19 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 20 | |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 21 | class GetBinhostsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 22 | """Unittests for GetBinhosts.""" |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 23 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 24 | def setUp(self): |
| 25 | self.response = binhost_pb2.BinhostGetResponse() |
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 testValidateOnly(self): |
| 28 | """Check that a validate only call does not execute any logic.""" |
| 29 | patch = self.PatchObject(binhost_service, "GetBinhosts") |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 30 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 31 | request = binhost_pb2.BinhostGetRequest() |
| 32 | request.build_target.name = "target" |
| 33 | binhost.GetBinhosts(request, self.response, self.validate_only_config) |
| 34 | patch.assert_not_called() |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 35 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 37 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | patch = self.PatchObject(binhost_service, "GetBinhosts") |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 39 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | input_proto = binhost_pb2.BinhostGetRequest() |
| 41 | input_proto.build_target.name = "target" |
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 | binhost.GetBinhosts(input_proto, self.response, self.mock_call_config) |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | self.assertEqual(len(self.response.binhosts), 1) |
| 46 | self.assertEqual(self.response.binhosts[0].package_index, "Packages") |
| 47 | patch.assert_not_called() |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 48 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 49 | def testGetBinhosts(self): |
| 50 | """GetBinhosts calls service with correct args.""" |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 51 | # pylint: disable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 52 | binhost_list = [ |
| 53 | "gs://cr-prebuilt/board/amd64-generic/paladin-R66-17.0.0-rc2/packages/", |
| 54 | "gs://cr-prebuilt/board/eve/paladin-R66-17.0.0-rc2/packages/", |
| 55 | ] |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 56 | # pylint: enable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | get_binhost = self.PatchObject( |
| 58 | binhost_service, "GetBinhosts", return_value=binhost_list |
| 59 | ) |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 60 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 61 | input_proto = binhost_pb2.BinhostGetRequest() |
| 62 | input_proto.build_target.name = "target" |
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 | binhost.GetBinhosts(input_proto, self.response, self.api_config) |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 65 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | self.assertEqual(len(self.response.binhosts), 2) |
| 67 | self.assertEqual(self.response.binhosts[0].package_index, "Packages") |
| 68 | get_binhost.assert_called_once_with(mock.ANY) |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 69 | |
| 70 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 71 | class GetPrivatePrebuiltAclArgsTest( |
| 72 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 73 | ): |
| 74 | """Unittests for GetPrivatePrebuiltAclArgs.""" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | def setUp(self): |
| 77 | self.response = binhost_pb2.AclArgsResponse() |
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 testValidateOnly(self): |
| 80 | """Check that a validate only call does not execute any logic.""" |
| 81 | patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 82 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 83 | request = binhost_pb2.AclArgsRequest() |
| 84 | request.build_target.name = "target" |
| 85 | binhost.GetPrivatePrebuiltAclArgs( |
| 86 | request, self.response, self.validate_only_config |
| 87 | ) |
| 88 | patch.assert_not_called() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 89 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 91 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 93 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 94 | input_proto = binhost_pb2.AclArgsRequest() |
| 95 | input_proto.build_target.name = "target" |
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 | binhost.GetPrivatePrebuiltAclArgs( |
| 98 | input_proto, self.response, self.mock_call_config |
| 99 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 100 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 101 | self.assertEqual(len(self.response.args), 1) |
| 102 | self.assertEqual(self.response.args[0].arg, "-g") |
| 103 | self.assertEqual(self.response.args[0].value, "group1:READ") |
| 104 | patch.assert_not_called() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 105 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | def testGetPrivatePrebuiltAclArgs(self): |
| 107 | """GetPrivatePrebuildAclsArgs calls service with correct args.""" |
| 108 | argvalue_list = [["-g", "group1:READ"]] |
| 109 | get_binhost = self.PatchObject( |
| 110 | binhost_service, "GetPrebuiltAclArgs", return_value=argvalue_list |
| 111 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | input_proto = binhost_pb2.AclArgsRequest() |
| 114 | input_proto.build_target.name = "target" |
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 | binhost.GetPrivatePrebuiltAclArgs( |
| 117 | input_proto, self.response, self.api_config |
| 118 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | self.assertEqual(len(self.response.args), 1) |
| 121 | self.assertEqual(self.response.args[0].arg, "-g") |
| 122 | self.assertEqual(self.response.args[0].value, "group1:READ") |
| 123 | get_binhost.assert_called_once_with(mock.ANY) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 124 | |
| 125 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | class PrepareBinhostUploadsTest( |
| 127 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 128 | ): |
| 129 | """Unittests for PrepareBinhostUploads.""" |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | def setUp(self): |
| 132 | self.PatchObject( |
| 133 | binhost_service, |
| 134 | "GetPrebuiltsRoot", |
| 135 | return_value="/build/target/packages", |
| 136 | ) |
| 137 | self.PatchObject( |
| 138 | binhost_service, |
| 139 | "GetPrebuiltsFiles", |
| 140 | return_value=["foo.tbz2", "bar.tbz2"], |
| 141 | ) |
| 142 | self.PatchObject( |
| 143 | binhost_service, |
| 144 | "UpdatePackageIndex", |
| 145 | return_value="/build/target/packages/Packages", |
| 146 | ) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 147 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 148 | self.response = binhost_pb2.PrepareBinhostUploadsResponse() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 149 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | def testValidateOnly(self): |
| 151 | """Check that a validate only call does not execute any logic.""" |
| 152 | patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 153 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | request = binhost_pb2.PrepareBinhostUploadsRequest() |
| 155 | request.build_target.name = "target" |
| 156 | request.uri = "gs://chromeos-prebuilt/target" |
| 157 | rc = binhost.PrepareBinhostUploads( |
| 158 | request, self.response, self.validate_only_config |
| 159 | ) |
| 160 | patch.assert_not_called() |
| 161 | self.assertEqual(rc, 0) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 162 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 164 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 166 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 167 | request = binhost_pb2.PrepareBinhostUploadsRequest() |
| 168 | request.build_target.name = "target" |
| 169 | request.uri = "gs://chromeos-prebuilt/target" |
| 170 | rc = binhost.PrepareBinhostUploads( |
| 171 | request, self.response, self.mock_call_config |
| 172 | ) |
| 173 | self.assertEqual(self.response.uploads_dir, "/upload/directory") |
| 174 | self.assertEqual(self.response.upload_targets[0].path, "upload_target") |
| 175 | patch.assert_not_called() |
| 176 | self.assertEqual(rc, 0) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 177 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | def testPrepareBinhostUploads(self): |
| 179 | """PrepareBinhostUploads returns Packages and tar files.""" |
| 180 | input_proto = binhost_pb2.PrepareBinhostUploadsRequest() |
| 181 | input_proto.build_target.name = "target" |
| 182 | input_proto.uri = "gs://chromeos-prebuilt/target" |
| 183 | binhost.PrepareBinhostUploads( |
| 184 | input_proto, self.response, self.api_config |
| 185 | ) |
| 186 | self.assertEqual(self.response.uploads_dir, "/build/target/packages") |
| 187 | self.assertCountEqual( |
| 188 | [ut.path for ut in self.response.upload_targets], |
| 189 | ["Packages", "foo.tbz2", "bar.tbz2"], |
| 190 | ) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 191 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 192 | def testPrepareBinhostUploadsNonGsUri(self): |
| 193 | """PrepareBinhostUploads dies when URI does not point to GS.""" |
| 194 | input_proto = binhost_pb2.PrepareBinhostUploadsRequest() |
| 195 | input_proto.build_target.name = "target" |
| 196 | input_proto.uri = "https://foo.bar" |
| 197 | with self.assertRaises(ValueError): |
| 198 | binhost.PrepareBinhostUploads( |
| 199 | input_proto, self.response, self.api_config |
| 200 | ) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 201 | |
| 202 | |
Greg Edelston | 724c13d | 2023-04-07 16:19:24 -0600 | [diff] [blame^] | 203 | class UpdatePackageIndexTest( |
| 204 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 205 | ): |
| 206 | """Unit tests for BinhostService/UpdatePackageIndex.""" |
| 207 | |
| 208 | def setUp(self): |
| 209 | self._original_pkg_index = binpkg.PackageIndex() |
| 210 | self._original_pkg_index.header["A"] = "B" |
| 211 | self._original_pkg_index.packages = [ |
| 212 | { |
| 213 | "CPV": "foo/bar", |
| 214 | "KEY": "value", |
| 215 | }, |
| 216 | { |
| 217 | "CPV": "cat/pkg", |
| 218 | "KEY": "also_value", |
| 219 | }, |
| 220 | ] |
| 221 | self._pkg_index_fp = os.path.join( |
| 222 | self.tempdir, |
| 223 | "path/to/packages/Packages", |
| 224 | ) |
| 225 | |
| 226 | def _write_original_package_index(self): |
| 227 | """Write the package index to the tempdir. |
| 228 | |
| 229 | Note that if an input_proto specifies location=INSIDE, then they will |
| 230 | not be able to find the written file, since the tempdir isn't actually |
| 231 | inside a chroot. |
| 232 | """ |
| 233 | osutils.Touch(self._pkg_index_fp, makedirs=True) |
| 234 | self._original_pkg_index.WriteFile(self._pkg_index_fp) |
| 235 | |
| 236 | def testValidateOnly(self): |
| 237 | """Check that a validate only call does not execute any logic.""" |
| 238 | self._write_original_package_index() |
| 239 | patch = self.PatchObject(binpkg.PackageIndex, "ReadFilePath") |
| 240 | request = binhost_pb2.UpdatePackageIndexRequest( |
| 241 | package_index_file=common_pb2.Path( |
| 242 | path=self._pkg_index_fp, |
| 243 | location=common_pb2.Path.Location.OUTSIDE, |
| 244 | ), |
| 245 | set_upload_location=True, |
| 246 | ) |
| 247 | response = binhost_pb2.UpdatePackageIndexResponse() |
| 248 | binhost.UpdatePackageIndex(request, response, self.validate_only_config) |
| 249 | patch.assert_not_called() |
| 250 | |
| 251 | def testMustProvideSomeCommand(self): |
| 252 | """Test that an error is raised if no update types are specified.""" |
| 253 | self._write_original_package_index() |
| 254 | request = binhost_pb2.UpdatePackageIndexRequest( |
| 255 | package_index_file=common_pb2.Path( |
| 256 | path=self._pkg_index_fp, |
| 257 | location=common_pb2.Path.OUTSIDE, |
| 258 | ), |
| 259 | uri="gs://chromeos-prebuilt/board/amd64-host/packages", |
| 260 | ) |
| 261 | response = binhost_pb2.UpdatePackageIndexResponse() |
| 262 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 263 | binhost.UpdatePackageIndex(request, response, self.api_config) |
| 264 | |
| 265 | def testSetUploadLocation(self): |
| 266 | """Test setting the package upload location in the index file. |
| 267 | |
| 268 | This test includes correctly parsing the input uri. |
| 269 | """ |
| 270 | # Arrange |
| 271 | self._write_original_package_index() |
| 272 | |
| 273 | # Act |
| 274 | request = binhost_pb2.UpdatePackageIndexRequest( |
| 275 | package_index_file=common_pb2.Path( |
| 276 | path=self._pkg_index_fp, |
| 277 | location=common_pb2.Path.Location.OUTSIDE, |
| 278 | ), |
| 279 | set_upload_location=True, |
| 280 | uri="gs://chromeos-prebuilt/board/amd64-host/packages/", |
| 281 | ) |
| 282 | response = binhost_pb2.UpdatePackageIndexResponse() |
| 283 | binhost.UpdatePackageIndex(request, response, self.api_config) |
| 284 | |
| 285 | # Assert |
| 286 | new_pkg_index = binpkg.PackageIndex() |
| 287 | new_pkg_index.ReadFilePath(self._pkg_index_fp) |
| 288 | self.assertEqual(new_pkg_index.header["URI"], "gs://chromeos-prebuilt") |
| 289 | self.assertDictEqual( |
| 290 | new_pkg_index.packages[0], |
| 291 | { |
| 292 | "CPV": "cat/pkg", |
| 293 | "KEY": "also_value", |
| 294 | "PATH": "board/amd64-host/packages/cat/pkg.tbz2", |
| 295 | }, |
| 296 | ) |
| 297 | self.assertDictEqual( |
| 298 | new_pkg_index.packages[1], |
| 299 | { |
| 300 | "CPV": "foo/bar", |
| 301 | "KEY": "value", |
| 302 | "PATH": "board/amd64-host/packages/foo/bar.tbz2", |
| 303 | }, |
| 304 | ) |
| 305 | |
| 306 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 307 | class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 308 | """Unittests for SetBinhost.""" |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 309 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 310 | def setUp(self): |
| 311 | self.response = binhost_pb2.SetBinhostResponse() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 312 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 313 | def testValidateOnly(self): |
| 314 | """Check that a validate only call does not execute any logic.""" |
| 315 | patch = self.PatchObject(binhost_service, "SetBinhost") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 316 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 317 | request = binhost_pb2.SetBinhostRequest() |
| 318 | request.build_target.name = "target" |
| 319 | request.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 320 | request.uri = "gs://chromeos-prebuilt/target" |
| 321 | binhost.SetBinhost(request, self.response, self.validate_only_config) |
| 322 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 323 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 324 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 325 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 326 | patch = self.PatchObject(binhost_service, "SetBinhost") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 327 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | request = binhost_pb2.SetBinhostRequest() |
| 329 | request.build_target.name = "target" |
| 330 | request.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 331 | request.uri = "gs://chromeos-prebuilt/target" |
Arif Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 332 | request.max_uris = 4 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 333 | binhost.SetBinhost(request, self.response, self.mock_call_config) |
| 334 | patch.assert_not_called() |
| 335 | self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 336 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 337 | def testSetBinhost(self): |
| 338 | """SetBinhost calls service with correct args.""" |
| 339 | set_binhost = self.PatchObject( |
| 340 | binhost_service, "SetBinhost", return_value="/path/to/BINHOST.conf" |
| 341 | ) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 342 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 343 | input_proto = binhost_pb2.SetBinhostRequest() |
| 344 | input_proto.build_target.name = "target" |
| 345 | input_proto.private = True |
| 346 | input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 347 | input_proto.uri = "gs://chromeos-prebuilt/target" |
Arif Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 348 | input_proto.max_uris = 4 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 349 | binhost.SetBinhost(input_proto, self.response, self.api_config) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 350 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 351 | self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf") |
| 352 | set_binhost.assert_called_once_with( |
| 353 | "target", |
| 354 | "POSTSUBMIT_BINHOST", |
| 355 | "gs://chromeos-prebuilt/target", |
| 356 | private=True, |
Arif Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 357 | max_uris=4, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 358 | ) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 359 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 360 | |
Arif Kasim | a046726 | 2022-11-11 17:08:14 +0000 | [diff] [blame] | 361 | class GetBinhostConfPathTest( |
| 362 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 363 | ): |
| 364 | """Unittests for GetBinhostConfPath.""" |
| 365 | |
| 366 | def setUp(self): |
| 367 | self.response = binhost_pb2.GetBinhostConfPathResponse() |
| 368 | |
| 369 | def testValidateOnly(self): |
| 370 | """Check that a validate only call does not execute any logic.""" |
| 371 | patch = self.PatchObject(binhost_service, "GetBinhostConfPath") |
| 372 | |
| 373 | request = binhost_pb2.GetBinhostConfPathRequest() |
| 374 | request.build_target.name = "target" |
| 375 | request.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 376 | binhost.GetBinhostConfPath( |
| 377 | request, self.response, self.validate_only_config |
| 378 | ) |
| 379 | patch.assert_not_called() |
| 380 | |
| 381 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 382 | """Test a mock call does not execute logic, returns mocked value.""" |
Arif Kasim | a046726 | 2022-11-11 17:08:14 +0000 | [diff] [blame] | 383 | patch = self.PatchObject(binhost_service, "GetBinhostConfPath") |
| 384 | |
| 385 | request = binhost_pb2.GetBinhostConfPathRequest() |
| 386 | request.build_target.name = "target" |
| 387 | request.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 388 | binhost.GetBinhostConfPath( |
| 389 | request, self.response, self.mock_call_config |
| 390 | ) |
| 391 | patch.assert_not_called() |
| 392 | self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf") |
| 393 | |
| 394 | def testGetBinhostConfPath(self): |
| 395 | """GetBinhostConfPath calls service with correct args.""" |
| 396 | get_binhost_conf_path = self.PatchObject( |
| 397 | binhost_service, |
| 398 | "GetBinhostConfPath", |
| 399 | return_value="/path/to/BINHOST.conf", |
| 400 | ) |
| 401 | input_proto = binhost_pb2.GetBinhostConfPathRequest() |
| 402 | input_proto.build_target.name = "target" |
| 403 | input_proto.private = True |
| 404 | input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 405 | binhost.GetBinhostConfPath(input_proto, self.response, self.api_config) |
| 406 | |
| 407 | self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf") |
| 408 | get_binhost_conf_path.assert_called_once_with( |
| 409 | "target", |
| 410 | "POSTSUBMIT_BINHOST", |
| 411 | True, |
| 412 | ) |
| 413 | |
| 414 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 415 | class RegenBuildCacheTest( |
| 416 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 417 | ): |
| 418 | """Unittests for RegenBuildCache.""" |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 419 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | def setUp(self): |
| 421 | self.response = binhost_pb2.RegenBuildCacheResponse() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 422 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 423 | def testValidateOnly(self): |
| 424 | """Check that a validate only call does not execute any logic.""" |
| 425 | patch = self.PatchObject(binhost_service, "RegenBuildCache") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 426 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 427 | request = binhost_pb2.RegenBuildCacheRequest() |
| 428 | request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH |
| 429 | binhost.RegenBuildCache( |
| 430 | request, self.response, self.validate_only_config |
| 431 | ) |
| 432 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 433 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 434 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 435 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 436 | patch = self.PatchObject(binhost_service, "RegenBuildCache") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 437 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 438 | request = binhost_pb2.RegenBuildCacheRequest() |
| 439 | request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH |
| 440 | binhost.RegenBuildCache(request, self.response, self.mock_call_config) |
| 441 | patch.assert_not_called() |
| 442 | self.assertEqual(len(self.response.modified_overlays), 1) |
| 443 | self.assertEqual( |
| 444 | self.response.modified_overlays[0].path, "/path/to/BuildCache" |
| 445 | ) |
| 446 | |
| 447 | def testRegenBuildCache(self): |
| 448 | """RegenBuildCache calls service with the correct args.""" |
| 449 | regen_cache = self.PatchObject(binhost_service, "RegenBuildCache") |
| 450 | |
| 451 | input_proto = binhost_pb2.RegenBuildCacheRequest() |
| 452 | input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH |
| 453 | |
| 454 | binhost.RegenBuildCache(input_proto, self.response, self.api_config) |
| 455 | regen_cache.assert_called_once_with(mock.ANY, "both") |
| 456 | |
| 457 | def testRequiresOverlayType(self): |
| 458 | """RegenBuildCache dies if overlay_type not specified.""" |
| 459 | regen_cache = self.PatchObject(binhost_service, "RegenBuildCache") |
| 460 | |
| 461 | input_proto = binhost_pb2.RegenBuildCacheRequest() |
| 462 | input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED |
| 463 | |
| 464 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 465 | binhost.RegenBuildCache(input_proto, self.response, self.api_config) |
| 466 | regen_cache.assert_not_called() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 467 | |
| 468 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 469 | class PrepareDevInstallerBinhostUploadsTest( |
| 470 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 471 | ): |
| 472 | """Tests for the UploadDevInstallerPrebuilts stage.""" |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 473 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 474 | def setUp(self): |
| 475 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 476 | # target packages dir |
| 477 | self.chroot_path = os.path.join(self.tempdir, "chroot") |
| 478 | self.sysroot_path = "/build/target" |
| 479 | self.chroot_path = os.path.join(self.tempdir, "chroot") |
| 480 | full_sysroot_path = os.path.join( |
| 481 | self.chroot_path, self.sysroot_path.lstrip(os.sep) |
| 482 | ) |
| 483 | self.full_sysroot_package_path = os.path.join( |
| 484 | full_sysroot_path, "packages" |
| 485 | ) |
| 486 | osutils.SafeMakedirs(self.full_sysroot_package_path) |
| 487 | self.uploads_dir = os.path.join(self.tempdir, "uploads_dir") |
| 488 | osutils.SafeMakedirs(self.uploads_dir) |
| 489 | # Create packages/Packages file |
| 490 | packages_file = os.path.join(self.full_sysroot_package_path, "Packages") |
| 491 | packages_content = """\ |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 492 | USE: test |
| 493 | |
| 494 | CPV: app-arch/brotli-1.0.6 |
| 495 | |
| 496 | CPV: app-arch/zip-3.0-r3 |
| 497 | |
| 498 | CPV: chromeos-base/shill-0.0.1-r1 |
| 499 | |
| 500 | CPV: chromeos-base/test-0.0.1-r1 |
| 501 | |
| 502 | CPV: virtual/chromium-os-printing-1-r4 |
| 503 | |
| 504 | CPV: virtual/python-enum34-1 |
| 505 | |
| 506 | """ |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 507 | osutils.WriteFile(packages_file, packages_content) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 508 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 509 | # Create package.installable file |
| 510 | self.dev_install_packages = [ |
| 511 | "app-arch/zip-3.0-r3", |
| 512 | "virtual/chromium-os-printing-1-r4", |
| 513 | "virtual/python-enum34-1", |
| 514 | ] |
| 515 | package_installable_dir = os.path.join( |
| 516 | full_sysroot_path, "build/dev-install" |
| 517 | ) |
| 518 | osutils.SafeMakedirs(package_installable_dir) |
| 519 | package_installable_filename = os.path.join( |
| 520 | package_installable_dir, "package.installable" |
| 521 | ) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 522 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 523 | # Create path to the dev_install_packages |
| 524 | packages_dir = os.path.join(full_sysroot_path, "packages") |
| 525 | osutils.SafeMakedirs(packages_dir) |
| 526 | for package in self.dev_install_packages: |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 527 | # Since a package has a category, such as app-arch/zip-3.0-r3, we |
| 528 | # need to create the packages_dir / category dir as needed. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 529 | category = package.split(os.sep)[0] |
| 530 | osutils.SafeMakedirs(os.path.join(packages_dir, category)) |
| 531 | package_tbz2_file = os.path.join(packages_dir, package) + ".tbz2" |
| 532 | osutils.Touch(package_tbz2_file) |
| 533 | with open( |
Mike Frysinger | d97829b | 2023-02-24 16:09:20 -0500 | [diff] [blame] | 534 | package_installable_filename, "w", encoding="utf-8" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 535 | ) as package_installable_file: |
| 536 | for package in self.dev_install_packages: |
| 537 | package_installable_file.write(package + "\n") |
| 538 | self.response = binhost_pb2.PrepareDevInstallBinhostUploadsResponse() |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 539 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 540 | def testValidateOnly(self): |
| 541 | """Check that a validate only call does not execute any logic.""" |
| 542 | patch = self.PatchObject( |
| 543 | binhost_service, "ReadDevInstallFilesToCreatePackageIndex" |
| 544 | ) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 545 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 546 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 547 | input_proto.uri = "gs://chromeos-prebuilt/target" |
| 548 | input_proto.chroot.path = self.chroot_path |
| 549 | input_proto.sysroot.path = self.sysroot_path |
| 550 | input_proto.uploads_dir = self.uploads_dir |
| 551 | binhost.PrepareDevInstallBinhostUploads( |
| 552 | input_proto, self.response, self.validate_only_config |
| 553 | ) |
| 554 | patch.assert_not_called() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 555 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 556 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 557 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 558 | patch = self.PatchObject( |
| 559 | binhost_service, "ReadDevInstallFilesToCreatePackageIndex" |
| 560 | ) |
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 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 563 | input_proto.uri = "gs://chromeos-prebuilt/target" |
| 564 | input_proto.chroot.path = self.chroot_path |
| 565 | input_proto.sysroot.path = self.sysroot_path |
| 566 | input_proto.uploads_dir = self.uploads_dir |
| 567 | binhost.PrepareDevInstallBinhostUploads( |
| 568 | input_proto, self.response, self.mock_call_config |
| 569 | ) |
| 570 | self.assertEqual(len(self.response.upload_targets), 3) |
| 571 | self.assertEqual(self.response.upload_targets[2].path, "Packages") |
| 572 | patch.assert_not_called() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 573 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 574 | def testDevInstallerUpload(self): |
| 575 | """Test uploads of dev installer prebuilts.""" |
| 576 | # self.RunStage() |
| 577 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 578 | input_proto.uri = "gs://chromeos-prebuilt/target" |
| 579 | input_proto.chroot.path = self.chroot_path |
| 580 | input_proto.sysroot.path = self.sysroot_path |
| 581 | input_proto.uploads_dir = self.uploads_dir |
| 582 | # Call method under test |
| 583 | binhost.PrepareDevInstallBinhostUploads( |
| 584 | input_proto, self.response, self.api_config |
| 585 | ) |
| 586 | # Verify results |
| 587 | expected_upload_targets = [ |
| 588 | "app-arch/zip-3.0-r3.tbz2", |
| 589 | "virtual/chromium-os-printing-1-r4.tbz2", |
| 590 | "virtual/python-enum34-1.tbz2", |
| 591 | "Packages", |
| 592 | ] |
| 593 | self.assertCountEqual( |
| 594 | [ut.path for ut in self.response.upload_targets], |
| 595 | expected_upload_targets, |
| 596 | ) |
| 597 | # All of the upload_targets should also be in the uploads_directory |
| 598 | for target in self.response.upload_targets: |
| 599 | self.assertExists( |
| 600 | os.path.join(input_proto.uploads_dir, target.path) |
| 601 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 602 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 603 | def testPrepareBinhostUploadsNonGsUri(self): |
| 604 | """PrepareBinhostUploads dies when URI does not point to GS.""" |
| 605 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 606 | input_proto.chroot.path = self.chroot_path |
| 607 | input_proto.sysroot.path = self.sysroot_path |
| 608 | input_proto.uploads_dir = self.uploads_dir |
| 609 | input_proto.uri = "https://foo.bar" |
| 610 | with self.assertRaises(ValueError): |
| 611 | binhost.PrepareDevInstallBinhostUploads( |
| 612 | input_proto, self.response, self.api_config |
| 613 | ) |