blob: 593a0f5497c0285f89d079c6b91aa11d0de3ea1e [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Kleineb77ffa2019-05-28 14:47:44 -06002# 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
Alex Klein220e3a72023-09-14 17:12:13 -060013from chromite.third_party.google.protobuf import field_mask_pb2
Mike Frysinger2c024062021-05-22 15:43:22 -040014from chromite.third_party.google.protobuf import json_format
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
Trent Apted39e74d32023-09-04 11:24:40 +100018from chromite.api.gen.config import replication_config_pb2
Alex Klein2960c752020-03-09 13:43:38 -060019from chromite.lib import build_target_lib
Ram Chandrasekar60f69f32022-06-03 22:49:30 +000020from chromite.lib import chromeos_version
Alex Klein220e3a72023-09-14 17:12:13 -060021from chromite.lib import chroot_lib
Andrew Lamb2bde9e42019-11-04 13:24:09 -070022from chromite.lib import constants
Michael Mortensene0f4b542019-10-24 15:30:23 -060023from chromite.lib import cros_build_lib
Alex Kleineb77ffa2019-05-28 14:47:44 -060024from chromite.lib import cros_test_lib
Alex Klein6becabc2020-09-11 14:03:05 -060025from chromite.lib import dependency_graph
Mike Frysinger68796b52019-08-25 00:04:27 -040026from chromite.lib import depgraph
Michael Mortensenb70e8a82019-10-10 18:43:41 -060027from chromite.lib import osutils
Mike Frysinger88d96362020-02-14 19:05:45 -050028from chromite.lib import partial_mock
Alex Klein87531182019-08-12 15:23:37 -060029from chromite.lib import portage_util
Chris McDonaldea0312c2020-05-04 23:33:15 -060030from chromite.lib import uprev_lib
Alex Klein18a60af2020-06-11 12:08:47 -060031from chromite.lib.parser import package_info
Shao-Chuan Lee05e51142021-11-24 12:27:37 +090032from chromite.service import android
Mike Frysinger8c9d7582023-08-21 19:28:21 -040033from chromite.service import dependency
Alex Kleineb77ffa2019-05-28 14:47:44 -060034from chromite.service import packages
35
Mike Frysinger68796b52019-08-25 00:04:27 -040036
Andrew Lamb2bde9e42019-11-04 13:24:09 -070037D = cros_test_lib.Directory
Trent Apted39e74d32023-09-04 11:24:40 +100038FILE_TYPE_JSON = replication_config_pb2.FILE_TYPE_JSON
39FileReplicationRule = replication_config_pb2.FileReplicationRule
40REPLICATION_TYPE_FILTER = replication_config_pb2.REPLICATION_TYPE_FILTER
41ReplicationConfig = replication_config_pb2.ReplicationConfig
Andrew Lamb2bde9e42019-11-04 13:24:09 -070042
Alex Kleineb77ffa2019-05-28 14:47:44 -060043
Alex Klein4de25e82019-08-05 15:58:39 -060044class UprevAndroidTest(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060045 """Uprev android tests."""
Alex Klein4de25e82019-08-05 15:58:39 -060046
Alex Klein1699fab2022-09-08 08:46:06 -060047 def _mock_successful_uprev(self):
48 self.rc.AddCmdResult(
49 partial_mock.In("cros_mark_android_as_stable"),
50 stdout=(
51 '{"revved": true,'
52 ' "android_atom": "android/android-1.0",'
53 ' "modified_files": ["file1", "file2"]}'
54 ),
55 )
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090056
Alex Klein1699fab2022-09-08 08:46:06 -060057 def test_success(self):
58 """Test successful run handling."""
59 self._mock_successful_uprev()
60 build_targets = [
61 build_target_lib.BuildTarget(t) for t in ["foo", "bar"]
62 ]
Alex Klein4de25e82019-08-05 15:58:39 -060063
Alex Klein1699fab2022-09-08 08:46:06 -060064 result = packages.uprev_android(
Alex Klein220e3a72023-09-14 17:12:13 -060065 "android/package", chroot_lib.Chroot(), build_targets=build_targets
Alex Klein1699fab2022-09-08 08:46:06 -060066 )
67 self.assertCommandContains(
68 [
69 "cros_mark_android_as_stable",
70 "--android_package=android/package",
71 "--boards=foo:bar",
72 ]
73 )
74 self.assertCommandContains(["emerge-foo"])
75 self.assertCommandContains(["emerge-bar"])
Alex Klein4de25e82019-08-05 15:58:39 -060076
Alex Klein1699fab2022-09-08 08:46:06 -060077 self.assertTrue(result.revved)
78 self.assertEqual(result.android_atom, "android/android-1.0")
79 self.assertListEqual(result.modified_files, ["file1", "file2"])
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090080
Alex Klein1699fab2022-09-08 08:46:06 -060081 def test_android_build_branch(self):
82 """Test specifying android_build_branch option."""
83 self._mock_successful_uprev()
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090084
Alex Klein1699fab2022-09-08 08:46:06 -060085 packages.uprev_android(
86 "android/package",
Alex Klein220e3a72023-09-14 17:12:13 -060087 chroot_lib.Chroot(),
Alex Klein1699fab2022-09-08 08:46:06 -060088 android_build_branch="android-build-branch",
89 )
90 self.assertCommandContains(
91 [
92 "cros_mark_android_as_stable",
93 "--android_package=android/package",
94 "--android_build_branch=android-build-branch",
95 ]
96 )
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090097
Alex Klein1699fab2022-09-08 08:46:06 -060098 def test_android_version(self):
99 """Test specifying android_version option."""
100 self._mock_successful_uprev()
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900101
Alex Klein1699fab2022-09-08 08:46:06 -0600102 packages.uprev_android(
Alex Klein220e3a72023-09-14 17:12:13 -0600103 "android/package", chroot_lib.Chroot(), android_version="7123456"
Alex Klein1699fab2022-09-08 08:46:06 -0600104 )
105 self.assertCommandContains(
106 [
107 "cros_mark_android_as_stable",
108 "--android_package=android/package",
109 "--force_version=7123456",
110 ]
111 )
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900112
Alex Klein1699fab2022-09-08 08:46:06 -0600113 def test_skip_commit(self):
114 """Test specifying skip_commit option."""
115 self._mock_successful_uprev()
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900116
Alex Klein220e3a72023-09-14 17:12:13 -0600117 packages.uprev_android(
118 "android/package", chroot_lib.Chroot(), skip_commit=True
119 )
Alex Klein1699fab2022-09-08 08:46:06 -0600120 self.assertCommandContains(
121 [
122 "cros_mark_android_as_stable",
123 "--android_package=android/package",
124 "--skip_commit",
125 ]
126 )
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900127
Alex Klein1699fab2022-09-08 08:46:06 -0600128 def test_no_uprev(self):
129 """Test no uprev handling."""
130 self.rc.AddCmdResult(
131 partial_mock.In("cros_mark_android_as_stable"),
132 stdout='{"revved": false}',
133 )
134 build_targets = [
135 build_target_lib.BuildTarget(t) for t in ["foo", "bar"]
136 ]
137 result = packages.uprev_android(
Alex Klein220e3a72023-09-14 17:12:13 -0600138 "android/package", chroot_lib.Chroot(), build_targets=build_targets
Alex Klein1699fab2022-09-08 08:46:06 -0600139 )
Alex Klein4de25e82019-08-05 15:58:39 -0600140
Alex Klein1699fab2022-09-08 08:46:06 -0600141 self.assertCommandContains(
142 ["cros_mark_android_as_stable", "--boards=foo:bar"]
143 )
144 self.assertCommandContains(["emerge-foo"], expected=False)
145 self.assertCommandContains(["emerge-bar"], expected=False)
Alex Klein4de25e82019-08-05 15:58:39 -0600146
Alex Klein1699fab2022-09-08 08:46:06 -0600147 self.assertFalse(result.revved)
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900148
Alex Klein1699fab2022-09-08 08:46:06 -0600149 def test_ignore_junk_in_stdout(self):
150 """Test when stdout contains junk messages."""
151 self.rc.AddCmdResult(
152 partial_mock.In("cros_mark_android_as_stable"),
153 stdout='foo\nbar\n{"revved": false}\n',
154 )
Alex Klein220e3a72023-09-14 17:12:13 -0600155 result = packages.uprev_android("android/package", chroot_lib.Chroot())
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900156
Alex Klein1699fab2022-09-08 08:46:06 -0600157 self.assertFalse(result.revved)
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900158
Alex Klein4de25e82019-08-05 15:58:39 -0600159
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900160class UprevAndroidLKGBTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600161 """Tests for uprevving Android with LKGB."""
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900162
Alex Klein1699fab2022-09-08 08:46:06 -0600163 def test_registered_handlers(self):
164 """Test that each Android package has an uprev handler registered."""
165 mock_handler = self.PatchObject(packages, "uprev_android_lkgb")
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900166
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +0900167 for android_package in android.GetAllAndroidPackages():
Alex Klein1699fab2022-09-08 08:46:06 -0600168 cpv = package_info.SplitCPV(
169 "chromeos-base/" + android_package, strict=False
170 )
171 build_targets = [build_target_lib.BuildTarget("foo")]
Alex Klein220e3a72023-09-14 17:12:13 -0600172 chroot = chroot_lib.Chroot()
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900173
Alex Klein1699fab2022-09-08 08:46:06 -0600174 packages.uprev_versioned_package(cpv, build_targets, [], chroot)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900175
Alex Klein1699fab2022-09-08 08:46:06 -0600176 mock_handler.assert_called_once_with(
177 android_package, build_targets, chroot
178 )
179 mock_handler.reset_mock()
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900180
Alex Klein1699fab2022-09-08 08:46:06 -0600181 def test_success(self):
182 """Test a successful uprev."""
183 self.PatchObject(android, "OVERLAY_DIR", new="overlay-dir")
Shao-Chuan Leee0b9ba92023-01-18 19:35:36 +0900184 self.PatchObject(
185 android, "ReadLKGB", return_value=dict(build_id="android-lkgb")
186 )
Alex Klein1699fab2022-09-08 08:46:06 -0600187 self.PatchObject(
188 packages,
189 "uprev_android",
190 return_value=packages.UprevAndroidResult(
191 revved=True,
192 android_atom="android-atom",
193 modified_files=["file1", "file2"],
194 ),
195 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900196
Alex Klein220e3a72023-09-14 17:12:13 -0600197 result = packages.uprev_android_lkgb(
198 "android-package", [], chroot_lib.Chroot()
199 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900200
Alex Klein1699fab2022-09-08 08:46:06 -0600201 self.assertListEqual(
202 result.modified,
203 [
204 uprev_lib.UprevVersionedPackageModifications(
205 "android-lkgb",
206 [
207 os.path.join("overlay-dir", "file1"),
208 os.path.join("overlay-dir", "file2"),
209 ],
210 )
211 ],
212 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900213
Alex Klein1699fab2022-09-08 08:46:06 -0600214 def test_no_rev(self):
215 """Test when nothing revved."""
Shao-Chuan Leee0b9ba92023-01-18 19:35:36 +0900216 self.PatchObject(
217 android, "ReadLKGB", return_value=dict(build_id="android-lkgb")
218 )
Alex Klein1699fab2022-09-08 08:46:06 -0600219 self.PatchObject(
220 packages,
221 "uprev_android",
222 return_value=packages.UprevAndroidResult(revved=False),
223 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900224
Alex Klein220e3a72023-09-14 17:12:13 -0600225 result = packages.uprev_android_lkgb(
226 "android-package", [], chroot_lib.Chroot()
227 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900228
Alex Klein1699fab2022-09-08 08:46:06 -0600229 self.assertListEqual(result.modified, [])
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900230
231
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700232class UprevECUtilsTest(cros_test_lib.MockTestCase):
233 """Tests for upreving ecutils."""
234
235 def test_success(self):
236 """Test a successful uprev."""
237
238 def fakeRunTasks(func, inputs):
239 results = []
240 for args in inputs:
241 results.append(func(*args))
242 return results
243
244 self.PatchObject(
245 packages.uprev_lib.parallel,
246 "RunTasksInProcessPool",
247 side_effect=fakeRunTasks,
248 )
249 mock_devutils = mock.MagicMock(name="dev-utils")
250 mock_ecutils = mock.MagicMock(name="ec-utils")
251 mock_ecutilstest = mock.MagicMock(name="ec-utils-test")
252 self.PatchObject(
253 packages.uprev_lib.portage_util,
254 "GetOverlayEBuilds",
255 return_value=[
256 mock_devutils,
257 mock_ecutils,
258 mock_ecutilstest,
259 ],
260 )
261 mock_overlay_mgr = mock.MagicMock(name="overlay-manager")
262 mock_overlay_mgr.modified_ebuilds = ["file1", "file2"]
263 self.PatchObject(
264 packages.uprev_lib,
265 "UprevOverlayManager",
266 return_value=mock_overlay_mgr,
267 )
Jeremy Bettis0186d252023-01-19 14:47:46 -0700268
269 for package in [
270 "chromeos-base/ec-devutils",
271 "chromeos-base/ec-utils",
272 "chromeos-base/ec-utils-test",
273 ]:
274 cpv = package_info.SplitCPV(package, strict=False)
275 assert cpv is not None
276 build_targets = [build_target_lib.BuildTarget("foo")]
277 refs = [
Alex Klein220e3a72023-09-14 17:12:13 -0600278 uprev_lib.GitRef(
Jeremy Bettis0186d252023-01-19 14:47:46 -0700279 path="/platform/ec",
280 ref="main",
281 revision="123",
282 )
283 ]
Alex Klein220e3a72023-09-14 17:12:13 -0600284 chroot = chroot_lib.Chroot()
Jeremy Bettis0186d252023-01-19 14:47:46 -0700285
286 result = packages.uprev_versioned_package(
287 cpv, build_targets, refs, chroot
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700288 )
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700289
Jeremy Bettis0186d252023-01-19 14:47:46 -0700290 self.assertEqual(1, len(result.modified))
291 self.assertEqual("123", result.modified[0].new_version)
292 self.assertListEqual(result.modified[0].files, ["file1", "file2"])
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700293
Jeremy Bettis0186d252023-01-19 14:47:46 -0700294 mock_overlay_mgr.uprev.assert_called_with(
295 package_list=[
296 package,
297 ],
298 force=True,
299 )
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700300
301
Alex Kleineb77ffa2019-05-28 14:47:44 -0600302class UprevBuildTargetsTest(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600303 """uprev_build_targets tests."""
Alex Kleineb77ffa2019-05-28 14:47:44 -0600304
Alex Klein1699fab2022-09-08 08:46:06 -0600305 def test_invalid_type_fails(self):
306 """Test invalid type fails."""
307 with self.assertRaises(AssertionError):
308 packages.uprev_build_targets(
309 [build_target_lib.BuildTarget("foo")], "invalid"
310 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600311
Alex Klein1699fab2022-09-08 08:46:06 -0600312 def test_none_type_fails(self):
313 """Test None type fails."""
314 with self.assertRaises(AssertionError):
315 packages.uprev_build_targets(
316 [build_target_lib.BuildTarget("foo")], None
317 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600318
319
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000320class PatchEbuildVarsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600321 """patch_ebuild_vars test."""
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000322
Alex Klein1699fab2022-09-08 08:46:06 -0600323 def setUp(self):
324 self.mock_input = self.PatchObject(packages.fileinput, "input")
325 self.mock_stdout_write = self.PatchObject(packages.sys.stdout, "write")
326 self.ebuild_path = "/path/to/ebuild"
327 self.old_var_value = "R100-5678.0.123456789"
328 self.new_var_value = "R102-5678.0.234566789"
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000329
Alex Klein1699fab2022-09-08 08:46:06 -0600330 def test_patch_ebuild_vars_var_only(self):
331 """patch_ebuild_vars changes ^var=value$."""
332 ebuild_contents = (
333 "This line does not change.\n"
334 'AFDO_PROFILE_VERSION="{var_value}"\n'
335 "\n"
336 "# The line with AFDO_PROFILE_VERSION is also unchanged."
337 )
338 # Ebuild contains old_var_value.
339 self.mock_input.return_value = io.StringIO(
340 ebuild_contents.format(var_value=self.old_var_value)
341 )
342 expected_calls = []
343 # Expect the line with new_var_value.
344 for line in io.StringIO(
345 ebuild_contents.format(var_value=self.new_var_value)
346 ):
347 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000348
Alex Klein1699fab2022-09-08 08:46:06 -0600349 packages.patch_ebuild_vars(
350 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
351 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000352
Alex Klein1699fab2022-09-08 08:46:06 -0600353 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000354
Alex Klein1699fab2022-09-08 08:46:06 -0600355 def test_patch_ebuild_vars_ignore_export(self):
356 """patch_ebuild_vars changes ^export var=value$ and keeps export."""
357 ebuild_contents = (
358 "This line does not change.\n"
359 'export AFDO_PROFILE_VERSION="{var_value}"\n'
360 "# This line is also unchanged."
361 )
362 # Ebuild contains old_var_value.
363 self.mock_input.return_value = io.StringIO(
364 ebuild_contents.format(var_value=self.old_var_value)
365 )
366 expected_calls = []
367 # Expect the line with new_var_value.
368 for line in io.StringIO(
369 ebuild_contents.format(var_value=self.new_var_value)
370 ):
371 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000372
Alex Klein1699fab2022-09-08 08:46:06 -0600373 packages.patch_ebuild_vars(
374 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
375 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000376
Alex Klein1699fab2022-09-08 08:46:06 -0600377 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000378
Alex Klein1699fab2022-09-08 08:46:06 -0600379 def test_patch_ebuild_vars_partial_match(self):
380 """patch_ebuild_vars ignores ^{prefix}var=value$."""
381 ebuild_contents = (
Alex Kleina53bd282022-09-09 12:42:55 -0600382 'This and the line below do not change.\nNEW_AFDO="{var_value}"'
Alex Klein1699fab2022-09-08 08:46:06 -0600383 )
384 # Ebuild contains old_var_value.
385 self.mock_input.return_value = io.StringIO(
386 ebuild_contents.format(var_value=self.old_var_value)
387 )
388 expected_calls = []
389 # Expect the line with UNCHANGED old_var_value.
390 for line in io.StringIO(
391 ebuild_contents.format(var_value=self.old_var_value)
392 ):
393 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000394
Alex Kleinfee86da2023-01-20 18:40:06 -0700395 # Note that the var name partially matches the ebuild var and hence it
396 # has to be ignored.
Alex Klein1699fab2022-09-08 08:46:06 -0600397 packages.patch_ebuild_vars(
398 self.ebuild_path, {"AFDO": self.new_var_value}
399 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000400
Alex Klein1699fab2022-09-08 08:46:06 -0600401 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000402
Alex Klein1699fab2022-09-08 08:46:06 -0600403 def test_patch_ebuild_vars_no_vars(self):
404 """patch_ebuild_vars keeps ebuild intact if there are no vars."""
405 ebuild_contents = (
406 "This line does not change.\n"
407 "The line with AFDO_PROFILE_VERSION is also unchanged."
408 )
409 self.mock_input.return_value = io.StringIO(ebuild_contents)
410 expected_calls = []
411 for line in io.StringIO(ebuild_contents):
412 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000413
Alex Klein1699fab2022-09-08 08:46:06 -0600414 packages.patch_ebuild_vars(
415 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
416 )
417
418 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000419
420
Alex Klein87531182019-08-12 15:23:37 -0600421class UprevsVersionedPackageTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600422 """uprevs_versioned_package decorator test."""
Alex Klein87531182019-08-12 15:23:37 -0600423
Alex Klein1699fab2022-09-08 08:46:06 -0600424 @packages.uprevs_versioned_package("category/package")
425 def uprev_category_package(self, *args, **kwargs):
426 """Registered function for testing."""
Alex Klein87531182019-08-12 15:23:37 -0600427
Alex Klein1699fab2022-09-08 08:46:06 -0600428 def test_calls_function(self):
429 """Test calling a registered function."""
430 self.PatchObject(self, "uprev_category_package")
Alex Klein87531182019-08-12 15:23:37 -0600431
Alex Klein1699fab2022-09-08 08:46:06 -0600432 cpv = package_info.SplitCPV("category/package", strict=False)
Alex Klein220e3a72023-09-14 17:12:13 -0600433 packages.uprev_versioned_package(cpv, [], [], chroot_lib.Chroot())
Alex Klein87531182019-08-12 15:23:37 -0600434
Alex Kleinfee86da2023-01-20 18:40:06 -0700435 # TODO(crbug/1065172): Invalid assertion that was previously mocked.
Alex Klein1699fab2022-09-08 08:46:06 -0600436 # patch.assert_called()
Alex Klein87531182019-08-12 15:23:37 -0600437
Alex Klein1699fab2022-09-08 08:46:06 -0600438 def test_unregistered_package(self):
439 """Test calling with an unregistered package."""
440 cpv = package_info.SplitCPV("does-not/exist", strict=False)
Alex Klein87531182019-08-12 15:23:37 -0600441
Alex Klein1699fab2022-09-08 08:46:06 -0600442 with self.assertRaises(packages.UnknownPackageError):
Alex Klein220e3a72023-09-14 17:12:13 -0600443 packages.uprev_versioned_package(cpv, [], [], chroot_lib.Chroot())
Alex Klein87531182019-08-12 15:23:37 -0600444
445
Trent Begin6daa8702020-01-29 14:58:12 -0700446class UprevEbuildFromPinTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600447 """Tests uprev_ebuild_from_pin function"""
Trent Begin315d9d92019-12-03 21:55:53 -0700448
Alex Klein1699fab2022-09-08 08:46:06 -0600449 package = "category/package"
450 version = "1.2.3"
451 new_version = "1.2.4"
452 ebuild_template = "package-%s-r1.ebuild"
453 ebuild = ebuild_template % version
454 unstable_ebuild = "package-9999.ebuild"
455 manifest = "Manifest"
Trent Begin315d9d92019-12-03 21:55:53 -0700456
Alex Klein1699fab2022-09-08 08:46:06 -0600457 def test_uprev_ebuild(self):
458 """Tests uprev of ebuild with version path"""
459 file_layout = (
460 D(self.package, [self.ebuild, self.unstable_ebuild, self.manifest]),
461 )
462 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700463
Andrew Lamb701696f2023-09-15 17:22:45 +0000464 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700465
Andrew Lamb701696f2023-09-15 17:22:45 +0000466 ebuild_path = os.path.join(package_path, self.ebuild)
Alex Klein1699fab2022-09-08 08:46:06 -0600467 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000468
Alex Klein1699fab2022-09-08 08:46:06 -0600469 result = uprev_lib.uprev_ebuild_from_pin(
Alex Klein220e3a72023-09-14 17:12:13 -0600470 package_path, self.new_version, chroot=chroot_lib.Chroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600471 )
472 self.assertEqual(
473 len(result.modified),
474 1,
475 "unexpected number of results: %s" % len(result.modified),
476 )
Trent Begin315d9d92019-12-03 21:55:53 -0700477
Alex Klein1699fab2022-09-08 08:46:06 -0600478 mod = result.modified[0]
479 self.assertEqual(
480 mod.new_version,
481 self.new_version + "-r1",
482 "unexpected version number: %s" % mod.new_version,
483 )
Trent Begin315d9d92019-12-03 21:55:53 -0700484
Andrew Lamb701696f2023-09-15 17:22:45 +0000485 old_ebuild_path = os.path.join(
486 package_path, self.ebuild_template % self.version
Alex Klein1699fab2022-09-08 08:46:06 -0600487 )
Andrew Lamb701696f2023-09-15 17:22:45 +0000488 new_ebuild_path = os.path.join(
489 package_path, self.ebuild_template % self.new_version
490 )
491 manifest_path = os.path.join(package_path, "Manifest")
Trent Begin2e5344f2020-03-02 10:46:55 -0700492
Alex Klein1699fab2022-09-08 08:46:06 -0600493 expected_modified_files = [
494 old_ebuild_path,
495 new_ebuild_path,
496 manifest_path,
497 ]
498 self.assertCountEqual(mod.files, expected_modified_files)
Trent Begin4a11a632020-02-28 12:59:58 -0700499
Alex Klein1699fab2022-09-08 08:46:06 -0600500 self.assertCommandContains(["ebuild", "manifest"])
Trent Begin6daa8702020-01-29 14:58:12 -0700501
Alex Klein1699fab2022-09-08 08:46:06 -0600502 def test_uprev_ebuild_same_version(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700503 """Tests uprev of ebuild with version path with unchanged version.
Fergus Dall2209d0b2020-08-06 11:51:43 +1000504
Alex Klein1699fab2022-09-08 08:46:06 -0600505 This should result in bumping the revision number.
506 """
507 file_layout = (
508 D(self.package, [self.ebuild, self.unstable_ebuild, self.manifest]),
509 )
510 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000511
Andrew Lamb701696f2023-09-15 17:22:45 +0000512 package_path = os.path.join(self.tempdir, self.package)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000513
Andrew Lamb701696f2023-09-15 17:22:45 +0000514 ebuild_path = os.path.join(package_path, self.ebuild)
Alex Klein1699fab2022-09-08 08:46:06 -0600515 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000516
Alex Klein1699fab2022-09-08 08:46:06 -0600517 result = uprev_lib.uprev_ebuild_from_pin(
Alex Klein220e3a72023-09-14 17:12:13 -0600518 package_path, self.version, chroot=chroot_lib.Chroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600519 )
520 self.assertEqual(
521 len(result.modified),
522 1,
523 "unexpected number of results: %s" % len(result.modified),
524 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000525
Alex Klein1699fab2022-09-08 08:46:06 -0600526 mod = result.modified[0]
527 self.assertEqual(
528 mod.new_version,
529 self.version + "-r2",
530 "unexpected version number: %s" % mod.new_version,
531 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000532
Andrew Lamb701696f2023-09-15 17:22:45 +0000533 old_ebuild_path = os.path.join(
534 package_path, self.ebuild_template % self.version
535 )
536 new_ebuild_path = os.path.join(
537 package_path, "package-%s-r2.ebuild" % self.version
538 )
539 manifest_path = os.path.join(package_path, "Manifest")
Fergus Dall2209d0b2020-08-06 11:51:43 +1000540
Alex Klein1699fab2022-09-08 08:46:06 -0600541 expected_modified_files = [
542 old_ebuild_path,
543 new_ebuild_path,
544 manifest_path,
545 ]
546 self.assertCountEqual(mod.files, expected_modified_files)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000547
Alex Klein1699fab2022-09-08 08:46:06 -0600548 self.assertCommandContains(["ebuild", "manifest"])
Fergus Dall2209d0b2020-08-06 11:51:43 +1000549
Alex Klein1699fab2022-09-08 08:46:06 -0600550 def test_no_ebuild(self):
551 """Tests assertion is raised if package has no ebuilds"""
552 file_layout = (D(self.package, [self.manifest]),)
553 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700554
Alex Klein1699fab2022-09-08 08:46:06 -0600555 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700556
Alex Klein1699fab2022-09-08 08:46:06 -0600557 with self.assertRaises(uprev_lib.EbuildUprevError):
558 uprev_lib.uprev_ebuild_from_pin(
Alex Klein220e3a72023-09-14 17:12:13 -0600559 package_path, self.new_version, chroot=chroot_lib.Chroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600560 )
Trent Begin315d9d92019-12-03 21:55:53 -0700561
Alex Klein1699fab2022-09-08 08:46:06 -0600562 def test_multiple_stable_ebuilds(self):
563 """Tests assertion is raised if multiple stable ebuilds are present"""
564 file_layout = (
565 D(
566 self.package,
567 [self.ebuild, self.ebuild_template % "1.2.1", self.manifest],
568 ),
569 )
570 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000571
Alex Klein1699fab2022-09-08 08:46:06 -0600572 package_path = os.path.join(self.tempdir, self.package)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000573
Alex Klein1699fab2022-09-08 08:46:06 -0600574 ebuild_path = os.path.join(package_path, self.ebuild)
575 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000576
Alex Klein1699fab2022-09-08 08:46:06 -0600577 ebuild_path = os.path.join(package_path, self.ebuild_template % "1.2.1")
578 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000579
Alex Klein1699fab2022-09-08 08:46:06 -0600580 with self.assertRaises(uprev_lib.EbuildUprevError):
581 uprev_lib.uprev_ebuild_from_pin(
Alex Klein220e3a72023-09-14 17:12:13 -0600582 package_path, self.new_version, chroot=chroot_lib.Chroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600583 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000584
Alex Klein1699fab2022-09-08 08:46:06 -0600585 def test_multiple_unstable_ebuilds(self):
586 """Tests assertion is raised if multiple unstable ebuilds are present"""
587 file_layout = (
588 D(
589 self.package,
590 [self.ebuild, self.ebuild_template % "1.2.1", self.manifest],
591 ),
592 )
593 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700594
Alex Klein1699fab2022-09-08 08:46:06 -0600595 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700596
Alex Klein1699fab2022-09-08 08:46:06 -0600597 with self.assertRaises(uprev_lib.EbuildUprevError):
598 uprev_lib.uprev_ebuild_from_pin(
Alex Klein220e3a72023-09-14 17:12:13 -0600599 package_path, self.new_version, chroot=chroot_lib.Chroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600600 )
Trent Begin315d9d92019-12-03 21:55:53 -0700601
602
Andrew Lamb9563a152019-12-04 11:42:18 -0700603class ReplicatePrivateConfigTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600604 """replicate_private_config tests."""
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700605
Alex Klein1699fab2022-09-08 08:46:06 -0600606 def setUp(self):
607 # Set up fake public and private chromeos-config overlays.
608 private_package_root = (
609 "src/private-overlays/overlay-coral-private/chromeos-base/"
610 "chromeos-config-bsp"
611 )
612 self.public_package_root = (
613 "src/overlays/overlay-coral/chromeos-base/chromeos-config-bsp"
614 )
615 file_layout = (
616 D(
617 os.path.join(private_package_root, "files"),
618 ["build_config.json"],
619 ),
620 D(private_package_root, ["replication_config.jsonpb"]),
621 D(
622 os.path.join(self.public_package_root, "files"),
623 ["build_config.json"],
624 ),
625 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700626
Alex Klein1699fab2022-09-08 08:46:06 -0600627 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700628
Alex Klein1699fab2022-09-08 08:46:06 -0600629 # Private config contains 'a' and 'b' fields.
630 self.private_config_path = os.path.join(
631 private_package_root, "files", "build_config.json"
632 )
633 self.WriteTempFile(
634 self.private_config_path,
635 json.dumps({"chromeos": {"configs": [{"a": 3, "b": 2}]}}),
636 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700637
Alex Kleinfee86da2023-01-20 18:40:06 -0700638 # Public config only contains the 'a' field. Note that the value of 'a'
639 # is 1 in the public config; it will get updated to 3 when the private
640 # config is replicated.
Alex Klein1699fab2022-09-08 08:46:06 -0600641 self.public_config_path = os.path.join(
642 self.public_package_root, "files", "build_config.json"
643 )
644 self.WriteTempFile(
645 self.public_config_path,
646 json.dumps({"chromeos": {"configs": [{"a": 1}]}}),
647 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700648
Alex Klein1699fab2022-09-08 08:46:06 -0600649 # Put a ReplicationConfig JSONPB in the private package. Note that it
650 # specifies only the 'a' field is replicated.
651 self.replication_config_path = os.path.join(
652 self.tempdir, private_package_root, "replication_config.jsonpb"
653 )
654 replication_config = ReplicationConfig(
655 file_replication_rules=[
656 FileReplicationRule(
657 source_path=self.private_config_path,
658 destination_path=self.public_config_path,
659 file_type=FILE_TYPE_JSON,
660 replication_type=REPLICATION_TYPE_FILTER,
Alex Klein220e3a72023-09-14 17:12:13 -0600661 destination_fields=field_mask_pb2.FieldMask(paths=["a"]),
Alex Klein1699fab2022-09-08 08:46:06 -0600662 )
663 ]
664 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700665
Alex Klein1699fab2022-09-08 08:46:06 -0600666 osutils.WriteFile(
667 self.replication_config_path,
668 json_format.MessageToJson(replication_config),
669 )
670 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700671
Alex Klein1699fab2022-09-08 08:46:06 -0600672 self.rc.SetDefaultCmdResult(side_effect=self._write_generated_c_files)
Andrew Lamb9563a152019-12-04 11:42:18 -0700673
Alex Klein1699fab2022-09-08 08:46:06 -0600674 def _write_generated_c_files(self, *_args, **_kwargs):
675 """Write fake generated C files to the public output dir.
Andrew Lamb9563a152019-12-04 11:42:18 -0700676
Alex Kleinfee86da2023-01-20 18:40:06 -0700677 Note that this function accepts args and kwargs so it can be used as a
678 side effect.
Alex Klein1699fab2022-09-08 08:46:06 -0600679 """
680 output_dir = os.path.join(self.public_package_root, "files")
681 self.WriteTempFile(os.path.join(output_dir, "config.c"), "")
682 self.WriteTempFile(os.path.join(output_dir, "ec_config.c"), "")
683 self.WriteTempFile(os.path.join(output_dir, "ec_config.h"), "")
Andrew Lamb9563a152019-12-04 11:42:18 -0700684
Alex Klein1699fab2022-09-08 08:46:06 -0600685 def _write_incorrect_generated_c_files(self, *_args, **_kwargs):
686 """Similar to _write_generated_c_files, with an expected file missing.
Andrew Lamb9563a152019-12-04 11:42:18 -0700687
Alex Kleinfee86da2023-01-20 18:40:06 -0700688 Note that this function accepts args and kwargs so it can be used as a
689 side effect.
Alex Klein1699fab2022-09-08 08:46:06 -0600690 """
691 output_dir = os.path.join(self.public_package_root, "files")
692 self.WriteTempFile(os.path.join(output_dir, "config.c"), "")
693 self.WriteTempFile(os.path.join(output_dir, "ec_config.c"), "")
Andrew Lamb9563a152019-12-04 11:42:18 -0700694
Alex Klein1699fab2022-09-08 08:46:06 -0600695 def test_replicate_private_config(self):
696 """Basic replication test."""
697 refs = [
Alex Klein220e3a72023-09-14 17:12:13 -0600698 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -0600699 path="/chromeos/overlays/overlay-coral-private",
700 ref="main",
701 revision="123",
702 )
703 ]
Alex Klein220e3a72023-09-14 17:12:13 -0600704 chroot = chroot_lib.Chroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600705 result = packages.replicate_private_config(
706 _build_targets=None, refs=refs, chroot=chroot
707 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700708
Alex Klein1699fab2022-09-08 08:46:06 -0600709 self.assertCommandContains(
710 [
711 "cros_config_schema",
712 "-m",
713 os.path.join(
714 constants.CHROOT_SOURCE_ROOT, self.public_config_path
715 ),
716 "-g",
717 os.path.join(
718 constants.CHROOT_SOURCE_ROOT,
719 self.public_package_root,
720 "files",
721 ),
722 "-f",
723 '"TRUE"',
724 ],
725 enter_chroot=True,
726 chroot_args=chroot.get_enter_args(),
727 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700728
Alex Klein1699fab2022-09-08 08:46:06 -0600729 self.assertEqual(len(result.modified), 1)
730 # The public build_config.json and generated C files were modified.
731 expected_modified_files = [
732 os.path.join(self.tempdir, self.public_config_path),
733 os.path.join(
734 self.tempdir, self.public_package_root, "files", "config.c"
735 ),
736 os.path.join(
737 self.tempdir, self.public_package_root, "files", "ec_config.c"
738 ),
739 os.path.join(
740 self.tempdir, self.public_package_root, "files", "ec_config.h"
741 ),
742 ]
743 self.assertEqual(result.modified[0].files, expected_modified_files)
744 self.assertEqual(result.modified[0].new_version, "123")
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700745
Alex Kleinfee86da2023-01-20 18:40:06 -0700746 # The update from the private build_config.json was copied to the
747 # public. Note that only the 'a' field is present, as per
748 # destination_fields.
Alex Klein1699fab2022-09-08 08:46:06 -0600749 self.assertEqual(
750 json.loads(self.ReadTempFile(self.public_config_path)),
751 {"chromeos": {"configs": [{"a": 3}]}},
752 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700753
Alex Klein1699fab2022-09-08 08:46:06 -0600754 def test_replicate_private_config_no_build_config(self):
755 """If there is no build config, don't generate C files."""
Alex Kleinfee86da2023-01-20 18:40:06 -0700756 # Modify the replication config to write to "other_config.json" instead
757 # of "build_config.json"
Alex Klein1699fab2022-09-08 08:46:06 -0600758 modified_destination_path = self.public_config_path.replace(
759 "build_config", "other_config"
760 )
761 replication_config = ReplicationConfig(
762 file_replication_rules=[
763 FileReplicationRule(
764 source_path=self.private_config_path,
765 destination_path=modified_destination_path,
766 file_type=FILE_TYPE_JSON,
767 replication_type=REPLICATION_TYPE_FILTER,
Alex Klein220e3a72023-09-14 17:12:13 -0600768 destination_fields=field_mask_pb2.FieldMask(paths=["a"]),
Alex Klein1699fab2022-09-08 08:46:06 -0600769 )
770 ]
771 )
772 osutils.WriteFile(
773 self.replication_config_path,
774 json_format.MessageToJson(replication_config),
775 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700776
Alex Klein1699fab2022-09-08 08:46:06 -0600777 refs = [
Alex Klein220e3a72023-09-14 17:12:13 -0600778 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -0600779 path="/chromeos/overlays/overlay-coral-private",
780 ref="main",
781 revision="123",
782 )
783 ]
784 result = packages.replicate_private_config(
Alex Klein220e3a72023-09-14 17:12:13 -0600785 _build_targets=None, refs=refs, chroot=chroot_lib.Chroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600786 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700787
Alex Klein1699fab2022-09-08 08:46:06 -0600788 self.assertEqual(len(result.modified), 1)
789 self.assertEqual(
790 result.modified[0].files,
791 [os.path.join(self.tempdir, modified_destination_path)],
792 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700793
Alex Klein1699fab2022-09-08 08:46:06 -0600794 def test_replicate_private_config_multiple_build_configs(self):
795 """An error is thrown if there is more than one build config."""
796 replication_config = ReplicationConfig(
797 file_replication_rules=[
798 FileReplicationRule(
799 source_path=self.private_config_path,
800 destination_path=self.public_config_path,
801 file_type=FILE_TYPE_JSON,
802 replication_type=REPLICATION_TYPE_FILTER,
Alex Klein220e3a72023-09-14 17:12:13 -0600803 destination_fields=field_mask_pb2.FieldMask(paths=["a"]),
Alex Klein1699fab2022-09-08 08:46:06 -0600804 ),
805 FileReplicationRule(
806 source_path=self.private_config_path,
807 destination_path=self.public_config_path,
808 file_type=FILE_TYPE_JSON,
809 replication_type=REPLICATION_TYPE_FILTER,
Alex Klein220e3a72023-09-14 17:12:13 -0600810 destination_fields=field_mask_pb2.FieldMask(paths=["a"]),
Alex Klein1699fab2022-09-08 08:46:06 -0600811 ),
812 ]
813 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700814
Alex Klein1699fab2022-09-08 08:46:06 -0600815 osutils.WriteFile(
816 self.replication_config_path,
817 json_format.MessageToJson(replication_config),
818 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700819
Alex Klein1699fab2022-09-08 08:46:06 -0600820 refs = [
Alex Klein220e3a72023-09-14 17:12:13 -0600821 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -0600822 path="/chromeos/overlays/overlay-coral-private",
823 ref="main",
824 revision="123",
825 )
826 ]
827 with self.assertRaisesRegex(
828 ValueError,
829 "Expected at most one build_config.json destination path.",
830 ):
831 packages.replicate_private_config(
Alex Klein220e3a72023-09-14 17:12:13 -0600832 _build_targets=None, refs=refs, chroot=chroot_lib.Chroot()
Alex Klein1699fab2022-09-08 08:46:06 -0600833 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700834
Alex Klein1699fab2022-09-08 08:46:06 -0600835 def test_replicate_private_config_generated_files_incorrect(self):
836 """An error is thrown if generated C files are missing."""
837 self.rc.SetDefaultCmdResult(
838 side_effect=self._write_incorrect_generated_c_files
839 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700840
Alex Klein1699fab2022-09-08 08:46:06 -0600841 refs = [
Alex Klein220e3a72023-09-14 17:12:13 -0600842 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -0600843 path="/chromeos/overlays/overlay-coral-private",
844 ref="main",
845 revision="123",
846 )
847 ]
Alex Klein220e3a72023-09-14 17:12:13 -0600848 chroot = chroot_lib.Chroot()
Andrew Lamb9563a152019-12-04 11:42:18 -0700849
Alex Klein1699fab2022-09-08 08:46:06 -0600850 with self.assertRaisesRegex(
851 packages.GeneratedCrosConfigFilesError,
852 "Expected to find generated C files",
853 ):
854 packages.replicate_private_config(
855 _build_targets=None, refs=refs, chroot=chroot
856 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700857
Alex Klein1699fab2022-09-08 08:46:06 -0600858 def test_replicate_private_config_wrong_number_of_refs(self):
859 """An error is thrown if there is not exactly one ref."""
860 with self.assertRaisesRegex(ValueError, "Expected exactly one ref"):
861 packages.replicate_private_config(
862 _build_targets=None, refs=[], chroot=None
863 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700864
Alex Klein1699fab2022-09-08 08:46:06 -0600865 with self.assertRaisesRegex(ValueError, "Expected exactly one ref"):
866 refs = [
Alex Klein220e3a72023-09-14 17:12:13 -0600867 uprev_lib.GitRef(path="a", ref="main", revision="1"),
868 uprev_lib.GitRef(path="a", ref="main", revision="2"),
Alex Klein1699fab2022-09-08 08:46:06 -0600869 ]
870 packages.replicate_private_config(
871 _build_targets=None, refs=refs, chroot=None
872 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700873
Alex Klein1699fab2022-09-08 08:46:06 -0600874 def test_replicate_private_config_replication_config_missing(self):
875 """An error is thrown if there is not a replication config."""
876 os.remove(self.replication_config_path)
877 with self.assertRaisesRegex(
878 ValueError,
879 "Expected ReplicationConfig missing at %s"
880 % self.replication_config_path,
881 ):
882 refs = [
Alex Klein220e3a72023-09-14 17:12:13 -0600883 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -0600884 path="/chromeos/overlays/overlay-coral-private",
885 ref="main",
886 revision="123",
887 )
888 ]
889 packages.replicate_private_config(
890 _build_targets=None, refs=refs, chroot=None
891 )
Andrew Lambe836f222019-12-09 12:27:38 -0700892
Alex Klein1699fab2022-09-08 08:46:06 -0600893 def test_replicate_private_config_wrong_git_ref_path(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700894 """Git ref that doesn't point to a private overlay throws error."""
Alex Klein1699fab2022-09-08 08:46:06 -0600895 with self.assertRaisesRegex(
896 ValueError, "ref.path must match the pattern"
897 ):
Alex Klein220e3a72023-09-14 17:12:13 -0600898 refs = [uprev_lib.GitRef(path="a/b/c", ref="main", revision="123")]
Alex Klein1699fab2022-09-08 08:46:06 -0600899 packages.replicate_private_config(
900 _build_targets=None, refs=refs, chroot=None
901 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700902
903
Alex Klein5caab872021-09-10 11:44:37 -0600904class GetBestVisibleTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600905 """get_best_visible tests."""
David Burger1e0fe232019-07-01 14:52:07 -0600906
Alex Klein1699fab2022-09-08 08:46:06 -0600907 def test_empty_atom_fails(self):
908 """Test empty atom raises an error."""
909 with self.assertRaises(AssertionError):
910 packages.get_best_visible("")
Alex Kleinda39c6d2019-09-16 14:36:36 -0600911
912
Alex Klein149fd3b2019-12-16 16:01:05 -0700913class HasPrebuiltTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600914 """has_prebuilt tests."""
Alex Kleinda39c6d2019-09-16 14:36:36 -0600915
Alex Klein1699fab2022-09-08 08:46:06 -0600916 def test_empty_atom_fails(self):
917 """Test an empty atom results in an error."""
918 with self.assertRaises(AssertionError):
919 packages.has_prebuilt("")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600920
Alex Klein1699fab2022-09-08 08:46:06 -0600921 def test_use_flags(self):
922 """Test use flags get propagated correctly."""
923 # We don't really care about the result, just the env handling.
924 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
925 # Ignore any flags that may be in the environment.
926 self.PatchObject(os.environ, "get", return_value="")
Alex Klein149fd3b2019-12-16 16:01:05 -0700927
Alex Klein1699fab2022-09-08 08:46:06 -0600928 packages.has_prebuilt("cat/pkg-1.2.3", useflags="useflag")
929 patch.assert_called_with(
930 "cat/pkg-1.2.3", board=None, extra_env={"USE": "useflag"}
931 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700932
Alex Klein1699fab2022-09-08 08:46:06 -0600933 def test_env_use_flags(self):
934 """Test env use flags get propagated correctly with passed useflags."""
935 # We don't really care about the result, just the env handling.
936 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
937 # Add some flags to the environment.
938 existing_flags = "already set flags"
939 self.PatchObject(os.environ, "get", return_value=existing_flags)
Alex Klein149fd3b2019-12-16 16:01:05 -0700940
Alex Klein1699fab2022-09-08 08:46:06 -0600941 new_flags = "useflag"
942 packages.has_prebuilt("cat/pkg-1.2.3", useflags=new_flags)
943 expected = "%s %s" % (existing_flags, new_flags)
944 patch.assert_called_with(
945 "cat/pkg-1.2.3", board=None, extra_env={"USE": expected}
946 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700947
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600948
949class AndroidVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600950 """Tests getting android versions."""
Michael Mortensen14960d02019-10-18 07:53:59 -0600951
Alex Klein1699fab2022-09-08 08:46:06 -0600952 def setUp(self):
953 package_result = [
954 "chromeos-base/android-container-nyc-4717008-r1",
955 "chromeos-base/update_engine-0.0.3-r3408",
956 ]
957 self.PatchObject(
958 portage_util, "GetPackageDependencies", return_value=package_result
959 )
960 self.board = "board"
961 self.PatchObject(
962 portage_util,
963 "FindEbuildForBoardPackage",
964 return_value="chromeos-base/android-container-nyc",
965 )
966 FakeEnvironment = {
967 "ARM_TARGET": "3-linux-target",
968 }
969 self.PatchObject(
970 osutils, "SourceEnvironment", return_value=FakeEnvironment
971 )
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600972
Alex Kleinfee86da2023-01-20 18:40:06 -0700973 # Clear the LRU cache for the function. We mock the function that
974 # provides the data this function processes to produce its result, so we
975 # need to clear it manually.
Alex Klein1699fab2022-09-08 08:46:06 -0600976 packages.determine_android_package.cache_clear()
Alex Klein68a28712021-11-08 11:08:30 -0700977
Alex Klein1699fab2022-09-08 08:46:06 -0600978 def test_determine_android_version(self):
979 """Tests that a valid android version is returned."""
980 version = packages.determine_android_version(self.board)
981 self.assertEqual(version, "4717008")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600982
Alex Klein1699fab2022-09-08 08:46:06 -0600983 def test_determine_android_version_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700984 """Test None is returned for version when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -0600985 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
986 self.PatchObject(
987 portage_util, "GetPackageDependencies", return_value=package_result
988 )
989 version = packages.determine_android_version(self.board)
990 self.assertEqual(version, None)
Michael Mortensenedf76532019-10-16 14:22:37 -0600991
Alex Klein1699fab2022-09-08 08:46:06 -0600992 def test_determine_android_branch(self):
993 """Tests that a valid android branch is returned."""
994 branch = packages.determine_android_branch(self.board)
995 self.assertEqual(branch, "3")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600996
Alex Klein1699fab2022-09-08 08:46:06 -0600997 def test_determine_android_branch_64bit_targets(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700998 """Tests a valid android branch is returned with only 64bit targets."""
Alex Klein1699fab2022-09-08 08:46:06 -0600999 self.PatchObject(
1000 osutils,
1001 "SourceEnvironment",
1002 return_value={"ARM64_TARGET": "3-linux-target"},
1003 )
1004 branch = packages.determine_android_branch(self.board)
1005 self.assertEqual(branch, "3")
Federico 'Morg' Pareschicd9165a2020-05-29 09:45:55 +09001006
Alex Klein1699fab2022-09-08 08:46:06 -06001007 def test_determine_android_branch_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001008 """Tests a None is returned for branch when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -06001009 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
1010 self.PatchObject(
1011 portage_util, "GetPackageDependencies", return_value=package_result
1012 )
1013 branch = packages.determine_android_branch(self.board)
1014 self.assertEqual(branch, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001015
Alex Klein1699fab2022-09-08 08:46:06 -06001016 def test_determine_android_target(self):
1017 """Tests that a valid android target is returned."""
1018 target = packages.determine_android_target(self.board)
1019 self.assertEqual(target, "cheets")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001020
Alex Klein1699fab2022-09-08 08:46:06 -06001021 def test_determine_android_target_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001022 """Tests a None is returned for target when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -06001023 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
1024 self.PatchObject(
1025 portage_util, "GetPackageDependencies", return_value=package_result
1026 )
1027 target = packages.determine_android_target(self.board)
1028 self.assertEqual(target, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001029
Alex Klein1699fab2022-09-08 08:46:06 -06001030 def test_determine_android_version_handle_exception(self):
1031 """Tests handling RunCommandError inside determine_android_version."""
Alex Kleinfee86da2023-01-20 18:40:06 -07001032 # Mock what happens when portage returns that bubbles up (via
1033 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001034 self.PatchObject(
1035 portage_util,
1036 "GetPackageDependencies",
1037 side_effect=cros_build_lib.RunCommandError("error"),
1038 )
1039 target = packages.determine_android_version(self.board)
1040 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -06001041
Alex Klein1699fab2022-09-08 08:46:06 -06001042 def test_determine_android_package_handle_exception(self):
1043 """Tests handling RunCommandError inside determine_android_package."""
Alex Kleinfee86da2023-01-20 18:40:06 -07001044 # Mock what happens when portage returns that bubbles up (via
1045 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001046 self.PatchObject(
1047 portage_util,
1048 "GetPackageDependencies",
1049 side_effect=cros_build_lib.RunCommandError("error"),
1050 )
1051 target = packages.determine_android_package(self.board)
1052 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -06001053
Alex Klein1699fab2022-09-08 08:46:06 -06001054 def test_determine_android_package_callers_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001055 """Tests RunCommandError caught by determine_android_package callers."""
1056 # Mock what happens when portage returns that bubbles up (via
1057 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001058 self.PatchObject(
1059 portage_util,
1060 "GetPackageDependencies",
1061 side_effect=cros_build_lib.RunCommandError("error"),
1062 )
1063 # Verify that target is None, as expected.
1064 target = packages.determine_android_package(self.board)
1065 self.assertEqual(target, None)
1066 # determine_android_branch calls determine_android_package
1067 branch = packages.determine_android_branch(self.board)
1068 self.assertEqual(branch, None)
1069 # determine_android_target calls determine_android_package
1070 target = packages.determine_android_target(self.board)
1071 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001072
Michael Mortensene0f4b542019-10-24 15:30:23 -06001073
Alex Klein1699fab2022-09-08 08:46:06 -06001074@pytest.mark.usefixtures("testcase_caplog", "testcase_monkeypatch")
Michael Mortensende716a12020-05-15 11:27:00 -06001075class FindFingerprintsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001076 """Tests for find_fingerprints."""
Michael Mortensende716a12020-05-15 11:27:00 -06001077
Alex Klein1699fab2022-09-08 08:46:06 -06001078 def setUp(self):
1079 self.board = "test-board"
1080 # Create cheets-fingerprints.txt based on tempdir/src...
1081 self.fingerprint_contents = (
1082 "google/test-board/test-board_cheets"
1083 ":9/R99-12345.0.9999/123456:user/release-keys"
1084 )
1085 fingerprint_path = os.path.join(
1086 self.tempdir,
1087 "src/build/images/test-board/latest/cheets-fingerprint.txt",
1088 )
Alex Klein220e3a72023-09-14 17:12:13 -06001089 self.chroot = chroot_lib.Chroot(
Brian Norris4f251e42023-03-09 15:53:26 -08001090 path=self.tempdir / "chroot", out_path=self.tempdir / "out"
1091 )
Alex Klein1699fab2022-09-08 08:46:06 -06001092 osutils.WriteFile(
1093 fingerprint_path, self.fingerprint_contents, makedirs=True
1094 )
Michael Mortensende716a12020-05-15 11:27:00 -06001095
Alex Klein1699fab2022-09-08 08:46:06 -06001096 def test_find_fingerprints_with_test_path(self):
1097 """Tests get_firmware_versions with mocked output."""
1098 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1099 build_target = build_target_lib.BuildTarget(self.board)
1100 result = packages.find_fingerprints(build_target)
1101 self.assertEqual(result, [self.fingerprint_contents])
1102 self.assertIn("Reading fingerprint file", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001103
Alex Klein1699fab2022-09-08 08:46:06 -06001104 def test_find_fingerprints(self):
1105 """Tests get_firmware_versions with mocked output."""
1106 # Use board name whose path for fingerprint file does not exist.
1107 # Verify that fingerprint file is not found and None is returned.
1108 build_target = build_target_lib.BuildTarget("wrong-boardname")
1109 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1110 result = packages.find_fingerprints(build_target)
1111 self.assertEqual(result, [])
1112 self.assertIn("Fingerprint file not found", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001113
1114
Michael Mortensen59e30872020-05-18 14:12:49 -06001115class GetAllFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001116 """Tests for get_firmware_versions."""
Michael Mortensen59e30872020-05-18 14:12:49 -06001117
Alex Klein1699fab2022-09-08 08:46:06 -06001118 def setUp(self):
1119 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001120 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001121 self.rc.SetDefaultCmdResult(
1122 stdout="""
Michael Mortensen59e30872020-05-18 14:12:49 -06001123
1124flashrom(8): 68935ee2fcfcffa47af81b966269cd2b */build/reef/usr/sbin/flashrom
1125 ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=e102cc98d45300b50088999d53775acbeff407dc, stripped
1126 0.9.9 : bbb2d6a : Jul 28 2017 15:12:34 UTC
1127
1128Model: reef
1129BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1130BIOS version: Google_Reef.9042.87.1
1131BIOS (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
1132BIOS (RW) version: Google_Reef.9042.110.0
1133EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1134EC version: reef_v1.1.5900-ab1ee51
1135EC (RW) version: reef_v1.1.5909-bd1f0c9
1136
1137Model: pyro
1138BIOS image: 9e62447ebf22a724a4a835018ab6234e */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/image.bin
1139BIOS version: Google_Pyro.9042.87.1
1140BIOS (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
1141BIOS (RW) version: Google_Pyro.9042.110.0
1142EC image: 44b93ed591733519e752e05aa0529eb5 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/ec.bin
1143EC version: pyro_v1.1.5900-ab1ee51
1144EC (RW) version: pyro_v1.1.5909-bd1f0c9
1145
1146Model: snappy
1147BIOS image: 3ab63ff080596bd7de4e7619f003bb64 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/image.bin
1148BIOS version: Google_Snappy.9042.110.0
1149EC image: c4db159e84428391d2ee25368c5fe5b6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/ec.bin
1150EC version: snappy_v1.1.5909-bd1f0c9
1151
1152Model: sand
1153BIOS image: 387da034a4f0a3f53e278ebfdcc2a412 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/image.bin
1154BIOS version: Google_Sand.9042.110.0
1155EC image: 411562e0589dacec131f5fdfbe95a561 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/ec.bin
1156EC version: sand_v1.1.5909-bd1f0c9
1157
1158Model: electro
1159BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1160BIOS version: Google_Reef.9042.87.1
1161BIOS (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
1162BIOS (RW) version: Google_Reef.9042.110.0
1163EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1164EC version: reef_v1.1.5900-ab1ee51
1165EC (RW) version: reef_v1.1.5909-bd1f0c9
1166
1167Package Content:
1168612e7bb6ed1fb0a05abf2ebdc834c18b *./updater4.sh
11690eafbee07282315829d0f42135ec7c0c *./gbb_utility
11706074e3ca424cb30a67c378c1d9681f9c *./mosys
117168935ee2fcfcffa47af81b966269cd2b *./flashrom
11720eafbee07282315829d0f42135ec7c0c *./dump_fmap
1173490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
117460899148600b8673ddb711faa55aee40 *./common.sh
11753c3a99346d1ca1273cbcd86c104851ff *./shflags
1176de7ce035e1f82a89f8909d888ee402c0 *./crosutil.sh
1177f9334372bdb9036ba09a6fd9bf30e7a2 *./crossystem
117822257a8d5f0adc1f50a1916c3a4a35dd *./models/reef/ec.bin
1179faf12dbb7cdaf21ce153bdffb67841fd *./models/reef/bios.bin
1180c9bbb417b7921b85a7ed999ee42f550e *./models/reef/setvars.sh
118129823d46f1ec1491ecacd7b830fd2686 *./models/pyro/ec.bin
11822320463aba8b22eb5ea836f094d281b3 *./models/pyro/bios.bin
118381614833ad77c9cd093360ba7bea76b8 *./models/pyro/setvars.sh
1184411562e0589dacec131f5fdfbe95a561 *./models/sand/ec.bin
1185387da034a4f0a3f53e278ebfdcc2a412 *./models/sand/bios.bin
1186fcd8cb0ac0e2ed6be220aaae435d43ff *./models/sand/setvars.sh
1187c4db159e84428391d2ee25368c5fe5b6 *./models/snappy/ec.bin
11883ab63ff080596bd7de4e7619f003bb64 *./models/snappy/bios.bin
1189fe5d699f2e9e4a7de031497953313dbd *./models/snappy/setvars.sh
119079aabd7cd8a215a54234c53d7bb2e6fb *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001191"""
1192 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001193 # pylint: enable=line-too-long
Michael Mortensen59e30872020-05-18 14:12:49 -06001194
Alex Klein1699fab2022-09-08 08:46:06 -06001195 def test_get_firmware_versions(self):
1196 """Tests get_firmware_versions with mocked output."""
1197 build_target = build_target_lib.BuildTarget(self.board)
1198 result = packages.get_all_firmware_versions(build_target)
1199 self.assertEqual(len(result), 5)
1200 self.assertEqual(
1201 result["reef"],
1202 packages.FirmwareVersions(
1203 "reef",
1204 "Google_Reef.9042.87.1",
1205 "Google_Reef.9042.110.0",
1206 "reef_v1.1.5900-ab1ee51",
1207 "reef_v1.1.5909-bd1f0c9",
1208 ),
1209 )
1210 self.assertEqual(
1211 result["pyro"],
1212 packages.FirmwareVersions(
1213 "pyro",
1214 "Google_Pyro.9042.87.1",
1215 "Google_Pyro.9042.110.0",
1216 "pyro_v1.1.5900-ab1ee51",
1217 "pyro_v1.1.5909-bd1f0c9",
1218 ),
1219 )
1220 self.assertEqual(
1221 result["snappy"],
1222 packages.FirmwareVersions(
1223 "snappy",
1224 "Google_Snappy.9042.110.0",
1225 None,
1226 "snappy_v1.1.5909-bd1f0c9",
1227 None,
1228 ),
1229 )
1230 self.assertEqual(
1231 result["sand"],
1232 packages.FirmwareVersions(
1233 "sand",
1234 "Google_Sand.9042.110.0",
1235 None,
1236 "sand_v1.1.5909-bd1f0c9",
1237 None,
1238 ),
1239 )
1240 self.assertEqual(
1241 result["electro"],
1242 packages.FirmwareVersions(
1243 "electro",
1244 "Google_Reef.9042.87.1",
1245 "Google_Reef.9042.110.0",
1246 "reef_v1.1.5900-ab1ee51",
1247 "reef_v1.1.5909-bd1f0c9",
1248 ),
1249 )
Michael Mortensen59e30872020-05-18 14:12:49 -06001250
Alex Klein1699fab2022-09-08 08:46:06 -06001251 def test_get_firmware_versions_error(self):
1252 """Tests get_firmware_versions with no output."""
1253 # Throw an exception when running the command.
Mike Frysinger5d85a542023-07-06 16:05:54 -04001254 self.rc.SetDefaultCmdResult(returncode=1)
Alex Klein1699fab2022-09-08 08:46:06 -06001255 build_target = build_target_lib.BuildTarget(self.board)
1256 result = packages.get_all_firmware_versions(build_target)
1257 self.assertEqual(result, {})
Benjamin Shai12c767e2022-01-12 15:17:44 +00001258
Michael Mortensen59e30872020-05-18 14:12:49 -06001259
Michael Mortensen71ef5682020-05-07 14:29:24 -06001260class GetFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001261 """Tests for get_firmware_versions."""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001262
Alex Klein1699fab2022-09-08 08:46:06 -06001263 def setUp(self):
1264 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001265 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001266 self.rc.SetDefaultCmdResult(
1267 stdout="""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001268
1269flashrom(8): a8f99c2e61e7dc09c4b25ef5a76ef692 */build/kevin/usr/sbin/flashrom
1270 ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.d
1271 0.9.4 : 860875a : Apr 10 2017 23:54:29 UTC
1272
1273BIOS image: 6b5b855a0b8fd1657546d1402c15b206 *chromeos-firmware-kevin-0.0.1/.dist/kevin_fw_8785.178.0.n
1274BIOS version: Google_Kevin.8785.178.0
1275EC image: 1ebfa9518e6cac0558a80b7ab2f5b489 *chromeos-firmware-kevin-0.0.1/.dist/kevin_ec_8785.178.0.n
1276EC version:kevin_v1.10.184-459421c
1277
1278Package Content:
1279a8f99c2e61e7dc09c4b25ef5a76ef692 *./flashrom
12803c3a99346d1ca1273cbcd86c104851ff *./shflags
1281457a8dc8546764affc9700f8da328d23 *./dump_fmap
1282c392980ddb542639edf44a965a59361a *./updater5.sh
1283490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
12846b5b855a0b8fd1657546d1402c15b206 *./bios.bin
12857b5bef0d2da90c23ff2e157250edf0fa *./crosutil.sh
1286d78722e4f1a0dc2d8c3d6b0bc7010ae3 *./crossystem
1287457a8dc8546764affc9700f8da328d23 *./gbb_utility
12881ebfa9518e6cac0558a80b7ab2f5b489 *./ec.bin
1289c98ca54db130886142ad582a58e90ddc *./common.sh
12905ba978bdec0f696f47f0f0de90936880 *./mosys
1291312e8ee6122057f2a246d7bcf1572f49 *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001292"""
1293 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001294 # pylint: enable=line-too-long
Michael Mortensen71ef5682020-05-07 14:29:24 -06001295
Alex Klein1699fab2022-09-08 08:46:06 -06001296 def test_get_firmware_versions(self):
1297 """Tests get_firmware_versions with mocked output."""
1298 build_target = build_target_lib.BuildTarget(self.board)
1299 result = packages.get_firmware_versions(build_target)
1300 versions = packages.FirmwareVersions(
1301 None,
1302 "Google_Kevin.8785.178.0",
1303 None,
1304 "kevin_v1.10.184-459421c",
1305 None,
1306 )
1307 self.assertEqual(result, versions)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001308
1309
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001310class DetermineKernelVersionTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001311 """Tests for determine_kernel_version."""
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001312
Alex Klein1699fab2022-09-08 08:46:06 -06001313 def setUp(self):
1314 self.board = "test-board"
1315 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001316
Alex Klein1699fab2022-09-08 08:46:06 -06001317 def test_determine_kernel_version(self):
1318 """Tests that a valid kernel version is returned."""
Lizzy Presland0b978e62022-09-09 16:55:29 +00001319 kernel_candidates = [
1320 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
Alex Klein1699fab2022-09-08 08:46:06 -06001321 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001322 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1323 "sys-kernel/upstream-kernel-next-9999",
1324 "sys-kernel/socfpga-kernel-4.20-r34",
Alex Klein1699fab2022-09-08 08:46:06 -06001325 ]
1326 self.PatchObject(
Lizzy Presland0b978e62022-09-09 16:55:29 +00001327 portage_util,
1328 "GetFlattenedDepsForPackage",
1329 return_value=kernel_candidates,
1330 )
1331
1332 installed_pkgs = [
1333 "sys-kernel/linux-firmware-0.0.1-r594",
1334 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1335 "virtual/linux-sources-1-r30",
1336 ]
1337 self.PatchObject(
1338 portage_util,
1339 "GetPackageDependencies",
1340 return_value=installed_pkgs,
Alex Klein1699fab2022-09-08 08:46:06 -06001341 )
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001342
Alex Klein1699fab2022-09-08 08:46:06 -06001343 result = packages.determine_kernel_version(self.build_target)
1344 self.assertEqual(result, "4.4.223-r2209")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001345
Lizzy Presland0b978e62022-09-09 16:55:29 +00001346 def test_determine_kernel_version_ignores_exact_duplicates(self):
1347 """Tests that multiple results for candidates is ignored."""
1348 # Depgraph is evaluated for version as well as revision, so graph will
1349 # return all results twice.
1350 kernel_candidates = [
1351 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1352 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1353 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1354 "sys-kernel/upstream-kernel-next-9999",
1355 "sys-kernel/socfpga-kernel-4.20-r34",
1356 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1357 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1358 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1359 "sys-kernel/upstream-kernel-next-9999",
1360 "sys-kernel/socfpga-kernel-4.20-r34",
1361 ]
1362 self.PatchObject(
1363 portage_util,
1364 "GetFlattenedDepsForPackage",
1365 return_value=kernel_candidates,
1366 )
1367
1368 installed_pkgs = [
1369 "sys-kernel/linux-firmware-0.0.1-r594",
1370 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1371 "virtual/linux-sources-1-r30",
1372 ]
Alex Klein1699fab2022-09-08 08:46:06 -06001373 self.PatchObject(
1374 portage_util,
1375 "GetPackageDependencies",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001376 return_value=installed_pkgs,
1377 )
1378
1379 result = packages.determine_kernel_version(self.build_target)
1380 self.assertEqual(result, "4.4.223-r2209")
1381
1382 def test_determine_kernel_version_ignores_virtual_package(self):
1383 """Tests that top-level package is ignored as potential kernel pkg."""
1384 # Depgraph results include the named package at level 0 as well as its
1385 # first-order dependencies, so verify that the virtual package is not
1386 # included as a kernel package.
1387 kernel_candidates = [
1388 "virtual/linux-sources-1",
1389 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1390 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1391 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1392 "sys-kernel/upstream-kernel-next-9999",
1393 "sys-kernel/socfpga-kernel-4.20-r34",
1394 "virtual/linux-sources-1-r30",
1395 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1396 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1397 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1398 "sys-kernel/upstream-kernel-next-9999",
1399 "sys-kernel/socfpga-kernel-4.20-r34",
1400 ]
1401 self.PatchObject(
1402 portage_util,
1403 "GetFlattenedDepsForPackage",
1404 return_value=kernel_candidates,
1405 )
1406
1407 installed_pkgs = [
1408 "sys-kernel/linux-firmware-0.0.1-r594",
1409 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1410 "virtual/linux-sources-1-r30",
1411 ]
1412 self.PatchObject(
1413 portage_util,
1414 "GetPackageDependencies",
1415 return_value=installed_pkgs,
1416 )
1417
1418 result = packages.determine_kernel_version(self.build_target)
1419 self.assertEqual(result, "4.4.223-r2209")
1420
1421 def test_determine_kernel_version_too_many(self):
1422 """Tests that an exception is thrown with too many matching packages."""
1423 package_result = [
1424 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1425 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1426 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1427 "sys-kernel/upstream-kernel-next-9999",
1428 "sys-kernel/socfpga-kernel-4.20-r34",
1429 ]
1430 self.PatchObject(
1431 portage_util,
1432 "GetFlattenedDepsForPackage",
1433 return_value=package_result,
1434 )
1435
1436 installed_pkgs = [
1437 "sys-kernel/linux-firmware-0.0.1-r594",
1438 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1439 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1440 "virtual/linux-sources-1-r30",
1441 ]
1442 self.PatchObject(
1443 portage_util,
1444 "GetPackageDependencies",
1445 return_value=installed_pkgs,
1446 )
1447
1448 with self.assertRaises(packages.KernelVersionError):
1449 packages.determine_kernel_version(self.build_target)
1450
1451 def test_determine_kernel_version_no_kernel_match(self):
1452 """Tests that an exception is thrown with 0-sized intersection."""
1453 package_result = [
1454 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1455 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1456 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1457 "sys-kernel/upstream-kernel-next-9999",
1458 ]
1459 self.PatchObject(
1460 portage_util,
1461 "GetFlattenedDepsForPackage",
1462 return_value=package_result,
1463 )
1464
1465 installed_pkgs = [
1466 "sys-kernel/linux-firmware-0.0.1-r594",
1467 "sys-kernel/socfpga-kernel-4.20-r34",
1468 "virtual/linux-sources-1-r30",
1469 ]
1470 self.PatchObject(
1471 portage_util,
1472 "GetPackageDependencies",
1473 return_value=installed_pkgs,
1474 )
1475
1476 with self.assertRaises(packages.KernelVersionError):
1477 packages.determine_kernel_version(self.build_target)
1478
1479 def test_determine_kernel_version_exception(self):
1480 """Tests that portage_util exceptions result in returning empty str."""
1481 self.PatchObject(
1482 portage_util,
1483 "GetFlattenedDepsForPackage",
Alex Klein1699fab2022-09-08 08:46:06 -06001484 side_effect=cros_build_lib.RunCommandError("error"),
1485 )
1486 result = packages.determine_kernel_version(self.build_target)
Lizzy Presland0b978e62022-09-09 16:55:29 +00001487 self.assertEqual(result, "")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001488
Alex Klein627e04c2021-11-10 15:56:47 -07001489
Michael Mortensenc2615b72019-10-15 08:12:24 -06001490class ChromeVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001491 """Tests getting chrome version."""
Michael Mortensen14960d02019-10-18 07:53:59 -06001492
Alex Klein1699fab2022-09-08 08:46:06 -06001493 def setUp(self):
1494 self.build_target = build_target_lib.BuildTarget("board")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001495
Alex Klein1699fab2022-09-08 08:46:06 -06001496 def test_determine_chrome_version(self):
1497 """Tests that a valid chrome version is returned."""
1498 # Mock PortageqBestVisible to return a valid chrome version string.
1499 r1_cpf = "chromeos-base/chromeos-chrome-78.0.3900.0_rc-r1"
1500 r1_cpv = package_info.SplitCPV(r1_cpf)
1501 self.PatchObject(
1502 portage_util, "PortageqBestVisible", return_value=r1_cpv
1503 )
Michael Mortensenc2615b72019-10-15 08:12:24 -06001504
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001505 chrome_version = packages.determine_package_version(
1506 constants.CHROME_CP, self.build_target
1507 )
Alex Klein1699fab2022-09-08 08:46:06 -06001508 version_numbers = chrome_version.split(".")
1509 self.assertEqual(len(version_numbers), 4)
1510 self.assertEqual(int(version_numbers[0]), 78)
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001511
Alex Klein1699fab2022-09-08 08:46:06 -06001512 def test_determine_chrome_version_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001513 # Mock what happens when portage throws an exception that bubbles up
1514 # (via RunCommand)inside portage_util.PortageqBestVisible.
Alex Klein1699fab2022-09-08 08:46:06 -06001515 self.PatchObject(
1516 portage_util,
1517 "PortageqBestVisible",
1518 side_effect=cros_build_lib.RunCommandError("error"),
1519 )
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001520 target = packages.determine_package_version(
1521 constants.CHROME_CP, self.build_target
1522 )
Alex Klein1699fab2022-09-08 08:46:06 -06001523 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001524
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001525
1526class PlatformVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001527 """Tests getting platform version."""
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001528
Alex Klein1699fab2022-09-08 08:46:06 -06001529 def test_determine_platform_version(self):
1530 """Test checking that a valid platform version is returned."""
1531 platform_version = packages.determine_platform_version()
1532 # The returned platform version is something like 12603.0.0.
1533 version_string_list = platform_version.split(".")
1534 self.assertEqual(len(version_string_list), 3)
Alex Kleinfee86da2023-01-20 18:40:06 -07001535 # We don't want to check an exact version, but the first number should
1536 # be non-zero.
Alex Klein1699fab2022-09-08 08:46:06 -06001537 self.assertGreaterEqual(int(version_string_list[0]), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001538
Alex Klein1699fab2022-09-08 08:46:06 -06001539 def test_determine_milestone_version(self):
1540 """Test checking that a valid milestone version is returned."""
1541 milestone_version = packages.determine_milestone_version()
1542 # Milestone version should be non-zero
1543 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001544
Alex Klein1699fab2022-09-08 08:46:06 -06001545 def test_determine_full_version(self):
1546 """Test checking that a valid full version is returned."""
1547 full_version = packages.determine_full_version()
1548 pattern = r"^R(\d+)-(\d+.\d+.\d+(-rc\d+)*)"
1549 m = re.match(pattern, full_version)
1550 self.assertTrue(m)
1551 milestone_version = m.group(1)
1552 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001553
Alex Klein1699fab2022-09-08 08:46:06 -06001554 def test_versions_based_on_mock(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001555 # Create a test version_info object, and then mock VersionInfo.from_repo
Alex Klein1699fab2022-09-08 08:46:06 -06001556 # return it.
1557 test_platform_version = "12575.0.0"
1558 test_chrome_branch = "75"
1559 version_info_mock = chromeos_version.VersionInfo(test_platform_version)
1560 version_info_mock.chrome_branch = test_chrome_branch
1561 self.PatchObject(
1562 chromeos_version.VersionInfo,
1563 "from_repo",
1564 return_value=version_info_mock,
1565 )
1566 test_full_version = (
1567 "R" + test_chrome_branch + "-" + test_platform_version
1568 )
1569 platform_version = packages.determine_platform_version()
1570 milestone_version = packages.determine_milestone_version()
1571 full_version = packages.determine_full_version()
1572 self.assertEqual(platform_version, test_platform_version)
1573 self.assertEqual(milestone_version, test_chrome_branch)
1574 self.assertEqual(full_version, test_full_version)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001575
1576
1577# Each of the columns in the following table is a separate dimension along
1578# which Chrome uprev test cases can vary in behavior. The full test space would
1579# be the Cartesian product of the possible values of each column.
1580# 'CHROME_EBUILD' refers to the relationship between the version of the existing
1581# Chrome ebuild vs. the requested uprev version. 'FOLLOWER_EBUILDS' refers to
1582# the same relationship but for the packages defined in OTHER_CHROME_PACKAGES.
1583# 'EBUILDS MODIFIED' refers to whether any of the existing 9999 ebuilds have
1584# modified contents relative to their corresponding stable ebuilds.
1585#
1586# CHROME_EBUILD FOLLOWER_EBUILDS EBUILDS_MODIFIED
1587#
1588# HIGHER HIGHER YES
1589# SAME SAME NO
1590# LOWER LOWER
1591# DOESN'T EXIST YET
1592
1593# These test cases cover both CHROME & FOLLOWER ebuilds being identically
1594# higher, lower, or the same versions, with no modified ebuilds.
1595UPREV_VERSION_CASES = (
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001596 # Uprev.
Chris McDonaldea0312c2020-05-04 23:33:15 -06001597 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001598 "80.0.8080.0",
1599 "81.0.8181.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001600 # One added and one deleted for chrome and each "other" package.
1601 2 * (1 + len(constants.OTHER_CHROME_PACKAGES)),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001602 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001603 id="newer_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001604 ),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001605 # Revbump.
1606 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001607 "80.0.8080.0",
1608 "80.0.8080.0",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001609 2,
1610 True,
Alex Klein1699fab2022-09-08 08:46:06 -06001611 id="chrome_revbump",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001612 ),
Chris McDonaldea0312c2020-05-04 23:33:15 -06001613 # No files should be changed in these cases.
1614 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001615 "80.0.8080.0",
1616 "80.0.8080.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001617 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001618 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001619 id="same_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001620 ),
1621 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001622 "80.0.8080.0",
1623 "79.0.7979.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001624 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001625 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001626 id="older_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001627 ),
1628)
1629
1630
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001631@pytest.mark.parametrize(
Alex Klein1699fab2022-09-08 08:46:06 -06001632 "old_version, new_version, expected_count, modify_unstable",
1633 UPREV_VERSION_CASES,
1634)
1635def test_uprev_chrome_all_files_already_exist(
1636 old_version,
1637 new_version,
1638 expected_count,
1639 modify_unstable,
1640 monkeypatch,
1641 overlay_stack,
1642):
1643 """Test Chrome uprevs work as expected when all packages already exist."""
1644 (overlay,) = overlay_stack(1)
1645 monkeypatch.setattr(uprev_lib, "_CHROME_OVERLAY_PATH", overlay.path)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001646
Alex Klein1699fab2022-09-08 08:46:06 -06001647 unstable_chrome = cr.test.Package(
1648 "chromeos-base", "chromeos-chrome", version="9999", keywords="~*"
1649 )
1650 if modify_unstable:
1651 # Add some field not set in stable.
1652 unstable_chrome.depend = "foo/bar"
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001653
Alex Klein1699fab2022-09-08 08:46:06 -06001654 stable_chrome = cr.test.Package(
1655 "chromeos-base", "chromeos-chrome", version=f"{old_version}_rc-r1"
1656 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001657
Alex Klein1699fab2022-09-08 08:46:06 -06001658 overlay.add_package(unstable_chrome)
1659 overlay.add_package(stable_chrome)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001660
Alex Klein1699fab2022-09-08 08:46:06 -06001661 for pkg_str in constants.OTHER_CHROME_PACKAGES:
1662 category, pkg_name = pkg_str.split("/")
1663 unstable_pkg = cr.test.Package(
1664 category, pkg_name, version="9999", keywords="~*"
1665 )
1666 stable_pkg = cr.test.Package(
1667 category, pkg_name, version=f"{old_version}_rc-r1"
1668 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001669
Alex Klein1699fab2022-09-08 08:46:06 -06001670 overlay.add_package(unstable_pkg)
1671 overlay.add_package(stable_pkg)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001672
Alex Klein1699fab2022-09-08 08:46:06 -06001673 git_refs = [
Alex Klein220e3a72023-09-14 17:12:13 -06001674 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -06001675 path="/foo", ref=f"refs/tags/{new_version}", revision="stubcommit"
1676 )
1677 ]
1678 res = packages.uprev_chrome_from_ref(None, git_refs, None)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001679
Alex Klein1699fab2022-09-08 08:46:06 -06001680 modified_file_count = sum(len(m.files) for m in res.modified)
1681 assert modified_file_count == expected_count
Michael Mortensen125bb012020-05-21 14:02:10 -06001682
1683
Alex Klein1699fab2022-09-08 08:46:06 -06001684@pytest.mark.usefixtures("testcase_monkeypatch")
Michael Mortensen125bb012020-05-21 14:02:10 -06001685class GetModelsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001686 """Tests for get_models."""
Michael Mortensen125bb012020-05-21 14:02:10 -06001687
Alex Klein1699fab2022-09-08 08:46:06 -06001688 def setUp(self):
1689 self.board = "test-board"
1690 self.rc.SetDefaultCmdResult(stdout="pyro\nreef\nsnappy\n")
1691 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1692 build_bin = os.path.join(
1693 self.tempdir, constants.DEFAULT_CHROOT_DIR, "usr", "bin"
1694 )
1695 osutils.Touch(
1696 os.path.join(build_bin, "cros_config_host"), makedirs=True
1697 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001698
Alex Klein1699fab2022-09-08 08:46:06 -06001699 def testGetModels(self):
1700 """Test get_models."""
1701 build_target = build_target_lib.BuildTarget(self.board)
1702 result = packages.get_models(build_target)
1703 self.assertEqual(result, ["pyro", "reef", "snappy"])
Michael Mortensen359c1f32020-05-28 19:35:42 -06001704
1705
1706class GetKeyIdTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001707 """Tests for get_key_id."""
Michael Mortensen359c1f32020-05-28 19:35:42 -06001708
Alex Klein1699fab2022-09-08 08:46:06 -06001709 def setUp(self):
1710 self.board = "test-board"
1711 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensen359c1f32020-05-28 19:35:42 -06001712
Alex Klein1699fab2022-09-08 08:46:06 -06001713 def testGetKeyId(self):
1714 """Test get_key_id when _run_cros_config_host returns a key."""
1715 self.PatchObject(
1716 packages, "_run_cros_config_host", return_value=["key"]
1717 )
1718 result = packages.get_key_id(self.build_target, "model")
1719 self.assertEqual(result, "key")
Michael Mortensen359c1f32020-05-28 19:35:42 -06001720
Alex Klein1699fab2022-09-08 08:46:06 -06001721 def testGetKeyIdNoKey(self):
1722 """Test get_key_id when None should be returned."""
1723 self.PatchObject(
1724 packages, "_run_cros_config_host", return_value=["key1", "key2"]
1725 )
1726 result = packages.get_key_id(self.build_target, "model")
1727 self.assertEqual(result, None)
Ben Reiche779cf42020-12-15 03:21:31 +00001728
1729
Harvey Yang3eee06c2021-03-18 15:47:56 +08001730class GetLatestVersionTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001731 """Tests for get_latest_version_from_refs."""
Ben Reiche779cf42020-12-15 03:21:31 +00001732
Alex Klein1699fab2022-09-08 08:46:06 -06001733 def setUp(self):
1734 self.prefix = "refs/tags/drivefs_"
1735 # The tag ref template.
1736 ref_tpl = self.prefix + "%s"
Ben Reiche779cf42020-12-15 03:21:31 +00001737
Alex Klein1699fab2022-09-08 08:46:06 -06001738 self.latest = "44.0.20"
1739 self.versions = ["42.0.1", self.latest, "44.0.19", "39.0.15"]
1740 self.latest_ref = uprev_lib.GitRef(
1741 "/path", ref_tpl % self.latest, "abc123"
1742 )
1743 self.refs = [
1744 uprev_lib.GitRef("/path", ref_tpl % v, "abc123")
1745 for v in self.versions
1746 ]
Ben Reiche779cf42020-12-15 03:21:31 +00001747
Alex Klein1699fab2022-09-08 08:46:06 -06001748 def test_single_ref(self):
1749 """Test a single ref is supplied."""
1750 # pylint: disable=protected-access
1751 self.assertEqual(
1752 self.latest,
1753 packages._get_latest_version_from_refs(
1754 self.prefix, [self.latest_ref]
1755 ),
1756 )
Ben Reiche779cf42020-12-15 03:21:31 +00001757
Alex Klein1699fab2022-09-08 08:46:06 -06001758 def test_multiple_ref_versions(self):
1759 """Test multiple refs supplied."""
1760 # pylint: disable=protected-access
1761 self.assertEqual(
1762 self.latest,
1763 packages._get_latest_version_from_refs(self.prefix, self.refs),
1764 )
Ben Reiche779cf42020-12-15 03:21:31 +00001765
Alex Klein1699fab2022-09-08 08:46:06 -06001766 def test_no_refs_returns_none(self):
1767 """Test no refs supplied."""
1768 # pylint: disable=protected-access
1769 self.assertEqual(
1770 packages._get_latest_version_from_refs(self.prefix, []), None
1771 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001772
Chinglin Yu84818732022-10-03 12:03:43 +08001773 def test_ref_prefix(self):
1774 """Test refs with a different prefix isn't used"""
1775 # pylint: disable=protected-access
1776 # Add refs/tags/foo_100.0.0 to the refs, which should be ignored in
1777 # _get_latest_version_from_refs because the prefix doesn't match, even
1778 # if its version number is larger.
1779 refs = self.refs + [
1780 uprev_lib.GitRef("/path", "refs/tags/foo_100.0.0", "abc123")
1781 ]
1782 self.assertEqual(
1783 self.latest,
1784 packages._get_latest_version_from_refs(self.prefix, refs),
1785 )
1786
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001787
Alex Klein6becabc2020-09-11 14:03:05 -06001788class NeedsChromeSourceTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001789 """Tests for needs_chrome_source."""
Alex Klein6becabc2020-09-11 14:03:05 -06001790
Alex Klein1699fab2022-09-08 08:46:06 -06001791 def _build_graph(self, with_chrome: bool, with_followers: bool):
1792 root = "/build/build_target"
1793 foo_bar = package_info.parse("foo/bar-1")
1794 chrome = package_info.parse(f"{constants.CHROME_CP}-1.2.3.4")
1795 followers = [
1796 package_info.parse(f"{pkg}-1.2.3.4")
1797 for pkg in constants.OTHER_CHROME_PACKAGES
1798 ]
1799 nodes = [dependency_graph.PackageNode(foo_bar, root)]
1800 root_pkgs = ["foo/bar-1"]
1801 if with_chrome:
1802 nodes.append(dependency_graph.PackageNode(chrome, root))
1803 root_pkgs.append(chrome.cpvr)
1804 if with_followers:
1805 nodes.extend(
1806 [dependency_graph.PackageNode(f, root) for f in followers]
1807 )
1808 root_pkgs.extend([f.cpvr for f in followers])
Alex Klein6becabc2020-09-11 14:03:05 -06001809
Alex Klein1699fab2022-09-08 08:46:06 -06001810 return dependency_graph.DependencyGraph(nodes, root, root_pkgs)
Alex Klein6becabc2020-09-11 14:03:05 -06001811
Alex Klein1699fab2022-09-08 08:46:06 -06001812 def test_needs_all(self):
1813 """Verify we need source when we have no prebuilts."""
1814 graph = self._build_graph(with_chrome=True, with_followers=True)
1815 self.PatchObject(
1816 depgraph, "get_sysroot_dependency_graph", return_value=graph
1817 )
1818 self.PatchObject(packages, "has_prebuilt", return_value=False)
1819 self.PatchObject(
1820 packages,
1821 "uprev_chrome",
1822 return_value=uprev_lib.UprevVersionedPackageResult(),
1823 )
Alex Klein6becabc2020-09-11 14:03:05 -06001824
Alex Klein1699fab2022-09-08 08:46:06 -06001825 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001826
Alex Klein1699fab2022-09-08 08:46:06 -06001827 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001828
Alex Klein1699fab2022-09-08 08:46:06 -06001829 self.assertTrue(result.needs_chrome_source)
1830 self.assertTrue(result.builds_chrome)
1831 self.assertTrue(result.packages)
1832 self.assertEqual(
1833 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1834 )
1835 self.assertTrue(result.missing_chrome_prebuilt)
1836 self.assertTrue(result.missing_follower_prebuilt)
1837 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001838
Alex Klein1699fab2022-09-08 08:46:06 -06001839 def test_needs_none(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001840 """Verify not building any chrome packages prevents needing it."""
Alex Klein1699fab2022-09-08 08:46:06 -06001841 graph = self._build_graph(with_chrome=False, with_followers=False)
1842 self.PatchObject(
1843 depgraph, "get_sysroot_dependency_graph", return_value=graph
1844 )
1845 self.PatchObject(packages, "has_prebuilt", return_value=False)
1846 self.PatchObject(
1847 packages,
1848 "uprev_chrome",
1849 return_value=uprev_lib.UprevVersionedPackageResult(),
1850 )
Alex Klein6becabc2020-09-11 14:03:05 -06001851
Alex Klein1699fab2022-09-08 08:46:06 -06001852 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001853
Alex Klein1699fab2022-09-08 08:46:06 -06001854 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001855
Alex Klein1699fab2022-09-08 08:46:06 -06001856 self.assertFalse(result.needs_chrome_source)
1857 self.assertFalse(result.builds_chrome)
1858 self.assertFalse(result.packages)
1859 self.assertFalse(result.missing_chrome_prebuilt)
1860 self.assertFalse(result.missing_follower_prebuilt)
1861 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001862
Alex Klein1699fab2022-09-08 08:46:06 -06001863 def test_needs_chrome_only(self):
1864 """Verify only chrome triggers needs chrome source."""
1865 graph = self._build_graph(with_chrome=True, with_followers=False)
1866 self.PatchObject(
1867 depgraph, "get_sysroot_dependency_graph", return_value=graph
1868 )
1869 self.PatchObject(packages, "has_prebuilt", return_value=False)
1870 self.PatchObject(
1871 packages,
1872 "uprev_chrome",
1873 return_value=uprev_lib.UprevVersionedPackageResult(),
1874 )
Alex Klein6becabc2020-09-11 14:03:05 -06001875
Alex Klein1699fab2022-09-08 08:46:06 -06001876 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001877
Alex Klein1699fab2022-09-08 08:46:06 -06001878 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001879
Alex Klein1699fab2022-09-08 08:46:06 -06001880 self.assertTrue(result.needs_chrome_source)
1881 self.assertTrue(result.builds_chrome)
1882 self.assertTrue(result.packages)
1883 self.assertEqual(
Alex Klein041edd82023-04-17 12:23:23 -06001884 {p.atom for p in result.packages}, {constants.CHROME_CP}
Alex Klein1699fab2022-09-08 08:46:06 -06001885 )
1886 self.assertTrue(result.missing_chrome_prebuilt)
1887 self.assertFalse(result.missing_follower_prebuilt)
1888 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001889
Alex Klein1699fab2022-09-08 08:46:06 -06001890 def test_needs_followers_only(self):
1891 """Verify only chrome followers triggers needs chrome source."""
1892 graph = self._build_graph(with_chrome=False, with_followers=True)
1893 self.PatchObject(
1894 depgraph, "get_sysroot_dependency_graph", return_value=graph
1895 )
1896 self.PatchObject(packages, "has_prebuilt", return_value=False)
1897 self.PatchObject(
1898 packages,
1899 "uprev_chrome",
1900 return_value=uprev_lib.UprevVersionedPackageResult(),
1901 )
Alex Klein6becabc2020-09-11 14:03:05 -06001902
Alex Klein1699fab2022-09-08 08:46:06 -06001903 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001904
Alex Klein1699fab2022-09-08 08:46:06 -06001905 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001906
Alex Klein1699fab2022-09-08 08:46:06 -06001907 self.assertTrue(result.needs_chrome_source)
1908 self.assertFalse(result.builds_chrome)
1909 self.assertTrue(result.packages)
1910 self.assertEqual(
Alex Klein041edd82023-04-17 12:23:23 -06001911 {p.atom for p in result.packages},
Alex Klein1699fab2022-09-08 08:46:06 -06001912 set(constants.OTHER_CHROME_PACKAGES),
1913 )
1914 self.assertFalse(result.missing_chrome_prebuilt)
1915 self.assertTrue(result.missing_follower_prebuilt)
1916 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001917
Alex Klein1699fab2022-09-08 08:46:06 -06001918 def test_has_prebuilts(self):
1919 """Test prebuilts prevent us from needing chrome source."""
1920 graph = self._build_graph(with_chrome=True, with_followers=True)
1921 self.PatchObject(
1922 depgraph, "get_sysroot_dependency_graph", return_value=graph
1923 )
1924 self.PatchObject(packages, "has_prebuilt", return_value=True)
1925 self.PatchObject(
1926 packages,
1927 "uprev_chrome",
1928 return_value=uprev_lib.UprevVersionedPackageResult(),
1929 )
Alex Klein6becabc2020-09-11 14:03:05 -06001930
Alex Klein1699fab2022-09-08 08:46:06 -06001931 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001932
Alex Klein1699fab2022-09-08 08:46:06 -06001933 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001934
Alex Klein1699fab2022-09-08 08:46:06 -06001935 self.assertFalse(result.needs_chrome_source)
1936 self.assertTrue(result.builds_chrome)
1937 self.assertFalse(result.packages)
1938 self.assertFalse(result.missing_chrome_prebuilt)
1939 self.assertFalse(result.missing_follower_prebuilt)
1940 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001941
Alex Klein1699fab2022-09-08 08:46:06 -06001942 def test_compile_source(self):
1943 """Test compile source ignores prebuilts."""
1944 graph = self._build_graph(with_chrome=True, with_followers=True)
1945 self.PatchObject(
1946 depgraph, "get_sysroot_dependency_graph", return_value=graph
1947 )
1948 self.PatchObject(packages, "has_prebuilt", return_value=True)
1949 self.PatchObject(
1950 packages,
1951 "uprev_chrome",
1952 return_value=uprev_lib.UprevVersionedPackageResult(),
1953 )
Alex Klein6becabc2020-09-11 14:03:05 -06001954
Alex Klein1699fab2022-09-08 08:46:06 -06001955 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001956
Alex Klein1699fab2022-09-08 08:46:06 -06001957 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Klein6becabc2020-09-11 14:03:05 -06001958
Alex Klein1699fab2022-09-08 08:46:06 -06001959 self.assertTrue(result.needs_chrome_source)
1960 self.assertTrue(result.builds_chrome)
1961 self.assertTrue(result.packages)
1962 self.assertEqual(
1963 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1964 )
1965 self.assertTrue(result.missing_chrome_prebuilt)
1966 self.assertTrue(result.missing_follower_prebuilt)
1967 self.assertFalse(result.local_uprev)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001968
Alex Klein1699fab2022-09-08 08:46:06 -06001969 def test_local_uprev(self):
1970 """Test compile source ignores prebuilts."""
1971 graph = self._build_graph(with_chrome=True, with_followers=True)
1972 self.PatchObject(
1973 depgraph, "get_sysroot_dependency_graph", return_value=graph
1974 )
1975 self.PatchObject(packages, "has_prebuilt", return_value=False)
Alex Klein75110572021-07-14 10:44:39 -06001976
Alex Klein1699fab2022-09-08 08:46:06 -06001977 uprev_result = uprev_lib.UprevVersionedPackageResult()
1978 uprev_result.add_result("1.2.3.4", ["/tmp/foo"])
1979 self.PatchObject(packages, "uprev_chrome", return_value=uprev_result)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001980
Alex Klein1699fab2022-09-08 08:46:06 -06001981 build_target = build_target_lib.BuildTarget("build_target")
Alex Kleinde7b76d2021-07-12 12:28:44 -06001982
Alex Klein1699fab2022-09-08 08:46:06 -06001983 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001984
Alex Klein1699fab2022-09-08 08:46:06 -06001985 self.assertTrue(result.needs_chrome_source)
1986 self.assertTrue(result.builds_chrome)
1987 self.assertTrue(result.packages)
1988 self.assertEqual(
1989 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1990 )
1991 self.assertTrue(result.missing_chrome_prebuilt)
1992 self.assertTrue(result.missing_follower_prebuilt)
1993 self.assertTrue(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001994
1995
Mike Frysinger8c9d7582023-08-21 19:28:21 -04001996class GetTargetVersionTest(cros_test_lib.RunCommandTestCase):
1997 """Tests for get_target_version."""
1998
1999 def setUp(self):
2000 self.build_target = build_target_lib.BuildTarget("build_target")
2001
2002 def test_default_empty(self):
2003 """Default behavior with mostly stub empty data."""
2004
2005 def GetBuildDependency(sysroot_path, board, mock_packages):
2006 assert sysroot_path == self.build_target.root
2007 assert board == self.build_target.name
2008 assert list(mock_packages) == [
2009 package_info.parse(constants.TARGET_OS_PKG)
2010 ]
2011 return {"package_deps": []}, {}
2012
2013 self.PatchObject(
2014 dependency, "GetBuildDependency", side_effect=GetBuildDependency
2015 )
2016 ret = packages.get_target_versions(self.build_target)
2017 assert ret.android_version is None
2018 assert ret.android_branch is None
2019 assert ret.android_target is None
2020 assert ret.chrome_version is None
2021 assert isinstance(ret.platform_version, str)
2022 assert isinstance(ret.milestone_version, str)
2023 assert isinstance(ret.full_version, str)
2024 assert ret.lacros_version is None
2025
2026
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002027class UprevDrivefsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002028 """Tests for uprev_drivefs."""
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002029
Alex Klein1699fab2022-09-08 08:46:06 -06002030 def setUp(self):
2031 self.refs = [
Alex Klein220e3a72023-09-14 17:12:13 -06002032 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -06002033 path="/chromeos/platform/drivefs-google3/",
2034 ref="refs/tags/drivefs_45.0.2",
2035 revision="123",
2036 )
2037 ]
2038 self.MOCK_DRIVEFS_EBUILD_PATH = "drivefs.45.0.2-r1.ebuild"
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002039
Alex Klein1699fab2022-09-08 08:46:06 -06002040 def revisionBumpOutcome(self, ebuild_path):
2041 return uprev_lib.UprevResult(
2042 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2043 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002044
Alex Klein1699fab2022-09-08 08:46:06 -06002045 def majorBumpOutcome(self, ebuild_path):
2046 return uprev_lib.UprevResult(
2047 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2048 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002049
Alex Klein1699fab2022-09-08 08:46:06 -06002050 def sameVersionOutcome(self):
2051 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002052
Alex Klein1699fab2022-09-08 08:46:06 -06002053 def test_latest_version_returns_none(self):
2054 """Test no refs were supplied"""
2055 output = packages.uprev_drivefs(None, [], None)
2056 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002057
Alex Klein1699fab2022-09-08 08:46:06 -06002058 def test_drivefs_uprev_fails(self):
2059 """Test a single ref is supplied."""
2060 self.PatchObject(
2061 uprev_lib,
2062 "uprev_workon_ebuild_to_version",
2063 side_effect=[None, None],
2064 )
2065 output = packages.uprev_drivefs(None, self.refs, None)
2066 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002067
Alex Klein1699fab2022-09-08 08:46:06 -06002068 def test_same_version_exists(self):
2069 """Test the same version exists uprev should not happen."""
2070 drivefs_outcome = self.sameVersionOutcome()
2071 self.PatchObject(
2072 uprev_lib,
2073 "uprev_workon_ebuild_to_version",
2074 side_effect=[drivefs_outcome],
2075 )
2076 output = packages.uprev_drivefs(None, self.refs, None)
2077 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002078
Alex Klein1699fab2022-09-08 08:46:06 -06002079 def test_revision_bump_both_packages(self):
2080 """Test both packages uprev, should succeed."""
2081 drivefs_outcome = self.revisionBumpOutcome(
2082 self.MOCK_DRIVEFS_EBUILD_PATH
2083 )
2084 self.PatchObject(
2085 uprev_lib,
2086 "uprev_workon_ebuild_to_version",
2087 side_effect=[drivefs_outcome],
2088 )
2089 output = packages.uprev_drivefs(None, self.refs, None)
2090 self.assertTrue(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002091
Alex Klein1699fab2022-09-08 08:46:06 -06002092 def test_major_bump_both_packages(self):
2093 """Test both packages uprev, should succeed."""
2094 drivefs_outcome = self.majorBumpOutcome(self.MOCK_DRIVEFS_EBUILD_PATH)
2095 self.PatchObject(
2096 uprev_lib,
2097 "uprev_workon_ebuild_to_version",
2098 side_effect=[drivefs_outcome],
2099 )
2100 output = packages.uprev_drivefs(None, self.refs, None)
2101 self.assertTrue(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002102
2103
Denis Nikitin63613e32022-09-09 22:26:50 -07002104class UprevKernelAfdo(cros_test_lib.RunCommandTempDirTestCase):
2105 """Tests for uprev_kernel_afdo."""
2106
2107 def setUp(self):
2108 # patch_ebuild_vars is tested separately.
2109 self.mock_patch = self.PatchObject(packages, "patch_ebuild_vars")
Denis Nikitin88ad5132022-09-28 12:10:01 -07002110 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Denis Nikitin63613e32022-09-09 22:26:50 -07002111 self.metadata_dir = os.path.join(
Denis Nikitin63613e32022-09-09 22:26:50 -07002112 "src",
2113 "third_party",
2114 "toolchain-utils",
2115 "afdo_metadata",
2116 )
Denis Nikitin88ad5132022-09-28 12:10:01 -07002117 osutils.SafeMakedirs(os.path.join(self.tempdir, self.metadata_dir))
Denis Nikitin63613e32022-09-09 22:26:50 -07002118
2119 def test_uprev_kernel_afdo_version(self):
2120 """Test kernel afdo version uprev."""
2121 json_files = {
2122 "kernel_afdo.json": (
2123 "{\n"
2124 ' "chromeos-kernel-5_4": {\n'
2125 ' "name": "R106-12345.0-0123456789"\n'
2126 " }\n"
2127 "}"
2128 ),
2129 "kernel_arm_afdo.json": (
2130 "{\n"
2131 ' "chromeos-kernel-5_15": {\n'
2132 ' "name": "R107-67890.0-0123456789"\n'
2133 " }\n"
2134 "}"
2135 ),
2136 }
2137 for f, contents in json_files.items():
2138 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2139
Alex Klein220e3a72023-09-14 17:12:13 -06002140 returned_output = packages.uprev_kernel_afdo(
2141 None, [], chroot_lib.Chroot()
2142 )
Denis Nikitin63613e32022-09-09 22:26:50 -07002143
Denis Nikitin88ad5132022-09-28 12:10:01 -07002144 package_root = os.path.join(
2145 constants.SOURCE_ROOT,
2146 constants.CHROMIUMOS_OVERLAY_DIR,
2147 "sys-kernel",
Denis Nikitin63613e32022-09-09 22:26:50 -07002148 )
2149 expect_result = [
2150 uprev_lib.UprevVersionedPackageModifications(
2151 new_version="R106-12345.0-0123456789",
2152 files=[
2153 os.path.join(
2154 package_root,
2155 "chromeos-kernel-5_4",
2156 "chromeos-kernel-5_4-9999.ebuild",
2157 ),
2158 os.path.join(
2159 package_root, "chromeos-kernel-5_4", "Manifest"
2160 ),
2161 ],
2162 ),
2163 uprev_lib.UprevVersionedPackageModifications(
2164 new_version="R107-67890.0-0123456789",
2165 files=[
2166 os.path.join(
2167 package_root,
2168 "chromeos-kernel-5_15",
2169 "chromeos-kernel-5_15-9999.ebuild",
2170 ),
2171 os.path.join(
2172 package_root, "chromeos-kernel-5_15", "Manifest"
2173 ),
2174 ],
2175 ),
2176 ]
2177 self.assertTrue(returned_output.uprevved)
2178 self.assertEqual(returned_output.modified, expect_result)
2179
2180 def test_uprev_kernel_afdo_empty_json(self):
2181 """Test kernel afdo version unchanged."""
2182 json_files = {
2183 "kernel_afdo.json": "{}",
2184 "kernel_arm_afdo.json": "{}",
2185 }
2186 for f, contents in json_files.items():
2187 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2188
Alex Klein220e3a72023-09-14 17:12:13 -06002189 returned_output = packages.uprev_kernel_afdo(
2190 None, [], chroot_lib.Chroot()
2191 )
Denis Nikitin63613e32022-09-09 22:26:50 -07002192 self.assertFalse(returned_output.uprevved)
2193
2194 def test_uprev_kernel_afdo_empty_file(self):
2195 """Test malformed json raises."""
2196 json_files = {
2197 "kernel_afdo.json": "",
2198 "kernel_arm_afdo.json": "",
2199 }
2200 for f, contents in json_files.items():
2201 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2202
2203 with self.assertRaisesRegex(
2204 json.decoder.JSONDecodeError, "Expecting value"
2205 ):
Alex Klein220e3a72023-09-14 17:12:13 -06002206 packages.uprev_kernel_afdo(None, [], chroot_lib.Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002207
2208 def test_uprev_kernel_afdo_manifest_raises(self):
2209 """Test manifest update raises."""
2210 json_files = {
2211 "kernel_afdo.json": (
2212 "{\n"
2213 ' "chromeos-kernel-5_4": {\n'
2214 ' "name": "R106-12345.0-0123456789"\n'
2215 " }\n"
2216 "}"
2217 ),
2218 }
2219 for f, contents in json_files.items():
2220 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2221 # run() raises exception.
2222 self.rc.SetDefaultCmdResult(
2223 side_effect=cros_build_lib.RunCommandError("error")
2224 )
2225
2226 with self.assertRaises(uprev_lib.EbuildManifestError):
Alex Klein220e3a72023-09-14 17:12:13 -06002227 packages.uprev_kernel_afdo(None, [], chroot_lib.Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002228
2229
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002230# TODO(chenghaoyang): Shouldn't use uprev_workon_ebuild_to_version.
2231class UprevPerfettoTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002232 """Tests for uprev_perfetto."""
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002233
Alex Klein1699fab2022-09-08 08:46:06 -06002234 def setUp(self):
Alex Klein220e3a72023-09-14 17:12:13 -06002235 self.refs = [
2236 uprev_lib.GitRef(path="/foo", ref="refs/tags/v12.0", revision="123")
2237 ]
Alex Klein1699fab2022-09-08 08:46:06 -06002238 self.MOCK_PERFETTO_EBUILD_PATH = "perfetto-12.0-r1.ebuild"
Chinglin Yufa728552023-04-13 03:12:04 +00002239 self.MOCK_PERFETTO_PROTO_EBUILD_PATH = "perfetto-protos-12.0-r1.ebuild"
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002240
Chinglin Yufa728552023-04-13 03:12:04 +00002241 def revisionBumpOutcome(self):
2242 return [
2243 uprev_lib.UprevResult(
2244 uprev_lib.Outcome.REVISION_BUMP,
2245 [self.MOCK_PERFETTO_EBUILD_PATH],
2246 ),
2247 uprev_lib.UprevResult(
2248 uprev_lib.Outcome.REVISION_BUMP,
2249 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2250 ),
2251 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002252
Chinglin Yufa728552023-04-13 03:12:04 +00002253 def majorBumpOutcome(self):
2254 return [
2255 uprev_lib.UprevResult(
2256 uprev_lib.Outcome.VERSION_BUMP, [self.MOCK_PERFETTO_EBUILD_PATH]
2257 ),
2258 uprev_lib.UprevResult(
2259 uprev_lib.Outcome.VERSION_BUMP,
2260 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2261 ),
2262 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002263
Alex Klein1699fab2022-09-08 08:46:06 -06002264 def newerVersionOutcome(self):
2265 return uprev_lib.UprevResult(uprev_lib.Outcome.NEWER_VERSION_EXISTS)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002266
Alex Klein1699fab2022-09-08 08:46:06 -06002267 def sameVersionOutcome(self):
2268 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002269
Alex Klein1699fab2022-09-08 08:46:06 -06002270 def test_latest_version_returns_none(self):
2271 """Test no refs were supplied"""
2272 output = packages.uprev_perfetto(None, [], None)
2273 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002274
Alex Klein1699fab2022-09-08 08:46:06 -06002275 def test_perfetto_uprev_fails(self):
2276 """Test a single ref is supplied."""
2277 self.PatchObject(
2278 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2279 )
2280 output = packages.uprev_perfetto(None, self.refs, None)
2281 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002282
Alex Klein1699fab2022-09-08 08:46:06 -06002283 def test_newer_version_exists(self):
2284 """Test the newer version exists uprev should not happen."""
2285 perfetto_outcome = self.newerVersionOutcome()
2286 self.PatchObject(
2287 uprev_lib,
2288 "uprev_workon_ebuild_to_version",
2289 side_effect=[perfetto_outcome],
2290 )
2291 output = packages.uprev_perfetto(None, self.refs, None)
2292 self.assertFalse(output.uprevved)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002293
Alex Klein1699fab2022-09-08 08:46:06 -06002294 def test_same_version_exists(self):
2295 """Test the same version exists uprev should not happen."""
2296 perfetto_outcome = self.sameVersionOutcome()
2297 self.PatchObject(
2298 uprev_lib,
2299 "uprev_workon_ebuild_to_version",
2300 side_effect=[perfetto_outcome],
2301 )
2302 output = packages.uprev_perfetto(None, self.refs, None)
2303 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002304
Alex Klein1699fab2022-09-08 08:46:06 -06002305 def test_revision_bump_perfetto_package(self):
2306 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002307 self.PatchObject(
2308 uprev_lib,
2309 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002310 side_effect=self.revisionBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002311 )
2312 output = packages.uprev_perfetto(None, self.refs, None)
2313 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002314 self.assertEqual(
2315 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2316 )
2317 self.assertEqual(
2318 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2319 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002320
Alex Klein1699fab2022-09-08 08:46:06 -06002321 def test_major_bump_perfetto_package(self):
2322 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002323 self.PatchObject(
2324 uprev_lib,
2325 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002326 side_effect=self.majorBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002327 )
2328 output = packages.uprev_perfetto(None, self.refs, None)
2329 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002330 self.assertEqual(
2331 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2332 )
2333 self.assertEqual(
2334 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2335 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002336
Chinglin Yuad12a512022-10-07 17:26:12 +08002337 def test_revision_bump_trunk(self):
2338 """Test revision bump on receiving non-versioned trunk refs."""
Chinglin Yu5de28a42022-11-11 19:52:21 +08002339 refs = [
Alex Klein220e3a72023-09-14 17:12:13 -06002340 uprev_lib.GitRef(
Chinglin Yu5de28a42022-11-11 19:52:21 +08002341 path="/foo", ref="refs/heads/main", revision="0123456789abcdef"
2342 )
2343 ]
Chinglin Yuad12a512022-10-07 17:26:12 +08002344 self.PatchObject(
2345 uprev_lib, "get_stable_ebuild_version", return_value="12.0"
2346 )
2347 self.PatchObject(
2348 uprev_lib,
2349 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002350 side_effect=self.revisionBumpOutcome(),
Chinglin Yuad12a512022-10-07 17:26:12 +08002351 )
2352 output = packages.uprev_perfetto(None, refs, None)
Chinglin Yufa728552023-04-13 03:12:04 +00002353
Chinglin Yuad12a512022-10-07 17:26:12 +08002354 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002355 self.assertEqual(
2356 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2357 )
Chinglin Yu5de28a42022-11-11 19:52:21 +08002358 self.assertEqual(output.modified[0].new_version, "12.0-012345678")
Chinglin Yufa728552023-04-13 03:12:04 +00002359 self.assertEqual(
2360 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2361 )
2362 self.assertEqual(output.modified[1].new_version, "12.0-012345678")
Chinglin Yuad12a512022-10-07 17:26:12 +08002363
Alex Klein627e04c2021-11-10 15:56:47 -07002364
Julio Hurtadof1befec2021-05-05 21:34:26 +00002365class UprevLacrosTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002366 """Tests for uprev_lacros"""
Julio Hurtadof1befec2021-05-05 21:34:26 +00002367
Alex Klein1699fab2022-09-08 08:46:06 -06002368 def setUp(self):
2369 self.refs = [
Alex Klein220e3a72023-09-14 17:12:13 -06002370 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -06002371 path="/lacros", ref="refs/heads/main", revision="123.456.789.0"
2372 )
2373 ]
2374 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtadof1befec2021-05-05 21:34:26 +00002375
Alex Klein1699fab2022-09-08 08:46:06 -06002376 def revisionBumpOutcome(self, ebuild_path):
2377 return uprev_lib.UprevResult(
2378 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2379 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002380
Alex Klein1699fab2022-09-08 08:46:06 -06002381 def majorBumpOutcome(self, ebuild_path):
2382 return uprev_lib.UprevResult(
2383 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2384 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002385
Alex Klein1699fab2022-09-08 08:46:06 -06002386 def newerVersionOutcome(self, ebuild_path):
2387 return uprev_lib.UprevResult(
2388 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2389 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002390
Alex Klein1699fab2022-09-08 08:46:06 -06002391 def sameVersionOutcome(self, ebuild_path):
2392 return uprev_lib.UprevResult(
2393 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2394 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002395
Alex Klein1699fab2022-09-08 08:46:06 -06002396 def newEbuildCreatedOutcome(self, ebuild_path):
2397 return uprev_lib.UprevResult(
2398 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2399 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002400
Alex Klein1699fab2022-09-08 08:46:06 -06002401 def test_lacros_uprev_fails(self):
2402 """Test a lacros package uprev with no triggers"""
2403 self.PatchObject(
2404 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2405 )
2406 with self.assertRaises(IndexError):
2407 packages.uprev_lacros(None, [], None)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002408
Alex Klein1699fab2022-09-08 08:46:06 -06002409 def test_lacros_uprev_revision_bump(self):
2410 """Test lacros package uprev."""
2411 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2412 self.PatchObject(
2413 uprev_lib,
2414 "uprev_workon_ebuild_to_version",
2415 side_effect=[lacros_outcome],
2416 )
2417 output = packages.uprev_lacros(None, self.refs, None)
2418 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002419
Alex Klein1699fab2022-09-08 08:46:06 -06002420 def test_lacros_uprev_version_bump(self):
2421 """Test lacros package uprev."""
2422 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2423 self.PatchObject(
2424 uprev_lib,
2425 "uprev_workon_ebuild_to_version",
2426 side_effect=[lacros_outcome],
2427 )
2428 output = packages.uprev_lacros(None, self.refs, None)
2429 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002430
Alex Klein1699fab2022-09-08 08:46:06 -06002431 def test_lacros_uprev_new_ebuild_created(self):
2432 """Test lacros package uprev."""
2433 lacros_outcome = self.newEbuildCreatedOutcome(
2434 self.MOCK_LACROS_EBUILD_PATH
2435 )
2436 self.PatchObject(
2437 uprev_lib,
2438 "uprev_workon_ebuild_to_version",
2439 side_effect=[lacros_outcome],
2440 )
2441 output = packages.uprev_lacros(None, self.refs, None)
2442 self.assertTrue(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002443
Alex Klein1699fab2022-09-08 08:46:06 -06002444 def test_lacros_uprev_newer_version_exist(self):
2445 """Test the newer version exists uprev should not happen."""
2446 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2447 self.PatchObject(
2448 uprev_lib,
2449 "uprev_workon_ebuild_to_version",
2450 side_effect=[lacros_outcome],
2451 )
2452 output = packages.uprev_lacros(None, self.refs, None)
2453 self.assertFalse(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002454
Alex Klein1699fab2022-09-08 08:46:06 -06002455 def test_lacros_uprev_same_version_exist(self):
2456 """Test the same version exists uprev should not happen."""
2457 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2458 self.PatchObject(
2459 uprev_lib,
2460 "uprev_workon_ebuild_to_version",
2461 side_effect=[lacros_outcome],
2462 )
2463 output = packages.uprev_lacros(None, self.refs, None)
2464 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002465
2466
2467class UprevLacrosInParallelTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002468 """Tests for uprev_lacros"""
Julio Hurtado870ed322021-12-03 18:22:40 +00002469
Alex Klein1699fab2022-09-08 08:46:06 -06002470 def setUp(self):
2471 self.refs = [
Alex Klein220e3a72023-09-14 17:12:13 -06002472 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -06002473 path="/lacros", revision="abc123", ref="refs/tags/123.456.789.0"
2474 )
2475 ]
2476 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtado870ed322021-12-03 18:22:40 +00002477
Alex Klein1699fab2022-09-08 08:46:06 -06002478 def revisionBumpOutcome(self, ebuild_path):
2479 return uprev_lib.UprevResult(
2480 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2481 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002482
Alex Klein1699fab2022-09-08 08:46:06 -06002483 def majorBumpOutcome(self, ebuild_path):
2484 return uprev_lib.UprevResult(
2485 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2486 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002487
Alex Klein1699fab2022-09-08 08:46:06 -06002488 def newerVersionOutcome(self, ebuild_path):
2489 return uprev_lib.UprevResult(
2490 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2491 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002492
Alex Klein1699fab2022-09-08 08:46:06 -06002493 def sameVersionOutcome(self, ebuild_path):
2494 return uprev_lib.UprevResult(
2495 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2496 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002497
Alex Klein1699fab2022-09-08 08:46:06 -06002498 def newEbuildCreatedOutcome(self, ebuild_path):
2499 return uprev_lib.UprevResult(
2500 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2501 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002502
Alex Klein1699fab2022-09-08 08:46:06 -06002503 def test_lacros_uprev_fails(self):
2504 """Test a lacros package uprev with no triggers"""
2505 self.PatchObject(
2506 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2507 )
Alex Klein314fb5d2022-10-24 14:56:31 -06002508 with self.assertRaises(uprev_lib.NoRefsError):
Alex Klein1699fab2022-09-08 08:46:06 -06002509 packages.uprev_lacros_in_parallel(None, [], None)
Julio Hurtado870ed322021-12-03 18:22:40 +00002510
Alex Klein1699fab2022-09-08 08:46:06 -06002511 def test_lacros_uprev_revision_bump(self):
2512 """Test lacros package uprev."""
2513 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2514 self.PatchObject(
2515 uprev_lib,
2516 "uprev_workon_ebuild_to_version",
2517 side_effect=[lacros_outcome],
2518 )
2519 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2520 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002521
Alex Klein1699fab2022-09-08 08:46:06 -06002522 def test_lacros_uprev_version_bump(self):
2523 """Test lacros package uprev."""
2524 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2525 self.PatchObject(
2526 uprev_lib,
2527 "uprev_workon_ebuild_to_version",
2528 side_effect=[lacros_outcome],
2529 )
2530 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2531 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002532
Alex Klein1699fab2022-09-08 08:46:06 -06002533 def test_lacros_uprev_new_ebuild_created(self):
2534 """Test lacros package uprev."""
2535 lacros_outcome = self.newEbuildCreatedOutcome(
2536 self.MOCK_LACROS_EBUILD_PATH
2537 )
2538 self.PatchObject(
2539 uprev_lib,
2540 "uprev_workon_ebuild_to_version",
2541 side_effect=[lacros_outcome],
2542 )
2543 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2544 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002545
Alex Klein1699fab2022-09-08 08:46:06 -06002546 def test_lacros_uprev_newer_version_exist(self):
2547 """Test the newer version exists uprev should not happen."""
2548 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2549 self.PatchObject(
2550 uprev_lib,
2551 "uprev_workon_ebuild_to_version",
2552 side_effect=[lacros_outcome],
2553 )
2554 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2555 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002556
Alex Klein1699fab2022-09-08 08:46:06 -06002557 def test_lacros_uprev_same_version_exist(self):
2558 """Test the same version exists uprev should not happen."""
2559 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2560 self.PatchObject(
2561 uprev_lib,
2562 "uprev_workon_ebuild_to_version",
2563 side_effect=[lacros_outcome],
2564 )
2565 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2566 self.assertFalse(output.uprevved)