blob: 038e5d80cc4f5e81613a1c1b680d0fe053932048 [file] [log] [blame]
Alex Kleina9d500b2019-04-22 15:37:51 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""controller_util unittests."""
6
Alex Kleina9d500b2019-04-22 15:37:51 -06007from chromite.api.controller import controller_util
Alex Klein171da612019-08-06 14:00:42 -06008from chromite.api.gen.chromite.api import build_api_test_pb2
Alex Klein26e472b2020-03-10 14:35:01 -06009from chromite.api.gen.chromite.api import sysroot_pb2
Alex Kleina9d500b2019-04-22 15:37:51 -060010from chromite.api.gen.chromiumos import common_pb2
Alex Klein26e472b2020-03-10 14:35:01 -060011from chromite.lib import build_target_lib
Alex Klein18a60af2020-06-11 12:08:47 -060012from chromite.lib import cros_test_lib
13from chromite.lib.parser import package_info
Alex Klein171da612019-08-06 14:00:42 -060014from chromite.lib.chroot_lib import Chroot
George Engelbrechtc9a8e812021-06-16 18:14:17 -060015from chromite.lib.sysroot_lib import Sysroot
Alex Klein171da612019-08-06 14:00:42 -060016
17
Michael Mortensen9a73c322019-10-03 17:14:37 -060018class ParseChrootTest(cros_test_lib.MockTestCase):
Alex Klein171da612019-08-06 14:00:42 -060019 """ParseChroot tests."""
20
21 def testSuccess(self):
22 """Test successful handling case."""
23 path = '/chroot/path'
24 cache_dir = '/cache/dir'
25 chrome_root = '/chrome/root'
26 use_flags = [{'flag': 'useflag1'}, {'flag': 'useflag2'}]
27 features = [{'feature': 'feature1'}, {'feature': 'feature2'}]
28 expected_env = {'USE': 'useflag1 useflag2',
Alex Kleinb7485bb2019-09-19 13:23:37 -060029 'FEATURES': 'feature1 feature2',
30 'CHROME_ORIGIN': 'LOCAL_SOURCE'}
Alex Klein171da612019-08-06 14:00:42 -060031
32 chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir,
33 chrome_dir=chrome_root,
34 env={'use_flags': use_flags,
35 'features': features})
36
37 expected = Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root,
38 env=expected_env)
39 result = controller_util.ParseChroot(chroot_message)
40
41 self.assertEqual(expected, result)
42
43 def testWrongMessage(self):
44 """Test invalid message type given."""
45 with self.assertRaises(AssertionError):
46 controller_util.ParseChroot(common_pb2.BuildTarget())
47
George Engelbrechtc9a8e812021-06-16 18:14:17 -060048class ParseSysrootTest(cros_test_lib.MockTestCase):
49 """ParseSysroot tests."""
50
51 def testSuccess(self):
52 """test successful handling case."""
53 path = '/build/rare_pokemon'
54 sysroot_message = sysroot_pb2.Sysroot(path=path)
55 expected = Sysroot(path=path)
56 result = controller_util.ParseSysroot(sysroot_message)
57 self.assertEqual(expected, result)
58
59 def testWrongMessage(self):
60 with self.assertRaises(AssertionError):
61 controller_util.ParseSysroot(common_pb2.BuildTarget())
Alex Klein171da612019-08-06 14:00:42 -060062
63class ParseBuildTargetTest(cros_test_lib.TestCase):
64 """ParseBuildTarget tests."""
65
66 def testSuccess(self):
67 """Test successful handling case."""
68 name = 'board'
69 build_target_message = common_pb2.BuildTarget(name=name)
Alex Klein26e472b2020-03-10 14:35:01 -060070 expected = build_target_lib.BuildTarget(name)
Alex Klein171da612019-08-06 14:00:42 -060071 result = controller_util.ParseBuildTarget(build_target_message)
72
73 self.assertEqual(expected, result)
74
Alex Klein26e472b2020-03-10 14:35:01 -060075 def testParseProfile(self):
76 """Test the parsing of a profile."""
77 name = 'build-target-name'
78 profile = 'profile'
79 build_target_msg = common_pb2.BuildTarget(name=name)
80 profile_msg = sysroot_pb2.Profile(name=profile)
81
82 expected = build_target_lib.BuildTarget(name, profile=profile)
83 result = controller_util.ParseBuildTarget(
84 build_target_msg, profile_message=profile_msg)
85
86 self.assertEqual(expected, result)
87
88
Alex Klein171da612019-08-06 14:00:42 -060089 def testWrongMessage(self):
90 """Test invalid message type given."""
91 with self.assertRaises(AssertionError):
Alex Klein26e472b2020-03-10 14:35:01 -060092 controller_util.ParseBuildTarget(build_api_test_pb2.TestRequestMessage())
Alex Klein171da612019-08-06 14:00:42 -060093
94
95class ParseBuildTargetsTest(cros_test_lib.TestCase):
96 """ParseBuildTargets tests."""
97
98 def testSuccess(self):
99 """Test successful handling case."""
100 names = ['foo', 'bar', 'baz']
101 message = build_api_test_pb2.TestRequestMessage()
102 for name in names:
103 message.build_targets.add().name = name
104
105 result = controller_util.ParseBuildTargets(message.build_targets)
106
Alex Klein26e472b2020-03-10 14:35:01 -0600107 expected = [build_target_lib.BuildTarget(name) for name in names]
108 self.assertCountEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -0600109
110 def testWrongMessage(self):
111 """Wrong message type handling."""
112 message = common_pb2.Chroot()
113 message.env.use_flags.add().flag = 'foo'
114 message.env.use_flags.add().flag = 'bar'
115
116 with self.assertRaises(AssertionError):
117 controller_util.ParseBuildTargets(message.env.use_flags)
Alex Kleina9d500b2019-04-22 15:37:51 -0600118
119
120class CPVToPackageInfoTest(cros_test_lib.TestCase):
121 """CPVToPackageInfo tests."""
122
123 def testAllFields(self):
Alex Kleine1abe2c2019-08-14 10:29:46 -0600124 """Test handling when all fields present."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600125 pi = common_pb2.PackageInfo()
Alex Klein18a60af2020-06-11 12:08:47 -0600126 cpv = package_info.SplitCPV('cat/pkg-2.0.0', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600127
128 controller_util.CPVToPackageInfo(cpv, pi)
129 self.assertEqual('cat', pi.category)
130 self.assertEqual('pkg', pi.package_name)
131 self.assertEqual('2.0.0', pi.version)
132
133 def testNoVersion(self):
Alex Kleine1abe2c2019-08-14 10:29:46 -0600134 """Test handling when no version given."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600135 pi = common_pb2.PackageInfo()
Alex Klein18a60af2020-06-11 12:08:47 -0600136 cpv = package_info.SplitCPV('cat/pkg', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600137
138 controller_util.CPVToPackageInfo(cpv, pi)
139 self.assertEqual('cat', pi.category)
140 self.assertEqual('pkg', pi.package_name)
141 self.assertEqual('', pi.version)
142
143 def testPackageOnly(self):
Alex Kleine1abe2c2019-08-14 10:29:46 -0600144 """Test handling when only given the package name."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600145 pi = common_pb2.PackageInfo()
Alex Klein18a60af2020-06-11 12:08:47 -0600146 cpv = package_info.SplitCPV('pkg', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600147
148 controller_util.CPVToPackageInfo(cpv, pi)
149 self.assertEqual('', pi.category)
150 self.assertEqual('pkg', pi.package_name)
151 self.assertEqual('', pi.version)
152
153
154class PackageInfoToCPVTest(cros_test_lib.TestCase):
155 """PackageInfoToCPV tests."""
156
157 def testAllFields(self):
158 """Quick sanity check it's working properly."""
159 pi = common_pb2.PackageInfo()
160 pi.package_name = 'pkg'
161 pi.category = 'cat'
162 pi.version = '2.0.0'
163
164 cpv = controller_util.PackageInfoToCPV(pi)
165
166 self.assertEqual('pkg', cpv.package)
167 self.assertEqual('cat', cpv.category)
168 self.assertEqual('2.0.0', cpv.version)
169
170 def testNoPackageInfo(self):
171 """Test no package info given."""
172 self.assertIsNone(controller_util.PackageInfoToCPV(None))
173
174 def testNoPackageName(self):
175 """Test no package name given."""
176 pi = common_pb2.PackageInfo()
177 pi.category = 'cat'
178 pi.version = '2.0.0'
179
180 self.assertIsNone(controller_util.PackageInfoToCPV(pi))
181
182
183class PackageInfoToStringTest(cros_test_lib.TestCase):
184 """PackageInfoToString tests."""
185
186 def testAllFields(self):
187 """Test all fields present."""
188 pi = common_pb2.PackageInfo()
189 pi.package_name = 'pkg'
190 pi.category = 'cat'
191 pi.version = '2.0.0'
192
193 cpv_str = controller_util.PackageInfoToString(pi)
194
195 self.assertEqual('cat/pkg-2.0.0', cpv_str)
196
197 def testNoVersion(self):
198 """Test no version provided."""
199 pi = common_pb2.PackageInfo()
200 pi.package_name = 'pkg'
201 pi.category = 'cat'
202
203 cpv_str = controller_util.PackageInfoToString(pi)
204
205 self.assertEqual('cat/pkg', cpv_str)
206
207 def testPackageOnly(self):
208 """Test no version provided."""
209 pi = common_pb2.PackageInfo()
210 pi.package_name = 'pkg'
211
212 cpv_str = controller_util.PackageInfoToString(pi)
213
214 self.assertEqual('pkg', cpv_str)
215
216 def testNoPackageName(self):
217 """Test no package name given."""
218 pi = common_pb2.PackageInfo()
219
220 with self.assertRaises(ValueError):
221 controller_util.PackageInfoToString(pi)
222
223
224class CPVToStringTest(cros_test_lib.TestCase):
225 """CPVToString tests."""
226
227 def testTranslations(self):
228 """Test standard translations used."""
229 cases = [
230 'cat/pkg-2.0.0-r1',
231 'cat/pkg-2.0.0',
232 'cat/pkg',
233 'pkg',
234 ]
235
236 for case in cases:
Alex Klein18a60af2020-06-11 12:08:47 -0600237 cpv = package_info.SplitCPV(case, strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600238 # We should end up with as much info as is available, so we should see
239 # the original value in each case.
240 self.assertEqual(case, controller_util.CPVToString(cpv))
241
242 def testInvalidCPV(self):
243 """Test invalid CPV object."""
Alex Klein18a60af2020-06-11 12:08:47 -0600244 cpv = package_info.SplitCPV('', strict=False)
Alex Kleina9d500b2019-04-22 15:37:51 -0600245 with self.assertRaises(ValueError):
246 controller_util.CPVToString(cpv)
Alex Klein1e68a8e2020-10-06 17:25:11 -0600247
248
249def test_serialize_package_info():
250 pkg_info = package_info.parse('foo/bar-1.2.3-r4')
251 pkg_info_msg = common_pb2.PackageInfo()
252 controller_util.serialize_package_info(pkg_info, pkg_info_msg)
253 assert pkg_info_msg.category == 'foo'
254 assert pkg_info_msg.package_name == 'bar'
255 assert pkg_info_msg.version == '1.2.3-r4'
256
257
258def test_deserialize_package_info():
259 pkg_info_msg = common_pb2.PackageInfo()
260 pkg_info_msg.category = 'foo'
261 pkg_info_msg.package_name = 'bar'
262 pkg_info_msg.version = '1.2.3-r4'
263 pkg_info = controller_util.deserialize_package_info(pkg_info_msg)
264 assert pkg_info.cpvr == 'foo/bar-1.2.3-r4'