blob: 085c5100038c941df590b760d09e2db82aba51e1 [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 Klein26e472b2020-03-10 14:35:01 -060011from chromite.lib import build_target_lib
Alex Klein7f768322023-01-18 15:34:01 -070012from chromite.lib import chroot_lib
Alex Klein18a60af2020-06-11 12:08:47 -060013from chromite.lib import cros_test_lib
Alex Klein7f768322023-01-18 15:34:01 -070014from chromite.lib import sysroot_lib
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040015from chromite.lib.parser import package_info
Alex Klein171da612019-08-06 14:00:42 -060016
17
Michael Mortensen9a73c322019-10-03 17:14:37 -060018class ParseChrootTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060019 """ParseChroot tests."""
Alex Klein171da612019-08-06 14:00:42 -060020
Alex Klein1699fab2022-09-08 08:46:06 -060021 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 = {
29 "USE": "useflag1 useflag2",
30 "FEATURES": "feature1 feature2",
31 "CHROME_ORIGIN": "LOCAL_SOURCE",
32 }
Alex Klein171da612019-08-06 14:00:42 -060033
Alex Klein1699fab2022-09-08 08:46:06 -060034 chroot_message = common_pb2.Chroot(
35 path=path,
36 cache_dir=cache_dir,
37 chrome_dir=chrome_root,
38 env={"use_flags": use_flags, "features": features},
39 )
Alex Klein171da612019-08-06 14:00:42 -060040
Alex Klein7f768322023-01-18 15:34:01 -070041 expected = chroot_lib.Chroot(
Alex Klein1699fab2022-09-08 08:46:06 -060042 path=path,
43 cache_dir=cache_dir,
44 chrome_root=chrome_root,
45 env=expected_env,
46 )
47 result = controller_util.ParseChroot(chroot_message)
Alex Klein171da612019-08-06 14:00:42 -060048
Alex Klein1699fab2022-09-08 08:46:06 -060049 self.assertEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -060050
Alex Klein1699fab2022-09-08 08:46:06 -060051 def testWrongMessage(self):
52 """Test invalid message type given."""
53 with self.assertRaises(AssertionError):
54 controller_util.ParseChroot(common_pb2.BuildTarget())
55
Alex Klein171da612019-08-06 14:00:42 -060056
George Engelbrechtc9a8e812021-06-16 18:14:17 -060057class ParseSysrootTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060058 """ParseSysroot tests."""
George Engelbrechtc9a8e812021-06-16 18:14:17 -060059
Alex Klein1699fab2022-09-08 08:46:06 -060060 def testSuccess(self):
61 """test successful handling case."""
62 path = "/build/rare_pokemon"
63 sysroot_message = sysroot_pb2.Sysroot(path=path)
Alex Klein7f768322023-01-18 15:34:01 -070064 expected = sysroot_lib.Sysroot(path=path)
Alex Klein1699fab2022-09-08 08:46:06 -060065 result = controller_util.ParseSysroot(sysroot_message)
66 self.assertEqual(expected, result)
George Engelbrechtc9a8e812021-06-16 18:14:17 -060067
Alex Klein1699fab2022-09-08 08:46:06 -060068 def testWrongMessage(self):
69 with self.assertRaises(AssertionError):
70 controller_util.ParseSysroot(common_pb2.BuildTarget())
71
Alex Klein171da612019-08-06 14:00:42 -060072
73class ParseBuildTargetTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060074 """ParseBuildTarget tests."""
Alex Klein171da612019-08-06 14:00:42 -060075
Alex Klein1699fab2022-09-08 08:46:06 -060076 def testSuccess(self):
77 """Test successful handling case."""
78 name = "board"
79 build_target_message = common_pb2.BuildTarget(name=name)
80 expected = build_target_lib.BuildTarget(name)
81 result = controller_util.ParseBuildTarget(build_target_message)
Alex Klein171da612019-08-06 14:00:42 -060082
Alex Klein1699fab2022-09-08 08:46:06 -060083 self.assertEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -060084
Alex Klein1699fab2022-09-08 08:46:06 -060085 def testParseProfile(self):
86 """Test the parsing of a profile."""
87 name = "build-target-name"
88 profile = "profile"
89 build_target_msg = common_pb2.BuildTarget(name=name)
90 profile_msg = sysroot_pb2.Profile(name=profile)
Alex Klein26e472b2020-03-10 14:35:01 -060091
Alex Klein1699fab2022-09-08 08:46:06 -060092 expected = build_target_lib.BuildTarget(name, profile=profile)
93 result = controller_util.ParseBuildTarget(
94 build_target_msg, profile_message=profile_msg
95 )
Alex Klein26e472b2020-03-10 14:35:01 -060096
Alex Klein1699fab2022-09-08 08:46:06 -060097 self.assertEqual(expected, result)
Alex Klein26e472b2020-03-10 14:35:01 -060098
Alex Klein1699fab2022-09-08 08:46:06 -060099 def testWrongMessage(self):
100 """Test invalid message type given."""
101 with self.assertRaises(AssertionError):
102 controller_util.ParseBuildTarget(
103 build_api_test_pb2.TestRequestMessage()
104 )
Alex Klein171da612019-08-06 14:00:42 -0600105
106
107class ParseBuildTargetsTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600108 """ParseBuildTargets tests."""
Alex Klein171da612019-08-06 14:00:42 -0600109
Alex Klein1699fab2022-09-08 08:46:06 -0600110 def testSuccess(self):
111 """Test successful handling case."""
112 names = ["foo", "bar", "baz"]
113 message = build_api_test_pb2.TestRequestMessage()
114 for name in names:
115 message.build_targets.add().name = name
Alex Klein171da612019-08-06 14:00:42 -0600116
Alex Klein1699fab2022-09-08 08:46:06 -0600117 result = controller_util.ParseBuildTargets(message.build_targets)
Alex Klein171da612019-08-06 14:00:42 -0600118
Alex Klein1699fab2022-09-08 08:46:06 -0600119 expected = [build_target_lib.BuildTarget(name) for name in names]
120 self.assertCountEqual(expected, result)
Alex Klein171da612019-08-06 14:00:42 -0600121
Alex Klein1699fab2022-09-08 08:46:06 -0600122 def testWrongMessage(self):
123 """Wrong message type handling."""
124 message = common_pb2.Chroot()
125 message.env.use_flags.add().flag = "foo"
126 message.env.use_flags.add().flag = "bar"
Alex Klein171da612019-08-06 14:00:42 -0600127
Alex Klein1699fab2022-09-08 08:46:06 -0600128 with self.assertRaises(AssertionError):
129 controller_util.ParseBuildTargets(message.env.use_flags)
Alex Kleina9d500b2019-04-22 15:37:51 -0600130
131
Alex Kleina9d500b2019-04-22 15:37:51 -0600132class PackageInfoToCPVTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600133 """PackageInfoToCPV tests."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600134
Alex Klein1699fab2022-09-08 08:46:06 -0600135 def testAllFields(self):
136 """Quick check CPV fields."""
137 pi = common_pb2.PackageInfo()
138 pi.package_name = "pkg"
139 pi.category = "cat"
140 pi.version = "2.0.0"
Alex Kleina9d500b2019-04-22 15:37:51 -0600141
Alex Klein1699fab2022-09-08 08:46:06 -0600142 cpv = controller_util.PackageInfoToCPV(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600143
Alex Klein1699fab2022-09-08 08:46:06 -0600144 self.assertEqual("pkg", cpv.package)
145 self.assertEqual("cat", cpv.category)
146 self.assertEqual("2.0.0", cpv.version)
Alex Kleina9d500b2019-04-22 15:37:51 -0600147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 def testNoPackageInfo(self):
149 """Test no package info given."""
150 self.assertIsNone(controller_util.PackageInfoToCPV(None))
Alex Kleina9d500b2019-04-22 15:37:51 -0600151
Alex Klein1699fab2022-09-08 08:46:06 -0600152 def testNoPackageName(self):
153 """Test no package name given."""
154 pi = common_pb2.PackageInfo()
155 pi.category = "cat"
156 pi.version = "2.0.0"
Alex Kleina9d500b2019-04-22 15:37:51 -0600157
Alex Klein1699fab2022-09-08 08:46:06 -0600158 self.assertIsNone(controller_util.PackageInfoToCPV(pi))
Alex Kleina9d500b2019-04-22 15:37:51 -0600159
160
161class PackageInfoToStringTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600162 """PackageInfoToString tests."""
Alex Kleina9d500b2019-04-22 15:37:51 -0600163
Alex Klein1699fab2022-09-08 08:46:06 -0600164 def testAllFields(self):
165 """Test all fields present."""
166 pi = common_pb2.PackageInfo()
167 pi.package_name = "pkg"
168 pi.category = "cat"
169 pi.version = "2.0.0"
Alex Kleina9d500b2019-04-22 15:37:51 -0600170
Alex Klein1699fab2022-09-08 08:46:06 -0600171 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600172
Alex Klein1699fab2022-09-08 08:46:06 -0600173 self.assertEqual("cat/pkg-2.0.0", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600174
Alex Klein1699fab2022-09-08 08:46:06 -0600175 def testNoVersion(self):
176 """Test no version provided."""
177 pi = common_pb2.PackageInfo()
178 pi.package_name = "pkg"
179 pi.category = "cat"
Alex Kleina9d500b2019-04-22 15:37:51 -0600180
Alex Klein1699fab2022-09-08 08:46:06 -0600181 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600182
Alex Klein1699fab2022-09-08 08:46:06 -0600183 self.assertEqual("cat/pkg", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600184
Alex Klein1699fab2022-09-08 08:46:06 -0600185 def testPackageOnly(self):
186 """Test no version provided."""
187 pi = common_pb2.PackageInfo()
188 pi.package_name = "pkg"
Alex Kleina9d500b2019-04-22 15:37:51 -0600189
Alex Klein1699fab2022-09-08 08:46:06 -0600190 cpv_str = controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600191
Alex Klein1699fab2022-09-08 08:46:06 -0600192 self.assertEqual("pkg", cpv_str)
Alex Kleina9d500b2019-04-22 15:37:51 -0600193
Alex Klein1699fab2022-09-08 08:46:06 -0600194 def testNoPackageName(self):
195 """Test no package name given."""
196 pi = common_pb2.PackageInfo()
Alex Kleina9d500b2019-04-22 15:37:51 -0600197
Alex Klein1699fab2022-09-08 08:46:06 -0600198 with self.assertRaises(ValueError):
199 controller_util.PackageInfoToString(pi)
Alex Kleina9d500b2019-04-22 15:37:51 -0600200
201
Alex Klein1e68a8e2020-10-06 17:25:11 -0600202def test_serialize_package_info():
Alex Klein1699fab2022-09-08 08:46:06 -0600203 pkg_info = package_info.parse("foo/bar-1.2.3-r4")
204 pkg_info_msg = common_pb2.PackageInfo()
205 controller_util.serialize_package_info(pkg_info, pkg_info_msg)
206 assert pkg_info_msg.category == "foo"
207 assert pkg_info_msg.package_name == "bar"
208 assert pkg_info_msg.version == "1.2.3-r4"
Alex Klein1e68a8e2020-10-06 17:25:11 -0600209
210
211def test_deserialize_package_info():
Alex Klein1699fab2022-09-08 08:46:06 -0600212 pkg_info_msg = common_pb2.PackageInfo()
213 pkg_info_msg.category = "foo"
214 pkg_info_msg.package_name = "bar"
215 pkg_info_msg.version = "1.2.3-r4"
216 pkg_info = controller_util.deserialize_package_info(pkg_info_msg)
217 assert pkg_info.cpvr == "foo/bar-1.2.3-r4"
Lizzy Presland29e62452022-01-05 21:58:21 +0000218
219
Lizzy Preslandfebffa72022-02-24 23:38:58 +0000220def test_retrieve_package_log_paths():
Alex Klein1699fab2022-09-08 08:46:06 -0600221 packages = [
222 package_info.parse("foo/bar%d-1.0-r1" % num) for num in range(1, 4)
223 ]
224 output_proto = sysroot_pb2.InstallPackagesResponse()
Alex Klein7f768322023-01-18 15:34:01 -0700225 target_sysroot = sysroot_lib.Sysroot(path="/path/to/sysroot")
Alex Klein1699fab2022-09-08 08:46:06 -0600226 controller_util.retrieve_package_log_paths(
227 packages, output_proto, target_sysroot
228 )
229 assert len(output_proto.failed_package_data) == 3