Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 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 | |
Greg Edelston | 1f5deb6 | 2023-03-31 14:22:08 -0600 | [diff] [blame] | 7 | from pathlib import Path |
| 8 | |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 9 | from chromite.api.controller import controller_util |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 10 | from chromite.api.gen.chromite.api import build_api_test_pb2 |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 11 | from chromite.api.gen.chromite.api import sysroot_pb2 |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 12 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | 247d792 | 2023-01-18 15:36:02 -0700 | [diff] [blame] | 13 | from chromite.lib import binpkg |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 14 | from chromite.lib import build_target_lib |
Alex Klein | 7f76832 | 2023-01-18 15:34:01 -0700 | [diff] [blame] | 15 | from chromite.lib import chroot_lib |
Brian Norris | 41f247b | 2023-06-30 11:09:40 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 17 | from chromite.lib import cros_test_lib |
Alex Klein | 7f76832 | 2023-01-18 15:34:01 -0700 | [diff] [blame] | 18 | from chromite.lib import sysroot_lib |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 19 | from chromite.lib.parser import package_info |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 20 | |
| 21 | |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 22 | class ParseChrootTest(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 23 | """ParseChroot tests.""" |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 24 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 37 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 44 | |
Alex Klein | 7f76832 | 2023-01-18 15:34:01 -0700 | [diff] [blame] | 45 | expected = chroot_lib.Chroot( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 46 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 52 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | self.assertEqual(expected, result) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 54 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | def testWrongMessage(self): |
| 56 | """Test invalid message type given.""" |
| 57 | with self.assertRaises(AssertionError): |
| 58 | controller_util.ParseChroot(common_pb2.BuildTarget()) |
| 59 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 60 | |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 61 | class ParseSysrootTest(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | """ParseSysroot tests.""" |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 63 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | def testSuccess(self): |
| 65 | """test successful handling case.""" |
| 66 | path = "/build/rare_pokemon" |
| 67 | sysroot_message = sysroot_pb2.Sysroot(path=path) |
Alex Klein | 7f76832 | 2023-01-18 15:34:01 -0700 | [diff] [blame] | 68 | expected = sysroot_lib.Sysroot(path=path) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | result = controller_util.ParseSysroot(sysroot_message) |
| 70 | self.assertEqual(expected, result) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 71 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | def testWrongMessage(self): |
| 73 | with self.assertRaises(AssertionError): |
| 74 | controller_util.ParseSysroot(common_pb2.BuildTarget()) |
| 75 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 76 | |
| 77 | class ParseBuildTargetTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | """ParseBuildTarget tests.""" |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 79 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 80 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 86 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | self.assertEqual(expected, result) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 88 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 89 | 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 Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | expected = build_target_lib.BuildTarget(name, profile=profile) |
| 97 | result = controller_util.ParseBuildTarget( |
| 98 | build_target_msg, profile_message=profile_msg |
| 99 | ) |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 100 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 101 | self.assertEqual(expected, result) |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 102 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 109 | |
| 110 | |
| 111 | class ParseBuildTargetsTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 112 | """ParseBuildTargets tests.""" |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | result = controller_util.ParseBuildTargets(message.build_targets) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 122 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 123 | expected = [build_target_lib.BuildTarget(name) for name in names] |
| 124 | self.assertCountEqual(expected, result) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 125 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 131 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 132 | with self.assertRaises(AssertionError): |
| 133 | controller_util.ParseBuildTargets(message.env.use_flags) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 134 | |
| 135 | |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 136 | class PackageInfoToStringTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 137 | """PackageInfoToString tests.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 138 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | 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 Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 145 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 146 | cpv_str = controller_util.PackageInfoToString(pi) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 147 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 148 | self.assertEqual("cat/pkg-2.0.0", cpv_str) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 149 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | def testNoVersion(self): |
| 151 | """Test no version provided.""" |
| 152 | pi = common_pb2.PackageInfo() |
| 153 | pi.package_name = "pkg" |
| 154 | pi.category = "cat" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 155 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 156 | cpv_str = controller_util.PackageInfoToString(pi) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 157 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 158 | self.assertEqual("cat/pkg", cpv_str) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 159 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 160 | def testPackageOnly(self): |
| 161 | """Test no version provided.""" |
| 162 | pi = common_pb2.PackageInfo() |
| 163 | pi.package_name = "pkg" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 164 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | cpv_str = controller_util.PackageInfoToString(pi) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 166 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 167 | self.assertEqual("pkg", cpv_str) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 168 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 169 | def testNoPackageName(self): |
| 170 | """Test no package name given.""" |
| 171 | pi = common_pb2.PackageInfo() |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 172 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 173 | with self.assertRaises(ValueError): |
| 174 | controller_util.PackageInfoToString(pi) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 175 | |
| 176 | |
Alex Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 177 | def test_serialize_package_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | 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 Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 184 | |
| 185 | |
| 186 | def test_deserialize_package_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 187 | 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 Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 193 | |
| 194 | |
Lizzy Presland | febffa7 | 2022-02-24 23:38:58 +0000 | [diff] [blame] | 195 | def test_retrieve_package_log_paths(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 196 | 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 Klein | 7f76832 | 2023-01-18 15:34:01 -0700 | [diff] [blame] | 200 | target_sysroot = sysroot_lib.Sysroot(path="/path/to/sysroot") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 201 | controller_util.retrieve_package_log_paths( |
| 202 | packages, output_proto, target_sysroot |
| 203 | ) |
| 204 | assert len(output_proto.failed_package_data) == 3 |
Alex Klein | 247d792 | 2023-01-18 15:36:02 -0700 | [diff] [blame] | 205 | |
| 206 | |
| 207 | def 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 Edelston | 1f5deb6 | 2023-03-31 14:22:08 -0600 | [diff] [blame] | 231 | |
| 232 | |
Brian Norris | 41f247b | 2023-06-30 11:09:40 -0700 | [diff] [blame] | 233 | class Pb2PathToPathlibPathTest(cros_test_lib.MockTestCase): |
Greg Edelston | 1f5deb6 | 2023-03-31 14:22:08 -0600 | [diff] [blame] | 234 | """Verify functionality for pb2_path_to_pathlib_path().""" |
| 235 | |
Brian Norris | 41f247b | 2023-06-30 11:09:40 -0700 | [diff] [blame] | 236 | 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 Edelston | 1f5deb6 | 2023-03-31 14:22:08 -0600 | [diff] [blame] | 240 | |
| 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")) |