blob: 98f562c8fd2af48c0c6167296ee24b277d74e6af [file] [log] [blame]
Alex Kleineb77ffa2019-05-28 14:47:44 -06001# -*- 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"""packages controller unit tests."""
7
8from __future__ import print_function
9
10import mock
11
Alex Kleinda39c6d2019-09-16 14:36:36 -060012from chromite.api.api_config import ApiConfigMixin
Alex Kleineb77ffa2019-05-28 14:47:44 -060013from chromite.api.controller import packages as packages_controller
14from chromite.api.gen.chromite.api import binhost_pb2
15from chromite.api.gen.chromite.api import packages_pb2
16from chromite.lib import constants
17from chromite.lib import cros_build_lib
18from chromite.lib import cros_test_lib
David Burger1e0fe232019-07-01 14:52:07 -060019from chromite.lib import portage_util
Alex Kleineb77ffa2019-05-28 14:47:44 -060020from chromite.service import packages as packages_service
21
22
Alex Kleinda39c6d2019-09-16 14:36:36 -060023class UprevTest(cros_test_lib.MockTestCase, ApiConfigMixin):
Alex Kleineb77ffa2019-05-28 14:47:44 -060024 """Uprev tests."""
25
26 _PUBLIC = binhost_pb2.OVERLAYTYPE_PUBLIC
27 _PRIVATE = binhost_pb2.OVERLAYTYPE_PRIVATE
28 _BOTH = binhost_pb2.OVERLAYTYPE_BOTH
29 _NONE = binhost_pb2.OVERLAYTYPE_NONE
30
31 def setUp(self):
32 self.uprev_patch = self.PatchObject(packages_service, 'uprev_build_targets')
Alex Klein231d2da2019-07-22 16:44:45 -060033 self.response = packages_pb2.UprevPackagesResponse()
Alex Kleineb77ffa2019-05-28 14:47:44 -060034
35 def _GetRequest(self, targets=None, overlay_type=None, output_dir=None):
36 return packages_pb2.UprevPackagesRequest(
37 build_targets=[{'name': name} for name in targets or []],
38 overlay_type=overlay_type,
39 output_dir=output_dir,
40 )
41
Alex Klein231d2da2019-07-22 16:44:45 -060042 def testValidateOnly(self):
43 """Sanity check that a validate only call does not execute any logic."""
44 patch = self.PatchObject(packages_service, 'uprev_build_targets')
45
46 targets = ['foo', 'bar']
47 request = self._GetRequest(targets=targets, overlay_type=self._BOTH)
48 packages_controller.Uprev(request, self.response, self.validate_only_config)
49 patch.assert_not_called()
Alex Kleineb77ffa2019-05-28 14:47:44 -060050
51 def testNoOverlayTypeFails(self):
52 """No overlay type provided should fail."""
53 request = self._GetRequest(targets=['foo'])
Alex Kleineb77ffa2019-05-28 14:47:44 -060054
55 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060056 packages_controller.Uprev(request, self.response, self.api_config)
Alex Kleineb77ffa2019-05-28 14:47:44 -060057
58 def testOverlayTypeNoneFails(self):
59 """Overlay type none means nothing here and should fail."""
60 request = self._GetRequest(targets=['foo'], overlay_type=self._NONE)
Alex Kleineb77ffa2019-05-28 14:47:44 -060061
62 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -060063 packages_controller.Uprev(request, self.response, self.api_config)
Alex Kleineb77ffa2019-05-28 14:47:44 -060064
65 def testSuccess(self):
66 """Test overall successful argument handling."""
67 targets = ['foo', 'bar']
68 output_dir = '/tmp/uprev_output_dir'
Alex Klein5dd6b1e2019-07-31 15:45:24 -060069 changed = ['/ebuild-1.0-r1.ebuild', '/ebuild-1.0-r2.ebuild']
Alex Kleineb77ffa2019-05-28 14:47:44 -060070 expected_type = constants.BOTH_OVERLAYS
71 request = self._GetRequest(targets=targets, overlay_type=self._BOTH,
72 output_dir=output_dir)
Alex Klein5dd6b1e2019-07-31 15:45:24 -060073 uprev_patch = self.PatchObject(packages_service, 'uprev_build_targets',
74 return_value=changed)
Alex Kleineb77ffa2019-05-28 14:47:44 -060075
Alex Klein231d2da2019-07-22 16:44:45 -060076 packages_controller.Uprev(request, self.response, self.api_config)
Alex Kleineb77ffa2019-05-28 14:47:44 -060077
78 # Make sure the type is right, verify build targets after.
79 uprev_patch.assert_called_once_with(mock.ANY, expected_type, mock.ANY,
80 output_dir)
81 # First argument (build targets) of the first (only) call.
82 call_targets = uprev_patch.call_args[0][0]
83 self.assertItemsEqual(targets, [t.name for t in call_targets])
David Burger1e0fe232019-07-01 14:52:07 -060084
Alex Klein231d2da2019-07-22 16:44:45 -060085 for ebuild in self.response.modified_ebuilds:
Alex Klein5dd6b1e2019-07-31 15:45:24 -060086 self.assertIn(ebuild.path, changed)
87 changed.remove(ebuild.path)
88 self.assertFalse(changed)
89
David Burger1e0fe232019-07-01 14:52:07 -060090
Alex Kleinda39c6d2019-09-16 14:36:36 -060091class UprevVersionedPackageTest(cros_test_lib.MockTestCase, ApiConfigMixin):
Alex Klein87531182019-08-12 15:23:37 -060092 """UprevVersionedPackage tests."""
93
94 def setUp(self):
Yaakov Shaul730814a2019-09-10 13:58:25 -060095 self.response = packages_pb2.UprevVersionedPackageResponse()
Alex Klein87531182019-08-12 15:23:37 -060096
97 def _addVersion(self, request, version):
98 """Helper method to add a full version message to the request."""
99 ref = request.versions.add()
100 ref.repository = '/some/path'
Alex Klein34afcbc2019-08-22 16:14:31 -0600101 ref.ref = 'refs/tags/%s' % version
Alex Klein87531182019-08-12 15:23:37 -0600102 ref.revision = 'abc123'
103
104 def testValidateOnly(self):
105 """Sanity check validate only calls are working properly."""
106 service = self.PatchObject(packages_service, 'uprev_versioned_package')
107
108 request = packages_pb2.UprevVersionedPackageRequest()
109 self._addVersion(request, '1.2.3.4')
110 request.package_info.category = 'chromeos-base'
111 request.package_info.package_name = 'chromeos-chrome'
112
113 packages_controller.UprevVersionedPackage(request, self.response,
114 self.validate_only_config)
115
116 service.assert_not_called()
117
118 def testNoVersions(self):
119 """Test no versions provided."""
120 request = packages_pb2.UprevVersionedPackageRequest()
121 request.package_info.category = 'chromeos-base'
122 request.package_info.package_name = 'chromeos-chrome'
123
124 with self.assertRaises(cros_build_lib.DieSystemExit):
125 packages_controller.UprevVersionedPackage(request, self.response,
126 self.api_config)
127
128 def testNoPackageName(self):
129 """Test no package name provided."""
130 request = packages_pb2.UprevVersionedPackageRequest()
131 self._addVersion(request, '1.2.3.4')
132 request.package_info.category = 'chromeos-base'
133
134 with self.assertRaises(cros_build_lib.DieSystemExit):
135 packages_controller.UprevVersionedPackage(request, self.response,
136 self.api_config)
137
138 def testNoCategory(self):
139 """Test no package category provided."""
140 request = packages_pb2.UprevVersionedPackageRequest()
141 self._addVersion(request, '1.2.3.4')
142 request.package_info.package_name = 'chromeos-chrome'
143
144 with self.assertRaises(cros_build_lib.DieSystemExit):
145 packages_controller.UprevVersionedPackage(request, self.response,
146 self.api_config)
147
148 def testOutputHandling(self):
149 """Test the modified files are getting correctly added to the output."""
Alex Klein34afcbc2019-08-22 16:14:31 -0600150 version = '1.2.3.4'
Yaakov Shaul730814a2019-09-10 13:58:25 -0600151 result = packages_service.UprevVersionedPackageResult().add_result(
Alex Klein34afcbc2019-08-22 16:14:31 -0600152 version, ['/file/one', '/file/two'])
Yaakov Shaul730814a2019-09-10 13:58:25 -0600153
Alex Klein34afcbc2019-08-22 16:14:31 -0600154 self.PatchObject(
155 packages_service, 'uprev_versioned_package', return_value=result)
Alex Klein87531182019-08-12 15:23:37 -0600156
157 request = packages_pb2.UprevVersionedPackageRequest()
Alex Klein34afcbc2019-08-22 16:14:31 -0600158 self._addVersion(request, version)
Alex Klein87531182019-08-12 15:23:37 -0600159 request.package_info.category = 'chromeos-base'
160 request.package_info.package_name = 'chromeos-chrome'
161
162 packages_controller.UprevVersionedPackage(request, self.response,
163 self.api_config)
164
Yaakov Shaul730814a2019-09-10 13:58:25 -0600165 for idx, uprev_response in enumerate(self.response.responses):
166 self.assertEqual(result.modified[idx].new_version, uprev_response.version)
167 self.assertItemsEqual(
168 result.modified[idx].files,
169 [ebuild.path for ebuild in uprev_response.modified_ebuilds])
Alex Klein87531182019-08-12 15:23:37 -0600170
171
Alex Kleinda39c6d2019-09-16 14:36:36 -0600172class GetBestVisibleTest(cros_test_lib.MockTestCase, ApiConfigMixin):
David Burger1e0fe232019-07-01 14:52:07 -0600173 """GetBestVisible tests."""
174
Alex Klein231d2da2019-07-22 16:44:45 -0600175 def setUp(self):
176 self.response = packages_pb2.GetBestVisibleResponse()
177
David Burger1e0fe232019-07-01 14:52:07 -0600178 def _GetRequest(self, atom=None):
179 return packages_pb2.GetBestVisibleRequest(
180 atom=atom,
181 )
182
David Burger1e0fe232019-07-01 14:52:07 -0600183 def _MakeCpv(self, category, package, version):
184 unused = {
185 'cp': None,
186 'cpv': None,
187 'cpf': None,
188 'pv': None,
189 'version_no_rev': None,
190 'rev': None,
191 }
192 return portage_util.CPV(
193 category=category,
194 package=package,
195 version=version,
196 **unused
197 )
198
Alex Klein231d2da2019-07-22 16:44:45 -0600199 def testValidateOnly(self):
200 """Sanity check that a validate only call does not execute any logic."""
201 patch = self.PatchObject(packages_service, 'get_best_visible')
202
203 request = self._GetRequest(atom='chromeos-chrome')
204 packages_controller.GetBestVisible(request, self.response,
205 self.validate_only_config)
206 patch.assert_not_called()
207
David Burger1e0fe232019-07-01 14:52:07 -0600208 def testNoAtomFails(self):
209 """No atom provided should fail."""
210 request = self._GetRequest()
David Burger1e0fe232019-07-01 14:52:07 -0600211 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600212 packages_controller.GetBestVisible(request, self.response,
213 self.api_config)
David Burger1e0fe232019-07-01 14:52:07 -0600214
215 def testSuccess(self):
216 """Test overall success, argument handling, result forwarding."""
217 cpv = self._MakeCpv('category', 'package', 'version')
218 self.PatchObject(packages_service, 'get_best_visible', return_value=cpv)
219
220 request = self._GetRequest(atom='chromeos-chrome')
David Burger1e0fe232019-07-01 14:52:07 -0600221
Alex Klein231d2da2019-07-22 16:44:45 -0600222 packages_controller.GetBestVisible(request, self.response, self.api_config)
David Burger1e0fe232019-07-01 14:52:07 -0600223
Alex Klein231d2da2019-07-22 16:44:45 -0600224 package_info = self.response.package_info
David Burger1e0fe232019-07-01 14:52:07 -0600225 self.assertEqual(package_info.category, cpv.category)
226 self.assertEqual(package_info.package_name, cpv.package)
227 self.assertEqual(package_info.version, cpv.version)
Alex Kleinda39c6d2019-09-16 14:36:36 -0600228
229
230class HasChromePrebuiltTest(cros_test_lib.MockTestCase, ApiConfigMixin):
231 """HasChromePrebuilt tests."""
232
233 def setUp(self):
234 self.response = packages_pb2.HasChromePrebuiltResponse()
235
236 def _GetRequest(self, board=None):
237 """Helper to build out a request."""
238 request = packages_pb2.HasChromePrebuiltRequest()
239
240 if board:
241 request.build_target.name = board
242
243 return request
244
245 def testValidateOnly(self):
246 """Sanity check that a validate only call does not execute any logic."""
247 patch = self.PatchObject(packages_service, 'has_prebuilt')
248
249 request = self._GetRequest(board='betty')
250 packages_controller.HasChromePrebuilt(request, self.response,
251 self.validate_only_config)
252 patch.assert_not_called()
253
254 def testNoBuildTargetFails(self):
255 """No build target argument should fail."""
256 request = self._GetRequest()
257
258 with self.assertRaises(cros_build_lib.DieSystemExit):
259 packages_controller.HasChromePrebuilt(request, self.response,
260 self.api_config)