blob: 7d66651e866c765c4675fee854d1df37ddeea17b [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)
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 Wangaf050172019-07-10 11:52:03 -070042
Alex Klein231d2da2019-07-22 16:44:45 -060043 def testValidateOnly(self):
44 """Sanity check that a validate only call does not execute any logic."""
45 patch = self.PatchObject(toolchain_util, 'OrderfileUpdateChromeEbuild')
Tiancong Wang24a3df72019-08-20 15:48:51 -070046 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 Klein231d2da2019-07-22 16:44:45 -060050 patch.assert_not_called()
51
Tiancong Wang24a3df72019-08-20 15:48:51 -070052 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 Wangaf050172019-07-10 11:52:03 -070079
80
Tiancong Wang24a3df72019-08-20 15:48:51 -070081class 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 Wangaf050172019-07-10 11:52:03 -070088
89 def setUp(self):
Tiancong Wang24a3df72019-08-20 15:48:51 -070090 self.board = 'board'
91 self.response = toolchain_pb2.VerifyAFDOArtifactsResponse()
92 self.invalid_artifact_type = toolchain_pb2.BENCHMARK_AFDO
93 self.command = self.PatchObject(
Tiancong Wangaf050172019-07-10 11:52:03 -070094 toolchain_util,
Tiancong Wang24a3df72019-08-20 15:48:51 -070095 'UploadAndPublishVettedAFDOArtifacts',
96 return_value=True)
97 self.PatchObject(cros_build_lib, 'Die', new=self.mock_die)
Tiancong Wangaf050172019-07-10 11:52:03 -070098
Alex Klein231d2da2019-07-22 16:44:45 -060099 def testValidateOnly(self):
100 """Sanity check that a validate only call does not execute any logic."""
Tiancong Wang24a3df72019-08-20 15:48:51 -0700101 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 Klein231d2da2019-07-22 16:44:45 -0600106
Tiancong Wang24a3df72019-08-20 15:48:51 -0700107 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 Klein231d2da2019-07-22 16:44:45 -0600116
Tiancong Wang24a3df72019-08-20 15:48:51 -0700117 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 Wangaf050172019-07-10 11:52:03 -0700123
Tiancong Wang24a3df72019-08-20 15:48:51 -0700124 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)