blob: 7b51c936bc955df57806f8961297bf59e4223f6a [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 Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import api_config
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 Klein231d2da2019-07-22 16:44:45 -060023class UprevTest(cros_test_lib.MockTestCase, api_config.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 Klein231d2da2019-07-22 16:44:45 -060091class GetBestVisibleTest(cros_test_lib.MockTestCase, api_config.ApiConfigMixin):
David Burger1e0fe232019-07-01 14:52:07 -060092 """GetBestVisible tests."""
93
Alex Klein231d2da2019-07-22 16:44:45 -060094 def setUp(self):
95 self.response = packages_pb2.GetBestVisibleResponse()
96
David Burger1e0fe232019-07-01 14:52:07 -060097 def _GetRequest(self, atom=None):
98 return packages_pb2.GetBestVisibleRequest(
99 atom=atom,
100 )
101
David Burger1e0fe232019-07-01 14:52:07 -0600102 def _MakeCpv(self, category, package, version):
103 unused = {
104 'cp': None,
105 'cpv': None,
106 'cpf': None,
107 'pv': None,
108 'version_no_rev': None,
109 'rev': None,
110 }
111 return portage_util.CPV(
112 category=category,
113 package=package,
114 version=version,
115 **unused
116 )
117
Alex Klein231d2da2019-07-22 16:44:45 -0600118 def testValidateOnly(self):
119 """Sanity check that a validate only call does not execute any logic."""
120 patch = self.PatchObject(packages_service, 'get_best_visible')
121
122 request = self._GetRequest(atom='chromeos-chrome')
123 packages_controller.GetBestVisible(request, self.response,
124 self.validate_only_config)
125 patch.assert_not_called()
126
David Burger1e0fe232019-07-01 14:52:07 -0600127 def testNoAtomFails(self):
128 """No atom provided should fail."""
129 request = self._GetRequest()
David Burger1e0fe232019-07-01 14:52:07 -0600130 with self.assertRaises(cros_build_lib.DieSystemExit):
Alex Klein231d2da2019-07-22 16:44:45 -0600131 packages_controller.GetBestVisible(request, self.response,
132 self.api_config)
David Burger1e0fe232019-07-01 14:52:07 -0600133
134 def testSuccess(self):
135 """Test overall success, argument handling, result forwarding."""
136 cpv = self._MakeCpv('category', 'package', 'version')
137 self.PatchObject(packages_service, 'get_best_visible', return_value=cpv)
138
139 request = self._GetRequest(atom='chromeos-chrome')
David Burger1e0fe232019-07-01 14:52:07 -0600140
Alex Klein231d2da2019-07-22 16:44:45 -0600141 packages_controller.GetBestVisible(request, self.response, self.api_config)
David Burger1e0fe232019-07-01 14:52:07 -0600142
Alex Klein231d2da2019-07-22 16:44:45 -0600143 package_info = self.response.package_info
David Burger1e0fe232019-07-01 14:52:07 -0600144 self.assertEqual(package_info.category, cpv.category)
145 self.assertEqual(package_info.package_name, cpv.package)
146 self.assertEqual(package_info.version, cpv.version)