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 |
LaMont Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame^] | 12 | from chromite.api.gen.chromite.api import sysroot_pb2 |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 13 | from chromite.api.gen.chromite.api import toolchain_pb2 |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 14 | from chromite.api.gen.chromiumos.builder_config_pb2 import BuilderConfig |
LaMont Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame^] | 15 | from chromite.api.gen.chromiumos import common_pb2 |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 16 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 17 | from chromite.lib import cros_build_lib |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 18 | from chromite.lib import cros_test_lib |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 19 | from chromite.lib import toolchain_util |
| 20 | |
| 21 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 22 | class 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 Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 29 | |
| 30 | def setUp(self): |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 31 | self.board = 'board' |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 32 | 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 Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 38 | self.chrome_command = self.PatchObject( |
| 39 | toolchain_util, 'AFDOUpdateChromeEbuild', return_value=True) |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 40 | 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 Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 47 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 48 | def testValidateOnly(self): |
| 49 | """Sanity check that a validate only call does not execute any logic.""" |
| 50 | patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild') |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 51 | 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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 55 | patch.assert_not_called() |
| 56 | |
Michael Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 57 | 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 Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 67 | 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 Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 85 | self.chrome_command.assert_not_called() |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 86 | |
| 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 Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 95 | 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 Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 106 | |
| 107 | |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 108 | class UploadVettedFDOArtifactsTest(UpdateEbuildWithAFDOArtifactsTest): |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 109 | """Unittests for UploadVettedAFDOArtifacts.""" |
| 110 | |
| 111 | @staticmethod |
| 112 | def mock_die(message, *args): |
| 113 | raise cros_build_lib.DieSystemExit(message % args) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 114 | |
| 115 | def setUp(self): |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 116 | self.board = 'board' |
| 117 | self.response = toolchain_pb2.VerifyAFDOArtifactsResponse() |
| 118 | self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO |
| 119 | self.command = self.PatchObject( |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 120 | toolchain_util, |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 121 | 'UploadAndPublishVettedAFDOArtifacts', |
| 122 | return_value=True) |
| 123 | self.PatchObject(cros_build_lib, 'Die', new=self.mock_die) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 124 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 125 | def testValidateOnly(self): |
| 126 | """Sanity check that a validate only call does not execute any logic.""" |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 127 | request = self._GetRequest( |
| 128 | build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE) |
Michael Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 129 | toolchain.UploadVettedAFDOArtifacts(request, self.response, |
| 130 | self.validate_only_config) |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 131 | self.command.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 132 | |
Michael Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 133 | 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 Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 142 | 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 Mortensen | 54bd70a | 2019-11-21 14:45:38 -0700 | [diff] [blame] | 147 | toolchain.UploadVettedAFDOArtifacts(request, self.response, |
| 148 | self.api_config) |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 149 | self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type, |
| 150 | str(context.exception)) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 151 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 152 | 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 Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 158 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 159 | 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 Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 165 | |
| 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 Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 172 | |
| 173 | |
LaMont Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame^] | 174 | class PrepareForBuildTest(cros_test_lib.MockTempDirTestCase, |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 175 | 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 Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame^] | 184 | chroot = common_pb2.Chroot(path=self.tempdir) |
| 185 | sysroot = sysroot_pb2.Sysroot( |
| 186 | path='/build/board', build_target=common_pb2.BuildTarget(name='board')) |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 187 | return toolchain_pb2.PrepareForToolchainBuildRequest( |
| 188 | artifact_types=artifact_types, |
LaMont Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame^] | 189 | chroot=chroot, sysroot=sysroot, |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 190 | ) |
| 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 | |
| 199 | class 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 Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame^] | 207 | chroot = common_pb2.Chroot(path=self.tempdir) |
| 208 | sysroot = sysroot_pb2.Sysroot( |
| 209 | path='/build/board', build_target=common_pb2.BuildTarget(name='board')) |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 210 | return toolchain_pb2.BundleToolchainRequest( |
LaMont Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame^] | 211 | chroot=chroot, sysroot=sysroot, |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 212 | output_dir=self.tempdir, |
LaMont Jones | 4579e8c | 2019-12-06 14:20:37 -0700 | [diff] [blame^] | 213 | artifact_types=artifact_types, |
LaMont Jones | b20b3d9 | 2019-11-23 11:47:48 -0700 | [diff] [blame] | 214 | ) |
| 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)) |