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