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