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