blob: b484215f5b283760c2c3c7e582fd569224726825 [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
Trent Apted39e74d32023-09-04 11:24:40 +100018from chromite.api.gen.config import replication_config_pb2
Alex Klein2960c752020-03-09 13:43:38 -060019from chromite.lib import build_target_lib
Ram Chandrasekar60f69f32022-06-03 22:49:30 +000020from chromite.lib import chromeos_version
Andrew Lamb2bde9e42019-11-04 13:24:09 -070021from chromite.lib import constants
Michael Mortensene0f4b542019-10-24 15:30:23 -060022from chromite.lib import cros_build_lib
Alex Kleineb77ffa2019-05-28 14:47:44 -060023from chromite.lib import cros_test_lib
Alex Klein6becabc2020-09-11 14:03:05 -060024from chromite.lib import dependency_graph
Mike Frysinger68796b52019-08-25 00:04:27 -040025from chromite.lib import depgraph
Michael Mortensenb70e8a82019-10-10 18:43:41 -060026from chromite.lib import osutils
Mike Frysinger88d96362020-02-14 19:05:45 -050027from chromite.lib import partial_mock
Alex Klein87531182019-08-12 15:23:37 -060028from chromite.lib import portage_util
Chris McDonaldea0312c2020-05-04 23:33:15 -060029from chromite.lib import uprev_lib
Alex Klein87531182019-08-12 15:23:37 -060030from chromite.lib.chroot_lib import Chroot
Alex Klein18a60af2020-06-11 12:08:47 -060031from chromite.lib.parser import package_info
Andrew Lamb2bde9e42019-11-04 13:24:09 -070032from chromite.lib.uprev_lib import GitRef
Shao-Chuan Lee05e51142021-11-24 12:27:37 +090033from chromite.service import android
Mike Frysinger8c9d7582023-08-21 19:28:21 -040034from chromite.service import dependency
Alex Kleineb77ffa2019-05-28 14:47:44 -060035from chromite.service import packages
36
Mike Frysinger68796b52019-08-25 00:04:27 -040037
Andrew Lamb2bde9e42019-11-04 13:24:09 -070038D = cros_test_lib.Directory
Trent Apted39e74d32023-09-04 11:24:40 +100039FILE_TYPE_JSON = replication_config_pb2.FILE_TYPE_JSON
40FileReplicationRule = replication_config_pb2.FileReplicationRule
41REPLICATION_TYPE_FILTER = replication_config_pb2.REPLICATION_TYPE_FILTER
42ReplicationConfig = replication_config_pb2.ReplicationConfig
Andrew Lamb2bde9e42019-11-04 13:24:09 -070043
Alex Kleineb77ffa2019-05-28 14:47:44 -060044
Alex Klein4de25e82019-08-05 15:58:39 -060045class UprevAndroidTest(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060046 """Uprev android tests."""
Alex Klein4de25e82019-08-05 15:58:39 -060047
Alex Klein1699fab2022-09-08 08:46:06 -060048 def _mock_successful_uprev(self):
49 self.rc.AddCmdResult(
50 partial_mock.In("cros_mark_android_as_stable"),
51 stdout=(
52 '{"revved": true,'
53 ' "android_atom": "android/android-1.0",'
54 ' "modified_files": ["file1", "file2"]}'
55 ),
56 )
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090057
Alex Klein1699fab2022-09-08 08:46:06 -060058 def test_success(self):
59 """Test successful run handling."""
60 self._mock_successful_uprev()
61 build_targets = [
62 build_target_lib.BuildTarget(t) for t in ["foo", "bar"]
63 ]
Alex Klein4de25e82019-08-05 15:58:39 -060064
Alex Klein1699fab2022-09-08 08:46:06 -060065 result = packages.uprev_android(
66 "android/package", Chroot(), build_targets=build_targets
67 )
68 self.assertCommandContains(
69 [
70 "cros_mark_android_as_stable",
71 "--android_package=android/package",
72 "--boards=foo:bar",
73 ]
74 )
75 self.assertCommandContains(["emerge-foo"])
76 self.assertCommandContains(["emerge-bar"])
Alex Klein4de25e82019-08-05 15:58:39 -060077
Alex Klein1699fab2022-09-08 08:46:06 -060078 self.assertTrue(result.revved)
79 self.assertEqual(result.android_atom, "android/android-1.0")
80 self.assertListEqual(result.modified_files, ["file1", "file2"])
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090081
Alex Klein1699fab2022-09-08 08:46:06 -060082 def test_android_build_branch(self):
83 """Test specifying android_build_branch option."""
84 self._mock_successful_uprev()
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090085
Alex Klein1699fab2022-09-08 08:46:06 -060086 packages.uprev_android(
87 "android/package",
88 Chroot(),
89 android_build_branch="android-build-branch",
90 )
91 self.assertCommandContains(
92 [
93 "cros_mark_android_as_stable",
94 "--android_package=android/package",
95 "--android_build_branch=android-build-branch",
96 ]
97 )
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090098
Alex Klein1699fab2022-09-08 08:46:06 -060099 def test_android_version(self):
100 """Test specifying android_version option."""
101 self._mock_successful_uprev()
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 packages.uprev_android(
104 "android/package", Chroot(), android_version="7123456"
105 )
106 self.assertCommandContains(
107 [
108 "cros_mark_android_as_stable",
109 "--android_package=android/package",
110 "--force_version=7123456",
111 ]
112 )
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 def test_skip_commit(self):
115 """Test specifying skip_commit option."""
116 self._mock_successful_uprev()
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900117
Alex Klein1699fab2022-09-08 08:46:06 -0600118 packages.uprev_android("android/package", Chroot(), skip_commit=True)
119 self.assertCommandContains(
120 [
121 "cros_mark_android_as_stable",
122 "--android_package=android/package",
123 "--skip_commit",
124 ]
125 )
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900126
Alex Klein1699fab2022-09-08 08:46:06 -0600127 def test_no_uprev(self):
128 """Test no uprev handling."""
129 self.rc.AddCmdResult(
130 partial_mock.In("cros_mark_android_as_stable"),
131 stdout='{"revved": false}',
132 )
133 build_targets = [
134 build_target_lib.BuildTarget(t) for t in ["foo", "bar"]
135 ]
136 result = packages.uprev_android(
137 "android/package", Chroot(), build_targets=build_targets
138 )
Alex Klein4de25e82019-08-05 15:58:39 -0600139
Alex Klein1699fab2022-09-08 08:46:06 -0600140 self.assertCommandContains(
141 ["cros_mark_android_as_stable", "--boards=foo:bar"]
142 )
143 self.assertCommandContains(["emerge-foo"], expected=False)
144 self.assertCommandContains(["emerge-bar"], expected=False)
Alex Klein4de25e82019-08-05 15:58:39 -0600145
Alex Klein1699fab2022-09-08 08:46:06 -0600146 self.assertFalse(result.revved)
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 def test_ignore_junk_in_stdout(self):
149 """Test when stdout contains junk messages."""
150 self.rc.AddCmdResult(
151 partial_mock.In("cros_mark_android_as_stable"),
152 stdout='foo\nbar\n{"revved": false}\n',
153 )
154 result = packages.uprev_android("android/package", Chroot())
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900155
Alex Klein1699fab2022-09-08 08:46:06 -0600156 self.assertFalse(result.revved)
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900157
Alex Klein4de25e82019-08-05 15:58:39 -0600158
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900159class UprevAndroidLKGBTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600160 """Tests for uprevving Android with LKGB."""
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900161
Alex Klein1699fab2022-09-08 08:46:06 -0600162 def test_registered_handlers(self):
163 """Test that each Android package has an uprev handler registered."""
164 mock_handler = self.PatchObject(packages, "uprev_android_lkgb")
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900165
Shao-Chuan Leeca2cbcc2022-11-02 08:28:31 +0900166 for android_package in android.GetAllAndroidPackages():
Alex Klein1699fab2022-09-08 08:46:06 -0600167 cpv = package_info.SplitCPV(
168 "chromeos-base/" + android_package, strict=False
169 )
170 build_targets = [build_target_lib.BuildTarget("foo")]
171 chroot = Chroot()
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900172
Alex Klein1699fab2022-09-08 08:46:06 -0600173 packages.uprev_versioned_package(cpv, build_targets, [], chroot)
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900174
Alex Klein1699fab2022-09-08 08:46:06 -0600175 mock_handler.assert_called_once_with(
176 android_package, build_targets, chroot
177 )
178 mock_handler.reset_mock()
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900179
Alex Klein1699fab2022-09-08 08:46:06 -0600180 def test_success(self):
181 """Test a successful uprev."""
182 self.PatchObject(android, "OVERLAY_DIR", new="overlay-dir")
Shao-Chuan Leee0b9ba92023-01-18 19:35:36 +0900183 self.PatchObject(
184 android, "ReadLKGB", return_value=dict(build_id="android-lkgb")
185 )
Alex Klein1699fab2022-09-08 08:46:06 -0600186 self.PatchObject(
187 packages,
188 "uprev_android",
189 return_value=packages.UprevAndroidResult(
190 revved=True,
191 android_atom="android-atom",
192 modified_files=["file1", "file2"],
193 ),
194 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900195
Alex Klein1699fab2022-09-08 08:46:06 -0600196 result = packages.uprev_android_lkgb("android-package", [], Chroot())
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900197
Alex Klein1699fab2022-09-08 08:46:06 -0600198 self.assertListEqual(
199 result.modified,
200 [
201 uprev_lib.UprevVersionedPackageModifications(
202 "android-lkgb",
203 [
204 os.path.join("overlay-dir", "file1"),
205 os.path.join("overlay-dir", "file2"),
206 ],
207 )
208 ],
209 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900210
Alex Klein1699fab2022-09-08 08:46:06 -0600211 def test_no_rev(self):
212 """Test when nothing revved."""
Shao-Chuan Leee0b9ba92023-01-18 19:35:36 +0900213 self.PatchObject(
214 android, "ReadLKGB", return_value=dict(build_id="android-lkgb")
215 )
Alex Klein1699fab2022-09-08 08:46:06 -0600216 self.PatchObject(
217 packages,
218 "uprev_android",
219 return_value=packages.UprevAndroidResult(revved=False),
220 )
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900221
Alex Klein1699fab2022-09-08 08:46:06 -0600222 result = packages.uprev_android_lkgb("android-package", [], Chroot())
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900223
Alex Klein1699fab2022-09-08 08:46:06 -0600224 self.assertListEqual(result.modified, [])
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900225
226
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700227class UprevECUtilsTest(cros_test_lib.MockTestCase):
228 """Tests for upreving ecutils."""
229
230 def test_success(self):
231 """Test a successful uprev."""
232
233 def fakeRunTasks(func, inputs):
234 results = []
235 for args in inputs:
236 results.append(func(*args))
237 return results
238
239 self.PatchObject(
240 packages.uprev_lib.parallel,
241 "RunTasksInProcessPool",
242 side_effect=fakeRunTasks,
243 )
244 mock_devutils = mock.MagicMock(name="dev-utils")
245 mock_ecutils = mock.MagicMock(name="ec-utils")
246 mock_ecutilstest = mock.MagicMock(name="ec-utils-test")
247 self.PatchObject(
248 packages.uprev_lib.portage_util,
249 "GetOverlayEBuilds",
250 return_value=[
251 mock_devutils,
252 mock_ecutils,
253 mock_ecutilstest,
254 ],
255 )
256 mock_overlay_mgr = mock.MagicMock(name="overlay-manager")
257 mock_overlay_mgr.modified_ebuilds = ["file1", "file2"]
258 self.PatchObject(
259 packages.uprev_lib,
260 "UprevOverlayManager",
261 return_value=mock_overlay_mgr,
262 )
Jeremy Bettis0186d252023-01-19 14:47:46 -0700263
264 for package in [
265 "chromeos-base/ec-devutils",
266 "chromeos-base/ec-utils",
267 "chromeos-base/ec-utils-test",
268 ]:
269 cpv = package_info.SplitCPV(package, strict=False)
270 assert cpv is not None
271 build_targets = [build_target_lib.BuildTarget("foo")]
272 refs = [
273 GitRef(
274 path="/platform/ec",
275 ref="main",
276 revision="123",
277 )
278 ]
279 chroot = Chroot()
280
281 result = packages.uprev_versioned_package(
282 cpv, build_targets, refs, chroot
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700283 )
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700284
Jeremy Bettis0186d252023-01-19 14:47:46 -0700285 self.assertEqual(1, len(result.modified))
286 self.assertEqual("123", result.modified[0].new_version)
287 self.assertListEqual(result.modified[0].files, ["file1", "file2"])
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700288
Jeremy Bettis0186d252023-01-19 14:47:46 -0700289 mock_overlay_mgr.uprev.assert_called_with(
290 package_list=[
291 package,
292 ],
293 force=True,
294 )
Jeremy Bettisaf96afb2023-01-11 16:09:58 -0700295
296
Alex Kleineb77ffa2019-05-28 14:47:44 -0600297class UprevBuildTargetsTest(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600298 """uprev_build_targets tests."""
Alex Kleineb77ffa2019-05-28 14:47:44 -0600299
Alex Klein1699fab2022-09-08 08:46:06 -0600300 def test_invalid_type_fails(self):
301 """Test invalid type fails."""
302 with self.assertRaises(AssertionError):
303 packages.uprev_build_targets(
304 [build_target_lib.BuildTarget("foo")], "invalid"
305 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600306
Alex Klein1699fab2022-09-08 08:46:06 -0600307 def test_none_type_fails(self):
308 """Test None type fails."""
309 with self.assertRaises(AssertionError):
310 packages.uprev_build_targets(
311 [build_target_lib.BuildTarget("foo")], None
312 )
Alex Kleineb77ffa2019-05-28 14:47:44 -0600313
314
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000315class PatchEbuildVarsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600316 """patch_ebuild_vars test."""
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000317
Alex Klein1699fab2022-09-08 08:46:06 -0600318 def setUp(self):
319 self.mock_input = self.PatchObject(packages.fileinput, "input")
320 self.mock_stdout_write = self.PatchObject(packages.sys.stdout, "write")
321 self.ebuild_path = "/path/to/ebuild"
322 self.old_var_value = "R100-5678.0.123456789"
323 self.new_var_value = "R102-5678.0.234566789"
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000324
Alex Klein1699fab2022-09-08 08:46:06 -0600325 def test_patch_ebuild_vars_var_only(self):
326 """patch_ebuild_vars changes ^var=value$."""
327 ebuild_contents = (
328 "This line does not change.\n"
329 'AFDO_PROFILE_VERSION="{var_value}"\n'
330 "\n"
331 "# The line with AFDO_PROFILE_VERSION is also unchanged."
332 )
333 # Ebuild contains old_var_value.
334 self.mock_input.return_value = io.StringIO(
335 ebuild_contents.format(var_value=self.old_var_value)
336 )
337 expected_calls = []
338 # Expect the line with new_var_value.
339 for line in io.StringIO(
340 ebuild_contents.format(var_value=self.new_var_value)
341 ):
342 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000343
Alex Klein1699fab2022-09-08 08:46:06 -0600344 packages.patch_ebuild_vars(
345 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
346 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000347
Alex Klein1699fab2022-09-08 08:46:06 -0600348 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000349
Alex Klein1699fab2022-09-08 08:46:06 -0600350 def test_patch_ebuild_vars_ignore_export(self):
351 """patch_ebuild_vars changes ^export var=value$ and keeps export."""
352 ebuild_contents = (
353 "This line does not change.\n"
354 'export AFDO_PROFILE_VERSION="{var_value}"\n'
355 "# This line is also unchanged."
356 )
357 # Ebuild contains old_var_value.
358 self.mock_input.return_value = io.StringIO(
359 ebuild_contents.format(var_value=self.old_var_value)
360 )
361 expected_calls = []
362 # Expect the line with new_var_value.
363 for line in io.StringIO(
364 ebuild_contents.format(var_value=self.new_var_value)
365 ):
366 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000367
Alex Klein1699fab2022-09-08 08:46:06 -0600368 packages.patch_ebuild_vars(
369 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
370 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000371
Alex Klein1699fab2022-09-08 08:46:06 -0600372 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000373
Alex Klein1699fab2022-09-08 08:46:06 -0600374 def test_patch_ebuild_vars_partial_match(self):
375 """patch_ebuild_vars ignores ^{prefix}var=value$."""
376 ebuild_contents = (
Alex Kleina53bd282022-09-09 12:42:55 -0600377 'This and the line below do not change.\nNEW_AFDO="{var_value}"'
Alex Klein1699fab2022-09-08 08:46:06 -0600378 )
379 # Ebuild contains old_var_value.
380 self.mock_input.return_value = io.StringIO(
381 ebuild_contents.format(var_value=self.old_var_value)
382 )
383 expected_calls = []
384 # Expect the line with UNCHANGED old_var_value.
385 for line in io.StringIO(
386 ebuild_contents.format(var_value=self.old_var_value)
387 ):
388 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000389
Alex Kleinfee86da2023-01-20 18:40:06 -0700390 # Note that the var name partially matches the ebuild var and hence it
391 # has to be ignored.
Alex Klein1699fab2022-09-08 08:46:06 -0600392 packages.patch_ebuild_vars(
393 self.ebuild_path, {"AFDO": self.new_var_value}
394 )
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000395
Alex Klein1699fab2022-09-08 08:46:06 -0600396 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000397
Alex Klein1699fab2022-09-08 08:46:06 -0600398 def test_patch_ebuild_vars_no_vars(self):
399 """patch_ebuild_vars keeps ebuild intact if there are no vars."""
400 ebuild_contents = (
401 "This line does not change.\n"
402 "The line with AFDO_PROFILE_VERSION is also unchanged."
403 )
404 self.mock_input.return_value = io.StringIO(ebuild_contents)
405 expected_calls = []
406 for line in io.StringIO(ebuild_contents):
407 expected_calls.append(mock.call(line))
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000408
Alex Klein1699fab2022-09-08 08:46:06 -0600409 packages.patch_ebuild_vars(
410 self.ebuild_path, {"AFDO_PROFILE_VERSION": self.new_var_value}
411 )
412
413 self.mock_stdout_write.assert_has_calls(expected_calls)
Madeleine Hardt8ae7f102022-03-24 20:26:11 +0000414
415
Alex Klein87531182019-08-12 15:23:37 -0600416class UprevsVersionedPackageTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600417 """uprevs_versioned_package decorator test."""
Alex Klein87531182019-08-12 15:23:37 -0600418
Alex Klein1699fab2022-09-08 08:46:06 -0600419 @packages.uprevs_versioned_package("category/package")
420 def uprev_category_package(self, *args, **kwargs):
421 """Registered function for testing."""
Alex Klein87531182019-08-12 15:23:37 -0600422
Alex Klein1699fab2022-09-08 08:46:06 -0600423 def test_calls_function(self):
424 """Test calling a registered function."""
425 self.PatchObject(self, "uprev_category_package")
Alex Klein87531182019-08-12 15:23:37 -0600426
Alex Klein1699fab2022-09-08 08:46:06 -0600427 cpv = package_info.SplitCPV("category/package", strict=False)
428 packages.uprev_versioned_package(cpv, [], [], Chroot())
Alex Klein87531182019-08-12 15:23:37 -0600429
Alex Kleinfee86da2023-01-20 18:40:06 -0700430 # TODO(crbug/1065172): Invalid assertion that was previously mocked.
Alex Klein1699fab2022-09-08 08:46:06 -0600431 # patch.assert_called()
Alex Klein87531182019-08-12 15:23:37 -0600432
Alex Klein1699fab2022-09-08 08:46:06 -0600433 def test_unregistered_package(self):
434 """Test calling with an unregistered package."""
435 cpv = package_info.SplitCPV("does-not/exist", strict=False)
Alex Klein87531182019-08-12 15:23:37 -0600436
Alex Klein1699fab2022-09-08 08:46:06 -0600437 with self.assertRaises(packages.UnknownPackageError):
438 packages.uprev_versioned_package(cpv, [], [], Chroot())
Alex Klein87531182019-08-12 15:23:37 -0600439
440
Trent Begin6daa8702020-01-29 14:58:12 -0700441class UprevEbuildFromPinTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600442 """Tests uprev_ebuild_from_pin function"""
Trent Begin315d9d92019-12-03 21:55:53 -0700443
Alex Klein1699fab2022-09-08 08:46:06 -0600444 package = "category/package"
445 version = "1.2.3"
446 new_version = "1.2.4"
447 ebuild_template = "package-%s-r1.ebuild"
448 ebuild = ebuild_template % version
449 unstable_ebuild = "package-9999.ebuild"
450 manifest = "Manifest"
Trent Begin315d9d92019-12-03 21:55:53 -0700451
Alex Klein1699fab2022-09-08 08:46:06 -0600452 def test_uprev_ebuild(self):
453 """Tests uprev of ebuild with version path"""
454 file_layout = (
455 D(self.package, [self.ebuild, self.unstable_ebuild, self.manifest]),
456 )
457 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700458
Jack Rosenthal451b0262023-09-13 12:52:16 -0600459 package_path = self.tempdir / self.package
Trent Begin315d9d92019-12-03 21:55:53 -0700460
Jack Rosenthal451b0262023-09-13 12:52:16 -0600461 ebuild_path = package_path / self.ebuild
Alex Klein1699fab2022-09-08 08:46:06 -0600462 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000463
Alex Klein1699fab2022-09-08 08:46:06 -0600464 result = uprev_lib.uprev_ebuild_from_pin(
465 package_path, self.new_version, chroot=Chroot()
466 )
467 self.assertEqual(
468 len(result.modified),
469 1,
470 "unexpected number of results: %s" % len(result.modified),
471 )
Trent Begin315d9d92019-12-03 21:55:53 -0700472
Alex Klein1699fab2022-09-08 08:46:06 -0600473 mod = result.modified[0]
474 self.assertEqual(
475 mod.new_version,
476 self.new_version + "-r1",
477 "unexpected version number: %s" % mod.new_version,
478 )
Trent Begin315d9d92019-12-03 21:55:53 -0700479
Jack Rosenthal451b0262023-09-13 12:52:16 -0600480 old_ebuild_path = package_path / (self.ebuild_template % self.version)
481 new_ebuild_path = package_path / (
482 self.ebuild_template % self.new_version
Alex Klein1699fab2022-09-08 08:46:06 -0600483 )
Jack Rosenthal451b0262023-09-13 12:52:16 -0600484 manifest_path = package_path / "Manifest"
Trent Begin2e5344f2020-03-02 10:46:55 -0700485
Alex Klein1699fab2022-09-08 08:46:06 -0600486 expected_modified_files = [
487 old_ebuild_path,
488 new_ebuild_path,
489 manifest_path,
490 ]
491 self.assertCountEqual(mod.files, expected_modified_files)
Trent Begin4a11a632020-02-28 12:59:58 -0700492
Alex Klein1699fab2022-09-08 08:46:06 -0600493 self.assertCommandContains(["ebuild", "manifest"])
Trent Begin6daa8702020-01-29 14:58:12 -0700494
Alex Klein1699fab2022-09-08 08:46:06 -0600495 def test_uprev_ebuild_same_version(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700496 """Tests uprev of ebuild with version path with unchanged version.
Fergus Dall2209d0b2020-08-06 11:51:43 +1000497
Alex Klein1699fab2022-09-08 08:46:06 -0600498 This should result in bumping the revision number.
499 """
500 file_layout = (
501 D(self.package, [self.ebuild, self.unstable_ebuild, self.manifest]),
502 )
503 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000504
Jack Rosenthal451b0262023-09-13 12:52:16 -0600505 package_path = self.tempdir / self.package
Fergus Dall2209d0b2020-08-06 11:51:43 +1000506
Jack Rosenthal451b0262023-09-13 12:52:16 -0600507 ebuild_path = package_path / self.ebuild
Alex Klein1699fab2022-09-08 08:46:06 -0600508 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000509
Alex Klein1699fab2022-09-08 08:46:06 -0600510 result = uprev_lib.uprev_ebuild_from_pin(
511 package_path, self.version, chroot=Chroot()
512 )
513 self.assertEqual(
514 len(result.modified),
515 1,
516 "unexpected number of results: %s" % len(result.modified),
517 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000518
Alex Klein1699fab2022-09-08 08:46:06 -0600519 mod = result.modified[0]
520 self.assertEqual(
521 mod.new_version,
522 self.version + "-r2",
523 "unexpected version number: %s" % mod.new_version,
524 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000525
Jack Rosenthal451b0262023-09-13 12:52:16 -0600526 old_ebuild_path = package_path / (self.ebuild_template % self.version)
527 new_ebuild_path = package_path / ("package-%s-r2.ebuild" % self.version)
528 manifest_path = package_path / "Manifest"
Fergus Dall2209d0b2020-08-06 11:51:43 +1000529
Alex Klein1699fab2022-09-08 08:46:06 -0600530 expected_modified_files = [
531 old_ebuild_path,
532 new_ebuild_path,
533 manifest_path,
534 ]
535 self.assertCountEqual(mod.files, expected_modified_files)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000536
Alex Klein1699fab2022-09-08 08:46:06 -0600537 self.assertCommandContains(["ebuild", "manifest"])
Fergus Dall2209d0b2020-08-06 11:51:43 +1000538
Alex Klein1699fab2022-09-08 08:46:06 -0600539 def test_no_ebuild(self):
540 """Tests assertion is raised if package has no ebuilds"""
541 file_layout = (D(self.package, [self.manifest]),)
542 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700543
Alex Klein1699fab2022-09-08 08:46:06 -0600544 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700545
Alex Klein1699fab2022-09-08 08:46:06 -0600546 with self.assertRaises(uprev_lib.EbuildUprevError):
547 uprev_lib.uprev_ebuild_from_pin(
548 package_path, self.new_version, chroot=Chroot()
549 )
Trent Begin315d9d92019-12-03 21:55:53 -0700550
Alex Klein1699fab2022-09-08 08:46:06 -0600551 def test_multiple_stable_ebuilds(self):
552 """Tests assertion is raised if multiple stable ebuilds are present"""
553 file_layout = (
554 D(
555 self.package,
556 [self.ebuild, self.ebuild_template % "1.2.1", self.manifest],
557 ),
558 )
559 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000560
Alex Klein1699fab2022-09-08 08:46:06 -0600561 package_path = os.path.join(self.tempdir, self.package)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000562
Alex Klein1699fab2022-09-08 08:46:06 -0600563 ebuild_path = os.path.join(package_path, self.ebuild)
564 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000565
Alex Klein1699fab2022-09-08 08:46:06 -0600566 ebuild_path = os.path.join(package_path, self.ebuild_template % "1.2.1")
567 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000568
Alex Klein1699fab2022-09-08 08:46:06 -0600569 with self.assertRaises(uprev_lib.EbuildUprevError):
570 uprev_lib.uprev_ebuild_from_pin(
571 package_path, self.new_version, chroot=Chroot()
572 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000573
Alex Klein1699fab2022-09-08 08:46:06 -0600574 def test_multiple_unstable_ebuilds(self):
575 """Tests assertion is raised if multiple unstable ebuilds are present"""
576 file_layout = (
577 D(
578 self.package,
579 [self.ebuild, self.ebuild_template % "1.2.1", self.manifest],
580 ),
581 )
582 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700583
Alex Klein1699fab2022-09-08 08:46:06 -0600584 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700585
Alex Klein1699fab2022-09-08 08:46:06 -0600586 with self.assertRaises(uprev_lib.EbuildUprevError):
587 uprev_lib.uprev_ebuild_from_pin(
588 package_path, self.new_version, chroot=Chroot()
589 )
Trent Begin315d9d92019-12-03 21:55:53 -0700590
591
Andrew Lamb9563a152019-12-04 11:42:18 -0700592class ReplicatePrivateConfigTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600593 """replicate_private_config tests."""
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700594
Alex Klein1699fab2022-09-08 08:46:06 -0600595 def setUp(self):
596 # Set up fake public and private chromeos-config overlays.
597 private_package_root = (
598 "src/private-overlays/overlay-coral-private/chromeos-base/"
599 "chromeos-config-bsp"
600 )
601 self.public_package_root = (
602 "src/overlays/overlay-coral/chromeos-base/chromeos-config-bsp"
603 )
604 file_layout = (
605 D(
606 os.path.join(private_package_root, "files"),
607 ["build_config.json"],
608 ),
609 D(private_package_root, ["replication_config.jsonpb"]),
610 D(
611 os.path.join(self.public_package_root, "files"),
612 ["build_config.json"],
613 ),
614 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700615
Alex Klein1699fab2022-09-08 08:46:06 -0600616 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700617
Alex Klein1699fab2022-09-08 08:46:06 -0600618 # Private config contains 'a' and 'b' fields.
619 self.private_config_path = os.path.join(
620 private_package_root, "files", "build_config.json"
621 )
622 self.WriteTempFile(
623 self.private_config_path,
624 json.dumps({"chromeos": {"configs": [{"a": 3, "b": 2}]}}),
625 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700626
Alex Kleinfee86da2023-01-20 18:40:06 -0700627 # Public config only contains the 'a' field. Note that the value of 'a'
628 # is 1 in the public config; it will get updated to 3 when the private
629 # config is replicated.
Alex Klein1699fab2022-09-08 08:46:06 -0600630 self.public_config_path = os.path.join(
631 self.public_package_root, "files", "build_config.json"
632 )
633 self.WriteTempFile(
634 self.public_config_path,
635 json.dumps({"chromeos": {"configs": [{"a": 1}]}}),
636 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700637
Alex Klein1699fab2022-09-08 08:46:06 -0600638 # Put a ReplicationConfig JSONPB in the private package. Note that it
639 # specifies only the 'a' field is replicated.
640 self.replication_config_path = os.path.join(
641 self.tempdir, private_package_root, "replication_config.jsonpb"
642 )
643 replication_config = ReplicationConfig(
644 file_replication_rules=[
645 FileReplicationRule(
646 source_path=self.private_config_path,
647 destination_path=self.public_config_path,
648 file_type=FILE_TYPE_JSON,
649 replication_type=REPLICATION_TYPE_FILTER,
650 destination_fields=FieldMask(paths=["a"]),
651 )
652 ]
653 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700654
Alex Klein1699fab2022-09-08 08:46:06 -0600655 osutils.WriteFile(
656 self.replication_config_path,
657 json_format.MessageToJson(replication_config),
658 )
659 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700660
Alex Klein1699fab2022-09-08 08:46:06 -0600661 self.rc.SetDefaultCmdResult(side_effect=self._write_generated_c_files)
Andrew Lamb9563a152019-12-04 11:42:18 -0700662
Alex Klein1699fab2022-09-08 08:46:06 -0600663 def _write_generated_c_files(self, *_args, **_kwargs):
664 """Write fake generated C files to the public output dir.
Andrew Lamb9563a152019-12-04 11:42:18 -0700665
Alex Kleinfee86da2023-01-20 18:40:06 -0700666 Note that this function accepts args and kwargs so it can be used as a
667 side effect.
Alex Klein1699fab2022-09-08 08:46:06 -0600668 """
669 output_dir = os.path.join(self.public_package_root, "files")
670 self.WriteTempFile(os.path.join(output_dir, "config.c"), "")
671 self.WriteTempFile(os.path.join(output_dir, "ec_config.c"), "")
672 self.WriteTempFile(os.path.join(output_dir, "ec_config.h"), "")
Andrew Lamb9563a152019-12-04 11:42:18 -0700673
Alex Klein1699fab2022-09-08 08:46:06 -0600674 def _write_incorrect_generated_c_files(self, *_args, **_kwargs):
675 """Similar to _write_generated_c_files, with an expected file missing.
Andrew Lamb9563a152019-12-04 11:42:18 -0700676
Alex Kleinfee86da2023-01-20 18:40:06 -0700677 Note that this function accepts args and kwargs so it can be used as a
678 side effect.
Alex Klein1699fab2022-09-08 08:46:06 -0600679 """
680 output_dir = os.path.join(self.public_package_root, "files")
681 self.WriteTempFile(os.path.join(output_dir, "config.c"), "")
682 self.WriteTempFile(os.path.join(output_dir, "ec_config.c"), "")
Andrew Lamb9563a152019-12-04 11:42:18 -0700683
Alex Klein1699fab2022-09-08 08:46:06 -0600684 def test_replicate_private_config(self):
685 """Basic replication test."""
686 refs = [
687 GitRef(
688 path="/chromeos/overlays/overlay-coral-private",
689 ref="main",
690 revision="123",
691 )
692 ]
693 chroot = Chroot()
694 result = packages.replicate_private_config(
695 _build_targets=None, refs=refs, chroot=chroot
696 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700697
Alex Klein1699fab2022-09-08 08:46:06 -0600698 self.assertCommandContains(
699 [
700 "cros_config_schema",
701 "-m",
702 os.path.join(
703 constants.CHROOT_SOURCE_ROOT, self.public_config_path
704 ),
705 "-g",
706 os.path.join(
707 constants.CHROOT_SOURCE_ROOT,
708 self.public_package_root,
709 "files",
710 ),
711 "-f",
712 '"TRUE"',
713 ],
714 enter_chroot=True,
715 chroot_args=chroot.get_enter_args(),
716 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700717
Alex Klein1699fab2022-09-08 08:46:06 -0600718 self.assertEqual(len(result.modified), 1)
719 # The public build_config.json and generated C files were modified.
720 expected_modified_files = [
721 os.path.join(self.tempdir, self.public_config_path),
722 os.path.join(
723 self.tempdir, self.public_package_root, "files", "config.c"
724 ),
725 os.path.join(
726 self.tempdir, self.public_package_root, "files", "ec_config.c"
727 ),
728 os.path.join(
729 self.tempdir, self.public_package_root, "files", "ec_config.h"
730 ),
731 ]
732 self.assertEqual(result.modified[0].files, expected_modified_files)
733 self.assertEqual(result.modified[0].new_version, "123")
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700734
Alex Kleinfee86da2023-01-20 18:40:06 -0700735 # The update from the private build_config.json was copied to the
736 # public. Note that only the 'a' field is present, as per
737 # destination_fields.
Alex Klein1699fab2022-09-08 08:46:06 -0600738 self.assertEqual(
739 json.loads(self.ReadTempFile(self.public_config_path)),
740 {"chromeos": {"configs": [{"a": 3}]}},
741 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700742
Alex Klein1699fab2022-09-08 08:46:06 -0600743 def test_replicate_private_config_no_build_config(self):
744 """If there is no build config, don't generate C files."""
Alex Kleinfee86da2023-01-20 18:40:06 -0700745 # Modify the replication config to write to "other_config.json" instead
746 # of "build_config.json"
Alex Klein1699fab2022-09-08 08:46:06 -0600747 modified_destination_path = self.public_config_path.replace(
748 "build_config", "other_config"
749 )
750 replication_config = ReplicationConfig(
751 file_replication_rules=[
752 FileReplicationRule(
753 source_path=self.private_config_path,
754 destination_path=modified_destination_path,
755 file_type=FILE_TYPE_JSON,
756 replication_type=REPLICATION_TYPE_FILTER,
757 destination_fields=FieldMask(paths=["a"]),
758 )
759 ]
760 )
761 osutils.WriteFile(
762 self.replication_config_path,
763 json_format.MessageToJson(replication_config),
764 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700765
Alex Klein1699fab2022-09-08 08:46:06 -0600766 refs = [
767 GitRef(
768 path="/chromeos/overlays/overlay-coral-private",
769 ref="main",
770 revision="123",
771 )
772 ]
773 result = packages.replicate_private_config(
774 _build_targets=None, refs=refs, chroot=Chroot()
775 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700776
Alex Klein1699fab2022-09-08 08:46:06 -0600777 self.assertEqual(len(result.modified), 1)
778 self.assertEqual(
779 result.modified[0].files,
780 [os.path.join(self.tempdir, modified_destination_path)],
781 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700782
Alex Klein1699fab2022-09-08 08:46:06 -0600783 def test_replicate_private_config_multiple_build_configs(self):
784 """An error is thrown if there is more than one build config."""
785 replication_config = ReplicationConfig(
786 file_replication_rules=[
787 FileReplicationRule(
788 source_path=self.private_config_path,
789 destination_path=self.public_config_path,
790 file_type=FILE_TYPE_JSON,
791 replication_type=REPLICATION_TYPE_FILTER,
792 destination_fields=FieldMask(paths=["a"]),
793 ),
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 ]
802 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700803
Alex Klein1699fab2022-09-08 08:46:06 -0600804 osutils.WriteFile(
805 self.replication_config_path,
806 json_format.MessageToJson(replication_config),
807 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700808
Alex Klein1699fab2022-09-08 08:46:06 -0600809 refs = [
810 GitRef(
811 path="/chromeos/overlays/overlay-coral-private",
812 ref="main",
813 revision="123",
814 )
815 ]
816 with self.assertRaisesRegex(
817 ValueError,
818 "Expected at most one build_config.json destination path.",
819 ):
820 packages.replicate_private_config(
821 _build_targets=None, refs=refs, chroot=Chroot()
822 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700823
Alex Klein1699fab2022-09-08 08:46:06 -0600824 def test_replicate_private_config_generated_files_incorrect(self):
825 """An error is thrown if generated C files are missing."""
826 self.rc.SetDefaultCmdResult(
827 side_effect=self._write_incorrect_generated_c_files
828 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700829
Alex Klein1699fab2022-09-08 08:46:06 -0600830 refs = [
831 GitRef(
832 path="/chromeos/overlays/overlay-coral-private",
833 ref="main",
834 revision="123",
835 )
836 ]
837 chroot = Chroot()
Andrew Lamb9563a152019-12-04 11:42:18 -0700838
Alex Klein1699fab2022-09-08 08:46:06 -0600839 with self.assertRaisesRegex(
840 packages.GeneratedCrosConfigFilesError,
841 "Expected to find generated C files",
842 ):
843 packages.replicate_private_config(
844 _build_targets=None, refs=refs, chroot=chroot
845 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700846
Alex Klein1699fab2022-09-08 08:46:06 -0600847 def test_replicate_private_config_wrong_number_of_refs(self):
848 """An error is thrown if there is not exactly one ref."""
849 with self.assertRaisesRegex(ValueError, "Expected exactly one ref"):
850 packages.replicate_private_config(
851 _build_targets=None, refs=[], chroot=None
852 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700853
Alex Klein1699fab2022-09-08 08:46:06 -0600854 with self.assertRaisesRegex(ValueError, "Expected exactly one ref"):
855 refs = [
856 GitRef(path="a", ref="main", revision="1"),
857 GitRef(path="a", ref="main", revision="2"),
858 ]
859 packages.replicate_private_config(
860 _build_targets=None, refs=refs, chroot=None
861 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700862
Alex Klein1699fab2022-09-08 08:46:06 -0600863 def test_replicate_private_config_replication_config_missing(self):
864 """An error is thrown if there is not a replication config."""
865 os.remove(self.replication_config_path)
866 with self.assertRaisesRegex(
867 ValueError,
868 "Expected ReplicationConfig missing at %s"
869 % self.replication_config_path,
870 ):
871 refs = [
872 GitRef(
873 path="/chromeos/overlays/overlay-coral-private",
874 ref="main",
875 revision="123",
876 )
877 ]
878 packages.replicate_private_config(
879 _build_targets=None, refs=refs, chroot=None
880 )
Andrew Lambe836f222019-12-09 12:27:38 -0700881
Alex Klein1699fab2022-09-08 08:46:06 -0600882 def test_replicate_private_config_wrong_git_ref_path(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700883 """Git ref that doesn't point to a private overlay throws error."""
Alex Klein1699fab2022-09-08 08:46:06 -0600884 with self.assertRaisesRegex(
885 ValueError, "ref.path must match the pattern"
886 ):
887 refs = [GitRef(path="a/b/c", ref="main", revision="123")]
888 packages.replicate_private_config(
889 _build_targets=None, refs=refs, chroot=None
890 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700891
892
Alex Klein5caab872021-09-10 11:44:37 -0600893class GetBestVisibleTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600894 """get_best_visible tests."""
David Burger1e0fe232019-07-01 14:52:07 -0600895
Alex Klein1699fab2022-09-08 08:46:06 -0600896 def test_empty_atom_fails(self):
897 """Test empty atom raises an error."""
898 with self.assertRaises(AssertionError):
899 packages.get_best_visible("")
Alex Kleinda39c6d2019-09-16 14:36:36 -0600900
901
Alex Klein149fd3b2019-12-16 16:01:05 -0700902class HasPrebuiltTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600903 """has_prebuilt tests."""
Alex Kleinda39c6d2019-09-16 14:36:36 -0600904
Alex Klein1699fab2022-09-08 08:46:06 -0600905 def test_empty_atom_fails(self):
906 """Test an empty atom results in an error."""
907 with self.assertRaises(AssertionError):
908 packages.has_prebuilt("")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600909
Alex Klein1699fab2022-09-08 08:46:06 -0600910 def test_use_flags(self):
911 """Test use flags get propagated correctly."""
912 # We don't really care about the result, just the env handling.
913 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
914 # Ignore any flags that may be in the environment.
915 self.PatchObject(os.environ, "get", return_value="")
Alex Klein149fd3b2019-12-16 16:01:05 -0700916
Alex Klein1699fab2022-09-08 08:46:06 -0600917 packages.has_prebuilt("cat/pkg-1.2.3", useflags="useflag")
918 patch.assert_called_with(
919 "cat/pkg-1.2.3", board=None, extra_env={"USE": "useflag"}
920 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700921
Alex Klein1699fab2022-09-08 08:46:06 -0600922 def test_env_use_flags(self):
923 """Test env use flags get propagated correctly with passed useflags."""
924 # We don't really care about the result, just the env handling.
925 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
926 # Add some flags to the environment.
927 existing_flags = "already set flags"
928 self.PatchObject(os.environ, "get", return_value=existing_flags)
Alex Klein149fd3b2019-12-16 16:01:05 -0700929
Alex Klein1699fab2022-09-08 08:46:06 -0600930 new_flags = "useflag"
931 packages.has_prebuilt("cat/pkg-1.2.3", useflags=new_flags)
932 expected = "%s %s" % (existing_flags, new_flags)
933 patch.assert_called_with(
934 "cat/pkg-1.2.3", board=None, extra_env={"USE": expected}
935 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700936
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600937
938class AndroidVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600939 """Tests getting android versions."""
Michael Mortensen14960d02019-10-18 07:53:59 -0600940
Alex Klein1699fab2022-09-08 08:46:06 -0600941 def setUp(self):
942 package_result = [
943 "chromeos-base/android-container-nyc-4717008-r1",
944 "chromeos-base/update_engine-0.0.3-r3408",
945 ]
946 self.PatchObject(
947 portage_util, "GetPackageDependencies", return_value=package_result
948 )
949 self.board = "board"
950 self.PatchObject(
951 portage_util,
952 "FindEbuildForBoardPackage",
953 return_value="chromeos-base/android-container-nyc",
954 )
955 FakeEnvironment = {
956 "ARM_TARGET": "3-linux-target",
957 }
958 self.PatchObject(
959 osutils, "SourceEnvironment", return_value=FakeEnvironment
960 )
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600961
Alex Kleinfee86da2023-01-20 18:40:06 -0700962 # Clear the LRU cache for the function. We mock the function that
963 # provides the data this function processes to produce its result, so we
964 # need to clear it manually.
Alex Klein1699fab2022-09-08 08:46:06 -0600965 packages.determine_android_package.cache_clear()
Alex Klein68a28712021-11-08 11:08:30 -0700966
Alex Klein1699fab2022-09-08 08:46:06 -0600967 def test_determine_android_version(self):
968 """Tests that a valid android version is returned."""
969 version = packages.determine_android_version(self.board)
970 self.assertEqual(version, "4717008")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600971
Alex Klein1699fab2022-09-08 08:46:06 -0600972 def test_determine_android_version_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700973 """Test None is returned for version when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -0600974 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
975 self.PatchObject(
976 portage_util, "GetPackageDependencies", return_value=package_result
977 )
978 version = packages.determine_android_version(self.board)
979 self.assertEqual(version, None)
Michael Mortensenedf76532019-10-16 14:22:37 -0600980
Alex Klein1699fab2022-09-08 08:46:06 -0600981 def test_determine_android_branch(self):
982 """Tests that a valid android branch is returned."""
983 branch = packages.determine_android_branch(self.board)
984 self.assertEqual(branch, "3")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600985
Alex Klein1699fab2022-09-08 08:46:06 -0600986 def test_determine_android_branch_64bit_targets(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700987 """Tests a valid android branch is returned with only 64bit targets."""
Alex Klein1699fab2022-09-08 08:46:06 -0600988 self.PatchObject(
989 osutils,
990 "SourceEnvironment",
991 return_value={"ARM64_TARGET": "3-linux-target"},
992 )
993 branch = packages.determine_android_branch(self.board)
994 self.assertEqual(branch, "3")
Federico 'Morg' Pareschicd9165a2020-05-29 09:45:55 +0900995
Alex Klein1699fab2022-09-08 08:46:06 -0600996 def test_determine_android_branch_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700997 """Tests a None is returned for branch when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -0600998 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
999 self.PatchObject(
1000 portage_util, "GetPackageDependencies", return_value=package_result
1001 )
1002 branch = packages.determine_android_branch(self.board)
1003 self.assertEqual(branch, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001004
Alex Klein1699fab2022-09-08 08:46:06 -06001005 def test_determine_android_target(self):
1006 """Tests that a valid android target is returned."""
1007 target = packages.determine_android_target(self.board)
1008 self.assertEqual(target, "cheets")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001009
Alex Klein1699fab2022-09-08 08:46:06 -06001010 def test_determine_android_target_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001011 """Tests a None is returned for target when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -06001012 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
1013 self.PatchObject(
1014 portage_util, "GetPackageDependencies", return_value=package_result
1015 )
1016 target = packages.determine_android_target(self.board)
1017 self.assertEqual(target, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001018
Alex Klein1699fab2022-09-08 08:46:06 -06001019 def test_determine_android_version_handle_exception(self):
1020 """Tests handling RunCommandError inside determine_android_version."""
Alex Kleinfee86da2023-01-20 18:40:06 -07001021 # Mock what happens when portage returns that bubbles up (via
1022 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001023 self.PatchObject(
1024 portage_util,
1025 "GetPackageDependencies",
1026 side_effect=cros_build_lib.RunCommandError("error"),
1027 )
1028 target = packages.determine_android_version(self.board)
1029 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -06001030
Alex Klein1699fab2022-09-08 08:46:06 -06001031 def test_determine_android_package_handle_exception(self):
1032 """Tests handling RunCommandError inside determine_android_package."""
Alex Kleinfee86da2023-01-20 18:40:06 -07001033 # Mock what happens when portage returns that bubbles up (via
1034 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001035 self.PatchObject(
1036 portage_util,
1037 "GetPackageDependencies",
1038 side_effect=cros_build_lib.RunCommandError("error"),
1039 )
1040 target = packages.determine_android_package(self.board)
1041 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -06001042
Alex Klein1699fab2022-09-08 08:46:06 -06001043 def test_determine_android_package_callers_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001044 """Tests RunCommandError caught by determine_android_package callers."""
1045 # Mock what happens when portage returns that bubbles up (via
1046 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001047 self.PatchObject(
1048 portage_util,
1049 "GetPackageDependencies",
1050 side_effect=cros_build_lib.RunCommandError("error"),
1051 )
1052 # Verify that target is None, as expected.
1053 target = packages.determine_android_package(self.board)
1054 self.assertEqual(target, None)
1055 # determine_android_branch calls determine_android_package
1056 branch = packages.determine_android_branch(self.board)
1057 self.assertEqual(branch, None)
1058 # determine_android_target calls determine_android_package
1059 target = packages.determine_android_target(self.board)
1060 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001061
Michael Mortensene0f4b542019-10-24 15:30:23 -06001062
Alex Klein1699fab2022-09-08 08:46:06 -06001063@pytest.mark.usefixtures("testcase_caplog", "testcase_monkeypatch")
Michael Mortensende716a12020-05-15 11:27:00 -06001064class FindFingerprintsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001065 """Tests for find_fingerprints."""
Michael Mortensende716a12020-05-15 11:27:00 -06001066
Alex Klein1699fab2022-09-08 08:46:06 -06001067 def setUp(self):
1068 self.board = "test-board"
1069 # Create cheets-fingerprints.txt based on tempdir/src...
1070 self.fingerprint_contents = (
1071 "google/test-board/test-board_cheets"
1072 ":9/R99-12345.0.9999/123456:user/release-keys"
1073 )
1074 fingerprint_path = os.path.join(
1075 self.tempdir,
1076 "src/build/images/test-board/latest/cheets-fingerprint.txt",
1077 )
Brian Norris4f251e42023-03-09 15:53:26 -08001078 self.chroot = Chroot(
1079 path=self.tempdir / "chroot", out_path=self.tempdir / "out"
1080 )
Alex Klein1699fab2022-09-08 08:46:06 -06001081 osutils.WriteFile(
1082 fingerprint_path, self.fingerprint_contents, makedirs=True
1083 )
Michael Mortensende716a12020-05-15 11:27:00 -06001084
Alex Klein1699fab2022-09-08 08:46:06 -06001085 def test_find_fingerprints_with_test_path(self):
1086 """Tests get_firmware_versions with mocked output."""
1087 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1088 build_target = build_target_lib.BuildTarget(self.board)
1089 result = packages.find_fingerprints(build_target)
1090 self.assertEqual(result, [self.fingerprint_contents])
1091 self.assertIn("Reading fingerprint file", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001092
Alex Klein1699fab2022-09-08 08:46:06 -06001093 def test_find_fingerprints(self):
1094 """Tests get_firmware_versions with mocked output."""
1095 # Use board name whose path for fingerprint file does not exist.
1096 # Verify that fingerprint file is not found and None is returned.
1097 build_target = build_target_lib.BuildTarget("wrong-boardname")
1098 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1099 result = packages.find_fingerprints(build_target)
1100 self.assertEqual(result, [])
1101 self.assertIn("Fingerprint file not found", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001102
1103
Michael Mortensen59e30872020-05-18 14:12:49 -06001104class GetAllFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001105 """Tests for get_firmware_versions."""
Michael Mortensen59e30872020-05-18 14:12:49 -06001106
Alex Klein1699fab2022-09-08 08:46:06 -06001107 def setUp(self):
1108 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001109 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001110 self.rc.SetDefaultCmdResult(
1111 stdout="""
Michael Mortensen59e30872020-05-18 14:12:49 -06001112
1113flashrom(8): 68935ee2fcfcffa47af81b966269cd2b */build/reef/usr/sbin/flashrom
1114 ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=e102cc98d45300b50088999d53775acbeff407dc, stripped
1115 0.9.9 : bbb2d6a : Jul 28 2017 15:12:34 UTC
1116
1117Model: reef
1118BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1119BIOS version: Google_Reef.9042.87.1
1120BIOS (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
1121BIOS (RW) version: Google_Reef.9042.110.0
1122EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1123EC version: reef_v1.1.5900-ab1ee51
1124EC (RW) version: reef_v1.1.5909-bd1f0c9
1125
1126Model: pyro
1127BIOS image: 9e62447ebf22a724a4a835018ab6234e */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/image.bin
1128BIOS version: Google_Pyro.9042.87.1
1129BIOS (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
1130BIOS (RW) version: Google_Pyro.9042.110.0
1131EC image: 44b93ed591733519e752e05aa0529eb5 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/ec.bin
1132EC version: pyro_v1.1.5900-ab1ee51
1133EC (RW) version: pyro_v1.1.5909-bd1f0c9
1134
1135Model: snappy
1136BIOS image: 3ab63ff080596bd7de4e7619f003bb64 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/image.bin
1137BIOS version: Google_Snappy.9042.110.0
1138EC image: c4db159e84428391d2ee25368c5fe5b6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/ec.bin
1139EC version: snappy_v1.1.5909-bd1f0c9
1140
1141Model: sand
1142BIOS image: 387da034a4f0a3f53e278ebfdcc2a412 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/image.bin
1143BIOS version: Google_Sand.9042.110.0
1144EC image: 411562e0589dacec131f5fdfbe95a561 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/ec.bin
1145EC version: sand_v1.1.5909-bd1f0c9
1146
1147Model: electro
1148BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1149BIOS version: Google_Reef.9042.87.1
1150BIOS (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
1151BIOS (RW) version: Google_Reef.9042.110.0
1152EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1153EC version: reef_v1.1.5900-ab1ee51
1154EC (RW) version: reef_v1.1.5909-bd1f0c9
1155
1156Package Content:
1157612e7bb6ed1fb0a05abf2ebdc834c18b *./updater4.sh
11580eafbee07282315829d0f42135ec7c0c *./gbb_utility
11596074e3ca424cb30a67c378c1d9681f9c *./mosys
116068935ee2fcfcffa47af81b966269cd2b *./flashrom
11610eafbee07282315829d0f42135ec7c0c *./dump_fmap
1162490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
116360899148600b8673ddb711faa55aee40 *./common.sh
11643c3a99346d1ca1273cbcd86c104851ff *./shflags
1165de7ce035e1f82a89f8909d888ee402c0 *./crosutil.sh
1166f9334372bdb9036ba09a6fd9bf30e7a2 *./crossystem
116722257a8d5f0adc1f50a1916c3a4a35dd *./models/reef/ec.bin
1168faf12dbb7cdaf21ce153bdffb67841fd *./models/reef/bios.bin
1169c9bbb417b7921b85a7ed999ee42f550e *./models/reef/setvars.sh
117029823d46f1ec1491ecacd7b830fd2686 *./models/pyro/ec.bin
11712320463aba8b22eb5ea836f094d281b3 *./models/pyro/bios.bin
117281614833ad77c9cd093360ba7bea76b8 *./models/pyro/setvars.sh
1173411562e0589dacec131f5fdfbe95a561 *./models/sand/ec.bin
1174387da034a4f0a3f53e278ebfdcc2a412 *./models/sand/bios.bin
1175fcd8cb0ac0e2ed6be220aaae435d43ff *./models/sand/setvars.sh
1176c4db159e84428391d2ee25368c5fe5b6 *./models/snappy/ec.bin
11773ab63ff080596bd7de4e7619f003bb64 *./models/snappy/bios.bin
1178fe5d699f2e9e4a7de031497953313dbd *./models/snappy/setvars.sh
117979aabd7cd8a215a54234c53d7bb2e6fb *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001180"""
1181 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001182 # pylint: enable=line-too-long
Michael Mortensen59e30872020-05-18 14:12:49 -06001183
Alex Klein1699fab2022-09-08 08:46:06 -06001184 def test_get_firmware_versions(self):
1185 """Tests get_firmware_versions with mocked output."""
1186 build_target = build_target_lib.BuildTarget(self.board)
1187 result = packages.get_all_firmware_versions(build_target)
1188 self.assertEqual(len(result), 5)
1189 self.assertEqual(
1190 result["reef"],
1191 packages.FirmwareVersions(
1192 "reef",
1193 "Google_Reef.9042.87.1",
1194 "Google_Reef.9042.110.0",
1195 "reef_v1.1.5900-ab1ee51",
1196 "reef_v1.1.5909-bd1f0c9",
1197 ),
1198 )
1199 self.assertEqual(
1200 result["pyro"],
1201 packages.FirmwareVersions(
1202 "pyro",
1203 "Google_Pyro.9042.87.1",
1204 "Google_Pyro.9042.110.0",
1205 "pyro_v1.1.5900-ab1ee51",
1206 "pyro_v1.1.5909-bd1f0c9",
1207 ),
1208 )
1209 self.assertEqual(
1210 result["snappy"],
1211 packages.FirmwareVersions(
1212 "snappy",
1213 "Google_Snappy.9042.110.0",
1214 None,
1215 "snappy_v1.1.5909-bd1f0c9",
1216 None,
1217 ),
1218 )
1219 self.assertEqual(
1220 result["sand"],
1221 packages.FirmwareVersions(
1222 "sand",
1223 "Google_Sand.9042.110.0",
1224 None,
1225 "sand_v1.1.5909-bd1f0c9",
1226 None,
1227 ),
1228 )
1229 self.assertEqual(
1230 result["electro"],
1231 packages.FirmwareVersions(
1232 "electro",
1233 "Google_Reef.9042.87.1",
1234 "Google_Reef.9042.110.0",
1235 "reef_v1.1.5900-ab1ee51",
1236 "reef_v1.1.5909-bd1f0c9",
1237 ),
1238 )
Michael Mortensen59e30872020-05-18 14:12:49 -06001239
Alex Klein1699fab2022-09-08 08:46:06 -06001240 def test_get_firmware_versions_error(self):
1241 """Tests get_firmware_versions with no output."""
1242 # Throw an exception when running the command.
Mike Frysinger5d85a542023-07-06 16:05:54 -04001243 self.rc.SetDefaultCmdResult(returncode=1)
Alex Klein1699fab2022-09-08 08:46:06 -06001244 build_target = build_target_lib.BuildTarget(self.board)
1245 result = packages.get_all_firmware_versions(build_target)
1246 self.assertEqual(result, {})
Benjamin Shai12c767e2022-01-12 15:17:44 +00001247
Michael Mortensen59e30872020-05-18 14:12:49 -06001248
Michael Mortensen71ef5682020-05-07 14:29:24 -06001249class GetFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001250 """Tests for get_firmware_versions."""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001251
Alex Klein1699fab2022-09-08 08:46:06 -06001252 def setUp(self):
1253 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001254 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001255 self.rc.SetDefaultCmdResult(
1256 stdout="""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001257
1258flashrom(8): a8f99c2e61e7dc09c4b25ef5a76ef692 */build/kevin/usr/sbin/flashrom
1259 ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.d
1260 0.9.4 : 860875a : Apr 10 2017 23:54:29 UTC
1261
1262BIOS image: 6b5b855a0b8fd1657546d1402c15b206 *chromeos-firmware-kevin-0.0.1/.dist/kevin_fw_8785.178.0.n
1263BIOS version: Google_Kevin.8785.178.0
1264EC image: 1ebfa9518e6cac0558a80b7ab2f5b489 *chromeos-firmware-kevin-0.0.1/.dist/kevin_ec_8785.178.0.n
1265EC version:kevin_v1.10.184-459421c
1266
1267Package Content:
1268a8f99c2e61e7dc09c4b25ef5a76ef692 *./flashrom
12693c3a99346d1ca1273cbcd86c104851ff *./shflags
1270457a8dc8546764affc9700f8da328d23 *./dump_fmap
1271c392980ddb542639edf44a965a59361a *./updater5.sh
1272490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
12736b5b855a0b8fd1657546d1402c15b206 *./bios.bin
12747b5bef0d2da90c23ff2e157250edf0fa *./crosutil.sh
1275d78722e4f1a0dc2d8c3d6b0bc7010ae3 *./crossystem
1276457a8dc8546764affc9700f8da328d23 *./gbb_utility
12771ebfa9518e6cac0558a80b7ab2f5b489 *./ec.bin
1278c98ca54db130886142ad582a58e90ddc *./common.sh
12795ba978bdec0f696f47f0f0de90936880 *./mosys
1280312e8ee6122057f2a246d7bcf1572f49 *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001281"""
1282 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001283 # pylint: enable=line-too-long
Michael Mortensen71ef5682020-05-07 14:29:24 -06001284
Alex Klein1699fab2022-09-08 08:46:06 -06001285 def test_get_firmware_versions(self):
1286 """Tests get_firmware_versions with mocked output."""
1287 build_target = build_target_lib.BuildTarget(self.board)
1288 result = packages.get_firmware_versions(build_target)
1289 versions = packages.FirmwareVersions(
1290 None,
1291 "Google_Kevin.8785.178.0",
1292 None,
1293 "kevin_v1.10.184-459421c",
1294 None,
1295 )
1296 self.assertEqual(result, versions)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001297
1298
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001299class DetermineKernelVersionTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001300 """Tests for determine_kernel_version."""
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001301
Alex Klein1699fab2022-09-08 08:46:06 -06001302 def setUp(self):
1303 self.board = "test-board"
1304 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001305
Alex Klein1699fab2022-09-08 08:46:06 -06001306 def test_determine_kernel_version(self):
1307 """Tests that a valid kernel version is returned."""
Lizzy Presland0b978e62022-09-09 16:55:29 +00001308 kernel_candidates = [
1309 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
Alex Klein1699fab2022-09-08 08:46:06 -06001310 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001311 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1312 "sys-kernel/upstream-kernel-next-9999",
1313 "sys-kernel/socfpga-kernel-4.20-r34",
Alex Klein1699fab2022-09-08 08:46:06 -06001314 ]
1315 self.PatchObject(
Lizzy Presland0b978e62022-09-09 16:55:29 +00001316 portage_util,
1317 "GetFlattenedDepsForPackage",
1318 return_value=kernel_candidates,
1319 )
1320
1321 installed_pkgs = [
1322 "sys-kernel/linux-firmware-0.0.1-r594",
1323 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1324 "virtual/linux-sources-1-r30",
1325 ]
1326 self.PatchObject(
1327 portage_util,
1328 "GetPackageDependencies",
1329 return_value=installed_pkgs,
Alex Klein1699fab2022-09-08 08:46:06 -06001330 )
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001331
Alex Klein1699fab2022-09-08 08:46:06 -06001332 result = packages.determine_kernel_version(self.build_target)
1333 self.assertEqual(result, "4.4.223-r2209")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001334
Lizzy Presland0b978e62022-09-09 16:55:29 +00001335 def test_determine_kernel_version_ignores_exact_duplicates(self):
1336 """Tests that multiple results for candidates is ignored."""
1337 # Depgraph is evaluated for version as well as revision, so graph will
1338 # return all results twice.
1339 kernel_candidates = [
1340 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1341 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1342 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1343 "sys-kernel/upstream-kernel-next-9999",
1344 "sys-kernel/socfpga-kernel-4.20-r34",
1345 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1346 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1347 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1348 "sys-kernel/upstream-kernel-next-9999",
1349 "sys-kernel/socfpga-kernel-4.20-r34",
1350 ]
1351 self.PatchObject(
1352 portage_util,
1353 "GetFlattenedDepsForPackage",
1354 return_value=kernel_candidates,
1355 )
1356
1357 installed_pkgs = [
1358 "sys-kernel/linux-firmware-0.0.1-r594",
1359 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1360 "virtual/linux-sources-1-r30",
1361 ]
Alex Klein1699fab2022-09-08 08:46:06 -06001362 self.PatchObject(
1363 portage_util,
1364 "GetPackageDependencies",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001365 return_value=installed_pkgs,
1366 )
1367
1368 result = packages.determine_kernel_version(self.build_target)
1369 self.assertEqual(result, "4.4.223-r2209")
1370
1371 def test_determine_kernel_version_ignores_virtual_package(self):
1372 """Tests that top-level package is ignored as potential kernel pkg."""
1373 # Depgraph results include the named package at level 0 as well as its
1374 # first-order dependencies, so verify that the virtual package is not
1375 # included as a kernel package.
1376 kernel_candidates = [
1377 "virtual/linux-sources-1",
1378 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1379 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1380 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1381 "sys-kernel/upstream-kernel-next-9999",
1382 "sys-kernel/socfpga-kernel-4.20-r34",
1383 "virtual/linux-sources-1-r30",
1384 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1385 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1386 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1387 "sys-kernel/upstream-kernel-next-9999",
1388 "sys-kernel/socfpga-kernel-4.20-r34",
1389 ]
1390 self.PatchObject(
1391 portage_util,
1392 "GetFlattenedDepsForPackage",
1393 return_value=kernel_candidates,
1394 )
1395
1396 installed_pkgs = [
1397 "sys-kernel/linux-firmware-0.0.1-r594",
1398 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1399 "virtual/linux-sources-1-r30",
1400 ]
1401 self.PatchObject(
1402 portage_util,
1403 "GetPackageDependencies",
1404 return_value=installed_pkgs,
1405 )
1406
1407 result = packages.determine_kernel_version(self.build_target)
1408 self.assertEqual(result, "4.4.223-r2209")
1409
1410 def test_determine_kernel_version_too_many(self):
1411 """Tests that an exception is thrown with too many matching packages."""
1412 package_result = [
1413 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1414 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1415 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1416 "sys-kernel/upstream-kernel-next-9999",
1417 "sys-kernel/socfpga-kernel-4.20-r34",
1418 ]
1419 self.PatchObject(
1420 portage_util,
1421 "GetFlattenedDepsForPackage",
1422 return_value=package_result,
1423 )
1424
1425 installed_pkgs = [
1426 "sys-kernel/linux-firmware-0.0.1-r594",
1427 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1428 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1429 "virtual/linux-sources-1-r30",
1430 ]
1431 self.PatchObject(
1432 portage_util,
1433 "GetPackageDependencies",
1434 return_value=installed_pkgs,
1435 )
1436
1437 with self.assertRaises(packages.KernelVersionError):
1438 packages.determine_kernel_version(self.build_target)
1439
1440 def test_determine_kernel_version_no_kernel_match(self):
1441 """Tests that an exception is thrown with 0-sized intersection."""
1442 package_result = [
1443 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1444 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1445 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1446 "sys-kernel/upstream-kernel-next-9999",
1447 ]
1448 self.PatchObject(
1449 portage_util,
1450 "GetFlattenedDepsForPackage",
1451 return_value=package_result,
1452 )
1453
1454 installed_pkgs = [
1455 "sys-kernel/linux-firmware-0.0.1-r594",
1456 "sys-kernel/socfpga-kernel-4.20-r34",
1457 "virtual/linux-sources-1-r30",
1458 ]
1459 self.PatchObject(
1460 portage_util,
1461 "GetPackageDependencies",
1462 return_value=installed_pkgs,
1463 )
1464
1465 with self.assertRaises(packages.KernelVersionError):
1466 packages.determine_kernel_version(self.build_target)
1467
1468 def test_determine_kernel_version_exception(self):
1469 """Tests that portage_util exceptions result in returning empty str."""
1470 self.PatchObject(
1471 portage_util,
1472 "GetFlattenedDepsForPackage",
Alex Klein1699fab2022-09-08 08:46:06 -06001473 side_effect=cros_build_lib.RunCommandError("error"),
1474 )
1475 result = packages.determine_kernel_version(self.build_target)
Lizzy Presland0b978e62022-09-09 16:55:29 +00001476 self.assertEqual(result, "")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001477
Alex Klein627e04c2021-11-10 15:56:47 -07001478
Michael Mortensenc2615b72019-10-15 08:12:24 -06001479class ChromeVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001480 """Tests getting chrome version."""
Michael Mortensen14960d02019-10-18 07:53:59 -06001481
Alex Klein1699fab2022-09-08 08:46:06 -06001482 def setUp(self):
1483 self.build_target = build_target_lib.BuildTarget("board")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001484
Alex Klein1699fab2022-09-08 08:46:06 -06001485 def test_determine_chrome_version(self):
1486 """Tests that a valid chrome version is returned."""
1487 # Mock PortageqBestVisible to return a valid chrome version string.
1488 r1_cpf = "chromeos-base/chromeos-chrome-78.0.3900.0_rc-r1"
1489 r1_cpv = package_info.SplitCPV(r1_cpf)
1490 self.PatchObject(
1491 portage_util, "PortageqBestVisible", return_value=r1_cpv
1492 )
Michael Mortensenc2615b72019-10-15 08:12:24 -06001493
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001494 chrome_version = packages.determine_package_version(
1495 constants.CHROME_CP, self.build_target
1496 )
Alex Klein1699fab2022-09-08 08:46:06 -06001497 version_numbers = chrome_version.split(".")
1498 self.assertEqual(len(version_numbers), 4)
1499 self.assertEqual(int(version_numbers[0]), 78)
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001500
Alex Klein1699fab2022-09-08 08:46:06 -06001501 def test_determine_chrome_version_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001502 # Mock what happens when portage throws an exception that bubbles up
1503 # (via RunCommand)inside portage_util.PortageqBestVisible.
Alex Klein1699fab2022-09-08 08:46:06 -06001504 self.PatchObject(
1505 portage_util,
1506 "PortageqBestVisible",
1507 side_effect=cros_build_lib.RunCommandError("error"),
1508 )
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001509 target = packages.determine_package_version(
1510 constants.CHROME_CP, self.build_target
1511 )
Alex Klein1699fab2022-09-08 08:46:06 -06001512 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001513
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001514
1515class PlatformVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001516 """Tests getting platform version."""
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001517
Alex Klein1699fab2022-09-08 08:46:06 -06001518 def test_determine_platform_version(self):
1519 """Test checking that a valid platform version is returned."""
1520 platform_version = packages.determine_platform_version()
1521 # The returned platform version is something like 12603.0.0.
1522 version_string_list = platform_version.split(".")
1523 self.assertEqual(len(version_string_list), 3)
Alex Kleinfee86da2023-01-20 18:40:06 -07001524 # We don't want to check an exact version, but the first number should
1525 # be non-zero.
Alex Klein1699fab2022-09-08 08:46:06 -06001526 self.assertGreaterEqual(int(version_string_list[0]), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001527
Alex Klein1699fab2022-09-08 08:46:06 -06001528 def test_determine_milestone_version(self):
1529 """Test checking that a valid milestone version is returned."""
1530 milestone_version = packages.determine_milestone_version()
1531 # Milestone version should be non-zero
1532 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001533
Alex Klein1699fab2022-09-08 08:46:06 -06001534 def test_determine_full_version(self):
1535 """Test checking that a valid full version is returned."""
1536 full_version = packages.determine_full_version()
1537 pattern = r"^R(\d+)-(\d+.\d+.\d+(-rc\d+)*)"
1538 m = re.match(pattern, full_version)
1539 self.assertTrue(m)
1540 milestone_version = m.group(1)
1541 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001542
Alex Klein1699fab2022-09-08 08:46:06 -06001543 def test_versions_based_on_mock(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001544 # Create a test version_info object, and then mock VersionInfo.from_repo
Alex Klein1699fab2022-09-08 08:46:06 -06001545 # return it.
1546 test_platform_version = "12575.0.0"
1547 test_chrome_branch = "75"
1548 version_info_mock = chromeos_version.VersionInfo(test_platform_version)
1549 version_info_mock.chrome_branch = test_chrome_branch
1550 self.PatchObject(
1551 chromeos_version.VersionInfo,
1552 "from_repo",
1553 return_value=version_info_mock,
1554 )
1555 test_full_version = (
1556 "R" + test_chrome_branch + "-" + test_platform_version
1557 )
1558 platform_version = packages.determine_platform_version()
1559 milestone_version = packages.determine_milestone_version()
1560 full_version = packages.determine_full_version()
1561 self.assertEqual(platform_version, test_platform_version)
1562 self.assertEqual(milestone_version, test_chrome_branch)
1563 self.assertEqual(full_version, test_full_version)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001564
1565
1566# Each of the columns in the following table is a separate dimension along
1567# which Chrome uprev test cases can vary in behavior. The full test space would
1568# be the Cartesian product of the possible values of each column.
1569# 'CHROME_EBUILD' refers to the relationship between the version of the existing
1570# Chrome ebuild vs. the requested uprev version. 'FOLLOWER_EBUILDS' refers to
1571# the same relationship but for the packages defined in OTHER_CHROME_PACKAGES.
1572# 'EBUILDS MODIFIED' refers to whether any of the existing 9999 ebuilds have
1573# modified contents relative to their corresponding stable ebuilds.
1574#
1575# CHROME_EBUILD FOLLOWER_EBUILDS EBUILDS_MODIFIED
1576#
1577# HIGHER HIGHER YES
1578# SAME SAME NO
1579# LOWER LOWER
1580# DOESN'T EXIST YET
1581
1582# These test cases cover both CHROME & FOLLOWER ebuilds being identically
1583# higher, lower, or the same versions, with no modified ebuilds.
1584UPREV_VERSION_CASES = (
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001585 # Uprev.
Chris McDonaldea0312c2020-05-04 23:33:15 -06001586 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001587 "80.0.8080.0",
1588 "81.0.8181.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001589 # One added and one deleted for chrome and each "other" package.
1590 2 * (1 + len(constants.OTHER_CHROME_PACKAGES)),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001591 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001592 id="newer_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001593 ),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001594 # Revbump.
1595 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001596 "80.0.8080.0",
1597 "80.0.8080.0",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001598 2,
1599 True,
Alex Klein1699fab2022-09-08 08:46:06 -06001600 id="chrome_revbump",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001601 ),
Chris McDonaldea0312c2020-05-04 23:33:15 -06001602 # No files should be changed in these cases.
1603 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001604 "80.0.8080.0",
1605 "80.0.8080.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001606 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001607 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001608 id="same_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001609 ),
1610 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001611 "80.0.8080.0",
1612 "79.0.7979.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="older_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001616 ),
1617)
1618
1619
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001620@pytest.mark.parametrize(
Alex Klein1699fab2022-09-08 08:46:06 -06001621 "old_version, new_version, expected_count, modify_unstable",
1622 UPREV_VERSION_CASES,
1623)
1624def test_uprev_chrome_all_files_already_exist(
1625 old_version,
1626 new_version,
1627 expected_count,
1628 modify_unstable,
1629 monkeypatch,
1630 overlay_stack,
1631):
1632 """Test Chrome uprevs work as expected when all packages already exist."""
1633 (overlay,) = overlay_stack(1)
1634 monkeypatch.setattr(uprev_lib, "_CHROME_OVERLAY_PATH", overlay.path)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001635
Alex Klein1699fab2022-09-08 08:46:06 -06001636 unstable_chrome = cr.test.Package(
1637 "chromeos-base", "chromeos-chrome", version="9999", keywords="~*"
1638 )
1639 if modify_unstable:
1640 # Add some field not set in stable.
1641 unstable_chrome.depend = "foo/bar"
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001642
Alex Klein1699fab2022-09-08 08:46:06 -06001643 stable_chrome = cr.test.Package(
1644 "chromeos-base", "chromeos-chrome", version=f"{old_version}_rc-r1"
1645 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001646
Alex Klein1699fab2022-09-08 08:46:06 -06001647 overlay.add_package(unstable_chrome)
1648 overlay.add_package(stable_chrome)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001649
Alex Klein1699fab2022-09-08 08:46:06 -06001650 for pkg_str in constants.OTHER_CHROME_PACKAGES:
1651 category, pkg_name = pkg_str.split("/")
1652 unstable_pkg = cr.test.Package(
1653 category, pkg_name, version="9999", keywords="~*"
1654 )
1655 stable_pkg = cr.test.Package(
1656 category, pkg_name, version=f"{old_version}_rc-r1"
1657 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001658
Alex Klein1699fab2022-09-08 08:46:06 -06001659 overlay.add_package(unstable_pkg)
1660 overlay.add_package(stable_pkg)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001661
Alex Klein1699fab2022-09-08 08:46:06 -06001662 git_refs = [
1663 GitRef(
1664 path="/foo", ref=f"refs/tags/{new_version}", revision="stubcommit"
1665 )
1666 ]
1667 res = packages.uprev_chrome_from_ref(None, git_refs, None)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001668
Alex Klein1699fab2022-09-08 08:46:06 -06001669 modified_file_count = sum(len(m.files) for m in res.modified)
1670 assert modified_file_count == expected_count
Michael Mortensen125bb012020-05-21 14:02:10 -06001671
1672
Alex Klein1699fab2022-09-08 08:46:06 -06001673@pytest.mark.usefixtures("testcase_monkeypatch")
Michael Mortensen125bb012020-05-21 14:02:10 -06001674class GetModelsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001675 """Tests for get_models."""
Michael Mortensen125bb012020-05-21 14:02:10 -06001676
Alex Klein1699fab2022-09-08 08:46:06 -06001677 def setUp(self):
1678 self.board = "test-board"
1679 self.rc.SetDefaultCmdResult(stdout="pyro\nreef\nsnappy\n")
1680 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1681 build_bin = os.path.join(
1682 self.tempdir, constants.DEFAULT_CHROOT_DIR, "usr", "bin"
1683 )
1684 osutils.Touch(
1685 os.path.join(build_bin, "cros_config_host"), makedirs=True
1686 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001687
Alex Klein1699fab2022-09-08 08:46:06 -06001688 def testGetModels(self):
1689 """Test get_models."""
1690 build_target = build_target_lib.BuildTarget(self.board)
1691 result = packages.get_models(build_target)
1692 self.assertEqual(result, ["pyro", "reef", "snappy"])
Michael Mortensen359c1f32020-05-28 19:35:42 -06001693
1694
1695class GetKeyIdTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001696 """Tests for get_key_id."""
Michael Mortensen359c1f32020-05-28 19:35:42 -06001697
Alex Klein1699fab2022-09-08 08:46:06 -06001698 def setUp(self):
1699 self.board = "test-board"
1700 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensen359c1f32020-05-28 19:35:42 -06001701
Alex Klein1699fab2022-09-08 08:46:06 -06001702 def testGetKeyId(self):
1703 """Test get_key_id when _run_cros_config_host returns a key."""
1704 self.PatchObject(
1705 packages, "_run_cros_config_host", return_value=["key"]
1706 )
1707 result = packages.get_key_id(self.build_target, "model")
1708 self.assertEqual(result, "key")
Michael Mortensen359c1f32020-05-28 19:35:42 -06001709
Alex Klein1699fab2022-09-08 08:46:06 -06001710 def testGetKeyIdNoKey(self):
1711 """Test get_key_id when None should be returned."""
1712 self.PatchObject(
1713 packages, "_run_cros_config_host", return_value=["key1", "key2"]
1714 )
1715 result = packages.get_key_id(self.build_target, "model")
1716 self.assertEqual(result, None)
Ben Reiche779cf42020-12-15 03:21:31 +00001717
1718
Harvey Yang3eee06c2021-03-18 15:47:56 +08001719class GetLatestVersionTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001720 """Tests for get_latest_version_from_refs."""
Ben Reiche779cf42020-12-15 03:21:31 +00001721
Alex Klein1699fab2022-09-08 08:46:06 -06001722 def setUp(self):
1723 self.prefix = "refs/tags/drivefs_"
1724 # The tag ref template.
1725 ref_tpl = self.prefix + "%s"
Ben Reiche779cf42020-12-15 03:21:31 +00001726
Alex Klein1699fab2022-09-08 08:46:06 -06001727 self.latest = "44.0.20"
1728 self.versions = ["42.0.1", self.latest, "44.0.19", "39.0.15"]
1729 self.latest_ref = uprev_lib.GitRef(
1730 "/path", ref_tpl % self.latest, "abc123"
1731 )
1732 self.refs = [
1733 uprev_lib.GitRef("/path", ref_tpl % v, "abc123")
1734 for v in self.versions
1735 ]
Ben Reiche779cf42020-12-15 03:21:31 +00001736
Alex Klein1699fab2022-09-08 08:46:06 -06001737 def test_single_ref(self):
1738 """Test a single ref is supplied."""
1739 # pylint: disable=protected-access
1740 self.assertEqual(
1741 self.latest,
1742 packages._get_latest_version_from_refs(
1743 self.prefix, [self.latest_ref]
1744 ),
1745 )
Ben Reiche779cf42020-12-15 03:21:31 +00001746
Alex Klein1699fab2022-09-08 08:46:06 -06001747 def test_multiple_ref_versions(self):
1748 """Test multiple refs supplied."""
1749 # pylint: disable=protected-access
1750 self.assertEqual(
1751 self.latest,
1752 packages._get_latest_version_from_refs(self.prefix, self.refs),
1753 )
Ben Reiche779cf42020-12-15 03:21:31 +00001754
Alex Klein1699fab2022-09-08 08:46:06 -06001755 def test_no_refs_returns_none(self):
1756 """Test no refs supplied."""
1757 # pylint: disable=protected-access
1758 self.assertEqual(
1759 packages._get_latest_version_from_refs(self.prefix, []), None
1760 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001761
Chinglin Yu84818732022-10-03 12:03:43 +08001762 def test_ref_prefix(self):
1763 """Test refs with a different prefix isn't used"""
1764 # pylint: disable=protected-access
1765 # Add refs/tags/foo_100.0.0 to the refs, which should be ignored in
1766 # _get_latest_version_from_refs because the prefix doesn't match, even
1767 # if its version number is larger.
1768 refs = self.refs + [
1769 uprev_lib.GitRef("/path", "refs/tags/foo_100.0.0", "abc123")
1770 ]
1771 self.assertEqual(
1772 self.latest,
1773 packages._get_latest_version_from_refs(self.prefix, refs),
1774 )
1775
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001776
Alex Klein6becabc2020-09-11 14:03:05 -06001777class NeedsChromeSourceTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001778 """Tests for needs_chrome_source."""
Alex Klein6becabc2020-09-11 14:03:05 -06001779
Alex Klein1699fab2022-09-08 08:46:06 -06001780 def _build_graph(self, with_chrome: bool, with_followers: bool):
1781 root = "/build/build_target"
1782 foo_bar = package_info.parse("foo/bar-1")
1783 chrome = package_info.parse(f"{constants.CHROME_CP}-1.2.3.4")
1784 followers = [
1785 package_info.parse(f"{pkg}-1.2.3.4")
1786 for pkg in constants.OTHER_CHROME_PACKAGES
1787 ]
1788 nodes = [dependency_graph.PackageNode(foo_bar, root)]
1789 root_pkgs = ["foo/bar-1"]
1790 if with_chrome:
1791 nodes.append(dependency_graph.PackageNode(chrome, root))
1792 root_pkgs.append(chrome.cpvr)
1793 if with_followers:
1794 nodes.extend(
1795 [dependency_graph.PackageNode(f, root) for f in followers]
1796 )
1797 root_pkgs.extend([f.cpvr for f in followers])
Alex Klein6becabc2020-09-11 14:03:05 -06001798
Alex Klein1699fab2022-09-08 08:46:06 -06001799 return dependency_graph.DependencyGraph(nodes, root, root_pkgs)
Alex Klein6becabc2020-09-11 14:03:05 -06001800
Alex Klein1699fab2022-09-08 08:46:06 -06001801 def test_needs_all(self):
1802 """Verify we need source when we have no prebuilts."""
1803 graph = self._build_graph(with_chrome=True, with_followers=True)
1804 self.PatchObject(
1805 depgraph, "get_sysroot_dependency_graph", return_value=graph
1806 )
1807 self.PatchObject(packages, "has_prebuilt", return_value=False)
1808 self.PatchObject(
1809 packages,
1810 "uprev_chrome",
1811 return_value=uprev_lib.UprevVersionedPackageResult(),
1812 )
Alex Klein6becabc2020-09-11 14:03:05 -06001813
Alex Klein1699fab2022-09-08 08:46:06 -06001814 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001815
Alex Klein1699fab2022-09-08 08:46:06 -06001816 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001817
Alex Klein1699fab2022-09-08 08:46:06 -06001818 self.assertTrue(result.needs_chrome_source)
1819 self.assertTrue(result.builds_chrome)
1820 self.assertTrue(result.packages)
1821 self.assertEqual(
1822 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1823 )
1824 self.assertTrue(result.missing_chrome_prebuilt)
1825 self.assertTrue(result.missing_follower_prebuilt)
1826 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001827
Alex Klein1699fab2022-09-08 08:46:06 -06001828 def test_needs_none(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001829 """Verify not building any chrome packages prevents needing it."""
Alex Klein1699fab2022-09-08 08:46:06 -06001830 graph = self._build_graph(with_chrome=False, with_followers=False)
1831 self.PatchObject(
1832 depgraph, "get_sysroot_dependency_graph", return_value=graph
1833 )
1834 self.PatchObject(packages, "has_prebuilt", return_value=False)
1835 self.PatchObject(
1836 packages,
1837 "uprev_chrome",
1838 return_value=uprev_lib.UprevVersionedPackageResult(),
1839 )
Alex Klein6becabc2020-09-11 14:03:05 -06001840
Alex Klein1699fab2022-09-08 08:46:06 -06001841 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001842
Alex Klein1699fab2022-09-08 08:46:06 -06001843 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001844
Alex Klein1699fab2022-09-08 08:46:06 -06001845 self.assertFalse(result.needs_chrome_source)
1846 self.assertFalse(result.builds_chrome)
1847 self.assertFalse(result.packages)
1848 self.assertFalse(result.missing_chrome_prebuilt)
1849 self.assertFalse(result.missing_follower_prebuilt)
1850 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001851
Alex Klein1699fab2022-09-08 08:46:06 -06001852 def test_needs_chrome_only(self):
1853 """Verify only chrome triggers needs chrome source."""
1854 graph = self._build_graph(with_chrome=True, with_followers=False)
1855 self.PatchObject(
1856 depgraph, "get_sysroot_dependency_graph", return_value=graph
1857 )
1858 self.PatchObject(packages, "has_prebuilt", return_value=False)
1859 self.PatchObject(
1860 packages,
1861 "uprev_chrome",
1862 return_value=uprev_lib.UprevVersionedPackageResult(),
1863 )
Alex Klein6becabc2020-09-11 14:03:05 -06001864
Alex Klein1699fab2022-09-08 08:46:06 -06001865 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001866
Alex Klein1699fab2022-09-08 08:46:06 -06001867 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001868
Alex Klein1699fab2022-09-08 08:46:06 -06001869 self.assertTrue(result.needs_chrome_source)
1870 self.assertTrue(result.builds_chrome)
1871 self.assertTrue(result.packages)
1872 self.assertEqual(
Alex Klein041edd82023-04-17 12:23:23 -06001873 {p.atom for p in result.packages}, {constants.CHROME_CP}
Alex Klein1699fab2022-09-08 08:46:06 -06001874 )
1875 self.assertTrue(result.missing_chrome_prebuilt)
1876 self.assertFalse(result.missing_follower_prebuilt)
1877 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001878
Alex Klein1699fab2022-09-08 08:46:06 -06001879 def test_needs_followers_only(self):
1880 """Verify only chrome followers triggers needs chrome source."""
1881 graph = self._build_graph(with_chrome=False, with_followers=True)
1882 self.PatchObject(
1883 depgraph, "get_sysroot_dependency_graph", return_value=graph
1884 )
1885 self.PatchObject(packages, "has_prebuilt", return_value=False)
1886 self.PatchObject(
1887 packages,
1888 "uprev_chrome",
1889 return_value=uprev_lib.UprevVersionedPackageResult(),
1890 )
Alex Klein6becabc2020-09-11 14:03:05 -06001891
Alex Klein1699fab2022-09-08 08:46:06 -06001892 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001893
Alex Klein1699fab2022-09-08 08:46:06 -06001894 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001895
Alex Klein1699fab2022-09-08 08:46:06 -06001896 self.assertTrue(result.needs_chrome_source)
1897 self.assertFalse(result.builds_chrome)
1898 self.assertTrue(result.packages)
1899 self.assertEqual(
Alex Klein041edd82023-04-17 12:23:23 -06001900 {p.atom for p in result.packages},
Alex Klein1699fab2022-09-08 08:46:06 -06001901 set(constants.OTHER_CHROME_PACKAGES),
1902 )
1903 self.assertFalse(result.missing_chrome_prebuilt)
1904 self.assertTrue(result.missing_follower_prebuilt)
1905 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001906
Alex Klein1699fab2022-09-08 08:46:06 -06001907 def test_has_prebuilts(self):
1908 """Test prebuilts prevent us from needing chrome source."""
1909 graph = self._build_graph(with_chrome=True, with_followers=True)
1910 self.PatchObject(
1911 depgraph, "get_sysroot_dependency_graph", return_value=graph
1912 )
1913 self.PatchObject(packages, "has_prebuilt", return_value=True)
1914 self.PatchObject(
1915 packages,
1916 "uprev_chrome",
1917 return_value=uprev_lib.UprevVersionedPackageResult(),
1918 )
Alex Klein6becabc2020-09-11 14:03:05 -06001919
Alex Klein1699fab2022-09-08 08:46:06 -06001920 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001921
Alex Klein1699fab2022-09-08 08:46:06 -06001922 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001923
Alex Klein1699fab2022-09-08 08:46:06 -06001924 self.assertFalse(result.needs_chrome_source)
1925 self.assertTrue(result.builds_chrome)
1926 self.assertFalse(result.packages)
1927 self.assertFalse(result.missing_chrome_prebuilt)
1928 self.assertFalse(result.missing_follower_prebuilt)
1929 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001930
Alex Klein1699fab2022-09-08 08:46:06 -06001931 def test_compile_source(self):
1932 """Test compile source ignores prebuilts."""
1933 graph = self._build_graph(with_chrome=True, with_followers=True)
1934 self.PatchObject(
1935 depgraph, "get_sysroot_dependency_graph", return_value=graph
1936 )
1937 self.PatchObject(packages, "has_prebuilt", return_value=True)
1938 self.PatchObject(
1939 packages,
1940 "uprev_chrome",
1941 return_value=uprev_lib.UprevVersionedPackageResult(),
1942 )
Alex Klein6becabc2020-09-11 14:03:05 -06001943
Alex Klein1699fab2022-09-08 08:46:06 -06001944 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001945
Alex Klein1699fab2022-09-08 08:46:06 -06001946 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Klein6becabc2020-09-11 14:03:05 -06001947
Alex Klein1699fab2022-09-08 08:46:06 -06001948 self.assertTrue(result.needs_chrome_source)
1949 self.assertTrue(result.builds_chrome)
1950 self.assertTrue(result.packages)
1951 self.assertEqual(
1952 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1953 )
1954 self.assertTrue(result.missing_chrome_prebuilt)
1955 self.assertTrue(result.missing_follower_prebuilt)
1956 self.assertFalse(result.local_uprev)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001957
Alex Klein1699fab2022-09-08 08:46:06 -06001958 def test_local_uprev(self):
1959 """Test compile source ignores prebuilts."""
1960 graph = self._build_graph(with_chrome=True, with_followers=True)
1961 self.PatchObject(
1962 depgraph, "get_sysroot_dependency_graph", return_value=graph
1963 )
1964 self.PatchObject(packages, "has_prebuilt", return_value=False)
Alex Klein75110572021-07-14 10:44:39 -06001965
Alex Klein1699fab2022-09-08 08:46:06 -06001966 uprev_result = uprev_lib.UprevVersionedPackageResult()
1967 uprev_result.add_result("1.2.3.4", ["/tmp/foo"])
1968 self.PatchObject(packages, "uprev_chrome", return_value=uprev_result)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001969
Alex Klein1699fab2022-09-08 08:46:06 -06001970 build_target = build_target_lib.BuildTarget("build_target")
Alex Kleinde7b76d2021-07-12 12:28:44 -06001971
Alex Klein1699fab2022-09-08 08:46:06 -06001972 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001973
Alex Klein1699fab2022-09-08 08:46:06 -06001974 self.assertTrue(result.needs_chrome_source)
1975 self.assertTrue(result.builds_chrome)
1976 self.assertTrue(result.packages)
1977 self.assertEqual(
1978 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1979 )
1980 self.assertTrue(result.missing_chrome_prebuilt)
1981 self.assertTrue(result.missing_follower_prebuilt)
1982 self.assertTrue(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001983
1984
Mike Frysinger8c9d7582023-08-21 19:28:21 -04001985class GetTargetVersionTest(cros_test_lib.RunCommandTestCase):
1986 """Tests for get_target_version."""
1987
1988 def setUp(self):
1989 self.build_target = build_target_lib.BuildTarget("build_target")
1990
1991 def test_default_empty(self):
1992 """Default behavior with mostly stub empty data."""
1993
1994 def GetBuildDependency(sysroot_path, board, mock_packages):
1995 assert sysroot_path == self.build_target.root
1996 assert board == self.build_target.name
1997 assert list(mock_packages) == [
1998 package_info.parse(constants.TARGET_OS_PKG)
1999 ]
2000 return {"package_deps": []}, {}
2001
2002 self.PatchObject(
2003 dependency, "GetBuildDependency", side_effect=GetBuildDependency
2004 )
2005 ret = packages.get_target_versions(self.build_target)
2006 assert ret.android_version is None
2007 assert ret.android_branch is None
2008 assert ret.android_target is None
2009 assert ret.chrome_version is None
2010 assert isinstance(ret.platform_version, str)
2011 assert isinstance(ret.milestone_version, str)
2012 assert isinstance(ret.full_version, str)
2013 assert ret.lacros_version is None
2014
2015
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002016class UprevDrivefsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002017 """Tests for uprev_drivefs."""
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002018
Alex Klein1699fab2022-09-08 08:46:06 -06002019 def setUp(self):
2020 self.refs = [
2021 GitRef(
2022 path="/chromeos/platform/drivefs-google3/",
2023 ref="refs/tags/drivefs_45.0.2",
2024 revision="123",
2025 )
2026 ]
2027 self.MOCK_DRIVEFS_EBUILD_PATH = "drivefs.45.0.2-r1.ebuild"
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002028
Alex Klein1699fab2022-09-08 08:46:06 -06002029 def revisionBumpOutcome(self, ebuild_path):
2030 return uprev_lib.UprevResult(
2031 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2032 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002033
Alex Klein1699fab2022-09-08 08:46:06 -06002034 def majorBumpOutcome(self, ebuild_path):
2035 return uprev_lib.UprevResult(
2036 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2037 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002038
Alex Klein1699fab2022-09-08 08:46:06 -06002039 def sameVersionOutcome(self):
2040 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002041
Alex Klein1699fab2022-09-08 08:46:06 -06002042 def test_latest_version_returns_none(self):
2043 """Test no refs were supplied"""
2044 output = packages.uprev_drivefs(None, [], None)
2045 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002046
Alex Klein1699fab2022-09-08 08:46:06 -06002047 def test_drivefs_uprev_fails(self):
2048 """Test a single ref is supplied."""
2049 self.PatchObject(
2050 uprev_lib,
2051 "uprev_workon_ebuild_to_version",
2052 side_effect=[None, None],
2053 )
2054 output = packages.uprev_drivefs(None, self.refs, None)
2055 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002056
Alex Klein1699fab2022-09-08 08:46:06 -06002057 def test_same_version_exists(self):
2058 """Test the same version exists uprev should not happen."""
2059 drivefs_outcome = self.sameVersionOutcome()
2060 self.PatchObject(
2061 uprev_lib,
2062 "uprev_workon_ebuild_to_version",
2063 side_effect=[drivefs_outcome],
2064 )
2065 output = packages.uprev_drivefs(None, self.refs, None)
2066 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002067
Alex Klein1699fab2022-09-08 08:46:06 -06002068 def test_revision_bump_both_packages(self):
2069 """Test both packages uprev, should succeed."""
2070 drivefs_outcome = self.revisionBumpOutcome(
2071 self.MOCK_DRIVEFS_EBUILD_PATH
2072 )
2073 self.PatchObject(
2074 uprev_lib,
2075 "uprev_workon_ebuild_to_version",
2076 side_effect=[drivefs_outcome],
2077 )
2078 output = packages.uprev_drivefs(None, self.refs, None)
2079 self.assertTrue(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002080
Alex Klein1699fab2022-09-08 08:46:06 -06002081 def test_major_bump_both_packages(self):
2082 """Test both packages uprev, should succeed."""
2083 drivefs_outcome = self.majorBumpOutcome(self.MOCK_DRIVEFS_EBUILD_PATH)
2084 self.PatchObject(
2085 uprev_lib,
2086 "uprev_workon_ebuild_to_version",
2087 side_effect=[drivefs_outcome],
2088 )
2089 output = packages.uprev_drivefs(None, self.refs, None)
2090 self.assertTrue(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002091
2092
Denis Nikitin63613e32022-09-09 22:26:50 -07002093class UprevKernelAfdo(cros_test_lib.RunCommandTempDirTestCase):
2094 """Tests for uprev_kernel_afdo."""
2095
2096 def setUp(self):
2097 # patch_ebuild_vars is tested separately.
2098 self.mock_patch = self.PatchObject(packages, "patch_ebuild_vars")
Denis Nikitin88ad5132022-09-28 12:10:01 -07002099 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Denis Nikitin63613e32022-09-09 22:26:50 -07002100 self.metadata_dir = os.path.join(
Denis Nikitin63613e32022-09-09 22:26:50 -07002101 "src",
2102 "third_party",
2103 "toolchain-utils",
2104 "afdo_metadata",
2105 )
Denis Nikitin88ad5132022-09-28 12:10:01 -07002106 osutils.SafeMakedirs(os.path.join(self.tempdir, self.metadata_dir))
Denis Nikitin63613e32022-09-09 22:26:50 -07002107
2108 def test_uprev_kernel_afdo_version(self):
2109 """Test kernel afdo version uprev."""
2110 json_files = {
2111 "kernel_afdo.json": (
2112 "{\n"
2113 ' "chromeos-kernel-5_4": {\n'
2114 ' "name": "R106-12345.0-0123456789"\n'
2115 " }\n"
2116 "}"
2117 ),
2118 "kernel_arm_afdo.json": (
2119 "{\n"
2120 ' "chromeos-kernel-5_15": {\n'
2121 ' "name": "R107-67890.0-0123456789"\n'
2122 " }\n"
2123 "}"
2124 ),
2125 }
2126 for f, contents in json_files.items():
2127 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2128
Brian Norrisf0c68572023-07-12 17:32:20 -07002129 returned_output = packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002130
Denis Nikitin88ad5132022-09-28 12:10:01 -07002131 package_root = os.path.join(
2132 constants.SOURCE_ROOT,
2133 constants.CHROMIUMOS_OVERLAY_DIR,
2134 "sys-kernel",
Denis Nikitin63613e32022-09-09 22:26:50 -07002135 )
2136 expect_result = [
2137 uprev_lib.UprevVersionedPackageModifications(
2138 new_version="R106-12345.0-0123456789",
2139 files=[
2140 os.path.join(
2141 package_root,
2142 "chromeos-kernel-5_4",
2143 "chromeos-kernel-5_4-9999.ebuild",
2144 ),
2145 os.path.join(
2146 package_root, "chromeos-kernel-5_4", "Manifest"
2147 ),
2148 ],
2149 ),
2150 uprev_lib.UprevVersionedPackageModifications(
2151 new_version="R107-67890.0-0123456789",
2152 files=[
2153 os.path.join(
2154 package_root,
2155 "chromeos-kernel-5_15",
2156 "chromeos-kernel-5_15-9999.ebuild",
2157 ),
2158 os.path.join(
2159 package_root, "chromeos-kernel-5_15", "Manifest"
2160 ),
2161 ],
2162 ),
2163 ]
2164 self.assertTrue(returned_output.uprevved)
2165 self.assertEqual(returned_output.modified, expect_result)
2166
2167 def test_uprev_kernel_afdo_empty_json(self):
2168 """Test kernel afdo version unchanged."""
2169 json_files = {
2170 "kernel_afdo.json": "{}",
2171 "kernel_arm_afdo.json": "{}",
2172 }
2173 for f, contents in json_files.items():
2174 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2175
Brian Norrisf0c68572023-07-12 17:32:20 -07002176 returned_output = packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002177 self.assertFalse(returned_output.uprevved)
2178
2179 def test_uprev_kernel_afdo_empty_file(self):
2180 """Test malformed json raises."""
2181 json_files = {
2182 "kernel_afdo.json": "",
2183 "kernel_arm_afdo.json": "",
2184 }
2185 for f, contents in json_files.items():
2186 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2187
2188 with self.assertRaisesRegex(
2189 json.decoder.JSONDecodeError, "Expecting value"
2190 ):
Brian Norrisf0c68572023-07-12 17:32:20 -07002191 packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002192
2193 def test_uprev_kernel_afdo_manifest_raises(self):
2194 """Test manifest update raises."""
2195 json_files = {
2196 "kernel_afdo.json": (
2197 "{\n"
2198 ' "chromeos-kernel-5_4": {\n'
2199 ' "name": "R106-12345.0-0123456789"\n'
2200 " }\n"
2201 "}"
2202 ),
2203 }
2204 for f, contents in json_files.items():
2205 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2206 # run() raises exception.
2207 self.rc.SetDefaultCmdResult(
2208 side_effect=cros_build_lib.RunCommandError("error")
2209 )
2210
2211 with self.assertRaises(uprev_lib.EbuildManifestError):
Brian Norrisf0c68572023-07-12 17:32:20 -07002212 packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002213
2214
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002215# TODO(chenghaoyang): Shouldn't use uprev_workon_ebuild_to_version.
2216class UprevPerfettoTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002217 """Tests for uprev_perfetto."""
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002218
Alex Klein1699fab2022-09-08 08:46:06 -06002219 def setUp(self):
2220 self.refs = [GitRef(path="/foo", ref="refs/tags/v12.0", revision="123")]
2221 self.MOCK_PERFETTO_EBUILD_PATH = "perfetto-12.0-r1.ebuild"
Chinglin Yufa728552023-04-13 03:12:04 +00002222 self.MOCK_PERFETTO_PROTO_EBUILD_PATH = "perfetto-protos-12.0-r1.ebuild"
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002223
Chinglin Yufa728552023-04-13 03:12:04 +00002224 def revisionBumpOutcome(self):
2225 return [
2226 uprev_lib.UprevResult(
2227 uprev_lib.Outcome.REVISION_BUMP,
2228 [self.MOCK_PERFETTO_EBUILD_PATH],
2229 ),
2230 uprev_lib.UprevResult(
2231 uprev_lib.Outcome.REVISION_BUMP,
2232 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2233 ),
2234 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002235
Chinglin Yufa728552023-04-13 03:12:04 +00002236 def majorBumpOutcome(self):
2237 return [
2238 uprev_lib.UprevResult(
2239 uprev_lib.Outcome.VERSION_BUMP, [self.MOCK_PERFETTO_EBUILD_PATH]
2240 ),
2241 uprev_lib.UprevResult(
2242 uprev_lib.Outcome.VERSION_BUMP,
2243 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2244 ),
2245 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002246
Alex Klein1699fab2022-09-08 08:46:06 -06002247 def newerVersionOutcome(self):
2248 return uprev_lib.UprevResult(uprev_lib.Outcome.NEWER_VERSION_EXISTS)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002249
Alex Klein1699fab2022-09-08 08:46:06 -06002250 def sameVersionOutcome(self):
2251 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002252
Alex Klein1699fab2022-09-08 08:46:06 -06002253 def test_latest_version_returns_none(self):
2254 """Test no refs were supplied"""
2255 output = packages.uprev_perfetto(None, [], None)
2256 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002257
Alex Klein1699fab2022-09-08 08:46:06 -06002258 def test_perfetto_uprev_fails(self):
2259 """Test a single ref is supplied."""
2260 self.PatchObject(
2261 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2262 )
2263 output = packages.uprev_perfetto(None, self.refs, None)
2264 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002265
Alex Klein1699fab2022-09-08 08:46:06 -06002266 def test_newer_version_exists(self):
2267 """Test the newer version exists uprev should not happen."""
2268 perfetto_outcome = self.newerVersionOutcome()
2269 self.PatchObject(
2270 uprev_lib,
2271 "uprev_workon_ebuild_to_version",
2272 side_effect=[perfetto_outcome],
2273 )
2274 output = packages.uprev_perfetto(None, self.refs, None)
2275 self.assertFalse(output.uprevved)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002276
Alex Klein1699fab2022-09-08 08:46:06 -06002277 def test_same_version_exists(self):
2278 """Test the same version exists uprev should not happen."""
2279 perfetto_outcome = self.sameVersionOutcome()
2280 self.PatchObject(
2281 uprev_lib,
2282 "uprev_workon_ebuild_to_version",
2283 side_effect=[perfetto_outcome],
2284 )
2285 output = packages.uprev_perfetto(None, self.refs, None)
2286 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002287
Alex Klein1699fab2022-09-08 08:46:06 -06002288 def test_revision_bump_perfetto_package(self):
2289 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002290 self.PatchObject(
2291 uprev_lib,
2292 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002293 side_effect=self.revisionBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002294 )
2295 output = packages.uprev_perfetto(None, self.refs, None)
2296 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002297 self.assertEqual(
2298 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2299 )
2300 self.assertEqual(
2301 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2302 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002303
Alex Klein1699fab2022-09-08 08:46:06 -06002304 def test_major_bump_perfetto_package(self):
2305 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002306 self.PatchObject(
2307 uprev_lib,
2308 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002309 side_effect=self.majorBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002310 )
2311 output = packages.uprev_perfetto(None, self.refs, None)
2312 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002313 self.assertEqual(
2314 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2315 )
2316 self.assertEqual(
2317 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2318 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002319
Chinglin Yuad12a512022-10-07 17:26:12 +08002320 def test_revision_bump_trunk(self):
2321 """Test revision bump on receiving non-versioned trunk refs."""
Chinglin Yu5de28a42022-11-11 19:52:21 +08002322 refs = [
2323 GitRef(
2324 path="/foo", ref="refs/heads/main", revision="0123456789abcdef"
2325 )
2326 ]
Chinglin Yuad12a512022-10-07 17:26:12 +08002327 self.PatchObject(
2328 uprev_lib, "get_stable_ebuild_version", return_value="12.0"
2329 )
2330 self.PatchObject(
2331 uprev_lib,
2332 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002333 side_effect=self.revisionBumpOutcome(),
Chinglin Yuad12a512022-10-07 17:26:12 +08002334 )
2335 output = packages.uprev_perfetto(None, refs, None)
Chinglin Yufa728552023-04-13 03:12:04 +00002336
Chinglin Yuad12a512022-10-07 17:26:12 +08002337 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002338 self.assertEqual(
2339 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2340 )
Chinglin Yu5de28a42022-11-11 19:52:21 +08002341 self.assertEqual(output.modified[0].new_version, "12.0-012345678")
Chinglin Yufa728552023-04-13 03:12:04 +00002342 self.assertEqual(
2343 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2344 )
2345 self.assertEqual(output.modified[1].new_version, "12.0-012345678")
Chinglin Yuad12a512022-10-07 17:26:12 +08002346
Alex Klein627e04c2021-11-10 15:56:47 -07002347
Julio Hurtadof1befec2021-05-05 21:34:26 +00002348class UprevLacrosTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002349 """Tests for uprev_lacros"""
Julio Hurtadof1befec2021-05-05 21:34:26 +00002350
Alex Klein1699fab2022-09-08 08:46:06 -06002351 def setUp(self):
2352 self.refs = [
2353 GitRef(
2354 path="/lacros", ref="refs/heads/main", revision="123.456.789.0"
2355 )
2356 ]
2357 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtadof1befec2021-05-05 21:34:26 +00002358
Alex Klein1699fab2022-09-08 08:46:06 -06002359 def revisionBumpOutcome(self, ebuild_path):
2360 return uprev_lib.UprevResult(
2361 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2362 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002363
Alex Klein1699fab2022-09-08 08:46:06 -06002364 def majorBumpOutcome(self, ebuild_path):
2365 return uprev_lib.UprevResult(
2366 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2367 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002368
Alex Klein1699fab2022-09-08 08:46:06 -06002369 def newerVersionOutcome(self, ebuild_path):
2370 return uprev_lib.UprevResult(
2371 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2372 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002373
Alex Klein1699fab2022-09-08 08:46:06 -06002374 def sameVersionOutcome(self, ebuild_path):
2375 return uprev_lib.UprevResult(
2376 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2377 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002378
Alex Klein1699fab2022-09-08 08:46:06 -06002379 def newEbuildCreatedOutcome(self, ebuild_path):
2380 return uprev_lib.UprevResult(
2381 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2382 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002383
Alex Klein1699fab2022-09-08 08:46:06 -06002384 def test_lacros_uprev_fails(self):
2385 """Test a lacros package uprev with no triggers"""
2386 self.PatchObject(
2387 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2388 )
2389 with self.assertRaises(IndexError):
2390 packages.uprev_lacros(None, [], None)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002391
Alex Klein1699fab2022-09-08 08:46:06 -06002392 def test_lacros_uprev_revision_bump(self):
2393 """Test lacros package uprev."""
2394 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2395 self.PatchObject(
2396 uprev_lib,
2397 "uprev_workon_ebuild_to_version",
2398 side_effect=[lacros_outcome],
2399 )
2400 output = packages.uprev_lacros(None, self.refs, None)
2401 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002402
Alex Klein1699fab2022-09-08 08:46:06 -06002403 def test_lacros_uprev_version_bump(self):
2404 """Test lacros package uprev."""
2405 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2406 self.PatchObject(
2407 uprev_lib,
2408 "uprev_workon_ebuild_to_version",
2409 side_effect=[lacros_outcome],
2410 )
2411 output = packages.uprev_lacros(None, self.refs, None)
2412 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002413
Alex Klein1699fab2022-09-08 08:46:06 -06002414 def test_lacros_uprev_new_ebuild_created(self):
2415 """Test lacros package uprev."""
2416 lacros_outcome = self.newEbuildCreatedOutcome(
2417 self.MOCK_LACROS_EBUILD_PATH
2418 )
2419 self.PatchObject(
2420 uprev_lib,
2421 "uprev_workon_ebuild_to_version",
2422 side_effect=[lacros_outcome],
2423 )
2424 output = packages.uprev_lacros(None, self.refs, None)
2425 self.assertTrue(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002426
Alex Klein1699fab2022-09-08 08:46:06 -06002427 def test_lacros_uprev_newer_version_exist(self):
2428 """Test the newer version exists uprev should not happen."""
2429 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2430 self.PatchObject(
2431 uprev_lib,
2432 "uprev_workon_ebuild_to_version",
2433 side_effect=[lacros_outcome],
2434 )
2435 output = packages.uprev_lacros(None, self.refs, None)
2436 self.assertFalse(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002437
Alex Klein1699fab2022-09-08 08:46:06 -06002438 def test_lacros_uprev_same_version_exist(self):
2439 """Test the same version exists uprev should not happen."""
2440 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2441 self.PatchObject(
2442 uprev_lib,
2443 "uprev_workon_ebuild_to_version",
2444 side_effect=[lacros_outcome],
2445 )
2446 output = packages.uprev_lacros(None, self.refs, None)
2447 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002448
2449
2450class UprevLacrosInParallelTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002451 """Tests for uprev_lacros"""
Julio Hurtado870ed322021-12-03 18:22:40 +00002452
Alex Klein1699fab2022-09-08 08:46:06 -06002453 def setUp(self):
2454 self.refs = [
2455 GitRef(
2456 path="/lacros", revision="abc123", ref="refs/tags/123.456.789.0"
2457 )
2458 ]
2459 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtado870ed322021-12-03 18:22:40 +00002460
Alex Klein1699fab2022-09-08 08:46:06 -06002461 def revisionBumpOutcome(self, ebuild_path):
2462 return uprev_lib.UprevResult(
2463 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2464 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002465
Alex Klein1699fab2022-09-08 08:46:06 -06002466 def majorBumpOutcome(self, ebuild_path):
2467 return uprev_lib.UprevResult(
2468 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2469 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002470
Alex Klein1699fab2022-09-08 08:46:06 -06002471 def newerVersionOutcome(self, ebuild_path):
2472 return uprev_lib.UprevResult(
2473 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2474 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002475
Alex Klein1699fab2022-09-08 08:46:06 -06002476 def sameVersionOutcome(self, ebuild_path):
2477 return uprev_lib.UprevResult(
2478 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2479 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002480
Alex Klein1699fab2022-09-08 08:46:06 -06002481 def newEbuildCreatedOutcome(self, ebuild_path):
2482 return uprev_lib.UprevResult(
2483 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2484 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002485
Alex Klein1699fab2022-09-08 08:46:06 -06002486 def test_lacros_uprev_fails(self):
2487 """Test a lacros package uprev with no triggers"""
2488 self.PatchObject(
2489 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2490 )
Alex Klein314fb5d2022-10-24 14:56:31 -06002491 with self.assertRaises(uprev_lib.NoRefsError):
Alex Klein1699fab2022-09-08 08:46:06 -06002492 packages.uprev_lacros_in_parallel(None, [], None)
Julio Hurtado870ed322021-12-03 18:22:40 +00002493
Alex Klein1699fab2022-09-08 08:46:06 -06002494 def test_lacros_uprev_revision_bump(self):
2495 """Test lacros package uprev."""
2496 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2497 self.PatchObject(
2498 uprev_lib,
2499 "uprev_workon_ebuild_to_version",
2500 side_effect=[lacros_outcome],
2501 )
2502 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2503 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002504
Alex Klein1699fab2022-09-08 08:46:06 -06002505 def test_lacros_uprev_version_bump(self):
2506 """Test lacros package uprev."""
2507 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2508 self.PatchObject(
2509 uprev_lib,
2510 "uprev_workon_ebuild_to_version",
2511 side_effect=[lacros_outcome],
2512 )
2513 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2514 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002515
Alex Klein1699fab2022-09-08 08:46:06 -06002516 def test_lacros_uprev_new_ebuild_created(self):
2517 """Test lacros package uprev."""
2518 lacros_outcome = self.newEbuildCreatedOutcome(
2519 self.MOCK_LACROS_EBUILD_PATH
2520 )
2521 self.PatchObject(
2522 uprev_lib,
2523 "uprev_workon_ebuild_to_version",
2524 side_effect=[lacros_outcome],
2525 )
2526 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2527 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002528
Alex Klein1699fab2022-09-08 08:46:06 -06002529 def test_lacros_uprev_newer_version_exist(self):
2530 """Test the newer version exists uprev should not happen."""
2531 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2532 self.PatchObject(
2533 uprev_lib,
2534 "uprev_workon_ebuild_to_version",
2535 side_effect=[lacros_outcome],
2536 )
2537 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2538 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002539
Alex Klein1699fab2022-09-08 08:46:06 -06002540 def test_lacros_uprev_same_version_exist(self):
2541 """Test the same version exists uprev should not happen."""
2542 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2543 self.PatchObject(
2544 uprev_lib,
2545 "uprev_workon_ebuild_to_version",
2546 side_effect=[lacros_outcome],
2547 )
2548 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2549 self.assertFalse(output.uprevved)