blob: 709b37953c7837f9e61ae123359f7cde982aa486 [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
Tiancong Wang24a3df72019-08-20 15:48:51 -070054 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 Wangf9c736c2019-08-26 13:54:38 -070072 self.chrome_command.assert_not_called()
Tiancong Wang24a3df72019-08-20 15:48:51 -070073
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 Wangf9c736c2019-08-26 13:54:38 -070082 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 Wangaf050172019-07-10 11:52:03 -070093
94
Tiancong Wangf9c736c2019-08-26 13:54:38 -070095class UploadVettedFDOArtifactsTest(UpdateEbuildWithAFDOArtifactsTest):
Tiancong Wang24a3df72019-08-20 15:48:51 -070096 """Unittests for UploadVettedAFDOArtifacts."""
97
98 @staticmethod
99 def mock_die(message, *args):
100 raise cros_build_lib.DieSystemExit(message % args)
Tiancong Wangaf050172019-07-10 11:52:03 -0700101
102 def setUp(self):
Tiancong Wang24a3df72019-08-20 15:48:51 -0700103 self.board = 'board'
104 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
105 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
106 self.command = self.PatchObject(
Tiancong Wangaf050172019-07-10 11:52:03 -0700107 toolchain_util,
Tiancong Wang24a3df72019-08-20 15:48:51 -0700108 'UploadAndPublishVettedAFDOArtifacts',
109 return_value=True)
110 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
Tiancong Wangaf050172019-07-10 11:52:03 -0700111
Alex Klein231d2da2019-07-22 16:44:45 -0600112 def testValidateOnly(self):
113 """Sanity check that a validate only call does not execute any logic."""
Tiancong Wang24a3df72019-08-20 15:48:51 -0700114 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 Klein231d2da2019-07-22 16:44:45 -0600119
Tiancong Wang24a3df72019-08-20 15:48:51 -0700120 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 Klein231d2da2019-07-22 16:44:45 -0600129
Tiancong Wang24a3df72019-08-20 15:48:51 -0700130 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 Wangaf050172019-07-10 11:52:03 -0700136
Tiancong Wang24a3df72019-08-20 15:48:51 -0700137 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 Wangf9c736c2019-08-26 13:54:38 -0700143
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)