Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """controller_util unittests.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | from chromite.api.controller import controller_util |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 11 | from chromite.api.gen.chromite.api import build_api_test_pb2 |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 12 | from chromite.api.gen.chromite.api import sysroot_pb2 |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 13 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 14 | from chromite.lib import build_target_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 15 | from chromite.lib import cros_test_lib |
| 16 | from chromite.lib.parser import package_info |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 17 | from chromite.lib.chroot_lib import Chroot |
| 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 | |
| 50 | |
| 51 | class ParseBuildTargetTest(cros_test_lib.TestCase): |
| 52 | """ParseBuildTarget tests.""" |
| 53 | |
| 54 | def testSuccess(self): |
| 55 | """Test successful handling case.""" |
| 56 | name = 'board' |
| 57 | build_target_message = common_pb2.BuildTarget(name=name) |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 58 | expected = build_target_lib.BuildTarget(name) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 59 | result = controller_util.ParseBuildTarget(build_target_message) |
| 60 | |
| 61 | self.assertEqual(expected, result) |
| 62 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 63 | def testParseProfile(self): |
| 64 | """Test the parsing of a profile.""" |
| 65 | name = 'build-target-name' |
| 66 | profile = 'profile' |
| 67 | build_target_msg = common_pb2.BuildTarget(name=name) |
| 68 | profile_msg = sysroot_pb2.Profile(name=profile) |
| 69 | |
| 70 | expected = build_target_lib.BuildTarget(name, profile=profile) |
| 71 | result = controller_util.ParseBuildTarget( |
| 72 | build_target_msg, profile_message=profile_msg) |
| 73 | |
| 74 | self.assertEqual(expected, result) |
| 75 | |
| 76 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 77 | def testWrongMessage(self): |
| 78 | """Test invalid message type given.""" |
| 79 | with self.assertRaises(AssertionError): |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 80 | controller_util.ParseBuildTarget(build_api_test_pb2.TestRequestMessage()) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 81 | |
| 82 | |
| 83 | class ParseBuildTargetsTest(cros_test_lib.TestCase): |
| 84 | """ParseBuildTargets tests.""" |
| 85 | |
| 86 | def testSuccess(self): |
| 87 | """Test successful handling case.""" |
| 88 | names = ['foo', 'bar', 'baz'] |
| 89 | message = build_api_test_pb2.TestRequestMessage() |
| 90 | for name in names: |
| 91 | message.build_targets.add().name = name |
| 92 | |
| 93 | result = controller_util.ParseBuildTargets(message.build_targets) |
| 94 | |
Alex Klein | 26e472b | 2020-03-10 14:35:01 -0600 | [diff] [blame] | 95 | expected = [build_target_lib.BuildTarget(name) for name in names] |
| 96 | self.assertCountEqual(expected, result) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 97 | |
| 98 | def testWrongMessage(self): |
| 99 | """Wrong message type handling.""" |
| 100 | message = common_pb2.Chroot() |
| 101 | message.env.use_flags.add().flag = 'foo' |
| 102 | message.env.use_flags.add().flag = 'bar' |
| 103 | |
| 104 | with self.assertRaises(AssertionError): |
| 105 | controller_util.ParseBuildTargets(message.env.use_flags) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 106 | |
| 107 | |
| 108 | class CPVToPackageInfoTest(cros_test_lib.TestCase): |
| 109 | """CPVToPackageInfo tests.""" |
| 110 | |
| 111 | def testAllFields(self): |
Alex Klein | e1abe2c | 2019-08-14 10:29:46 -0600 | [diff] [blame] | 112 | """Test handling when all fields present.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 113 | pi = common_pb2.PackageInfo() |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 114 | cpv = package_info.SplitCPV('cat/pkg-2.0.0', strict=False) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 115 | |
| 116 | controller_util.CPVToPackageInfo(cpv, pi) |
| 117 | self.assertEqual('cat', pi.category) |
| 118 | self.assertEqual('pkg', pi.package_name) |
| 119 | self.assertEqual('2.0.0', pi.version) |
| 120 | |
| 121 | def testNoVersion(self): |
Alex Klein | e1abe2c | 2019-08-14 10:29:46 -0600 | [diff] [blame] | 122 | """Test handling when no version given.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 123 | pi = common_pb2.PackageInfo() |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 124 | cpv = package_info.SplitCPV('cat/pkg', strict=False) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 125 | |
| 126 | controller_util.CPVToPackageInfo(cpv, pi) |
| 127 | self.assertEqual('cat', pi.category) |
| 128 | self.assertEqual('pkg', pi.package_name) |
| 129 | self.assertEqual('', pi.version) |
| 130 | |
| 131 | def testPackageOnly(self): |
Alex Klein | e1abe2c | 2019-08-14 10:29:46 -0600 | [diff] [blame] | 132 | """Test handling when only given the package name.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 133 | pi = common_pb2.PackageInfo() |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 134 | cpv = package_info.SplitCPV('pkg', strict=False) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 135 | |
| 136 | controller_util.CPVToPackageInfo(cpv, pi) |
| 137 | self.assertEqual('', pi.category) |
| 138 | self.assertEqual('pkg', pi.package_name) |
| 139 | self.assertEqual('', pi.version) |
| 140 | |
| 141 | |
| 142 | class PackageInfoToCPVTest(cros_test_lib.TestCase): |
| 143 | """PackageInfoToCPV tests.""" |
| 144 | |
| 145 | def testAllFields(self): |
| 146 | """Quick sanity check it's working properly.""" |
| 147 | pi = common_pb2.PackageInfo() |
| 148 | pi.package_name = 'pkg' |
| 149 | pi.category = 'cat' |
| 150 | pi.version = '2.0.0' |
| 151 | |
| 152 | cpv = controller_util.PackageInfoToCPV(pi) |
| 153 | |
| 154 | self.assertEqual('pkg', cpv.package) |
| 155 | self.assertEqual('cat', cpv.category) |
| 156 | self.assertEqual('2.0.0', cpv.version) |
| 157 | |
| 158 | def testNoPackageInfo(self): |
| 159 | """Test no package info given.""" |
| 160 | self.assertIsNone(controller_util.PackageInfoToCPV(None)) |
| 161 | |
| 162 | def testNoPackageName(self): |
| 163 | """Test no package name given.""" |
| 164 | pi = common_pb2.PackageInfo() |
| 165 | pi.category = 'cat' |
| 166 | pi.version = '2.0.0' |
| 167 | |
| 168 | self.assertIsNone(controller_util.PackageInfoToCPV(pi)) |
| 169 | |
| 170 | |
| 171 | class PackageInfoToStringTest(cros_test_lib.TestCase): |
| 172 | """PackageInfoToString tests.""" |
| 173 | |
| 174 | def testAllFields(self): |
| 175 | """Test all fields present.""" |
| 176 | pi = common_pb2.PackageInfo() |
| 177 | pi.package_name = 'pkg' |
| 178 | pi.category = 'cat' |
| 179 | pi.version = '2.0.0' |
| 180 | |
| 181 | cpv_str = controller_util.PackageInfoToString(pi) |
| 182 | |
| 183 | self.assertEqual('cat/pkg-2.0.0', cpv_str) |
| 184 | |
| 185 | def testNoVersion(self): |
| 186 | """Test no version provided.""" |
| 187 | pi = common_pb2.PackageInfo() |
| 188 | pi.package_name = 'pkg' |
| 189 | pi.category = 'cat' |
| 190 | |
| 191 | cpv_str = controller_util.PackageInfoToString(pi) |
| 192 | |
| 193 | self.assertEqual('cat/pkg', cpv_str) |
| 194 | |
| 195 | def testPackageOnly(self): |
| 196 | """Test no version provided.""" |
| 197 | pi = common_pb2.PackageInfo() |
| 198 | pi.package_name = 'pkg' |
| 199 | |
| 200 | cpv_str = controller_util.PackageInfoToString(pi) |
| 201 | |
| 202 | self.assertEqual('pkg', cpv_str) |
| 203 | |
| 204 | def testNoPackageName(self): |
| 205 | """Test no package name given.""" |
| 206 | pi = common_pb2.PackageInfo() |
| 207 | |
| 208 | with self.assertRaises(ValueError): |
| 209 | controller_util.PackageInfoToString(pi) |
| 210 | |
| 211 | |
| 212 | class CPVToStringTest(cros_test_lib.TestCase): |
| 213 | """CPVToString tests.""" |
| 214 | |
| 215 | def testTranslations(self): |
| 216 | """Test standard translations used.""" |
| 217 | cases = [ |
| 218 | 'cat/pkg-2.0.0-r1', |
| 219 | 'cat/pkg-2.0.0', |
| 220 | 'cat/pkg', |
| 221 | 'pkg', |
| 222 | ] |
| 223 | |
| 224 | for case in cases: |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 225 | cpv = package_info.SplitCPV(case, strict=False) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 226 | # We should end up with as much info as is available, so we should see |
| 227 | # the original value in each case. |
| 228 | self.assertEqual(case, controller_util.CPVToString(cpv)) |
| 229 | |
| 230 | def testInvalidCPV(self): |
| 231 | """Test invalid CPV object.""" |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 232 | cpv = package_info.SplitCPV('', strict=False) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 233 | with self.assertRaises(ValueError): |
| 234 | controller_util.CPVToString(cpv) |