blob: b8d61ef2d2b90ee1bb02c9e010578153e7a2e970 [file] [log] [blame]
Tiancong Wangaf050172019-07-10 11:52:03 -07001# -*- coding: utf-8 -*-
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unittests for Toolchain-related operations."""
7
8from __future__ import print_function
9
Alex Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import api_config
LaMont Jones5d2edcb2019-12-23 11:32:03 -070011from chromite.api import controller
Tiancong Wangaf050172019-07-10 11:52:03 -070012from chromite.api.controller import toolchain
LaMont Jones5d2edcb2019-12-23 11:32:03 -070013from chromite.api.gen.chromite.api import artifacts_pb2
LaMont Jones4579e8c2019-12-06 14:20:37 -070014from chromite.api.gen.chromite.api import sysroot_pb2
Tiancong Wangaf050172019-07-10 11:52:03 -070015from chromite.api.gen.chromite.api import toolchain_pb2
LaMont Jonesb20b3d92019-11-23 11:47:48 -070016from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig
LaMont Jones4579e8c2019-12-06 14:20:37 -070017from chromite.api.gen.chromiumos import common_pb2
Tiancong Wangaf050172019-07-10 11:52:03 -070018
Tiancong Wang24a3df72019-08-20 15:48:51 -070019from chromite.lib import cros_build_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070020from chromite.lib import cros_test_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070021from chromite.lib import toolchain_util
22
LaMont Jones5d2edcb2019-12-23 11:32:03 -070023# pylint: disable=protected-access
Tiancong Wangaf050172019-07-10 11:52:03 -070024
Tiancong Wangba2a1c22021-01-19 10:45:06 -080025
Tiancong Wang24a3df72019-08-20 15:48:51 -070026class UpdateEbuildWithAFDOArtifactsTest(cros_test_lib.MockTestCase,
27 api_config.ApiConfigMixin):
28 """Unittests for UpdateEbuildWithAFDOArtifacts."""
29
30 @staticmethod
31 def mock_die(message, *args):
32 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -070033
34 def setUp(self):
Tiancong Wangaf050172019-07-10 11:52:03 -070035 self.board = 'board'
Tiancong Wang24a3df72019-08-20 15:48:51 -070036 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
37 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
38 self.orderfile_command = self.PatchObject(
39 toolchain_util, 'OrderfileUpdateChromeEbuild', return_value=True)
40 self.kernel_command = self.PatchObject(
41 toolchain_util, 'AFDOUpdateKernelEbuild', return_value=True)
Tiancong Wangf9c736c2019-08-26 13:54:38 -070042 self.chrome_command = self.PatchObject(
43 toolchain_util, 'AFDOUpdateChromeEbuild', return_value=True)
Tiancong Wang24a3df72019-08-20 15:48:51 -070044 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
45
46 def _GetRequest(self, build_target=None, artifact_type=None):
47 return toolchain_pb2.VerifyAFDOArtifactsRequest(
48 build_target={'name': build_target},
49 artifact_type=artifact_type,
50 )
Tiancong Wangaf050172019-07-10 11:52:03 -070051
Alex Klein231d2da2019-07-22 16:44:45 -060052 def testValidateOnly(self):
53 """Sanity check that a validate only call does not execute any logic."""
54 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
Tiancong Wang24a3df72019-08-20 15:48:51 -070055 request = self._GetRequest(
56 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
57 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
58 self.validate_only_config)
Alex Klein231d2da2019-07-22 16:44:45 -060059 patch.assert_not_called()
60
Michael Mortensen54bd70a2019-11-21 14:45:38 -070061 def testMockCall(self):
62 """Test that a mock call does not execute logic, returns mock value."""
63 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
64 request = self._GetRequest(
65 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
66 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
67 self.mock_call_config)
68 patch.assert_not_called()
69 self.assertEqual(self.response.status, True)
70
Tiancong Wang24a3df72019-08-20 15:48:51 -070071 def testWrongArtifactType(self):
72 """Test passing wrong artifact type."""
73 request = self._GetRequest(
74 build_target=self.board, artifact_type=self.invalid_artifact_type)
75 with self.assertRaises(cros_build_lib.DieSystemExit) as context:
76 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
77 self.api_config)
78 self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type,
79 str(context.exception))
80
81 def testOrderfileSuccess(self):
82 """Test the command is called correctly with orderfile."""
83 request = self._GetRequest(
84 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
85 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
86 self.api_config)
87 self.orderfile_command.assert_called_once_with(self.board)
88 self.kernel_command.assert_not_called()
Tiancong Wangf9c736c2019-08-26 13:54:38 -070089 self.chrome_command.assert_not_called()
Tiancong Wang24a3df72019-08-20 15:48:51 -070090
91 def testKernelAFDOSuccess(self):
92 """Test the command is called correctly with kernel afdo."""
93 request = self._GetRequest(
94 build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO)
95 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
96 self.api_config)
97 self.kernel_command.assert_called_once_with(self.board)
98 self.orderfile_command.assert_not_called()
Tiancong Wangf9c736c2019-08-26 13:54:38 -070099 self.chrome_command.assert_not_called()
100
101 def testChromeAFDOSuccess(self):
102 """Test the command is called correctly with Chrome afdo."""
103 request = self._GetRequest(
104 build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO)
105 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
106 self.api_config)
107 self.chrome_command.assert_called_once_with(self.board)
108 self.orderfile_command.assert_not_called()
109 self.kernel_command.assert_not_called()
Tiancong Wangaf050172019-07-10 11:52:03 -0700110
111
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700112class UploadVettedFDOArtifactsTest(UpdateEbuildWithAFDOArtifactsTest):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700113 """Unittests for UploadVettedAFDOArtifacts."""
114
115 @staticmethod
116 def mock_die(message, *args):
117 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -0700118
119 def setUp(self):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700120 self.board = 'board'
121 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
122 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
123 self.command = self.PatchObject(
Tiancong Wangaf050172019-07-10 11:52:03 -0700124 toolchain_util,
Tiancong Wang24a3df72019-08-20 15:48:51 -0700125 'UploadAndPublishVettedAFDOArtifacts',
126 return_value=True)
127 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
Tiancong Wangaf050172019-07-10 11:52:03 -0700128
Alex Klein231d2da2019-07-22 16:44:45 -0600129 def testValidateOnly(self):
130 """Sanity check that a validate only call does not execute any logic."""
Tiancong Wang24a3df72019-08-20 15:48:51 -0700131 request = self._GetRequest(
132 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700133 toolchain.UploadVettedAFDOArtifacts(request, self.response,
134 self.validate_only_config)
Tiancong Wang24a3df72019-08-20 15:48:51 -0700135 self.command.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600136
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700137 def testMockCall(self):
138 """Test that a mock call does not execute logic, returns mock value."""
139 request = self._GetRequest(
140 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
141 toolchain.UploadVettedAFDOArtifacts(request, self.response,
142 self.mock_call_config)
143 self.command.assert_not_called()
144 self.assertEqual(self.response.status, True)
145
Tiancong Wang24a3df72019-08-20 15:48:51 -0700146 def testWrongArtifactType(self):
147 """Test passing wrong artifact type."""
148 request = self._GetRequest(
149 build_target=self.board, artifact_type=self.invalid_artifact_type)
150 with self.assertRaises(cros_build_lib.DieSystemExit) as context:
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700151 toolchain.UploadVettedAFDOArtifacts(request, self.response,
152 self.api_config)
Tiancong Wang24a3df72019-08-20 15:48:51 -0700153 self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type,
154 str(context.exception))
Alex Klein231d2da2019-07-22 16:44:45 -0600155
Tiancong Wang24a3df72019-08-20 15:48:51 -0700156 def testOrderfileSuccess(self):
157 """Test the command is called correctly with orderfile."""
158 request = self._GetRequest(
159 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
160 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
161 self.command.assert_called_once_with('orderfile', self.board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700162
Tiancong Wang24a3df72019-08-20 15:48:51 -0700163 def testKernelAFDOSuccess(self):
164 """Test the command is called correctly with kernel afdo."""
165 request = self._GetRequest(
166 build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO)
167 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
168 self.command.assert_called_once_with('kernel_afdo', self.board)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700169
170 def testChromeAFDOSuccess(self):
171 """Test the command is called correctly with Chrome afdo."""
172 request = self._GetRequest(
173 build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO)
174 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
175 self.command.assert_called_once_with('chrome_afdo', self.board)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700176
177
LaMont Jones4579e8c2019-12-06 14:20:37 -0700178class PrepareForBuildTest(cros_test_lib.MockTempDirTestCase,
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700179 api_config.ApiConfigMixin):
180 """Unittests for PrepareForBuild."""
181
182 def setUp(self):
183 self.response = toolchain_pb2.PrepareForToolchainBuildResponse()
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700184 self.prep = self.PatchObject(
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800185 toolchain_util,
186 'PrepareForBuild',
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700187 return_value=toolchain_util.PrepareForBuildReturn.NEEDED)
188 self.bundle = self.PatchObject(
189 toolchain_util, 'BundleArtifacts', return_value=[])
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800190 self.PatchObject(
191 toolchain, '_TOOLCHAIN_ARTIFACT_HANDLERS', {
192 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE:
193 toolchain._Handlers('UnverifiedChromeLlvmOrderfile', self.prep,
194 self.bundle),
195 })
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700196
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800197 def _GetRequest(self,
198 artifact_types=None,
199 input_artifacts=None,
200 additional_args=None):
LaMont Jones4579e8c2019-12-06 14:20:37 -0700201 chroot = common_pb2.Chroot(path=self.tempdir)
202 sysroot = sysroot_pb2.Sysroot(
203 path='/build/board', build_target=common_pb2.BuildTarget(name='board'))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700204 return toolchain_pb2.PrepareForToolchainBuildRequest(
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800205 artifact_types=artifact_types,
206 chroot=chroot,
207 sysroot=sysroot,
208 input_artifacts=input_artifacts,
209 additional_args=additional_args)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700210
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700211 def testRaisesForUnknown(self):
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700212 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800213 self.assertRaises(KeyError, toolchain.PrepareForBuild, request,
214 self.response, self.api_config)
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700215
216 def testAcceptsNone(self):
217 request = toolchain_pb2.PrepareForToolchainBuildRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700218 artifact_types=[
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800219 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE
220 ],
221 chroot=None,
222 sysroot=None)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700223 toolchain.PrepareForBuild(request, self.response, self.api_config)
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800224 self.prep.assert_called_once_with('UnverifiedChromeLlvmOrderfile', None, '',
225 '', {}, {})
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700226
227 def testHandlesUnknownInputArtifacts(self):
228 request = toolchain_pb2.PrepareForToolchainBuildRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700229 artifact_types=[
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800230 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE
231 ],
232 chroot=None,
233 sysroot=None,
234 input_artifacts=[
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700235 BuilderConfig.Artifacts.InputArtifactInfo(
236 input_artifact_type=BuilderConfig.Artifacts.IMAGE_ZIP,
237 input_artifact_gs_locations=['path1']),
238 ])
239 toolchain.PrepareForBuild(request, self.response, self.api_config)
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800240 self.prep.assert_called_once_with('UnverifiedChromeLlvmOrderfile', None, '',
241 '', {}, {})
LaMont Jones45ca6c42020-02-05 09:39:09 -0700242
LaMont Jonese7821672020-04-09 08:56:26 -0600243 def testPassesProfileInfo(self):
LaMont Jones45ca6c42020-02-05 09:39:09 -0700244 request = toolchain_pb2.PrepareForToolchainBuildRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700245 artifact_types=[
246 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE],
LaMont Jones45ca6c42020-02-05 09:39:09 -0700247 chroot=None, sysroot=None, input_artifacts=[
248 BuilderConfig.Artifacts.InputArtifactInfo(
249 input_artifact_type=\
LaMont Jonescd1503d2020-03-04 09:09:59 -0700250 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
LaMont Jones45ca6c42020-02-05 09:39:09 -0700251 input_artifact_gs_locations=['path1', 'path2']),
252 BuilderConfig.Artifacts.InputArtifactInfo(
253 input_artifact_type=\
LaMont Jonescd1503d2020-03-04 09:09:59 -0700254 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
LaMont Jones45ca6c42020-02-05 09:39:09 -0700255 input_artifact_gs_locations=['path3']),
256 ],
LaMont Jonese7821672020-04-09 08:56:26 -0600257 profile_info=common_pb2.ArtifactProfileInfo(
LaMont Jones45ca6c42020-02-05 09:39:09 -0700258 chrome_cwp_profile='CWPVERSION'),
259 )
260 toolchain.PrepareForBuild(request, self.response, self.api_config)
261 self.prep.assert_called_once_with(
LaMont Jonesd3944582020-03-04 10:37:05 -0700262 'UnverifiedChromeLlvmOrderfile', None, '', '', {
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800263 'UnverifiedChromeLlvmOrderfile':
264 ['gs://path1', 'gs://path2', 'gs://path3'],
265 }, {'chrome_cwp_profile': 'CWPVERSION'})
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700266
LaMont Jonese7821672020-04-09 08:56:26 -0600267 def testPassesProfileInfoAfdoRelease(self):
268 request = toolchain_pb2.PrepareForToolchainBuildRequest(
269 artifact_types=[
270 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE],
271 chroot=None, sysroot=None, input_artifacts=[
272 BuilderConfig.Artifacts.InputArtifactInfo(
273 input_artifact_type=\
274 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
275 input_artifact_gs_locations=['path1', 'path2']),
276 BuilderConfig.Artifacts.InputArtifactInfo(
277 input_artifact_type=\
278 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
279 input_artifact_gs_locations=['path3']),
280 ],
281 profile_info=common_pb2.ArtifactProfileInfo(
282 afdo_release=common_pb2.AfdoRelease(
283 chrome_cwp_profile='CWPVERSION',
284 image_build_id=1234)),
285 )
286 toolchain.PrepareForBuild(request, self.response, self.api_config)
287 self.prep.assert_called_once_with(
288 'UnverifiedChromeLlvmOrderfile', None, '', '', {
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800289 'UnverifiedChromeLlvmOrderfile':
290 ['gs://path1', 'gs://path2', 'gs://path3'],
291 }, {
292 'chrome_cwp_profile': 'CWPVERSION',
293 'image_build_id': 1234
294 })
LaMont Jonese7821672020-04-09 08:56:26 -0600295
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700296 def testHandlesDuplicateInputArtifacts(self):
297 request = toolchain_pb2.PrepareForToolchainBuildRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700298 artifact_types=[
299 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE],
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700300 chroot=None, sysroot=None, input_artifacts=[
301 BuilderConfig.Artifacts.InputArtifactInfo(
302 input_artifact_type=\
LaMont Jonescd1503d2020-03-04 09:09:59 -0700303 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700304 input_artifact_gs_locations=['path1', 'path2']),
305 BuilderConfig.Artifacts.InputArtifactInfo(
306 input_artifact_type=\
LaMont Jonescd1503d2020-03-04 09:09:59 -0700307 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE,
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700308 input_artifact_gs_locations=['path3']),
309 ])
310 toolchain.PrepareForBuild(request, self.response, self.api_config)
311 self.prep.assert_called_once_with(
LaMont Jonesd3944582020-03-04 10:37:05 -0700312 'UnverifiedChromeLlvmOrderfile', None, '', '', {
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800313 'UnverifiedChromeLlvmOrderfile':
314 ['gs://path1', 'gs://path2', 'gs://path3'],
LaMont Jonesd3944582020-03-04 10:37:05 -0700315 }, {})
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700316
317
318class BundleToolchainTest(cros_test_lib.MockTempDirTestCase,
319 api_config.ApiConfigMixin):
320 """Unittests for BundleToolchain."""
321
322 def setUp(self):
323 self.response = toolchain_pb2.BundleToolchainResponse()
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700324 self.prep = self.PatchObject(
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800325 toolchain_util,
326 'PrepareForBuild',
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700327 return_value=toolchain_util.PrepareForBuildReturn.NEEDED)
328 self.bundle = self.PatchObject(
329 toolchain_util, 'BundleArtifacts', return_value=[])
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800330 self.PatchObject(
331 toolchain, '_TOOLCHAIN_ARTIFACT_HANDLERS', {
332 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE:
333 toolchain._Handlers('UnverifiedChromeLlvmOrderfile', self.prep,
334 self.bundle),
335 })
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700336
337 def _GetRequest(self, artifact_types=None):
LaMont Jones4579e8c2019-12-06 14:20:37 -0700338 chroot = common_pb2.Chroot(path=self.tempdir)
339 sysroot = sysroot_pb2.Sysroot(
340 path='/build/board', build_target=common_pb2.BuildTarget(name='board'))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700341 return toolchain_pb2.BundleToolchainRequest(
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800342 chroot=chroot,
343 sysroot=sysroot,
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700344 output_dir=self.tempdir,
LaMont Jones4579e8c2019-12-06 14:20:37 -0700345 artifact_types=artifact_types,
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700346 )
347
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700348 def testRaisesForUnknown(self):
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700349 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700350 self.assertEqual(
351 controller.RETURN_CODE_UNRECOVERABLE,
352 toolchain.BundleArtifacts(request, self.response, self.api_config))
Michael Mortensen3232ab32020-01-05 19:14:36 -0700353
354 def testValidateOnly(self):
355 """Sanity check that a validate only call does not execute any logic."""
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700356 request = self._GetRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700357 [BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE])
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800358 toolchain.BundleArtifacts(request, self.response, self.validate_only_config)
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700359 self.bundle.assert_not_called()
360
361 def testSetsArtifactsInfo(self):
362 request = self._GetRequest(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700363 [BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE])
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700364 self.bundle.return_value = ['artifact.xz']
365 toolchain.BundleArtifacts(request, self.response, self.api_config)
366 self.assertEqual(1, len(self.response.artifacts_info))
367 self.assertEqual(
368 self.response.artifacts_info[0],
369 toolchain_pb2.ArtifactInfo(
LaMont Jonescd1503d2020-03-04 09:09:59 -0700370 artifact_type=(
371 BuilderConfig.Artifacts.UNVERIFIED_CHROME_LLVM_ORDERFILE),
LaMont Jones5d2edcb2019-12-23 11:32:03 -0700372 artifacts=[
Tiancong Wangba2a1c22021-01-19 10:45:06 -0800373 artifacts_pb2.Artifact(path=self.bundle.return_value[0])
374 ]))
Tiancong Wangd5214132021-01-12 10:43:57 -0800375
376
377class GetUpdatedFilesTest(cros_test_lib.MockTempDirTestCase,
378 api_config.ApiConfigMixin):
379 """Unittests for GetUpdatedFiles."""
380
381 def setUp(self):
382 self.response = toolchain_pb2.GetUpdatedFilesResponse()
383 self.artifact_path = '/any/path/to/metadata'
384 self.profile_info = common_pb2.ArtifactProfileInfo(kernel_version='4.4')
385 self.update = self.PatchObject(
386 toolchain_util, 'GetUpdatedFiles', return_value=([], ''))
387
388 def _GetRequest(self, uploaded_artifacts):
389 uploaded = []
390 for artifact_type, artifact_path, profile_info in uploaded_artifacts:
391 uploaded.append(
392 toolchain_pb2.GetUpdatedFilesRequest.UploadedArtifacts(
393 artifact_info=toolchain_pb2.ArtifactInfo(
394 artifact_type=artifact_type,
395 artifacts=[artifacts_pb2.Artifact(path=artifact_path)]),
396 profile_info=profile_info))
397 return toolchain_pb2.GetUpdatedFilesRequest(uploaded_artifacts=uploaded)
398
399 def testRaisesForUnknown(self):
400 request = self._GetRequest([
401 (BuilderConfig.Artifacts.UNVERIFIED_KERNEL_CWP_AFDO_FILE,
402 self.artifact_path, self.profile_info)
403 ])
404 self.assertEqual(
405 controller.RETURN_CODE_UNRECOVERABLE,
406 toolchain.GetUpdatedFiles(request, self.response, self.api_config))
407
408 def testValidateOnly(self):
409 """Sanity check that a validate only call does not execute any logic."""
410 request = self._GetRequest([
411 (BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE,
412 self.artifact_path, self.profile_info)
413 ])
414 toolchain.GetUpdatedFiles(request, self.response, self.validate_only_config)
415
416 def testUpdateSuccess(self):
417 updated_file = '/path/to/updated_file'
418 self.update.return_value = ([updated_file], 'Commit Message')
419 request = self._GetRequest([
420 (BuilderConfig.Artifacts.VERIFIED_KERNEL_CWP_AFDO_FILE,
421 self.artifact_path, self.profile_info)
422 ])
423 toolchain.GetUpdatedFiles(request, self.response, self.api_config)
424 print(self.response.updated_files)
425 self.assertEqual(len(self.response.updated_files), 1)
426 self.assertEqual(
427 self.response.updated_files[0],
428 toolchain_pb2.GetUpdatedFilesResponse.UpdatedFile(path=updated_file),
429 )
430 self.assertIn('Commit Message', self.response.commit_message)
431 self.assertEqual(len(self.response.commit_footer), 0)