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