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 |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 13 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 14 | from chromite.lib import cros_test_lib |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 15 | from chromite.lib import osutils |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 16 | from chromite.service import binhost as binhost_service |
| 17 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 18 | |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 19 | class GetBinhostsTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | """Unittests for GetBinhosts.""" |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 21 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 22 | def setUp(self): |
| 23 | self.response = binhost_pb2.BinhostGetResponse() |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 24 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | def testValidateOnly(self): |
| 26 | """Check that a validate only call does not execute any logic.""" |
| 27 | patch = self.PatchObject(binhost_service, "GetBinhosts") |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 28 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | request = binhost_pb2.BinhostGetRequest() |
| 30 | request.build_target.name = "target" |
| 31 | binhost.GetBinhosts(request, self.response, self.validate_only_config) |
| 32 | patch.assert_not_called() |
Michael 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 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame^] | 35 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | patch = self.PatchObject(binhost_service, "GetBinhosts") |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 37 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | input_proto = binhost_pb2.BinhostGetRequest() |
| 39 | input_proto.build_target.name = "target" |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 40 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | binhost.GetBinhosts(input_proto, self.response, self.mock_call_config) |
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 | self.assertEqual(len(self.response.binhosts), 1) |
| 44 | self.assertEqual(self.response.binhosts[0].package_index, "Packages") |
| 45 | patch.assert_not_called() |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 46 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | def testGetBinhosts(self): |
| 48 | """GetBinhosts calls service with correct args.""" |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame^] | 49 | # pylint: disable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | binhost_list = [ |
| 51 | "gs://cr-prebuilt/board/amd64-generic/paladin-R66-17.0.0-rc2/packages/", |
| 52 | "gs://cr-prebuilt/board/eve/paladin-R66-17.0.0-rc2/packages/", |
| 53 | ] |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame^] | 54 | # pylint: enable=line-too-long |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | get_binhost = self.PatchObject( |
| 56 | binhost_service, "GetBinhosts", return_value=binhost_list |
| 57 | ) |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 58 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | input_proto = binhost_pb2.BinhostGetRequest() |
| 60 | input_proto.build_target.name = "target" |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | binhost.GetBinhosts(input_proto, self.response, self.api_config) |
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 | self.assertEqual(len(self.response.binhosts), 2) |
| 65 | self.assertEqual(self.response.binhosts[0].package_index, "Packages") |
| 66 | get_binhost.assert_called_once_with(mock.ANY) |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 67 | |
| 68 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | class GetPrivatePrebuiltAclArgsTest( |
| 70 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 71 | ): |
| 72 | """Unittests for GetPrivatePrebuiltAclArgs.""" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 73 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 74 | def setUp(self): |
| 75 | self.response = binhost_pb2.AclArgsResponse() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | def testValidateOnly(self): |
| 78 | """Check that a validate only call does not execute any logic.""" |
| 79 | patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | request = binhost_pb2.AclArgsRequest() |
| 82 | request.build_target.name = "target" |
| 83 | binhost.GetPrivatePrebuiltAclArgs( |
| 84 | request, self.response, self.validate_only_config |
| 85 | ) |
| 86 | patch.assert_not_called() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 87 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame^] | 89 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 90 | patch = self.PatchObject(binhost_service, "GetPrebuiltAclArgs") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 91 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | input_proto = binhost_pb2.AclArgsRequest() |
| 93 | input_proto.build_target.name = "target" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 94 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | binhost.GetPrivatePrebuiltAclArgs( |
| 96 | input_proto, self.response, self.mock_call_config |
| 97 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 98 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | self.assertEqual(len(self.response.args), 1) |
| 100 | self.assertEqual(self.response.args[0].arg, "-g") |
| 101 | self.assertEqual(self.response.args[0].value, "group1:READ") |
| 102 | patch.assert_not_called() |
Michael 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 | def testGetPrivatePrebuiltAclArgs(self): |
| 105 | """GetPrivatePrebuildAclsArgs calls service with correct args.""" |
| 106 | argvalue_list = [["-g", "group1:READ"]] |
| 107 | get_binhost = self.PatchObject( |
| 108 | binhost_service, "GetPrebuiltAclArgs", return_value=argvalue_list |
| 109 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 110 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 111 | input_proto = binhost_pb2.AclArgsRequest() |
| 112 | input_proto.build_target.name = "target" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | binhost.GetPrivatePrebuiltAclArgs( |
| 115 | input_proto, self.response, self.api_config |
| 116 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | self.assertEqual(len(self.response.args), 1) |
| 119 | self.assertEqual(self.response.args[0].arg, "-g") |
| 120 | self.assertEqual(self.response.args[0].value, "group1:READ") |
| 121 | get_binhost.assert_called_once_with(mock.ANY) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 122 | |
| 123 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 124 | class PrepareBinhostUploadsTest( |
| 125 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 126 | ): |
| 127 | """Unittests for PrepareBinhostUploads.""" |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | def setUp(self): |
| 130 | self.PatchObject( |
| 131 | binhost_service, |
| 132 | "GetPrebuiltsRoot", |
| 133 | return_value="/build/target/packages", |
| 134 | ) |
| 135 | self.PatchObject( |
| 136 | binhost_service, |
| 137 | "GetPrebuiltsFiles", |
| 138 | return_value=["foo.tbz2", "bar.tbz2"], |
| 139 | ) |
| 140 | self.PatchObject( |
| 141 | binhost_service, |
| 142 | "UpdatePackageIndex", |
| 143 | return_value="/build/target/packages/Packages", |
| 144 | ) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 146 | self.response = binhost_pb2.PrepareBinhostUploadsResponse() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 147 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 148 | def testValidateOnly(self): |
| 149 | """Check that a validate only call does not execute any logic.""" |
| 150 | patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 151 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 152 | request = binhost_pb2.PrepareBinhostUploadsRequest() |
| 153 | request.build_target.name = "target" |
| 154 | request.uri = "gs://chromeos-prebuilt/target" |
| 155 | rc = binhost.PrepareBinhostUploads( |
| 156 | request, self.response, self.validate_only_config |
| 157 | ) |
| 158 | patch.assert_not_called() |
| 159 | self.assertEqual(rc, 0) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 160 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 161 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame^] | 162 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | patch = self.PatchObject(binhost_service, "GetPrebuiltsRoot") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 164 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | request = binhost_pb2.PrepareBinhostUploadsRequest() |
| 166 | request.build_target.name = "target" |
| 167 | request.uri = "gs://chromeos-prebuilt/target" |
| 168 | rc = binhost.PrepareBinhostUploads( |
| 169 | request, self.response, self.mock_call_config |
| 170 | ) |
| 171 | self.assertEqual(self.response.uploads_dir, "/upload/directory") |
| 172 | self.assertEqual(self.response.upload_targets[0].path, "upload_target") |
| 173 | patch.assert_not_called() |
| 174 | self.assertEqual(rc, 0) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 175 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 176 | def testPrepareBinhostUploads(self): |
| 177 | """PrepareBinhostUploads returns Packages and tar files.""" |
| 178 | input_proto = binhost_pb2.PrepareBinhostUploadsRequest() |
| 179 | input_proto.build_target.name = "target" |
| 180 | input_proto.uri = "gs://chromeos-prebuilt/target" |
| 181 | binhost.PrepareBinhostUploads( |
| 182 | input_proto, self.response, self.api_config |
| 183 | ) |
| 184 | self.assertEqual(self.response.uploads_dir, "/build/target/packages") |
| 185 | self.assertCountEqual( |
| 186 | [ut.path for ut in self.response.upload_targets], |
| 187 | ["Packages", "foo.tbz2", "bar.tbz2"], |
| 188 | ) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 189 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 190 | def testPrepareBinhostUploadsNonGsUri(self): |
| 191 | """PrepareBinhostUploads dies when URI does not point to GS.""" |
| 192 | input_proto = binhost_pb2.PrepareBinhostUploadsRequest() |
| 193 | input_proto.build_target.name = "target" |
| 194 | input_proto.uri = "https://foo.bar" |
| 195 | with self.assertRaises(ValueError): |
| 196 | binhost.PrepareBinhostUploads( |
| 197 | input_proto, self.response, self.api_config |
| 198 | ) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 199 | |
| 200 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 201 | class SetBinhostTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 202 | """Unittests for SetBinhost.""" |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 203 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | def setUp(self): |
| 205 | self.response = binhost_pb2.SetBinhostResponse() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 206 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 207 | def testValidateOnly(self): |
| 208 | """Check that a validate only call does not execute any logic.""" |
| 209 | patch = self.PatchObject(binhost_service, "SetBinhost") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 210 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 211 | request = binhost_pb2.SetBinhostRequest() |
| 212 | request.build_target.name = "target" |
| 213 | request.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 214 | request.uri = "gs://chromeos-prebuilt/target" |
| 215 | binhost.SetBinhost(request, self.response, self.validate_only_config) |
| 216 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 217 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 218 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame^] | 219 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 220 | patch = self.PatchObject(binhost_service, "SetBinhost") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 221 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 222 | request = binhost_pb2.SetBinhostRequest() |
| 223 | request.build_target.name = "target" |
| 224 | request.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 225 | request.uri = "gs://chromeos-prebuilt/target" |
Arif Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 226 | request.max_uris = 4 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 227 | binhost.SetBinhost(request, self.response, self.mock_call_config) |
| 228 | patch.assert_not_called() |
| 229 | self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 230 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 231 | def testSetBinhost(self): |
| 232 | """SetBinhost calls service with correct args.""" |
| 233 | set_binhost = self.PatchObject( |
| 234 | binhost_service, "SetBinhost", return_value="/path/to/BINHOST.conf" |
| 235 | ) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 236 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 237 | input_proto = binhost_pb2.SetBinhostRequest() |
| 238 | input_proto.build_target.name = "target" |
| 239 | input_proto.private = True |
| 240 | input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 241 | input_proto.uri = "gs://chromeos-prebuilt/target" |
Arif Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 242 | input_proto.max_uris = 4 |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 243 | binhost.SetBinhost(input_proto, self.response, self.api_config) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 244 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 245 | self.assertEqual(self.response.output_file, "/path/to/BINHOST.conf") |
| 246 | set_binhost.assert_called_once_with( |
| 247 | "target", |
| 248 | "POSTSUBMIT_BINHOST", |
| 249 | "gs://chromeos-prebuilt/target", |
| 250 | private=True, |
Arif Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 251 | max_uris=4, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 252 | ) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 253 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 254 | |
Arif Kasim | a046726 | 2022-11-11 17:08:14 +0000 | [diff] [blame] | 255 | class GetBinhostConfPathTest( |
| 256 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 257 | ): |
| 258 | """Unittests for GetBinhostConfPath.""" |
| 259 | |
| 260 | def setUp(self): |
| 261 | self.response = binhost_pb2.GetBinhostConfPathResponse() |
| 262 | |
| 263 | def testValidateOnly(self): |
| 264 | """Check that a validate only call does not execute any logic.""" |
| 265 | patch = self.PatchObject(binhost_service, "GetBinhostConfPath") |
| 266 | |
| 267 | request = binhost_pb2.GetBinhostConfPathRequest() |
| 268 | request.build_target.name = "target" |
| 269 | request.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 270 | binhost.GetBinhostConfPath( |
| 271 | request, self.response, self.validate_only_config |
| 272 | ) |
| 273 | patch.assert_not_called() |
| 274 | |
| 275 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame^] | 276 | """Test a mock call does not execute logic, returns mocked value.""" |
Arif Kasim | a046726 | 2022-11-11 17:08:14 +0000 | [diff] [blame] | 277 | patch = self.PatchObject(binhost_service, "GetBinhostConfPath") |
| 278 | |
| 279 | request = binhost_pb2.GetBinhostConfPathRequest() |
| 280 | request.build_target.name = "target" |
| 281 | request.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 282 | binhost.GetBinhostConfPath( |
| 283 | request, self.response, self.mock_call_config |
| 284 | ) |
| 285 | patch.assert_not_called() |
| 286 | self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf") |
| 287 | |
| 288 | def testGetBinhostConfPath(self): |
| 289 | """GetBinhostConfPath calls service with correct args.""" |
| 290 | get_binhost_conf_path = self.PatchObject( |
| 291 | binhost_service, |
| 292 | "GetBinhostConfPath", |
| 293 | return_value="/path/to/BINHOST.conf", |
| 294 | ) |
| 295 | input_proto = binhost_pb2.GetBinhostConfPathRequest() |
| 296 | input_proto.build_target.name = "target" |
| 297 | input_proto.private = True |
| 298 | input_proto.key = binhost_pb2.POSTSUBMIT_BINHOST |
| 299 | binhost.GetBinhostConfPath(input_proto, self.response, self.api_config) |
| 300 | |
| 301 | self.assertEqual(self.response.conf_path, "/path/to/BINHOST.conf") |
| 302 | get_binhost_conf_path.assert_called_once_with( |
| 303 | "target", |
| 304 | "POSTSUBMIT_BINHOST", |
| 305 | True, |
| 306 | ) |
| 307 | |
| 308 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 309 | class RegenBuildCacheTest( |
| 310 | cros_test_lib.MockTestCase, api_config.ApiConfigMixin |
| 311 | ): |
| 312 | """Unittests for RegenBuildCache.""" |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 313 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 314 | def setUp(self): |
| 315 | self.response = binhost_pb2.RegenBuildCacheResponse() |
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 | def testValidateOnly(self): |
| 318 | """Check that a validate only call does not execute any logic.""" |
| 319 | patch = self.PatchObject(binhost_service, "RegenBuildCache") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 320 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 321 | request = binhost_pb2.RegenBuildCacheRequest() |
| 322 | request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH |
| 323 | binhost.RegenBuildCache( |
| 324 | request, self.response, self.validate_only_config |
| 325 | ) |
| 326 | patch.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 327 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame^] | 329 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 330 | patch = self.PatchObject(binhost_service, "RegenBuildCache") |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 331 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 332 | request = binhost_pb2.RegenBuildCacheRequest() |
| 333 | request.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH |
| 334 | binhost.RegenBuildCache(request, self.response, self.mock_call_config) |
| 335 | patch.assert_not_called() |
| 336 | self.assertEqual(len(self.response.modified_overlays), 1) |
| 337 | self.assertEqual( |
| 338 | self.response.modified_overlays[0].path, "/path/to/BuildCache" |
| 339 | ) |
| 340 | |
| 341 | def testRegenBuildCache(self): |
| 342 | """RegenBuildCache calls service with the correct args.""" |
| 343 | regen_cache = self.PatchObject(binhost_service, "RegenBuildCache") |
| 344 | |
| 345 | input_proto = binhost_pb2.RegenBuildCacheRequest() |
| 346 | input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH |
| 347 | |
| 348 | binhost.RegenBuildCache(input_proto, self.response, self.api_config) |
| 349 | regen_cache.assert_called_once_with(mock.ANY, "both") |
| 350 | |
| 351 | def testRequiresOverlayType(self): |
| 352 | """RegenBuildCache dies if overlay_type not specified.""" |
| 353 | regen_cache = self.PatchObject(binhost_service, "RegenBuildCache") |
| 354 | |
| 355 | input_proto = binhost_pb2.RegenBuildCacheRequest() |
| 356 | input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED |
| 357 | |
| 358 | with self.assertRaises(cros_build_lib.DieSystemExit): |
| 359 | binhost.RegenBuildCache(input_proto, self.response, self.api_config) |
| 360 | regen_cache.assert_not_called() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 361 | |
| 362 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 363 | class PrepareDevInstallerBinhostUploadsTest( |
| 364 | cros_test_lib.MockTempDirTestCase, api_config.ApiConfigMixin |
| 365 | ): |
| 366 | """Tests for the UploadDevInstallerPrebuilts stage.""" |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 367 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 368 | def setUp(self): |
| 369 | self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False) |
| 370 | # target packages dir |
| 371 | self.chroot_path = os.path.join(self.tempdir, "chroot") |
| 372 | self.sysroot_path = "/build/target" |
| 373 | self.chroot_path = os.path.join(self.tempdir, "chroot") |
| 374 | full_sysroot_path = os.path.join( |
| 375 | self.chroot_path, self.sysroot_path.lstrip(os.sep) |
| 376 | ) |
| 377 | self.full_sysroot_package_path = os.path.join( |
| 378 | full_sysroot_path, "packages" |
| 379 | ) |
| 380 | osutils.SafeMakedirs(self.full_sysroot_package_path) |
| 381 | self.uploads_dir = os.path.join(self.tempdir, "uploads_dir") |
| 382 | osutils.SafeMakedirs(self.uploads_dir) |
| 383 | # Create packages/Packages file |
| 384 | packages_file = os.path.join(self.full_sysroot_package_path, "Packages") |
| 385 | packages_content = """\ |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 386 | USE: test |
| 387 | |
| 388 | CPV: app-arch/brotli-1.0.6 |
| 389 | |
| 390 | CPV: app-arch/zip-3.0-r3 |
| 391 | |
| 392 | CPV: chromeos-base/shill-0.0.1-r1 |
| 393 | |
| 394 | CPV: chromeos-base/test-0.0.1-r1 |
| 395 | |
| 396 | CPV: virtual/chromium-os-printing-1-r4 |
| 397 | |
| 398 | CPV: virtual/python-enum34-1 |
| 399 | |
| 400 | """ |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 401 | osutils.WriteFile(packages_file, packages_content) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 402 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 403 | # Create package.installable file |
| 404 | self.dev_install_packages = [ |
| 405 | "app-arch/zip-3.0-r3", |
| 406 | "virtual/chromium-os-printing-1-r4", |
| 407 | "virtual/python-enum34-1", |
| 408 | ] |
| 409 | package_installable_dir = os.path.join( |
| 410 | full_sysroot_path, "build/dev-install" |
| 411 | ) |
| 412 | osutils.SafeMakedirs(package_installable_dir) |
| 413 | package_installable_filename = os.path.join( |
| 414 | package_installable_dir, "package.installable" |
| 415 | ) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 416 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 417 | # Create path to the dev_install_packages |
| 418 | packages_dir = os.path.join(full_sysroot_path, "packages") |
| 419 | osutils.SafeMakedirs(packages_dir) |
| 420 | for package in self.dev_install_packages: |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame^] | 421 | # Since a package has a category, such as app-arch/zip-3.0-r3, we |
| 422 | # need to create the packages_dir / category dir as needed. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 423 | category = package.split(os.sep)[0] |
| 424 | osutils.SafeMakedirs(os.path.join(packages_dir, category)) |
| 425 | package_tbz2_file = os.path.join(packages_dir, package) + ".tbz2" |
| 426 | osutils.Touch(package_tbz2_file) |
| 427 | with open( |
| 428 | package_installable_filename, "w" |
| 429 | ) as package_installable_file: |
| 430 | for package in self.dev_install_packages: |
| 431 | package_installable_file.write(package + "\n") |
| 432 | self.response = binhost_pb2.PrepareDevInstallBinhostUploadsResponse() |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 433 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 434 | def testValidateOnly(self): |
| 435 | """Check that a validate only call does not execute any logic.""" |
| 436 | patch = self.PatchObject( |
| 437 | binhost_service, "ReadDevInstallFilesToCreatePackageIndex" |
| 438 | ) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 439 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 440 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 441 | input_proto.uri = "gs://chromeos-prebuilt/target" |
| 442 | input_proto.chroot.path = self.chroot_path |
| 443 | input_proto.sysroot.path = self.sysroot_path |
| 444 | input_proto.uploads_dir = self.uploads_dir |
| 445 | binhost.PrepareDevInstallBinhostUploads( |
| 446 | input_proto, self.response, self.validate_only_config |
| 447 | ) |
| 448 | patch.assert_not_called() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 449 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 450 | def testMockCall(self): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame^] | 451 | """Test a mock call does not execute logic, returns mocked value.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 452 | patch = self.PatchObject( |
| 453 | binhost_service, "ReadDevInstallFilesToCreatePackageIndex" |
| 454 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 455 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 456 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 457 | input_proto.uri = "gs://chromeos-prebuilt/target" |
| 458 | input_proto.chroot.path = self.chroot_path |
| 459 | input_proto.sysroot.path = self.sysroot_path |
| 460 | input_proto.uploads_dir = self.uploads_dir |
| 461 | binhost.PrepareDevInstallBinhostUploads( |
| 462 | input_proto, self.response, self.mock_call_config |
| 463 | ) |
| 464 | self.assertEqual(len(self.response.upload_targets), 3) |
| 465 | self.assertEqual(self.response.upload_targets[2].path, "Packages") |
| 466 | patch.assert_not_called() |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 467 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | def testDevInstallerUpload(self): |
| 469 | """Test uploads of dev installer prebuilts.""" |
| 470 | # self.RunStage() |
| 471 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 472 | input_proto.uri = "gs://chromeos-prebuilt/target" |
| 473 | input_proto.chroot.path = self.chroot_path |
| 474 | input_proto.sysroot.path = self.sysroot_path |
| 475 | input_proto.uploads_dir = self.uploads_dir |
| 476 | # Call method under test |
| 477 | binhost.PrepareDevInstallBinhostUploads( |
| 478 | input_proto, self.response, self.api_config |
| 479 | ) |
| 480 | # Verify results |
| 481 | expected_upload_targets = [ |
| 482 | "app-arch/zip-3.0-r3.tbz2", |
| 483 | "virtual/chromium-os-printing-1-r4.tbz2", |
| 484 | "virtual/python-enum34-1.tbz2", |
| 485 | "Packages", |
| 486 | ] |
| 487 | self.assertCountEqual( |
| 488 | [ut.path for ut in self.response.upload_targets], |
| 489 | expected_upload_targets, |
| 490 | ) |
| 491 | # All of the upload_targets should also be in the uploads_directory |
| 492 | for target in self.response.upload_targets: |
| 493 | self.assertExists( |
| 494 | os.path.join(input_proto.uploads_dir, target.path) |
| 495 | ) |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 496 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 497 | def testPrepareBinhostUploadsNonGsUri(self): |
| 498 | """PrepareBinhostUploads dies when URI does not point to GS.""" |
| 499 | input_proto = binhost_pb2.PrepareDevInstallBinhostUploadsRequest() |
| 500 | input_proto.chroot.path = self.chroot_path |
| 501 | input_proto.sysroot.path = self.sysroot_path |
| 502 | input_proto.uploads_dir = self.uploads_dir |
| 503 | input_proto.uri = "https://foo.bar" |
| 504 | with self.assertRaises(ValueError): |
| 505 | binhost.PrepareDevInstallBinhostUploads( |
| 506 | input_proto, self.response, self.api_config |
| 507 | ) |