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