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