blob: 0c5973d7e1419588c29943210d3bd4dec5300a60 [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
13
Tiancong Wang24a3df72019-08-20 15:48:51 -070014from chromite.lib import cros_build_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070015from chromite.lib import cros_test_lib
Tiancong Wangaf050172019-07-10 11:52:03 -070016from chromite.lib import toolchain_util
17
18
Tiancong Wang24a3df72019-08-20 15:48:51 -070019class 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 Wangaf050172019-07-10 11:52:03 -070026
27 def setUp(self):
Tiancong Wangaf050172019-07-10 11:52:03 -070028 self.board = 'board'
Tiancong Wang24a3df72019-08-20 15:48:51 -070029 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 Wangf9c736c2019-08-26 13:54:38 -070035 self.chrome_command = self.PatchObject(
36 toolchain_util, 'AFDOUpdateChromeEbuild', return_value=True)
Tiancong Wang24a3df72019-08-20 15:48:51 -070037 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 Wangaf050172019-07-10 11:52:03 -070044
Alex Klein231d2da2019-07-22 16:44:45 -060045 def testValidateOnly(self):
46 """Sanity check that a validate only call does not execute any logic."""
47 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
Tiancong Wang24a3df72019-08-20 15:48:51 -070048 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 Klein231d2da2019-07-22 16:44:45 -060052 patch.assert_not_called()
53
Michael Mortensen54bd70a2019-11-21 14:45:38 -070054 def testMockCall(self):
55 """Test that a mock call does not execute logic, returns mock value."""
56 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
57 request = self._GetRequest(
58 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
59 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
60 self.mock_call_config)
61 patch.assert_not_called()
62 self.assertEqual(self.response.status, True)
63
Tiancong Wang24a3df72019-08-20 15:48:51 -070064 def testWrongArtifactType(self):
65 """Test passing wrong artifact type."""
66 request = self._GetRequest(
67 build_target=self.board, artifact_type=self.invalid_artifact_type)
68 with self.assertRaises(cros_build_lib.DieSystemExit) as context:
69 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
70 self.api_config)
71 self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type,
72 str(context.exception))
73
74 def testOrderfileSuccess(self):
75 """Test the command is called correctly with orderfile."""
76 request = self._GetRequest(
77 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
78 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
79 self.api_config)
80 self.orderfile_command.assert_called_once_with(self.board)
81 self.kernel_command.assert_not_called()
Tiancong Wangf9c736c2019-08-26 13:54:38 -070082 self.chrome_command.assert_not_called()
Tiancong Wang24a3df72019-08-20 15:48:51 -070083
84 def testKernelAFDOSuccess(self):
85 """Test the command is called correctly with kernel afdo."""
86 request = self._GetRequest(
87 build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO)
88 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
89 self.api_config)
90 self.kernel_command.assert_called_once_with(self.board)
91 self.orderfile_command.assert_not_called()
Tiancong Wangf9c736c2019-08-26 13:54:38 -070092 self.chrome_command.assert_not_called()
93
94 def testChromeAFDOSuccess(self):
95 """Test the command is called correctly with Chrome afdo."""
96 request = self._GetRequest(
97 build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO)
98 toolchain.UpdateEbuildWithAFDOArtifacts(request, self.response,
99 self.api_config)
100 self.chrome_command.assert_called_once_with(self.board)
101 self.orderfile_command.assert_not_called()
102 self.kernel_command.assert_not_called()
Tiancong Wangaf050172019-07-10 11:52:03 -0700103
104
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700105class UploadVettedFDOArtifactsTest(UpdateEbuildWithAFDOArtifactsTest):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700106 """Unittests for UploadVettedAFDOArtifacts."""
107
108 @staticmethod
109 def mock_die(message, *args):
110 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -0700111
112 def setUp(self):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700113 self.board = 'board'
114 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
115 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
116 self.command = self.PatchObject(
Tiancong Wangaf050172019-07-10 11:52:03 -0700117 toolchain_util,
Tiancong Wang24a3df72019-08-20 15:48:51 -0700118 'UploadAndPublishVettedAFDOArtifacts',
119 return_value=True)
120 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
Tiancong Wangaf050172019-07-10 11:52:03 -0700121
Alex Klein231d2da2019-07-22 16:44:45 -0600122 def testValidateOnly(self):
123 """Sanity check that a validate only call does not execute any logic."""
Tiancong Wang24a3df72019-08-20 15:48:51 -0700124 request = self._GetRequest(
125 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700126 toolchain.UploadVettedAFDOArtifacts(request, self.response,
127 self.validate_only_config)
Tiancong Wang24a3df72019-08-20 15:48:51 -0700128 self.command.assert_not_called()
Alex Klein231d2da2019-07-22 16:44:45 -0600129
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700130 def testMockCall(self):
131 """Test that a mock call does not execute logic, returns mock value."""
132 request = self._GetRequest(
133 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
134 toolchain.UploadVettedAFDOArtifacts(request, self.response,
135 self.mock_call_config)
136 self.command.assert_not_called()
137 self.assertEqual(self.response.status, True)
138
Tiancong Wang24a3df72019-08-20 15:48:51 -0700139 def testWrongArtifactType(self):
140 """Test passing wrong artifact type."""
141 request = self._GetRequest(
142 build_target=self.board, artifact_type=self.invalid_artifact_type)
143 with self.assertRaises(cros_build_lib.DieSystemExit) as context:
Michael Mortensen54bd70a2019-11-21 14:45:38 -0700144 toolchain.UploadVettedAFDOArtifacts(request, self.response,
145 self.api_config)
Tiancong Wang24a3df72019-08-20 15:48:51 -0700146 self.assertIn('artifact_type (%d) must be in' % self.invalid_artifact_type,
147 str(context.exception))
Alex Klein231d2da2019-07-22 16:44:45 -0600148
Tiancong Wang24a3df72019-08-20 15:48:51 -0700149 def testOrderfileSuccess(self):
150 """Test the command is called correctly with orderfile."""
151 request = self._GetRequest(
152 build_target=self.board, artifact_type=toolchain_pb2.ORDERFILE)
153 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
154 self.command.assert_called_once_with('orderfile', self.board)
Tiancong Wangaf050172019-07-10 11:52:03 -0700155
Tiancong Wang24a3df72019-08-20 15:48:51 -0700156 def testKernelAFDOSuccess(self):
157 """Test the command is called correctly with kernel afdo."""
158 request = self._GetRequest(
159 build_target=self.board, artifact_type=toolchain_pb2.KERNEL_AFDO)
160 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
161 self.command.assert_called_once_with('kernel_afdo', self.board)
Tiancong Wangf9c736c2019-08-26 13:54:38 -0700162
163 def testChromeAFDOSuccess(self):
164 """Test the command is called correctly with Chrome afdo."""
165 request = self._GetRequest(
166 build_target=self.board, artifact_type=toolchain_pb2.CHROME_AFDO)
167 toolchain.UploadVettedAFDOArtifacts(request, self.response, self.api_config)
168 self.command.assert_called_once_with('chrome_afdo', self.board)