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 | |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 7 | from chromite.api.controller import controller_util |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 8 | from chromite.api.gen.chromite.api import build_api_test_pb2 |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 9 | from chromite.api.gen.chromite.api import sysroot_pb2 |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 10 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | 247d792 | 2023-01-18 15:36:02 -0700 | [diff] [blame] | 11 | from chromite.lib import binpkg |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 12 | from chromite.lib import build_target_lib |
Alex Klein | 7f76832 | 2023-01-18 15:34:01 -0700 | [diff] [blame] | 13 | from chromite.lib import chroot_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 14 | from chromite.lib import cros_test_lib |
Alex Klein | 7f76832 | 2023-01-18 15:34:01 -0700 | [diff] [blame] | 15 | from chromite.lib import sysroot_lib |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 16 | from chromite.lib.parser import package_info |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 17 | |
| 18 | |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 19 | class ParseChrootTest(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | """ParseChroot tests.""" |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 21 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 22 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 34 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 41 | |
Alex Klein | 7f76832 | 2023-01-18 15:34:01 -0700 | [diff] [blame] | 42 | expected = chroot_lib.Chroot( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 49 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 50 | self.assertEqual(expected, result) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 51 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 52 | def testWrongMessage(self): |
| 53 | """Test invalid message type given.""" |
| 54 | with self.assertRaises(AssertionError): |
| 55 | controller_util.ParseChroot(common_pb2.BuildTarget()) |
| 56 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 57 | |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 58 | class ParseSysrootTest(cros_test_lib.MockTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 59 | """ParseSysroot tests.""" |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 60 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 61 | def testSuccess(self): |
| 62 | """test successful handling case.""" |
| 63 | path = "/build/rare_pokemon" |
| 64 | sysroot_message = sysroot_pb2.Sysroot(path=path) |
Alex Klein | 7f76832 | 2023-01-18 15:34:01 -0700 | [diff] [blame] | 65 | expected = sysroot_lib.Sysroot(path=path) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 66 | result = controller_util.ParseSysroot(sysroot_message) |
| 67 | self.assertEqual(expected, result) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 68 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | def testWrongMessage(self): |
| 70 | with self.assertRaises(AssertionError): |
| 71 | controller_util.ParseSysroot(common_pb2.BuildTarget()) |
| 72 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 73 | |
| 74 | class ParseBuildTargetTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | """ParseBuildTarget tests.""" |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | self.assertEqual(expected, result) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 85 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 86 | 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 Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 92 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | expected = build_target_lib.BuildTarget(name, profile=profile) |
| 94 | result = controller_util.ParseBuildTarget( |
| 95 | build_target_msg, profile_message=profile_msg |
| 96 | ) |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 97 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 98 | self.assertEqual(expected, result) |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 99 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 106 | |
| 107 | |
| 108 | class ParseBuildTargetsTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 109 | """ParseBuildTargets tests.""" |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 110 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 111 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | result = controller_util.ParseBuildTargets(message.build_targets) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | expected = [build_target_lib.BuildTarget(name) for name in names] |
| 121 | self.assertCountEqual(expected, result) |
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 | 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 Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | with self.assertRaises(AssertionError): |
| 130 | controller_util.ParseBuildTargets(message.env.use_flags) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 131 | |
| 132 | |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 133 | class PackageInfoToCPVTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | """PackageInfoToCPV tests.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 135 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 136 | 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 Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 142 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 143 | cpv = controller_util.PackageInfoToCPV(pi) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | self.assertEqual("pkg", cpv.package) |
| 146 | self.assertEqual("cat", cpv.category) |
| 147 | self.assertEqual("2.0.0", cpv.version) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 148 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 149 | def testNoPackageInfo(self): |
| 150 | """Test no package info given.""" |
| 151 | self.assertIsNone(controller_util.PackageInfoToCPV(None)) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 152 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 153 | 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 Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 158 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 159 | self.assertIsNone(controller_util.PackageInfoToCPV(pi)) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 160 | |
| 161 | |
| 162 | class PackageInfoToStringTest(cros_test_lib.TestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | """PackageInfoToString tests.""" |
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 | 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 Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 171 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | cpv_str = controller_util.PackageInfoToString(pi) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 173 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | self.assertEqual("cat/pkg-2.0.0", cpv_str) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 175 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 176 | def testNoVersion(self): |
| 177 | """Test no version provided.""" |
| 178 | pi = common_pb2.PackageInfo() |
| 179 | pi.package_name = "pkg" |
| 180 | pi.category = "cat" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 181 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 182 | cpv_str = controller_util.PackageInfoToString(pi) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 183 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 184 | self.assertEqual("cat/pkg", cpv_str) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 185 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | def testPackageOnly(self): |
| 187 | """Test no version provided.""" |
| 188 | pi = common_pb2.PackageInfo() |
| 189 | pi.package_name = "pkg" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 190 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 191 | cpv_str = controller_util.PackageInfoToString(pi) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 192 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 193 | self.assertEqual("pkg", cpv_str) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 194 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | def testNoPackageName(self): |
| 196 | """Test no package name given.""" |
| 197 | pi = common_pb2.PackageInfo() |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 198 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 199 | with self.assertRaises(ValueError): |
| 200 | controller_util.PackageInfoToString(pi) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 201 | |
| 202 | |
Alex Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 203 | def test_serialize_package_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 204 | 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 Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 210 | |
| 211 | |
| 212 | def test_deserialize_package_info(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 213 | 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 Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 219 | |
| 220 | |
Lizzy Presland | febffa7 | 2022-02-24 23:38:58 +0000 | [diff] [blame] | 221 | def test_retrieve_package_log_paths(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 222 | 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 Klein | 7f76832 | 2023-01-18 15:34:01 -0700 | [diff] [blame] | 226 | target_sysroot = sysroot_lib.Sysroot(path="/path/to/sysroot") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 227 | controller_util.retrieve_package_log_paths( |
| 228 | packages, output_proto, target_sysroot |
| 229 | ) |
| 230 | assert len(output_proto.failed_package_data) == 3 |
Alex Klein | 247d792 | 2023-01-18 15:36:02 -0700 | [diff] [blame] | 231 | |
| 232 | |
| 233 | def 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) |