blob: 8f1a4eaa292e74e0f0c5dd396d54c190a3a5b103 [file] [log] [blame]
Tiancong Wangaf050172019-07-10 11:52:03 -07001# Copyright 2019 The Chromium OS Authors. All rights reserved.
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 Toolchain-related operations."""
6
Alex Klein231d2da2019-07-22 16:44:45 -06007from chromite.api import api_config
LaMont Jones5d2edcb2019-12-23 11:32:03 -07008from chromite.api import controller
Tiancong Wangaf050172019-07-10 11:52:03 -07009from chromite.api.controller import toolchain
LaMont Jones5d2edcb2019-12-23 11:32:03 -070010from chromite.api.gen.chromite.api import artifacts_pb2
LaMont Jones4579e8c2019-12-06 14:20:37 -070011from chromite.api.gen.chromite.api import sysroot_pb2
Tiancong Wangaf050172019-07-10 11:52:03 -070012from chromite.api.gen.chromite.api import toolchain_pb2
LaMont Jonesb20b3d92019-11-23 11:47:48 -070013from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig
LaMont Jones4579e8c2019-12-06 14:20:37 -070014from chromite.api.gen.chromiumos import common_pb2
Tiancong Wangaf050172019-07-10 11:52:03 -070015
Tiancong Wang24a3df72019-08-20 15:48:51 -070016from chromite.lib import cros_build_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070017from chromite.lib import cros_test_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070018from chromite.lib import toolchain_util
19
LaMont Jones5d2edcb2019-12-23 11:32:03 -070020# pylint: disable=protected-access
Tiancong Wangaf050172019-07-10 11:52:03 -070021
Tiancong Wangba2a1c22021-01-19 10:45:06 -080022
Tiancong Wang24a3df72019-08-20 15:48:51 -070023class UpdateEbuildWithAFDOArtifactsTest(cros_test_lib.MockTestCase,
24 api_config.ApiConfigMixin):
25 """Unittests for UpdateEbuildWithAFDOArtifacts."""
26
27 @staticmethod
28 def mock_die(message, *args):
29 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -070030
31 def setUp(self):
Tiancong Wangaf050172019-07-10 11:52:03 -070032 self.board = 'board'
Tiancong Wang24a3df72019-08-20 15:48:51 -070033 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
34 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
35 self.orderfile_command = self.PatchObject(
36 toolchain_util, 'OrderfileUpdateChromeEbuild', return_value=True)
37 self.kernel_command = self.PatchObject(
38 toolchain_util, 'AFDOUpdateKernelEbuild', return_value=True)
Tiancong Wangf9c736c2019-08-26 13:54:38 -070039 self.chrome_command = self.PatchObject(
40 toolchain_util, 'AFDOUpdateChromeEbuild', return_value=True)
Tiancong Wang24a3df72019-08-20 15:48:51 -070041 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
42
43 def _GetRequest(self, build_target=None, artifact_type=None):
44 return toolchain_pb2.VerifyAFDOArtifactsRequest(
45 build_target={'name': build_target},
46 artifact_type=artifact_type,
47 )
Tiancong Wangaf050172019-07-10 11:52:03 -070048
Alex Klein231d2da2019-07-22 16:44:45 -060049 def testValidateOnly(self):
50 """Sanity check that a validate only call does not execute any logic."""
51 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
Tiancong Wang24a3df72019-08-20 15:48:51 -070052 request = self._GetRequest(
53 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
54 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
55 self.validate_only_config)
Alex Klein231d2da2019-07-22 16:44:45 -060056 patch.assert_not_called()
57
Michael Mortensen54bd70a2019-11-21 14:45:38 -070058 def testMockCall(self):
59 """Test that a mock call does not execute logic, returns mock value."""
60 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
61 request = self._GetRequest(
62 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
63 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
64 self.mock_call_config)
65 patch.assert_not_called()
66 self.assertEqual(self.response.status, True)
67
Tiancong Wang24a3df72019-08-20 15:48:51 -070068 def testWrongArtifactType(self):
69 """Test passing wrong artifact type."""
70 request = self._GetRequest(
71 build_target=self.board, artifact_type=self.invalid_artifact_type)
72 with self.assertRaises(cros_build_lib.DieSystemExit) as context:
73 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
74 self.api_config)
75 self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type,
76 str(context.exception))
77
78 def testOrderfileSuccess(self):
79 """Test the command is called correctly with orderfile."""
80 request = self._GetRequest(
81 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
82 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
83 self.api_config)
84 self.orderfile_command.assert_called_once_with(self.board)
85 self.kernel_command.assert_not_called()
Tiancong Wangf9c736c2019-08-26 13:54:38 -070086 self.chrome_command.assert_not_called()
Tiancong Wang24a3df72019-08-20 15:48:51 -070087
88 def testKernelAFDOSuccess(self):
89 """Test the command is called correctly with kernel afdo."""
90 request = self._GetRequest(
91 build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO)
92 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
93 self.api_config)
94 self.kernel_command.assert_called_once_with(self.board)
95 self.orderfile_command.assert_not_called()
Tiancong Wangf9c736c2019-08-26 13:54:38 -070096 self.chrome_command.assert_not_called()
97
98 def testChromeAFDOSuccess(self):
99 """Test the command is called correctly with Chrome afdo."""
100 request = self._GetRequest(
101 build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO)
102 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
103 self.api_config)
104 self.chrome_command.assert_called_once_with(self.board)
105 self.orderfile_command.assert_not_called()
106 self.kernel_command.assert_not_called()
Tiancong Wangaf050172019-07-10 11:52:03 -0700107
108
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700109class UploadVettedFDOArtifactsTest(UpdateEbuildWithAFDOArtifactsTest):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700110 """Unittests for UploadVettedAFDOArtifacts."""
111
112 @staticmethod
113 def mock_die(message, *args):
114 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -0700115
116 def setUp(self):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700117 self.board = 'board'
118 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
119 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
120 self.command = self.PatchObject(
Tiancong Wangaf050172019-07-10 11:52:03 -0700121 toolchain_util,
Tiancong Wang24a3df72019-08-20 15:48:51 -0700122 'UploadAndPublishVettedAFDOArtifacts',
123 return_value=True)
124 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
Tiancong Wangaf050172019-07-10 11:52:03 -0700125
Alex Klein231d2da2019-07-22 16:44:45 -0600126 def testValidateOnly(self):
127 """Sanity check that a validate only call does not execute any logic."""
Tiancong Wang24a3df72019-08-20 15:48:51 -0700128 request = self._GetRequest(
129 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700130 toolchain.UploadVettedAFDOArtifacts(request, self.response,
131 self.validate_only_config)
Tiancong Wang24a3df72019-08-20 15:48:51 -0700132 self.command.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600133
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700134 def testMockCall(self):
135 """Test that a mock call does not execute logic, returns mock value."""
136 request = self._GetRequest(
137 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
138 toolchain.UploadVettedAFDOArtifacts(request, self.response,
139 self.mock_call_config)
140 self.command.assert_not_called()
141 self.assertEqual(self.response.status, True)
142
Tiancong Wang24a3df72019-08-20 15:48:51 -0700143 def testWrongArtifactType(self):
144 """Test passing wrong artifact type."""
145 request = self._GetRequest(
146 build_target=self.board, artifact_type=self.invalid_artifact_type)
147 with self.assertRaises(cros_build_lib.DieSystemExit) as context:
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700148 toolchain.UploadVettedAFDOArtifacts(request, self.response,
149 self.api_config)
Tiancong Wang24a3df72019-08-20 15:48:51 -0700150 self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type,
151 str(context.exception))
Alex Klein231d2da2019-07-22 16:44:45 -0600152
Tiancong Wang24a3df72019-08-20 15:48:51 -0700153 def testOrderfileSuccess(self):
154 """Test the command is called correctly with orderfile."""
155 request = self._GetRequest(
156 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
157 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
158 self.command.assert_called_once_with('orderfile', self.board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700159
Tiancong Wang24a3df72019-08-20 15:48:51 -0700160 def testKernelAFDOSuccess(self):
161 """Test the command is called correctly with kernel afdo."""
162 request = self._GetRequest(
163 build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO)
164 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
165 self.command.assert_called_once_with('kernel_afdo', self.board)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700166
167 def testChromeAFDOSuccess(self):
168 """Test the command is called correctly with Chrome afdo."""
169 request = self._GetRequest(
170 build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO)
171 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
172 self.command.assert_called_once_with('chrome_afdo', self.board)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700173
174
LaMont Jones4579e8c2019-12-06 14:20:37 -0700175class PrepareForBuildTest(cros_test_lib.MockTempDirTestCase,
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700176 api_config.ApiConfigMixin):
177 """Unittests for PrepareForBuild."""
178
179 def setUp(self):
180 self.response = toolchain_pb2.PrepareForToolchainBuildResponse()
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700181 self.prep = self.PatchObject(
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800182 toolchain_util,
183 'PrepareForBuild',
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700184 return_value=toolchain_util.PrepareForBuildReturn.NEEDED)
185 self.bundle = self.PatchObject(
186 toolchain_util, 'BundleArtifacts', return_value=[])
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800187 self.PatchObject(
188 toolchain, '_TOOLCHAIN_ARTIFACT_HANDLERS', {
189 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE:
190 toolchain._Handlers('UnverifiedChromeLlvmOrderfile', self.prep,
191 self.bundle),
192 })
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700193
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800194 def _GetRequest(self,
195 artifact_types=None,
196 input_artifacts=None,
197 additional_args=None):
LaMont Jones4579e8c2019-12-06 14:20:37 -0700198 chroot = common_pb2.Chroot(path=self.tempdir)
199 sysroot = sysroot_pb2.Sysroot(
200 path='/build/board', build_target=common_pb2.BuildTarget(name='board'))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700201 return toolchain_pb2.PrepareForToolchainBuildRequest(
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800202 artifact_types=artifact_types,
203 chroot=chroot,
204 sysroot=sysroot,
205 input_artifacts=input_artifacts,
206 additional_args=additional_args)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700207
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700208 def testRaisesForUnknown(self):
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700209 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800210 self.assertRaises(KeyError, toolchain.PrepareForBuild, request,
211 self.response, self.api_config)
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700212
213 def testAcceptsNone(self):
214 request = toolchain_pb2.PrepareForToolchainBuildRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700215 artifact_types=[
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800216 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE
217 ],
218 chroot=None,
219 sysroot=None)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700220 toolchain.PrepareForBuild(request, self.response, self.api_config)
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800221 self.prep.assert_called_once_with('UnverifiedChromeLlvmOrderfile', None, '',
222 '', {}, {})
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700223
224 def testHandlesUnknownInputArtifacts(self):
225 request = toolchain_pb2.PrepareForToolchainBuildRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700226 artifact_types=[
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800227 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE
228 ],
229 chroot=None,
230 sysroot=None,
231 input_artifacts=[
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700232 BuilderConfig.Artifacts.InputArtifactInfo(
233 input_artifact_type=BuilderConfig.Artifacts.IMAGE_ZIP,
234 input_artifact_gs_locations=['path1']),
235 ])
236 toolchain.PrepareForBuild(request, self.response, self.api_config)
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800237 self.prep.assert_called_once_with('UnverifiedChromeLlvmOrderfile', None, '',
238 '', {}, {})
LaMont Jones45ca6c42020-02-05 09:39:09 -0700239
LaMont Jonese7821672020-04-09 08:56:26 -0600240 def testPassesProfileInfo(self):
LaMont Jones45ca6c42020-02-05 09:39:09 -0700241 request = toolchain_pb2.PrepareForToolchainBuildRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700242 artifact_types=[
243 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE],
LaMont Jones45ca6c42020-02-05 09:39:09 -0700244 chroot=None, sysroot=None, input_artifacts=[
245 BuilderConfig.Artifacts.InputArtifactInfo(
Mike Frysingerfcca49e2021-03-17 01:09:20 -0400246 input_artifact_type=
LaMont Jonescd1503d2020-03-04 09:09:59 -0700247 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
LaMont Jones45ca6c42020-02-05 09:39:09 -0700248 input_artifact_gs_locations=['path1', 'path2']),
249 BuilderConfig.Artifacts.InputArtifactInfo(
Mike Frysingerfcca49e2021-03-17 01:09:20 -0400250 input_artifact_type=
LaMont Jonescd1503d2020-03-04 09:09:59 -0700251 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
LaMont Jones45ca6c42020-02-05 09:39:09 -0700252 input_artifact_gs_locations=['path3']),
253 ],
LaMont Jonese7821672020-04-09 08:56:26 -0600254 profile_info=common_pb2.ArtifactProfileInfo(
LaMont Jones45ca6c42020-02-05 09:39:09 -0700255 chrome_cwp_profile='CWPVERSION'),
256 )
257 toolchain.PrepareForBuild(request, self.response, self.api_config)
258 self.prep.assert_called_once_with(
LaMont Jonesd3944582020-03-04 10:37:05 -0700259 'UnverifiedChromeLlvmOrderfile', None, '', '', {
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800260 'UnverifiedChromeLlvmOrderfile':
261 ['gs://path1', 'gs://path2', 'gs://path3'],
262 }, {'chrome_cwp_profile': 'CWPVERSION'})
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700263
LaMont Jonese7821672020-04-09 08:56:26 -0600264 def testPassesProfileInfoAfdoRelease(self):
265 request = toolchain_pb2.PrepareForToolchainBuildRequest(
266 artifact_types=[
267 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE],
268 chroot=None, sysroot=None, input_artifacts=[
269 BuilderConfig.Artifacts.InputArtifactInfo(
Mike Frysingerfcca49e2021-03-17 01:09:20 -0400270 input_artifact_type=
LaMont Jonese7821672020-04-09 08:56:26 -0600271 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
272 input_artifact_gs_locations=['path1', 'path2']),
273 BuilderConfig.Artifacts.InputArtifactInfo(
Mike Frysingerfcca49e2021-03-17 01:09:20 -0400274 input_artifact_type=
LaMont Jonese7821672020-04-09 08:56:26 -0600275 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
276 input_artifact_gs_locations=['path3']),
277 ],
278 profile_info=common_pb2.ArtifactProfileInfo(
279 afdo_release=common_pb2.AfdoRelease(
280 chrome_cwp_profile='CWPVERSION',
281 image_build_id=1234)),
282 )
283 toolchain.PrepareForBuild(request, self.response, self.api_config)
284 self.prep.assert_called_once_with(
285 'UnverifiedChromeLlvmOrderfile', None, '', '', {
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800286 'UnverifiedChromeLlvmOrderfile':
287 ['gs://path1', 'gs://path2', 'gs://path3'],
288 }, {
289 'chrome_cwp_profile': 'CWPVERSION',
290 'image_build_id': 1234
291 })
LaMont Jonese7821672020-04-09 08:56:26 -0600292
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700293 def testHandlesDuplicateInputArtifacts(self):
294 request = toolchain_pb2.PrepareForToolchainBuildRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700295 artifact_types=[
296 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE],
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700297 chroot=None, sysroot=None, input_artifacts=[
298 BuilderConfig.Artifacts.InputArtifactInfo(
Mike Frysingerfcca49e2021-03-17 01:09:20 -0400299 input_artifact_type=
LaMont Jonescd1503d2020-03-04 09:09:59 -0700300 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700301 input_artifact_gs_locations=['path1', 'path2']),
302 BuilderConfig.Artifacts.InputArtifactInfo(
Mike Frysingerfcca49e2021-03-17 01:09:20 -0400303 input_artifact_type=
LaMont Jonescd1503d2020-03-04 09:09:59 -0700304 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700305 input_artifact_gs_locations=['path3']),
306 ])
307 toolchain.PrepareForBuild(request, self.response, self.api_config)
308 self.prep.assert_called_once_with(
LaMont Jonesd3944582020-03-04 10:37:05 -0700309 'UnverifiedChromeLlvmOrderfile', None, '', '', {
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800310 'UnverifiedChromeLlvmOrderfile':
311 ['gs://path1', 'gs://path2', 'gs://path3'],
LaMont Jonesd3944582020-03-04 10:37:05 -0700312 }, {})
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700313
314
315class BundleToolchainTest(cros_test_lib.MockTempDirTestCase,
316 api_config.ApiConfigMixin):
317 """Unittests for BundleToolchain."""
318
319 def setUp(self):
320 self.response = toolchain_pb2.BundleToolchainResponse()
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700321 self.prep = self.PatchObject(
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800322 toolchain_util,
323 'PrepareForBuild',
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700324 return_value=toolchain_util.PrepareForBuildReturn.NEEDED)
325 self.bundle = self.PatchObject(
326 toolchain_util, 'BundleArtifacts', return_value=[])
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800327 self.PatchObject(
328 toolchain, '_TOOLCHAIN_ARTIFACT_HANDLERS', {
329 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE:
330 toolchain._Handlers('UnverifiedChromeLlvmOrderfile', self.prep,
331 self.bundle),
332 })
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700333
334 def _GetRequest(self, artifact_types=None):
LaMont Jones4579e8c2019-12-06 14:20:37 -0700335 chroot = common_pb2.Chroot(path=self.tempdir)
336 sysroot = sysroot_pb2.Sysroot(
337 path='/build/board', build_target=common_pb2.BuildTarget(name='board'))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700338 return toolchain_pb2.BundleToolchainRequest(
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800339 chroot=chroot,
340 sysroot=sysroot,
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700341 output_dir=self.tempdir,
LaMont Jones4579e8c2019-12-06 14:20:37 -0700342 artifact_types=artifact_types,
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700343 )
344
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700345 def testRaisesForUnknown(self):
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700346 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700347 self.assertEqual(
348 controller.RETURN_CODE_UNRECOVERABLE,
349 toolchain.BundleArtifacts(request, self.response, self.api_config))
Michael Mortensen3232ab32020-01-05 19:14:36 -0700350
351 def testValidateOnly(self):
352 """Sanity check that a validate only call does not execute any logic."""
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700353 request = self._GetRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700354 [BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE])
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800355 toolchain.BundleArtifacts(request, self.response, self.validate_only_config)
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700356 self.bundle.assert_not_called()
357
358 def testSetsArtifactsInfo(self):
359 request = self._GetRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700360 [BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE])
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700361 self.bundle.return_value = ['artifact.xz']
362 toolchain.BundleArtifacts(request, self.response, self.api_config)
363 self.assertEqual(1, len(self.response.artifacts_info))
364 self.assertEqual(
365 self.response.artifacts_info[0],
366 toolchain_pb2.ArtifactInfo(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700367 artifact_type=(
368 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE),
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700369 artifacts=[
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800370 artifacts_pb2.Artifact(path=self.bundle.return_value[0])
371 ]))
Tiancong Wangd5214132021-01-12 10:43:57 -0800372
373
374class GetUpdatedFilesTest(cros_test_lib.MockTempDirTestCase,
375 api_config.ApiConfigMixin):
376 """Unittests for GetUpdatedFiles."""
377
378 def setUp(self):
379 self.response = toolchain_pb2.GetUpdatedFilesResponse()
380 self.artifact_path = '/any/path/to/metadata'
381 self.profile_info = common_pb2.ArtifactProfileInfo(kernel_version='4.4')
382 self.update = self.PatchObject(
383 toolchain_util, 'GetUpdatedFiles', return_value=([], ''))
384
385 def _GetRequest(self, uploaded_artifacts):
386 uploaded = []
387 for artifact_type, artifact_path, profile_info in uploaded_artifacts:
388 uploaded.append(
389 toolchain_pb2.GetUpdatedFilesRequest.UploadedArtifacts(
390 artifact_info=toolchain_pb2.ArtifactInfo(
391 artifact_type=artifact_type,
392 artifacts=[artifacts_pb2.Artifact(path=artifact_path)]),
393 profile_info=profile_info))
394 return toolchain_pb2.GetUpdatedFilesRequest(uploaded_artifacts=uploaded)
395
396 def testRaisesForUnknown(self):
397 request = self._GetRequest([
398 (BuilderConfig.Artifacts.UNVERIFIED_KERNEL_CWP_AFDO_FILE,
399 self.artifact_path, self.profile_info)
400 ])
401 self.assertEqual(
402 controller.RETURN_CODE_UNRECOVERABLE,
403 toolchain.GetUpdatedFiles(request, self.response, self.api_config))
404
405 def testValidateOnly(self):
406 """Sanity check that a validate only call does not execute any logic."""
407 request = self._GetRequest([
408 (BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE,
409 self.artifact_path, self.profile_info)
410 ])
411 toolchain.GetUpdatedFiles(request, self.response, self.validate_only_config)
412
413 def testUpdateSuccess(self):
414 updated_file = '/path/to/updated_file'
415 self.update.return_value = ([updated_file], 'Commit Message')
416 request = self._GetRequest([
417 (BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE,
418 self.artifact_path, self.profile_info)
419 ])
420 toolchain.GetUpdatedFiles(request, self.response, self.api_config)
421 print(self.response.updated_files)
422 self.assertEqual(len(self.response.updated_files), 1)
423 self.assertEqual(
424 self.response.updated_files[0],
425 toolchain_pb2.GetUpdatedFilesResponse.UpdatedFile(path=updated_file),
426 )
427 self.assertIn('Commit Message', self.response.commit_message)
428 self.assertEqual(len(self.response.commit_footer), 0)