blob: 675b6460ba20a5681fbf33505ea78fad1c5d2643 [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
Tiancong Wangaf050172019-07-10 11:52:03 -070011from chromite.api.controller import toolchain
LaMont Jones4579e8c2019-12-06 14:20:37 -070012from chromite.api.gen.chromite.api import sysroot_pb2
Tiancong Wangaf050172019-07-10 11:52:03 -070013from chromite.api.gen.chromite.api import toolchain_pb2
LaMont Jonesb20b3d92019-11-23 11:47:48 -070014from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig
LaMont Jones4579e8c2019-12-06 14:20:37 -070015from chromite.api.gen.chromiumos import common_pb2
Tiancong Wangaf050172019-07-10 11:52:03 -070016
Tiancong Wang24a3df72019-08-20 15:48:51 -070017from chromite.lib import cros_build_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070018from chromite.lib import cros_test_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070019from chromite.lib import toolchain_util
20
21
Tiancong Wang24a3df72019-08-20 15:48:51 -070022class UpdateEbuildWithAFDOArtifactsTest(cros_test_lib.MockTestCase,
23 api_config.ApiConfigMixin):
24 """Unittests for UpdateEbuildWithAFDOArtifacts."""
25
26 @staticmethod
27 def mock_die(message, *args):
28 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -070029
30 def setUp(self):
Tiancong Wangaf050172019-07-10 11:52:03 -070031 self.board = 'board'
Tiancong Wang24a3df72019-08-20 15:48:51 -070032 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
33 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
34 self.orderfile_command = self.PatchObject(
35 toolchain_util, 'OrderfileUpdateChromeEbuild', return_value=True)
36 self.kernel_command = self.PatchObject(
37 toolchain_util, 'AFDOUpdateKernelEbuild', return_value=True)
Tiancong Wangf9c736c2019-08-26 13:54:38 -070038 self.chrome_command = self.PatchObject(
39 toolchain_util, 'AFDOUpdateChromeEbuild', return_value=True)
Tiancong Wang24a3df72019-08-20 15:48:51 -070040 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
41
42 def _GetRequest(self, build_target=None, artifact_type=None):
43 return toolchain_pb2.VerifyAFDOArtifactsRequest(
44 build_target={'name': build_target},
45 artifact_type=artifact_type,
46 )
Tiancong Wangaf050172019-07-10 11:52:03 -070047
Alex Klein231d2da2019-07-22 16:44:45 -060048 def testValidateOnly(self):
49 """Sanity check that a validate only call does not execute any logic."""
50 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
Tiancong Wang24a3df72019-08-20 15:48:51 -070051 request = self._GetRequest(
52 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
53 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
54 self.validate_only_config)
Alex Klein231d2da2019-07-22 16:44:45 -060055 patch.assert_not_called()
56
Michael Mortensen54bd70a2019-11-21 14:45:38 -070057 def testMockCall(self):
58 """Test that a mock call does not execute logic, returns mock value."""
59 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
60 request = self._GetRequest(
61 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
62 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
63 self.mock_call_config)
64 patch.assert_not_called()
65 self.assertEqual(self.response.status, True)
66
Tiancong Wang24a3df72019-08-20 15:48:51 -070067 def testWrongArtifactType(self):
68 """Test passing wrong artifact type."""
69 request = self._GetRequest(
70 build_target=self.board, artifact_type=self.invalid_artifact_type)
71 with self.assertRaises(cros_build_lib.DieSystemExit) as context:
72 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
73 self.api_config)
74 self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type,
75 str(context.exception))
76
77 def testOrderfileSuccess(self):
78 """Test the command is called correctly with orderfile."""
79 request = self._GetRequest(
80 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
81 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
82 self.api_config)
83 self.orderfile_command.assert_called_once_with(self.board)
84 self.kernel_command.assert_not_called()
Tiancong Wangf9c736c2019-08-26 13:54:38 -070085 self.chrome_command.assert_not_called()
Tiancong Wang24a3df72019-08-20 15:48:51 -070086
87 def testKernelAFDOSuccess(self):
88 """Test the command is called correctly with kernel afdo."""
89 request = self._GetRequest(
90 build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO)
91 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
92 self.api_config)
93 self.kernel_command.assert_called_once_with(self.board)
94 self.orderfile_command.assert_not_called()
Tiancong Wangf9c736c2019-08-26 13:54:38 -070095 self.chrome_command.assert_not_called()
96
97 def testChromeAFDOSuccess(self):
98 """Test the command is called correctly with Chrome afdo."""
99 request = self._GetRequest(
100 build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO)
101 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
102 self.api_config)
103 self.chrome_command.assert_called_once_with(self.board)
104 self.orderfile_command.assert_not_called()
105 self.kernel_command.assert_not_called()
Tiancong Wangaf050172019-07-10 11:52:03 -0700106
107
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700108class UploadVettedFDOArtifactsTest(UpdateEbuildWithAFDOArtifactsTest):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700109 """Unittests for UploadVettedAFDOArtifacts."""
110
111 @staticmethod
112 def mock_die(message, *args):
113 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -0700114
115 def setUp(self):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700116 self.board = 'board'
117 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
118 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
119 self.command = self.PatchObject(
Tiancong Wangaf050172019-07-10 11:52:03 -0700120 toolchain_util,
Tiancong Wang24a3df72019-08-20 15:48:51 -0700121 'UploadAndPublishVettedAFDOArtifacts',
122 return_value=True)
123 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
Tiancong Wangaf050172019-07-10 11:52:03 -0700124
Alex Klein231d2da2019-07-22 16:44:45 -0600125 def testValidateOnly(self):
126 """Sanity check that a validate only call does not execute any logic."""
Tiancong Wang24a3df72019-08-20 15:48:51 -0700127 request = self._GetRequest(
128 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700129 toolchain.UploadVettedAFDOArtifacts(request, self.response,
130 self.validate_only_config)
Tiancong Wang24a3df72019-08-20 15:48:51 -0700131 self.command.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600132
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700133 def testMockCall(self):
134 """Test that a mock call does not execute logic, returns mock value."""
135 request = self._GetRequest(
136 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
137 toolchain.UploadVettedAFDOArtifacts(request, self.response,
138 self.mock_call_config)
139 self.command.assert_not_called()
140 self.assertEqual(self.response.status, True)
141
Tiancong Wang24a3df72019-08-20 15:48:51 -0700142 def testWrongArtifactType(self):
143 """Test passing wrong artifact type."""
144 request = self._GetRequest(
145 build_target=self.board, artifact_type=self.invalid_artifact_type)
146 with self.assertRaises(cros_build_lib.DieSystemExit) as context:
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700147 toolchain.UploadVettedAFDOArtifacts(request, self.response,
148 self.api_config)
Tiancong Wang24a3df72019-08-20 15:48:51 -0700149 self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type,
150 str(context.exception))
Alex Klein231d2da2019-07-22 16:44:45 -0600151
Tiancong Wang24a3df72019-08-20 15:48:51 -0700152 def testOrderfileSuccess(self):
153 """Test the command is called correctly with orderfile."""
154 request = self._GetRequest(
155 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
156 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
157 self.command.assert_called_once_with('orderfile', self.board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700158
Tiancong Wang24a3df72019-08-20 15:48:51 -0700159 def testKernelAFDOSuccess(self):
160 """Test the command is called correctly with kernel afdo."""
161 request = self._GetRequest(
162 build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO)
163 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
164 self.command.assert_called_once_with('kernel_afdo', self.board)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700165
166 def testChromeAFDOSuccess(self):
167 """Test the command is called correctly with Chrome afdo."""
168 request = self._GetRequest(
169 build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO)
170 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
171 self.command.assert_called_once_with('chrome_afdo', self.board)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700172
173
LaMont Jones4579e8c2019-12-06 14:20:37 -0700174class PrepareForBuildTest(cros_test_lib.MockTempDirTestCase,
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700175 api_config.ApiConfigMixin):
176 """Unittests for PrepareForBuild."""
177
178 def setUp(self):
179 self.response = toolchain_pb2.PrepareForToolchainBuildResponse()
180
181 def _GetRequest(self, artifact_types=None):
182 if artifact_types is None:
183 artifact_types = []
LaMont Jones4579e8c2019-12-06 14:20:37 -0700184 chroot = common_pb2.Chroot(path=self.tempdir)
185 sysroot = sysroot_pb2.Sysroot(
186 path='/build/board', build_target=common_pb2.BuildTarget(name='board'))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700187 return toolchain_pb2.PrepareForToolchainBuildRequest(
188 artifact_types=artifact_types,
LaMont Jones4579e8c2019-12-06 14:20:37 -0700189 chroot=chroot, sysroot=sysroot,
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700190 )
191
192 def testReturnsUnknownForUnknown(self):
193 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
194 toolchain.PrepareForBuild(request, self.response, self.api_config)
195 self.assertEqual(toolchain_pb2.PrepareForToolchainBuildResponse.UNKNOWN,
196 self.response.build_relevance)
197
198
199class BundleToolchainTest(cros_test_lib.MockTempDirTestCase,
200 api_config.ApiConfigMixin):
201 """Unittests for BundleToolchain."""
202
203 def setUp(self):
204 self.response = toolchain_pb2.BundleToolchainResponse()
205
206 def _GetRequest(self, artifact_types=None):
LaMont Jones4579e8c2019-12-06 14:20:37 -0700207 chroot = common_pb2.Chroot(path=self.tempdir)
208 sysroot = sysroot_pb2.Sysroot(
209 path='/build/board', build_target=common_pb2.BuildTarget(name='board'))
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700210 return toolchain_pb2.BundleToolchainRequest(
LaMont Jones4579e8c2019-12-06 14:20:37 -0700211 chroot=chroot, sysroot=sysroot,
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700212 output_dir=self.tempdir,
LaMont Jones4579e8c2019-12-06 14:20:37 -0700213 artifact_types=artifact_types,
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700214 )
215
216 def testReturnsUnknownForUnknown(self):
217 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
218 toolchain.BundleArtifacts(request, self.response, self.api_config)
219 self.assertEqual([], list(self.response.artifacts_info))
Michael Mortensen3232ab32020-01-05 19:14:36 -0700220
221 def testValidateOnly(self):
222 """Sanity check that a validate only call does not execute any logic."""
223 patch = self.PatchObject(toolchain_util, 'BundleArtifacts')
224 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
225 toolchain.BundleArtifacts(request, self.response,
226 self.validate_only_config)
227 patch.assert_not_called()