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