blob: 95e076245bc0a83df6194fe368ac867217540378 [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(
Mike Frysinger043f6052023-10-04 00:00:26 -0400930 "cat/pkg-1.2.3",
931 board=None,
932 extra_env={"USE": "useflag"},
933 depgraph_root_packages=None,
Alex Klein1699fab2022-09-08 08:46:06 -0600934 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700935
Alex Klein1699fab2022-09-08 08:46:06 -0600936 def test_env_use_flags(self):
937 """Test env use flags get propagated correctly with passed useflags."""
938 # We don't really care about the result, just the env handling.
939 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
940 # Add some flags to the environment.
941 existing_flags = "already set flags"
942 self.PatchObject(os.environ, "get", return_value=existing_flags)
Alex Klein149fd3b2019-12-16 16:01:05 -0700943
Alex Klein1699fab2022-09-08 08:46:06 -0600944 new_flags = "useflag"
945 packages.has_prebuilt("cat/pkg-1.2.3", useflags=new_flags)
946 expected = "%s %s" % (existing_flags, new_flags)
947 patch.assert_called_with(
Mike Frysinger043f6052023-10-04 00:00:26 -0400948 "cat/pkg-1.2.3",
949 board=None,
950 extra_env={"USE": expected},
951 depgraph_root_packages=None,
952 )
953
954 def test_depgraph_root_packages(self):
955 """Test root depgraph packages."""
956 # We don't really care about the result, just the package handling.
957 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
958 os.unsetenv("USE")
959
960 depgraph_pkgs = ["virtual/target-os"]
961 packages.has_prebuilt(
962 "cat/pkg-1.2.3", depgraph_root_packages=depgraph_pkgs
963 )
964 patch.assert_called_with(
965 "cat/pkg-1.2.3",
966 board=None,
967 extra_env=mock.ANY,
968 depgraph_root_packages=depgraph_pkgs,
Alex Klein1699fab2022-09-08 08:46:06 -0600969 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700970
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600971
972class AndroidVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600973 """Tests getting android versions."""
Michael Mortensen14960d02019-10-18 07:53:59 -0600974
Alex Klein1699fab2022-09-08 08:46:06 -0600975 def setUp(self):
976 package_result = [
977 "chromeos-base/android-container-nyc-4717008-r1",
978 "chromeos-base/update_engine-0.0.3-r3408",
979 ]
980 self.PatchObject(
981 portage_util, "GetPackageDependencies", return_value=package_result
982 )
983 self.board = "board"
984 self.PatchObject(
985 portage_util,
986 "FindEbuildForBoardPackage",
987 return_value="chromeos-base/android-container-nyc",
988 )
989 FakeEnvironment = {
990 "ARM_TARGET": "3-linux-target",
991 }
992 self.PatchObject(
993 osutils, "SourceEnvironment", return_value=FakeEnvironment
994 )
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600995
Alex Kleinfee86da2023-01-20 18:40:06 -0700996 # Clear the LRU cache for the function. We mock the function that
997 # provides the data this function processes to produce its result, so we
998 # need to clear it manually.
Alex Klein1699fab2022-09-08 08:46:06 -0600999 packages.determine_android_package.cache_clear()
Alex Klein68a28712021-11-08 11:08:30 -07001000
Alex Klein1699fab2022-09-08 08:46:06 -06001001 def test_determine_android_version(self):
1002 """Tests that a valid android version is returned."""
1003 version = packages.determine_android_version(self.board)
1004 self.assertEqual(version, "4717008")
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001005
Alex Klein1699fab2022-09-08 08:46:06 -06001006 def test_determine_android_version_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001007 """Test None is returned for version when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -06001008 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
1009 self.PatchObject(
1010 portage_util, "GetPackageDependencies", return_value=package_result
1011 )
1012 version = packages.determine_android_version(self.board)
1013 self.assertEqual(version, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001014
Alex Klein1699fab2022-09-08 08:46:06 -06001015 def test_determine_android_branch(self):
1016 """Tests that a valid android branch is returned."""
1017 branch = packages.determine_android_branch(self.board)
1018 self.assertEqual(branch, "3")
Michael Mortensenb70e8a82019-10-10 18:43:41 -06001019
Alex Klein1699fab2022-09-08 08:46:06 -06001020 def test_determine_android_branch_64bit_targets(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001021 """Tests a valid android branch is returned with only 64bit targets."""
Alex Klein1699fab2022-09-08 08:46:06 -06001022 self.PatchObject(
1023 osutils,
1024 "SourceEnvironment",
1025 return_value={"ARM64_TARGET": "3-linux-target"},
1026 )
1027 branch = packages.determine_android_branch(self.board)
1028 self.assertEqual(branch, "3")
Federico 'Morg' Pareschicd9165a2020-05-29 09:45:55 +09001029
Alex Klein1699fab2022-09-08 08:46:06 -06001030 def test_determine_android_branch_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001031 """Tests a None is returned for branch when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -06001032 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
1033 self.PatchObject(
1034 portage_util, "GetPackageDependencies", return_value=package_result
1035 )
1036 branch = packages.determine_android_branch(self.board)
1037 self.assertEqual(branch, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001038
Alex Klein1699fab2022-09-08 08:46:06 -06001039 def test_determine_android_target(self):
1040 """Tests that a valid android target is returned."""
1041 target = packages.determine_android_target(self.board)
1042 self.assertEqual(target, "cheets")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001043
Alex Klein1699fab2022-09-08 08:46:06 -06001044 def test_determine_android_target_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001045 """Tests a None is returned for target when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -06001046 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
1047 self.PatchObject(
1048 portage_util, "GetPackageDependencies", return_value=package_result
1049 )
1050 target = packages.determine_android_target(self.board)
1051 self.assertEqual(target, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001052
Alex Klein1699fab2022-09-08 08:46:06 -06001053 def test_determine_android_version_handle_exception(self):
1054 """Tests handling RunCommandError inside determine_android_version."""
Alex Kleinfee86da2023-01-20 18:40:06 -07001055 # Mock what happens when portage returns that bubbles up (via
1056 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001057 self.PatchObject(
1058 portage_util,
1059 "GetPackageDependencies",
1060 side_effect=cros_build_lib.RunCommandError("error"),
1061 )
1062 target = packages.determine_android_version(self.board)
1063 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -06001064
Alex Klein1699fab2022-09-08 08:46:06 -06001065 def test_determine_android_package_handle_exception(self):
1066 """Tests handling RunCommandError inside determine_android_package."""
Alex Kleinfee86da2023-01-20 18:40:06 -07001067 # Mock what happens when portage returns that bubbles up (via
1068 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001069 self.PatchObject(
1070 portage_util,
1071 "GetPackageDependencies",
1072 side_effect=cros_build_lib.RunCommandError("error"),
1073 )
1074 target = packages.determine_android_package(self.board)
1075 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -06001076
Alex Klein1699fab2022-09-08 08:46:06 -06001077 def test_determine_android_package_callers_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001078 """Tests RunCommandError caught by determine_android_package callers."""
1079 # Mock what happens when portage returns that bubbles up (via
1080 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001081 self.PatchObject(
1082 portage_util,
1083 "GetPackageDependencies",
1084 side_effect=cros_build_lib.RunCommandError("error"),
1085 )
1086 # Verify that target is None, as expected.
1087 target = packages.determine_android_package(self.board)
1088 self.assertEqual(target, None)
1089 # determine_android_branch calls determine_android_package
1090 branch = packages.determine_android_branch(self.board)
1091 self.assertEqual(branch, None)
1092 # determine_android_target calls determine_android_package
1093 target = packages.determine_android_target(self.board)
1094 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001095
Michael Mortensene0f4b542019-10-24 15:30:23 -06001096
Alex Klein1699fab2022-09-08 08:46:06 -06001097@pytest.mark.usefixtures("testcase_caplog", "testcase_monkeypatch")
Michael Mortensende716a12020-05-15 11:27:00 -06001098class FindFingerprintsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001099 """Tests for find_fingerprints."""
Michael Mortensende716a12020-05-15 11:27:00 -06001100
Alex Klein1699fab2022-09-08 08:46:06 -06001101 def setUp(self):
1102 self.board = "test-board"
1103 # Create cheets-fingerprints.txt based on tempdir/src...
1104 self.fingerprint_contents = (
1105 "google/test-board/test-board_cheets"
1106 ":9/R99-12345.0.9999/123456:user/release-keys"
1107 )
1108 fingerprint_path = os.path.join(
1109 self.tempdir,
1110 "src/build/images/test-board/latest/cheets-fingerprint.txt",
1111 )
Alex Klein220e3a72023-09-14 17:12:13 -06001112 self.chroot = chroot_lib.Chroot(
Brian Norris4f251e42023-03-09 15:53:26 -08001113 path=self.tempdir / "chroot", out_path=self.tempdir / "out"
1114 )
Alex Klein1699fab2022-09-08 08:46:06 -06001115 osutils.WriteFile(
1116 fingerprint_path, self.fingerprint_contents, makedirs=True
1117 )
Michael Mortensende716a12020-05-15 11:27:00 -06001118
Alex Klein1699fab2022-09-08 08:46:06 -06001119 def test_find_fingerprints_with_test_path(self):
1120 """Tests get_firmware_versions with mocked output."""
1121 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1122 build_target = build_target_lib.BuildTarget(self.board)
1123 result = packages.find_fingerprints(build_target)
1124 self.assertEqual(result, [self.fingerprint_contents])
1125 self.assertIn("Reading fingerprint file", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001126
Alex Klein1699fab2022-09-08 08:46:06 -06001127 def test_find_fingerprints(self):
1128 """Tests get_firmware_versions with mocked output."""
1129 # Use board name whose path for fingerprint file does not exist.
1130 # Verify that fingerprint file is not found and None is returned.
1131 build_target = build_target_lib.BuildTarget("wrong-boardname")
1132 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1133 result = packages.find_fingerprints(build_target)
1134 self.assertEqual(result, [])
1135 self.assertIn("Fingerprint file not found", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001136
1137
Michael Mortensen59e30872020-05-18 14:12:49 -06001138class GetAllFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001139 """Tests for get_firmware_versions."""
Michael Mortensen59e30872020-05-18 14:12:49 -06001140
Alex Klein1699fab2022-09-08 08:46:06 -06001141 def setUp(self):
1142 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001143 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001144 self.rc.SetDefaultCmdResult(
1145 stdout="""
Michael Mortensen59e30872020-05-18 14:12:49 -06001146
1147flashrom(8): 68935ee2fcfcffa47af81b966269cd2b */build/reef/usr/sbin/flashrom
1148 ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=e102cc98d45300b50088999d53775acbeff407dc, stripped
1149 0.9.9 : bbb2d6a : Jul 28 2017 15:12:34 UTC
1150
1151Model: reef
1152BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1153BIOS version: Google_Reef.9042.87.1
1154BIOS (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
1155BIOS (RW) version: Google_Reef.9042.110.0
1156EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1157EC version: reef_v1.1.5900-ab1ee51
1158EC (RW) version: reef_v1.1.5909-bd1f0c9
1159
1160Model: pyro
1161BIOS image: 9e62447ebf22a724a4a835018ab6234e */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/image.bin
1162BIOS version: Google_Pyro.9042.87.1
1163BIOS (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
1164BIOS (RW) version: Google_Pyro.9042.110.0
1165EC image: 44b93ed591733519e752e05aa0529eb5 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/ec.bin
1166EC version: pyro_v1.1.5900-ab1ee51
1167EC (RW) version: pyro_v1.1.5909-bd1f0c9
1168
1169Model: snappy
1170BIOS image: 3ab63ff080596bd7de4e7619f003bb64 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/image.bin
1171BIOS version: Google_Snappy.9042.110.0
1172EC image: c4db159e84428391d2ee25368c5fe5b6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/ec.bin
1173EC version: snappy_v1.1.5909-bd1f0c9
1174
1175Model: sand
1176BIOS image: 387da034a4f0a3f53e278ebfdcc2a412 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/image.bin
1177BIOS version: Google_Sand.9042.110.0
1178EC image: 411562e0589dacec131f5fdfbe95a561 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/ec.bin
1179EC version: sand_v1.1.5909-bd1f0c9
1180
1181Model: electro
1182BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1183BIOS version: Google_Reef.9042.87.1
1184BIOS (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
1185BIOS (RW) version: Google_Reef.9042.110.0
1186EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1187EC version: reef_v1.1.5900-ab1ee51
1188EC (RW) version: reef_v1.1.5909-bd1f0c9
1189
1190Package Content:
1191612e7bb6ed1fb0a05abf2ebdc834c18b *./updater4.sh
11920eafbee07282315829d0f42135ec7c0c *./gbb_utility
11936074e3ca424cb30a67c378c1d9681f9c *./mosys
119468935ee2fcfcffa47af81b966269cd2b *./flashrom
11950eafbee07282315829d0f42135ec7c0c *./dump_fmap
1196490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
119760899148600b8673ddb711faa55aee40 *./common.sh
11983c3a99346d1ca1273cbcd86c104851ff *./shflags
1199de7ce035e1f82a89f8909d888ee402c0 *./crosutil.sh
1200f9334372bdb9036ba09a6fd9bf30e7a2 *./crossystem
120122257a8d5f0adc1f50a1916c3a4a35dd *./models/reef/ec.bin
1202faf12dbb7cdaf21ce153bdffb67841fd *./models/reef/bios.bin
1203c9bbb417b7921b85a7ed999ee42f550e *./models/reef/setvars.sh
120429823d46f1ec1491ecacd7b830fd2686 *./models/pyro/ec.bin
12052320463aba8b22eb5ea836f094d281b3 *./models/pyro/bios.bin
120681614833ad77c9cd093360ba7bea76b8 *./models/pyro/setvars.sh
1207411562e0589dacec131f5fdfbe95a561 *./models/sand/ec.bin
1208387da034a4f0a3f53e278ebfdcc2a412 *./models/sand/bios.bin
1209fcd8cb0ac0e2ed6be220aaae435d43ff *./models/sand/setvars.sh
1210c4db159e84428391d2ee25368c5fe5b6 *./models/snappy/ec.bin
12113ab63ff080596bd7de4e7619f003bb64 *./models/snappy/bios.bin
1212fe5d699f2e9e4a7de031497953313dbd *./models/snappy/setvars.sh
121379aabd7cd8a215a54234c53d7bb2e6fb *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001214"""
1215 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001216 # pylint: enable=line-too-long
Michael Mortensen59e30872020-05-18 14:12:49 -06001217
Alex Klein1699fab2022-09-08 08:46:06 -06001218 def test_get_firmware_versions(self):
1219 """Tests get_firmware_versions with mocked output."""
1220 build_target = build_target_lib.BuildTarget(self.board)
1221 result = packages.get_all_firmware_versions(build_target)
1222 self.assertEqual(len(result), 5)
1223 self.assertEqual(
1224 result["reef"],
1225 packages.FirmwareVersions(
1226 "reef",
1227 "Google_Reef.9042.87.1",
1228 "Google_Reef.9042.110.0",
1229 "reef_v1.1.5900-ab1ee51",
1230 "reef_v1.1.5909-bd1f0c9",
1231 ),
1232 )
1233 self.assertEqual(
1234 result["pyro"],
1235 packages.FirmwareVersions(
1236 "pyro",
1237 "Google_Pyro.9042.87.1",
1238 "Google_Pyro.9042.110.0",
1239 "pyro_v1.1.5900-ab1ee51",
1240 "pyro_v1.1.5909-bd1f0c9",
1241 ),
1242 )
1243 self.assertEqual(
1244 result["snappy"],
1245 packages.FirmwareVersions(
1246 "snappy",
1247 "Google_Snappy.9042.110.0",
1248 None,
1249 "snappy_v1.1.5909-bd1f0c9",
1250 None,
1251 ),
1252 )
1253 self.assertEqual(
1254 result["sand"],
1255 packages.FirmwareVersions(
1256 "sand",
1257 "Google_Sand.9042.110.0",
1258 None,
1259 "sand_v1.1.5909-bd1f0c9",
1260 None,
1261 ),
1262 )
1263 self.assertEqual(
1264 result["electro"],
1265 packages.FirmwareVersions(
1266 "electro",
1267 "Google_Reef.9042.87.1",
1268 "Google_Reef.9042.110.0",
1269 "reef_v1.1.5900-ab1ee51",
1270 "reef_v1.1.5909-bd1f0c9",
1271 ),
1272 )
Michael Mortensen59e30872020-05-18 14:12:49 -06001273
Alex Klein1699fab2022-09-08 08:46:06 -06001274 def test_get_firmware_versions_error(self):
1275 """Tests get_firmware_versions with no output."""
1276 # Throw an exception when running the command.
Mike Frysinger5d85a542023-07-06 16:05:54 -04001277 self.rc.SetDefaultCmdResult(returncode=1)
Alex Klein1699fab2022-09-08 08:46:06 -06001278 build_target = build_target_lib.BuildTarget(self.board)
1279 result = packages.get_all_firmware_versions(build_target)
1280 self.assertEqual(result, {})
Benjamin Shai12c767e2022-01-12 15:17:44 +00001281
Michael Mortensen59e30872020-05-18 14:12:49 -06001282
Michael Mortensen71ef5682020-05-07 14:29:24 -06001283class GetFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001284 """Tests for get_firmware_versions."""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001285
Alex Klein1699fab2022-09-08 08:46:06 -06001286 def setUp(self):
1287 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001288 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001289 self.rc.SetDefaultCmdResult(
1290 stdout="""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001291
1292flashrom(8): a8f99c2e61e7dc09c4b25ef5a76ef692 */build/kevin/usr/sbin/flashrom
1293 ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.d
1294 0.9.4 : 860875a : Apr 10 2017 23:54:29 UTC
1295
1296BIOS image: 6b5b855a0b8fd1657546d1402c15b206 *chromeos-firmware-kevin-0.0.1/.dist/kevin_fw_8785.178.0.n
1297BIOS version: Google_Kevin.8785.178.0
1298EC image: 1ebfa9518e6cac0558a80b7ab2f5b489 *chromeos-firmware-kevin-0.0.1/.dist/kevin_ec_8785.178.0.n
1299EC version:kevin_v1.10.184-459421c
1300
1301Package Content:
1302a8f99c2e61e7dc09c4b25ef5a76ef692 *./flashrom
13033c3a99346d1ca1273cbcd86c104851ff *./shflags
1304457a8dc8546764affc9700f8da328d23 *./dump_fmap
1305c392980ddb542639edf44a965a59361a *./updater5.sh
1306490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
13076b5b855a0b8fd1657546d1402c15b206 *./bios.bin
13087b5bef0d2da90c23ff2e157250edf0fa *./crosutil.sh
1309d78722e4f1a0dc2d8c3d6b0bc7010ae3 *./crossystem
1310457a8dc8546764affc9700f8da328d23 *./gbb_utility
13111ebfa9518e6cac0558a80b7ab2f5b489 *./ec.bin
1312c98ca54db130886142ad582a58e90ddc *./common.sh
13135ba978bdec0f696f47f0f0de90936880 *./mosys
1314312e8ee6122057f2a246d7bcf1572f49 *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001315"""
1316 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001317 # pylint: enable=line-too-long
Michael Mortensen71ef5682020-05-07 14:29:24 -06001318
Alex Klein1699fab2022-09-08 08:46:06 -06001319 def test_get_firmware_versions(self):
1320 """Tests get_firmware_versions with mocked output."""
1321 build_target = build_target_lib.BuildTarget(self.board)
1322 result = packages.get_firmware_versions(build_target)
1323 versions = packages.FirmwareVersions(
1324 None,
1325 "Google_Kevin.8785.178.0",
1326 None,
1327 "kevin_v1.10.184-459421c",
1328 None,
1329 )
1330 self.assertEqual(result, versions)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001331
1332
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001333class DetermineKernelVersionTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001334 """Tests for determine_kernel_version."""
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001335
Alex Klein1699fab2022-09-08 08:46:06 -06001336 def setUp(self):
1337 self.board = "test-board"
1338 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001339
Alex Klein1699fab2022-09-08 08:46:06 -06001340 def test_determine_kernel_version(self):
1341 """Tests that a valid kernel version is returned."""
Lizzy Presland0b978e62022-09-09 16:55:29 +00001342 kernel_candidates = [
1343 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
Alex Klein1699fab2022-09-08 08:46:06 -06001344 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001345 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1346 "sys-kernel/upstream-kernel-next-9999",
1347 "sys-kernel/socfpga-kernel-4.20-r34",
Alex Klein1699fab2022-09-08 08:46:06 -06001348 ]
1349 self.PatchObject(
Lizzy Presland0b978e62022-09-09 16:55:29 +00001350 portage_util,
1351 "GetFlattenedDepsForPackage",
1352 return_value=kernel_candidates,
1353 )
1354
1355 installed_pkgs = [
1356 "sys-kernel/linux-firmware-0.0.1-r594",
1357 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1358 "virtual/linux-sources-1-r30",
1359 ]
1360 self.PatchObject(
1361 portage_util,
1362 "GetPackageDependencies",
1363 return_value=installed_pkgs,
Alex Klein1699fab2022-09-08 08:46:06 -06001364 )
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001365
Alex Klein1699fab2022-09-08 08:46:06 -06001366 result = packages.determine_kernel_version(self.build_target)
1367 self.assertEqual(result, "4.4.223-r2209")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001368
Lizzy Presland0b978e62022-09-09 16:55:29 +00001369 def test_determine_kernel_version_ignores_exact_duplicates(self):
1370 """Tests that multiple results for candidates is ignored."""
1371 # Depgraph is evaluated for version as well as revision, so graph will
1372 # return all results twice.
1373 kernel_candidates = [
1374 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1375 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1376 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1377 "sys-kernel/upstream-kernel-next-9999",
1378 "sys-kernel/socfpga-kernel-4.20-r34",
1379 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1380 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1381 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1382 "sys-kernel/upstream-kernel-next-9999",
1383 "sys-kernel/socfpga-kernel-4.20-r34",
1384 ]
1385 self.PatchObject(
1386 portage_util,
1387 "GetFlattenedDepsForPackage",
1388 return_value=kernel_candidates,
1389 )
1390
1391 installed_pkgs = [
1392 "sys-kernel/linux-firmware-0.0.1-r594",
1393 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1394 "virtual/linux-sources-1-r30",
1395 ]
Alex Klein1699fab2022-09-08 08:46:06 -06001396 self.PatchObject(
1397 portage_util,
1398 "GetPackageDependencies",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001399 return_value=installed_pkgs,
1400 )
1401
1402 result = packages.determine_kernel_version(self.build_target)
1403 self.assertEqual(result, "4.4.223-r2209")
1404
1405 def test_determine_kernel_version_ignores_virtual_package(self):
1406 """Tests that top-level package is ignored as potential kernel pkg."""
1407 # Depgraph results include the named package at level 0 as well as its
1408 # first-order dependencies, so verify that the virtual package is not
1409 # included as a kernel package.
1410 kernel_candidates = [
1411 "virtual/linux-sources-1",
1412 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1413 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1414 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1415 "sys-kernel/upstream-kernel-next-9999",
1416 "sys-kernel/socfpga-kernel-4.20-r34",
1417 "virtual/linux-sources-1-r30",
1418 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1419 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1420 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1421 "sys-kernel/upstream-kernel-next-9999",
1422 "sys-kernel/socfpga-kernel-4.20-r34",
1423 ]
1424 self.PatchObject(
1425 portage_util,
1426 "GetFlattenedDepsForPackage",
1427 return_value=kernel_candidates,
1428 )
1429
1430 installed_pkgs = [
1431 "sys-kernel/linux-firmware-0.0.1-r594",
1432 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1433 "virtual/linux-sources-1-r30",
1434 ]
1435 self.PatchObject(
1436 portage_util,
1437 "GetPackageDependencies",
1438 return_value=installed_pkgs,
1439 )
1440
1441 result = packages.determine_kernel_version(self.build_target)
1442 self.assertEqual(result, "4.4.223-r2209")
1443
1444 def test_determine_kernel_version_too_many(self):
1445 """Tests that an exception is thrown with too many matching packages."""
1446 package_result = [
1447 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1448 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1449 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1450 "sys-kernel/upstream-kernel-next-9999",
1451 "sys-kernel/socfpga-kernel-4.20-r34",
1452 ]
1453 self.PatchObject(
1454 portage_util,
1455 "GetFlattenedDepsForPackage",
1456 return_value=package_result,
1457 )
1458
1459 installed_pkgs = [
1460 "sys-kernel/linux-firmware-0.0.1-r594",
1461 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1462 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1463 "virtual/linux-sources-1-r30",
1464 ]
1465 self.PatchObject(
1466 portage_util,
1467 "GetPackageDependencies",
1468 return_value=installed_pkgs,
1469 )
1470
1471 with self.assertRaises(packages.KernelVersionError):
1472 packages.determine_kernel_version(self.build_target)
1473
1474 def test_determine_kernel_version_no_kernel_match(self):
1475 """Tests that an exception is thrown with 0-sized intersection."""
1476 package_result = [
1477 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1478 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1479 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1480 "sys-kernel/upstream-kernel-next-9999",
1481 ]
1482 self.PatchObject(
1483 portage_util,
1484 "GetFlattenedDepsForPackage",
1485 return_value=package_result,
1486 )
1487
1488 installed_pkgs = [
1489 "sys-kernel/linux-firmware-0.0.1-r594",
1490 "sys-kernel/socfpga-kernel-4.20-r34",
1491 "virtual/linux-sources-1-r30",
1492 ]
1493 self.PatchObject(
1494 portage_util,
1495 "GetPackageDependencies",
1496 return_value=installed_pkgs,
1497 )
1498
1499 with self.assertRaises(packages.KernelVersionError):
1500 packages.determine_kernel_version(self.build_target)
1501
1502 def test_determine_kernel_version_exception(self):
1503 """Tests that portage_util exceptions result in returning empty str."""
1504 self.PatchObject(
1505 portage_util,
1506 "GetFlattenedDepsForPackage",
Alex Klein1699fab2022-09-08 08:46:06 -06001507 side_effect=cros_build_lib.RunCommandError("error"),
1508 )
1509 result = packages.determine_kernel_version(self.build_target)
Lizzy Presland0b978e62022-09-09 16:55:29 +00001510 self.assertEqual(result, "")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001511
Alex Klein627e04c2021-11-10 15:56:47 -07001512
Michael Mortensenc2615b72019-10-15 08:12:24 -06001513class ChromeVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001514 """Tests getting chrome version."""
Michael Mortensen14960d02019-10-18 07:53:59 -06001515
Alex Klein1699fab2022-09-08 08:46:06 -06001516 def setUp(self):
1517 self.build_target = build_target_lib.BuildTarget("board")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001518
Alex Klein1699fab2022-09-08 08:46:06 -06001519 def test_determine_chrome_version(self):
1520 """Tests that a valid chrome version is returned."""
1521 # Mock PortageqBestVisible to return a valid chrome version string.
1522 r1_cpf = "chromeos-base/chromeos-chrome-78.0.3900.0_rc-r1"
1523 r1_cpv = package_info.SplitCPV(r1_cpf)
1524 self.PatchObject(
1525 portage_util, "PortageqBestVisible", return_value=r1_cpv
1526 )
Michael Mortensenc2615b72019-10-15 08:12:24 -06001527
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001528 chrome_version = packages.determine_package_version(
1529 constants.CHROME_CP, self.build_target
1530 )
Alex Klein1699fab2022-09-08 08:46:06 -06001531 version_numbers = chrome_version.split(".")
1532 self.assertEqual(len(version_numbers), 4)
1533 self.assertEqual(int(version_numbers[0]), 78)
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001534
Alex Klein1699fab2022-09-08 08:46:06 -06001535 def test_determine_chrome_version_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001536 # Mock what happens when portage throws an exception that bubbles up
1537 # (via RunCommand)inside portage_util.PortageqBestVisible.
Alex Klein1699fab2022-09-08 08:46:06 -06001538 self.PatchObject(
1539 portage_util,
1540 "PortageqBestVisible",
1541 side_effect=cros_build_lib.RunCommandError("error"),
1542 )
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001543 target = packages.determine_package_version(
1544 constants.CHROME_CP, self.build_target
1545 )
Alex Klein1699fab2022-09-08 08:46:06 -06001546 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001547
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001548
1549class PlatformVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001550 """Tests getting platform version."""
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001551
Alex Klein1699fab2022-09-08 08:46:06 -06001552 def test_determine_platform_version(self):
1553 """Test checking that a valid platform version is returned."""
1554 platform_version = packages.determine_platform_version()
1555 # The returned platform version is something like 12603.0.0.
1556 version_string_list = platform_version.split(".")
1557 self.assertEqual(len(version_string_list), 3)
Alex Kleinfee86da2023-01-20 18:40:06 -07001558 # We don't want to check an exact version, but the first number should
1559 # be non-zero.
Alex Klein1699fab2022-09-08 08:46:06 -06001560 self.assertGreaterEqual(int(version_string_list[0]), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001561
Alex Klein1699fab2022-09-08 08:46:06 -06001562 def test_determine_milestone_version(self):
1563 """Test checking that a valid milestone version is returned."""
1564 milestone_version = packages.determine_milestone_version()
1565 # Milestone version should be non-zero
1566 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001567
Alex Klein1699fab2022-09-08 08:46:06 -06001568 def test_determine_full_version(self):
1569 """Test checking that a valid full version is returned."""
1570 full_version = packages.determine_full_version()
1571 pattern = r"^R(\d+)-(\d+.\d+.\d+(-rc\d+)*)"
1572 m = re.match(pattern, full_version)
1573 self.assertTrue(m)
1574 milestone_version = m.group(1)
1575 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001576
Alex Klein1699fab2022-09-08 08:46:06 -06001577 def test_versions_based_on_mock(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001578 # Create a test version_info object, and then mock VersionInfo.from_repo
Alex Klein1699fab2022-09-08 08:46:06 -06001579 # return it.
1580 test_platform_version = "12575.0.0"
1581 test_chrome_branch = "75"
1582 version_info_mock = chromeos_version.VersionInfo(test_platform_version)
1583 version_info_mock.chrome_branch = test_chrome_branch
1584 self.PatchObject(
1585 chromeos_version.VersionInfo,
1586 "from_repo",
1587 return_value=version_info_mock,
1588 )
1589 test_full_version = (
1590 "R" + test_chrome_branch + "-" + test_platform_version
1591 )
1592 platform_version = packages.determine_platform_version()
1593 milestone_version = packages.determine_milestone_version()
1594 full_version = packages.determine_full_version()
1595 self.assertEqual(platform_version, test_platform_version)
1596 self.assertEqual(milestone_version, test_chrome_branch)
1597 self.assertEqual(full_version, test_full_version)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001598
1599
1600# Each of the columns in the following table is a separate dimension along
1601# which Chrome uprev test cases can vary in behavior. The full test space would
1602# be the Cartesian product of the possible values of each column.
1603# 'CHROME_EBUILD' refers to the relationship between the version of the existing
1604# Chrome ebuild vs. the requested uprev version. 'FOLLOWER_EBUILDS' refers to
1605# the same relationship but for the packages defined in OTHER_CHROME_PACKAGES.
1606# 'EBUILDS MODIFIED' refers to whether any of the existing 9999 ebuilds have
1607# modified contents relative to their corresponding stable ebuilds.
1608#
1609# CHROME_EBUILD FOLLOWER_EBUILDS EBUILDS_MODIFIED
1610#
1611# HIGHER HIGHER YES
1612# SAME SAME NO
1613# LOWER LOWER
1614# DOESN'T EXIST YET
1615
1616# These test cases cover both CHROME & FOLLOWER ebuilds being identically
1617# higher, lower, or the same versions, with no modified ebuilds.
1618UPREV_VERSION_CASES = (
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001619 # Uprev.
Chris McDonaldea0312c2020-05-04 23:33:15 -06001620 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001621 "80.0.8080.0",
1622 "81.0.8181.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001623 # One added and one deleted for chrome and each "other" package.
1624 2 * (1 + len(constants.OTHER_CHROME_PACKAGES)),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001625 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001626 id="newer_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001627 ),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001628 # Revbump.
1629 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001630 "80.0.8080.0",
1631 "80.0.8080.0",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001632 2,
1633 True,
Alex Klein1699fab2022-09-08 08:46:06 -06001634 id="chrome_revbump",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001635 ),
Chris McDonaldea0312c2020-05-04 23:33:15 -06001636 # No files should be changed in these cases.
1637 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001638 "80.0.8080.0",
1639 "80.0.8080.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001640 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001641 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001642 id="same_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001643 ),
1644 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001645 "80.0.8080.0",
1646 "79.0.7979.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001647 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001648 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001649 id="older_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001650 ),
1651)
1652
1653
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001654@pytest.mark.parametrize(
Alex Klein1699fab2022-09-08 08:46:06 -06001655 "old_version, new_version, expected_count, modify_unstable",
1656 UPREV_VERSION_CASES,
1657)
1658def test_uprev_chrome_all_files_already_exist(
1659 old_version,
1660 new_version,
1661 expected_count,
1662 modify_unstable,
1663 monkeypatch,
1664 overlay_stack,
1665):
1666 """Test Chrome uprevs work as expected when all packages already exist."""
1667 (overlay,) = overlay_stack(1)
1668 monkeypatch.setattr(uprev_lib, "_CHROME_OVERLAY_PATH", overlay.path)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001669
Alex Klein1699fab2022-09-08 08:46:06 -06001670 unstable_chrome = cr.test.Package(
1671 "chromeos-base", "chromeos-chrome", version="9999", keywords="~*"
1672 )
1673 if modify_unstable:
1674 # Add some field not set in stable.
1675 unstable_chrome.depend = "foo/bar"
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001676
Alex Klein1699fab2022-09-08 08:46:06 -06001677 stable_chrome = cr.test.Package(
1678 "chromeos-base", "chromeos-chrome", version=f"{old_version}_rc-r1"
1679 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001680
Alex Klein1699fab2022-09-08 08:46:06 -06001681 overlay.add_package(unstable_chrome)
1682 overlay.add_package(stable_chrome)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001683
Alex Klein1699fab2022-09-08 08:46:06 -06001684 for pkg_str in constants.OTHER_CHROME_PACKAGES:
1685 category, pkg_name = pkg_str.split("/")
1686 unstable_pkg = cr.test.Package(
1687 category, pkg_name, version="9999", keywords="~*"
1688 )
1689 stable_pkg = cr.test.Package(
1690 category, pkg_name, version=f"{old_version}_rc-r1"
1691 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001692
Alex Klein1699fab2022-09-08 08:46:06 -06001693 overlay.add_package(unstable_pkg)
1694 overlay.add_package(stable_pkg)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001695
Alex Klein1699fab2022-09-08 08:46:06 -06001696 git_refs = [
Alex Klein220e3a72023-09-14 17:12:13 -06001697 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -06001698 path="/foo", ref=f"refs/tags/{new_version}", revision="stubcommit"
1699 )
1700 ]
1701 res = packages.uprev_chrome_from_ref(None, git_refs, None)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001702
Alex Klein1699fab2022-09-08 08:46:06 -06001703 modified_file_count = sum(len(m.files) for m in res.modified)
1704 assert modified_file_count == expected_count
Michael Mortensen125bb012020-05-21 14:02:10 -06001705
1706
Alex Klein1699fab2022-09-08 08:46:06 -06001707@pytest.mark.usefixtures("testcase_monkeypatch")
Michael Mortensen125bb012020-05-21 14:02:10 -06001708class GetModelsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001709 """Tests for get_models."""
Michael Mortensen125bb012020-05-21 14:02:10 -06001710
Alex Klein1699fab2022-09-08 08:46:06 -06001711 def setUp(self):
1712 self.board = "test-board"
1713 self.rc.SetDefaultCmdResult(stdout="pyro\nreef\nsnappy\n")
1714 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1715 build_bin = os.path.join(
1716 self.tempdir, constants.DEFAULT_CHROOT_DIR, "usr", "bin"
1717 )
1718 osutils.Touch(
1719 os.path.join(build_bin, "cros_config_host"), makedirs=True
1720 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001721
Alex Klein1699fab2022-09-08 08:46:06 -06001722 def testGetModels(self):
1723 """Test get_models."""
1724 build_target = build_target_lib.BuildTarget(self.board)
1725 result = packages.get_models(build_target)
1726 self.assertEqual(result, ["pyro", "reef", "snappy"])
Michael Mortensen359c1f32020-05-28 19:35:42 -06001727
1728
1729class GetKeyIdTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001730 """Tests for get_key_id."""
Michael Mortensen359c1f32020-05-28 19:35:42 -06001731
Alex Klein1699fab2022-09-08 08:46:06 -06001732 def setUp(self):
1733 self.board = "test-board"
1734 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensen359c1f32020-05-28 19:35:42 -06001735
Alex Klein1699fab2022-09-08 08:46:06 -06001736 def testGetKeyId(self):
1737 """Test get_key_id when _run_cros_config_host returns a key."""
1738 self.PatchObject(
1739 packages, "_run_cros_config_host", return_value=["key"]
1740 )
1741 result = packages.get_key_id(self.build_target, "model")
1742 self.assertEqual(result, "key")
Michael Mortensen359c1f32020-05-28 19:35:42 -06001743
Alex Klein1699fab2022-09-08 08:46:06 -06001744 def testGetKeyIdNoKey(self):
1745 """Test get_key_id when None should be returned."""
1746 self.PatchObject(
1747 packages, "_run_cros_config_host", return_value=["key1", "key2"]
1748 )
1749 result = packages.get_key_id(self.build_target, "model")
1750 self.assertEqual(result, None)
Ben Reiche779cf42020-12-15 03:21:31 +00001751
1752
Harvey Yang3eee06c2021-03-18 15:47:56 +08001753class GetLatestVersionTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001754 """Tests for get_latest_version_from_refs."""
Ben Reiche779cf42020-12-15 03:21:31 +00001755
Alex Klein1699fab2022-09-08 08:46:06 -06001756 def setUp(self):
1757 self.prefix = "refs/tags/drivefs_"
1758 # The tag ref template.
1759 ref_tpl = self.prefix + "%s"
Ben Reiche779cf42020-12-15 03:21:31 +00001760
Alex Klein1699fab2022-09-08 08:46:06 -06001761 self.latest = "44.0.20"
1762 self.versions = ["42.0.1", self.latest, "44.0.19", "39.0.15"]
1763 self.latest_ref = uprev_lib.GitRef(
1764 "/path", ref_tpl % self.latest, "abc123"
1765 )
1766 self.refs = [
1767 uprev_lib.GitRef("/path", ref_tpl % v, "abc123")
1768 for v in self.versions
1769 ]
Ben Reiche779cf42020-12-15 03:21:31 +00001770
Alex Klein1699fab2022-09-08 08:46:06 -06001771 def test_single_ref(self):
1772 """Test a single ref is supplied."""
1773 # pylint: disable=protected-access
1774 self.assertEqual(
1775 self.latest,
1776 packages._get_latest_version_from_refs(
1777 self.prefix, [self.latest_ref]
1778 ),
1779 )
Ben Reiche779cf42020-12-15 03:21:31 +00001780
Alex Klein1699fab2022-09-08 08:46:06 -06001781 def test_multiple_ref_versions(self):
1782 """Test multiple refs supplied."""
1783 # pylint: disable=protected-access
1784 self.assertEqual(
1785 self.latest,
1786 packages._get_latest_version_from_refs(self.prefix, self.refs),
1787 )
Ben Reiche779cf42020-12-15 03:21:31 +00001788
Alex Klein1699fab2022-09-08 08:46:06 -06001789 def test_no_refs_returns_none(self):
1790 """Test no refs supplied."""
1791 # pylint: disable=protected-access
1792 self.assertEqual(
1793 packages._get_latest_version_from_refs(self.prefix, []), None
1794 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001795
Chinglin Yu84818732022-10-03 12:03:43 +08001796 def test_ref_prefix(self):
1797 """Test refs with a different prefix isn't used"""
1798 # pylint: disable=protected-access
1799 # Add refs/tags/foo_100.0.0 to the refs, which should be ignored in
1800 # _get_latest_version_from_refs because the prefix doesn't match, even
1801 # if its version number is larger.
1802 refs = self.refs + [
1803 uprev_lib.GitRef("/path", "refs/tags/foo_100.0.0", "abc123")
1804 ]
1805 self.assertEqual(
1806 self.latest,
1807 packages._get_latest_version_from_refs(self.prefix, refs),
1808 )
1809
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001810
Alex Klein6becabc2020-09-11 14:03:05 -06001811class NeedsChromeSourceTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001812 """Tests for needs_chrome_source."""
Alex Klein6becabc2020-09-11 14:03:05 -06001813
Alex Klein1699fab2022-09-08 08:46:06 -06001814 def _build_graph(self, with_chrome: bool, with_followers: bool):
1815 root = "/build/build_target"
1816 foo_bar = package_info.parse("foo/bar-1")
1817 chrome = package_info.parse(f"{constants.CHROME_CP}-1.2.3.4")
1818 followers = [
1819 package_info.parse(f"{pkg}-1.2.3.4")
1820 for pkg in constants.OTHER_CHROME_PACKAGES
1821 ]
1822 nodes = [dependency_graph.PackageNode(foo_bar, root)]
1823 root_pkgs = ["foo/bar-1"]
1824 if with_chrome:
1825 nodes.append(dependency_graph.PackageNode(chrome, root))
1826 root_pkgs.append(chrome.cpvr)
1827 if with_followers:
1828 nodes.extend(
1829 [dependency_graph.PackageNode(f, root) for f in followers]
1830 )
1831 root_pkgs.extend([f.cpvr for f in followers])
Alex Klein6becabc2020-09-11 14:03:05 -06001832
Alex Klein1699fab2022-09-08 08:46:06 -06001833 return dependency_graph.DependencyGraph(nodes, root, root_pkgs)
Alex Klein6becabc2020-09-11 14:03:05 -06001834
Alex Klein1699fab2022-09-08 08:46:06 -06001835 def test_needs_all(self):
1836 """Verify we need source when we have no prebuilts."""
1837 graph = self._build_graph(with_chrome=True, with_followers=True)
1838 self.PatchObject(
1839 depgraph, "get_sysroot_dependency_graph", return_value=graph
1840 )
1841 self.PatchObject(packages, "has_prebuilt", return_value=False)
1842 self.PatchObject(
1843 packages,
1844 "uprev_chrome",
1845 return_value=uprev_lib.UprevVersionedPackageResult(),
1846 )
Alex Klein6becabc2020-09-11 14:03:05 -06001847
Alex Klein1699fab2022-09-08 08:46:06 -06001848 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001849
Alex Klein1699fab2022-09-08 08:46:06 -06001850 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001851
Alex Klein1699fab2022-09-08 08:46:06 -06001852 self.assertTrue(result.needs_chrome_source)
1853 self.assertTrue(result.builds_chrome)
1854 self.assertTrue(result.packages)
1855 self.assertEqual(
1856 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1857 )
1858 self.assertTrue(result.missing_chrome_prebuilt)
1859 self.assertTrue(result.missing_follower_prebuilt)
1860 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001861
Alex Klein1699fab2022-09-08 08:46:06 -06001862 def test_needs_none(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001863 """Verify not building any chrome packages prevents needing it."""
Alex Klein1699fab2022-09-08 08:46:06 -06001864 graph = self._build_graph(with_chrome=False, with_followers=False)
1865 self.PatchObject(
1866 depgraph, "get_sysroot_dependency_graph", return_value=graph
1867 )
1868 self.PatchObject(packages, "has_prebuilt", return_value=False)
1869 self.PatchObject(
1870 packages,
1871 "uprev_chrome",
1872 return_value=uprev_lib.UprevVersionedPackageResult(),
1873 )
Alex Klein6becabc2020-09-11 14:03:05 -06001874
Alex Klein1699fab2022-09-08 08:46:06 -06001875 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001876
Alex Klein1699fab2022-09-08 08:46:06 -06001877 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001878
Alex Klein1699fab2022-09-08 08:46:06 -06001879 self.assertFalse(result.needs_chrome_source)
1880 self.assertFalse(result.builds_chrome)
1881 self.assertFalse(result.packages)
1882 self.assertFalse(result.missing_chrome_prebuilt)
1883 self.assertFalse(result.missing_follower_prebuilt)
1884 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001885
Alex Klein1699fab2022-09-08 08:46:06 -06001886 def test_needs_chrome_only(self):
1887 """Verify only chrome triggers needs chrome source."""
1888 graph = self._build_graph(with_chrome=True, with_followers=False)
1889 self.PatchObject(
1890 depgraph, "get_sysroot_dependency_graph", return_value=graph
1891 )
1892 self.PatchObject(packages, "has_prebuilt", return_value=False)
1893 self.PatchObject(
1894 packages,
1895 "uprev_chrome",
1896 return_value=uprev_lib.UprevVersionedPackageResult(),
1897 )
Alex Klein6becabc2020-09-11 14:03:05 -06001898
Alex Klein1699fab2022-09-08 08:46:06 -06001899 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001900
Alex Klein1699fab2022-09-08 08:46:06 -06001901 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001902
Alex Klein1699fab2022-09-08 08:46:06 -06001903 self.assertTrue(result.needs_chrome_source)
1904 self.assertTrue(result.builds_chrome)
1905 self.assertTrue(result.packages)
1906 self.assertEqual(
Alex Klein041edd82023-04-17 12:23:23 -06001907 {p.atom for p in result.packages}, {constants.CHROME_CP}
Alex Klein1699fab2022-09-08 08:46:06 -06001908 )
1909 self.assertTrue(result.missing_chrome_prebuilt)
1910 self.assertFalse(result.missing_follower_prebuilt)
1911 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001912
Alex Klein1699fab2022-09-08 08:46:06 -06001913 def test_needs_followers_only(self):
1914 """Verify only chrome followers triggers needs chrome source."""
1915 graph = self._build_graph(with_chrome=False, with_followers=True)
1916 self.PatchObject(
1917 depgraph, "get_sysroot_dependency_graph", return_value=graph
1918 )
1919 self.PatchObject(packages, "has_prebuilt", return_value=False)
1920 self.PatchObject(
1921 packages,
1922 "uprev_chrome",
1923 return_value=uprev_lib.UprevVersionedPackageResult(),
1924 )
Alex Klein6becabc2020-09-11 14:03:05 -06001925
Alex Klein1699fab2022-09-08 08:46:06 -06001926 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001927
Alex Klein1699fab2022-09-08 08:46:06 -06001928 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001929
Alex Klein1699fab2022-09-08 08:46:06 -06001930 self.assertTrue(result.needs_chrome_source)
1931 self.assertFalse(result.builds_chrome)
1932 self.assertTrue(result.packages)
1933 self.assertEqual(
Alex Klein041edd82023-04-17 12:23:23 -06001934 {p.atom for p in result.packages},
Alex Klein1699fab2022-09-08 08:46:06 -06001935 set(constants.OTHER_CHROME_PACKAGES),
1936 )
1937 self.assertFalse(result.missing_chrome_prebuilt)
1938 self.assertTrue(result.missing_follower_prebuilt)
1939 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001940
Alex Klein1699fab2022-09-08 08:46:06 -06001941 def test_has_prebuilts(self):
1942 """Test prebuilts prevent us from needing chrome source."""
1943 graph = self._build_graph(with_chrome=True, with_followers=True)
1944 self.PatchObject(
1945 depgraph, "get_sysroot_dependency_graph", return_value=graph
1946 )
1947 self.PatchObject(packages, "has_prebuilt", return_value=True)
1948 self.PatchObject(
1949 packages,
1950 "uprev_chrome",
1951 return_value=uprev_lib.UprevVersionedPackageResult(),
1952 )
Alex Klein6becabc2020-09-11 14:03:05 -06001953
Alex Klein1699fab2022-09-08 08:46:06 -06001954 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001955
Alex Klein1699fab2022-09-08 08:46:06 -06001956 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001957
Alex Klein1699fab2022-09-08 08:46:06 -06001958 self.assertFalse(result.needs_chrome_source)
1959 self.assertTrue(result.builds_chrome)
1960 self.assertFalse(result.packages)
1961 self.assertFalse(result.missing_chrome_prebuilt)
1962 self.assertFalse(result.missing_follower_prebuilt)
1963 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001964
Alex Klein1699fab2022-09-08 08:46:06 -06001965 def test_compile_source(self):
1966 """Test compile source ignores prebuilts."""
1967 graph = self._build_graph(with_chrome=True, with_followers=True)
1968 self.PatchObject(
1969 depgraph, "get_sysroot_dependency_graph", return_value=graph
1970 )
1971 self.PatchObject(packages, "has_prebuilt", return_value=True)
1972 self.PatchObject(
1973 packages,
1974 "uprev_chrome",
1975 return_value=uprev_lib.UprevVersionedPackageResult(),
1976 )
Alex Klein6becabc2020-09-11 14:03:05 -06001977
Alex Klein1699fab2022-09-08 08:46:06 -06001978 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001979
Alex Klein1699fab2022-09-08 08:46:06 -06001980 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Klein6becabc2020-09-11 14:03:05 -06001981
Alex Klein1699fab2022-09-08 08:46:06 -06001982 self.assertTrue(result.needs_chrome_source)
1983 self.assertTrue(result.builds_chrome)
1984 self.assertTrue(result.packages)
1985 self.assertEqual(
1986 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1987 )
1988 self.assertTrue(result.missing_chrome_prebuilt)
1989 self.assertTrue(result.missing_follower_prebuilt)
1990 self.assertFalse(result.local_uprev)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001991
Alex Klein1699fab2022-09-08 08:46:06 -06001992 def test_local_uprev(self):
1993 """Test compile source ignores prebuilts."""
1994 graph = self._build_graph(with_chrome=True, with_followers=True)
1995 self.PatchObject(
1996 depgraph, "get_sysroot_dependency_graph", return_value=graph
1997 )
1998 self.PatchObject(packages, "has_prebuilt", return_value=False)
Alex Klein75110572021-07-14 10:44:39 -06001999
Alex Klein1699fab2022-09-08 08:46:06 -06002000 uprev_result = uprev_lib.UprevVersionedPackageResult()
2001 uprev_result.add_result("1.2.3.4", ["/tmp/foo"])
2002 self.PatchObject(packages, "uprev_chrome", return_value=uprev_result)
Alex Kleinde7b76d2021-07-12 12:28:44 -06002003
Alex Klein1699fab2022-09-08 08:46:06 -06002004 build_target = build_target_lib.BuildTarget("build_target")
Alex Kleinde7b76d2021-07-12 12:28:44 -06002005
Alex Klein1699fab2022-09-08 08:46:06 -06002006 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Kleinde7b76d2021-07-12 12:28:44 -06002007
Alex Klein1699fab2022-09-08 08:46:06 -06002008 self.assertTrue(result.needs_chrome_source)
2009 self.assertTrue(result.builds_chrome)
2010 self.assertTrue(result.packages)
2011 self.assertEqual(
2012 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
2013 )
2014 self.assertTrue(result.missing_chrome_prebuilt)
2015 self.assertTrue(result.missing_follower_prebuilt)
2016 self.assertTrue(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06002017
2018
Mike Frysinger8c9d7582023-08-21 19:28:21 -04002019class GetTargetVersionTest(cros_test_lib.RunCommandTestCase):
2020 """Tests for get_target_version."""
2021
2022 def setUp(self):
2023 self.build_target = build_target_lib.BuildTarget("build_target")
2024
2025 def test_default_empty(self):
2026 """Default behavior with mostly stub empty data."""
2027
2028 def GetBuildDependency(sysroot_path, board, mock_packages):
2029 assert sysroot_path == self.build_target.root
2030 assert board == self.build_target.name
2031 assert list(mock_packages) == [
2032 package_info.parse(constants.TARGET_OS_PKG)
2033 ]
2034 return {"package_deps": []}, {}
2035
2036 self.PatchObject(
2037 dependency, "GetBuildDependency", side_effect=GetBuildDependency
2038 )
2039 ret = packages.get_target_versions(self.build_target)
2040 assert ret.android_version is None
2041 assert ret.android_branch is None
2042 assert ret.android_target is None
2043 assert ret.chrome_version is None
2044 assert isinstance(ret.platform_version, str)
2045 assert isinstance(ret.milestone_version, str)
2046 assert isinstance(ret.full_version, str)
2047 assert ret.lacros_version is None
2048
2049
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002050class UprevDrivefsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002051 """Tests for uprev_drivefs."""
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002052
Alex Klein1699fab2022-09-08 08:46:06 -06002053 def setUp(self):
2054 self.refs = [
Alex Klein220e3a72023-09-14 17:12:13 -06002055 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -06002056 path="/chromeos/platform/drivefs-google3/",
2057 ref="refs/tags/drivefs_45.0.2",
2058 revision="123",
2059 )
2060 ]
2061 self.MOCK_DRIVEFS_EBUILD_PATH = "drivefs.45.0.2-r1.ebuild"
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002062
Alex Klein1699fab2022-09-08 08:46:06 -06002063 def revisionBumpOutcome(self, ebuild_path):
2064 return uprev_lib.UprevResult(
2065 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2066 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002067
Alex Klein1699fab2022-09-08 08:46:06 -06002068 def majorBumpOutcome(self, ebuild_path):
2069 return uprev_lib.UprevResult(
2070 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2071 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002072
Alex Klein1699fab2022-09-08 08:46:06 -06002073 def sameVersionOutcome(self):
2074 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002075
Alex Klein1699fab2022-09-08 08:46:06 -06002076 def test_latest_version_returns_none(self):
2077 """Test no refs were supplied"""
2078 output = packages.uprev_drivefs(None, [], None)
2079 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002080
Alex Klein1699fab2022-09-08 08:46:06 -06002081 def test_drivefs_uprev_fails(self):
2082 """Test a single ref is supplied."""
2083 self.PatchObject(
2084 uprev_lib,
2085 "uprev_workon_ebuild_to_version",
2086 side_effect=[None, None],
2087 )
2088 output = packages.uprev_drivefs(None, self.refs, None)
2089 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002090
Alex Klein1699fab2022-09-08 08:46:06 -06002091 def test_same_version_exists(self):
2092 """Test the same version exists uprev should not happen."""
2093 drivefs_outcome = self.sameVersionOutcome()
2094 self.PatchObject(
2095 uprev_lib,
2096 "uprev_workon_ebuild_to_version",
2097 side_effect=[drivefs_outcome],
2098 )
2099 output = packages.uprev_drivefs(None, self.refs, None)
2100 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002101
Alex Klein1699fab2022-09-08 08:46:06 -06002102 def test_revision_bump_both_packages(self):
2103 """Test both packages uprev, should succeed."""
2104 drivefs_outcome = self.revisionBumpOutcome(
2105 self.MOCK_DRIVEFS_EBUILD_PATH
2106 )
2107 self.PatchObject(
2108 uprev_lib,
2109 "uprev_workon_ebuild_to_version",
2110 side_effect=[drivefs_outcome],
2111 )
2112 output = packages.uprev_drivefs(None, self.refs, None)
2113 self.assertTrue(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002114
Alex Klein1699fab2022-09-08 08:46:06 -06002115 def test_major_bump_both_packages(self):
2116 """Test both packages uprev, should succeed."""
2117 drivefs_outcome = self.majorBumpOutcome(self.MOCK_DRIVEFS_EBUILD_PATH)
2118 self.PatchObject(
2119 uprev_lib,
2120 "uprev_workon_ebuild_to_version",
2121 side_effect=[drivefs_outcome],
2122 )
2123 output = packages.uprev_drivefs(None, self.refs, None)
2124 self.assertTrue(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002125
2126
Denis Nikitin63613e32022-09-09 22:26:50 -07002127class UprevKernelAfdo(cros_test_lib.RunCommandTempDirTestCase):
2128 """Tests for uprev_kernel_afdo."""
2129
2130 def setUp(self):
2131 # patch_ebuild_vars is tested separately.
2132 self.mock_patch = self.PatchObject(packages, "patch_ebuild_vars")
Denis Nikitin88ad5132022-09-28 12:10:01 -07002133 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Denis Nikitin63613e32022-09-09 22:26:50 -07002134 self.metadata_dir = os.path.join(
Denis Nikitin63613e32022-09-09 22:26:50 -07002135 "src",
2136 "third_party",
2137 "toolchain-utils",
2138 "afdo_metadata",
2139 )
Denis Nikitin88ad5132022-09-28 12:10:01 -07002140 osutils.SafeMakedirs(os.path.join(self.tempdir, self.metadata_dir))
Denis Nikitin63613e32022-09-09 22:26:50 -07002141
2142 def test_uprev_kernel_afdo_version(self):
2143 """Test kernel afdo version uprev."""
2144 json_files = {
2145 "kernel_afdo.json": (
2146 "{\n"
2147 ' "chromeos-kernel-5_4": {\n'
2148 ' "name": "R106-12345.0-0123456789"\n'
2149 " }\n"
2150 "}"
2151 ),
2152 "kernel_arm_afdo.json": (
2153 "{\n"
2154 ' "chromeos-kernel-5_15": {\n'
2155 ' "name": "R107-67890.0-0123456789"\n'
2156 " }\n"
2157 "}"
2158 ),
2159 }
2160 for f, contents in json_files.items():
2161 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2162
Alex Klein220e3a72023-09-14 17:12:13 -06002163 returned_output = packages.uprev_kernel_afdo(
2164 None, [], chroot_lib.Chroot()
2165 )
Denis Nikitin63613e32022-09-09 22:26:50 -07002166
Denis Nikitin88ad5132022-09-28 12:10:01 -07002167 package_root = os.path.join(
2168 constants.SOURCE_ROOT,
2169 constants.CHROMIUMOS_OVERLAY_DIR,
2170 "sys-kernel",
Denis Nikitin63613e32022-09-09 22:26:50 -07002171 )
2172 expect_result = [
2173 uprev_lib.UprevVersionedPackageModifications(
2174 new_version="R106-12345.0-0123456789",
2175 files=[
2176 os.path.join(
2177 package_root,
2178 "chromeos-kernel-5_4",
2179 "chromeos-kernel-5_4-9999.ebuild",
2180 ),
2181 os.path.join(
2182 package_root, "chromeos-kernel-5_4", "Manifest"
2183 ),
2184 ],
2185 ),
2186 uprev_lib.UprevVersionedPackageModifications(
2187 new_version="R107-67890.0-0123456789",
2188 files=[
2189 os.path.join(
2190 package_root,
2191 "chromeos-kernel-5_15",
2192 "chromeos-kernel-5_15-9999.ebuild",
2193 ),
2194 os.path.join(
2195 package_root, "chromeos-kernel-5_15", "Manifest"
2196 ),
2197 ],
2198 ),
2199 ]
2200 self.assertTrue(returned_output.uprevved)
2201 self.assertEqual(returned_output.modified, expect_result)
2202
2203 def test_uprev_kernel_afdo_empty_json(self):
2204 """Test kernel afdo version unchanged."""
2205 json_files = {
2206 "kernel_afdo.json": "{}",
2207 "kernel_arm_afdo.json": "{}",
2208 }
2209 for f, contents in json_files.items():
2210 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2211
Alex Klein220e3a72023-09-14 17:12:13 -06002212 returned_output = packages.uprev_kernel_afdo(
2213 None, [], chroot_lib.Chroot()
2214 )
Denis Nikitin63613e32022-09-09 22:26:50 -07002215 self.assertFalse(returned_output.uprevved)
2216
2217 def test_uprev_kernel_afdo_empty_file(self):
2218 """Test malformed json raises."""
2219 json_files = {
2220 "kernel_afdo.json": "",
2221 "kernel_arm_afdo.json": "",
2222 }
2223 for f, contents in json_files.items():
2224 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2225
2226 with self.assertRaisesRegex(
2227 json.decoder.JSONDecodeError, "Expecting value"
2228 ):
Alex Klein220e3a72023-09-14 17:12:13 -06002229 packages.uprev_kernel_afdo(None, [], chroot_lib.Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002230
2231 def test_uprev_kernel_afdo_manifest_raises(self):
2232 """Test manifest update raises."""
2233 json_files = {
2234 "kernel_afdo.json": (
2235 "{\n"
2236 ' "chromeos-kernel-5_4": {\n'
2237 ' "name": "R106-12345.0-0123456789"\n'
2238 " }\n"
2239 "}"
2240 ),
2241 }
2242 for f, contents in json_files.items():
2243 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2244 # run() raises exception.
2245 self.rc.SetDefaultCmdResult(
2246 side_effect=cros_build_lib.RunCommandError("error")
2247 )
2248
2249 with self.assertRaises(uprev_lib.EbuildManifestError):
Alex Klein220e3a72023-09-14 17:12:13 -06002250 packages.uprev_kernel_afdo(None, [], chroot_lib.Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002251
2252
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002253# TODO(chenghaoyang): Shouldn't use uprev_workon_ebuild_to_version.
2254class UprevPerfettoTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002255 """Tests for uprev_perfetto."""
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002256
Alex Klein1699fab2022-09-08 08:46:06 -06002257 def setUp(self):
Alex Klein220e3a72023-09-14 17:12:13 -06002258 self.refs = [
2259 uprev_lib.GitRef(path="/foo", ref="refs/tags/v12.0", revision="123")
2260 ]
Alex Klein1699fab2022-09-08 08:46:06 -06002261 self.MOCK_PERFETTO_EBUILD_PATH = "perfetto-12.0-r1.ebuild"
Chinglin Yufa728552023-04-13 03:12:04 +00002262 self.MOCK_PERFETTO_PROTO_EBUILD_PATH = "perfetto-protos-12.0-r1.ebuild"
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002263
Chinglin Yufa728552023-04-13 03:12:04 +00002264 def revisionBumpOutcome(self):
2265 return [
2266 uprev_lib.UprevResult(
2267 uprev_lib.Outcome.REVISION_BUMP,
2268 [self.MOCK_PERFETTO_EBUILD_PATH],
2269 ),
2270 uprev_lib.UprevResult(
2271 uprev_lib.Outcome.REVISION_BUMP,
2272 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2273 ),
2274 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002275
Chinglin Yufa728552023-04-13 03:12:04 +00002276 def majorBumpOutcome(self):
2277 return [
2278 uprev_lib.UprevResult(
2279 uprev_lib.Outcome.VERSION_BUMP, [self.MOCK_PERFETTO_EBUILD_PATH]
2280 ),
2281 uprev_lib.UprevResult(
2282 uprev_lib.Outcome.VERSION_BUMP,
2283 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2284 ),
2285 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002286
Alex Klein1699fab2022-09-08 08:46:06 -06002287 def newerVersionOutcome(self):
2288 return uprev_lib.UprevResult(uprev_lib.Outcome.NEWER_VERSION_EXISTS)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002289
Alex Klein1699fab2022-09-08 08:46:06 -06002290 def sameVersionOutcome(self):
2291 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002292
Alex Klein1699fab2022-09-08 08:46:06 -06002293 def test_latest_version_returns_none(self):
2294 """Test no refs were supplied"""
2295 output = packages.uprev_perfetto(None, [], None)
2296 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002297
Alex Klein1699fab2022-09-08 08:46:06 -06002298 def test_perfetto_uprev_fails(self):
2299 """Test a single ref is supplied."""
2300 self.PatchObject(
2301 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2302 )
2303 output = packages.uprev_perfetto(None, self.refs, None)
2304 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002305
Alex Klein1699fab2022-09-08 08:46:06 -06002306 def test_newer_version_exists(self):
2307 """Test the newer version exists uprev should not happen."""
2308 perfetto_outcome = self.newerVersionOutcome()
2309 self.PatchObject(
2310 uprev_lib,
2311 "uprev_workon_ebuild_to_version",
2312 side_effect=[perfetto_outcome],
2313 )
2314 output = packages.uprev_perfetto(None, self.refs, None)
2315 self.assertFalse(output.uprevved)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002316
Alex Klein1699fab2022-09-08 08:46:06 -06002317 def test_same_version_exists(self):
2318 """Test the same version exists uprev should not happen."""
2319 perfetto_outcome = self.sameVersionOutcome()
2320 self.PatchObject(
2321 uprev_lib,
2322 "uprev_workon_ebuild_to_version",
2323 side_effect=[perfetto_outcome],
2324 )
2325 output = packages.uprev_perfetto(None, self.refs, None)
2326 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002327
Alex Klein1699fab2022-09-08 08:46:06 -06002328 def test_revision_bump_perfetto_package(self):
2329 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002330 self.PatchObject(
2331 uprev_lib,
2332 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002333 side_effect=self.revisionBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002334 )
2335 output = packages.uprev_perfetto(None, self.refs, None)
2336 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002337 self.assertEqual(
2338 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2339 )
2340 self.assertEqual(
2341 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2342 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002343
Alex Klein1699fab2022-09-08 08:46:06 -06002344 def test_major_bump_perfetto_package(self):
2345 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002346 self.PatchObject(
2347 uprev_lib,
2348 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002349 side_effect=self.majorBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002350 )
2351 output = packages.uprev_perfetto(None, self.refs, None)
2352 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002353 self.assertEqual(
2354 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2355 )
2356 self.assertEqual(
2357 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2358 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002359
Chinglin Yuad12a512022-10-07 17:26:12 +08002360 def test_revision_bump_trunk(self):
2361 """Test revision bump on receiving non-versioned trunk refs."""
Chinglin Yu5de28a42022-11-11 19:52:21 +08002362 refs = [
Alex Klein220e3a72023-09-14 17:12:13 -06002363 uprev_lib.GitRef(
Chinglin Yu5de28a42022-11-11 19:52:21 +08002364 path="/foo", ref="refs/heads/main", revision="0123456789abcdef"
2365 )
2366 ]
Chinglin Yuad12a512022-10-07 17:26:12 +08002367 self.PatchObject(
2368 uprev_lib, "get_stable_ebuild_version", return_value="12.0"
2369 )
2370 self.PatchObject(
2371 uprev_lib,
2372 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002373 side_effect=self.revisionBumpOutcome(),
Chinglin Yuad12a512022-10-07 17:26:12 +08002374 )
2375 output = packages.uprev_perfetto(None, refs, None)
Chinglin Yufa728552023-04-13 03:12:04 +00002376
Chinglin Yuad12a512022-10-07 17:26:12 +08002377 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002378 self.assertEqual(
2379 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2380 )
Chinglin Yu5de28a42022-11-11 19:52:21 +08002381 self.assertEqual(output.modified[0].new_version, "12.0-012345678")
Chinglin Yufa728552023-04-13 03:12:04 +00002382 self.assertEqual(
2383 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2384 )
2385 self.assertEqual(output.modified[1].new_version, "12.0-012345678")
Chinglin Yuad12a512022-10-07 17:26:12 +08002386
Alex Klein627e04c2021-11-10 15:56:47 -07002387
Julio Hurtadof1befec2021-05-05 21:34:26 +00002388class UprevLacrosTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002389 """Tests for uprev_lacros"""
Julio Hurtadof1befec2021-05-05 21:34:26 +00002390
Alex Klein1699fab2022-09-08 08:46:06 -06002391 def setUp(self):
2392 self.refs = [
Alex Klein220e3a72023-09-14 17:12:13 -06002393 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -06002394 path="/lacros", ref="refs/heads/main", revision="123.456.789.0"
2395 )
2396 ]
2397 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtadof1befec2021-05-05 21:34:26 +00002398
Alex Klein1699fab2022-09-08 08:46:06 -06002399 def revisionBumpOutcome(self, ebuild_path):
2400 return uprev_lib.UprevResult(
2401 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2402 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002403
Alex Klein1699fab2022-09-08 08:46:06 -06002404 def majorBumpOutcome(self, ebuild_path):
2405 return uprev_lib.UprevResult(
2406 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2407 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002408
Alex Klein1699fab2022-09-08 08:46:06 -06002409 def newerVersionOutcome(self, ebuild_path):
2410 return uprev_lib.UprevResult(
2411 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2412 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002413
Alex Klein1699fab2022-09-08 08:46:06 -06002414 def sameVersionOutcome(self, ebuild_path):
2415 return uprev_lib.UprevResult(
2416 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2417 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002418
Alex Klein1699fab2022-09-08 08:46:06 -06002419 def newEbuildCreatedOutcome(self, ebuild_path):
2420 return uprev_lib.UprevResult(
2421 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2422 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002423
Alex Klein1699fab2022-09-08 08:46:06 -06002424 def test_lacros_uprev_fails(self):
2425 """Test a lacros package uprev with no triggers"""
2426 self.PatchObject(
2427 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2428 )
2429 with self.assertRaises(IndexError):
2430 packages.uprev_lacros(None, [], None)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002431
Alex Klein1699fab2022-09-08 08:46:06 -06002432 def test_lacros_uprev_revision_bump(self):
2433 """Test lacros package uprev."""
2434 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2435 self.PatchObject(
2436 uprev_lib,
2437 "uprev_workon_ebuild_to_version",
2438 side_effect=[lacros_outcome],
2439 )
2440 output = packages.uprev_lacros(None, self.refs, None)
2441 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002442
Alex Klein1699fab2022-09-08 08:46:06 -06002443 def test_lacros_uprev_version_bump(self):
2444 """Test lacros package uprev."""
2445 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2446 self.PatchObject(
2447 uprev_lib,
2448 "uprev_workon_ebuild_to_version",
2449 side_effect=[lacros_outcome],
2450 )
2451 output = packages.uprev_lacros(None, self.refs, None)
2452 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002453
Alex Klein1699fab2022-09-08 08:46:06 -06002454 def test_lacros_uprev_new_ebuild_created(self):
2455 """Test lacros package uprev."""
2456 lacros_outcome = self.newEbuildCreatedOutcome(
2457 self.MOCK_LACROS_EBUILD_PATH
2458 )
2459 self.PatchObject(
2460 uprev_lib,
2461 "uprev_workon_ebuild_to_version",
2462 side_effect=[lacros_outcome],
2463 )
2464 output = packages.uprev_lacros(None, self.refs, None)
2465 self.assertTrue(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002466
Alex Klein1699fab2022-09-08 08:46:06 -06002467 def test_lacros_uprev_newer_version_exist(self):
2468 """Test the newer version exists uprev should not happen."""
2469 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2470 self.PatchObject(
2471 uprev_lib,
2472 "uprev_workon_ebuild_to_version",
2473 side_effect=[lacros_outcome],
2474 )
2475 output = packages.uprev_lacros(None, self.refs, None)
2476 self.assertFalse(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002477
Alex Klein1699fab2022-09-08 08:46:06 -06002478 def test_lacros_uprev_same_version_exist(self):
2479 """Test the same version exists uprev should not happen."""
2480 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2481 self.PatchObject(
2482 uprev_lib,
2483 "uprev_workon_ebuild_to_version",
2484 side_effect=[lacros_outcome],
2485 )
2486 output = packages.uprev_lacros(None, self.refs, None)
2487 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002488
2489
2490class UprevLacrosInParallelTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002491 """Tests for uprev_lacros"""
Julio Hurtado870ed322021-12-03 18:22:40 +00002492
Alex Klein1699fab2022-09-08 08:46:06 -06002493 def setUp(self):
2494 self.refs = [
Alex Klein220e3a72023-09-14 17:12:13 -06002495 uprev_lib.GitRef(
Alex Klein1699fab2022-09-08 08:46:06 -06002496 path="/lacros", revision="abc123", ref="refs/tags/123.456.789.0"
2497 )
2498 ]
2499 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtado870ed322021-12-03 18:22:40 +00002500
Alex Klein1699fab2022-09-08 08:46:06 -06002501 def revisionBumpOutcome(self, ebuild_path):
2502 return uprev_lib.UprevResult(
2503 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2504 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002505
Alex Klein1699fab2022-09-08 08:46:06 -06002506 def majorBumpOutcome(self, ebuild_path):
2507 return uprev_lib.UprevResult(
2508 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2509 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002510
Alex Klein1699fab2022-09-08 08:46:06 -06002511 def newerVersionOutcome(self, ebuild_path):
2512 return uprev_lib.UprevResult(
2513 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2514 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002515
Alex Klein1699fab2022-09-08 08:46:06 -06002516 def sameVersionOutcome(self, ebuild_path):
2517 return uprev_lib.UprevResult(
2518 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2519 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002520
Alex Klein1699fab2022-09-08 08:46:06 -06002521 def newEbuildCreatedOutcome(self, ebuild_path):
2522 return uprev_lib.UprevResult(
2523 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2524 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002525
Alex Klein1699fab2022-09-08 08:46:06 -06002526 def test_lacros_uprev_fails(self):
2527 """Test a lacros package uprev with no triggers"""
2528 self.PatchObject(
2529 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2530 )
Alex Klein314fb5d2022-10-24 14:56:31 -06002531 with self.assertRaises(uprev_lib.NoRefsError):
Alex Klein1699fab2022-09-08 08:46:06 -06002532 packages.uprev_lacros_in_parallel(None, [], None)
Julio Hurtado870ed322021-12-03 18:22:40 +00002533
Alex Klein1699fab2022-09-08 08:46:06 -06002534 def test_lacros_uprev_revision_bump(self):
2535 """Test lacros package uprev."""
2536 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2537 self.PatchObject(
2538 uprev_lib,
2539 "uprev_workon_ebuild_to_version",
2540 side_effect=[lacros_outcome],
2541 )
2542 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2543 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002544
Alex Klein1699fab2022-09-08 08:46:06 -06002545 def test_lacros_uprev_version_bump(self):
2546 """Test lacros package uprev."""
2547 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2548 self.PatchObject(
2549 uprev_lib,
2550 "uprev_workon_ebuild_to_version",
2551 side_effect=[lacros_outcome],
2552 )
2553 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2554 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002555
Alex Klein1699fab2022-09-08 08:46:06 -06002556 def test_lacros_uprev_new_ebuild_created(self):
2557 """Test lacros package uprev."""
2558 lacros_outcome = self.newEbuildCreatedOutcome(
2559 self.MOCK_LACROS_EBUILD_PATH
2560 )
2561 self.PatchObject(
2562 uprev_lib,
2563 "uprev_workon_ebuild_to_version",
2564 side_effect=[lacros_outcome],
2565 )
2566 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2567 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002568
Alex Klein1699fab2022-09-08 08:46:06 -06002569 def test_lacros_uprev_newer_version_exist(self):
2570 """Test the newer version exists uprev should not happen."""
2571 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2572 self.PatchObject(
2573 uprev_lib,
2574 "uprev_workon_ebuild_to_version",
2575 side_effect=[lacros_outcome],
2576 )
2577 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2578 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002579
Alex Klein1699fab2022-09-08 08:46:06 -06002580 def test_lacros_uprev_same_version_exist(self):
2581 """Test the same version exists uprev should not happen."""
2582 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2583 self.PatchObject(
2584 uprev_lib,
2585 "uprev_workon_ebuild_to_version",
2586 side_effect=[lacros_outcome],
2587 )
2588 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2589 self.assertFalse(output.uprevved)
Luigi Semenzatoea25fe82023-08-03 17:11:25 -07002590
2591
2592class UprevStarbaseArtifactsTest(cros_test_lib.RunCommandTempDirTestCase):
2593 """Tests of uprev of starbase artifacts ebuild."""
2594
2595 package_name = "chromeos-base/starbase-artifacts"
2596 version = "2.4.6"
2597 revision = "111"
2598 ebuild_name_format = "starbase-artifacts-%s-r%s.ebuild"
2599 old_ebuild_name = ebuild_name_format % (version, revision)
2600 ebuild_content_format = """# Buildable ebuild
2601foo
2602bar
2603baz
2604SRC_URI="${DISTFILES}/%s"
2605zab
2606rab
2607oof
2608"""
2609
2610 def test_uprev(self):
2611 """Test that the ebuild is modified and uprevved."""
2612
2613 # Create ebuild directory.
2614 directory_tree = (
2615 D(self.package_name, [self.old_ebuild_name, "Manifest"]),
2616 )
2617 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, directory_tree)
2618 package_path = os.path.join(self.tempdir, self.package_name)
2619 old_ebuild_path = os.path.join(package_path, self.old_ebuild_name)
2620 old_ebuild_content = self.ebuild_content_format % "to-be-clobbered"
2621
2622 # Create mock ebuild to be uprevved.
2623 self.WriteTempFile(old_ebuild_path, old_ebuild_content)
2624 tarfile_name = "starbase-artifacts-20230101-rc123.tar.zst"
2625 manifest_path = os.path.join(package_path, "Manifest")
2626
2627 # Run the function under test.
2628 modified = packages.starbase_find_and_uprev(package_path, tarfile_name)
2629
2630 # Check that the expected files were modified.
2631 new_revision = str(int(self.revision) + 1)
2632 new_ebuild_name = self.ebuild_name_format % (self.version, new_revision)
2633 new_ebuild_path = os.path.join(package_path, new_ebuild_name)
2634
2635 self.assertEqual(modified[0], manifest_path)
2636 self.assertEqual(modified[1], old_ebuild_path)
2637 self.assertEqual(modified[2], new_ebuild_path)
2638
2639 # Check that the new ebuild file contains the expected content.
2640 new_ebuild_content = self.ebuild_content_format % tarfile_name
2641 found_content = osutils.ReadFile(new_ebuild_path)
2642 self.assertEqual(new_ebuild_content, found_content)