blob: 8ca6dd0b42d448282305ca933248997432815a3d [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
Mike Frysinger2c024062021-05-22 15:43:22 -040013from chromite.third_party.google.protobuf import json_format
14from chromite.third_party.google.protobuf.field_mask_pb2 import FieldMask
Mike Frysinger68796b52019-08-25 00:04:27 -040015import pytest
Andrew Lamb2bde9e42019-11-04 13:24:09 -070016
Chris McDonaldea0312c2020-05-04 23:33:15 -060017import chromite as cr
Andrew Lamb2bde9e42019-11-04 13:24:09 -070018from chromite.api.gen.config.replication_config_pb2 import (
Mike Frysinger68796b52019-08-25 00:04:27 -040019 FILE_TYPE_JSON,
20 FileReplicationRule,
21 REPLICATION_TYPE_FILTER,
22 ReplicationConfig,
23)
Alex Klein2960c752020-03-09 13:43:38 -060024from chromite.lib import build_target_lib
Ram Chandrasekar60f69f32022-06-03 22:49:30 +000025from chromite.lib import chromeos_version
Andrew Lamb2bde9e42019-11-04 13:24:09 -070026from chromite.lib import constants
Michael Mortensene0f4b542019-10-24 15:30:23 -060027from chromite.lib import cros_build_lib
Alex Kleineb77ffa2019-05-28 14:47:44 -060028from chromite.lib import cros_test_lib
Alex Klein6becabc2020-09-11 14:03:05 -060029from chromite.lib import dependency_graph
Mike Frysinger68796b52019-08-25 00:04:27 -040030from chromite.lib import depgraph
Michael Mortensenb70e8a82019-10-10 18:43:41 -060031from chromite.lib import osutils
Mike Frysinger88d96362020-02-14 19:05:45 -050032from chromite.lib import partial_mock
Alex Klein87531182019-08-12 15:23:37 -060033from chromite.lib import portage_util
Chris McDonaldea0312c2020-05-04 23:33:15 -060034from chromite.lib import uprev_lib
Alex Klein87531182019-08-12 15:23:37 -060035from chromite.lib.chroot_lib import Chroot
Alex Klein18a60af2020-06-11 12:08:47 -060036from chromite.lib.parser import package_info
Andrew Lamb2bde9e42019-11-04 13:24:09 -070037from chromite.lib.uprev_lib import GitRef
Shao-Chuan Lee05e51142021-11-24 12:27:37 +090038from chromite.service import android
Mike Frysinger8c9d7582023-08-21 19:28:21 -040039from chromite.service import dependency
Alex Kleineb77ffa2019-05-28 14:47:44 -060040from chromite.service import packages
41
Mike Frysinger68796b52019-08-25 00:04:27 -040042
Andrew Lamb2bde9e42019-11-04 13:24:09 -070043D = cros_test_lib.Directory
44
Alex Kleineb77ffa2019-05-28 14:47:44 -060045
Alex Klein4de25e82019-08-05 15:58:39 -060046class UprevAndroidTest(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060047 """Uprev android tests."""
Alex Klein4de25e82019-08-05 15:58:39 -060048
Alex Klein1699fab2022-09-08 08:46:06 -060049 def _mock_successful_uprev(self):
50 self.rc.AddCmdResult(
51 partial_mock.In("cros_mark_android_as_stable"),
52 stdout=(
53 '{"revved": true,'
54 ' "android_atom": "android/android-1.0",'
55 ' "modified_files": ["file1", "file2"]}'
56 ),
57 )
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090058
Alex Klein1699fab2022-09-08 08:46:06 -060059 def test_success(self):
60 """Test successful run handling."""
61 self._mock_successful_uprev()
62 build_targets = [
63 build_target_lib.BuildTarget(t) for t in ["foo", "bar"]
64 ]
Alex Klein4de25e82019-08-05 15:58:39 -060065
Alex Klein1699fab2022-09-08 08:46:06 -060066 result = packages.uprev_android(
67 "android/package", Chroot(), build_targets=build_targets
68 )
69 self.assertCommandContains(
70 [
71 "cros_mark_android_as_stable",
72 "--android_package=android/package",
73 "--boards=foo:bar",
74 ]
75 )
76 self.assertCommandContains(["emerge-foo"])
77 self.assertCommandContains(["emerge-bar"])
Alex Klein4de25e82019-08-05 15:58:39 -060078
Alex Klein1699fab2022-09-08 08:46:06 -060079 self.assertTrue(result.revved)
80 self.assertEqual(result.android_atom, "android/android-1.0")
81 self.assertListEqual(result.modified_files, ["file1", "file2"])
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090082
Alex Klein1699fab2022-09-08 08:46:06 -060083 def test_android_build_branch(self):
84 """Test specifying android_build_branch option."""
85 self._mock_successful_uprev()
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090086
Alex Klein1699fab2022-09-08 08:46:06 -060087 packages.uprev_android(
88 "android/package",
89 Chroot(),
90 android_build_branch="android-build-branch",
91 )
92 self.assertCommandContains(
93 [
94 "cros_mark_android_as_stable",
95 "--android_package=android/package",
96 "--android_build_branch=android-build-branch",
97 ]
98 )
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 def test_android_version(self):
101 """Test specifying android_version option."""
102 self._mock_successful_uprev()
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 packages.uprev_android(
105 "android/package", Chroot(), android_version="7123456"
106 )
107 self.assertCommandContains(
108 [
109 "cros_mark_android_as_stable",
110 "--android_package=android/package",
111 "--force_version=7123456",
112 ]
113 )
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900114
Alex Klein1699fab2022-09-08 08:46:06 -0600115 def test_skip_commit(self):
116 """Test specifying skip_commit option."""
117 self._mock_successful_uprev()
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900118
Alex Klein1699fab2022-09-08 08:46:06 -0600119 packages.uprev_android("android/package", Chroot(), skip_commit=True)
120 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(
138 "android/package", Chroot(), build_targets=build_targets
139 )
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 )
155 result = packages.uprev_android("android/package", 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")]
172 chroot = 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 Klein1699fab2022-09-08 08:46:06 -0600197 result = packages.uprev_android_lkgb("android-package", [], Chroot())
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900198
Alex Klein1699fab2022-09-08 08:46:06 -0600199 self.assertListEqual(
200 result.modified,
201 [
202 uprev_lib.UprevVersionedPackageModifications(
203 "android-lkgb",
204 [
205 os.path.join("overlay-dir", "file1"),
206 os.path.join("overlay-dir", "file2"),
207 ],
208 )
209 ],
210 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900211
Alex Klein1699fab2022-09-08 08:46:06 -0600212 def test_no_rev(self):
213 """Test when nothing revved."""
Shao-Chuan Leee0b9ba92023-01-18 19:35:36 +0900214 self.PatchObject(
215 android, "ReadLKGB", return_value=dict(build_id="android-lkgb")
216 )
Alex Klein1699fab2022-09-08 08:46:06 -0600217 self.PatchObject(
218 packages,
219 "uprev_android",
220 return_value=packages.UprevAndroidResult(revved=False),
221 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900222
Alex Klein1699fab2022-09-08 08:46:06 -0600223 result = packages.uprev_android_lkgb("android-package", [], Chroot())
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900224
Alex Klein1699fab2022-09-08 08:46:06 -0600225 self.assertListEqual(result.modified, [])
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900226
227
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700228class UprevECUtilsTest(cros_test_lib.MockTestCase):
229 """Tests for upreving ecutils."""
230
231 def test_success(self):
232 """Test a successful uprev."""
233
234 def fakeRunTasks(func, inputs):
235 results = []
236 for args in inputs:
237 results.append(func(*args))
238 return results
239
240 self.PatchObject(
241 packages.uprev_lib.parallel,
242 "RunTasksInProcessPool",
243 side_effect=fakeRunTasks,
244 )
245 mock_devutils = mock.MagicMock(name="dev-utils")
246 mock_ecutils = mock.MagicMock(name="ec-utils")
247 mock_ecutilstest = mock.MagicMock(name="ec-utils-test")
248 self.PatchObject(
249 packages.uprev_lib.portage_util,
250 "GetOverlayEBuilds",
251 return_value=[
252 mock_devutils,
253 mock_ecutils,
254 mock_ecutilstest,
255 ],
256 )
257 mock_overlay_mgr = mock.MagicMock(name="overlay-manager")
258 mock_overlay_mgr.modified_ebuilds = ["file1", "file2"]
259 self.PatchObject(
260 packages.uprev_lib,
261 "UprevOverlayManager",
262 return_value=mock_overlay_mgr,
263 )
Jeremy Bettis0186d252023-01-19 14:47:46 -0700264
265 for package in [
266 "chromeos-base/ec-devutils",
267 "chromeos-base/ec-utils",
268 "chromeos-base/ec-utils-test",
269 ]:
270 cpv = package_info.SplitCPV(package, strict=False)
271 assert cpv is not None
272 build_targets = [build_target_lib.BuildTarget("foo")]
273 refs = [
274 GitRef(
275 path="/platform/ec",
276 ref="main",
277 revision="123",
278 )
279 ]
280 chroot = Chroot()
281
282 result = packages.uprev_versioned_package(
283 cpv, build_targets, refs, chroot
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700284 )
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700285
Jeremy Bettis0186d252023-01-19 14:47:46 -0700286 self.assertEqual(1, len(result.modified))
287 self.assertEqual("123", result.modified[0].new_version)
288 self.assertListEqual(result.modified[0].files, ["file1", "file2"])
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700289
Jeremy Bettis0186d252023-01-19 14:47:46 -0700290 mock_overlay_mgr.uprev.assert_called_with(
291 package_list=[
292 package,
293 ],
294 force=True,
295 )
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700296
297
Alex Kleineb77ffa2019-05-28 14:47:44 -0600298class UprevBuildTargetsTest(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600299 """uprev_build_targets tests."""
Alex Kleineb77ffa2019-05-28 14:47:44 -0600300
Alex Klein1699fab2022-09-08 08:46:06 -0600301 def test_invalid_type_fails(self):
302 """Test invalid type fails."""
303 with self.assertRaises(AssertionError):
304 packages.uprev_build_targets(
305 [build_target_lib.BuildTarget("foo")], "invalid"
306 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600307
Alex Klein1699fab2022-09-08 08:46:06 -0600308 def test_none_type_fails(self):
309 """Test None type fails."""
310 with self.assertRaises(AssertionError):
311 packages.uprev_build_targets(
312 [build_target_lib.BuildTarget("foo")], None
313 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600314
315
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000316class PatchEbuildVarsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600317 """patch_ebuild_vars test."""
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000318
Alex Klein1699fab2022-09-08 08:46:06 -0600319 def setUp(self):
320 self.mock_input = self.PatchObject(packages.fileinput, "input")
321 self.mock_stdout_write = self.PatchObject(packages.sys.stdout, "write")
322 self.ebuild_path = "/path/to/ebuild"
323 self.old_var_value = "R100-5678.0.123456789"
324 self.new_var_value = "R102-5678.0.234566789"
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000325
Alex Klein1699fab2022-09-08 08:46:06 -0600326 def test_patch_ebuild_vars_var_only(self):
327 """patch_ebuild_vars changes ^var=value$."""
328 ebuild_contents = (
329 "This line does not change.\n"
330 'AFDO_PROFILE_VERSION="{var_value}"\n'
331 "\n"
332 "# The line with AFDO_PROFILE_VERSION is also unchanged."
333 )
334 # Ebuild contains old_var_value.
335 self.mock_input.return_value = io.StringIO(
336 ebuild_contents.format(var_value=self.old_var_value)
337 )
338 expected_calls = []
339 # Expect the line with new_var_value.
340 for line in io.StringIO(
341 ebuild_contents.format(var_value=self.new_var_value)
342 ):
343 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000344
Alex Klein1699fab2022-09-08 08:46:06 -0600345 packages.patch_ebuild_vars(
346 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
347 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000348
Alex Klein1699fab2022-09-08 08:46:06 -0600349 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000350
Alex Klein1699fab2022-09-08 08:46:06 -0600351 def test_patch_ebuild_vars_ignore_export(self):
352 """patch_ebuild_vars changes ^export var=value$ and keeps export."""
353 ebuild_contents = (
354 "This line does not change.\n"
355 'export AFDO_PROFILE_VERSION="{var_value}"\n'
356 "# This line is also unchanged."
357 )
358 # Ebuild contains old_var_value.
359 self.mock_input.return_value = io.StringIO(
360 ebuild_contents.format(var_value=self.old_var_value)
361 )
362 expected_calls = []
363 # Expect the line with new_var_value.
364 for line in io.StringIO(
365 ebuild_contents.format(var_value=self.new_var_value)
366 ):
367 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000368
Alex Klein1699fab2022-09-08 08:46:06 -0600369 packages.patch_ebuild_vars(
370 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
371 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000372
Alex Klein1699fab2022-09-08 08:46:06 -0600373 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000374
Alex Klein1699fab2022-09-08 08:46:06 -0600375 def test_patch_ebuild_vars_partial_match(self):
376 """patch_ebuild_vars ignores ^{prefix}var=value$."""
377 ebuild_contents = (
Alex Kleina53bd282022-09-09 12:42:55 -0600378 'This and the line below do not change.\nNEW_AFDO="{var_value}"'
Alex Klein1699fab2022-09-08 08:46:06 -0600379 )
380 # Ebuild contains old_var_value.
381 self.mock_input.return_value = io.StringIO(
382 ebuild_contents.format(var_value=self.old_var_value)
383 )
384 expected_calls = []
385 # Expect the line with UNCHANGED old_var_value.
386 for line in io.StringIO(
387 ebuild_contents.format(var_value=self.old_var_value)
388 ):
389 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000390
Alex Kleinfee86da2023-01-20 18:40:06 -0700391 # Note that the var name partially matches the ebuild var and hence it
392 # has to be ignored.
Alex Klein1699fab2022-09-08 08:46:06 -0600393 packages.patch_ebuild_vars(
394 self.ebuild_path, {"AFDO": self.new_var_value}
395 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000396
Alex Klein1699fab2022-09-08 08:46:06 -0600397 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000398
Alex Klein1699fab2022-09-08 08:46:06 -0600399 def test_patch_ebuild_vars_no_vars(self):
400 """patch_ebuild_vars keeps ebuild intact if there are no vars."""
401 ebuild_contents = (
402 "This line does not change.\n"
403 "The line with AFDO_PROFILE_VERSION is also unchanged."
404 )
405 self.mock_input.return_value = io.StringIO(ebuild_contents)
406 expected_calls = []
407 for line in io.StringIO(ebuild_contents):
408 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000409
Alex Klein1699fab2022-09-08 08:46:06 -0600410 packages.patch_ebuild_vars(
411 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
412 )
413
414 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000415
416
Alex Klein87531182019-08-12 15:23:37 -0600417class UprevsVersionedPackageTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600418 """uprevs_versioned_package decorator test."""
Alex Klein87531182019-08-12 15:23:37 -0600419
Alex Klein1699fab2022-09-08 08:46:06 -0600420 @packages.uprevs_versioned_package("category/package")
421 def uprev_category_package(self, *args, **kwargs):
422 """Registered function for testing."""
Alex Klein87531182019-08-12 15:23:37 -0600423
Alex Klein1699fab2022-09-08 08:46:06 -0600424 def test_calls_function(self):
425 """Test calling a registered function."""
426 self.PatchObject(self, "uprev_category_package")
Alex Klein87531182019-08-12 15:23:37 -0600427
Alex Klein1699fab2022-09-08 08:46:06 -0600428 cpv = package_info.SplitCPV("category/package", strict=False)
429 packages.uprev_versioned_package(cpv, [], [], Chroot())
Alex Klein87531182019-08-12 15:23:37 -0600430
Alex Kleinfee86da2023-01-20 18:40:06 -0700431 # TODO(crbug/1065172): Invalid assertion that was previously mocked.
Alex Klein1699fab2022-09-08 08:46:06 -0600432 # patch.assert_called()
Alex Klein87531182019-08-12 15:23:37 -0600433
Alex Klein1699fab2022-09-08 08:46:06 -0600434 def test_unregistered_package(self):
435 """Test calling with an unregistered package."""
436 cpv = package_info.SplitCPV("does-not/exist", strict=False)
Alex Klein87531182019-08-12 15:23:37 -0600437
Alex Klein1699fab2022-09-08 08:46:06 -0600438 with self.assertRaises(packages.UnknownPackageError):
439 packages.uprev_versioned_package(cpv, [], [], Chroot())
Alex Klein87531182019-08-12 15:23:37 -0600440
441
Trent Begin6daa8702020-01-29 14:58:12 -0700442class UprevEbuildFromPinTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600443 """Tests uprev_ebuild_from_pin function"""
Trent Begin315d9d92019-12-03 21:55:53 -0700444
Alex Klein1699fab2022-09-08 08:46:06 -0600445 package = "category/package"
446 version = "1.2.3"
447 new_version = "1.2.4"
448 ebuild_template = "package-%s-r1.ebuild"
449 ebuild = ebuild_template % version
450 unstable_ebuild = "package-9999.ebuild"
451 manifest = "Manifest"
Trent Begin315d9d92019-12-03 21:55:53 -0700452
Alex Klein1699fab2022-09-08 08:46:06 -0600453 def test_uprev_ebuild(self):
454 """Tests uprev of ebuild with version path"""
455 file_layout = (
456 D(self.package, [self.ebuild, self.unstable_ebuild, self.manifest]),
457 )
458 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700459
Alex Klein1699fab2022-09-08 08:46:06 -0600460 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700461
Alex Klein1699fab2022-09-08 08:46:06 -0600462 ebuild_path = os.path.join(package_path, self.ebuild)
463 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000464
Alex Klein1699fab2022-09-08 08:46:06 -0600465 result = uprev_lib.uprev_ebuild_from_pin(
466 package_path, self.new_version, chroot=Chroot()
467 )
468 self.assertEqual(
469 len(result.modified),
470 1,
471 "unexpected number of results: %s" % len(result.modified),
472 )
Trent Begin315d9d92019-12-03 21:55:53 -0700473
Alex Klein1699fab2022-09-08 08:46:06 -0600474 mod = result.modified[0]
475 self.assertEqual(
476 mod.new_version,
477 self.new_version + "-r1",
478 "unexpected version number: %s" % mod.new_version,
479 )
Trent Begin315d9d92019-12-03 21:55:53 -0700480
Alex Klein1699fab2022-09-08 08:46:06 -0600481 old_ebuild_path = os.path.join(
482 package_path, self.ebuild_template % self.version
483 )
484 new_ebuild_path = os.path.join(
485 package_path, self.ebuild_template % self.new_version
486 )
487 manifest_path = os.path.join(package_path, "Manifest")
Trent Begin2e5344f2020-03-02 10:46:55 -0700488
Alex Klein1699fab2022-09-08 08:46:06 -0600489 expected_modified_files = [
490 old_ebuild_path,
491 new_ebuild_path,
492 manifest_path,
493 ]
494 self.assertCountEqual(mod.files, expected_modified_files)
Trent Begin4a11a632020-02-28 12:59:58 -0700495
Alex Klein1699fab2022-09-08 08:46:06 -0600496 self.assertCommandContains(["ebuild", "manifest"])
Trent Begin6daa8702020-01-29 14:58:12 -0700497
Alex Klein1699fab2022-09-08 08:46:06 -0600498 def test_uprev_ebuild_same_version(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700499 """Tests uprev of ebuild with version path with unchanged version.
Fergus Dall2209d0b2020-08-06 11:51:43 +1000500
Alex Klein1699fab2022-09-08 08:46:06 -0600501 This should result in bumping the revision number.
502 """
503 file_layout = (
504 D(self.package, [self.ebuild, self.unstable_ebuild, self.manifest]),
505 )
506 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000507
Alex Klein1699fab2022-09-08 08:46:06 -0600508 package_path = os.path.join(self.tempdir, self.package)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000509
Alex Klein1699fab2022-09-08 08:46:06 -0600510 ebuild_path = os.path.join(package_path, self.ebuild)
511 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000512
Alex Klein1699fab2022-09-08 08:46:06 -0600513 result = uprev_lib.uprev_ebuild_from_pin(
514 package_path, self.version, chroot=Chroot()
515 )
516 self.assertEqual(
517 len(result.modified),
518 1,
519 "unexpected number of results: %s" % len(result.modified),
520 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000521
Alex Klein1699fab2022-09-08 08:46:06 -0600522 mod = result.modified[0]
523 self.assertEqual(
524 mod.new_version,
525 self.version + "-r2",
526 "unexpected version number: %s" % mod.new_version,
527 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000528
Alex Klein1699fab2022-09-08 08:46:06 -0600529 old_ebuild_path = os.path.join(
530 package_path, self.ebuild_template % self.version
531 )
532 new_ebuild_path = os.path.join(
533 package_path, "package-%s-r2.ebuild" % self.version
534 )
535 manifest_path = os.path.join(package_path, "Manifest")
Fergus Dall2209d0b2020-08-06 11:51:43 +1000536
Alex Klein1699fab2022-09-08 08:46:06 -0600537 expected_modified_files = [
538 old_ebuild_path,
539 new_ebuild_path,
540 manifest_path,
541 ]
542 self.assertCountEqual(mod.files, expected_modified_files)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000543
Alex Klein1699fab2022-09-08 08:46:06 -0600544 self.assertCommandContains(["ebuild", "manifest"])
Fergus Dall2209d0b2020-08-06 11:51:43 +1000545
Alex Klein1699fab2022-09-08 08:46:06 -0600546 def test_no_ebuild(self):
547 """Tests assertion is raised if package has no ebuilds"""
548 file_layout = (D(self.package, [self.manifest]),)
549 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700550
Alex Klein1699fab2022-09-08 08:46:06 -0600551 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700552
Alex Klein1699fab2022-09-08 08:46:06 -0600553 with self.assertRaises(uprev_lib.EbuildUprevError):
554 uprev_lib.uprev_ebuild_from_pin(
555 package_path, self.new_version, chroot=Chroot()
556 )
Trent Begin315d9d92019-12-03 21:55:53 -0700557
Alex Klein1699fab2022-09-08 08:46:06 -0600558 def test_multiple_stable_ebuilds(self):
559 """Tests assertion is raised if multiple stable ebuilds are present"""
560 file_layout = (
561 D(
562 self.package,
563 [self.ebuild, self.ebuild_template % "1.2.1", self.manifest],
564 ),
565 )
566 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000567
Alex Klein1699fab2022-09-08 08:46:06 -0600568 package_path = os.path.join(self.tempdir, self.package)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000569
Alex Klein1699fab2022-09-08 08:46:06 -0600570 ebuild_path = os.path.join(package_path, self.ebuild)
571 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000572
Alex Klein1699fab2022-09-08 08:46:06 -0600573 ebuild_path = os.path.join(package_path, self.ebuild_template % "1.2.1")
574 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000575
Alex Klein1699fab2022-09-08 08:46:06 -0600576 with self.assertRaises(uprev_lib.EbuildUprevError):
577 uprev_lib.uprev_ebuild_from_pin(
578 package_path, self.new_version, chroot=Chroot()
579 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000580
Alex Klein1699fab2022-09-08 08:46:06 -0600581 def test_multiple_unstable_ebuilds(self):
582 """Tests assertion is raised if multiple unstable ebuilds are present"""
583 file_layout = (
584 D(
585 self.package,
586 [self.ebuild, self.ebuild_template % "1.2.1", self.manifest],
587 ),
588 )
589 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700590
Alex Klein1699fab2022-09-08 08:46:06 -0600591 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700592
Alex Klein1699fab2022-09-08 08:46:06 -0600593 with self.assertRaises(uprev_lib.EbuildUprevError):
594 uprev_lib.uprev_ebuild_from_pin(
595 package_path, self.new_version, chroot=Chroot()
596 )
Trent Begin315d9d92019-12-03 21:55:53 -0700597
598
Andrew Lamb9563a152019-12-04 11:42:18 -0700599class ReplicatePrivateConfigTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600600 """replicate_private_config tests."""
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700601
Alex Klein1699fab2022-09-08 08:46:06 -0600602 def setUp(self):
603 # Set up fake public and private chromeos-config overlays.
604 private_package_root = (
605 "src/private-overlays/overlay-coral-private/chromeos-base/"
606 "chromeos-config-bsp"
607 )
608 self.public_package_root = (
609 "src/overlays/overlay-coral/chromeos-base/chromeos-config-bsp"
610 )
611 file_layout = (
612 D(
613 os.path.join(private_package_root, "files"),
614 ["build_config.json"],
615 ),
616 D(private_package_root, ["replication_config.jsonpb"]),
617 D(
618 os.path.join(self.public_package_root, "files"),
619 ["build_config.json"],
620 ),
621 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700622
Alex Klein1699fab2022-09-08 08:46:06 -0600623 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700624
Alex Klein1699fab2022-09-08 08:46:06 -0600625 # Private config contains 'a' and 'b' fields.
626 self.private_config_path = os.path.join(
627 private_package_root, "files", "build_config.json"
628 )
629 self.WriteTempFile(
630 self.private_config_path,
631 json.dumps({"chromeos": {"configs": [{"a": 3, "b": 2}]}}),
632 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700633
Alex Kleinfee86da2023-01-20 18:40:06 -0700634 # Public config only contains the 'a' field. Note that the value of 'a'
635 # is 1 in the public config; it will get updated to 3 when the private
636 # config is replicated.
Alex Klein1699fab2022-09-08 08:46:06 -0600637 self.public_config_path = os.path.join(
638 self.public_package_root, "files", "build_config.json"
639 )
640 self.WriteTempFile(
641 self.public_config_path,
642 json.dumps({"chromeos": {"configs": [{"a": 1}]}}),
643 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700644
Alex Klein1699fab2022-09-08 08:46:06 -0600645 # Put a ReplicationConfig JSONPB in the private package. Note that it
646 # specifies only the 'a' field is replicated.
647 self.replication_config_path = os.path.join(
648 self.tempdir, private_package_root, "replication_config.jsonpb"
649 )
650 replication_config = ReplicationConfig(
651 file_replication_rules=[
652 FileReplicationRule(
653 source_path=self.private_config_path,
654 destination_path=self.public_config_path,
655 file_type=FILE_TYPE_JSON,
656 replication_type=REPLICATION_TYPE_FILTER,
657 destination_fields=FieldMask(paths=["a"]),
658 )
659 ]
660 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700661
Alex Klein1699fab2022-09-08 08:46:06 -0600662 osutils.WriteFile(
663 self.replication_config_path,
664 json_format.MessageToJson(replication_config),
665 )
666 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700667
Alex Klein1699fab2022-09-08 08:46:06 -0600668 self.rc.SetDefaultCmdResult(side_effect=self._write_generated_c_files)
Andrew Lamb9563a152019-12-04 11:42:18 -0700669
Alex Klein1699fab2022-09-08 08:46:06 -0600670 def _write_generated_c_files(self, *_args, **_kwargs):
671 """Write fake generated C files to the public output dir.
Andrew Lamb9563a152019-12-04 11:42:18 -0700672
Alex Kleinfee86da2023-01-20 18:40:06 -0700673 Note that this function accepts args and kwargs so it can be used as a
674 side effect.
Alex Klein1699fab2022-09-08 08:46:06 -0600675 """
676 output_dir = os.path.join(self.public_package_root, "files")
677 self.WriteTempFile(os.path.join(output_dir, "config.c"), "")
678 self.WriteTempFile(os.path.join(output_dir, "ec_config.c"), "")
679 self.WriteTempFile(os.path.join(output_dir, "ec_config.h"), "")
Andrew Lamb9563a152019-12-04 11:42:18 -0700680
Alex Klein1699fab2022-09-08 08:46:06 -0600681 def _write_incorrect_generated_c_files(self, *_args, **_kwargs):
682 """Similar to _write_generated_c_files, with an expected file missing.
Andrew Lamb9563a152019-12-04 11:42:18 -0700683
Alex Kleinfee86da2023-01-20 18:40:06 -0700684 Note that this function accepts args and kwargs so it can be used as a
685 side effect.
Alex Klein1699fab2022-09-08 08:46:06 -0600686 """
687 output_dir = os.path.join(self.public_package_root, "files")
688 self.WriteTempFile(os.path.join(output_dir, "config.c"), "")
689 self.WriteTempFile(os.path.join(output_dir, "ec_config.c"), "")
Andrew Lamb9563a152019-12-04 11:42:18 -0700690
Alex Klein1699fab2022-09-08 08:46:06 -0600691 def test_replicate_private_config(self):
692 """Basic replication test."""
693 refs = [
694 GitRef(
695 path="/chromeos/overlays/overlay-coral-private",
696 ref="main",
697 revision="123",
698 )
699 ]
700 chroot = Chroot()
701 result = packages.replicate_private_config(
702 _build_targets=None, refs=refs, chroot=chroot
703 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700704
Alex Klein1699fab2022-09-08 08:46:06 -0600705 self.assertCommandContains(
706 [
707 "cros_config_schema",
708 "-m",
709 os.path.join(
710 constants.CHROOT_SOURCE_ROOT, self.public_config_path
711 ),
712 "-g",
713 os.path.join(
714 constants.CHROOT_SOURCE_ROOT,
715 self.public_package_root,
716 "files",
717 ),
718 "-f",
719 '"TRUE"',
720 ],
721 enter_chroot=True,
722 chroot_args=chroot.get_enter_args(),
723 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700724
Alex Klein1699fab2022-09-08 08:46:06 -0600725 self.assertEqual(len(result.modified), 1)
726 # The public build_config.json and generated C files were modified.
727 expected_modified_files = [
728 os.path.join(self.tempdir, self.public_config_path),
729 os.path.join(
730 self.tempdir, self.public_package_root, "files", "config.c"
731 ),
732 os.path.join(
733 self.tempdir, self.public_package_root, "files", "ec_config.c"
734 ),
735 os.path.join(
736 self.tempdir, self.public_package_root, "files", "ec_config.h"
737 ),
738 ]
739 self.assertEqual(result.modified[0].files, expected_modified_files)
740 self.assertEqual(result.modified[0].new_version, "123")
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700741
Alex Kleinfee86da2023-01-20 18:40:06 -0700742 # The update from the private build_config.json was copied to the
743 # public. Note that only the 'a' field is present, as per
744 # destination_fields.
Alex Klein1699fab2022-09-08 08:46:06 -0600745 self.assertEqual(
746 json.loads(self.ReadTempFile(self.public_config_path)),
747 {"chromeos": {"configs": [{"a": 3}]}},
748 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700749
Alex Klein1699fab2022-09-08 08:46:06 -0600750 def test_replicate_private_config_no_build_config(self):
751 """If there is no build config, don't generate C files."""
Alex Kleinfee86da2023-01-20 18:40:06 -0700752 # Modify the replication config to write to "other_config.json" instead
753 # of "build_config.json"
Alex Klein1699fab2022-09-08 08:46:06 -0600754 modified_destination_path = self.public_config_path.replace(
755 "build_config", "other_config"
756 )
757 replication_config = ReplicationConfig(
758 file_replication_rules=[
759 FileReplicationRule(
760 source_path=self.private_config_path,
761 destination_path=modified_destination_path,
762 file_type=FILE_TYPE_JSON,
763 replication_type=REPLICATION_TYPE_FILTER,
764 destination_fields=FieldMask(paths=["a"]),
765 )
766 ]
767 )
768 osutils.WriteFile(
769 self.replication_config_path,
770 json_format.MessageToJson(replication_config),
771 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700772
Alex Klein1699fab2022-09-08 08:46:06 -0600773 refs = [
774 GitRef(
775 path="/chromeos/overlays/overlay-coral-private",
776 ref="main",
777 revision="123",
778 )
779 ]
780 result = packages.replicate_private_config(
781 _build_targets=None, refs=refs, chroot=Chroot()
782 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700783
Alex Klein1699fab2022-09-08 08:46:06 -0600784 self.assertEqual(len(result.modified), 1)
785 self.assertEqual(
786 result.modified[0].files,
787 [os.path.join(self.tempdir, modified_destination_path)],
788 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700789
Alex Klein1699fab2022-09-08 08:46:06 -0600790 def test_replicate_private_config_multiple_build_configs(self):
791 """An error is thrown if there is more than one build config."""
792 replication_config = ReplicationConfig(
793 file_replication_rules=[
794 FileReplicationRule(
795 source_path=self.private_config_path,
796 destination_path=self.public_config_path,
797 file_type=FILE_TYPE_JSON,
798 replication_type=REPLICATION_TYPE_FILTER,
799 destination_fields=FieldMask(paths=["a"]),
800 ),
801 FileReplicationRule(
802 source_path=self.private_config_path,
803 destination_path=self.public_config_path,
804 file_type=FILE_TYPE_JSON,
805 replication_type=REPLICATION_TYPE_FILTER,
806 destination_fields=FieldMask(paths=["a"]),
807 ),
808 ]
809 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700810
Alex Klein1699fab2022-09-08 08:46:06 -0600811 osutils.WriteFile(
812 self.replication_config_path,
813 json_format.MessageToJson(replication_config),
814 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700815
Alex Klein1699fab2022-09-08 08:46:06 -0600816 refs = [
817 GitRef(
818 path="/chromeos/overlays/overlay-coral-private",
819 ref="main",
820 revision="123",
821 )
822 ]
823 with self.assertRaisesRegex(
824 ValueError,
825 "Expected at most one build_config.json destination path.",
826 ):
827 packages.replicate_private_config(
828 _build_targets=None, refs=refs, chroot=Chroot()
829 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700830
Alex Klein1699fab2022-09-08 08:46:06 -0600831 def test_replicate_private_config_generated_files_incorrect(self):
832 """An error is thrown if generated C files are missing."""
833 self.rc.SetDefaultCmdResult(
834 side_effect=self._write_incorrect_generated_c_files
835 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700836
Alex Klein1699fab2022-09-08 08:46:06 -0600837 refs = [
838 GitRef(
839 path="/chromeos/overlays/overlay-coral-private",
840 ref="main",
841 revision="123",
842 )
843 ]
844 chroot = Chroot()
Andrew Lamb9563a152019-12-04 11:42:18 -0700845
Alex Klein1699fab2022-09-08 08:46:06 -0600846 with self.assertRaisesRegex(
847 packages.GeneratedCrosConfigFilesError,
848 "Expected to find generated C files",
849 ):
850 packages.replicate_private_config(
851 _build_targets=None, refs=refs, chroot=chroot
852 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700853
Alex Klein1699fab2022-09-08 08:46:06 -0600854 def test_replicate_private_config_wrong_number_of_refs(self):
855 """An error is thrown if there is not exactly one ref."""
856 with self.assertRaisesRegex(ValueError, "Expected exactly one ref"):
857 packages.replicate_private_config(
858 _build_targets=None, refs=[], chroot=None
859 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700860
Alex Klein1699fab2022-09-08 08:46:06 -0600861 with self.assertRaisesRegex(ValueError, "Expected exactly one ref"):
862 refs = [
863 GitRef(path="a", ref="main", revision="1"),
864 GitRef(path="a", ref="main", revision="2"),
865 ]
866 packages.replicate_private_config(
867 _build_targets=None, refs=refs, chroot=None
868 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700869
Alex Klein1699fab2022-09-08 08:46:06 -0600870 def test_replicate_private_config_replication_config_missing(self):
871 """An error is thrown if there is not a replication config."""
872 os.remove(self.replication_config_path)
873 with self.assertRaisesRegex(
874 ValueError,
875 "Expected ReplicationConfig missing at %s"
876 % self.replication_config_path,
877 ):
878 refs = [
879 GitRef(
880 path="/chromeos/overlays/overlay-coral-private",
881 ref="main",
882 revision="123",
883 )
884 ]
885 packages.replicate_private_config(
886 _build_targets=None, refs=refs, chroot=None
887 )
Andrew Lambe836f222019-12-09 12:27:38 -0700888
Alex Klein1699fab2022-09-08 08:46:06 -0600889 def test_replicate_private_config_wrong_git_ref_path(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700890 """Git ref that doesn't point to a private overlay throws error."""
Alex Klein1699fab2022-09-08 08:46:06 -0600891 with self.assertRaisesRegex(
892 ValueError, "ref.path must match the pattern"
893 ):
894 refs = [GitRef(path="a/b/c", ref="main", revision="123")]
895 packages.replicate_private_config(
896 _build_targets=None, refs=refs, chroot=None
897 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700898
899
Alex Klein5caab872021-09-10 11:44:37 -0600900class GetBestVisibleTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600901 """get_best_visible tests."""
David Burger1e0fe232019-07-01 14:52:07 -0600902
Alex Klein1699fab2022-09-08 08:46:06 -0600903 def test_empty_atom_fails(self):
904 """Test empty atom raises an error."""
905 with self.assertRaises(AssertionError):
906 packages.get_best_visible("")
Alex Kleinda39c6d2019-09-16 14:36:36 -0600907
908
Alex Klein149fd3b2019-12-16 16:01:05 -0700909class HasPrebuiltTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600910 """has_prebuilt tests."""
Alex Kleinda39c6d2019-09-16 14:36:36 -0600911
Alex Klein1699fab2022-09-08 08:46:06 -0600912 def test_empty_atom_fails(self):
913 """Test an empty atom results in an error."""
914 with self.assertRaises(AssertionError):
915 packages.has_prebuilt("")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600916
Alex Klein1699fab2022-09-08 08:46:06 -0600917 def test_use_flags(self):
918 """Test use flags get propagated correctly."""
919 # We don't really care about the result, just the env handling.
920 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
921 # Ignore any flags that may be in the environment.
922 self.PatchObject(os.environ, "get", return_value="")
Alex Klein149fd3b2019-12-16 16:01:05 -0700923
Alex Klein1699fab2022-09-08 08:46:06 -0600924 packages.has_prebuilt("cat/pkg-1.2.3", useflags="useflag")
925 patch.assert_called_with(
926 "cat/pkg-1.2.3", board=None, extra_env={"USE": "useflag"}
927 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700928
Alex Klein1699fab2022-09-08 08:46:06 -0600929 def test_env_use_flags(self):
930 """Test env use flags get propagated correctly with passed useflags."""
931 # We don't really care about the result, just the env handling.
932 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
933 # Add some flags to the environment.
934 existing_flags = "already set flags"
935 self.PatchObject(os.environ, "get", return_value=existing_flags)
Alex Klein149fd3b2019-12-16 16:01:05 -0700936
Alex Klein1699fab2022-09-08 08:46:06 -0600937 new_flags = "useflag"
938 packages.has_prebuilt("cat/pkg-1.2.3", useflags=new_flags)
939 expected = "%s %s" % (existing_flags, new_flags)
940 patch.assert_called_with(
941 "cat/pkg-1.2.3", board=None, extra_env={"USE": expected}
942 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700943
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600944
945class AndroidVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600946 """Tests getting android versions."""
Michael Mortensen14960d02019-10-18 07:53:59 -0600947
Alex Klein1699fab2022-09-08 08:46:06 -0600948 def setUp(self):
949 package_result = [
950 "chromeos-base/android-container-nyc-4717008-r1",
951 "chromeos-base/update_engine-0.0.3-r3408",
952 ]
953 self.PatchObject(
954 portage_util, "GetPackageDependencies", return_value=package_result
955 )
956 self.board = "board"
957 self.PatchObject(
958 portage_util,
959 "FindEbuildForBoardPackage",
960 return_value="chromeos-base/android-container-nyc",
961 )
962 FakeEnvironment = {
963 "ARM_TARGET": "3-linux-target",
964 }
965 self.PatchObject(
966 osutils, "SourceEnvironment", return_value=FakeEnvironment
967 )
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600968
Alex Kleinfee86da2023-01-20 18:40:06 -0700969 # Clear the LRU cache for the function. We mock the function that
970 # provides the data this function processes to produce its result, so we
971 # need to clear it manually.
Alex Klein1699fab2022-09-08 08:46:06 -0600972 packages.determine_android_package.cache_clear()
Alex Klein68a28712021-11-08 11:08:30 -0700973
Alex Klein1699fab2022-09-08 08:46:06 -0600974 def test_determine_android_version(self):
975 """Tests that a valid android version is returned."""
976 version = packages.determine_android_version(self.board)
977 self.assertEqual(version, "4717008")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600978
Alex Klein1699fab2022-09-08 08:46:06 -0600979 def test_determine_android_version_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700980 """Test None is returned for version when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -0600981 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
982 self.PatchObject(
983 portage_util, "GetPackageDependencies", return_value=package_result
984 )
985 version = packages.determine_android_version(self.board)
986 self.assertEqual(version, None)
Michael Mortensenedf76532019-10-16 14:22:37 -0600987
Alex Klein1699fab2022-09-08 08:46:06 -0600988 def test_determine_android_branch(self):
989 """Tests that a valid android branch is returned."""
990 branch = packages.determine_android_branch(self.board)
991 self.assertEqual(branch, "3")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600992
Alex Klein1699fab2022-09-08 08:46:06 -0600993 def test_determine_android_branch_64bit_targets(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700994 """Tests a valid android branch is returned with only 64bit targets."""
Alex Klein1699fab2022-09-08 08:46:06 -0600995 self.PatchObject(
996 osutils,
997 "SourceEnvironment",
998 return_value={"ARM64_TARGET": "3-linux-target"},
999 )
1000 branch = packages.determine_android_branch(self.board)
1001 self.assertEqual(branch, "3")
Federico 'Morg' Pareschicd9165a2020-05-29 09:45:55 +09001002
Alex Klein1699fab2022-09-08 08:46:06 -06001003 def test_determine_android_branch_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001004 """Tests a None is returned for branch when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -06001005 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
1006 self.PatchObject(
1007 portage_util, "GetPackageDependencies", return_value=package_result
1008 )
1009 branch = packages.determine_android_branch(self.board)
1010 self.assertEqual(branch, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001011
Alex Klein1699fab2022-09-08 08:46:06 -06001012 def test_determine_android_target(self):
1013 """Tests that a valid android target is returned."""
1014 target = packages.determine_android_target(self.board)
1015 self.assertEqual(target, "cheets")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001016
Alex Klein1699fab2022-09-08 08:46:06 -06001017 def test_determine_android_target_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001018 """Tests a None is returned for target when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -06001019 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
1020 self.PatchObject(
1021 portage_util, "GetPackageDependencies", return_value=package_result
1022 )
1023 target = packages.determine_android_target(self.board)
1024 self.assertEqual(target, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001025
Alex Klein1699fab2022-09-08 08:46:06 -06001026 def test_determine_android_version_handle_exception(self):
1027 """Tests handling RunCommandError inside determine_android_version."""
Alex Kleinfee86da2023-01-20 18:40:06 -07001028 # Mock what happens when portage returns that bubbles up (via
1029 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001030 self.PatchObject(
1031 portage_util,
1032 "GetPackageDependencies",
1033 side_effect=cros_build_lib.RunCommandError("error"),
1034 )
1035 target = packages.determine_android_version(self.board)
1036 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -06001037
Alex Klein1699fab2022-09-08 08:46:06 -06001038 def test_determine_android_package_handle_exception(self):
1039 """Tests handling RunCommandError inside determine_android_package."""
Alex Kleinfee86da2023-01-20 18:40:06 -07001040 # Mock what happens when portage returns that bubbles up (via
1041 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001042 self.PatchObject(
1043 portage_util,
1044 "GetPackageDependencies",
1045 side_effect=cros_build_lib.RunCommandError("error"),
1046 )
1047 target = packages.determine_android_package(self.board)
1048 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -06001049
Alex Klein1699fab2022-09-08 08:46:06 -06001050 def test_determine_android_package_callers_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001051 """Tests RunCommandError caught by determine_android_package callers."""
1052 # Mock what happens when portage returns that bubbles up (via
1053 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001054 self.PatchObject(
1055 portage_util,
1056 "GetPackageDependencies",
1057 side_effect=cros_build_lib.RunCommandError("error"),
1058 )
1059 # Verify that target is None, as expected.
1060 target = packages.determine_android_package(self.board)
1061 self.assertEqual(target, None)
1062 # determine_android_branch calls determine_android_package
1063 branch = packages.determine_android_branch(self.board)
1064 self.assertEqual(branch, None)
1065 # determine_android_target calls determine_android_package
1066 target = packages.determine_android_target(self.board)
1067 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001068
Michael Mortensene0f4b542019-10-24 15:30:23 -06001069
Alex Klein1699fab2022-09-08 08:46:06 -06001070@pytest.mark.usefixtures("testcase_caplog", "testcase_monkeypatch")
Michael Mortensende716a12020-05-15 11:27:00 -06001071class FindFingerprintsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001072 """Tests for find_fingerprints."""
Michael Mortensende716a12020-05-15 11:27:00 -06001073
Alex Klein1699fab2022-09-08 08:46:06 -06001074 def setUp(self):
1075 self.board = "test-board"
1076 # Create cheets-fingerprints.txt based on tempdir/src...
1077 self.fingerprint_contents = (
1078 "google/test-board/test-board_cheets"
1079 ":9/R99-12345.0.9999/123456:user/release-keys"
1080 )
1081 fingerprint_path = os.path.join(
1082 self.tempdir,
1083 "src/build/images/test-board/latest/cheets-fingerprint.txt",
1084 )
Brian Norris4f251e42023-03-09 15:53:26 -08001085 self.chroot = Chroot(
1086 path=self.tempdir / "chroot", out_path=self.tempdir / "out"
1087 )
Alex Klein1699fab2022-09-08 08:46:06 -06001088 osutils.WriteFile(
1089 fingerprint_path, self.fingerprint_contents, makedirs=True
1090 )
Michael Mortensende716a12020-05-15 11:27:00 -06001091
Alex Klein1699fab2022-09-08 08:46:06 -06001092 def test_find_fingerprints_with_test_path(self):
1093 """Tests get_firmware_versions with mocked output."""
1094 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1095 build_target = build_target_lib.BuildTarget(self.board)
1096 result = packages.find_fingerprints(build_target)
1097 self.assertEqual(result, [self.fingerprint_contents])
1098 self.assertIn("Reading fingerprint file", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001099
Alex Klein1699fab2022-09-08 08:46:06 -06001100 def test_find_fingerprints(self):
1101 """Tests get_firmware_versions with mocked output."""
1102 # Use board name whose path for fingerprint file does not exist.
1103 # Verify that fingerprint file is not found and None is returned.
1104 build_target = build_target_lib.BuildTarget("wrong-boardname")
1105 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1106 result = packages.find_fingerprints(build_target)
1107 self.assertEqual(result, [])
1108 self.assertIn("Fingerprint file not found", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001109
1110
Michael Mortensen59e30872020-05-18 14:12:49 -06001111class GetAllFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001112 """Tests for get_firmware_versions."""
Michael Mortensen59e30872020-05-18 14:12:49 -06001113
Alex Klein1699fab2022-09-08 08:46:06 -06001114 def setUp(self):
1115 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001116 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001117 self.rc.SetDefaultCmdResult(
1118 stdout="""
Michael Mortensen59e30872020-05-18 14:12:49 -06001119
1120flashrom(8): 68935ee2fcfcffa47af81b966269cd2b */build/reef/usr/sbin/flashrom
1121 ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=e102cc98d45300b50088999d53775acbeff407dc, stripped
1122 0.9.9 : bbb2d6a : Jul 28 2017 15:12:34 UTC
1123
1124Model: reef
1125BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1126BIOS version: Google_Reef.9042.87.1
1127BIOS (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
1128BIOS (RW) version: Google_Reef.9042.110.0
1129EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1130EC version: reef_v1.1.5900-ab1ee51
1131EC (RW) version: reef_v1.1.5909-bd1f0c9
1132
1133Model: pyro
1134BIOS image: 9e62447ebf22a724a4a835018ab6234e */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/image.bin
1135BIOS version: Google_Pyro.9042.87.1
1136BIOS (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
1137BIOS (RW) version: Google_Pyro.9042.110.0
1138EC image: 44b93ed591733519e752e05aa0529eb5 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/ec.bin
1139EC version: pyro_v1.1.5900-ab1ee51
1140EC (RW) version: pyro_v1.1.5909-bd1f0c9
1141
1142Model: snappy
1143BIOS image: 3ab63ff080596bd7de4e7619f003bb64 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/image.bin
1144BIOS version: Google_Snappy.9042.110.0
1145EC image: c4db159e84428391d2ee25368c5fe5b6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/ec.bin
1146EC version: snappy_v1.1.5909-bd1f0c9
1147
1148Model: sand
1149BIOS image: 387da034a4f0a3f53e278ebfdcc2a412 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/image.bin
1150BIOS version: Google_Sand.9042.110.0
1151EC image: 411562e0589dacec131f5fdfbe95a561 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/ec.bin
1152EC version: sand_v1.1.5909-bd1f0c9
1153
1154Model: electro
1155BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1156BIOS version: Google_Reef.9042.87.1
1157BIOS (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
1158BIOS (RW) version: Google_Reef.9042.110.0
1159EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1160EC version: reef_v1.1.5900-ab1ee51
1161EC (RW) version: reef_v1.1.5909-bd1f0c9
1162
1163Package Content:
1164612e7bb6ed1fb0a05abf2ebdc834c18b *./updater4.sh
11650eafbee07282315829d0f42135ec7c0c *./gbb_utility
11666074e3ca424cb30a67c378c1d9681f9c *./mosys
116768935ee2fcfcffa47af81b966269cd2b *./flashrom
11680eafbee07282315829d0f42135ec7c0c *./dump_fmap
1169490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
117060899148600b8673ddb711faa55aee40 *./common.sh
11713c3a99346d1ca1273cbcd86c104851ff *./shflags
1172de7ce035e1f82a89f8909d888ee402c0 *./crosutil.sh
1173f9334372bdb9036ba09a6fd9bf30e7a2 *./crossystem
117422257a8d5f0adc1f50a1916c3a4a35dd *./models/reef/ec.bin
1175faf12dbb7cdaf21ce153bdffb67841fd *./models/reef/bios.bin
1176c9bbb417b7921b85a7ed999ee42f550e *./models/reef/setvars.sh
117729823d46f1ec1491ecacd7b830fd2686 *./models/pyro/ec.bin
11782320463aba8b22eb5ea836f094d281b3 *./models/pyro/bios.bin
117981614833ad77c9cd093360ba7bea76b8 *./models/pyro/setvars.sh
1180411562e0589dacec131f5fdfbe95a561 *./models/sand/ec.bin
1181387da034a4f0a3f53e278ebfdcc2a412 *./models/sand/bios.bin
1182fcd8cb0ac0e2ed6be220aaae435d43ff *./models/sand/setvars.sh
1183c4db159e84428391d2ee25368c5fe5b6 *./models/snappy/ec.bin
11843ab63ff080596bd7de4e7619f003bb64 *./models/snappy/bios.bin
1185fe5d699f2e9e4a7de031497953313dbd *./models/snappy/setvars.sh
118679aabd7cd8a215a54234c53d7bb2e6fb *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001187"""
1188 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001189 # pylint: enable=line-too-long
Michael Mortensen59e30872020-05-18 14:12:49 -06001190
Alex Klein1699fab2022-09-08 08:46:06 -06001191 def test_get_firmware_versions(self):
1192 """Tests get_firmware_versions with mocked output."""
1193 build_target = build_target_lib.BuildTarget(self.board)
1194 result = packages.get_all_firmware_versions(build_target)
1195 self.assertEqual(len(result), 5)
1196 self.assertEqual(
1197 result["reef"],
1198 packages.FirmwareVersions(
1199 "reef",
1200 "Google_Reef.9042.87.1",
1201 "Google_Reef.9042.110.0",
1202 "reef_v1.1.5900-ab1ee51",
1203 "reef_v1.1.5909-bd1f0c9",
1204 ),
1205 )
1206 self.assertEqual(
1207 result["pyro"],
1208 packages.FirmwareVersions(
1209 "pyro",
1210 "Google_Pyro.9042.87.1",
1211 "Google_Pyro.9042.110.0",
1212 "pyro_v1.1.5900-ab1ee51",
1213 "pyro_v1.1.5909-bd1f0c9",
1214 ),
1215 )
1216 self.assertEqual(
1217 result["snappy"],
1218 packages.FirmwareVersions(
1219 "snappy",
1220 "Google_Snappy.9042.110.0",
1221 None,
1222 "snappy_v1.1.5909-bd1f0c9",
1223 None,
1224 ),
1225 )
1226 self.assertEqual(
1227 result["sand"],
1228 packages.FirmwareVersions(
1229 "sand",
1230 "Google_Sand.9042.110.0",
1231 None,
1232 "sand_v1.1.5909-bd1f0c9",
1233 None,
1234 ),
1235 )
1236 self.assertEqual(
1237 result["electro"],
1238 packages.FirmwareVersions(
1239 "electro",
1240 "Google_Reef.9042.87.1",
1241 "Google_Reef.9042.110.0",
1242 "reef_v1.1.5900-ab1ee51",
1243 "reef_v1.1.5909-bd1f0c9",
1244 ),
1245 )
Michael Mortensen59e30872020-05-18 14:12:49 -06001246
Alex Klein1699fab2022-09-08 08:46:06 -06001247 def test_get_firmware_versions_error(self):
1248 """Tests get_firmware_versions with no output."""
1249 # Throw an exception when running the command.
Mike Frysinger5d85a542023-07-06 16:05:54 -04001250 self.rc.SetDefaultCmdResult(returncode=1)
Alex Klein1699fab2022-09-08 08:46:06 -06001251 build_target = build_target_lib.BuildTarget(self.board)
1252 result = packages.get_all_firmware_versions(build_target)
1253 self.assertEqual(result, {})
Benjamin Shai12c767e2022-01-12 15:17:44 +00001254
Michael Mortensen59e30872020-05-18 14:12:49 -06001255
Michael Mortensen71ef5682020-05-07 14:29:24 -06001256class GetFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001257 """Tests for get_firmware_versions."""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001258
Alex Klein1699fab2022-09-08 08:46:06 -06001259 def setUp(self):
1260 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001261 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001262 self.rc.SetDefaultCmdResult(
1263 stdout="""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001264
1265flashrom(8): a8f99c2e61e7dc09c4b25ef5a76ef692 */build/kevin/usr/sbin/flashrom
1266 ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.d
1267 0.9.4 : 860875a : Apr 10 2017 23:54:29 UTC
1268
1269BIOS image: 6b5b855a0b8fd1657546d1402c15b206 *chromeos-firmware-kevin-0.0.1/.dist/kevin_fw_8785.178.0.n
1270BIOS version: Google_Kevin.8785.178.0
1271EC image: 1ebfa9518e6cac0558a80b7ab2f5b489 *chromeos-firmware-kevin-0.0.1/.dist/kevin_ec_8785.178.0.n
1272EC version:kevin_v1.10.184-459421c
1273
1274Package Content:
1275a8f99c2e61e7dc09c4b25ef5a76ef692 *./flashrom
12763c3a99346d1ca1273cbcd86c104851ff *./shflags
1277457a8dc8546764affc9700f8da328d23 *./dump_fmap
1278c392980ddb542639edf44a965a59361a *./updater5.sh
1279490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
12806b5b855a0b8fd1657546d1402c15b206 *./bios.bin
12817b5bef0d2da90c23ff2e157250edf0fa *./crosutil.sh
1282d78722e4f1a0dc2d8c3d6b0bc7010ae3 *./crossystem
1283457a8dc8546764affc9700f8da328d23 *./gbb_utility
12841ebfa9518e6cac0558a80b7ab2f5b489 *./ec.bin
1285c98ca54db130886142ad582a58e90ddc *./common.sh
12865ba978bdec0f696f47f0f0de90936880 *./mosys
1287312e8ee6122057f2a246d7bcf1572f49 *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001288"""
1289 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001290 # pylint: enable=line-too-long
Michael Mortensen71ef5682020-05-07 14:29:24 -06001291
Alex Klein1699fab2022-09-08 08:46:06 -06001292 def test_get_firmware_versions(self):
1293 """Tests get_firmware_versions with mocked output."""
1294 build_target = build_target_lib.BuildTarget(self.board)
1295 result = packages.get_firmware_versions(build_target)
1296 versions = packages.FirmwareVersions(
1297 None,
1298 "Google_Kevin.8785.178.0",
1299 None,
1300 "kevin_v1.10.184-459421c",
1301 None,
1302 )
1303 self.assertEqual(result, versions)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001304
1305
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001306class DetermineKernelVersionTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001307 """Tests for determine_kernel_version."""
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001308
Alex Klein1699fab2022-09-08 08:46:06 -06001309 def setUp(self):
1310 self.board = "test-board"
1311 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001312
Alex Klein1699fab2022-09-08 08:46:06 -06001313 def test_determine_kernel_version(self):
1314 """Tests that a valid kernel version is returned."""
Lizzy Presland0b978e62022-09-09 16:55:29 +00001315 kernel_candidates = [
1316 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
Alex Klein1699fab2022-09-08 08:46:06 -06001317 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001318 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1319 "sys-kernel/upstream-kernel-next-9999",
1320 "sys-kernel/socfpga-kernel-4.20-r34",
Alex Klein1699fab2022-09-08 08:46:06 -06001321 ]
1322 self.PatchObject(
Lizzy Presland0b978e62022-09-09 16:55:29 +00001323 portage_util,
1324 "GetFlattenedDepsForPackage",
1325 return_value=kernel_candidates,
1326 )
1327
1328 installed_pkgs = [
1329 "sys-kernel/linux-firmware-0.0.1-r594",
1330 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1331 "virtual/linux-sources-1-r30",
1332 ]
1333 self.PatchObject(
1334 portage_util,
1335 "GetPackageDependencies",
1336 return_value=installed_pkgs,
Alex Klein1699fab2022-09-08 08:46:06 -06001337 )
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001338
Alex Klein1699fab2022-09-08 08:46:06 -06001339 result = packages.determine_kernel_version(self.build_target)
1340 self.assertEqual(result, "4.4.223-r2209")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001341
Lizzy Presland0b978e62022-09-09 16:55:29 +00001342 def test_determine_kernel_version_ignores_exact_duplicates(self):
1343 """Tests that multiple results for candidates is ignored."""
1344 # Depgraph is evaluated for version as well as revision, so graph will
1345 # return all results twice.
1346 kernel_candidates = [
1347 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1348 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1349 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1350 "sys-kernel/upstream-kernel-next-9999",
1351 "sys-kernel/socfpga-kernel-4.20-r34",
1352 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1353 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1354 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1355 "sys-kernel/upstream-kernel-next-9999",
1356 "sys-kernel/socfpga-kernel-4.20-r34",
1357 ]
1358 self.PatchObject(
1359 portage_util,
1360 "GetFlattenedDepsForPackage",
1361 return_value=kernel_candidates,
1362 )
1363
1364 installed_pkgs = [
1365 "sys-kernel/linux-firmware-0.0.1-r594",
1366 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1367 "virtual/linux-sources-1-r30",
1368 ]
Alex Klein1699fab2022-09-08 08:46:06 -06001369 self.PatchObject(
1370 portage_util,
1371 "GetPackageDependencies",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001372 return_value=installed_pkgs,
1373 )
1374
1375 result = packages.determine_kernel_version(self.build_target)
1376 self.assertEqual(result, "4.4.223-r2209")
1377
1378 def test_determine_kernel_version_ignores_virtual_package(self):
1379 """Tests that top-level package is ignored as potential kernel pkg."""
1380 # Depgraph results include the named package at level 0 as well as its
1381 # first-order dependencies, so verify that the virtual package is not
1382 # included as a kernel package.
1383 kernel_candidates = [
1384 "virtual/linux-sources-1",
1385 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1386 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1387 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1388 "sys-kernel/upstream-kernel-next-9999",
1389 "sys-kernel/socfpga-kernel-4.20-r34",
1390 "virtual/linux-sources-1-r30",
1391 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1392 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1393 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1394 "sys-kernel/upstream-kernel-next-9999",
1395 "sys-kernel/socfpga-kernel-4.20-r34",
1396 ]
1397 self.PatchObject(
1398 portage_util,
1399 "GetFlattenedDepsForPackage",
1400 return_value=kernel_candidates,
1401 )
1402
1403 installed_pkgs = [
1404 "sys-kernel/linux-firmware-0.0.1-r594",
1405 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1406 "virtual/linux-sources-1-r30",
1407 ]
1408 self.PatchObject(
1409 portage_util,
1410 "GetPackageDependencies",
1411 return_value=installed_pkgs,
1412 )
1413
1414 result = packages.determine_kernel_version(self.build_target)
1415 self.assertEqual(result, "4.4.223-r2209")
1416
1417 def test_determine_kernel_version_too_many(self):
1418 """Tests that an exception is thrown with too many matching packages."""
1419 package_result = [
1420 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1421 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1422 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1423 "sys-kernel/upstream-kernel-next-9999",
1424 "sys-kernel/socfpga-kernel-4.20-r34",
1425 ]
1426 self.PatchObject(
1427 portage_util,
1428 "GetFlattenedDepsForPackage",
1429 return_value=package_result,
1430 )
1431
1432 installed_pkgs = [
1433 "sys-kernel/linux-firmware-0.0.1-r594",
1434 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1435 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1436 "virtual/linux-sources-1-r30",
1437 ]
1438 self.PatchObject(
1439 portage_util,
1440 "GetPackageDependencies",
1441 return_value=installed_pkgs,
1442 )
1443
1444 with self.assertRaises(packages.KernelVersionError):
1445 packages.determine_kernel_version(self.build_target)
1446
1447 def test_determine_kernel_version_no_kernel_match(self):
1448 """Tests that an exception is thrown with 0-sized intersection."""
1449 package_result = [
1450 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1451 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1452 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1453 "sys-kernel/upstream-kernel-next-9999",
1454 ]
1455 self.PatchObject(
1456 portage_util,
1457 "GetFlattenedDepsForPackage",
1458 return_value=package_result,
1459 )
1460
1461 installed_pkgs = [
1462 "sys-kernel/linux-firmware-0.0.1-r594",
1463 "sys-kernel/socfpga-kernel-4.20-r34",
1464 "virtual/linux-sources-1-r30",
1465 ]
1466 self.PatchObject(
1467 portage_util,
1468 "GetPackageDependencies",
1469 return_value=installed_pkgs,
1470 )
1471
1472 with self.assertRaises(packages.KernelVersionError):
1473 packages.determine_kernel_version(self.build_target)
1474
1475 def test_determine_kernel_version_exception(self):
1476 """Tests that portage_util exceptions result in returning empty str."""
1477 self.PatchObject(
1478 portage_util,
1479 "GetFlattenedDepsForPackage",
Alex Klein1699fab2022-09-08 08:46:06 -06001480 side_effect=cros_build_lib.RunCommandError("error"),
1481 )
1482 result = packages.determine_kernel_version(self.build_target)
Lizzy Presland0b978e62022-09-09 16:55:29 +00001483 self.assertEqual(result, "")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001484
Alex Klein627e04c2021-11-10 15:56:47 -07001485
Michael Mortensenc2615b72019-10-15 08:12:24 -06001486class ChromeVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001487 """Tests getting chrome version."""
Michael Mortensen14960d02019-10-18 07:53:59 -06001488
Alex Klein1699fab2022-09-08 08:46:06 -06001489 def setUp(self):
1490 self.build_target = build_target_lib.BuildTarget("board")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001491
Alex Klein1699fab2022-09-08 08:46:06 -06001492 def test_determine_chrome_version(self):
1493 """Tests that a valid chrome version is returned."""
1494 # Mock PortageqBestVisible to return a valid chrome version string.
1495 r1_cpf = "chromeos-base/chromeos-chrome-78.0.3900.0_rc-r1"
1496 r1_cpv = package_info.SplitCPV(r1_cpf)
1497 self.PatchObject(
1498 portage_util, "PortageqBestVisible", return_value=r1_cpv
1499 )
Michael Mortensenc2615b72019-10-15 08:12:24 -06001500
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001501 chrome_version = packages.determine_package_version(
1502 constants.CHROME_CP, self.build_target
1503 )
Alex Klein1699fab2022-09-08 08:46:06 -06001504 version_numbers = chrome_version.split(".")
1505 self.assertEqual(len(version_numbers), 4)
1506 self.assertEqual(int(version_numbers[0]), 78)
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001507
Alex Klein1699fab2022-09-08 08:46:06 -06001508 def test_determine_chrome_version_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001509 # Mock what happens when portage throws an exception that bubbles up
1510 # (via RunCommand)inside portage_util.PortageqBestVisible.
Alex Klein1699fab2022-09-08 08:46:06 -06001511 self.PatchObject(
1512 portage_util,
1513 "PortageqBestVisible",
1514 side_effect=cros_build_lib.RunCommandError("error"),
1515 )
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001516 target = packages.determine_package_version(
1517 constants.CHROME_CP, self.build_target
1518 )
Alex Klein1699fab2022-09-08 08:46:06 -06001519 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001520
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001521
1522class PlatformVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001523 """Tests getting platform version."""
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001524
Alex Klein1699fab2022-09-08 08:46:06 -06001525 def test_determine_platform_version(self):
1526 """Test checking that a valid platform version is returned."""
1527 platform_version = packages.determine_platform_version()
1528 # The returned platform version is something like 12603.0.0.
1529 version_string_list = platform_version.split(".")
1530 self.assertEqual(len(version_string_list), 3)
Alex Kleinfee86da2023-01-20 18:40:06 -07001531 # We don't want to check an exact version, but the first number should
1532 # be non-zero.
Alex Klein1699fab2022-09-08 08:46:06 -06001533 self.assertGreaterEqual(int(version_string_list[0]), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001534
Alex Klein1699fab2022-09-08 08:46:06 -06001535 def test_determine_milestone_version(self):
1536 """Test checking that a valid milestone version is returned."""
1537 milestone_version = packages.determine_milestone_version()
1538 # Milestone version should be non-zero
1539 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001540
Alex Klein1699fab2022-09-08 08:46:06 -06001541 def test_determine_full_version(self):
1542 """Test checking that a valid full version is returned."""
1543 full_version = packages.determine_full_version()
1544 pattern = r"^R(\d+)-(\d+.\d+.\d+(-rc\d+)*)"
1545 m = re.match(pattern, full_version)
1546 self.assertTrue(m)
1547 milestone_version = m.group(1)
1548 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001549
Alex Klein1699fab2022-09-08 08:46:06 -06001550 def test_versions_based_on_mock(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001551 # Create a test version_info object, and then mock VersionInfo.from_repo
Alex Klein1699fab2022-09-08 08:46:06 -06001552 # return it.
1553 test_platform_version = "12575.0.0"
1554 test_chrome_branch = "75"
1555 version_info_mock = chromeos_version.VersionInfo(test_platform_version)
1556 version_info_mock.chrome_branch = test_chrome_branch
1557 self.PatchObject(
1558 chromeos_version.VersionInfo,
1559 "from_repo",
1560 return_value=version_info_mock,
1561 )
1562 test_full_version = (
1563 "R" + test_chrome_branch + "-" + test_platform_version
1564 )
1565 platform_version = packages.determine_platform_version()
1566 milestone_version = packages.determine_milestone_version()
1567 full_version = packages.determine_full_version()
1568 self.assertEqual(platform_version, test_platform_version)
1569 self.assertEqual(milestone_version, test_chrome_branch)
1570 self.assertEqual(full_version, test_full_version)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001571
1572
1573# Each of the columns in the following table is a separate dimension along
1574# which Chrome uprev test cases can vary in behavior. The full test space would
1575# be the Cartesian product of the possible values of each column.
1576# 'CHROME_EBUILD' refers to the relationship between the version of the existing
1577# Chrome ebuild vs. the requested uprev version. 'FOLLOWER_EBUILDS' refers to
1578# the same relationship but for the packages defined in OTHER_CHROME_PACKAGES.
1579# 'EBUILDS MODIFIED' refers to whether any of the existing 9999 ebuilds have
1580# modified contents relative to their corresponding stable ebuilds.
1581#
1582# CHROME_EBUILD FOLLOWER_EBUILDS EBUILDS_MODIFIED
1583#
1584# HIGHER HIGHER YES
1585# SAME SAME NO
1586# LOWER LOWER
1587# DOESN'T EXIST YET
1588
1589# These test cases cover both CHROME & FOLLOWER ebuilds being identically
1590# higher, lower, or the same versions, with no modified ebuilds.
1591UPREV_VERSION_CASES = (
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001592 # Uprev.
Chris McDonaldea0312c2020-05-04 23:33:15 -06001593 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001594 "80.0.8080.0",
1595 "81.0.8181.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001596 # One added and one deleted for chrome and each "other" package.
1597 2 * (1 + len(constants.OTHER_CHROME_PACKAGES)),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001598 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001599 id="newer_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001600 ),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001601 # Revbump.
1602 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001603 "80.0.8080.0",
1604 "80.0.8080.0",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001605 2,
1606 True,
Alex Klein1699fab2022-09-08 08:46:06 -06001607 id="chrome_revbump",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001608 ),
Chris McDonaldea0312c2020-05-04 23:33:15 -06001609 # No files should be changed in these cases.
1610 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001611 "80.0.8080.0",
1612 "80.0.8080.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001613 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001614 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001615 id="same_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001616 ),
1617 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001618 "80.0.8080.0",
1619 "79.0.7979.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001620 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001621 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001622 id="older_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001623 ),
1624)
1625
1626
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001627@pytest.mark.parametrize(
Alex Klein1699fab2022-09-08 08:46:06 -06001628 "old_version, new_version, expected_count, modify_unstable",
1629 UPREV_VERSION_CASES,
1630)
1631def test_uprev_chrome_all_files_already_exist(
1632 old_version,
1633 new_version,
1634 expected_count,
1635 modify_unstable,
1636 monkeypatch,
1637 overlay_stack,
1638):
1639 """Test Chrome uprevs work as expected when all packages already exist."""
1640 (overlay,) = overlay_stack(1)
1641 monkeypatch.setattr(uprev_lib, "_CHROME_OVERLAY_PATH", overlay.path)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001642
Alex Klein1699fab2022-09-08 08:46:06 -06001643 unstable_chrome = cr.test.Package(
1644 "chromeos-base", "chromeos-chrome", version="9999", keywords="~*"
1645 )
1646 if modify_unstable:
1647 # Add some field not set in stable.
1648 unstable_chrome.depend = "foo/bar"
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001649
Alex Klein1699fab2022-09-08 08:46:06 -06001650 stable_chrome = cr.test.Package(
1651 "chromeos-base", "chromeos-chrome", version=f"{old_version}_rc-r1"
1652 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001653
Alex Klein1699fab2022-09-08 08:46:06 -06001654 overlay.add_package(unstable_chrome)
1655 overlay.add_package(stable_chrome)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001656
Alex Klein1699fab2022-09-08 08:46:06 -06001657 for pkg_str in constants.OTHER_CHROME_PACKAGES:
1658 category, pkg_name = pkg_str.split("/")
1659 unstable_pkg = cr.test.Package(
1660 category, pkg_name, version="9999", keywords="~*"
1661 )
1662 stable_pkg = cr.test.Package(
1663 category, pkg_name, version=f"{old_version}_rc-r1"
1664 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001665
Alex Klein1699fab2022-09-08 08:46:06 -06001666 overlay.add_package(unstable_pkg)
1667 overlay.add_package(stable_pkg)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001668
Alex Klein1699fab2022-09-08 08:46:06 -06001669 git_refs = [
1670 GitRef(
1671 path="/foo", ref=f"refs/tags/{new_version}", revision="stubcommit"
1672 )
1673 ]
1674 res = packages.uprev_chrome_from_ref(None, git_refs, None)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001675
Alex Klein1699fab2022-09-08 08:46:06 -06001676 modified_file_count = sum(len(m.files) for m in res.modified)
1677 assert modified_file_count == expected_count
Michael Mortensen125bb012020-05-21 14:02:10 -06001678
1679
Alex Klein1699fab2022-09-08 08:46:06 -06001680@pytest.mark.usefixtures("testcase_monkeypatch")
Michael Mortensen125bb012020-05-21 14:02:10 -06001681class GetModelsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001682 """Tests for get_models."""
Michael Mortensen125bb012020-05-21 14:02:10 -06001683
Alex Klein1699fab2022-09-08 08:46:06 -06001684 def setUp(self):
1685 self.board = "test-board"
1686 self.rc.SetDefaultCmdResult(stdout="pyro\nreef\nsnappy\n")
1687 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1688 build_bin = os.path.join(
1689 self.tempdir, constants.DEFAULT_CHROOT_DIR, "usr", "bin"
1690 )
1691 osutils.Touch(
1692 os.path.join(build_bin, "cros_config_host"), makedirs=True
1693 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001694
Alex Klein1699fab2022-09-08 08:46:06 -06001695 def testGetModels(self):
1696 """Test get_models."""
1697 build_target = build_target_lib.BuildTarget(self.board)
1698 result = packages.get_models(build_target)
1699 self.assertEqual(result, ["pyro", "reef", "snappy"])
Michael Mortensen359c1f32020-05-28 19:35:42 -06001700
1701
1702class GetKeyIdTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001703 """Tests for get_key_id."""
Michael Mortensen359c1f32020-05-28 19:35:42 -06001704
Alex Klein1699fab2022-09-08 08:46:06 -06001705 def setUp(self):
1706 self.board = "test-board"
1707 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensen359c1f32020-05-28 19:35:42 -06001708
Alex Klein1699fab2022-09-08 08:46:06 -06001709 def testGetKeyId(self):
1710 """Test get_key_id when _run_cros_config_host returns a key."""
1711 self.PatchObject(
1712 packages, "_run_cros_config_host", return_value=["key"]
1713 )
1714 result = packages.get_key_id(self.build_target, "model")
1715 self.assertEqual(result, "key")
Michael Mortensen359c1f32020-05-28 19:35:42 -06001716
Alex Klein1699fab2022-09-08 08:46:06 -06001717 def testGetKeyIdNoKey(self):
1718 """Test get_key_id when None should be returned."""
1719 self.PatchObject(
1720 packages, "_run_cros_config_host", return_value=["key1", "key2"]
1721 )
1722 result = packages.get_key_id(self.build_target, "model")
1723 self.assertEqual(result, None)
Ben Reiche779cf42020-12-15 03:21:31 +00001724
1725
Harvey Yang3eee06c2021-03-18 15:47:56 +08001726class GetLatestVersionTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001727 """Tests for get_latest_version_from_refs."""
Ben Reiche779cf42020-12-15 03:21:31 +00001728
Alex Klein1699fab2022-09-08 08:46:06 -06001729 def setUp(self):
1730 self.prefix = "refs/tags/drivefs_"
1731 # The tag ref template.
1732 ref_tpl = self.prefix + "%s"
Ben Reiche779cf42020-12-15 03:21:31 +00001733
Alex Klein1699fab2022-09-08 08:46:06 -06001734 self.latest = "44.0.20"
1735 self.versions = ["42.0.1", self.latest, "44.0.19", "39.0.15"]
1736 self.latest_ref = uprev_lib.GitRef(
1737 "/path", ref_tpl % self.latest, "abc123"
1738 )
1739 self.refs = [
1740 uprev_lib.GitRef("/path", ref_tpl % v, "abc123")
1741 for v in self.versions
1742 ]
Ben Reiche779cf42020-12-15 03:21:31 +00001743
Alex Klein1699fab2022-09-08 08:46:06 -06001744 def test_single_ref(self):
1745 """Test a single ref is supplied."""
1746 # pylint: disable=protected-access
1747 self.assertEqual(
1748 self.latest,
1749 packages._get_latest_version_from_refs(
1750 self.prefix, [self.latest_ref]
1751 ),
1752 )
Ben Reiche779cf42020-12-15 03:21:31 +00001753
Alex Klein1699fab2022-09-08 08:46:06 -06001754 def test_multiple_ref_versions(self):
1755 """Test multiple refs supplied."""
1756 # pylint: disable=protected-access
1757 self.assertEqual(
1758 self.latest,
1759 packages._get_latest_version_from_refs(self.prefix, self.refs),
1760 )
Ben Reiche779cf42020-12-15 03:21:31 +00001761
Alex Klein1699fab2022-09-08 08:46:06 -06001762 def test_no_refs_returns_none(self):
1763 """Test no refs supplied."""
1764 # pylint: disable=protected-access
1765 self.assertEqual(
1766 packages._get_latest_version_from_refs(self.prefix, []), None
1767 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001768
Chinglin Yu84818732022-10-03 12:03:43 +08001769 def test_ref_prefix(self):
1770 """Test refs with a different prefix isn't used"""
1771 # pylint: disable=protected-access
1772 # Add refs/tags/foo_100.0.0 to the refs, which should be ignored in
1773 # _get_latest_version_from_refs because the prefix doesn't match, even
1774 # if its version number is larger.
1775 refs = self.refs + [
1776 uprev_lib.GitRef("/path", "refs/tags/foo_100.0.0", "abc123")
1777 ]
1778 self.assertEqual(
1779 self.latest,
1780 packages._get_latest_version_from_refs(self.prefix, refs),
1781 )
1782
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001783
Alex Klein6becabc2020-09-11 14:03:05 -06001784class NeedsChromeSourceTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001785 """Tests for needs_chrome_source."""
Alex Klein6becabc2020-09-11 14:03:05 -06001786
Alex Klein1699fab2022-09-08 08:46:06 -06001787 def _build_graph(self, with_chrome: bool, with_followers: bool):
1788 root = "/build/build_target"
1789 foo_bar = package_info.parse("foo/bar-1")
1790 chrome = package_info.parse(f"{constants.CHROME_CP}-1.2.3.4")
1791 followers = [
1792 package_info.parse(f"{pkg}-1.2.3.4")
1793 for pkg in constants.OTHER_CHROME_PACKAGES
1794 ]
1795 nodes = [dependency_graph.PackageNode(foo_bar, root)]
1796 root_pkgs = ["foo/bar-1"]
1797 if with_chrome:
1798 nodes.append(dependency_graph.PackageNode(chrome, root))
1799 root_pkgs.append(chrome.cpvr)
1800 if with_followers:
1801 nodes.extend(
1802 [dependency_graph.PackageNode(f, root) for f in followers]
1803 )
1804 root_pkgs.extend([f.cpvr for f in followers])
Alex Klein6becabc2020-09-11 14:03:05 -06001805
Alex Klein1699fab2022-09-08 08:46:06 -06001806 return dependency_graph.DependencyGraph(nodes, root, root_pkgs)
Alex Klein6becabc2020-09-11 14:03:05 -06001807
Alex Klein1699fab2022-09-08 08:46:06 -06001808 def test_needs_all(self):
1809 """Verify we need source when we have no prebuilts."""
1810 graph = self._build_graph(with_chrome=True, with_followers=True)
1811 self.PatchObject(
1812 depgraph, "get_sysroot_dependency_graph", return_value=graph
1813 )
1814 self.PatchObject(packages, "has_prebuilt", return_value=False)
1815 self.PatchObject(
1816 packages,
1817 "uprev_chrome",
1818 return_value=uprev_lib.UprevVersionedPackageResult(),
1819 )
Alex Klein6becabc2020-09-11 14:03:05 -06001820
Alex Klein1699fab2022-09-08 08:46:06 -06001821 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001822
Alex Klein1699fab2022-09-08 08:46:06 -06001823 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001824
Alex Klein1699fab2022-09-08 08:46:06 -06001825 self.assertTrue(result.needs_chrome_source)
1826 self.assertTrue(result.builds_chrome)
1827 self.assertTrue(result.packages)
1828 self.assertEqual(
1829 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1830 )
1831 self.assertTrue(result.missing_chrome_prebuilt)
1832 self.assertTrue(result.missing_follower_prebuilt)
1833 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001834
Alex Klein1699fab2022-09-08 08:46:06 -06001835 def test_needs_none(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001836 """Verify not building any chrome packages prevents needing it."""
Alex Klein1699fab2022-09-08 08:46:06 -06001837 graph = self._build_graph(with_chrome=False, with_followers=False)
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.assertFalse(result.needs_chrome_source)
1853 self.assertFalse(result.builds_chrome)
1854 self.assertFalse(result.packages)
1855 self.assertFalse(result.missing_chrome_prebuilt)
1856 self.assertFalse(result.missing_follower_prebuilt)
1857 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001858
Alex Klein1699fab2022-09-08 08:46:06 -06001859 def test_needs_chrome_only(self):
1860 """Verify only chrome triggers needs chrome source."""
1861 graph = self._build_graph(with_chrome=True, with_followers=False)
1862 self.PatchObject(
1863 depgraph, "get_sysroot_dependency_graph", return_value=graph
1864 )
1865 self.PatchObject(packages, "has_prebuilt", return_value=False)
1866 self.PatchObject(
1867 packages,
1868 "uprev_chrome",
1869 return_value=uprev_lib.UprevVersionedPackageResult(),
1870 )
Alex Klein6becabc2020-09-11 14:03:05 -06001871
Alex Klein1699fab2022-09-08 08:46:06 -06001872 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001873
Alex Klein1699fab2022-09-08 08:46:06 -06001874 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001875
Alex Klein1699fab2022-09-08 08:46:06 -06001876 self.assertTrue(result.needs_chrome_source)
1877 self.assertTrue(result.builds_chrome)
1878 self.assertTrue(result.packages)
1879 self.assertEqual(
Alex Klein041edd82023-04-17 12:23:23 -06001880 {p.atom for p in result.packages}, {constants.CHROME_CP}
Alex Klein1699fab2022-09-08 08:46:06 -06001881 )
1882 self.assertTrue(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_followers_only(self):
1887 """Verify only chrome followers triggers needs chrome source."""
1888 graph = self._build_graph(with_chrome=False, with_followers=True)
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.assertFalse(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},
Alex Klein1699fab2022-09-08 08:46:06 -06001908 set(constants.OTHER_CHROME_PACKAGES),
1909 )
1910 self.assertFalse(result.missing_chrome_prebuilt)
1911 self.assertTrue(result.missing_follower_prebuilt)
1912 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001913
Alex Klein1699fab2022-09-08 08:46:06 -06001914 def test_has_prebuilts(self):
1915 """Test prebuilts prevent us from needing chrome source."""
1916 graph = self._build_graph(with_chrome=True, with_followers=True)
1917 self.PatchObject(
1918 depgraph, "get_sysroot_dependency_graph", return_value=graph
1919 )
1920 self.PatchObject(packages, "has_prebuilt", return_value=True)
1921 self.PatchObject(
1922 packages,
1923 "uprev_chrome",
1924 return_value=uprev_lib.UprevVersionedPackageResult(),
1925 )
Alex Klein6becabc2020-09-11 14:03:05 -06001926
Alex Klein1699fab2022-09-08 08:46:06 -06001927 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001928
Alex Klein1699fab2022-09-08 08:46:06 -06001929 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001930
Alex Klein1699fab2022-09-08 08:46:06 -06001931 self.assertFalse(result.needs_chrome_source)
1932 self.assertTrue(result.builds_chrome)
1933 self.assertFalse(result.packages)
1934 self.assertFalse(result.missing_chrome_prebuilt)
1935 self.assertFalse(result.missing_follower_prebuilt)
1936 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001937
Alex Klein1699fab2022-09-08 08:46:06 -06001938 def test_compile_source(self):
1939 """Test compile source ignores prebuilts."""
1940 graph = self._build_graph(with_chrome=True, with_followers=True)
1941 self.PatchObject(
1942 depgraph, "get_sysroot_dependency_graph", return_value=graph
1943 )
1944 self.PatchObject(packages, "has_prebuilt", return_value=True)
1945 self.PatchObject(
1946 packages,
1947 "uprev_chrome",
1948 return_value=uprev_lib.UprevVersionedPackageResult(),
1949 )
Alex Klein6becabc2020-09-11 14:03:05 -06001950
Alex Klein1699fab2022-09-08 08:46:06 -06001951 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001952
Alex Klein1699fab2022-09-08 08:46:06 -06001953 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Klein6becabc2020-09-11 14:03:05 -06001954
Alex Klein1699fab2022-09-08 08:46:06 -06001955 self.assertTrue(result.needs_chrome_source)
1956 self.assertTrue(result.builds_chrome)
1957 self.assertTrue(result.packages)
1958 self.assertEqual(
1959 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1960 )
1961 self.assertTrue(result.missing_chrome_prebuilt)
1962 self.assertTrue(result.missing_follower_prebuilt)
1963 self.assertFalse(result.local_uprev)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001964
Alex Klein1699fab2022-09-08 08:46:06 -06001965 def test_local_uprev(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=False)
Alex Klein75110572021-07-14 10:44:39 -06001972
Alex Klein1699fab2022-09-08 08:46:06 -06001973 uprev_result = uprev_lib.UprevVersionedPackageResult()
1974 uprev_result.add_result("1.2.3.4", ["/tmp/foo"])
1975 self.PatchObject(packages, "uprev_chrome", return_value=uprev_result)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001976
Alex Klein1699fab2022-09-08 08:46:06 -06001977 build_target = build_target_lib.BuildTarget("build_target")
Alex Kleinde7b76d2021-07-12 12:28:44 -06001978
Alex Klein1699fab2022-09-08 08:46:06 -06001979 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001980
Alex Klein1699fab2022-09-08 08:46:06 -06001981 self.assertTrue(result.needs_chrome_source)
1982 self.assertTrue(result.builds_chrome)
1983 self.assertTrue(result.packages)
1984 self.assertEqual(
1985 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1986 )
1987 self.assertTrue(result.missing_chrome_prebuilt)
1988 self.assertTrue(result.missing_follower_prebuilt)
1989 self.assertTrue(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001990
1991
Mike Frysinger8c9d7582023-08-21 19:28:21 -04001992class GetTargetVersionTest(cros_test_lib.RunCommandTestCase):
1993 """Tests for get_target_version."""
1994
1995 def setUp(self):
1996 self.build_target = build_target_lib.BuildTarget("build_target")
1997
1998 def test_default_empty(self):
1999 """Default behavior with mostly stub empty data."""
2000
2001 def GetBuildDependency(sysroot_path, board, mock_packages):
2002 assert sysroot_path == self.build_target.root
2003 assert board == self.build_target.name
2004 assert list(mock_packages) == [
2005 package_info.parse(constants.TARGET_OS_PKG)
2006 ]
2007 return {"package_deps": []}, {}
2008
2009 self.PatchObject(
2010 dependency, "GetBuildDependency", side_effect=GetBuildDependency
2011 )
2012 ret = packages.get_target_versions(self.build_target)
2013 assert ret.android_version is None
2014 assert ret.android_branch is None
2015 assert ret.android_target is None
2016 assert ret.chrome_version is None
2017 assert isinstance(ret.platform_version, str)
2018 assert isinstance(ret.milestone_version, str)
2019 assert isinstance(ret.full_version, str)
2020 assert ret.lacros_version is None
2021
2022
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002023class UprevDrivefsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002024 """Tests for uprev_drivefs."""
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002025
Alex Klein1699fab2022-09-08 08:46:06 -06002026 def setUp(self):
2027 self.refs = [
2028 GitRef(
2029 path="/chromeos/platform/drivefs-google3/",
2030 ref="refs/tags/drivefs_45.0.2",
2031 revision="123",
2032 )
2033 ]
2034 self.MOCK_DRIVEFS_EBUILD_PATH = "drivefs.45.0.2-r1.ebuild"
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002035
Alex Klein1699fab2022-09-08 08:46:06 -06002036 def revisionBumpOutcome(self, ebuild_path):
2037 return uprev_lib.UprevResult(
2038 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2039 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002040
Alex Klein1699fab2022-09-08 08:46:06 -06002041 def majorBumpOutcome(self, ebuild_path):
2042 return uprev_lib.UprevResult(
2043 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2044 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002045
Alex Klein1699fab2022-09-08 08:46:06 -06002046 def sameVersionOutcome(self):
2047 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002048
Alex Klein1699fab2022-09-08 08:46:06 -06002049 def test_latest_version_returns_none(self):
2050 """Test no refs were supplied"""
2051 output = packages.uprev_drivefs(None, [], None)
2052 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002053
Alex Klein1699fab2022-09-08 08:46:06 -06002054 def test_drivefs_uprev_fails(self):
2055 """Test a single ref is supplied."""
2056 self.PatchObject(
2057 uprev_lib,
2058 "uprev_workon_ebuild_to_version",
2059 side_effect=[None, None],
2060 )
2061 output = packages.uprev_drivefs(None, self.refs, None)
2062 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002063
Alex Klein1699fab2022-09-08 08:46:06 -06002064 def test_same_version_exists(self):
2065 """Test the same version exists uprev should not happen."""
2066 drivefs_outcome = self.sameVersionOutcome()
2067 self.PatchObject(
2068 uprev_lib,
2069 "uprev_workon_ebuild_to_version",
2070 side_effect=[drivefs_outcome],
2071 )
2072 output = packages.uprev_drivefs(None, self.refs, None)
2073 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002074
Alex Klein1699fab2022-09-08 08:46:06 -06002075 def test_revision_bump_both_packages(self):
2076 """Test both packages uprev, should succeed."""
2077 drivefs_outcome = self.revisionBumpOutcome(
2078 self.MOCK_DRIVEFS_EBUILD_PATH
2079 )
2080 self.PatchObject(
2081 uprev_lib,
2082 "uprev_workon_ebuild_to_version",
2083 side_effect=[drivefs_outcome],
2084 )
2085 output = packages.uprev_drivefs(None, self.refs, None)
2086 self.assertTrue(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002087
Alex Klein1699fab2022-09-08 08:46:06 -06002088 def test_major_bump_both_packages(self):
2089 """Test both packages uprev, should succeed."""
2090 drivefs_outcome = self.majorBumpOutcome(self.MOCK_DRIVEFS_EBUILD_PATH)
2091 self.PatchObject(
2092 uprev_lib,
2093 "uprev_workon_ebuild_to_version",
2094 side_effect=[drivefs_outcome],
2095 )
2096 output = packages.uprev_drivefs(None, self.refs, None)
2097 self.assertTrue(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002098
2099
Denis Nikitin63613e32022-09-09 22:26:50 -07002100class UprevKernelAfdo(cros_test_lib.RunCommandTempDirTestCase):
2101 """Tests for uprev_kernel_afdo."""
2102
2103 def setUp(self):
2104 # patch_ebuild_vars is tested separately.
2105 self.mock_patch = self.PatchObject(packages, "patch_ebuild_vars")
Denis Nikitin88ad5132022-09-28 12:10:01 -07002106 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Denis Nikitin63613e32022-09-09 22:26:50 -07002107 self.metadata_dir = os.path.join(
Denis Nikitin63613e32022-09-09 22:26:50 -07002108 "src",
2109 "third_party",
2110 "toolchain-utils",
2111 "afdo_metadata",
2112 )
Denis Nikitin88ad5132022-09-28 12:10:01 -07002113 osutils.SafeMakedirs(os.path.join(self.tempdir, self.metadata_dir))
Denis Nikitin63613e32022-09-09 22:26:50 -07002114
2115 def test_uprev_kernel_afdo_version(self):
2116 """Test kernel afdo version uprev."""
2117 json_files = {
2118 "kernel_afdo.json": (
2119 "{\n"
2120 ' "chromeos-kernel-5_4": {\n'
2121 ' "name": "R106-12345.0-0123456789"\n'
2122 " }\n"
2123 "}"
2124 ),
2125 "kernel_arm_afdo.json": (
2126 "{\n"
2127 ' "chromeos-kernel-5_15": {\n'
2128 ' "name": "R107-67890.0-0123456789"\n'
2129 " }\n"
2130 "}"
2131 ),
2132 }
2133 for f, contents in json_files.items():
2134 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2135
Brian Norrisf0c68572023-07-12 17:32:20 -07002136 returned_output = packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002137
Denis Nikitin88ad5132022-09-28 12:10:01 -07002138 package_root = os.path.join(
2139 constants.SOURCE_ROOT,
2140 constants.CHROMIUMOS_OVERLAY_DIR,
2141 "sys-kernel",
Denis Nikitin63613e32022-09-09 22:26:50 -07002142 )
2143 expect_result = [
2144 uprev_lib.UprevVersionedPackageModifications(
2145 new_version="R106-12345.0-0123456789",
2146 files=[
2147 os.path.join(
2148 package_root,
2149 "chromeos-kernel-5_4",
2150 "chromeos-kernel-5_4-9999.ebuild",
2151 ),
2152 os.path.join(
2153 package_root, "chromeos-kernel-5_4", "Manifest"
2154 ),
2155 ],
2156 ),
2157 uprev_lib.UprevVersionedPackageModifications(
2158 new_version="R107-67890.0-0123456789",
2159 files=[
2160 os.path.join(
2161 package_root,
2162 "chromeos-kernel-5_15",
2163 "chromeos-kernel-5_15-9999.ebuild",
2164 ),
2165 os.path.join(
2166 package_root, "chromeos-kernel-5_15", "Manifest"
2167 ),
2168 ],
2169 ),
2170 ]
2171 self.assertTrue(returned_output.uprevved)
2172 self.assertEqual(returned_output.modified, expect_result)
2173
2174 def test_uprev_kernel_afdo_empty_json(self):
2175 """Test kernel afdo version unchanged."""
2176 json_files = {
2177 "kernel_afdo.json": "{}",
2178 "kernel_arm_afdo.json": "{}",
2179 }
2180 for f, contents in json_files.items():
2181 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2182
Brian Norrisf0c68572023-07-12 17:32:20 -07002183 returned_output = packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002184 self.assertFalse(returned_output.uprevved)
2185
2186 def test_uprev_kernel_afdo_empty_file(self):
2187 """Test malformed json raises."""
2188 json_files = {
2189 "kernel_afdo.json": "",
2190 "kernel_arm_afdo.json": "",
2191 }
2192 for f, contents in json_files.items():
2193 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2194
2195 with self.assertRaisesRegex(
2196 json.decoder.JSONDecodeError, "Expecting value"
2197 ):
Brian Norrisf0c68572023-07-12 17:32:20 -07002198 packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002199
2200 def test_uprev_kernel_afdo_manifest_raises(self):
2201 """Test manifest update raises."""
2202 json_files = {
2203 "kernel_afdo.json": (
2204 "{\n"
2205 ' "chromeos-kernel-5_4": {\n'
2206 ' "name": "R106-12345.0-0123456789"\n'
2207 " }\n"
2208 "}"
2209 ),
2210 }
2211 for f, contents in json_files.items():
2212 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2213 # run() raises exception.
2214 self.rc.SetDefaultCmdResult(
2215 side_effect=cros_build_lib.RunCommandError("error")
2216 )
2217
2218 with self.assertRaises(uprev_lib.EbuildManifestError):
Brian Norrisf0c68572023-07-12 17:32:20 -07002219 packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002220
2221
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002222# TODO(chenghaoyang): Shouldn't use uprev_workon_ebuild_to_version.
2223class UprevPerfettoTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002224 """Tests for uprev_perfetto."""
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002225
Alex Klein1699fab2022-09-08 08:46:06 -06002226 def setUp(self):
2227 self.refs = [GitRef(path="/foo", ref="refs/tags/v12.0", revision="123")]
2228 self.MOCK_PERFETTO_EBUILD_PATH = "perfetto-12.0-r1.ebuild"
Chinglin Yufa728552023-04-13 03:12:04 +00002229 self.MOCK_PERFETTO_PROTO_EBUILD_PATH = "perfetto-protos-12.0-r1.ebuild"
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002230
Chinglin Yufa728552023-04-13 03:12:04 +00002231 def revisionBumpOutcome(self):
2232 return [
2233 uprev_lib.UprevResult(
2234 uprev_lib.Outcome.REVISION_BUMP,
2235 [self.MOCK_PERFETTO_EBUILD_PATH],
2236 ),
2237 uprev_lib.UprevResult(
2238 uprev_lib.Outcome.REVISION_BUMP,
2239 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2240 ),
2241 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002242
Chinglin Yufa728552023-04-13 03:12:04 +00002243 def majorBumpOutcome(self):
2244 return [
2245 uprev_lib.UprevResult(
2246 uprev_lib.Outcome.VERSION_BUMP, [self.MOCK_PERFETTO_EBUILD_PATH]
2247 ),
2248 uprev_lib.UprevResult(
2249 uprev_lib.Outcome.VERSION_BUMP,
2250 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2251 ),
2252 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002253
Alex Klein1699fab2022-09-08 08:46:06 -06002254 def newerVersionOutcome(self):
2255 return uprev_lib.UprevResult(uprev_lib.Outcome.NEWER_VERSION_EXISTS)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002256
Alex Klein1699fab2022-09-08 08:46:06 -06002257 def sameVersionOutcome(self):
2258 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002259
Alex Klein1699fab2022-09-08 08:46:06 -06002260 def test_latest_version_returns_none(self):
2261 """Test no refs were supplied"""
2262 output = packages.uprev_perfetto(None, [], None)
2263 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002264
Alex Klein1699fab2022-09-08 08:46:06 -06002265 def test_perfetto_uprev_fails(self):
2266 """Test a single ref is supplied."""
2267 self.PatchObject(
2268 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2269 )
2270 output = packages.uprev_perfetto(None, self.refs, None)
2271 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002272
Alex Klein1699fab2022-09-08 08:46:06 -06002273 def test_newer_version_exists(self):
2274 """Test the newer version exists uprev should not happen."""
2275 perfetto_outcome = self.newerVersionOutcome()
2276 self.PatchObject(
2277 uprev_lib,
2278 "uprev_workon_ebuild_to_version",
2279 side_effect=[perfetto_outcome],
2280 )
2281 output = packages.uprev_perfetto(None, self.refs, None)
2282 self.assertFalse(output.uprevved)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002283
Alex Klein1699fab2022-09-08 08:46:06 -06002284 def test_same_version_exists(self):
2285 """Test the same version exists uprev should not happen."""
2286 perfetto_outcome = self.sameVersionOutcome()
2287 self.PatchObject(
2288 uprev_lib,
2289 "uprev_workon_ebuild_to_version",
2290 side_effect=[perfetto_outcome],
2291 )
2292 output = packages.uprev_perfetto(None, self.refs, None)
2293 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002294
Alex Klein1699fab2022-09-08 08:46:06 -06002295 def test_revision_bump_perfetto_package(self):
2296 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002297 self.PatchObject(
2298 uprev_lib,
2299 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002300 side_effect=self.revisionBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002301 )
2302 output = packages.uprev_perfetto(None, self.refs, None)
2303 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002304 self.assertEqual(
2305 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2306 )
2307 self.assertEqual(
2308 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2309 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002310
Alex Klein1699fab2022-09-08 08:46:06 -06002311 def test_major_bump_perfetto_package(self):
2312 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002313 self.PatchObject(
2314 uprev_lib,
2315 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002316 side_effect=self.majorBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002317 )
2318 output = packages.uprev_perfetto(None, self.refs, None)
2319 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002320 self.assertEqual(
2321 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2322 )
2323 self.assertEqual(
2324 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2325 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002326
Chinglin Yuad12a512022-10-07 17:26:12 +08002327 def test_revision_bump_trunk(self):
2328 """Test revision bump on receiving non-versioned trunk refs."""
Chinglin Yu5de28a42022-11-11 19:52:21 +08002329 refs = [
2330 GitRef(
2331 path="/foo", ref="refs/heads/main", revision="0123456789abcdef"
2332 )
2333 ]
Chinglin Yuad12a512022-10-07 17:26:12 +08002334 self.PatchObject(
2335 uprev_lib, "get_stable_ebuild_version", return_value="12.0"
2336 )
2337 self.PatchObject(
2338 uprev_lib,
2339 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002340 side_effect=self.revisionBumpOutcome(),
Chinglin Yuad12a512022-10-07 17:26:12 +08002341 )
2342 output = packages.uprev_perfetto(None, refs, None)
Chinglin Yufa728552023-04-13 03:12:04 +00002343
Chinglin Yuad12a512022-10-07 17:26:12 +08002344 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002345 self.assertEqual(
2346 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2347 )
Chinglin Yu5de28a42022-11-11 19:52:21 +08002348 self.assertEqual(output.modified[0].new_version, "12.0-012345678")
Chinglin Yufa728552023-04-13 03:12:04 +00002349 self.assertEqual(
2350 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2351 )
2352 self.assertEqual(output.modified[1].new_version, "12.0-012345678")
Chinglin Yuad12a512022-10-07 17:26:12 +08002353
Alex Klein627e04c2021-11-10 15:56:47 -07002354
Julio Hurtadof1befec2021-05-05 21:34:26 +00002355class UprevLacrosTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002356 """Tests for uprev_lacros"""
Julio Hurtadof1befec2021-05-05 21:34:26 +00002357
Alex Klein1699fab2022-09-08 08:46:06 -06002358 def setUp(self):
2359 self.refs = [
2360 GitRef(
2361 path="/lacros", ref="refs/heads/main", revision="123.456.789.0"
2362 )
2363 ]
2364 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtadof1befec2021-05-05 21:34:26 +00002365
Alex Klein1699fab2022-09-08 08:46:06 -06002366 def revisionBumpOutcome(self, ebuild_path):
2367 return uprev_lib.UprevResult(
2368 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2369 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002370
Alex Klein1699fab2022-09-08 08:46:06 -06002371 def majorBumpOutcome(self, ebuild_path):
2372 return uprev_lib.UprevResult(
2373 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2374 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002375
Alex Klein1699fab2022-09-08 08:46:06 -06002376 def newerVersionOutcome(self, ebuild_path):
2377 return uprev_lib.UprevResult(
2378 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2379 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002380
Alex Klein1699fab2022-09-08 08:46:06 -06002381 def sameVersionOutcome(self, ebuild_path):
2382 return uprev_lib.UprevResult(
2383 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2384 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002385
Alex Klein1699fab2022-09-08 08:46:06 -06002386 def newEbuildCreatedOutcome(self, ebuild_path):
2387 return uprev_lib.UprevResult(
2388 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2389 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002390
Alex Klein1699fab2022-09-08 08:46:06 -06002391 def test_lacros_uprev_fails(self):
2392 """Test a lacros package uprev with no triggers"""
2393 self.PatchObject(
2394 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2395 )
2396 with self.assertRaises(IndexError):
2397 packages.uprev_lacros(None, [], None)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002398
Alex Klein1699fab2022-09-08 08:46:06 -06002399 def test_lacros_uprev_revision_bump(self):
2400 """Test lacros package uprev."""
2401 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2402 self.PatchObject(
2403 uprev_lib,
2404 "uprev_workon_ebuild_to_version",
2405 side_effect=[lacros_outcome],
2406 )
2407 output = packages.uprev_lacros(None, self.refs, None)
2408 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002409
Alex Klein1699fab2022-09-08 08:46:06 -06002410 def test_lacros_uprev_version_bump(self):
2411 """Test lacros package uprev."""
2412 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2413 self.PatchObject(
2414 uprev_lib,
2415 "uprev_workon_ebuild_to_version",
2416 side_effect=[lacros_outcome],
2417 )
2418 output = packages.uprev_lacros(None, self.refs, None)
2419 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002420
Alex Klein1699fab2022-09-08 08:46:06 -06002421 def test_lacros_uprev_new_ebuild_created(self):
2422 """Test lacros package uprev."""
2423 lacros_outcome = self.newEbuildCreatedOutcome(
2424 self.MOCK_LACROS_EBUILD_PATH
2425 )
2426 self.PatchObject(
2427 uprev_lib,
2428 "uprev_workon_ebuild_to_version",
2429 side_effect=[lacros_outcome],
2430 )
2431 output = packages.uprev_lacros(None, self.refs, None)
2432 self.assertTrue(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002433
Alex Klein1699fab2022-09-08 08:46:06 -06002434 def test_lacros_uprev_newer_version_exist(self):
2435 """Test the newer version exists uprev should not happen."""
2436 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2437 self.PatchObject(
2438 uprev_lib,
2439 "uprev_workon_ebuild_to_version",
2440 side_effect=[lacros_outcome],
2441 )
2442 output = packages.uprev_lacros(None, self.refs, None)
2443 self.assertFalse(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002444
Alex Klein1699fab2022-09-08 08:46:06 -06002445 def test_lacros_uprev_same_version_exist(self):
2446 """Test the same version exists uprev should not happen."""
2447 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2448 self.PatchObject(
2449 uprev_lib,
2450 "uprev_workon_ebuild_to_version",
2451 side_effect=[lacros_outcome],
2452 )
2453 output = packages.uprev_lacros(None, self.refs, None)
2454 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002455
2456
2457class UprevLacrosInParallelTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002458 """Tests for uprev_lacros"""
Julio Hurtado870ed322021-12-03 18:22:40 +00002459
Alex Klein1699fab2022-09-08 08:46:06 -06002460 def setUp(self):
2461 self.refs = [
2462 GitRef(
2463 path="/lacros", revision="abc123", ref="refs/tags/123.456.789.0"
2464 )
2465 ]
2466 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtado870ed322021-12-03 18:22:40 +00002467
Alex Klein1699fab2022-09-08 08:46:06 -06002468 def revisionBumpOutcome(self, ebuild_path):
2469 return uprev_lib.UprevResult(
2470 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2471 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002472
Alex Klein1699fab2022-09-08 08:46:06 -06002473 def majorBumpOutcome(self, ebuild_path):
2474 return uprev_lib.UprevResult(
2475 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2476 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002477
Alex Klein1699fab2022-09-08 08:46:06 -06002478 def newerVersionOutcome(self, ebuild_path):
2479 return uprev_lib.UprevResult(
2480 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2481 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002482
Alex Klein1699fab2022-09-08 08:46:06 -06002483 def sameVersionOutcome(self, ebuild_path):
2484 return uprev_lib.UprevResult(
2485 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2486 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002487
Alex Klein1699fab2022-09-08 08:46:06 -06002488 def newEbuildCreatedOutcome(self, ebuild_path):
2489 return uprev_lib.UprevResult(
2490 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2491 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002492
Alex Klein1699fab2022-09-08 08:46:06 -06002493 def test_lacros_uprev_fails(self):
2494 """Test a lacros package uprev with no triggers"""
2495 self.PatchObject(
2496 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2497 )
Alex Klein314fb5d2022-10-24 14:56:31 -06002498 with self.assertRaises(uprev_lib.NoRefsError):
Alex Klein1699fab2022-09-08 08:46:06 -06002499 packages.uprev_lacros_in_parallel(None, [], None)
Julio Hurtado870ed322021-12-03 18:22:40 +00002500
Alex Klein1699fab2022-09-08 08:46:06 -06002501 def test_lacros_uprev_revision_bump(self):
2502 """Test lacros package uprev."""
2503 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2504 self.PatchObject(
2505 uprev_lib,
2506 "uprev_workon_ebuild_to_version",
2507 side_effect=[lacros_outcome],
2508 )
2509 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2510 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002511
Alex Klein1699fab2022-09-08 08:46:06 -06002512 def test_lacros_uprev_version_bump(self):
2513 """Test lacros package uprev."""
2514 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2515 self.PatchObject(
2516 uprev_lib,
2517 "uprev_workon_ebuild_to_version",
2518 side_effect=[lacros_outcome],
2519 )
2520 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2521 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002522
Alex Klein1699fab2022-09-08 08:46:06 -06002523 def test_lacros_uprev_new_ebuild_created(self):
2524 """Test lacros package uprev."""
2525 lacros_outcome = self.newEbuildCreatedOutcome(
2526 self.MOCK_LACROS_EBUILD_PATH
2527 )
2528 self.PatchObject(
2529 uprev_lib,
2530 "uprev_workon_ebuild_to_version",
2531 side_effect=[lacros_outcome],
2532 )
2533 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2534 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002535
Alex Klein1699fab2022-09-08 08:46:06 -06002536 def test_lacros_uprev_newer_version_exist(self):
2537 """Test the newer version exists uprev should not happen."""
2538 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2539 self.PatchObject(
2540 uprev_lib,
2541 "uprev_workon_ebuild_to_version",
2542 side_effect=[lacros_outcome],
2543 )
2544 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2545 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002546
Alex Klein1699fab2022-09-08 08:46:06 -06002547 def test_lacros_uprev_same_version_exist(self):
2548 """Test the same version exists uprev should not happen."""
2549 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2550 self.PatchObject(
2551 uprev_lib,
2552 "uprev_workon_ebuild_to_version",
2553 side_effect=[lacros_outcome],
2554 )
2555 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2556 self.assertFalse(output.uprevved)