blob: 545681cb3be26dcd3df47743e46001c387b98985 [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
Brian Norris41f247b2023-06-30 11:09:40 -070016from chromite.lib import cros_build_lib
Alex Klein18a60af2020-06-11 12:08:47 -060017from chromite.lib import cros_test_lib
Alex Klein7f768322023-01-18 15:34:01 -070018from chromite.lib import sysroot_lib
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040019from chromite.lib.parser import package_info
Alex Klein171da612019-08-06 14:00:42 -060020
21
Michael Mortensen9a73c322019-10-03 17:14:37 -060022class ParseChrootTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060023 """ParseChroot tests."""
Alex Klein171da612019-08-06 14:00:42 -060024
Alex Klein1699fab2022-09-08 08:46:06 -060025 def testSuccess(self):
26 """Test successful handling case."""
27 path = "/chroot/path"
28 cache_dir = "/cache/dir"
29 chrome_root = "/chrome/root"
30 use_flags = [{"flag": "useflag1"}, {"flag": "useflag2"}]
31 features = [{"feature": "feature1"}, {"feature": "feature2"}]
32 expected_env = {
33 "USE": "useflag1 useflag2",
34 "FEATURES": "feature1 feature2",
35 "CHROME_ORIGIN": "LOCAL_SOURCE",
36 }
Alex Klein171da612019-08-06 14:00:42 -060037
Alex Klein1699fab2022-09-08 08:46:06 -060038 chroot_message = common_pb2.Chroot(
39 path=path,
40 cache_dir=cache_dir,
41 chrome_dir=chrome_root,
42 env={"use_flags": use_flags, "features": features},
43 )
Alex Klein171da612019-08-06 14:00:42 -060044
Alex Klein7f768322023-01-18 15:34:01 -070045 expected = chroot_lib.Chroot(
Alex Klein1699fab2022-09-08 08:46:06 -060046 path=path,
47 cache_dir=cache_dir,
48 chrome_root=chrome_root,
49 env=expected_env,
50 )
51 result = controller_util.ParseChroot(chroot_message)
Alex Klein171da612019-08-06 14:00:42 -060052
Alex Klein1699fab2022-09-08 08:46:06 -060053 self.assertEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -060054
Alex Klein1699fab2022-09-08 08:46:06 -060055 def testWrongMessage(self):
56 """Test invalid message type given."""
57 with self.assertRaises(AssertionError):
58 controller_util.ParseChroot(common_pb2.BuildTarget())
59
Alex Klein171da612019-08-06 14:00:42 -060060
George Engelbrechtc9a8e812021-06-16 18:14:17 -060061class ParseSysrootTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060062 """ParseSysroot tests."""
George Engelbrechtc9a8e812021-06-16 18:14:17 -060063
Alex Klein1699fab2022-09-08 08:46:06 -060064 def testSuccess(self):
65 """test successful handling case."""
66 path = "/build/rare_pokemon"
67 sysroot_message = sysroot_pb2.Sysroot(path=path)
Alex Klein7f768322023-01-18 15:34:01 -070068 expected = sysroot_lib.Sysroot(path=path)
Alex Klein1699fab2022-09-08 08:46:06 -060069 result = controller_util.ParseSysroot(sysroot_message)
70 self.assertEqual(expected, result)
George Engelbrechtc9a8e812021-06-16 18:14:17 -060071
Alex Klein1699fab2022-09-08 08:46:06 -060072 def testWrongMessage(self):
73 with self.assertRaises(AssertionError):
74 controller_util.ParseSysroot(common_pb2.BuildTarget())
75
Alex Klein171da612019-08-06 14:00:42 -060076
77class ParseBuildTargetTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060078 """ParseBuildTarget tests."""
Alex Klein171da612019-08-06 14:00:42 -060079
Alex Klein1699fab2022-09-08 08:46:06 -060080 def testSuccess(self):
81 """Test successful handling case."""
82 name = "board"
83 build_target_message = common_pb2.BuildTarget(name=name)
84 expected = build_target_lib.BuildTarget(name)
85 result = controller_util.ParseBuildTarget(build_target_message)
Alex Klein171da612019-08-06 14:00:42 -060086
Alex Klein1699fab2022-09-08 08:46:06 -060087 self.assertEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -060088
Alex Klein1699fab2022-09-08 08:46:06 -060089 def testParseProfile(self):
90 """Test the parsing of a profile."""
91 name = "build-target-name"
92 profile = "profile"
93 build_target_msg = common_pb2.BuildTarget(name=name)
94 profile_msg = sysroot_pb2.Profile(name=profile)
Alex Klein26e472b2020-03-10 14:35:01 -060095
Alex Klein1699fab2022-09-08 08:46:06 -060096 expected = build_target_lib.BuildTarget(name, profile=profile)
97 result = controller_util.ParseBuildTarget(
98 build_target_msg, profile_message=profile_msg
99 )
Alex Klein26e472b2020-03-10 14:35:01 -0600100
Alex Klein1699fab2022-09-08 08:46:06 -0600101 self.assertEqual(expected, result)
Alex Klein26e472b2020-03-10 14:35:01 -0600102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 def testWrongMessage(self):
104 """Test invalid message type given."""
105 with self.assertRaises(AssertionError):
106 controller_util.ParseBuildTarget(
107 build_api_test_pb2.TestRequestMessage()
108 )
Alex Klein171da612019-08-06 14:00:42 -0600109
110
111class ParseBuildTargetsTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600112 """ParseBuildTargets tests."""
Alex Klein171da612019-08-06 14:00:42 -0600113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 def testSuccess(self):
115 """Test successful handling case."""
116 names = ["foo", "bar", "baz"]
117 message = build_api_test_pb2.TestRequestMessage()
118 for name in names:
119 message.build_targets.add().name = name
Alex Klein171da612019-08-06 14:00:42 -0600120
Alex Klein1699fab2022-09-08 08:46:06 -0600121 result = controller_util.ParseBuildTargets(message.build_targets)
Alex Klein171da612019-08-06 14:00:42 -0600122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 expected = [build_target_lib.BuildTarget(name) for name in names]
124 self.assertCountEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -0600125
Alex Klein1699fab2022-09-08 08:46:06 -0600126 def testWrongMessage(self):
127 """Wrong message type handling."""
128 message = common_pb2.Chroot()
129 message.env.use_flags.add().flag = "foo"
130 message.env.use_flags.add().flag = "bar"
Alex Klein171da612019-08-06 14:00:42 -0600131
Alex Klein1699fab2022-09-08 08:46:06 -0600132 with self.assertRaises(AssertionError):
133 controller_util.ParseBuildTargets(message.env.use_flags)
Alex Kleina9d500b2019-04-22 15:37:51 -0600134
135
Alex Kleina9d500b2019-04-22 15:37:51 -0600136class PackageInfoToStringTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600137 """PackageInfoToString tests."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600138
Alex Klein1699fab2022-09-08 08:46:06 -0600139 def testAllFields(self):
140 """Test all fields present."""
141 pi = common_pb2.PackageInfo()
142 pi.package_name = "pkg"
143 pi.category = "cat"
144 pi.version = "2.0.0"
Alex Kleina9d500b2019-04-22 15:37:51 -0600145
Alex Klein1699fab2022-09-08 08:46:06 -0600146 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 self.assertEqual("cat/pkg-2.0.0", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600149
Alex Klein1699fab2022-09-08 08:46:06 -0600150 def testNoVersion(self):
151 """Test no version provided."""
152 pi = common_pb2.PackageInfo()
153 pi.package_name = "pkg"
154 pi.category = "cat"
Alex Kleina9d500b2019-04-22 15:37:51 -0600155
Alex Klein1699fab2022-09-08 08:46:06 -0600156 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600157
Alex Klein1699fab2022-09-08 08:46:06 -0600158 self.assertEqual("cat/pkg", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600159
Alex Klein1699fab2022-09-08 08:46:06 -0600160 def testPackageOnly(self):
161 """Test no version provided."""
162 pi = common_pb2.PackageInfo()
163 pi.package_name = "pkg"
Alex Kleina9d500b2019-04-22 15:37:51 -0600164
Alex Klein1699fab2022-09-08 08:46:06 -0600165 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600166
Alex Klein1699fab2022-09-08 08:46:06 -0600167 self.assertEqual("pkg", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600168
Alex Klein1699fab2022-09-08 08:46:06 -0600169 def testNoPackageName(self):
170 """Test no package name given."""
171 pi = common_pb2.PackageInfo()
Alex Kleina9d500b2019-04-22 15:37:51 -0600172
Alex Klein1699fab2022-09-08 08:46:06 -0600173 with self.assertRaises(ValueError):
174 controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600175
176
Alex Klein1e68a8e2020-10-06 17:25:11 -0600177def test_serialize_package_info():
Alex Klein1699fab2022-09-08 08:46:06 -0600178 pkg_info = package_info.parse("foo/bar-1.2.3-r4")
179 pkg_info_msg = common_pb2.PackageInfo()
180 controller_util.serialize_package_info(pkg_info, pkg_info_msg)
181 assert pkg_info_msg.category == "foo"
182 assert pkg_info_msg.package_name == "bar"
183 assert pkg_info_msg.version == "1.2.3-r4"
Alex Klein1e68a8e2020-10-06 17:25:11 -0600184
185
186def test_deserialize_package_info():
Alex Klein1699fab2022-09-08 08:46:06 -0600187 pkg_info_msg = common_pb2.PackageInfo()
188 pkg_info_msg.category = "foo"
189 pkg_info_msg.package_name = "bar"
190 pkg_info_msg.version = "1.2.3-r4"
191 pkg_info = controller_util.deserialize_package_info(pkg_info_msg)
192 assert pkg_info.cpvr == "foo/bar-1.2.3-r4"
Lizzy Presland29e62452022-01-05 21:58:21 +0000193
194
Lizzy Preslandfebffa72022-02-24 23:38:58 +0000195def test_retrieve_package_log_paths():
Alex Klein1699fab2022-09-08 08:46:06 -0600196 packages = [
197 package_info.parse("foo/bar%d-1.0-r1" % num) for num in range(1, 4)
198 ]
199 output_proto = sysroot_pb2.InstallPackagesResponse()
Alex Klein7f768322023-01-18 15:34:01 -0700200 target_sysroot = sysroot_lib.Sysroot(path="/path/to/sysroot")
Alex Klein1699fab2022-09-08 08:46:06 -0600201 controller_util.retrieve_package_log_paths(
202 packages, output_proto, target_sysroot
203 )
204 assert len(output_proto.failed_package_data) == 3
Alex Klein247d7922023-01-18 15:36:02 -0700205
206
207def test_package_index_info():
208 """Quick check converting to/from protobuf works."""
209 sha = "SHA"
210 number = 5
211 build_target_name = "build_target"
212 profile_name = "profile"
213 location = "location"
214
215 msg = common_pb2.PackageIndexInfo()
216 msg.snapshot_sha = sha
217 msg.snapshot_number = number
218 msg.build_target.name = build_target_name
219 msg.profile.name = profile_name
220 msg.location = location
221
222 obj = binpkg.PackageIndexInfo(
223 snapshot_sha=sha,
224 snapshot_number=number,
225 build_target=build_target_lib.BuildTarget(name=build_target_name),
226 profile=sysroot_lib.Profile(name=profile_name),
227 location=location,
228 )
229
230 assert obj == controller_util.deserialize_package_index_info(msg)
Greg Edelston1f5deb62023-03-31 14:22:08 -0600231
232
Brian Norris41f247b2023-06-30 11:09:40 -0700233class Pb2PathToPathlibPathTest(cros_test_lib.MockTestCase):
Greg Edelston1f5deb62023-03-31 14:22:08 -0600234 """Verify functionality for pb2_path_to_pathlib_path()."""
235
Brian Norris41f247b2023-06-30 11:09:40 -0700236 chroot = common_pb2.Chroot(path="/path/to/chroot", out_path="/path/to/out")
237
238 def setUp(self):
239 self.PatchObject(cros_build_lib, "IsInsideChroot", return_value=False)
Greg Edelston1f5deb62023-03-31 14:22:08 -0600240
241 @staticmethod
242 def create_pb2_path(path: str, inside: bool) -> common_pb2.Path:
243 """Helper function to create a common_pb2.Path."""
244 location = (
245 common_pb2.Path.Location.INSIDE
246 if inside
247 else common_pb2.Path.Location.OUTSIDE
248 )
249 return common_pb2.Path(path=path, location=location)
250
251 def test_relative_inside(self):
252 """Verify that passing in a relative path inside the chroot fails"""
253 pb2_path = self.create_pb2_path(path="usr/bin", inside=True)
254 with self.assertRaises(ValueError):
255 controller_util.pb2_path_to_pathlib_path(
256 pb2_path, chroot=self.chroot
257 )
258
259 def test_relative_outside(self):
260 """Verify that passing in a relative path outside the chroot fails"""
261 pb2_path = self.create_pb2_path(path="usr/bin", inside=False)
262 with self.assertRaises(ValueError):
263 controller_util.pb2_path_to_pathlib_path(
264 pb2_path, chroot=self.chroot
265 )
266
267 def test_inside_with_chroot(self):
268 """Verify that we can convert an inside path with a chroot."""
269 pb2_path = self.create_pb2_path(path="/usr/bin", inside=True)
270 pathlib_path = controller_util.pb2_path_to_pathlib_path(
271 pb2_path, chroot=self.chroot
272 )
273 self.assertEqual(pathlib_path, Path("/path/to/chroot/usr/bin"))
274
275 def test_outside_with_chroot(self):
276 """Verify that we can convert an outside path with a chroot."""
277 pb2_path = self.create_pb2_path(path="/usr/bin", inside=False)
278 pathlib_path = controller_util.pb2_path_to_pathlib_path(
279 pb2_path, chroot=self.chroot
280 )
281 self.assertEqual(pathlib_path, Path("/usr/bin"))
282
283 def test_inside_without_chroot(self):
284 """Verify that we cannot convert an inside path without a chroot."""
285 pb2_path = self.create_pb2_path(path="/usr/bin", inside=True)
286 with self.assertRaises(ValueError):
287 controller_util.pb2_path_to_pathlib_path(pb2_path)
288
289 def test_outside_without_chroot(self):
290 """Verify that we can convert an outside path without a chroot."""
291 pb2_path = self.create_pb2_path(path="/usr/bin", inside=False)
292 pathlib_path = controller_util.pb2_path_to_pathlib_path(pb2_path)
293 self.assertEqual(pathlib_path, Path("/usr/bin"))