Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 10 | from chromite.api import api_config |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 11 | from chromite.api.controller import toolchain |
| 12 | from chromite.api.gen.chromite.api import toolchain_pb2 |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame^] | 13 | from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 14 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_build_lib |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_test_lib |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 17 | from chromite.lib import toolchain_util |
| 18 | |
| 19 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 20 | class 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 Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 27 | |
| 28 | def setUp(self): |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 29 | self.board = 'board' |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 30 | 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 Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 36 | self.chrome_command = self.PatchObject( |
| 37 | toolchain_util, 'AFDOUpdateChromeEbuild', return_value=True) |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 38 | 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 Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 45 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 46 | def testValidateOnly(self): |
| 47 | """Sanity check that a validate only call does not execute any logic.""" |
| 48 | patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild') |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 49 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 53 | patch.assert_not_called() |
| 54 | |
Michael Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 55 | 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 Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 65 | 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 Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 83 | self.chrome_command.assert_not_called() |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 84 | |
| 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 Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 93 | 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 Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 104 | |
| 105 | |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 106 | class UploadVettedFDOArtifactsTest(UpdateEbuildWithAFDOArtifactsTest): |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 107 | """Unittests for UploadVettedAFDOArtifacts.""" |
| 108 | |
| 109 | @staticmethod |
| 110 | def mock_die(message, *args): |
| 111 | raise cros_build_lib.DieSystemExit(message % args) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 112 | |
| 113 | def setUp(self): |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 114 | self.board = 'board' |
| 115 | self.response = toolchain_pb2.VerifyAFDOArtifactsResponse() |
| 116 | self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO |
| 117 | self.command = self.PatchObject( |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 118 | toolchain_util, |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 119 | 'UploadAndPublishVettedAFDOArtifacts', |
| 120 | return_value=True) |
| 121 | self.PatchObject(cros_build_lib, 'Die', new=self.mock_die) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 122 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 123 | def testValidateOnly(self): |
| 124 | """Sanity check that a validate only call does not execute any logic.""" |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 125 | request = self._GetRequest( |
| 126 | build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE) |
Michael Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 127 | toolchain.UploadVettedAFDOArtifacts(request, self.response, |
| 128 | self.validate_only_config) |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 129 | self.command.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 130 | |
Michael Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 131 | 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 Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 140 | 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 Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 145 | toolchain.UploadVettedAFDOArtifacts(request, self.response, |
| 146 | self.api_config) |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 147 | self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type, |
| 148 | str(context.exception)) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 149 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 150 | 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 Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 156 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 157 | 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 Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 163 | |
| 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 Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame^] | 170 | |
| 171 | |
| 172 | class 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 | |
| 193 | class 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)) |