blob: 13069067b2077bf0446fb775640e1fe217fc40f9 [file] [log] [blame]
Alex Kleineb77ffa2019-05-28 14:47:44 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Packages service tests."""
6
Madeleine Hardt8ae7f102022-03-24 20:26:11 +00007import io
Andrew Lamb2bde9e42019-11-04 13:24:09 -07008import json
9import os
Michael Mortensen009cb662019-10-21 11:38:43 -060010import re
Madeleine Hardt8ae7f102022-03-24 20:26:11 +000011from unittest import mock
Michael Mortensen009cb662019-10-21 11:38:43 -060012
Mike Frysinger2c024062021-05-22 15:43:22 -040013from chromite.third_party.google.protobuf import json_format
14from chromite.third_party.google.protobuf.field_mask_pb2 import FieldMask
Mike Frysinger68796b52019-08-25 00:04:27 -040015import pytest
Andrew Lamb2bde9e42019-11-04 13:24:09 -070016
Chris McDonaldea0312c2020-05-04 23:33:15 -060017import chromite as cr
Andrew Lamb2bde9e42019-11-04 13:24:09 -070018from chromite.api.gen.config.replication_config_pb2 import (
Mike Frysinger68796b52019-08-25 00:04:27 -040019 FILE_TYPE_JSON,
20 FileReplicationRule,
21 REPLICATION_TYPE_FILTER,
22 ReplicationConfig,
23)
Alex Klein2960c752020-03-09 13:43:38 -060024from chromite.lib import build_target_lib
Ram Chandrasekar60f69f32022-06-03 22:49:30 +000025from chromite.lib import chromeos_version
Andrew Lamb2bde9e42019-11-04 13:24:09 -070026from chromite.lib import constants
Michael Mortensene0f4b542019-10-24 15:30:23 -060027from chromite.lib import cros_build_lib
Alex Kleineb77ffa2019-05-28 14:47:44 -060028from chromite.lib import cros_test_lib
Alex Klein6becabc2020-09-11 14:03:05 -060029from chromite.lib import dependency_graph
Mike Frysinger68796b52019-08-25 00:04:27 -040030from chromite.lib import depgraph
Michael Mortensenb70e8a82019-10-10 18:43:41 -060031from chromite.lib import osutils
Mike Frysinger88d96362020-02-14 19:05:45 -050032from chromite.lib import partial_mock
Alex Klein87531182019-08-12 15:23:37 -060033from chromite.lib import portage_util
Chris McDonaldea0312c2020-05-04 23:33:15 -060034from chromite.lib import uprev_lib
Alex Klein87531182019-08-12 15:23:37 -060035from chromite.lib.chroot_lib import Chroot
Alex Klein18a60af2020-06-11 12:08:47 -060036from chromite.lib.parser import package_info
Andrew Lamb2bde9e42019-11-04 13:24:09 -070037from chromite.lib.uprev_lib import GitRef
Shao-Chuan Lee05e51142021-11-24 12:27:37 +090038from chromite.service import android
Alex Kleineb77ffa2019-05-28 14:47:44 -060039from chromite.service import packages
40
Mike Frysinger68796b52019-08-25 00:04:27 -040041
Andrew Lamb2bde9e42019-11-04 13:24:09 -070042D = cros_test_lib.Directory
43
Alex Kleineb77ffa2019-05-28 14:47:44 -060044
Alex Klein4de25e82019-08-05 15:58:39 -060045class UprevAndroidTest(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060046 """Uprev android tests."""
Alex Klein4de25e82019-08-05 15:58:39 -060047
Alex Klein1699fab2022-09-08 08:46:06 -060048 def _mock_successful_uprev(self):
49 self.rc.AddCmdResult(
50 partial_mock.In("cros_mark_android_as_stable"),
51 stdout=(
52 '{"revved": true,'
53 ' "android_atom": "android/android-1.0",'
54 ' "modified_files": ["file1", "file2"]}'
55 ),
56 )
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090057
Alex Klein1699fab2022-09-08 08:46:06 -060058 def test_success(self):
59 """Test successful run handling."""
60 self._mock_successful_uprev()
61 build_targets = [
62 build_target_lib.BuildTarget(t) for t in ["foo", "bar"]
63 ]
Alex Klein4de25e82019-08-05 15:58:39 -060064
Alex Klein1699fab2022-09-08 08:46:06 -060065 result = packages.uprev_android(
66 "android/package", Chroot(), build_targets=build_targets
67 )
68 self.assertCommandContains(
69 [
70 "cros_mark_android_as_stable",
71 "--android_package=android/package",
72 "--boards=foo:bar",
73 ]
74 )
75 self.assertCommandContains(["emerge-foo"])
76 self.assertCommandContains(["emerge-bar"])
Alex Klein4de25e82019-08-05 15:58:39 -060077
Alex Klein1699fab2022-09-08 08:46:06 -060078 self.assertTrue(result.revved)
79 self.assertEqual(result.android_atom, "android/android-1.0")
80 self.assertListEqual(result.modified_files, ["file1", "file2"])
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090081
Alex Klein1699fab2022-09-08 08:46:06 -060082 def test_android_build_branch(self):
83 """Test specifying android_build_branch option."""
84 self._mock_successful_uprev()
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090085
Alex Klein1699fab2022-09-08 08:46:06 -060086 packages.uprev_android(
87 "android/package",
88 Chroot(),
89 android_build_branch="android-build-branch",
90 )
91 self.assertCommandContains(
92 [
93 "cros_mark_android_as_stable",
94 "--android_package=android/package",
95 "--android_build_branch=android-build-branch",
96 ]
97 )
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090098
Alex Klein1699fab2022-09-08 08:46:06 -060099 def test_android_version(self):
100 """Test specifying android_version option."""
101 self._mock_successful_uprev()
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 packages.uprev_android(
104 "android/package", Chroot(), android_version="7123456"
105 )
106 self.assertCommandContains(
107 [
108 "cros_mark_android_as_stable",
109 "--android_package=android/package",
110 "--force_version=7123456",
111 ]
112 )
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 def test_skip_commit(self):
115 """Test specifying skip_commit option."""
116 self._mock_successful_uprev()
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900117
Alex Klein1699fab2022-09-08 08:46:06 -0600118 packages.uprev_android("android/package", Chroot(), skip_commit=True)
119 self.assertCommandContains(
120 [
121 "cros_mark_android_as_stable",
122 "--android_package=android/package",
123 "--skip_commit",
124 ]
125 )
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900126
Alex Klein1699fab2022-09-08 08:46:06 -0600127 def test_no_uprev(self):
128 """Test no uprev handling."""
129 self.rc.AddCmdResult(
130 partial_mock.In("cros_mark_android_as_stable"),
131 stdout='{"revved": false}',
132 )
133 build_targets = [
134 build_target_lib.BuildTarget(t) for t in ["foo", "bar"]
135 ]
136 result = packages.uprev_android(
137 "android/package", Chroot(), build_targets=build_targets
138 )
Alex Klein4de25e82019-08-05 15:58:39 -0600139
Alex Klein1699fab2022-09-08 08:46:06 -0600140 self.assertCommandContains(
141 ["cros_mark_android_as_stable", "--boards=foo:bar"]
142 )
143 self.assertCommandContains(["emerge-foo"], expected=False)
144 self.assertCommandContains(["emerge-bar"], expected=False)
Alex Klein4de25e82019-08-05 15:58:39 -0600145
Alex Klein1699fab2022-09-08 08:46:06 -0600146 self.assertFalse(result.revved)
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 def test_ignore_junk_in_stdout(self):
149 """Test when stdout contains junk messages."""
150 self.rc.AddCmdResult(
151 partial_mock.In("cros_mark_android_as_stable"),
152 stdout='foo\nbar\n{"revved": false}\n',
153 )
154 result = packages.uprev_android("android/package", Chroot())
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900155
Alex Klein1699fab2022-09-08 08:46:06 -0600156 self.assertFalse(result.revved)
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900157
Alex Klein4de25e82019-08-05 15:58:39 -0600158
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900159class UprevAndroidLKGBTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600160 """Tests for uprevving Android with LKGB."""
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900161
Alex Klein1699fab2022-09-08 08:46:06 -0600162 def test_registered_handlers(self):
163 """Test that each Android package has an uprev handler registered."""
164 mock_handler = self.PatchObject(packages, "uprev_android_lkgb")
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900165
Alex Klein1699fab2022-09-08 08:46:06 -0600166 for android_package in constants.ANDROID_ALL_PACKAGES:
167 cpv = package_info.SplitCPV(
168 "chromeos-base/" + android_package, strict=False
169 )
170 build_targets = [build_target_lib.BuildTarget("foo")]
171 chroot = Chroot()
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900172
Alex Klein1699fab2022-09-08 08:46:06 -0600173 packages.uprev_versioned_package(cpv, build_targets, [], chroot)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900174
Alex Klein1699fab2022-09-08 08:46:06 -0600175 mock_handler.assert_called_once_with(
176 android_package, build_targets, chroot
177 )
178 mock_handler.reset_mock()
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900179
Alex Klein1699fab2022-09-08 08:46:06 -0600180 def test_success(self):
181 """Test a successful uprev."""
182 self.PatchObject(android, "OVERLAY_DIR", new="overlay-dir")
183 self.PatchObject(android, "ReadLKGB", return_value="android-lkgb")
184 self.PatchObject(
185 packages,
186 "uprev_android",
187 return_value=packages.UprevAndroidResult(
188 revved=True,
189 android_atom="android-atom",
190 modified_files=["file1", "file2"],
191 ),
192 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900193
Alex Klein1699fab2022-09-08 08:46:06 -0600194 result = packages.uprev_android_lkgb("android-package", [], Chroot())
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900195
Alex Klein1699fab2022-09-08 08:46:06 -0600196 self.assertListEqual(
197 result.modified,
198 [
199 uprev_lib.UprevVersionedPackageModifications(
200 "android-lkgb",
201 [
202 os.path.join("overlay-dir", "file1"),
203 os.path.join("overlay-dir", "file2"),
204 ],
205 )
206 ],
207 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900208
Alex Klein1699fab2022-09-08 08:46:06 -0600209 def test_no_rev(self):
210 """Test when nothing revved."""
211 self.PatchObject(android, "ReadLKGB", return_value="android-lkgb")
212 self.PatchObject(
213 packages,
214 "uprev_android",
215 return_value=packages.UprevAndroidResult(revved=False),
216 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900217
Alex Klein1699fab2022-09-08 08:46:06 -0600218 result = packages.uprev_android_lkgb("android-package", [], Chroot())
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900219
Alex Klein1699fab2022-09-08 08:46:06 -0600220 self.assertListEqual(result.modified, [])
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900221
222
Alex Kleineb77ffa2019-05-28 14:47:44 -0600223class UprevBuildTargetsTest(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600224 """uprev_build_targets tests."""
Alex Kleineb77ffa2019-05-28 14:47:44 -0600225
Alex Klein1699fab2022-09-08 08:46:06 -0600226 def test_invalid_type_fails(self):
227 """Test invalid type fails."""
228 with self.assertRaises(AssertionError):
229 packages.uprev_build_targets(
230 [build_target_lib.BuildTarget("foo")], "invalid"
231 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600232
Alex Klein1699fab2022-09-08 08:46:06 -0600233 def test_none_type_fails(self):
234 """Test None type fails."""
235 with self.assertRaises(AssertionError):
236 packages.uprev_build_targets(
237 [build_target_lib.BuildTarget("foo")], None
238 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600239
240
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000241class PatchEbuildVarsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600242 """patch_ebuild_vars test."""
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000243
Alex Klein1699fab2022-09-08 08:46:06 -0600244 def setUp(self):
245 self.mock_input = self.PatchObject(packages.fileinput, "input")
246 self.mock_stdout_write = self.PatchObject(packages.sys.stdout, "write")
247 self.ebuild_path = "/path/to/ebuild"
248 self.old_var_value = "R100-5678.0.123456789"
249 self.new_var_value = "R102-5678.0.234566789"
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000250
Alex Klein1699fab2022-09-08 08:46:06 -0600251 def test_patch_ebuild_vars_var_only(self):
252 """patch_ebuild_vars changes ^var=value$."""
253 ebuild_contents = (
254 "This line does not change.\n"
255 'AFDO_PROFILE_VERSION="{var_value}"\n'
256 "\n"
257 "# The line with AFDO_PROFILE_VERSION is also unchanged."
258 )
259 # Ebuild contains old_var_value.
260 self.mock_input.return_value = io.StringIO(
261 ebuild_contents.format(var_value=self.old_var_value)
262 )
263 expected_calls = []
264 # Expect the line with new_var_value.
265 for line in io.StringIO(
266 ebuild_contents.format(var_value=self.new_var_value)
267 ):
268 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000269
Alex Klein1699fab2022-09-08 08:46:06 -0600270 packages.patch_ebuild_vars(
271 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
272 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000273
Alex Klein1699fab2022-09-08 08:46:06 -0600274 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000275
Alex Klein1699fab2022-09-08 08:46:06 -0600276 def test_patch_ebuild_vars_ignore_export(self):
277 """patch_ebuild_vars changes ^export var=value$ and keeps export."""
278 ebuild_contents = (
279 "This line does not change.\n"
280 'export AFDO_PROFILE_VERSION="{var_value}"\n'
281 "# This line is also unchanged."
282 )
283 # Ebuild contains old_var_value.
284 self.mock_input.return_value = io.StringIO(
285 ebuild_contents.format(var_value=self.old_var_value)
286 )
287 expected_calls = []
288 # Expect the line with new_var_value.
289 for line in io.StringIO(
290 ebuild_contents.format(var_value=self.new_var_value)
291 ):
292 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000293
Alex Klein1699fab2022-09-08 08:46:06 -0600294 packages.patch_ebuild_vars(
295 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
296 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000297
Alex Klein1699fab2022-09-08 08:46:06 -0600298 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000299
Alex Klein1699fab2022-09-08 08:46:06 -0600300 def test_patch_ebuild_vars_partial_match(self):
301 """patch_ebuild_vars ignores ^{prefix}var=value$."""
302 ebuild_contents = (
303 "This and the line below do not change.\n" 'NEW_AFDO="{var_value}"'
304 )
305 # Ebuild contains old_var_value.
306 self.mock_input.return_value = io.StringIO(
307 ebuild_contents.format(var_value=self.old_var_value)
308 )
309 expected_calls = []
310 # Expect the line with UNCHANGED old_var_value.
311 for line in io.StringIO(
312 ebuild_contents.format(var_value=self.old_var_value)
313 ):
314 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000315
Alex Klein1699fab2022-09-08 08:46:06 -0600316 # Note that the var name partially matches the ebuild var and hence it has
317 # to be ignored.
318 packages.patch_ebuild_vars(
319 self.ebuild_path, {"AFDO": self.new_var_value}
320 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000321
Alex Klein1699fab2022-09-08 08:46:06 -0600322 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000323
Alex Klein1699fab2022-09-08 08:46:06 -0600324 def test_patch_ebuild_vars_no_vars(self):
325 """patch_ebuild_vars keeps ebuild intact if there are no vars."""
326 ebuild_contents = (
327 "This line does not change.\n"
328 "The line with AFDO_PROFILE_VERSION is also unchanged."
329 )
330 self.mock_input.return_value = io.StringIO(ebuild_contents)
331 expected_calls = []
332 for line in io.StringIO(ebuild_contents):
333 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000334
Alex Klein1699fab2022-09-08 08:46:06 -0600335 packages.patch_ebuild_vars(
336 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
337 )
338
339 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000340
341
Alex Klein87531182019-08-12 15:23:37 -0600342class UprevsVersionedPackageTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600343 """uprevs_versioned_package decorator test."""
Alex Klein87531182019-08-12 15:23:37 -0600344
Alex Klein1699fab2022-09-08 08:46:06 -0600345 @packages.uprevs_versioned_package("category/package")
346 def uprev_category_package(self, *args, **kwargs):
347 """Registered function for testing."""
Alex Klein87531182019-08-12 15:23:37 -0600348
Alex Klein1699fab2022-09-08 08:46:06 -0600349 def test_calls_function(self):
350 """Test calling a registered function."""
351 self.PatchObject(self, "uprev_category_package")
Alex Klein87531182019-08-12 15:23:37 -0600352
Alex Klein1699fab2022-09-08 08:46:06 -0600353 cpv = package_info.SplitCPV("category/package", strict=False)
354 packages.uprev_versioned_package(cpv, [], [], Chroot())
Alex Klein87531182019-08-12 15:23:37 -0600355
Alex Klein1699fab2022-09-08 08:46:06 -0600356 # TODO(crbug/1065172): Invalid assertion that had previously been mocked.
357 # patch.assert_called()
Alex Klein87531182019-08-12 15:23:37 -0600358
Alex Klein1699fab2022-09-08 08:46:06 -0600359 def test_unregistered_package(self):
360 """Test calling with an unregistered package."""
361 cpv = package_info.SplitCPV("does-not/exist", strict=False)
Alex Klein87531182019-08-12 15:23:37 -0600362
Alex Klein1699fab2022-09-08 08:46:06 -0600363 with self.assertRaises(packages.UnknownPackageError):
364 packages.uprev_versioned_package(cpv, [], [], Chroot())
Alex Klein87531182019-08-12 15:23:37 -0600365
366
Trent Begin6daa8702020-01-29 14:58:12 -0700367class UprevEbuildFromPinTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600368 """Tests uprev_ebuild_from_pin function"""
Trent Begin315d9d92019-12-03 21:55:53 -0700369
Alex Klein1699fab2022-09-08 08:46:06 -0600370 package = "category/package"
371 version = "1.2.3"
372 new_version = "1.2.4"
373 ebuild_template = "package-%s-r1.ebuild"
374 ebuild = ebuild_template % version
375 unstable_ebuild = "package-9999.ebuild"
376 manifest = "Manifest"
Trent Begin315d9d92019-12-03 21:55:53 -0700377
Alex Klein1699fab2022-09-08 08:46:06 -0600378 def test_uprev_ebuild(self):
379 """Tests uprev of ebuild with version path"""
380 file_layout = (
381 D(self.package, [self.ebuild, self.unstable_ebuild, self.manifest]),
382 )
383 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700384
Alex Klein1699fab2022-09-08 08:46:06 -0600385 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700386
Alex Klein1699fab2022-09-08 08:46:06 -0600387 ebuild_path = os.path.join(package_path, self.ebuild)
388 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000389
Alex Klein1699fab2022-09-08 08:46:06 -0600390 result = uprev_lib.uprev_ebuild_from_pin(
391 package_path, self.new_version, chroot=Chroot()
392 )
393 self.assertEqual(
394 len(result.modified),
395 1,
396 "unexpected number of results: %s" % len(result.modified),
397 )
Trent Begin315d9d92019-12-03 21:55:53 -0700398
Alex Klein1699fab2022-09-08 08:46:06 -0600399 mod = result.modified[0]
400 self.assertEqual(
401 mod.new_version,
402 self.new_version + "-r1",
403 "unexpected version number: %s" % mod.new_version,
404 )
Trent Begin315d9d92019-12-03 21:55:53 -0700405
Alex Klein1699fab2022-09-08 08:46:06 -0600406 old_ebuild_path = os.path.join(
407 package_path, self.ebuild_template % self.version
408 )
409 new_ebuild_path = os.path.join(
410 package_path, self.ebuild_template % self.new_version
411 )
412 manifest_path = os.path.join(package_path, "Manifest")
Trent Begin2e5344f2020-03-02 10:46:55 -0700413
Alex Klein1699fab2022-09-08 08:46:06 -0600414 expected_modified_files = [
415 old_ebuild_path,
416 new_ebuild_path,
417 manifest_path,
418 ]
419 self.assertCountEqual(mod.files, expected_modified_files)
Trent Begin4a11a632020-02-28 12:59:58 -0700420
Alex Klein1699fab2022-09-08 08:46:06 -0600421 self.assertCommandContains(["ebuild", "manifest"])
Trent Begin6daa8702020-01-29 14:58:12 -0700422
Alex Klein1699fab2022-09-08 08:46:06 -0600423 def test_uprev_ebuild_same_version(self):
424 """Tests uprev of ebuild with version path when the version has not changed.
Fergus Dall2209d0b2020-08-06 11:51:43 +1000425
Alex Klein1699fab2022-09-08 08:46:06 -0600426 This should result in bumping the revision number.
427 """
428 file_layout = (
429 D(self.package, [self.ebuild, self.unstable_ebuild, self.manifest]),
430 )
431 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000432
Alex Klein1699fab2022-09-08 08:46:06 -0600433 package_path = os.path.join(self.tempdir, self.package)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000434
Alex Klein1699fab2022-09-08 08:46:06 -0600435 ebuild_path = os.path.join(package_path, self.ebuild)
436 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000437
Alex Klein1699fab2022-09-08 08:46:06 -0600438 result = uprev_lib.uprev_ebuild_from_pin(
439 package_path, self.version, chroot=Chroot()
440 )
441 self.assertEqual(
442 len(result.modified),
443 1,
444 "unexpected number of results: %s" % len(result.modified),
445 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000446
Alex Klein1699fab2022-09-08 08:46:06 -0600447 mod = result.modified[0]
448 self.assertEqual(
449 mod.new_version,
450 self.version + "-r2",
451 "unexpected version number: %s" % mod.new_version,
452 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000453
Alex Klein1699fab2022-09-08 08:46:06 -0600454 old_ebuild_path = os.path.join(
455 package_path, self.ebuild_template % self.version
456 )
457 new_ebuild_path = os.path.join(
458 package_path, "package-%s-r2.ebuild" % self.version
459 )
460 manifest_path = os.path.join(package_path, "Manifest")
Fergus Dall2209d0b2020-08-06 11:51:43 +1000461
Alex Klein1699fab2022-09-08 08:46:06 -0600462 expected_modified_files = [
463 old_ebuild_path,
464 new_ebuild_path,
465 manifest_path,
466 ]
467 self.assertCountEqual(mod.files, expected_modified_files)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000468
Alex Klein1699fab2022-09-08 08:46:06 -0600469 self.assertCommandContains(["ebuild", "manifest"])
Fergus Dall2209d0b2020-08-06 11:51:43 +1000470
Alex Klein1699fab2022-09-08 08:46:06 -0600471 def test_no_ebuild(self):
472 """Tests assertion is raised if package has no ebuilds"""
473 file_layout = (D(self.package, [self.manifest]),)
474 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700475
Alex Klein1699fab2022-09-08 08:46:06 -0600476 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700477
Alex Klein1699fab2022-09-08 08:46:06 -0600478 with self.assertRaises(uprev_lib.EbuildUprevError):
479 uprev_lib.uprev_ebuild_from_pin(
480 package_path, self.new_version, chroot=Chroot()
481 )
Trent Begin315d9d92019-12-03 21:55:53 -0700482
Alex Klein1699fab2022-09-08 08:46:06 -0600483 def test_multiple_stable_ebuilds(self):
484 """Tests assertion is raised if multiple stable ebuilds are present"""
485 file_layout = (
486 D(
487 self.package,
488 [self.ebuild, self.ebuild_template % "1.2.1", self.manifest],
489 ),
490 )
491 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000492
Alex Klein1699fab2022-09-08 08:46:06 -0600493 package_path = os.path.join(self.tempdir, self.package)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000494
Alex Klein1699fab2022-09-08 08:46:06 -0600495 ebuild_path = os.path.join(package_path, self.ebuild)
496 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000497
Alex Klein1699fab2022-09-08 08:46:06 -0600498 ebuild_path = os.path.join(package_path, self.ebuild_template % "1.2.1")
499 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000500
Alex Klein1699fab2022-09-08 08:46:06 -0600501 with self.assertRaises(uprev_lib.EbuildUprevError):
502 uprev_lib.uprev_ebuild_from_pin(
503 package_path, self.new_version, chroot=Chroot()
504 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000505
Alex Klein1699fab2022-09-08 08:46:06 -0600506 def test_multiple_unstable_ebuilds(self):
507 """Tests assertion is raised if multiple unstable ebuilds are present"""
508 file_layout = (
509 D(
510 self.package,
511 [self.ebuild, self.ebuild_template % "1.2.1", self.manifest],
512 ),
513 )
514 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700515
Alex Klein1699fab2022-09-08 08:46:06 -0600516 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700517
Alex Klein1699fab2022-09-08 08:46:06 -0600518 with self.assertRaises(uprev_lib.EbuildUprevError):
519 uprev_lib.uprev_ebuild_from_pin(
520 package_path, self.new_version, chroot=Chroot()
521 )
Trent Begin315d9d92019-12-03 21:55:53 -0700522
523
Andrew Lamb9563a152019-12-04 11:42:18 -0700524class ReplicatePrivateConfigTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600525 """replicate_private_config tests."""
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700526
Alex Klein1699fab2022-09-08 08:46:06 -0600527 def setUp(self):
528 # Set up fake public and private chromeos-config overlays.
529 private_package_root = (
530 "src/private-overlays/overlay-coral-private/chromeos-base/"
531 "chromeos-config-bsp"
532 )
533 self.public_package_root = (
534 "src/overlays/overlay-coral/chromeos-base/chromeos-config-bsp"
535 )
536 file_layout = (
537 D(
538 os.path.join(private_package_root, "files"),
539 ["build_config.json"],
540 ),
541 D(private_package_root, ["replication_config.jsonpb"]),
542 D(
543 os.path.join(self.public_package_root, "files"),
544 ["build_config.json"],
545 ),
546 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700547
Alex Klein1699fab2022-09-08 08:46:06 -0600548 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700549
Alex Klein1699fab2022-09-08 08:46:06 -0600550 # Private config contains 'a' and 'b' fields.
551 self.private_config_path = os.path.join(
552 private_package_root, "files", "build_config.json"
553 )
554 self.WriteTempFile(
555 self.private_config_path,
556 json.dumps({"chromeos": {"configs": [{"a": 3, "b": 2}]}}),
557 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700558
Alex Klein1699fab2022-09-08 08:46:06 -0600559 # Public config only contains the 'a' field. Note that the value of 'a' is
560 # 1 in the public config; it will get updated to 3 when the private config
561 # is replicated.
562 self.public_config_path = os.path.join(
563 self.public_package_root, "files", "build_config.json"
564 )
565 self.WriteTempFile(
566 self.public_config_path,
567 json.dumps({"chromeos": {"configs": [{"a": 1}]}}),
568 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700569
Alex Klein1699fab2022-09-08 08:46:06 -0600570 # Put a ReplicationConfig JSONPB in the private package. Note that it
571 # specifies only the 'a' field is replicated.
572 self.replication_config_path = os.path.join(
573 self.tempdir, private_package_root, "replication_config.jsonpb"
574 )
575 replication_config = ReplicationConfig(
576 file_replication_rules=[
577 FileReplicationRule(
578 source_path=self.private_config_path,
579 destination_path=self.public_config_path,
580 file_type=FILE_TYPE_JSON,
581 replication_type=REPLICATION_TYPE_FILTER,
582 destination_fields=FieldMask(paths=["a"]),
583 )
584 ]
585 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700586
Alex Klein1699fab2022-09-08 08:46:06 -0600587 osutils.WriteFile(
588 self.replication_config_path,
589 json_format.MessageToJson(replication_config),
590 )
591 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700592
Alex Klein1699fab2022-09-08 08:46:06 -0600593 self.rc.SetDefaultCmdResult(side_effect=self._write_generated_c_files)
Andrew Lamb9563a152019-12-04 11:42:18 -0700594
Alex Klein1699fab2022-09-08 08:46:06 -0600595 def _write_generated_c_files(self, *_args, **_kwargs):
596 """Write fake generated C files to the public output dir.
Andrew Lamb9563a152019-12-04 11:42:18 -0700597
Alex Klein1699fab2022-09-08 08:46:06 -0600598 Note that this function accepts args and kwargs so it can be used as a side
599 effect.
600 """
601 output_dir = os.path.join(self.public_package_root, "files")
602 self.WriteTempFile(os.path.join(output_dir, "config.c"), "")
603 self.WriteTempFile(os.path.join(output_dir, "ec_config.c"), "")
604 self.WriteTempFile(os.path.join(output_dir, "ec_config.h"), "")
Andrew Lamb9563a152019-12-04 11:42:18 -0700605
Alex Klein1699fab2022-09-08 08:46:06 -0600606 def _write_incorrect_generated_c_files(self, *_args, **_kwargs):
607 """Similar to _write_generated_c_files, with an expected file missing.
Andrew Lamb9563a152019-12-04 11:42:18 -0700608
Alex Klein1699fab2022-09-08 08:46:06 -0600609 Note that this function accepts args and kwargs so it can be used as a side
610 effect.
611 """
612 output_dir = os.path.join(self.public_package_root, "files")
613 self.WriteTempFile(os.path.join(output_dir, "config.c"), "")
614 self.WriteTempFile(os.path.join(output_dir, "ec_config.c"), "")
Andrew Lamb9563a152019-12-04 11:42:18 -0700615
Alex Klein1699fab2022-09-08 08:46:06 -0600616 def test_replicate_private_config(self):
617 """Basic replication test."""
618 refs = [
619 GitRef(
620 path="/chromeos/overlays/overlay-coral-private",
621 ref="main",
622 revision="123",
623 )
624 ]
625 chroot = Chroot()
626 result = packages.replicate_private_config(
627 _build_targets=None, refs=refs, chroot=chroot
628 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700629
Alex Klein1699fab2022-09-08 08:46:06 -0600630 self.assertCommandContains(
631 [
632 "cros_config_schema",
633 "-m",
634 os.path.join(
635 constants.CHROOT_SOURCE_ROOT, self.public_config_path
636 ),
637 "-g",
638 os.path.join(
639 constants.CHROOT_SOURCE_ROOT,
640 self.public_package_root,
641 "files",
642 ),
643 "-f",
644 '"TRUE"',
645 ],
646 enter_chroot=True,
647 chroot_args=chroot.get_enter_args(),
648 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700649
Alex Klein1699fab2022-09-08 08:46:06 -0600650 self.assertEqual(len(result.modified), 1)
651 # The public build_config.json and generated C files were modified.
652 expected_modified_files = [
653 os.path.join(self.tempdir, self.public_config_path),
654 os.path.join(
655 self.tempdir, self.public_package_root, "files", "config.c"
656 ),
657 os.path.join(
658 self.tempdir, self.public_package_root, "files", "ec_config.c"
659 ),
660 os.path.join(
661 self.tempdir, self.public_package_root, "files", "ec_config.h"
662 ),
663 ]
664 self.assertEqual(result.modified[0].files, expected_modified_files)
665 self.assertEqual(result.modified[0].new_version, "123")
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700666
Alex Klein1699fab2022-09-08 08:46:06 -0600667 # The update from the private build_config.json was copied to the public.
668 # Note that only the 'a' field is present, as per destination_fields.
669 self.assertEqual(
670 json.loads(self.ReadTempFile(self.public_config_path)),
671 {"chromeos": {"configs": [{"a": 3}]}},
672 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700673
Alex Klein1699fab2022-09-08 08:46:06 -0600674 def test_replicate_private_config_no_build_config(self):
675 """If there is no build config, don't generate C files."""
676 # Modify the replication config to write to "other_config.json" instead of
677 # "build_config.json"
678 modified_destination_path = self.public_config_path.replace(
679 "build_config", "other_config"
680 )
681 replication_config = ReplicationConfig(
682 file_replication_rules=[
683 FileReplicationRule(
684 source_path=self.private_config_path,
685 destination_path=modified_destination_path,
686 file_type=FILE_TYPE_JSON,
687 replication_type=REPLICATION_TYPE_FILTER,
688 destination_fields=FieldMask(paths=["a"]),
689 )
690 ]
691 )
692 osutils.WriteFile(
693 self.replication_config_path,
694 json_format.MessageToJson(replication_config),
695 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700696
Alex Klein1699fab2022-09-08 08:46:06 -0600697 refs = [
698 GitRef(
699 path="/chromeos/overlays/overlay-coral-private",
700 ref="main",
701 revision="123",
702 )
703 ]
704 result = packages.replicate_private_config(
705 _build_targets=None, refs=refs, chroot=Chroot()
706 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700707
Alex Klein1699fab2022-09-08 08:46:06 -0600708 self.assertEqual(len(result.modified), 1)
709 self.assertEqual(
710 result.modified[0].files,
711 [os.path.join(self.tempdir, modified_destination_path)],
712 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700713
Alex Klein1699fab2022-09-08 08:46:06 -0600714 def test_replicate_private_config_multiple_build_configs(self):
715 """An error is thrown if there is more than one build config."""
716 replication_config = ReplicationConfig(
717 file_replication_rules=[
718 FileReplicationRule(
719 source_path=self.private_config_path,
720 destination_path=self.public_config_path,
721 file_type=FILE_TYPE_JSON,
722 replication_type=REPLICATION_TYPE_FILTER,
723 destination_fields=FieldMask(paths=["a"]),
724 ),
725 FileReplicationRule(
726 source_path=self.private_config_path,
727 destination_path=self.public_config_path,
728 file_type=FILE_TYPE_JSON,
729 replication_type=REPLICATION_TYPE_FILTER,
730 destination_fields=FieldMask(paths=["a"]),
731 ),
732 ]
733 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700734
Alex Klein1699fab2022-09-08 08:46:06 -0600735 osutils.WriteFile(
736 self.replication_config_path,
737 json_format.MessageToJson(replication_config),
738 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700739
Alex Klein1699fab2022-09-08 08:46:06 -0600740 refs = [
741 GitRef(
742 path="/chromeos/overlays/overlay-coral-private",
743 ref="main",
744 revision="123",
745 )
746 ]
747 with self.assertRaisesRegex(
748 ValueError,
749 "Expected at most one build_config.json destination path.",
750 ):
751 packages.replicate_private_config(
752 _build_targets=None, refs=refs, chroot=Chroot()
753 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700754
Alex Klein1699fab2022-09-08 08:46:06 -0600755 def test_replicate_private_config_generated_files_incorrect(self):
756 """An error is thrown if generated C files are missing."""
757 self.rc.SetDefaultCmdResult(
758 side_effect=self._write_incorrect_generated_c_files
759 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700760
Alex Klein1699fab2022-09-08 08:46:06 -0600761 refs = [
762 GitRef(
763 path="/chromeos/overlays/overlay-coral-private",
764 ref="main",
765 revision="123",
766 )
767 ]
768 chroot = Chroot()
Andrew Lamb9563a152019-12-04 11:42:18 -0700769
Alex Klein1699fab2022-09-08 08:46:06 -0600770 with self.assertRaisesRegex(
771 packages.GeneratedCrosConfigFilesError,
772 "Expected to find generated C files",
773 ):
774 packages.replicate_private_config(
775 _build_targets=None, refs=refs, chroot=chroot
776 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700777
Alex Klein1699fab2022-09-08 08:46:06 -0600778 def test_replicate_private_config_wrong_number_of_refs(self):
779 """An error is thrown if there is not exactly one ref."""
780 with self.assertRaisesRegex(ValueError, "Expected exactly one ref"):
781 packages.replicate_private_config(
782 _build_targets=None, refs=[], chroot=None
783 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700784
Alex Klein1699fab2022-09-08 08:46:06 -0600785 with self.assertRaisesRegex(ValueError, "Expected exactly one ref"):
786 refs = [
787 GitRef(path="a", ref="main", revision="1"),
788 GitRef(path="a", ref="main", revision="2"),
789 ]
790 packages.replicate_private_config(
791 _build_targets=None, refs=refs, chroot=None
792 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700793
Alex Klein1699fab2022-09-08 08:46:06 -0600794 def test_replicate_private_config_replication_config_missing(self):
795 """An error is thrown if there is not a replication config."""
796 os.remove(self.replication_config_path)
797 with self.assertRaisesRegex(
798 ValueError,
799 "Expected ReplicationConfig missing at %s"
800 % self.replication_config_path,
801 ):
802 refs = [
803 GitRef(
804 path="/chromeos/overlays/overlay-coral-private",
805 ref="main",
806 revision="123",
807 )
808 ]
809 packages.replicate_private_config(
810 _build_targets=None, refs=refs, chroot=None
811 )
Andrew Lambe836f222019-12-09 12:27:38 -0700812
Alex Klein1699fab2022-09-08 08:46:06 -0600813 def test_replicate_private_config_wrong_git_ref_path(self):
814 """An error is thrown if the git ref doesn't point to a private overlay."""
815 with self.assertRaisesRegex(
816 ValueError, "ref.path must match the pattern"
817 ):
818 refs = [GitRef(path="a/b/c", ref="main", revision="123")]
819 packages.replicate_private_config(
820 _build_targets=None, refs=refs, chroot=None
821 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700822
823
Alex Klein5caab872021-09-10 11:44:37 -0600824class GetBestVisibleTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600825 """get_best_visible tests."""
David Burger1e0fe232019-07-01 14:52:07 -0600826
Alex Klein1699fab2022-09-08 08:46:06 -0600827 def test_empty_atom_fails(self):
828 """Test empty atom raises an error."""
829 with self.assertRaises(AssertionError):
830 packages.get_best_visible("")
Alex Kleinda39c6d2019-09-16 14:36:36 -0600831
832
Alex Klein149fd3b2019-12-16 16:01:05 -0700833class HasPrebuiltTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600834 """has_prebuilt tests."""
Alex Kleinda39c6d2019-09-16 14:36:36 -0600835
Alex Klein1699fab2022-09-08 08:46:06 -0600836 def test_empty_atom_fails(self):
837 """Test an empty atom results in an error."""
838 with self.assertRaises(AssertionError):
839 packages.has_prebuilt("")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600840
Alex Klein1699fab2022-09-08 08:46:06 -0600841 def test_use_flags(self):
842 """Test use flags get propagated correctly."""
843 # We don't really care about the result, just the env handling.
844 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
845 # Ignore any flags that may be in the environment.
846 self.PatchObject(os.environ, "get", return_value="")
Alex Klein149fd3b2019-12-16 16:01:05 -0700847
Alex Klein1699fab2022-09-08 08:46:06 -0600848 packages.has_prebuilt("cat/pkg-1.2.3", useflags="useflag")
849 patch.assert_called_with(
850 "cat/pkg-1.2.3", board=None, extra_env={"USE": "useflag"}
851 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700852
Alex Klein1699fab2022-09-08 08:46:06 -0600853 def test_env_use_flags(self):
854 """Test env use flags get propagated correctly with passed useflags."""
855 # We don't really care about the result, just the env handling.
856 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
857 # Add some flags to the environment.
858 existing_flags = "already set flags"
859 self.PatchObject(os.environ, "get", return_value=existing_flags)
Alex Klein149fd3b2019-12-16 16:01:05 -0700860
Alex Klein1699fab2022-09-08 08:46:06 -0600861 new_flags = "useflag"
862 packages.has_prebuilt("cat/pkg-1.2.3", useflags=new_flags)
863 expected = "%s %s" % (existing_flags, new_flags)
864 patch.assert_called_with(
865 "cat/pkg-1.2.3", board=None, extra_env={"USE": expected}
866 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700867
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600868
869class AndroidVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600870 """Tests getting android versions."""
Michael Mortensen14960d02019-10-18 07:53:59 -0600871
Alex Klein1699fab2022-09-08 08:46:06 -0600872 def setUp(self):
873 package_result = [
874 "chromeos-base/android-container-nyc-4717008-r1",
875 "chromeos-base/update_engine-0.0.3-r3408",
876 ]
877 self.PatchObject(
878 portage_util, "GetPackageDependencies", return_value=package_result
879 )
880 self.board = "board"
881 self.PatchObject(
882 portage_util,
883 "FindEbuildForBoardPackage",
884 return_value="chromeos-base/android-container-nyc",
885 )
886 FakeEnvironment = {
887 "ARM_TARGET": "3-linux-target",
888 }
889 self.PatchObject(
890 osutils, "SourceEnvironment", return_value=FakeEnvironment
891 )
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600892
Alex Klein1699fab2022-09-08 08:46:06 -0600893 # Clear the LRU cache for the function. We mock the function that provides
894 # the data this function processes to produce its result, so we need to
895 # clear it manually.
896 packages.determine_android_package.cache_clear()
Alex Klein68a28712021-11-08 11:08:30 -0700897
Alex Klein1699fab2022-09-08 08:46:06 -0600898 def test_determine_android_version(self):
899 """Tests that a valid android version is returned."""
900 version = packages.determine_android_version(self.board)
901 self.assertEqual(version, "4717008")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600902
Alex Klein1699fab2022-09-08 08:46:06 -0600903 def test_determine_android_version_when_not_present(self):
904 """Tests that a None is returned for version when android is not present."""
905 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
906 self.PatchObject(
907 portage_util, "GetPackageDependencies", return_value=package_result
908 )
909 version = packages.determine_android_version(self.board)
910 self.assertEqual(version, None)
Michael Mortensenedf76532019-10-16 14:22:37 -0600911
Alex Klein1699fab2022-09-08 08:46:06 -0600912 def test_determine_android_branch(self):
913 """Tests that a valid android branch is returned."""
914 branch = packages.determine_android_branch(self.board)
915 self.assertEqual(branch, "3")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600916
Alex Klein1699fab2022-09-08 08:46:06 -0600917 def test_determine_android_branch_64bit_targets(self):
918 """Tests that a valid android branch is returned with only 64bit targets."""
919 self.PatchObject(
920 osutils,
921 "SourceEnvironment",
922 return_value={"ARM64_TARGET": "3-linux-target"},
923 )
924 branch = packages.determine_android_branch(self.board)
925 self.assertEqual(branch, "3")
Federico 'Morg' Pareschicd9165a2020-05-29 09:45:55 +0900926
Alex Klein1699fab2022-09-08 08:46:06 -0600927 def test_determine_android_branch_when_not_present(self):
928 """Tests that a None is returned for branch when android is not present."""
929 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
930 self.PatchObject(
931 portage_util, "GetPackageDependencies", return_value=package_result
932 )
933 branch = packages.determine_android_branch(self.board)
934 self.assertEqual(branch, None)
Michael Mortensenedf76532019-10-16 14:22:37 -0600935
Alex Klein1699fab2022-09-08 08:46:06 -0600936 def test_determine_android_target(self):
937 """Tests that a valid android target is returned."""
938 target = packages.determine_android_target(self.board)
939 self.assertEqual(target, "cheets")
Michael Mortensenc2615b72019-10-15 08:12:24 -0600940
Alex Klein1699fab2022-09-08 08:46:06 -0600941 def test_determine_android_target_when_not_present(self):
942 """Tests that a None is returned for target when android is not present."""
943 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
944 self.PatchObject(
945 portage_util, "GetPackageDependencies", return_value=package_result
946 )
947 target = packages.determine_android_target(self.board)
948 self.assertEqual(target, None)
Michael Mortensenedf76532019-10-16 14:22:37 -0600949
Alex Klein1699fab2022-09-08 08:46:06 -0600950 def test_determine_android_version_handle_exception(self):
951 """Tests handling RunCommandError inside determine_android_version."""
952 # Mock what happens when portage returns that bubbles up (via RunCommand)
953 # inside portage_util.GetPackageDependencies.
954 self.PatchObject(
955 portage_util,
956 "GetPackageDependencies",
957 side_effect=cros_build_lib.RunCommandError("error"),
958 )
959 target = packages.determine_android_version(self.board)
960 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -0600961
Alex Klein1699fab2022-09-08 08:46:06 -0600962 def test_determine_android_package_handle_exception(self):
963 """Tests handling RunCommandError inside determine_android_package."""
964 # Mock what happens when portage returns that bubbles up (via RunCommand)
965 # inside portage_util.GetPackageDependencies.
966 self.PatchObject(
967 portage_util,
968 "GetPackageDependencies",
969 side_effect=cros_build_lib.RunCommandError("error"),
970 )
971 target = packages.determine_android_package(self.board)
972 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -0600973
Alex Klein1699fab2022-09-08 08:46:06 -0600974 def test_determine_android_package_callers_handle_exception(self):
975 """Tests handling RunCommandError by determine_android_package callers."""
976 # Mock what happens when portage returns that bubbles up (via RunCommand)
977 # inside portage_util.GetPackageDependencies.
978 self.PatchObject(
979 portage_util,
980 "GetPackageDependencies",
981 side_effect=cros_build_lib.RunCommandError("error"),
982 )
983 # Verify that target is None, as expected.
984 target = packages.determine_android_package(self.board)
985 self.assertEqual(target, None)
986 # determine_android_branch calls determine_android_package
987 branch = packages.determine_android_branch(self.board)
988 self.assertEqual(branch, None)
989 # determine_android_target calls determine_android_package
990 target = packages.determine_android_target(self.board)
991 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -0600992
Michael Mortensene0f4b542019-10-24 15:30:23 -0600993
Alex Klein1699fab2022-09-08 08:46:06 -0600994@pytest.mark.usefixtures("testcase_caplog", "testcase_monkeypatch")
Michael Mortensende716a12020-05-15 11:27:00 -0600995class FindFingerprintsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600996 """Tests for find_fingerprints."""
Michael Mortensende716a12020-05-15 11:27:00 -0600997
Alex Klein1699fab2022-09-08 08:46:06 -0600998 def setUp(self):
999 self.board = "test-board"
1000 # Create cheets-fingerprints.txt based on tempdir/src...
1001 self.fingerprint_contents = (
1002 "google/test-board/test-board_cheets"
1003 ":9/R99-12345.0.9999/123456:user/release-keys"
1004 )
1005 fingerprint_path = os.path.join(
1006 self.tempdir,
1007 "src/build/images/test-board/latest/cheets-fingerprint.txt",
1008 )
1009 self.chroot = Chroot(self.tempdir)
1010 osutils.WriteFile(
1011 fingerprint_path, self.fingerprint_contents, makedirs=True
1012 )
Michael Mortensende716a12020-05-15 11:27:00 -06001013
Alex Klein1699fab2022-09-08 08:46:06 -06001014 def test_find_fingerprints_with_test_path(self):
1015 """Tests get_firmware_versions with mocked output."""
1016 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1017 build_target = build_target_lib.BuildTarget(self.board)
1018 result = packages.find_fingerprints(build_target)
1019 self.assertEqual(result, [self.fingerprint_contents])
1020 self.assertIn("Reading fingerprint file", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001021
Alex Klein1699fab2022-09-08 08:46:06 -06001022 def test_find_fingerprints(self):
1023 """Tests get_firmware_versions with mocked output."""
1024 # Use board name whose path for fingerprint file does not exist.
1025 # Verify that fingerprint file is not found and None is returned.
1026 build_target = build_target_lib.BuildTarget("wrong-boardname")
1027 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1028 result = packages.find_fingerprints(build_target)
1029 self.assertEqual(result, [])
1030 self.assertIn("Fingerprint file not found", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001031
1032
Michael Mortensen59e30872020-05-18 14:12:49 -06001033class GetAllFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001034 """Tests for get_firmware_versions."""
Michael Mortensen59e30872020-05-18 14:12:49 -06001035
Alex Klein1699fab2022-09-08 08:46:06 -06001036 def setUp(self):
1037 self.board = "test-board"
1038 self.rc.SetDefaultCmdResult(
1039 stdout="""
Michael Mortensen59e30872020-05-18 14:12:49 -06001040
1041flashrom(8): 68935ee2fcfcffa47af81b966269cd2b */build/reef/usr/sbin/flashrom
1042 ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=e102cc98d45300b50088999d53775acbeff407dc, stripped
1043 0.9.9 : bbb2d6a : Jul 28 2017 15:12:34 UTC
1044
1045Model: reef
1046BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1047BIOS version: Google_Reef.9042.87.1
1048BIOS (RW) image: 0ef265eb8f2d228c09f75b011adbdcbb */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.binrw
1049BIOS (RW) version: Google_Reef.9042.110.0
1050EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1051EC version: reef_v1.1.5900-ab1ee51
1052EC (RW) version: reef_v1.1.5909-bd1f0c9
1053
1054Model: pyro
1055BIOS image: 9e62447ebf22a724a4a835018ab6234e */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/image.bin
1056BIOS version: Google_Pyro.9042.87.1
1057BIOS (RW) image: 1897457303c85de99f3e98b2eaa0eccc */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/image.binrw
1058BIOS (RW) version: Google_Pyro.9042.110.0
1059EC image: 44b93ed591733519e752e05aa0529eb5 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/ec.bin
1060EC version: pyro_v1.1.5900-ab1ee51
1061EC (RW) version: pyro_v1.1.5909-bd1f0c9
1062
1063Model: snappy
1064BIOS image: 3ab63ff080596bd7de4e7619f003bb64 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/image.bin
1065BIOS version: Google_Snappy.9042.110.0
1066EC image: c4db159e84428391d2ee25368c5fe5b6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/ec.bin
1067EC version: snappy_v1.1.5909-bd1f0c9
1068
1069Model: sand
1070BIOS image: 387da034a4f0a3f53e278ebfdcc2a412 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/image.bin
1071BIOS version: Google_Sand.9042.110.0
1072EC image: 411562e0589dacec131f5fdfbe95a561 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/ec.bin
1073EC version: sand_v1.1.5909-bd1f0c9
1074
1075Model: electro
1076BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1077BIOS version: Google_Reef.9042.87.1
1078BIOS (RW) image: 0ef265eb8f2d228c09f75b011adbdcbb */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.binrw
1079BIOS (RW) version: Google_Reef.9042.110.0
1080EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1081EC version: reef_v1.1.5900-ab1ee51
1082EC (RW) version: reef_v1.1.5909-bd1f0c9
1083
1084Package Content:
1085612e7bb6ed1fb0a05abf2ebdc834c18b *./updater4.sh
10860eafbee07282315829d0f42135ec7c0c *./gbb_utility
10876074e3ca424cb30a67c378c1d9681f9c *./mosys
108868935ee2fcfcffa47af81b966269cd2b *./flashrom
10890eafbee07282315829d0f42135ec7c0c *./dump_fmap
1090490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
109160899148600b8673ddb711faa55aee40 *./common.sh
10923c3a99346d1ca1273cbcd86c104851ff *./shflags
1093de7ce035e1f82a89f8909d888ee402c0 *./crosutil.sh
1094f9334372bdb9036ba09a6fd9bf30e7a2 *./crossystem
109522257a8d5f0adc1f50a1916c3a4a35dd *./models/reef/ec.bin
1096faf12dbb7cdaf21ce153bdffb67841fd *./models/reef/bios.bin
1097c9bbb417b7921b85a7ed999ee42f550e *./models/reef/setvars.sh
109829823d46f1ec1491ecacd7b830fd2686 *./models/pyro/ec.bin
10992320463aba8b22eb5ea836f094d281b3 *./models/pyro/bios.bin
110081614833ad77c9cd093360ba7bea76b8 *./models/pyro/setvars.sh
1101411562e0589dacec131f5fdfbe95a561 *./models/sand/ec.bin
1102387da034a4f0a3f53e278ebfdcc2a412 *./models/sand/bios.bin
1103fcd8cb0ac0e2ed6be220aaae435d43ff *./models/sand/setvars.sh
1104c4db159e84428391d2ee25368c5fe5b6 *./models/snappy/ec.bin
11053ab63ff080596bd7de4e7619f003bb64 *./models/snappy/bios.bin
1106fe5d699f2e9e4a7de031497953313dbd *./models/snappy/setvars.sh
110779aabd7cd8a215a54234c53d7bb2e6fb *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001108"""
1109 )
Michael Mortensen59e30872020-05-18 14:12:49 -06001110
Alex Klein1699fab2022-09-08 08:46:06 -06001111 def test_get_firmware_versions(self):
1112 """Tests get_firmware_versions with mocked output."""
1113 build_target = build_target_lib.BuildTarget(self.board)
1114 result = packages.get_all_firmware_versions(build_target)
1115 self.assertEqual(len(result), 5)
1116 self.assertEqual(
1117 result["reef"],
1118 packages.FirmwareVersions(
1119 "reef",
1120 "Google_Reef.9042.87.1",
1121 "Google_Reef.9042.110.0",
1122 "reef_v1.1.5900-ab1ee51",
1123 "reef_v1.1.5909-bd1f0c9",
1124 ),
1125 )
1126 self.assertEqual(
1127 result["pyro"],
1128 packages.FirmwareVersions(
1129 "pyro",
1130 "Google_Pyro.9042.87.1",
1131 "Google_Pyro.9042.110.0",
1132 "pyro_v1.1.5900-ab1ee51",
1133 "pyro_v1.1.5909-bd1f0c9",
1134 ),
1135 )
1136 self.assertEqual(
1137 result["snappy"],
1138 packages.FirmwareVersions(
1139 "snappy",
1140 "Google_Snappy.9042.110.0",
1141 None,
1142 "snappy_v1.1.5909-bd1f0c9",
1143 None,
1144 ),
1145 )
1146 self.assertEqual(
1147 result["sand"],
1148 packages.FirmwareVersions(
1149 "sand",
1150 "Google_Sand.9042.110.0",
1151 None,
1152 "sand_v1.1.5909-bd1f0c9",
1153 None,
1154 ),
1155 )
1156 self.assertEqual(
1157 result["electro"],
1158 packages.FirmwareVersions(
1159 "electro",
1160 "Google_Reef.9042.87.1",
1161 "Google_Reef.9042.110.0",
1162 "reef_v1.1.5900-ab1ee51",
1163 "reef_v1.1.5909-bd1f0c9",
1164 ),
1165 )
Michael Mortensen59e30872020-05-18 14:12:49 -06001166
Alex Klein1699fab2022-09-08 08:46:06 -06001167 def test_get_firmware_versions_error(self):
1168 """Tests get_firmware_versions with no output."""
1169 # Throw an exception when running the command.
1170 self.PatchObject(
1171 cros_build_lib,
1172 "run",
1173 side_effect=cros_build_lib.RunCommandError("error"),
1174 )
1175 build_target = build_target_lib.BuildTarget(self.board)
1176 result = packages.get_all_firmware_versions(build_target)
1177 self.assertEqual(result, {})
Benjamin Shai12c767e2022-01-12 15:17:44 +00001178
Michael Mortensen59e30872020-05-18 14:12:49 -06001179
Michael Mortensen71ef5682020-05-07 14:29:24 -06001180class GetFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001181 """Tests for get_firmware_versions."""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001182
Alex Klein1699fab2022-09-08 08:46:06 -06001183 def setUp(self):
1184 self.board = "test-board"
1185 self.rc.SetDefaultCmdResult(
1186 stdout="""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001187
1188flashrom(8): a8f99c2e61e7dc09c4b25ef5a76ef692 */build/kevin/usr/sbin/flashrom
1189 ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.d
1190 0.9.4 : 860875a : Apr 10 2017 23:54:29 UTC
1191
1192BIOS image: 6b5b855a0b8fd1657546d1402c15b206 *chromeos-firmware-kevin-0.0.1/.dist/kevin_fw_8785.178.0.n
1193BIOS version: Google_Kevin.8785.178.0
1194EC image: 1ebfa9518e6cac0558a80b7ab2f5b489 *chromeos-firmware-kevin-0.0.1/.dist/kevin_ec_8785.178.0.n
1195EC version:kevin_v1.10.184-459421c
1196
1197Package Content:
1198a8f99c2e61e7dc09c4b25ef5a76ef692 *./flashrom
11993c3a99346d1ca1273cbcd86c104851ff *./shflags
1200457a8dc8546764affc9700f8da328d23 *./dump_fmap
1201c392980ddb542639edf44a965a59361a *./updater5.sh
1202490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
12036b5b855a0b8fd1657546d1402c15b206 *./bios.bin
12047b5bef0d2da90c23ff2e157250edf0fa *./crosutil.sh
1205d78722e4f1a0dc2d8c3d6b0bc7010ae3 *./crossystem
1206457a8dc8546764affc9700f8da328d23 *./gbb_utility
12071ebfa9518e6cac0558a80b7ab2f5b489 *./ec.bin
1208c98ca54db130886142ad582a58e90ddc *./common.sh
12095ba978bdec0f696f47f0f0de90936880 *./mosys
1210312e8ee6122057f2a246d7bcf1572f49 *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001211"""
1212 )
Michael Mortensen71ef5682020-05-07 14:29:24 -06001213
Alex Klein1699fab2022-09-08 08:46:06 -06001214 def test_get_firmware_versions(self):
1215 """Tests get_firmware_versions with mocked output."""
1216 build_target = build_target_lib.BuildTarget(self.board)
1217 result = packages.get_firmware_versions(build_target)
1218 versions = packages.FirmwareVersions(
1219 None,
1220 "Google_Kevin.8785.178.0",
1221 None,
1222 "kevin_v1.10.184-459421c",
1223 None,
1224 )
1225 self.assertEqual(result, versions)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001226
1227
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001228class DetermineKernelVersionTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001229 """Tests for determine_kernel_version."""
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001230
Alex Klein1699fab2022-09-08 08:46:06 -06001231 def setUp(self):
1232 self.board = "test-board"
1233 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001234
Alex Klein1699fab2022-09-08 08:46:06 -06001235 def test_determine_kernel_version(self):
1236 """Tests that a valid kernel version is returned."""
1237 package_result = [
1238 "sys-kernel/linux-headers-4.14-r24",
1239 "sys-devel/flex-2.6.4-r1",
1240 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1241 ]
1242 self.PatchObject(
1243 portage_util, "GetPackageDependencies", return_value=package_result
1244 )
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001245
Alex Klein1699fab2022-09-08 08:46:06 -06001246 result = packages.determine_kernel_version(self.build_target)
1247 self.assertEqual(result, "4.4.223-r2209")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001248
Alex Klein1699fab2022-09-08 08:46:06 -06001249 def test_determine_kernel_version_exception(self):
1250 """Tests that portage_util exceptions result in returning None."""
1251 self.PatchObject(
1252 portage_util,
1253 "GetPackageDependencies",
1254 side_effect=cros_build_lib.RunCommandError("error"),
1255 )
1256 result = packages.determine_kernel_version(self.build_target)
1257 self.assertEqual(result, None)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001258
Alex Klein627e04c2021-11-10 15:56:47 -07001259
Michael Mortensenc2615b72019-10-15 08:12:24 -06001260class ChromeVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001261 """Tests getting chrome version."""
Michael Mortensen14960d02019-10-18 07:53:59 -06001262
Alex Klein1699fab2022-09-08 08:46:06 -06001263 def setUp(self):
1264 self.build_target = build_target_lib.BuildTarget("board")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001265
Alex Klein1699fab2022-09-08 08:46:06 -06001266 def test_determine_chrome_version(self):
1267 """Tests that a valid chrome version is returned."""
1268 # Mock PortageqBestVisible to return a valid chrome version string.
1269 r1_cpf = "chromeos-base/chromeos-chrome-78.0.3900.0_rc-r1"
1270 r1_cpv = package_info.SplitCPV(r1_cpf)
1271 self.PatchObject(
1272 portage_util, "PortageqBestVisible", return_value=r1_cpv
1273 )
Michael Mortensenc2615b72019-10-15 08:12:24 -06001274
Alex Klein1699fab2022-09-08 08:46:06 -06001275 chrome_version = packages.determine_chrome_version(self.build_target)
1276 version_numbers = chrome_version.split(".")
1277 self.assertEqual(len(version_numbers), 4)
1278 self.assertEqual(int(version_numbers[0]), 78)
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001279
Alex Klein1699fab2022-09-08 08:46:06 -06001280 def test_determine_chrome_version_handle_exception(self):
1281 # Mock what happens when portage throws an exception that bubbles up (via
1282 # RunCommand)inside portage_util.PortageqBestVisible.
1283 self.PatchObject(
1284 portage_util,
1285 "PortageqBestVisible",
1286 side_effect=cros_build_lib.RunCommandError("error"),
1287 )
1288 target = packages.determine_chrome_version(self.build_target)
1289 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001290
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001291
1292class PlatformVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001293 """Tests getting platform version."""
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001294
Alex Klein1699fab2022-09-08 08:46:06 -06001295 def test_determine_platform_version(self):
1296 """Test checking that a valid platform version is returned."""
1297 platform_version = packages.determine_platform_version()
1298 # The returned platform version is something like 12603.0.0.
1299 version_string_list = platform_version.split(".")
1300 self.assertEqual(len(version_string_list), 3)
1301 # We don't want to check an exact version, but the first number should be
1302 # non-zero.
1303 self.assertGreaterEqual(int(version_string_list[0]), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001304
Alex Klein1699fab2022-09-08 08:46:06 -06001305 def test_determine_milestone_version(self):
1306 """Test checking that a valid milestone version is returned."""
1307 milestone_version = packages.determine_milestone_version()
1308 # Milestone version should be non-zero
1309 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001310
Alex Klein1699fab2022-09-08 08:46:06 -06001311 def test_determine_full_version(self):
1312 """Test checking that a valid full version is returned."""
1313 full_version = packages.determine_full_version()
1314 pattern = r"^R(\d+)-(\d+.\d+.\d+(-rc\d+)*)"
1315 m = re.match(pattern, full_version)
1316 self.assertTrue(m)
1317 milestone_version = m.group(1)
1318 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001319
Alex Klein1699fab2022-09-08 08:46:06 -06001320 def test_versions_based_on_mock(self):
1321 # Create a test version_info object, and than mock VersionInfo.from_repo
1322 # return it.
1323 test_platform_version = "12575.0.0"
1324 test_chrome_branch = "75"
1325 version_info_mock = chromeos_version.VersionInfo(test_platform_version)
1326 version_info_mock.chrome_branch = test_chrome_branch
1327 self.PatchObject(
1328 chromeos_version.VersionInfo,
1329 "from_repo",
1330 return_value=version_info_mock,
1331 )
1332 test_full_version = (
1333 "R" + test_chrome_branch + "-" + test_platform_version
1334 )
1335 platform_version = packages.determine_platform_version()
1336 milestone_version = packages.determine_milestone_version()
1337 full_version = packages.determine_full_version()
1338 self.assertEqual(platform_version, test_platform_version)
1339 self.assertEqual(milestone_version, test_chrome_branch)
1340 self.assertEqual(full_version, test_full_version)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001341
1342
1343# Each of the columns in the following table is a separate dimension along
1344# which Chrome uprev test cases can vary in behavior. The full test space would
1345# be the Cartesian product of the possible values of each column.
1346# 'CHROME_EBUILD' refers to the relationship between the version of the existing
1347# Chrome ebuild vs. the requested uprev version. 'FOLLOWER_EBUILDS' refers to
1348# the same relationship but for the packages defined in OTHER_CHROME_PACKAGES.
1349# 'EBUILDS MODIFIED' refers to whether any of the existing 9999 ebuilds have
1350# modified contents relative to their corresponding stable ebuilds.
1351#
1352# CHROME_EBUILD FOLLOWER_EBUILDS EBUILDS_MODIFIED
1353#
1354# HIGHER HIGHER YES
1355# SAME SAME NO
1356# LOWER LOWER
1357# DOESN'T EXIST YET
1358
1359# These test cases cover both CHROME & FOLLOWER ebuilds being identically
1360# higher, lower, or the same versions, with no modified ebuilds.
1361UPREV_VERSION_CASES = (
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001362 # Uprev.
Chris McDonaldea0312c2020-05-04 23:33:15 -06001363 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001364 "80.0.8080.0",
1365 "81.0.8181.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001366 # One added and one deleted for chrome and each "other" package.
1367 2 * (1 + len(constants.OTHER_CHROME_PACKAGES)),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001368 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001369 id="newer_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001370 ),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001371 # Revbump.
1372 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001373 "80.0.8080.0",
1374 "80.0.8080.0",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001375 2,
1376 True,
Alex Klein1699fab2022-09-08 08:46:06 -06001377 id="chrome_revbump",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001378 ),
Chris McDonaldea0312c2020-05-04 23:33:15 -06001379 # No files should be changed in these cases.
1380 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001381 "80.0.8080.0",
1382 "80.0.8080.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001383 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001384 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001385 id="same_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001386 ),
1387 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001388 "80.0.8080.0",
1389 "79.0.7979.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001390 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001391 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001392 id="older_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001393 ),
1394)
1395
1396
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001397@pytest.mark.parametrize(
Alex Klein1699fab2022-09-08 08:46:06 -06001398 "old_version, new_version, expected_count, modify_unstable",
1399 UPREV_VERSION_CASES,
1400)
1401def test_uprev_chrome_all_files_already_exist(
1402 old_version,
1403 new_version,
1404 expected_count,
1405 modify_unstable,
1406 monkeypatch,
1407 overlay_stack,
1408):
1409 """Test Chrome uprevs work as expected when all packages already exist."""
1410 (overlay,) = overlay_stack(1)
1411 monkeypatch.setattr(uprev_lib, "_CHROME_OVERLAY_PATH", overlay.path)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001412
Alex Klein1699fab2022-09-08 08:46:06 -06001413 unstable_chrome = cr.test.Package(
1414 "chromeos-base", "chromeos-chrome", version="9999", keywords="~*"
1415 )
1416 if modify_unstable:
1417 # Add some field not set in stable.
1418 unstable_chrome.depend = "foo/bar"
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001419
Alex Klein1699fab2022-09-08 08:46:06 -06001420 stable_chrome = cr.test.Package(
1421 "chromeos-base", "chromeos-chrome", version=f"{old_version}_rc-r1"
1422 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001423
Alex Klein1699fab2022-09-08 08:46:06 -06001424 overlay.add_package(unstable_chrome)
1425 overlay.add_package(stable_chrome)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001426
Alex Klein1699fab2022-09-08 08:46:06 -06001427 for pkg_str in constants.OTHER_CHROME_PACKAGES:
1428 category, pkg_name = pkg_str.split("/")
1429 unstable_pkg = cr.test.Package(
1430 category, pkg_name, version="9999", keywords="~*"
1431 )
1432 stable_pkg = cr.test.Package(
1433 category, pkg_name, version=f"{old_version}_rc-r1"
1434 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001435
Alex Klein1699fab2022-09-08 08:46:06 -06001436 overlay.add_package(unstable_pkg)
1437 overlay.add_package(stable_pkg)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001438
Alex Klein1699fab2022-09-08 08:46:06 -06001439 git_refs = [
1440 GitRef(
1441 path="/foo", ref=f"refs/tags/{new_version}", revision="stubcommit"
1442 )
1443 ]
1444 res = packages.uprev_chrome_from_ref(None, git_refs, None)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001445
Alex Klein1699fab2022-09-08 08:46:06 -06001446 modified_file_count = sum(len(m.files) for m in res.modified)
1447 assert modified_file_count == expected_count
Michael Mortensen125bb012020-05-21 14:02:10 -06001448
1449
Alex Klein1699fab2022-09-08 08:46:06 -06001450@pytest.mark.usefixtures("testcase_monkeypatch")
Michael Mortensen125bb012020-05-21 14:02:10 -06001451class GetModelsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001452 """Tests for get_models."""
Michael Mortensen125bb012020-05-21 14:02:10 -06001453
Alex Klein1699fab2022-09-08 08:46:06 -06001454 def setUp(self):
1455 self.board = "test-board"
1456 self.rc.SetDefaultCmdResult(stdout="pyro\nreef\nsnappy\n")
1457 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1458 build_bin = os.path.join(
1459 self.tempdir, constants.DEFAULT_CHROOT_DIR, "usr", "bin"
1460 )
1461 osutils.Touch(
1462 os.path.join(build_bin, "cros_config_host"), makedirs=True
1463 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001464
Alex Klein1699fab2022-09-08 08:46:06 -06001465 def testGetModels(self):
1466 """Test get_models."""
1467 build_target = build_target_lib.BuildTarget(self.board)
1468 result = packages.get_models(build_target)
1469 self.assertEqual(result, ["pyro", "reef", "snappy"])
Michael Mortensen359c1f32020-05-28 19:35:42 -06001470
1471
1472class GetKeyIdTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001473 """Tests for get_key_id."""
Michael Mortensen359c1f32020-05-28 19:35:42 -06001474
Alex Klein1699fab2022-09-08 08:46:06 -06001475 def setUp(self):
1476 self.board = "test-board"
1477 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensen359c1f32020-05-28 19:35:42 -06001478
Alex Klein1699fab2022-09-08 08:46:06 -06001479 def testGetKeyId(self):
1480 """Test get_key_id when _run_cros_config_host returns a key."""
1481 self.PatchObject(
1482 packages, "_run_cros_config_host", return_value=["key"]
1483 )
1484 result = packages.get_key_id(self.build_target, "model")
1485 self.assertEqual(result, "key")
Michael Mortensen359c1f32020-05-28 19:35:42 -06001486
Alex Klein1699fab2022-09-08 08:46:06 -06001487 def testGetKeyIdNoKey(self):
1488 """Test get_key_id when None should be returned."""
1489 self.PatchObject(
1490 packages, "_run_cros_config_host", return_value=["key1", "key2"]
1491 )
1492 result = packages.get_key_id(self.build_target, "model")
1493 self.assertEqual(result, None)
Ben Reiche779cf42020-12-15 03:21:31 +00001494
1495
Harvey Yang3eee06c2021-03-18 15:47:56 +08001496class GetLatestVersionTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001497 """Tests for get_latest_version_from_refs."""
Ben Reiche779cf42020-12-15 03:21:31 +00001498
Alex Klein1699fab2022-09-08 08:46:06 -06001499 def setUp(self):
1500 self.prefix = "refs/tags/drivefs_"
1501 # The tag ref template.
1502 ref_tpl = self.prefix + "%s"
Ben Reiche779cf42020-12-15 03:21:31 +00001503
Alex Klein1699fab2022-09-08 08:46:06 -06001504 self.latest = "44.0.20"
1505 self.versions = ["42.0.1", self.latest, "44.0.19", "39.0.15"]
1506 self.latest_ref = uprev_lib.GitRef(
1507 "/path", ref_tpl % self.latest, "abc123"
1508 )
1509 self.refs = [
1510 uprev_lib.GitRef("/path", ref_tpl % v, "abc123")
1511 for v in self.versions
1512 ]
Ben Reiche779cf42020-12-15 03:21:31 +00001513
Alex Klein1699fab2022-09-08 08:46:06 -06001514 def test_single_ref(self):
1515 """Test a single ref is supplied."""
1516 # pylint: disable=protected-access
1517 self.assertEqual(
1518 self.latest,
1519 packages._get_latest_version_from_refs(
1520 self.prefix, [self.latest_ref]
1521 ),
1522 )
Ben Reiche779cf42020-12-15 03:21:31 +00001523
Alex Klein1699fab2022-09-08 08:46:06 -06001524 def test_multiple_ref_versions(self):
1525 """Test multiple refs supplied."""
1526 # pylint: disable=protected-access
1527 self.assertEqual(
1528 self.latest,
1529 packages._get_latest_version_from_refs(self.prefix, self.refs),
1530 )
Ben Reiche779cf42020-12-15 03:21:31 +00001531
Alex Klein1699fab2022-09-08 08:46:06 -06001532 def test_no_refs_returns_none(self):
1533 """Test no refs supplied."""
1534 # pylint: disable=protected-access
1535 self.assertEqual(
1536 packages._get_latest_version_from_refs(self.prefix, []), None
1537 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001538
1539
Alex Klein6becabc2020-09-11 14:03:05 -06001540class NeedsChromeSourceTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001541 """Tests for needs_chrome_source."""
Alex Klein6becabc2020-09-11 14:03:05 -06001542
Alex Klein1699fab2022-09-08 08:46:06 -06001543 def _build_graph(self, with_chrome: bool, with_followers: bool):
1544 root = "/build/build_target"
1545 foo_bar = package_info.parse("foo/bar-1")
1546 chrome = package_info.parse(f"{constants.CHROME_CP}-1.2.3.4")
1547 followers = [
1548 package_info.parse(f"{pkg}-1.2.3.4")
1549 for pkg in constants.OTHER_CHROME_PACKAGES
1550 ]
1551 nodes = [dependency_graph.PackageNode(foo_bar, root)]
1552 root_pkgs = ["foo/bar-1"]
1553 if with_chrome:
1554 nodes.append(dependency_graph.PackageNode(chrome, root))
1555 root_pkgs.append(chrome.cpvr)
1556 if with_followers:
1557 nodes.extend(
1558 [dependency_graph.PackageNode(f, root) for f in followers]
1559 )
1560 root_pkgs.extend([f.cpvr for f in followers])
Alex Klein6becabc2020-09-11 14:03:05 -06001561
Alex Klein1699fab2022-09-08 08:46:06 -06001562 return dependency_graph.DependencyGraph(nodes, root, root_pkgs)
Alex Klein6becabc2020-09-11 14:03:05 -06001563
Alex Klein1699fab2022-09-08 08:46:06 -06001564 def test_needs_all(self):
1565 """Verify we need source when we have no prebuilts."""
1566 graph = self._build_graph(with_chrome=True, with_followers=True)
1567 self.PatchObject(
1568 depgraph, "get_sysroot_dependency_graph", return_value=graph
1569 )
1570 self.PatchObject(packages, "has_prebuilt", return_value=False)
1571 self.PatchObject(
1572 packages,
1573 "uprev_chrome",
1574 return_value=uprev_lib.UprevVersionedPackageResult(),
1575 )
Alex Klein6becabc2020-09-11 14:03:05 -06001576
Alex Klein1699fab2022-09-08 08:46:06 -06001577 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001578
Alex Klein1699fab2022-09-08 08:46:06 -06001579 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001580
Alex Klein1699fab2022-09-08 08:46:06 -06001581 self.assertTrue(result.needs_chrome_source)
1582 self.assertTrue(result.builds_chrome)
1583 self.assertTrue(result.packages)
1584 self.assertEqual(
1585 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1586 )
1587 self.assertTrue(result.missing_chrome_prebuilt)
1588 self.assertTrue(result.missing_follower_prebuilt)
1589 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001590
Alex Klein1699fab2022-09-08 08:46:06 -06001591 def test_needs_none(self):
1592 """Verify not building any of the chrome packages prevents needing it."""
1593 graph = self._build_graph(with_chrome=False, with_followers=False)
1594 self.PatchObject(
1595 depgraph, "get_sysroot_dependency_graph", return_value=graph
1596 )
1597 self.PatchObject(packages, "has_prebuilt", return_value=False)
1598 self.PatchObject(
1599 packages,
1600 "uprev_chrome",
1601 return_value=uprev_lib.UprevVersionedPackageResult(),
1602 )
Alex Klein6becabc2020-09-11 14:03:05 -06001603
Alex Klein1699fab2022-09-08 08:46:06 -06001604 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001605
Alex Klein1699fab2022-09-08 08:46:06 -06001606 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001607
Alex Klein1699fab2022-09-08 08:46:06 -06001608 self.assertFalse(result.needs_chrome_source)
1609 self.assertFalse(result.builds_chrome)
1610 self.assertFalse(result.packages)
1611 self.assertFalse(result.missing_chrome_prebuilt)
1612 self.assertFalse(result.missing_follower_prebuilt)
1613 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001614
Alex Klein1699fab2022-09-08 08:46:06 -06001615 def test_needs_chrome_only(self):
1616 """Verify only chrome triggers needs chrome source."""
1617 graph = self._build_graph(with_chrome=True, with_followers=False)
1618 self.PatchObject(
1619 depgraph, "get_sysroot_dependency_graph", return_value=graph
1620 )
1621 self.PatchObject(packages, "has_prebuilt", return_value=False)
1622 self.PatchObject(
1623 packages,
1624 "uprev_chrome",
1625 return_value=uprev_lib.UprevVersionedPackageResult(),
1626 )
Alex Klein6becabc2020-09-11 14:03:05 -06001627
Alex Klein1699fab2022-09-08 08:46:06 -06001628 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001629
Alex Klein1699fab2022-09-08 08:46:06 -06001630 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001631
Alex Klein1699fab2022-09-08 08:46:06 -06001632 self.assertTrue(result.needs_chrome_source)
1633 self.assertTrue(result.builds_chrome)
1634 self.assertTrue(result.packages)
1635 self.assertEqual(
1636 set([p.atom for p in result.packages]), {constants.CHROME_CP}
1637 )
1638 self.assertTrue(result.missing_chrome_prebuilt)
1639 self.assertFalse(result.missing_follower_prebuilt)
1640 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001641
Alex Klein1699fab2022-09-08 08:46:06 -06001642 def test_needs_followers_only(self):
1643 """Verify only chrome followers triggers needs chrome source."""
1644 graph = self._build_graph(with_chrome=False, with_followers=True)
1645 self.PatchObject(
1646 depgraph, "get_sysroot_dependency_graph", return_value=graph
1647 )
1648 self.PatchObject(packages, "has_prebuilt", return_value=False)
1649 self.PatchObject(
1650 packages,
1651 "uprev_chrome",
1652 return_value=uprev_lib.UprevVersionedPackageResult(),
1653 )
Alex Klein6becabc2020-09-11 14:03:05 -06001654
Alex Klein1699fab2022-09-08 08:46:06 -06001655 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001656
Alex Klein1699fab2022-09-08 08:46:06 -06001657 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001658
Alex Klein1699fab2022-09-08 08:46:06 -06001659 self.assertTrue(result.needs_chrome_source)
1660 self.assertFalse(result.builds_chrome)
1661 self.assertTrue(result.packages)
1662 self.assertEqual(
1663 set([p.atom for p in result.packages]),
1664 set(constants.OTHER_CHROME_PACKAGES),
1665 )
1666 self.assertFalse(result.missing_chrome_prebuilt)
1667 self.assertTrue(result.missing_follower_prebuilt)
1668 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001669
Alex Klein1699fab2022-09-08 08:46:06 -06001670 def test_has_prebuilts(self):
1671 """Test prebuilts prevent us from needing chrome source."""
1672 graph = self._build_graph(with_chrome=True, with_followers=True)
1673 self.PatchObject(
1674 depgraph, "get_sysroot_dependency_graph", return_value=graph
1675 )
1676 self.PatchObject(packages, "has_prebuilt", return_value=True)
1677 self.PatchObject(
1678 packages,
1679 "uprev_chrome",
1680 return_value=uprev_lib.UprevVersionedPackageResult(),
1681 )
Alex Klein6becabc2020-09-11 14:03:05 -06001682
Alex Klein1699fab2022-09-08 08:46:06 -06001683 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001684
Alex Klein1699fab2022-09-08 08:46:06 -06001685 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001686
Alex Klein1699fab2022-09-08 08:46:06 -06001687 self.assertFalse(result.needs_chrome_source)
1688 self.assertTrue(result.builds_chrome)
1689 self.assertFalse(result.packages)
1690 self.assertFalse(result.missing_chrome_prebuilt)
1691 self.assertFalse(result.missing_follower_prebuilt)
1692 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001693
Alex Klein1699fab2022-09-08 08:46:06 -06001694 def test_compile_source(self):
1695 """Test compile source ignores prebuilts."""
1696 graph = self._build_graph(with_chrome=True, with_followers=True)
1697 self.PatchObject(
1698 depgraph, "get_sysroot_dependency_graph", return_value=graph
1699 )
1700 self.PatchObject(packages, "has_prebuilt", return_value=True)
1701 self.PatchObject(
1702 packages,
1703 "uprev_chrome",
1704 return_value=uprev_lib.UprevVersionedPackageResult(),
1705 )
Alex Klein6becabc2020-09-11 14:03:05 -06001706
Alex Klein1699fab2022-09-08 08:46:06 -06001707 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001708
Alex Klein1699fab2022-09-08 08:46:06 -06001709 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Klein6becabc2020-09-11 14:03:05 -06001710
Alex Klein1699fab2022-09-08 08:46:06 -06001711 self.assertTrue(result.needs_chrome_source)
1712 self.assertTrue(result.builds_chrome)
1713 self.assertTrue(result.packages)
1714 self.assertEqual(
1715 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1716 )
1717 self.assertTrue(result.missing_chrome_prebuilt)
1718 self.assertTrue(result.missing_follower_prebuilt)
1719 self.assertFalse(result.local_uprev)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001720
Alex Klein1699fab2022-09-08 08:46:06 -06001721 def test_local_uprev(self):
1722 """Test compile source ignores prebuilts."""
1723 graph = self._build_graph(with_chrome=True, with_followers=True)
1724 self.PatchObject(
1725 depgraph, "get_sysroot_dependency_graph", return_value=graph
1726 )
1727 self.PatchObject(packages, "has_prebuilt", return_value=False)
Alex Klein75110572021-07-14 10:44:39 -06001728
Alex Klein1699fab2022-09-08 08:46:06 -06001729 uprev_result = uprev_lib.UprevVersionedPackageResult()
1730 uprev_result.add_result("1.2.3.4", ["/tmp/foo"])
1731 self.PatchObject(packages, "uprev_chrome", return_value=uprev_result)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001732
Alex Klein1699fab2022-09-08 08:46:06 -06001733 build_target = build_target_lib.BuildTarget("build_target")
Alex Kleinde7b76d2021-07-12 12:28:44 -06001734
Alex Klein1699fab2022-09-08 08:46:06 -06001735 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001736
Alex Klein1699fab2022-09-08 08:46:06 -06001737 self.assertTrue(result.needs_chrome_source)
1738 self.assertTrue(result.builds_chrome)
1739 self.assertTrue(result.packages)
1740 self.assertEqual(
1741 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1742 )
1743 self.assertTrue(result.missing_chrome_prebuilt)
1744 self.assertTrue(result.missing_follower_prebuilt)
1745 self.assertTrue(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001746
1747
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001748class UprevDrivefsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001749 """Tests for uprev_drivefs."""
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001750
Alex Klein1699fab2022-09-08 08:46:06 -06001751 def setUp(self):
1752 self.refs = [
1753 GitRef(
1754 path="/chromeos/platform/drivefs-google3/",
1755 ref="refs/tags/drivefs_45.0.2",
1756 revision="123",
1757 )
1758 ]
1759 self.MOCK_DRIVEFS_EBUILD_PATH = "drivefs.45.0.2-r1.ebuild"
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001760
Alex Klein1699fab2022-09-08 08:46:06 -06001761 def revisionBumpOutcome(self, ebuild_path):
1762 return uprev_lib.UprevResult(
1763 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
1764 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001765
Alex Klein1699fab2022-09-08 08:46:06 -06001766 def majorBumpOutcome(self, ebuild_path):
1767 return uprev_lib.UprevResult(
1768 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
1769 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001770
Alex Klein1699fab2022-09-08 08:46:06 -06001771 def sameVersionOutcome(self):
1772 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001773
Alex Klein1699fab2022-09-08 08:46:06 -06001774 def test_latest_version_returns_none(self):
1775 """Test no refs were supplied"""
1776 output = packages.uprev_drivefs(None, [], None)
1777 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001778
Alex Klein1699fab2022-09-08 08:46:06 -06001779 def test_drivefs_uprev_fails(self):
1780 """Test a single ref is supplied."""
1781 self.PatchObject(
1782 uprev_lib,
1783 "uprev_workon_ebuild_to_version",
1784 side_effect=[None, None],
1785 )
1786 output = packages.uprev_drivefs(None, self.refs, None)
1787 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001788
Alex Klein1699fab2022-09-08 08:46:06 -06001789 def test_same_version_exists(self):
1790 """Test the same version exists uprev should not happen."""
1791 drivefs_outcome = self.sameVersionOutcome()
1792 self.PatchObject(
1793 uprev_lib,
1794 "uprev_workon_ebuild_to_version",
1795 side_effect=[drivefs_outcome],
1796 )
1797 output = packages.uprev_drivefs(None, self.refs, None)
1798 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001799
Alex Klein1699fab2022-09-08 08:46:06 -06001800 def test_revision_bump_both_packages(self):
1801 """Test both packages uprev, should succeed."""
1802 drivefs_outcome = self.revisionBumpOutcome(
1803 self.MOCK_DRIVEFS_EBUILD_PATH
1804 )
1805 self.PatchObject(
1806 uprev_lib,
1807 "uprev_workon_ebuild_to_version",
1808 side_effect=[drivefs_outcome],
1809 )
1810 output = packages.uprev_drivefs(None, self.refs, None)
1811 self.assertTrue(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001812
Alex Klein1699fab2022-09-08 08:46:06 -06001813 def test_major_bump_both_packages(self):
1814 """Test both packages uprev, should succeed."""
1815 drivefs_outcome = self.majorBumpOutcome(self.MOCK_DRIVEFS_EBUILD_PATH)
1816 self.PatchObject(
1817 uprev_lib,
1818 "uprev_workon_ebuild_to_version",
1819 side_effect=[drivefs_outcome],
1820 )
1821 output = packages.uprev_drivefs(None, self.refs, None)
1822 self.assertTrue(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001823
1824
1825# TODO(chenghaoyang): Shouldn't use uprev_workon_ebuild_to_version.
1826class UprevPerfettoTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001827 """Tests for uprev_perfetto."""
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001828
Alex Klein1699fab2022-09-08 08:46:06 -06001829 def setUp(self):
1830 self.refs = [GitRef(path="/foo", ref="refs/tags/v12.0", revision="123")]
1831 self.MOCK_PERFETTO_EBUILD_PATH = "perfetto-12.0-r1.ebuild"
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001832
Alex Klein1699fab2022-09-08 08:46:06 -06001833 def revisionBumpOutcome(self, ebuild_path):
1834 return uprev_lib.UprevResult(
1835 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
1836 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001837
Alex Klein1699fab2022-09-08 08:46:06 -06001838 def majorBumpOutcome(self, ebuild_path):
1839 return uprev_lib.UprevResult(
1840 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
1841 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001842
Alex Klein1699fab2022-09-08 08:46:06 -06001843 def newerVersionOutcome(self):
1844 return uprev_lib.UprevResult(uprev_lib.Outcome.NEWER_VERSION_EXISTS)
Harvey Yang3eee06c2021-03-18 15:47:56 +08001845
Alex Klein1699fab2022-09-08 08:46:06 -06001846 def sameVersionOutcome(self):
1847 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001848
Alex Klein1699fab2022-09-08 08:46:06 -06001849 def test_latest_version_returns_none(self):
1850 """Test no refs were supplied"""
1851 output = packages.uprev_perfetto(None, [], None)
1852 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001853
Alex Klein1699fab2022-09-08 08:46:06 -06001854 def test_perfetto_uprev_fails(self):
1855 """Test a single ref is supplied."""
1856 self.PatchObject(
1857 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
1858 )
1859 output = packages.uprev_perfetto(None, self.refs, None)
1860 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001861
Alex Klein1699fab2022-09-08 08:46:06 -06001862 def test_newer_version_exists(self):
1863 """Test the newer version exists uprev should not happen."""
1864 perfetto_outcome = self.newerVersionOutcome()
1865 self.PatchObject(
1866 uprev_lib,
1867 "uprev_workon_ebuild_to_version",
1868 side_effect=[perfetto_outcome],
1869 )
1870 output = packages.uprev_perfetto(None, self.refs, None)
1871 self.assertFalse(output.uprevved)
Harvey Yang3eee06c2021-03-18 15:47:56 +08001872
Alex Klein1699fab2022-09-08 08:46:06 -06001873 def test_same_version_exists(self):
1874 """Test the same version exists uprev should not happen."""
1875 perfetto_outcome = self.sameVersionOutcome()
1876 self.PatchObject(
1877 uprev_lib,
1878 "uprev_workon_ebuild_to_version",
1879 side_effect=[perfetto_outcome],
1880 )
1881 output = packages.uprev_perfetto(None, self.refs, None)
1882 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001883
Alex Klein1699fab2022-09-08 08:46:06 -06001884 def test_revision_bump_perfetto_package(self):
1885 """Test perfetto package uprev."""
1886 perfetto_outcome = self.revisionBumpOutcome(
1887 self.MOCK_PERFETTO_EBUILD_PATH
1888 )
1889 self.PatchObject(
1890 uprev_lib,
1891 "uprev_workon_ebuild_to_version",
1892 side_effect=[perfetto_outcome],
1893 )
1894 output = packages.uprev_perfetto(None, self.refs, None)
1895 self.assertTrue(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001896
Alex Klein1699fab2022-09-08 08:46:06 -06001897 def test_major_bump_perfetto_package(self):
1898 """Test perfetto package uprev."""
1899 perfetto_outcome = self.majorBumpOutcome(self.MOCK_PERFETTO_EBUILD_PATH)
1900 self.PatchObject(
1901 uprev_lib,
1902 "uprev_workon_ebuild_to_version",
1903 side_effect=[perfetto_outcome],
1904 )
1905 output = packages.uprev_perfetto(None, self.refs, None)
1906 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00001907
Alex Klein627e04c2021-11-10 15:56:47 -07001908
Julio Hurtadof1befec2021-05-05 21:34:26 +00001909class UprevLacrosTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001910 """Tests for uprev_lacros"""
Julio Hurtadof1befec2021-05-05 21:34:26 +00001911
Alex Klein1699fab2022-09-08 08:46:06 -06001912 def setUp(self):
1913 self.refs = [
1914 GitRef(
1915 path="/lacros", ref="refs/heads/main", revision="123.456.789.0"
1916 )
1917 ]
1918 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtadof1befec2021-05-05 21:34:26 +00001919
Alex Klein1699fab2022-09-08 08:46:06 -06001920 def revisionBumpOutcome(self, ebuild_path):
1921 return uprev_lib.UprevResult(
1922 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
1923 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00001924
Alex Klein1699fab2022-09-08 08:46:06 -06001925 def majorBumpOutcome(self, ebuild_path):
1926 return uprev_lib.UprevResult(
1927 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
1928 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00001929
Alex Klein1699fab2022-09-08 08:46:06 -06001930 def newerVersionOutcome(self, ebuild_path):
1931 return uprev_lib.UprevResult(
1932 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
1933 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00001934
Alex Klein1699fab2022-09-08 08:46:06 -06001935 def sameVersionOutcome(self, ebuild_path):
1936 return uprev_lib.UprevResult(
1937 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
1938 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00001939
Alex Klein1699fab2022-09-08 08:46:06 -06001940 def newEbuildCreatedOutcome(self, ebuild_path):
1941 return uprev_lib.UprevResult(
1942 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
1943 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00001944
Alex Klein1699fab2022-09-08 08:46:06 -06001945 def test_lacros_uprev_fails(self):
1946 """Test a lacros package uprev with no triggers"""
1947 self.PatchObject(
1948 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
1949 )
1950 with self.assertRaises(IndexError):
1951 packages.uprev_lacros(None, [], None)
Julio Hurtadof1befec2021-05-05 21:34:26 +00001952
Alex Klein1699fab2022-09-08 08:46:06 -06001953 def test_lacros_uprev_revision_bump(self):
1954 """Test lacros package uprev."""
1955 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
1956 self.PatchObject(
1957 uprev_lib,
1958 "uprev_workon_ebuild_to_version",
1959 side_effect=[lacros_outcome],
1960 )
1961 output = packages.uprev_lacros(None, self.refs, None)
1962 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00001963
Alex Klein1699fab2022-09-08 08:46:06 -06001964 def test_lacros_uprev_version_bump(self):
1965 """Test lacros package uprev."""
1966 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
1967 self.PatchObject(
1968 uprev_lib,
1969 "uprev_workon_ebuild_to_version",
1970 side_effect=[lacros_outcome],
1971 )
1972 output = packages.uprev_lacros(None, self.refs, None)
1973 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00001974
Alex Klein1699fab2022-09-08 08:46:06 -06001975 def test_lacros_uprev_new_ebuild_created(self):
1976 """Test lacros package uprev."""
1977 lacros_outcome = self.newEbuildCreatedOutcome(
1978 self.MOCK_LACROS_EBUILD_PATH
1979 )
1980 self.PatchObject(
1981 uprev_lib,
1982 "uprev_workon_ebuild_to_version",
1983 side_effect=[lacros_outcome],
1984 )
1985 output = packages.uprev_lacros(None, self.refs, None)
1986 self.assertTrue(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00001987
Alex Klein1699fab2022-09-08 08:46:06 -06001988 def test_lacros_uprev_newer_version_exist(self):
1989 """Test the newer version exists uprev should not happen."""
1990 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
1991 self.PatchObject(
1992 uprev_lib,
1993 "uprev_workon_ebuild_to_version",
1994 side_effect=[lacros_outcome],
1995 )
1996 output = packages.uprev_lacros(None, self.refs, None)
1997 self.assertFalse(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00001998
Alex Klein1699fab2022-09-08 08:46:06 -06001999 def test_lacros_uprev_same_version_exist(self):
2000 """Test the same version exists uprev should not happen."""
2001 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2002 self.PatchObject(
2003 uprev_lib,
2004 "uprev_workon_ebuild_to_version",
2005 side_effect=[lacros_outcome],
2006 )
2007 output = packages.uprev_lacros(None, self.refs, None)
2008 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002009
2010
2011class UprevLacrosInParallelTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002012 """Tests for uprev_lacros"""
Julio Hurtado870ed322021-12-03 18:22:40 +00002013
Alex Klein1699fab2022-09-08 08:46:06 -06002014 def setUp(self):
2015 self.refs = [
2016 GitRef(
2017 path="/lacros", revision="abc123", ref="refs/tags/123.456.789.0"
2018 )
2019 ]
2020 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtado870ed322021-12-03 18:22:40 +00002021
Alex Klein1699fab2022-09-08 08:46:06 -06002022 def revisionBumpOutcome(self, ebuild_path):
2023 return uprev_lib.UprevResult(
2024 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2025 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002026
Alex Klein1699fab2022-09-08 08:46:06 -06002027 def majorBumpOutcome(self, ebuild_path):
2028 return uprev_lib.UprevResult(
2029 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2030 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002031
Alex Klein1699fab2022-09-08 08:46:06 -06002032 def newerVersionOutcome(self, ebuild_path):
2033 return uprev_lib.UprevResult(
2034 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2035 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002036
Alex Klein1699fab2022-09-08 08:46:06 -06002037 def sameVersionOutcome(self, ebuild_path):
2038 return uprev_lib.UprevResult(
2039 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2040 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002041
Alex Klein1699fab2022-09-08 08:46:06 -06002042 def newEbuildCreatedOutcome(self, ebuild_path):
2043 return uprev_lib.UprevResult(
2044 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2045 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002046
Alex Klein1699fab2022-09-08 08:46:06 -06002047 def test_lacros_uprev_fails(self):
2048 """Test a lacros package uprev with no triggers"""
2049 self.PatchObject(
2050 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2051 )
2052 with self.assertRaises(TypeError):
2053 packages.uprev_lacros_in_parallel(None, [], None)
Julio Hurtado870ed322021-12-03 18:22:40 +00002054
Alex Klein1699fab2022-09-08 08:46:06 -06002055 def test_lacros_uprev_revision_bump(self):
2056 """Test lacros package uprev."""
2057 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2058 self.PatchObject(
2059 uprev_lib,
2060 "uprev_workon_ebuild_to_version",
2061 side_effect=[lacros_outcome],
2062 )
2063 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2064 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002065
Alex Klein1699fab2022-09-08 08:46:06 -06002066 def test_lacros_uprev_version_bump(self):
2067 """Test lacros package uprev."""
2068 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2069 self.PatchObject(
2070 uprev_lib,
2071 "uprev_workon_ebuild_to_version",
2072 side_effect=[lacros_outcome],
2073 )
2074 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2075 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002076
Alex Klein1699fab2022-09-08 08:46:06 -06002077 def test_lacros_uprev_new_ebuild_created(self):
2078 """Test lacros package uprev."""
2079 lacros_outcome = self.newEbuildCreatedOutcome(
2080 self.MOCK_LACROS_EBUILD_PATH
2081 )
2082 self.PatchObject(
2083 uprev_lib,
2084 "uprev_workon_ebuild_to_version",
2085 side_effect=[lacros_outcome],
2086 )
2087 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2088 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002089
Alex Klein1699fab2022-09-08 08:46:06 -06002090 def test_lacros_uprev_newer_version_exist(self):
2091 """Test the newer version exists uprev should not happen."""
2092 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2093 self.PatchObject(
2094 uprev_lib,
2095 "uprev_workon_ebuild_to_version",
2096 side_effect=[lacros_outcome],
2097 )
2098 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2099 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002100
Alex Klein1699fab2022-09-08 08:46:06 -06002101 def test_lacros_uprev_same_version_exist(self):
2102 """Test the same version exists uprev should not happen."""
2103 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2104 self.PatchObject(
2105 uprev_lib,
2106 "uprev_workon_ebuild_to_version",
2107 side_effect=[lacros_outcome],
2108 )
2109 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2110 self.assertFalse(output.uprevved)