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