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