blob: df12121acb70c934002355693f95b826ffe4019e [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Kleina9d500b2019-04-22 15:37:51 -06002# 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
Greg Edelston1f5deb62023-03-31 14:22:08 -06007from pathlib import Path
8
Alex Kleina9d500b2019-04-22 15:37:51 -06009from chromite.api.controller import controller_util
Alex Klein171da612019-08-06 14:00:42 -060010from chromite.api.gen.chromite.api import build_api_test_pb2
Alex Klein26e472b2020-03-10 14:35:01 -060011from chromite.api.gen.chromite.api import sysroot_pb2
Alex Kleina9d500b2019-04-22 15:37:51 -060012from chromite.api.gen.chromiumos import common_pb2
Alex Klein247d7922023-01-18 15:36:02 -070013from chromite.lib import binpkg
Alex Klein26e472b2020-03-10 14:35:01 -060014from chromite.lib import build_target_lib
Alex Klein7f768322023-01-18 15:34:01 -070015from chromite.lib import chroot_lib
Alex Klein18a60af2020-06-11 12:08:47 -060016from chromite.lib import cros_test_lib
Alex Klein7f768322023-01-18 15:34:01 -070017from chromite.lib import sysroot_lib
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040018from chromite.lib.parser import package_info
Alex Klein171da612019-08-06 14:00:42 -060019
20
Michael Mortensen9a73c322019-10-03 17:14:37 -060021class ParseChrootTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060022 """ParseChroot tests."""
Alex Klein171da612019-08-06 14:00:42 -060023
Alex Klein1699fab2022-09-08 08:46:06 -060024 def testSuccess(self):
25 """Test successful handling case."""
26 path = "/chroot/path"
27 cache_dir = "/cache/dir"
28 chrome_root = "/chrome/root"
29 use_flags = [{"flag": "useflag1"}, {"flag": "useflag2"}]
30 features = [{"feature": "feature1"}, {"feature": "feature2"}]
31 expected_env = {
32 "USE": "useflag1 useflag2",
33 "FEATURES": "feature1 feature2",
34 "CHROME_ORIGIN": "LOCAL_SOURCE",
35 }
Alex Klein171da612019-08-06 14:00:42 -060036
Alex Klein1699fab2022-09-08 08:46:06 -060037 chroot_message = common_pb2.Chroot(
38 path=path,
39 cache_dir=cache_dir,
40 chrome_dir=chrome_root,
41 env={"use_flags": use_flags, "features": features},
42 )
Alex Klein171da612019-08-06 14:00:42 -060043
Alex Klein7f768322023-01-18 15:34:01 -070044 expected = chroot_lib.Chroot(
Alex Klein1699fab2022-09-08 08:46:06 -060045 path=path,
46 cache_dir=cache_dir,
47 chrome_root=chrome_root,
48 env=expected_env,
49 )
50 result = controller_util.ParseChroot(chroot_message)
Alex Klein171da612019-08-06 14:00:42 -060051
Alex Klein1699fab2022-09-08 08:46:06 -060052 self.assertEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -060053
Alex Klein1699fab2022-09-08 08:46:06 -060054 def testWrongMessage(self):
55 """Test invalid message type given."""
56 with self.assertRaises(AssertionError):
57 controller_util.ParseChroot(common_pb2.BuildTarget())
58
Alex Klein171da612019-08-06 14:00:42 -060059
George Engelbrechtc9a8e812021-06-16 18:14:17 -060060class ParseSysrootTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060061 """ParseSysroot tests."""
George Engelbrechtc9a8e812021-06-16 18:14:17 -060062
Alex Klein1699fab2022-09-08 08:46:06 -060063 def testSuccess(self):
64 """test successful handling case."""
65 path = "/build/rare_pokemon"
66 sysroot_message = sysroot_pb2.Sysroot(path=path)
Alex Klein7f768322023-01-18 15:34:01 -070067 expected = sysroot_lib.Sysroot(path=path)
Alex Klein1699fab2022-09-08 08:46:06 -060068 result = controller_util.ParseSysroot(sysroot_message)
69 self.assertEqual(expected, result)
George Engelbrechtc9a8e812021-06-16 18:14:17 -060070
Alex Klein1699fab2022-09-08 08:46:06 -060071 def testWrongMessage(self):
72 with self.assertRaises(AssertionError):
73 controller_util.ParseSysroot(common_pb2.BuildTarget())
74
Alex Klein171da612019-08-06 14:00:42 -060075
76class ParseBuildTargetTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060077 """ParseBuildTarget tests."""
Alex Klein171da612019-08-06 14:00:42 -060078
Alex Klein1699fab2022-09-08 08:46:06 -060079 def testSuccess(self):
80 """Test successful handling case."""
81 name = "board"
82 build_target_message = common_pb2.BuildTarget(name=name)
83 expected = build_target_lib.BuildTarget(name)
84 result = controller_util.ParseBuildTarget(build_target_message)
Alex Klein171da612019-08-06 14:00:42 -060085
Alex Klein1699fab2022-09-08 08:46:06 -060086 self.assertEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -060087
Alex Klein1699fab2022-09-08 08:46:06 -060088 def testParseProfile(self):
89 """Test the parsing of a profile."""
90 name = "build-target-name"
91 profile = "profile"
92 build_target_msg = common_pb2.BuildTarget(name=name)
93 profile_msg = sysroot_pb2.Profile(name=profile)
Alex Klein26e472b2020-03-10 14:35:01 -060094
Alex Klein1699fab2022-09-08 08:46:06 -060095 expected = build_target_lib.BuildTarget(name, profile=profile)
96 result = controller_util.ParseBuildTarget(
97 build_target_msg, profile_message=profile_msg
98 )
Alex Klein26e472b2020-03-10 14:35:01 -060099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 self.assertEqual(expected, result)
Alex Klein26e472b2020-03-10 14:35:01 -0600101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 def testWrongMessage(self):
103 """Test invalid message type given."""
104 with self.assertRaises(AssertionError):
105 controller_util.ParseBuildTarget(
106 build_api_test_pb2.TestRequestMessage()
107 )
Alex Klein171da612019-08-06 14:00:42 -0600108
109
110class ParseBuildTargetsTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600111 """ParseBuildTargets tests."""
Alex Klein171da612019-08-06 14:00:42 -0600112
Alex Klein1699fab2022-09-08 08:46:06 -0600113 def testSuccess(self):
114 """Test successful handling case."""
115 names = ["foo", "bar", "baz"]
116 message = build_api_test_pb2.TestRequestMessage()
117 for name in names:
118 message.build_targets.add().name = name
Alex Klein171da612019-08-06 14:00:42 -0600119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 result = controller_util.ParseBuildTargets(message.build_targets)
Alex Klein171da612019-08-06 14:00:42 -0600121
Alex Klein1699fab2022-09-08 08:46:06 -0600122 expected = [build_target_lib.BuildTarget(name) for name in names]
123 self.assertCountEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -0600124
Alex Klein1699fab2022-09-08 08:46:06 -0600125 def testWrongMessage(self):
126 """Wrong message type handling."""
127 message = common_pb2.Chroot()
128 message.env.use_flags.add().flag = "foo"
129 message.env.use_flags.add().flag = "bar"
Alex Klein171da612019-08-06 14:00:42 -0600130
Alex Klein1699fab2022-09-08 08:46:06 -0600131 with self.assertRaises(AssertionError):
132 controller_util.ParseBuildTargets(message.env.use_flags)
Alex Kleina9d500b2019-04-22 15:37:51 -0600133
134
Alex Kleina9d500b2019-04-22 15:37:51 -0600135class PackageInfoToCPVTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600136 """PackageInfoToCPV tests."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600137
Alex Klein1699fab2022-09-08 08:46:06 -0600138 def testAllFields(self):
139 """Quick check CPV fields."""
140 pi = common_pb2.PackageInfo()
141 pi.package_name = "pkg"
142 pi.category = "cat"
143 pi.version = "2.0.0"
Alex Kleina9d500b2019-04-22 15:37:51 -0600144
Alex Klein1699fab2022-09-08 08:46:06 -0600145 cpv = controller_util.PackageInfoToCPV(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600146
Alex Klein1699fab2022-09-08 08:46:06 -0600147 self.assertEqual("pkg", cpv.package)
148 self.assertEqual("cat", cpv.category)
149 self.assertEqual("2.0.0", cpv.version)
Alex Kleina9d500b2019-04-22 15:37:51 -0600150
Alex Klein1699fab2022-09-08 08:46:06 -0600151 def testNoPackageInfo(self):
152 """Test no package info given."""
153 self.assertIsNone(controller_util.PackageInfoToCPV(None))
Alex Kleina9d500b2019-04-22 15:37:51 -0600154
Alex Klein1699fab2022-09-08 08:46:06 -0600155 def testNoPackageName(self):
156 """Test no package name given."""
157 pi = common_pb2.PackageInfo()
158 pi.category = "cat"
159 pi.version = "2.0.0"
Alex Kleina9d500b2019-04-22 15:37:51 -0600160
Alex Klein1699fab2022-09-08 08:46:06 -0600161 self.assertIsNone(controller_util.PackageInfoToCPV(pi))
Alex Kleina9d500b2019-04-22 15:37:51 -0600162
163
164class PackageInfoToStringTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600165 """PackageInfoToString tests."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600166
Alex Klein1699fab2022-09-08 08:46:06 -0600167 def testAllFields(self):
168 """Test all fields present."""
169 pi = common_pb2.PackageInfo()
170 pi.package_name = "pkg"
171 pi.category = "cat"
172 pi.version = "2.0.0"
Alex Kleina9d500b2019-04-22 15:37:51 -0600173
Alex Klein1699fab2022-09-08 08:46:06 -0600174 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600175
Alex Klein1699fab2022-09-08 08:46:06 -0600176 self.assertEqual("cat/pkg-2.0.0", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600177
Alex Klein1699fab2022-09-08 08:46:06 -0600178 def testNoVersion(self):
179 """Test no version provided."""
180 pi = common_pb2.PackageInfo()
181 pi.package_name = "pkg"
182 pi.category = "cat"
Alex Kleina9d500b2019-04-22 15:37:51 -0600183
Alex Klein1699fab2022-09-08 08:46:06 -0600184 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600185
Alex Klein1699fab2022-09-08 08:46:06 -0600186 self.assertEqual("cat/pkg", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600187
Alex Klein1699fab2022-09-08 08:46:06 -0600188 def testPackageOnly(self):
189 """Test no version provided."""
190 pi = common_pb2.PackageInfo()
191 pi.package_name = "pkg"
Alex Kleina9d500b2019-04-22 15:37:51 -0600192
Alex Klein1699fab2022-09-08 08:46:06 -0600193 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600194
Alex Klein1699fab2022-09-08 08:46:06 -0600195 self.assertEqual("pkg", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600196
Alex Klein1699fab2022-09-08 08:46:06 -0600197 def testNoPackageName(self):
198 """Test no package name given."""
199 pi = common_pb2.PackageInfo()
Alex Kleina9d500b2019-04-22 15:37:51 -0600200
Alex Klein1699fab2022-09-08 08:46:06 -0600201 with self.assertRaises(ValueError):
202 controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600203
204
Alex Klein1e68a8e2020-10-06 17:25:11 -0600205def test_serialize_package_info():
Alex Klein1699fab2022-09-08 08:46:06 -0600206 pkg_info = package_info.parse("foo/bar-1.2.3-r4")
207 pkg_info_msg = common_pb2.PackageInfo()
208 controller_util.serialize_package_info(pkg_info, pkg_info_msg)
209 assert pkg_info_msg.category == "foo"
210 assert pkg_info_msg.package_name == "bar"
211 assert pkg_info_msg.version == "1.2.3-r4"
Alex Klein1e68a8e2020-10-06 17:25:11 -0600212
213
214def test_deserialize_package_info():
Alex Klein1699fab2022-09-08 08:46:06 -0600215 pkg_info_msg = common_pb2.PackageInfo()
216 pkg_info_msg.category = "foo"
217 pkg_info_msg.package_name = "bar"
218 pkg_info_msg.version = "1.2.3-r4"
219 pkg_info = controller_util.deserialize_package_info(pkg_info_msg)
220 assert pkg_info.cpvr == "foo/bar-1.2.3-r4"
Lizzy Presland29e62452022-01-05 21:58:21 +0000221
222
Lizzy Preslandfebffa72022-02-24 23:38:58 +0000223def test_retrieve_package_log_paths():
Alex Klein1699fab2022-09-08 08:46:06 -0600224 packages = [
225 package_info.parse("foo/bar%d-1.0-r1" % num) for num in range(1, 4)
226 ]
227 output_proto = sysroot_pb2.InstallPackagesResponse()
Alex Klein7f768322023-01-18 15:34:01 -0700228 target_sysroot = sysroot_lib.Sysroot(path="/path/to/sysroot")
Alex Klein1699fab2022-09-08 08:46:06 -0600229 controller_util.retrieve_package_log_paths(
230 packages, output_proto, target_sysroot
231 )
232 assert len(output_proto.failed_package_data) == 3
Alex Klein247d7922023-01-18 15:36:02 -0700233
234
235def test_package_index_info():
236 """Quick check converting to/from protobuf works."""
237 sha = "SHA"
238 number = 5
239 build_target_name = "build_target"
240 profile_name = "profile"
241 location = "location"
242
243 msg = common_pb2.PackageIndexInfo()
244 msg.snapshot_sha = sha
245 msg.snapshot_number = number
246 msg.build_target.name = build_target_name
247 msg.profile.name = profile_name
248 msg.location = location
249
250 obj = binpkg.PackageIndexInfo(
251 snapshot_sha=sha,
252 snapshot_number=number,
253 build_target=build_target_lib.BuildTarget(name=build_target_name),
254 profile=sysroot_lib.Profile(name=profile_name),
255 location=location,
256 )
257
258 assert obj == controller_util.deserialize_package_index_info(msg)
Greg Edelston1f5deb62023-03-31 14:22:08 -0600259
260
261class Pb2PathToPathlibPathTest(cros_test_lib.TestCase):
262 """Verify functionality for pb2_path_to_pathlib_path()."""
263
264 chroot = common_pb2.Chroot(path="/path/to/chroot")
265
266 @staticmethod
267 def create_pb2_path(path: str, inside: bool) -> common_pb2.Path:
268 """Helper function to create a common_pb2.Path."""
269 location = (
270 common_pb2.Path.Location.INSIDE
271 if inside
272 else common_pb2.Path.Location.OUTSIDE
273 )
274 return common_pb2.Path(path=path, location=location)
275
276 def test_relative_inside(self):
277 """Verify that passing in a relative path inside the chroot fails"""
278 pb2_path = self.create_pb2_path(path="usr/bin", inside=True)
279 with self.assertRaises(ValueError):
280 controller_util.pb2_path_to_pathlib_path(
281 pb2_path, chroot=self.chroot
282 )
283
284 def test_relative_outside(self):
285 """Verify that passing in a relative path outside the chroot fails"""
286 pb2_path = self.create_pb2_path(path="usr/bin", inside=False)
287 with self.assertRaises(ValueError):
288 controller_util.pb2_path_to_pathlib_path(
289 pb2_path, chroot=self.chroot
290 )
291
292 def test_inside_with_chroot(self):
293 """Verify that we can convert an inside path with a chroot."""
294 pb2_path = self.create_pb2_path(path="/usr/bin", inside=True)
295 pathlib_path = controller_util.pb2_path_to_pathlib_path(
296 pb2_path, chroot=self.chroot
297 )
298 self.assertEqual(pathlib_path, Path("/path/to/chroot/usr/bin"))
299
300 def test_outside_with_chroot(self):
301 """Verify that we can convert an outside path with a chroot."""
302 pb2_path = self.create_pb2_path(path="/usr/bin", inside=False)
303 pathlib_path = controller_util.pb2_path_to_pathlib_path(
304 pb2_path, chroot=self.chroot
305 )
306 self.assertEqual(pathlib_path, Path("/usr/bin"))
307
308 def test_inside_without_chroot(self):
309 """Verify that we cannot convert an inside path without a chroot."""
310 pb2_path = self.create_pb2_path(path="/usr/bin", inside=True)
311 with self.assertRaises(ValueError):
312 controller_util.pb2_path_to_pathlib_path(pb2_path)
313
314 def test_outside_without_chroot(self):
315 """Verify that we can convert an outside path without a chroot."""
316 pb2_path = self.create_pb2_path(path="/usr/bin", inside=False)
317 pathlib_path = controller_util.pb2_path_to_pathlib_path(pb2_path)
318 self.assertEqual(pathlib_path, Path("/usr/bin"))