blob: e955f24fa4c7089c1fe4e3b4d862db2aa1dc40d1 [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
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 Klein247d7922023-01-18 15:36:02 -070011from chromite.lib import binpkg
Alex Klein26e472b2020-03-10 14:35:01 -060012from chromite.lib import build_target_lib
Alex Klein7f768322023-01-18 15:34:01 -070013from chromite.lib import chroot_lib
Alex Klein18a60af2020-06-11 12:08:47 -060014from chromite.lib import cros_test_lib
Alex Klein7f768322023-01-18 15:34:01 -070015from chromite.lib import sysroot_lib
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040016from chromite.lib.parser import package_info
Alex Klein171da612019-08-06 14:00:42 -060017
18
Michael Mortensen9a73c322019-10-03 17:14:37 -060019class ParseChrootTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060020 """ParseChroot tests."""
Alex Klein171da612019-08-06 14:00:42 -060021
Alex Klein1699fab2022-09-08 08:46:06 -060022 def testSuccess(self):
23 """Test successful handling case."""
24 path = "/chroot/path"
25 cache_dir = "/cache/dir"
26 chrome_root = "/chrome/root"
27 use_flags = [{"flag": "useflag1"}, {"flag": "useflag2"}]
28 features = [{"feature": "feature1"}, {"feature": "feature2"}]
29 expected_env = {
30 "USE": "useflag1 useflag2",
31 "FEATURES": "feature1 feature2",
32 "CHROME_ORIGIN": "LOCAL_SOURCE",
33 }
Alex Klein171da612019-08-06 14:00:42 -060034
Alex Klein1699fab2022-09-08 08:46:06 -060035 chroot_message = common_pb2.Chroot(
36 path=path,
37 cache_dir=cache_dir,
38 chrome_dir=chrome_root,
39 env={"use_flags": use_flags, "features": features},
40 )
Alex Klein171da612019-08-06 14:00:42 -060041
Alex Klein7f768322023-01-18 15:34:01 -070042 expected = chroot_lib.Chroot(
Alex Klein1699fab2022-09-08 08:46:06 -060043 path=path,
44 cache_dir=cache_dir,
45 chrome_root=chrome_root,
46 env=expected_env,
47 )
48 result = controller_util.ParseChroot(chroot_message)
Alex Klein171da612019-08-06 14:00:42 -060049
Alex Klein1699fab2022-09-08 08:46:06 -060050 self.assertEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -060051
Alex Klein1699fab2022-09-08 08:46:06 -060052 def testWrongMessage(self):
53 """Test invalid message type given."""
54 with self.assertRaises(AssertionError):
55 controller_util.ParseChroot(common_pb2.BuildTarget())
56
Alex Klein171da612019-08-06 14:00:42 -060057
George Engelbrechtc9a8e812021-06-16 18:14:17 -060058class ParseSysrootTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060059 """ParseSysroot tests."""
George Engelbrechtc9a8e812021-06-16 18:14:17 -060060
Alex Klein1699fab2022-09-08 08:46:06 -060061 def testSuccess(self):
62 """test successful handling case."""
63 path = "/build/rare_pokemon"
64 sysroot_message = sysroot_pb2.Sysroot(path=path)
Alex Klein7f768322023-01-18 15:34:01 -070065 expected = sysroot_lib.Sysroot(path=path)
Alex Klein1699fab2022-09-08 08:46:06 -060066 result = controller_util.ParseSysroot(sysroot_message)
67 self.assertEqual(expected, result)
George Engelbrechtc9a8e812021-06-16 18:14:17 -060068
Alex Klein1699fab2022-09-08 08:46:06 -060069 def testWrongMessage(self):
70 with self.assertRaises(AssertionError):
71 controller_util.ParseSysroot(common_pb2.BuildTarget())
72
Alex Klein171da612019-08-06 14:00:42 -060073
74class ParseBuildTargetTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060075 """ParseBuildTarget tests."""
Alex Klein171da612019-08-06 14:00:42 -060076
Alex Klein1699fab2022-09-08 08:46:06 -060077 def testSuccess(self):
78 """Test successful handling case."""
79 name = "board"
80 build_target_message = common_pb2.BuildTarget(name=name)
81 expected = build_target_lib.BuildTarget(name)
82 result = controller_util.ParseBuildTarget(build_target_message)
Alex Klein171da612019-08-06 14:00:42 -060083
Alex Klein1699fab2022-09-08 08:46:06 -060084 self.assertEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -060085
Alex Klein1699fab2022-09-08 08:46:06 -060086 def testParseProfile(self):
87 """Test the parsing of a profile."""
88 name = "build-target-name"
89 profile = "profile"
90 build_target_msg = common_pb2.BuildTarget(name=name)
91 profile_msg = sysroot_pb2.Profile(name=profile)
Alex Klein26e472b2020-03-10 14:35:01 -060092
Alex Klein1699fab2022-09-08 08:46:06 -060093 expected = build_target_lib.BuildTarget(name, profile=profile)
94 result = controller_util.ParseBuildTarget(
95 build_target_msg, profile_message=profile_msg
96 )
Alex Klein26e472b2020-03-10 14:35:01 -060097
Alex Klein1699fab2022-09-08 08:46:06 -060098 self.assertEqual(expected, result)
Alex Klein26e472b2020-03-10 14:35:01 -060099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 def testWrongMessage(self):
101 """Test invalid message type given."""
102 with self.assertRaises(AssertionError):
103 controller_util.ParseBuildTarget(
104 build_api_test_pb2.TestRequestMessage()
105 )
Alex Klein171da612019-08-06 14:00:42 -0600106
107
108class ParseBuildTargetsTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600109 """ParseBuildTargets tests."""
Alex Klein171da612019-08-06 14:00:42 -0600110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 def testSuccess(self):
112 """Test successful handling case."""
113 names = ["foo", "bar", "baz"]
114 message = build_api_test_pb2.TestRequestMessage()
115 for name in names:
116 message.build_targets.add().name = name
Alex Klein171da612019-08-06 14:00:42 -0600117
Alex Klein1699fab2022-09-08 08:46:06 -0600118 result = controller_util.ParseBuildTargets(message.build_targets)
Alex Klein171da612019-08-06 14:00:42 -0600119
Alex Klein1699fab2022-09-08 08:46:06 -0600120 expected = [build_target_lib.BuildTarget(name) for name in names]
121 self.assertCountEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -0600122
Alex Klein1699fab2022-09-08 08:46:06 -0600123 def testWrongMessage(self):
124 """Wrong message type handling."""
125 message = common_pb2.Chroot()
126 message.env.use_flags.add().flag = "foo"
127 message.env.use_flags.add().flag = "bar"
Alex Klein171da612019-08-06 14:00:42 -0600128
Alex Klein1699fab2022-09-08 08:46:06 -0600129 with self.assertRaises(AssertionError):
130 controller_util.ParseBuildTargets(message.env.use_flags)
Alex Kleina9d500b2019-04-22 15:37:51 -0600131
132
Alex Kleina9d500b2019-04-22 15:37:51 -0600133class PackageInfoToCPVTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600134 """PackageInfoToCPV tests."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600135
Alex Klein1699fab2022-09-08 08:46:06 -0600136 def testAllFields(self):
137 """Quick check CPV fields."""
138 pi = common_pb2.PackageInfo()
139 pi.package_name = "pkg"
140 pi.category = "cat"
141 pi.version = "2.0.0"
Alex Kleina9d500b2019-04-22 15:37:51 -0600142
Alex Klein1699fab2022-09-08 08:46:06 -0600143 cpv = controller_util.PackageInfoToCPV(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600144
Alex Klein1699fab2022-09-08 08:46:06 -0600145 self.assertEqual("pkg", cpv.package)
146 self.assertEqual("cat", cpv.category)
147 self.assertEqual("2.0.0", cpv.version)
Alex Kleina9d500b2019-04-22 15:37:51 -0600148
Alex Klein1699fab2022-09-08 08:46:06 -0600149 def testNoPackageInfo(self):
150 """Test no package info given."""
151 self.assertIsNone(controller_util.PackageInfoToCPV(None))
Alex Kleina9d500b2019-04-22 15:37:51 -0600152
Alex Klein1699fab2022-09-08 08:46:06 -0600153 def testNoPackageName(self):
154 """Test no package name given."""
155 pi = common_pb2.PackageInfo()
156 pi.category = "cat"
157 pi.version = "2.0.0"
Alex Kleina9d500b2019-04-22 15:37:51 -0600158
Alex Klein1699fab2022-09-08 08:46:06 -0600159 self.assertIsNone(controller_util.PackageInfoToCPV(pi))
Alex Kleina9d500b2019-04-22 15:37:51 -0600160
161
162class PackageInfoToStringTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600163 """PackageInfoToString tests."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600164
Alex Klein1699fab2022-09-08 08:46:06 -0600165 def testAllFields(self):
166 """Test all fields present."""
167 pi = common_pb2.PackageInfo()
168 pi.package_name = "pkg"
169 pi.category = "cat"
170 pi.version = "2.0.0"
Alex Kleina9d500b2019-04-22 15:37:51 -0600171
Alex Klein1699fab2022-09-08 08:46:06 -0600172 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600173
Alex Klein1699fab2022-09-08 08:46:06 -0600174 self.assertEqual("cat/pkg-2.0.0", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600175
Alex Klein1699fab2022-09-08 08:46:06 -0600176 def testNoVersion(self):
177 """Test no version provided."""
178 pi = common_pb2.PackageInfo()
179 pi.package_name = "pkg"
180 pi.category = "cat"
Alex Kleina9d500b2019-04-22 15:37:51 -0600181
Alex Klein1699fab2022-09-08 08:46:06 -0600182 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600183
Alex Klein1699fab2022-09-08 08:46:06 -0600184 self.assertEqual("cat/pkg", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600185
Alex Klein1699fab2022-09-08 08:46:06 -0600186 def testPackageOnly(self):
187 """Test no version provided."""
188 pi = common_pb2.PackageInfo()
189 pi.package_name = "pkg"
Alex Kleina9d500b2019-04-22 15:37:51 -0600190
Alex Klein1699fab2022-09-08 08:46:06 -0600191 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600192
Alex Klein1699fab2022-09-08 08:46:06 -0600193 self.assertEqual("pkg", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600194
Alex Klein1699fab2022-09-08 08:46:06 -0600195 def testNoPackageName(self):
196 """Test no package name given."""
197 pi = common_pb2.PackageInfo()
Alex Kleina9d500b2019-04-22 15:37:51 -0600198
Alex Klein1699fab2022-09-08 08:46:06 -0600199 with self.assertRaises(ValueError):
200 controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600201
202
Alex Klein1e68a8e2020-10-06 17:25:11 -0600203def test_serialize_package_info():
Alex Klein1699fab2022-09-08 08:46:06 -0600204 pkg_info = package_info.parse("foo/bar-1.2.3-r4")
205 pkg_info_msg = common_pb2.PackageInfo()
206 controller_util.serialize_package_info(pkg_info, pkg_info_msg)
207 assert pkg_info_msg.category == "foo"
208 assert pkg_info_msg.package_name == "bar"
209 assert pkg_info_msg.version == "1.2.3-r4"
Alex Klein1e68a8e2020-10-06 17:25:11 -0600210
211
212def test_deserialize_package_info():
Alex Klein1699fab2022-09-08 08:46:06 -0600213 pkg_info_msg = common_pb2.PackageInfo()
214 pkg_info_msg.category = "foo"
215 pkg_info_msg.package_name = "bar"
216 pkg_info_msg.version = "1.2.3-r4"
217 pkg_info = controller_util.deserialize_package_info(pkg_info_msg)
218 assert pkg_info.cpvr == "foo/bar-1.2.3-r4"
Lizzy Presland29e62452022-01-05 21:58:21 +0000219
220
Lizzy Preslandfebffa72022-02-24 23:38:58 +0000221def test_retrieve_package_log_paths():
Alex Klein1699fab2022-09-08 08:46:06 -0600222 packages = [
223 package_info.parse("foo/bar%d-1.0-r1" % num) for num in range(1, 4)
224 ]
225 output_proto = sysroot_pb2.InstallPackagesResponse()
Alex Klein7f768322023-01-18 15:34:01 -0700226 target_sysroot = sysroot_lib.Sysroot(path="/path/to/sysroot")
Alex Klein1699fab2022-09-08 08:46:06 -0600227 controller_util.retrieve_package_log_paths(
228 packages, output_proto, target_sysroot
229 )
230 assert len(output_proto.failed_package_data) == 3
Alex Klein247d7922023-01-18 15:36:02 -0700231
232
233def test_package_index_info():
234 """Quick check converting to/from protobuf works."""
235 sha = "SHA"
236 number = 5
237 build_target_name = "build_target"
238 profile_name = "profile"
239 location = "location"
240
241 msg = common_pb2.PackageIndexInfo()
242 msg.snapshot_sha = sha
243 msg.snapshot_number = number
244 msg.build_target.name = build_target_name
245 msg.profile.name = profile_name
246 msg.location = location
247
248 obj = binpkg.PackageIndexInfo(
249 snapshot_sha=sha,
250 snapshot_number=number,
251 build_target=build_target_lib.BuildTarget(name=build_target_name),
252 profile=sysroot_lib.Profile(name=profile_name),
253 location=location,
254 )
255
256 assert obj == controller_util.deserialize_package_index_info(msg)