Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 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 |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 12 | from chromite.lib import cros_build_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 13 | from chromite.lib import cros_test_lib |
| 14 | from chromite.lib.parser import package_info |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 15 | from chromite.lib.chroot_lib import Chroot |
Lizzy Presland | febffa7 | 2022-02-24 23:38:58 +0000 | [diff] [blame] | 16 | from chromite.lib.sysroot_lib import Sysroot |
| 17 | from chromite.lib.sysroot_lib import PackageInstallError |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 18 | |
| 19 | |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 20 | class ParseChrootTest(cros_test_lib.MockTestCase): |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 21 | """ParseChroot tests.""" |
| 22 | |
| 23 | def testSuccess(self): |
| 24 | """Test successful handling case.""" |
| 25 | path = '/chroot/path' |
| 26 | cache_dir = '/cache/dir' |
| 27 | chrome_root = '/chrome/root' |
| 28 | use_flags = [{'flag': 'useflag1'}, {'flag': 'useflag2'}] |
| 29 | features = [{'feature': 'feature1'}, {'feature': 'feature2'}] |
| 30 | expected_env = {'USE': 'useflag1 useflag2', |
Alex Klein | b7485bb | 2019-09-19 13:23:37 -0600 | [diff] [blame] | 31 | 'FEATURES': 'feature1 feature2', |
| 32 | 'CHROME_ORIGIN': 'LOCAL_SOURCE'} |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 33 | |
| 34 | chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, |
| 35 | chrome_dir=chrome_root, |
| 36 | env={'use_flags': use_flags, |
| 37 | 'features': features}) |
| 38 | |
| 39 | expected = Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root, |
| 40 | env=expected_env) |
| 41 | result = controller_util.ParseChroot(chroot_message) |
| 42 | |
| 43 | self.assertEqual(expected, result) |
| 44 | |
| 45 | def testWrongMessage(self): |
| 46 | """Test invalid message type given.""" |
| 47 | with self.assertRaises(AssertionError): |
| 48 | controller_util.ParseChroot(common_pb2.BuildTarget()) |
| 49 | |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 50 | class ParseSysrootTest(cros_test_lib.MockTestCase): |
| 51 | """ParseSysroot tests.""" |
| 52 | |
| 53 | def testSuccess(self): |
| 54 | """test successful handling case.""" |
| 55 | path = '/build/rare_pokemon' |
| 56 | sysroot_message = sysroot_pb2.Sysroot(path=path) |
Lizzy Presland | febffa7 | 2022-02-24 23:38:58 +0000 | [diff] [blame] | 57 | expected = Sysroot(path=path) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 58 | result = controller_util.ParseSysroot(sysroot_message) |
| 59 | self.assertEqual(expected, result) |
| 60 | |
| 61 | def testWrongMessage(self): |
| 62 | with self.assertRaises(AssertionError): |
| 63 | controller_util.ParseSysroot(common_pb2.BuildTarget()) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 64 | |
| 65 | class ParseBuildTargetTest(cros_test_lib.TestCase): |
| 66 | """ParseBuildTarget tests.""" |
| 67 | |
| 68 | def testSuccess(self): |
| 69 | """Test successful handling case.""" |
| 70 | name = 'board' |
| 71 | build_target_message = common_pb2.BuildTarget(name=name) |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 72 | expected = build_target_lib.BuildTarget(name) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 73 | result = controller_util.ParseBuildTarget(build_target_message) |
| 74 | |
| 75 | self.assertEqual(expected, result) |
| 76 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 77 | def testParseProfile(self): |
| 78 | """Test the parsing of a profile.""" |
| 79 | name = 'build-target-name' |
| 80 | profile = 'profile' |
| 81 | build_target_msg = common_pb2.BuildTarget(name=name) |
| 82 | profile_msg = sysroot_pb2.Profile(name=profile) |
| 83 | |
| 84 | expected = build_target_lib.BuildTarget(name, profile=profile) |
| 85 | result = controller_util.ParseBuildTarget( |
| 86 | build_target_msg, profile_message=profile_msg) |
| 87 | |
| 88 | self.assertEqual(expected, result) |
| 89 | |
| 90 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 91 | def testWrongMessage(self): |
| 92 | """Test invalid message type given.""" |
| 93 | with self.assertRaises(AssertionError): |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 94 | controller_util.ParseBuildTarget(build_api_test_pb2.TestRequestMessage()) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 95 | |
| 96 | |
| 97 | class ParseBuildTargetsTest(cros_test_lib.TestCase): |
| 98 | """ParseBuildTargets tests.""" |
| 99 | |
| 100 | def testSuccess(self): |
| 101 | """Test successful handling case.""" |
| 102 | names = ['foo', 'bar', 'baz'] |
| 103 | message = build_api_test_pb2.TestRequestMessage() |
| 104 | for name in names: |
| 105 | message.build_targets.add().name = name |
| 106 | |
| 107 | result = controller_util.ParseBuildTargets(message.build_targets) |
| 108 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 109 | expected = [build_target_lib.BuildTarget(name) for name in names] |
| 110 | self.assertCountEqual(expected, result) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 111 | |
| 112 | def testWrongMessage(self): |
| 113 | """Wrong message type handling.""" |
| 114 | message = common_pb2.Chroot() |
| 115 | message.env.use_flags.add().flag = 'foo' |
| 116 | message.env.use_flags.add().flag = 'bar' |
| 117 | |
| 118 | with self.assertRaises(AssertionError): |
| 119 | controller_util.ParseBuildTargets(message.env.use_flags) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 120 | |
| 121 | |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 122 | class PackageInfoToCPVTest(cros_test_lib.TestCase): |
| 123 | """PackageInfoToCPV tests.""" |
| 124 | |
| 125 | def testAllFields(self): |
| 126 | """Quick sanity check it's working properly.""" |
| 127 | pi = common_pb2.PackageInfo() |
| 128 | pi.package_name = 'pkg' |
| 129 | pi.category = 'cat' |
| 130 | pi.version = '2.0.0' |
| 131 | |
| 132 | cpv = controller_util.PackageInfoToCPV(pi) |
| 133 | |
| 134 | self.assertEqual('pkg', cpv.package) |
| 135 | self.assertEqual('cat', cpv.category) |
| 136 | self.assertEqual('2.0.0', cpv.version) |
| 137 | |
| 138 | def testNoPackageInfo(self): |
| 139 | """Test no package info given.""" |
| 140 | self.assertIsNone(controller_util.PackageInfoToCPV(None)) |
| 141 | |
| 142 | def testNoPackageName(self): |
| 143 | """Test no package name given.""" |
| 144 | pi = common_pb2.PackageInfo() |
| 145 | pi.category = 'cat' |
| 146 | pi.version = '2.0.0' |
| 147 | |
| 148 | self.assertIsNone(controller_util.PackageInfoToCPV(pi)) |
| 149 | |
| 150 | |
| 151 | class PackageInfoToStringTest(cros_test_lib.TestCase): |
| 152 | """PackageInfoToString tests.""" |
| 153 | |
| 154 | def testAllFields(self): |
| 155 | """Test all fields present.""" |
| 156 | pi = common_pb2.PackageInfo() |
| 157 | pi.package_name = 'pkg' |
| 158 | pi.category = 'cat' |
| 159 | pi.version = '2.0.0' |
| 160 | |
| 161 | cpv_str = controller_util.PackageInfoToString(pi) |
| 162 | |
| 163 | self.assertEqual('cat/pkg-2.0.0', cpv_str) |
| 164 | |
| 165 | def testNoVersion(self): |
| 166 | """Test no version provided.""" |
| 167 | pi = common_pb2.PackageInfo() |
| 168 | pi.package_name = 'pkg' |
| 169 | pi.category = 'cat' |
| 170 | |
| 171 | cpv_str = controller_util.PackageInfoToString(pi) |
| 172 | |
| 173 | self.assertEqual('cat/pkg', cpv_str) |
| 174 | |
| 175 | def testPackageOnly(self): |
| 176 | """Test no version provided.""" |
| 177 | pi = common_pb2.PackageInfo() |
| 178 | pi.package_name = 'pkg' |
| 179 | |
| 180 | cpv_str = controller_util.PackageInfoToString(pi) |
| 181 | |
| 182 | self.assertEqual('pkg', cpv_str) |
| 183 | |
| 184 | def testNoPackageName(self): |
| 185 | """Test no package name given.""" |
| 186 | pi = common_pb2.PackageInfo() |
| 187 | |
| 188 | with self.assertRaises(ValueError): |
| 189 | controller_util.PackageInfoToString(pi) |
| 190 | |
| 191 | |
| 192 | class CPVToStringTest(cros_test_lib.TestCase): |
| 193 | """CPVToString tests.""" |
| 194 | |
| 195 | def testTranslations(self): |
| 196 | """Test standard translations used.""" |
| 197 | cases = [ |
| 198 | 'cat/pkg-2.0.0-r1', |
| 199 | 'cat/pkg-2.0.0', |
| 200 | 'cat/pkg', |
| 201 | 'pkg', |
| 202 | ] |
| 203 | |
| 204 | for case in cases: |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 205 | cpv = package_info.SplitCPV(case, strict=False) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 206 | # We should end up with as much info as is available, so we should see |
| 207 | # the original value in each case. |
| 208 | self.assertEqual(case, controller_util.CPVToString(cpv)) |
| 209 | |
| 210 | def testInvalidCPV(self): |
| 211 | """Test invalid CPV object.""" |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 212 | cpv = package_info.SplitCPV('', strict=False) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 213 | with self.assertRaises(ValueError): |
| 214 | controller_util.CPVToString(cpv) |
Alex Klein | 1e68a8e | 2020-10-06 17:25:11 -0600 | [diff] [blame] | 215 | |
| 216 | |
| 217 | def test_serialize_package_info(): |
| 218 | pkg_info = package_info.parse('foo/bar-1.2.3-r4') |
| 219 | pkg_info_msg = common_pb2.PackageInfo() |
| 220 | controller_util.serialize_package_info(pkg_info, pkg_info_msg) |
| 221 | assert pkg_info_msg.category == 'foo' |
| 222 | assert pkg_info_msg.package_name == 'bar' |
| 223 | assert pkg_info_msg.version == '1.2.3-r4' |
| 224 | |
| 225 | |
| 226 | def test_deserialize_package_info(): |
| 227 | pkg_info_msg = common_pb2.PackageInfo() |
| 228 | pkg_info_msg.category = 'foo' |
| 229 | pkg_info_msg.package_name = 'bar' |
| 230 | pkg_info_msg.version = '1.2.3-r4' |
| 231 | pkg_info = controller_util.deserialize_package_info(pkg_info_msg) |
| 232 | assert pkg_info.cpvr == 'foo/bar-1.2.3-r4' |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 233 | |
| 234 | |
Lizzy Presland | febffa7 | 2022-02-24 23:38:58 +0000 | [diff] [blame] | 235 | def test_retrieve_package_log_paths(): |
| 236 | error = PackageInstallError( |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 237 | msg='Failed to install 3 packages', |
| 238 | result=cros_build_lib.CommandResult(), |
| 239 | packages=[package_info.parse('foo/bar%d-1.0-r1' % num) |
| 240 | for num in range(1, 4)]) |
| 241 | output_proto = sysroot_pb2.InstallPackagesResponse() |
Lizzy Presland | febffa7 | 2022-02-24 23:38:58 +0000 | [diff] [blame] | 242 | target_sysroot = Sysroot(path='/path/to/sysroot') |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 243 | controller_util.retrieve_package_log_paths(error, |
| 244 | output_proto, |
Lizzy Presland | febffa7 | 2022-02-24 23:38:58 +0000 | [diff] [blame] | 245 | target_sysroot) |
Lizzy Presland | 29e6245 | 2022-01-05 21:58:21 +0000 | [diff] [blame] | 246 | assert len(output_proto.failed_package_data) == 3 |