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 |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 13 | from chromite.cbuildbot import goma_util |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 14 | from chromite.lib import cros_test_lib |
| 15 | from chromite.lib import portage_util |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 16 | from chromite.lib.build_target_util import BuildTarget |
| 17 | from chromite.lib.chroot_lib import Chroot |
| 18 | |
| 19 | |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 20 | class ParseChrootTest(cros_test_lib.MockTestCase): |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 21 | """ParseChroot tests.""" |
| 22 | |
| 23 | def testSuccess(self): |
| 24 | """Test successful handling case.""" |
| 25 | path = '/chroot/path' |
| 26 | cache_dir = '/cache/dir' |
| 27 | chrome_root = '/chrome/root' |
| 28 | use_flags = [{'flag': 'useflag1'}, {'flag': 'useflag2'}] |
| 29 | features = [{'feature': 'feature1'}, {'feature': 'feature2'}] |
| 30 | expected_env = {'USE': 'useflag1 useflag2', |
Alex Klein | b7485bb | 2019-09-19 13:23:37 -0600 | [diff] [blame] | 31 | 'FEATURES': 'feature1 feature2', |
| 32 | 'CHROME_ORIGIN': 'LOCAL_SOURCE'} |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 33 | |
| 34 | chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, |
| 35 | chrome_dir=chrome_root, |
| 36 | env={'use_flags': use_flags, |
| 37 | 'features': features}) |
| 38 | |
| 39 | expected = Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root, |
| 40 | env=expected_env) |
| 41 | result = controller_util.ParseChroot(chroot_message) |
| 42 | |
| 43 | self.assertEqual(expected, result) |
| 44 | |
Alex Klein | b37d39e | 2019-12-09 14:31:53 -0700 | [diff] [blame^] | 45 | # TODO(saklein): Rewrite this test. |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 46 | def testChrootCallToGoma(self): |
| 47 | """Test calls to goma.""" |
| 48 | path = '/chroot/path' |
| 49 | cache_dir = '/cache/dir' |
| 50 | chrome_root = '/chrome/root' |
| 51 | use_flags = [{'flag': 'useflag1'}, {'flag': 'useflag2'}] |
| 52 | features = [{'feature': 'feature1'}, {'feature': 'feature2'}] |
| 53 | goma_test_dir = '/goma/test/dir' |
| 54 | goma_test_json_string = 'goma_json' |
| 55 | chromeos_goma_test_dir = '/chromeos/goma/test/dir' |
Alex Klein | b37d39e | 2019-12-09 14:31:53 -0700 | [diff] [blame^] | 56 | log_dir = '/log/dir' |
| 57 | stats_filename = 'stats_filename' |
| 58 | counterz_filename = 'counterz_filename' |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 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, |
David Burger | e3dcc75 | 2019-10-18 09:55:32 -0600 | [diff] [blame] | 74 | chroot_dir=path, |
Alex Klein | b37d39e | 2019-12-09 14:31:53 -0700 | [diff] [blame^] | 75 | goma_approach=None, |
| 76 | log_dir=None, stats_filename=None, |
| 77 | counterz_filename=None) |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 78 | |
| 79 | goma_config.chromeos_goma_dir = chromeos_goma_test_dir |
| 80 | chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, |
| 81 | chrome_dir=chrome_root, |
| 82 | env={'use_flags': use_flags, |
| 83 | 'features': features}, |
| 84 | goma=goma_config) |
| 85 | |
| 86 | controller_util.ParseChroot(chroot_message) |
| 87 | patch.assert_called_with(goma_test_dir, goma_test_json_string, |
| 88 | stage_name='BuildAPI', |
Alex Klein | 1f67cf3 | 2019-10-09 11:13:42 -0600 | [diff] [blame] | 89 | chromeos_goma_dir=chromeos_goma_test_dir, |
David Burger | e3dcc75 | 2019-10-18 09:55:32 -0600 | [diff] [blame] | 90 | chroot_dir=path, |
Alex Klein | b37d39e | 2019-12-09 14:31:53 -0700 | [diff] [blame^] | 91 | goma_approach=None, |
| 92 | log_dir=None, stats_filename=None, |
| 93 | counterz_filename=None) |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 94 | |
Alex Klein | b37d39e | 2019-12-09 14:31:53 -0700 | [diff] [blame^] | 95 | # Test the goma approach options. |
David Burger | e3dcc75 | 2019-10-18 09:55:32 -0600 | [diff] [blame] | 96 | goma_config.goma_approach = common_pb2.GomaConfig.RBE_PROD |
| 97 | chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, |
| 98 | chrome_dir=chrome_root, |
| 99 | env={'use_flags': use_flags, |
| 100 | 'features': features}, |
| 101 | goma=goma_config) |
| 102 | |
| 103 | controller_util.ParseChroot(chroot_message) |
| 104 | patch.assert_called_with(goma_test_dir, goma_test_json_string, |
| 105 | stage_name='BuildAPI', |
| 106 | chromeos_goma_dir=chromeos_goma_test_dir, |
| 107 | chroot_dir=path, |
| 108 | goma_approach=goma_util.GomaApproach( |
Alex Klein | b37d39e | 2019-12-09 14:31:53 -0700 | [diff] [blame^] | 109 | '?prod', 'goma.chromium.org', True), |
| 110 | log_dir=None, stats_filename=None, |
| 111 | counterz_filename=None) |
David Burger | e3dcc75 | 2019-10-18 09:55:32 -0600 | [diff] [blame] | 112 | |
| 113 | goma_config.goma_approach = common_pb2.GomaConfig.RBE_STAGING |
| 114 | chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, |
| 115 | chrome_dir=chrome_root, |
| 116 | env={'use_flags': use_flags, |
| 117 | 'features': features}, |
| 118 | goma=goma_config) |
| 119 | |
| 120 | controller_util.ParseChroot(chroot_message) |
| 121 | patch.assert_called_with(goma_test_dir, goma_test_json_string, |
| 122 | stage_name='BuildAPI', |
| 123 | chromeos_goma_dir=chromeos_goma_test_dir, |
| 124 | chroot_dir=path, |
| 125 | goma_approach=goma_util.GomaApproach( |
Alex Klein | b37d39e | 2019-12-09 14:31:53 -0700 | [diff] [blame^] | 126 | '?staging', 'staging-goma.chromium.org', True), |
| 127 | log_dir=None, stats_filename=None, |
| 128 | counterz_filename=None) |
| 129 | |
| 130 | # Test the goma log options. |
| 131 | goma_config = common_pb2.GomaConfig(goma_dir=goma_test_dir, |
| 132 | goma_client_json=goma_test_json_string, |
| 133 | log_dir={'dir': log_dir}, |
| 134 | stats_file=stats_filename, |
| 135 | counterz_file=counterz_filename) |
| 136 | chroot_message = common_pb2.Chroot(path=path, cache_dir=cache_dir, |
| 137 | chrome_dir=chrome_root, |
| 138 | env={'use_flags': use_flags, |
| 139 | 'features': features}, |
| 140 | goma=goma_config) |
| 141 | |
| 142 | controller_util.ParseChroot(chroot_message) |
| 143 | patch.assert_called_with(goma_test_dir, goma_test_json_string, |
| 144 | stage_name='BuildAPI', chromeos_goma_dir=None, |
| 145 | chroot_dir=path, goma_approach=None, |
| 146 | log_dir=log_dir, stats_filename=stats_filename, |
| 147 | counterz_filename=counterz_filename) |
Michael Mortensen | 9a73c32 | 2019-10-03 17:14:37 -0600 | [diff] [blame] | 148 | |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 149 | def testWrongMessage(self): |
| 150 | """Test invalid message type given.""" |
| 151 | with self.assertRaises(AssertionError): |
| 152 | controller_util.ParseChroot(common_pb2.BuildTarget()) |
| 153 | |
| 154 | |
| 155 | class ParseBuildTargetTest(cros_test_lib.TestCase): |
| 156 | """ParseBuildTarget tests.""" |
| 157 | |
| 158 | def testSuccess(self): |
| 159 | """Test successful handling case.""" |
| 160 | name = 'board' |
| 161 | build_target_message = common_pb2.BuildTarget(name=name) |
| 162 | expected = BuildTarget(name) |
| 163 | result = controller_util.ParseBuildTarget(build_target_message) |
| 164 | |
| 165 | self.assertEqual(expected, result) |
| 166 | |
| 167 | def testWrongMessage(self): |
| 168 | """Test invalid message type given.""" |
| 169 | with self.assertRaises(AssertionError): |
| 170 | controller_util.ParseBuildTarget(common_pb2.Chroot()) |
| 171 | |
| 172 | |
| 173 | class ParseBuildTargetsTest(cros_test_lib.TestCase): |
| 174 | """ParseBuildTargets tests.""" |
| 175 | |
| 176 | def testSuccess(self): |
| 177 | """Test successful handling case.""" |
| 178 | names = ['foo', 'bar', 'baz'] |
| 179 | message = build_api_test_pb2.TestRequestMessage() |
| 180 | for name in names: |
| 181 | message.build_targets.add().name = name |
| 182 | |
| 183 | result = controller_util.ParseBuildTargets(message.build_targets) |
| 184 | |
Mike Frysinger | 678735c | 2019-09-28 18:23:28 -0400 | [diff] [blame] | 185 | self.assertCountEqual([BuildTarget(name) for name in names], result) |
Alex Klein | 171da61 | 2019-08-06 14:00:42 -0600 | [diff] [blame] | 186 | |
| 187 | def testWrongMessage(self): |
| 188 | """Wrong message type handling.""" |
| 189 | message = common_pb2.Chroot() |
| 190 | message.env.use_flags.add().flag = 'foo' |
| 191 | message.env.use_flags.add().flag = 'bar' |
| 192 | |
| 193 | with self.assertRaises(AssertionError): |
| 194 | controller_util.ParseBuildTargets(message.env.use_flags) |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 195 | |
| 196 | |
| 197 | class CPVToPackageInfoTest(cros_test_lib.TestCase): |
| 198 | """CPVToPackageInfo tests.""" |
| 199 | |
| 200 | def testAllFields(self): |
Alex Klein | e1abe2c | 2019-08-14 10:29:46 -0600 | [diff] [blame] | 201 | """Test handling when all fields present.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 202 | pi = common_pb2.PackageInfo() |
| 203 | cpv = portage_util.SplitCPV('cat/pkg-2.0.0', strict=False) |
| 204 | |
| 205 | controller_util.CPVToPackageInfo(cpv, pi) |
| 206 | self.assertEqual('cat', pi.category) |
| 207 | self.assertEqual('pkg', pi.package_name) |
| 208 | self.assertEqual('2.0.0', pi.version) |
| 209 | |
| 210 | def testNoVersion(self): |
Alex Klein | e1abe2c | 2019-08-14 10:29:46 -0600 | [diff] [blame] | 211 | """Test handling when no version given.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 212 | pi = common_pb2.PackageInfo() |
| 213 | cpv = portage_util.SplitCPV('cat/pkg', strict=False) |
| 214 | |
| 215 | controller_util.CPVToPackageInfo(cpv, pi) |
| 216 | self.assertEqual('cat', pi.category) |
| 217 | self.assertEqual('pkg', pi.package_name) |
| 218 | self.assertEqual('', pi.version) |
| 219 | |
| 220 | def testPackageOnly(self): |
Alex Klein | e1abe2c | 2019-08-14 10:29:46 -0600 | [diff] [blame] | 221 | """Test handling when only given the package name.""" |
Alex Klein | a9d500b | 2019-04-22 15:37:51 -0600 | [diff] [blame] | 222 | pi = common_pb2.PackageInfo() |
| 223 | cpv = portage_util.SplitCPV('pkg', strict=False) |
| 224 | |
| 225 | controller_util.CPVToPackageInfo(cpv, pi) |
| 226 | self.assertEqual('', pi.category) |
| 227 | self.assertEqual('pkg', pi.package_name) |
| 228 | self.assertEqual('', pi.version) |
| 229 | |
| 230 | |
| 231 | class PackageInfoToCPVTest(cros_test_lib.TestCase): |
| 232 | """PackageInfoToCPV tests.""" |
| 233 | |
| 234 | def testAllFields(self): |
| 235 | """Quick sanity check it's working properly.""" |
| 236 | pi = common_pb2.PackageInfo() |
| 237 | pi.package_name = 'pkg' |
| 238 | pi.category = 'cat' |
| 239 | pi.version = '2.0.0' |
| 240 | |
| 241 | cpv = controller_util.PackageInfoToCPV(pi) |
| 242 | |
| 243 | self.assertEqual('pkg', cpv.package) |
| 244 | self.assertEqual('cat', cpv.category) |
| 245 | self.assertEqual('2.0.0', cpv.version) |
| 246 | |
| 247 | def testNoPackageInfo(self): |
| 248 | """Test no package info given.""" |
| 249 | self.assertIsNone(controller_util.PackageInfoToCPV(None)) |
| 250 | |
| 251 | def testNoPackageName(self): |
| 252 | """Test no package name given.""" |
| 253 | pi = common_pb2.PackageInfo() |
| 254 | pi.category = 'cat' |
| 255 | pi.version = '2.0.0' |
| 256 | |
| 257 | self.assertIsNone(controller_util.PackageInfoToCPV(pi)) |
| 258 | |
| 259 | |
| 260 | class PackageInfoToStringTest(cros_test_lib.TestCase): |
| 261 | """PackageInfoToString tests.""" |
| 262 | |
| 263 | def testAllFields(self): |
| 264 | """Test all fields present.""" |
| 265 | pi = common_pb2.PackageInfo() |
| 266 | pi.package_name = 'pkg' |
| 267 | pi.category = 'cat' |
| 268 | pi.version = '2.0.0' |
| 269 | |
| 270 | cpv_str = controller_util.PackageInfoToString(pi) |
| 271 | |
| 272 | self.assertEqual('cat/pkg-2.0.0', cpv_str) |
| 273 | |
| 274 | def testNoVersion(self): |
| 275 | """Test no version provided.""" |
| 276 | pi = common_pb2.PackageInfo() |
| 277 | pi.package_name = 'pkg' |
| 278 | pi.category = 'cat' |
| 279 | |
| 280 | cpv_str = controller_util.PackageInfoToString(pi) |
| 281 | |
| 282 | self.assertEqual('cat/pkg', cpv_str) |
| 283 | |
| 284 | def testPackageOnly(self): |
| 285 | """Test no version provided.""" |
| 286 | pi = common_pb2.PackageInfo() |
| 287 | pi.package_name = 'pkg' |
| 288 | |
| 289 | cpv_str = controller_util.PackageInfoToString(pi) |
| 290 | |
| 291 | self.assertEqual('pkg', cpv_str) |
| 292 | |
| 293 | def testNoPackageName(self): |
| 294 | """Test no package name given.""" |
| 295 | pi = common_pb2.PackageInfo() |
| 296 | |
| 297 | with self.assertRaises(ValueError): |
| 298 | controller_util.PackageInfoToString(pi) |
| 299 | |
| 300 | |
| 301 | class CPVToStringTest(cros_test_lib.TestCase): |
| 302 | """CPVToString tests.""" |
| 303 | |
| 304 | def testTranslations(self): |
| 305 | """Test standard translations used.""" |
| 306 | cases = [ |
| 307 | 'cat/pkg-2.0.0-r1', |
| 308 | 'cat/pkg-2.0.0', |
| 309 | 'cat/pkg', |
| 310 | 'pkg', |
| 311 | ] |
| 312 | |
| 313 | for case in cases: |
| 314 | cpv = portage_util.SplitCPV(case, strict=False) |
| 315 | # We should end up with as much info as is available, so we should see |
| 316 | # the original value in each case. |
| 317 | self.assertEqual(case, controller_util.CPVToString(cpv)) |
| 318 | |
| 319 | def testInvalidCPV(self): |
| 320 | """Test invalid CPV object.""" |
| 321 | cpv = portage_util.SplitCPV('', strict=False) |
| 322 | with self.assertRaises(ValueError): |
| 323 | controller_util.CPVToString(cpv) |