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 |
| 13 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_build_lib |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_test_lib |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 16 | from chromite.lib import toolchain_util |
| 17 | |
| 18 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 19 | class UpdateEbuildWithAFDOArtifactsTest(cros_test_lib.MockTestCase, |
| 20 | api_config.ApiConfigMixin): |
| 21 | """Unittests for UpdateEbuildWithAFDOArtifacts.""" |
| 22 | |
| 23 | @staticmethod |
| 24 | def mock_die(message, *args): |
| 25 | raise cros_build_lib.DieSystemExit(message % args) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 26 | |
| 27 | def setUp(self): |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 28 | self.board = 'board' |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 29 | self.response = toolchain_pb2.VerifyAFDOArtifactsResponse() |
| 30 | self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO |
| 31 | self.orderfile_command = self.PatchObject( |
| 32 | toolchain_util, 'OrderfileUpdateChromeEbuild', return_value=True) |
| 33 | self.kernel_command = self.PatchObject( |
| 34 | toolchain_util, 'AFDOUpdateKernelEbuild', return_value=True) |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 35 | self.chrome_command = self.PatchObject( |
| 36 | toolchain_util, 'AFDOUpdateChromeEbuild', return_value=True) |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 37 | self.PatchObject(cros_build_lib, 'Die', new=self.mock_die) |
| 38 | |
| 39 | def _GetRequest(self, build_target=None, artifact_type=None): |
| 40 | return toolchain_pb2.VerifyAFDOArtifactsRequest( |
| 41 | build_target={'name': build_target}, |
| 42 | artifact_type=artifact_type, |
| 43 | ) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 44 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 45 | def testValidateOnly(self): |
| 46 | """Sanity check that a validate only call does not execute any logic.""" |
| 47 | patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild') |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 48 | request = self._GetRequest( |
| 49 | build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE) |
| 50 | toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response, |
| 51 | self.validate_only_config) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 52 | patch.assert_not_called() |
| 53 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 54 | def testWrongArtifactType(self): |
| 55 | """Test passing wrong artifact type.""" |
| 56 | request = self._GetRequest( |
| 57 | build_target=self.board, artifact_type=self.invalid_artifact_type) |
| 58 | with self.assertRaises(cros_build_lib.DieSystemExit) as context: |
| 59 | toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response, |
| 60 | self.api_config) |
| 61 | self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type, |
| 62 | str(context.exception)) |
| 63 | |
| 64 | def testOrderfileSuccess(self): |
| 65 | """Test the command is called correctly with orderfile.""" |
| 66 | request = self._GetRequest( |
| 67 | build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE) |
| 68 | toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response, |
| 69 | self.api_config) |
| 70 | self.orderfile_command.assert_called_once_with(self.board) |
| 71 | self.kernel_command.assert_not_called() |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 72 | self.chrome_command.assert_not_called() |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 73 | |
| 74 | def testKernelAFDOSuccess(self): |
| 75 | """Test the command is called correctly with kernel afdo.""" |
| 76 | request = self._GetRequest( |
| 77 | build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO) |
| 78 | toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response, |
| 79 | self.api_config) |
| 80 | self.kernel_command.assert_called_once_with(self.board) |
| 81 | self.orderfile_command.assert_not_called() |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 82 | self.chrome_command.assert_not_called() |
| 83 | |
| 84 | def testChromeAFDOSuccess(self): |
| 85 | """Test the command is called correctly with Chrome afdo.""" |
| 86 | request = self._GetRequest( |
| 87 | build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO) |
| 88 | toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response, |
| 89 | self.api_config) |
| 90 | self.chrome_command.assert_called_once_with(self.board) |
| 91 | self.orderfile_command.assert_not_called() |
| 92 | self.kernel_command.assert_not_called() |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 93 | |
| 94 | |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 95 | class UploadVettedFDOArtifactsTest(UpdateEbuildWithAFDOArtifactsTest): |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 96 | """Unittests for UploadVettedAFDOArtifacts.""" |
| 97 | |
| 98 | @staticmethod |
| 99 | def mock_die(message, *args): |
| 100 | raise cros_build_lib.DieSystemExit(message % args) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 101 | |
| 102 | def setUp(self): |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 103 | self.board = 'board' |
| 104 | self.response = toolchain_pb2.VerifyAFDOArtifactsResponse() |
| 105 | self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO |
| 106 | self.command = self.PatchObject( |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 107 | toolchain_util, |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 108 | 'UploadAndPublishVettedAFDOArtifacts', |
| 109 | return_value=True) |
| 110 | self.PatchObject(cros_build_lib, 'Die', new=self.mock_die) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 111 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 112 | def testValidateOnly(self): |
| 113 | """Sanity check that a validate only call does not execute any logic.""" |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 114 | request = self._GetRequest( |
| 115 | build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE) |
| 116 | toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response, |
| 117 | self.validate_only_config) |
| 118 | self.command.assert_not_called() |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 119 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 120 | def testWrongArtifactType(self): |
| 121 | """Test passing wrong artifact type.""" |
| 122 | request = self._GetRequest( |
| 123 | build_target=self.board, artifact_type=self.invalid_artifact_type) |
| 124 | with self.assertRaises(cros_build_lib.DieSystemExit) as context: |
| 125 | toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response, |
| 126 | self.api_config) |
| 127 | self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type, |
| 128 | str(context.exception)) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 129 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 130 | def testOrderfileSuccess(self): |
| 131 | """Test the command is called correctly with orderfile.""" |
| 132 | request = self._GetRequest( |
| 133 | build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE) |
| 134 | toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config) |
| 135 | self.command.assert_called_once_with('orderfile', self.board) |
Tiancong Wang | af05017 | 2019-07-10 11:52:03 -0700 | [diff] [blame] | 136 | |
Tiancong Wang | 24a3df7 | 2019-08-20 15:48:51 -0700 | [diff] [blame] | 137 | def testKernelAFDOSuccess(self): |
| 138 | """Test the command is called correctly with kernel afdo.""" |
| 139 | request = self._GetRequest( |
| 140 | build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO) |
| 141 | toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config) |
| 142 | self.command.assert_called_once_with('kernel_afdo', self.board) |
Tiancong Wang | f9c736c | 2019-08-26 13:54:38 -0700 | [diff] [blame] | 143 | |
| 144 | def testChromeAFDOSuccess(self): |
| 145 | """Test the command is called correctly with Chrome afdo.""" |
| 146 | request = self._GetRequest( |
| 147 | build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO) |
| 148 | toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config) |
| 149 | self.command.assert_called_once_with('chrome_afdo', self.board) |