blob: 0d024d1db69f1c86cb61de7129ec31f2bc84b441 [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
12from 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
Tiancong Wangaf050172019-07-10 11:52:03 -070014
Tiancong Wang24a3df72019-08-20 15:48:51 -070015from chromite.lib import cros_build_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070016from chromite.lib import cros_test_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070017from chromite.lib import toolchain_util
18
19
Tiancong Wang24a3df72019-08-20 15:48:51 -070020class UpdateEbuildWithAFDOArtifactsTest(cros_test_lib.MockTestCase,
21 api_config.ApiConfigMixin):
22 """Unittests for UpdateEbuildWithAFDOArtifacts."""
23
24 @staticmethod
25 def mock_die(message, *args):
26 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -070027
28 def setUp(self):
Tiancong Wangaf050172019-07-10 11:52:03 -070029 self.board = 'board'
Tiancong Wang24a3df72019-08-20 15:48:51 -070030 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
31 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
32 self.orderfile_command = self.PatchObject(
33 toolchain_util, 'OrderfileUpdateChromeEbuild', return_value=True)
34 self.kernel_command = self.PatchObject(
35 toolchain_util, 'AFDOUpdateKernelEbuild', return_value=True)
Tiancong Wangf9c736c2019-08-26 13:54:38 -070036 self.chrome_command = self.PatchObject(
37 toolchain_util, 'AFDOUpdateChromeEbuild', return_value=True)
Tiancong Wang24a3df72019-08-20 15:48:51 -070038 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
39
40 def _GetRequest(self, build_target=None, artifact_type=None):
41 return toolchain_pb2.VerifyAFDOArtifactsRequest(
42 build_target={'name': build_target},
43 artifact_type=artifact_type,
44 )
Tiancong Wangaf050172019-07-10 11:52:03 -070045
Alex Klein231d2da2019-07-22 16:44:45 -060046 def testValidateOnly(self):
47 """Sanity check that a validate only call does not execute any logic."""
48 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
Tiancong Wang24a3df72019-08-20 15:48:51 -070049 request = self._GetRequest(
50 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
51 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
52 self.validate_only_config)
Alex Klein231d2da2019-07-22 16:44:45 -060053 patch.assert_not_called()
54
Michael Mortensen54bd70a2019-11-21 14:45:38 -070055 def testMockCall(self):
56 """Test that a mock call does not execute logic, returns mock value."""
57 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
58 request = self._GetRequest(
59 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
60 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
61 self.mock_call_config)
62 patch.assert_not_called()
63 self.assertEqual(self.response.status, True)
64
Tiancong Wang24a3df72019-08-20 15:48:51 -070065 def testWrongArtifactType(self):
66 """Test passing wrong artifact type."""
67 request = self._GetRequest(
68 build_target=self.board, artifact_type=self.invalid_artifact_type)
69 with self.assertRaises(cros_build_lib.DieSystemExit) as context:
70 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
71 self.api_config)
72 self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type,
73 str(context.exception))
74
75 def testOrderfileSuccess(self):
76 """Test the command is called correctly with orderfile."""
77 request = self._GetRequest(
78 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
79 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
80 self.api_config)
81 self.orderfile_command.assert_called_once_with(self.board)
82 self.kernel_command.assert_not_called()
Tiancong Wangf9c736c2019-08-26 13:54:38 -070083 self.chrome_command.assert_not_called()
Tiancong Wang24a3df72019-08-20 15:48:51 -070084
85 def testKernelAFDOSuccess(self):
86 """Test the command is called correctly with kernel afdo."""
87 request = self._GetRequest(
88 build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO)
89 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
90 self.api_config)
91 self.kernel_command.assert_called_once_with(self.board)
92 self.orderfile_command.assert_not_called()
Tiancong Wangf9c736c2019-08-26 13:54:38 -070093 self.chrome_command.assert_not_called()
94
95 def testChromeAFDOSuccess(self):
96 """Test the command is called correctly with Chrome afdo."""
97 request = self._GetRequest(
98 build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO)
99 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
100 self.api_config)
101 self.chrome_command.assert_called_once_with(self.board)
102 self.orderfile_command.assert_not_called()
103 self.kernel_command.assert_not_called()
Tiancong Wangaf050172019-07-10 11:52:03 -0700104
105
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700106class UploadVettedFDOArtifactsTest(UpdateEbuildWithAFDOArtifactsTest):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700107 """Unittests for UploadVettedAFDOArtifacts."""
108
109 @staticmethod
110 def mock_die(message, *args):
111 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -0700112
113 def setUp(self):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700114 self.board = 'board'
115 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
116 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
117 self.command = self.PatchObject(
Tiancong Wangaf050172019-07-10 11:52:03 -0700118 toolchain_util,
Tiancong Wang24a3df72019-08-20 15:48:51 -0700119 'UploadAndPublishVettedAFDOArtifacts',
120 return_value=True)
121 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
Tiancong Wangaf050172019-07-10 11:52:03 -0700122
Alex Klein231d2da2019-07-22 16:44:45 -0600123 def testValidateOnly(self):
124 """Sanity check that a validate only call does not execute any logic."""
Tiancong Wang24a3df72019-08-20 15:48:51 -0700125 request = self._GetRequest(
126 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700127 toolchain.UploadVettedAFDOArtifacts(request, self.response,
128 self.validate_only_config)
Tiancong Wang24a3df72019-08-20 15:48:51 -0700129 self.command.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600130
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700131 def testMockCall(self):
132 """Test that a mock call does not execute logic, returns mock value."""
133 request = self._GetRequest(
134 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
135 toolchain.UploadVettedAFDOArtifacts(request, self.response,
136 self.mock_call_config)
137 self.command.assert_not_called()
138 self.assertEqual(self.response.status, True)
139
Tiancong Wang24a3df72019-08-20 15:48:51 -0700140 def testWrongArtifactType(self):
141 """Test passing wrong artifact type."""
142 request = self._GetRequest(
143 build_target=self.board, artifact_type=self.invalid_artifact_type)
144 with self.assertRaises(cros_build_lib.DieSystemExit) as context:
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700145 toolchain.UploadVettedAFDOArtifacts(request, self.response,
146 self.api_config)
Tiancong Wang24a3df72019-08-20 15:48:51 -0700147 self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type,
148 str(context.exception))
Alex Klein231d2da2019-07-22 16:44:45 -0600149
Tiancong Wang24a3df72019-08-20 15:48:51 -0700150 def testOrderfileSuccess(self):
151 """Test the command is called correctly with orderfile."""
152 request = self._GetRequest(
153 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
154 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
155 self.command.assert_called_once_with('orderfile', self.board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700156
Tiancong Wang24a3df72019-08-20 15:48:51 -0700157 def testKernelAFDOSuccess(self):
158 """Test the command is called correctly with kernel afdo."""
159 request = self._GetRequest(
160 build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO)
161 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
162 self.command.assert_called_once_with('kernel_afdo', self.board)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700163
164 def testChromeAFDOSuccess(self):
165 """Test the command is called correctly with Chrome afdo."""
166 request = self._GetRequest(
167 build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO)
168 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
169 self.command.assert_called_once_with('chrome_afdo', self.board)
LaMont Jonesb20b3d92019-11-23 11:47:48 -0700170
171
172class PrepareForBuildTest(cros_test_lib.MockTestCase,
173 api_config.ApiConfigMixin):
174 """Unittests for PrepareForBuild."""
175
176 def setUp(self):
177 self.response = toolchain_pb2.PrepareForToolchainBuildResponse()
178
179 def _GetRequest(self, artifact_types=None):
180 if artifact_types is None:
181 artifact_types = []
182 return toolchain_pb2.PrepareForToolchainBuildRequest(
183 artifact_types=artifact_types,
184 )
185
186 def testReturnsUnknownForUnknown(self):
187 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
188 toolchain.PrepareForBuild(request, self.response, self.api_config)
189 self.assertEqual(toolchain_pb2.PrepareForToolchainBuildResponse.UNKNOWN,
190 self.response.build_relevance)
191
192
193class BundleToolchainTest(cros_test_lib.MockTempDirTestCase,
194 api_config.ApiConfigMixin):
195 """Unittests for BundleToolchain."""
196
197 def setUp(self):
198 self.response = toolchain_pb2.BundleToolchainResponse()
199
200 def _GetRequest(self, artifact_types=None):
201 return toolchain_pb2.BundleToolchainRequest(
202 artifact_types=artifact_types,
203 output_dir=self.tempdir,
204 )
205
206 def testReturnsUnknownForUnknown(self):
207 request = self._GetRequest([BuilderConfig.Artifacts.IMAGE_ARCHIVES])
208 toolchain.BundleArtifacts(request, self.response, self.api_config)
209 self.assertEqual([], list(self.response.artifacts_info))