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