blob: 5cd6f74e6f6bf05bfdc2ffb1c464507fbdce48f5 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Kleineb77ffa2019-05-28 14:47:44 -06002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Packages service tests."""
6
Madeleine Hardt8ae7f102022-03-24 20:26:11 +00007import io
Andrew Lamb2bde9e42019-11-04 13:24:09 -07008import json
9import os
Michael Mortensen009cb662019-10-21 11:38:43 -060010import re
Madeleine Hardt8ae7f102022-03-24 20:26:11 +000011from unittest import mock
Michael Mortensen009cb662019-10-21 11:38:43 -060012
Mike Frysinger2c024062021-05-22 15:43:22 -040013from chromite.third_party.google.protobuf import json_format
14from chromite.third_party.google.protobuf.field_mask_pb2 import FieldMask
Mike Frysinger68796b52019-08-25 00:04:27 -040015import pytest
Andrew Lamb2bde9e42019-11-04 13:24:09 -070016
Chris McDonaldea0312c2020-05-04 23:33:15 -060017import chromite as cr
Andrew Lamb2bde9e42019-11-04 13:24:09 -070018from chromite.api.gen.config.replication_config_pb2 import (
Mike Frysinger68796b52019-08-25 00:04:27 -040019 FILE_TYPE_JSON,
20 FileReplicationRule,
21 REPLICATION_TYPE_FILTER,
22 ReplicationConfig,
23)
Alex Klein2960c752020-03-09 13:43:38 -060024from chromite.lib import build_target_lib
Ram Chandrasekar60f69f32022-06-03 22:49:30 +000025from chromite.lib import chromeos_version
Andrew Lamb2bde9e42019-11-04 13:24:09 -070026from chromite.lib import constants
Michael Mortensene0f4b542019-10-24 15:30:23 -060027from chromite.lib import cros_build_lib
Alex Kleineb77ffa2019-05-28 14:47:44 -060028from chromite.lib import cros_test_lib
Alex Klein6becabc2020-09-11 14:03:05 -060029from chromite.lib import dependency_graph
Mike Frysinger68796b52019-08-25 00:04:27 -040030from chromite.lib import depgraph
Michael Mortensenb70e8a82019-10-10 18:43:41 -060031from chromite.lib import osutils
Mike Frysinger88d96362020-02-14 19:05:45 -050032from chromite.lib import partial_mock
Alex Klein87531182019-08-12 15:23:37 -060033from chromite.lib import portage_util
Chris McDonaldea0312c2020-05-04 23:33:15 -060034from chromite.lib import uprev_lib
Alex Klein87531182019-08-12 15:23:37 -060035from chromite.lib.chroot_lib import Chroot
Alex Klein18a60af2020-06-11 12:08:47 -060036from chromite.lib.parser import package_info
Andrew Lamb2bde9e42019-11-04 13:24:09 -070037from chromite.lib.uprev_lib import GitRef
Shao-Chuan Lee05e51142021-11-24 12:27:37 +090038from chromite.service import android
Alex Kleineb77ffa2019-05-28 14:47:44 -060039from chromite.service import packages
40
Mike Frysinger68796b52019-08-25 00:04:27 -040041
Andrew Lamb2bde9e42019-11-04 13:24:09 -070042D = cros_test_lib.Directory
43
Alex Kleineb77ffa2019-05-28 14:47:44 -060044
Alex Klein4de25e82019-08-05 15:58:39 -060045class UprevAndroidTest(cros_test_lib.RunCommandTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -060046 """Uprev android tests."""
Alex Klein4de25e82019-08-05 15:58:39 -060047
Alex Klein1699fab2022-09-08 08:46:06 -060048 def _mock_successful_uprev(self):
49 self.rc.AddCmdResult(
50 partial_mock.In("cros_mark_android_as_stable"),
51 stdout=(
52 '{"revved": true,'
53 ' "android_atom": "android/android-1.0",'
54 ' "modified_files": ["file1", "file2"]}'
55 ),
56 )
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090057
Alex Klein1699fab2022-09-08 08:46:06 -060058 def test_success(self):
59 """Test successful run handling."""
60 self._mock_successful_uprev()
61 build_targets = [
62 build_target_lib.BuildTarget(t) for t in ["foo", "bar"]
63 ]
Alex Klein4de25e82019-08-05 15:58:39 -060064
Alex Klein1699fab2022-09-08 08:46:06 -060065 result = packages.uprev_android(
66 "android/package", Chroot(), build_targets=build_targets
67 )
68 self.assertCommandContains(
69 [
70 "cros_mark_android_as_stable",
71 "--android_package=android/package",
72 "--boards=foo:bar",
73 ]
74 )
75 self.assertCommandContains(["emerge-foo"])
76 self.assertCommandContains(["emerge-bar"])
Alex Klein4de25e82019-08-05 15:58:39 -060077
Alex Klein1699fab2022-09-08 08:46:06 -060078 self.assertTrue(result.revved)
79 self.assertEqual(result.android_atom, "android/android-1.0")
80 self.assertListEqual(result.modified_files, ["file1", "file2"])
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +090081
Alex Klein1699fab2022-09-08 08:46:06 -060082 def test_android_build_branch(self):
83 """Test specifying android_build_branch option."""
84 self._mock_successful_uprev()
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090085
Alex Klein1699fab2022-09-08 08:46:06 -060086 packages.uprev_android(
87 "android/package",
88 Chroot(),
89 android_build_branch="android-build-branch",
90 )
91 self.assertCommandContains(
92 [
93 "cros_mark_android_as_stable",
94 "--android_package=android/package",
95 "--android_build_branch=android-build-branch",
96 ]
97 )
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +090098
Alex Klein1699fab2022-09-08 08:46:06 -060099 def test_android_version(self):
100 """Test specifying android_version option."""
101 self._mock_successful_uprev()
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 packages.uprev_android(
104 "android/package", Chroot(), android_version="7123456"
105 )
106 self.assertCommandContains(
107 [
108 "cros_mark_android_as_stable",
109 "--android_package=android/package",
110 "--force_version=7123456",
111 ]
112 )
Shao-Chuan Leea4b4f302021-05-12 14:40:20 +0900113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 def test_skip_commit(self):
115 """Test specifying skip_commit option."""
116 self._mock_successful_uprev()
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900117
Alex Klein1699fab2022-09-08 08:46:06 -0600118 packages.uprev_android("android/package", Chroot(), skip_commit=True)
119 self.assertCommandContains(
120 [
121 "cros_mark_android_as_stable",
122 "--android_package=android/package",
123 "--skip_commit",
124 ]
125 )
Shao-Chuan Lee85ba7ce2021-02-09 13:50:11 +0900126
Alex Klein1699fab2022-09-08 08:46:06 -0600127 def test_no_uprev(self):
128 """Test no uprev handling."""
129 self.rc.AddCmdResult(
130 partial_mock.In("cros_mark_android_as_stable"),
131 stdout='{"revved": false}',
132 )
133 build_targets = [
134 build_target_lib.BuildTarget(t) for t in ["foo", "bar"]
135 ]
136 result = packages.uprev_android(
137 "android/package", Chroot(), build_targets=build_targets
138 )
Alex Klein4de25e82019-08-05 15:58:39 -0600139
Alex Klein1699fab2022-09-08 08:46:06 -0600140 self.assertCommandContains(
141 ["cros_mark_android_as_stable", "--boards=foo:bar"]
142 )
143 self.assertCommandContains(["emerge-foo"], expected=False)
144 self.assertCommandContains(["emerge-bar"], expected=False)
Alex Klein4de25e82019-08-05 15:58:39 -0600145
Alex Klein1699fab2022-09-08 08:46:06 -0600146 self.assertFalse(result.revved)
Shao-Chuan Lee84bf9a22021-11-19 17:42:11 +0900147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 def test_ignore_junk_in_stdout(self):
149 """Test when stdout contains junk messages."""
150 self.rc.AddCmdResult(
151 partial_mock.In("cros_mark_android_as_stable"),
152 stdout='foo\nbar\n{"revved": false}\n',
153 )
154 result = packages.uprev_android("android/package", Chroot())
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900155
Alex Klein1699fab2022-09-08 08:46:06 -0600156 self.assertFalse(result.revved)
Shao-Chuan Leedea458f2021-11-25 23:46:53 +0900157
Alex Klein4de25e82019-08-05 15:58:39 -0600158
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900159class UprevAndroidLKGBTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600160 """Tests for uprevving Android with LKGB."""
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900161
Alex Klein1699fab2022-09-08 08:46:06 -0600162 def test_registered_handlers(self):
163 """Test that each Android package has an uprev handler registered."""
164 mock_handler = self.PatchObject(packages, "uprev_android_lkgb")
Shao-Chuan Lee05e51142021-11-24 12:27:37 +0900165
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
Alex Klein1699fab2022-09-08 08:46:06 -0600459 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700460
Alex Klein1699fab2022-09-08 08:46:06 -0600461 ebuild_path = os.path.join(package_path, self.ebuild)
462 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
Alex Klein1699fab2022-09-08 08:46:06 -0600480 old_ebuild_path = os.path.join(
481 package_path, self.ebuild_template % self.version
482 )
483 new_ebuild_path = os.path.join(
484 package_path, self.ebuild_template % self.new_version
485 )
486 manifest_path = os.path.join(package_path, "Manifest")
Trent Begin2e5344f2020-03-02 10:46:55 -0700487
Alex Klein1699fab2022-09-08 08:46:06 -0600488 expected_modified_files = [
489 old_ebuild_path,
490 new_ebuild_path,
491 manifest_path,
492 ]
493 self.assertCountEqual(mod.files, expected_modified_files)
Trent Begin4a11a632020-02-28 12:59:58 -0700494
Alex Klein1699fab2022-09-08 08:46:06 -0600495 self.assertCommandContains(["ebuild", "manifest"])
Trent Begin6daa8702020-01-29 14:58:12 -0700496
Alex Klein1699fab2022-09-08 08:46:06 -0600497 def test_uprev_ebuild_same_version(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700498 """Tests uprev of ebuild with version path with unchanged version.
Fergus Dall2209d0b2020-08-06 11:51:43 +1000499
Alex Klein1699fab2022-09-08 08:46:06 -0600500 This should result in bumping the revision number.
501 """
502 file_layout = (
503 D(self.package, [self.ebuild, self.unstable_ebuild, self.manifest]),
504 )
505 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000506
Alex Klein1699fab2022-09-08 08:46:06 -0600507 package_path = os.path.join(self.tempdir, self.package)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000508
Alex Klein1699fab2022-09-08 08:46:06 -0600509 ebuild_path = os.path.join(package_path, self.ebuild)
510 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000511
Alex Klein1699fab2022-09-08 08:46:06 -0600512 result = uprev_lib.uprev_ebuild_from_pin(
513 package_path, self.version, chroot=Chroot()
514 )
515 self.assertEqual(
516 len(result.modified),
517 1,
518 "unexpected number of results: %s" % len(result.modified),
519 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000520
Alex Klein1699fab2022-09-08 08:46:06 -0600521 mod = result.modified[0]
522 self.assertEqual(
523 mod.new_version,
524 self.version + "-r2",
525 "unexpected version number: %s" % mod.new_version,
526 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000527
Alex Klein1699fab2022-09-08 08:46:06 -0600528 old_ebuild_path = os.path.join(
529 package_path, self.ebuild_template % self.version
530 )
531 new_ebuild_path = os.path.join(
532 package_path, "package-%s-r2.ebuild" % self.version
533 )
534 manifest_path = os.path.join(package_path, "Manifest")
Fergus Dall2209d0b2020-08-06 11:51:43 +1000535
Alex Klein1699fab2022-09-08 08:46:06 -0600536 expected_modified_files = [
537 old_ebuild_path,
538 new_ebuild_path,
539 manifest_path,
540 ]
541 self.assertCountEqual(mod.files, expected_modified_files)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000542
Alex Klein1699fab2022-09-08 08:46:06 -0600543 self.assertCommandContains(["ebuild", "manifest"])
Fergus Dall2209d0b2020-08-06 11:51:43 +1000544
Alex Klein1699fab2022-09-08 08:46:06 -0600545 def test_no_ebuild(self):
546 """Tests assertion is raised if package has no ebuilds"""
547 file_layout = (D(self.package, [self.manifest]),)
548 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700549
Alex Klein1699fab2022-09-08 08:46:06 -0600550 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700551
Alex Klein1699fab2022-09-08 08:46:06 -0600552 with self.assertRaises(uprev_lib.EbuildUprevError):
553 uprev_lib.uprev_ebuild_from_pin(
554 package_path, self.new_version, chroot=Chroot()
555 )
Trent Begin315d9d92019-12-03 21:55:53 -0700556
Alex Klein1699fab2022-09-08 08:46:06 -0600557 def test_multiple_stable_ebuilds(self):
558 """Tests assertion is raised if multiple stable ebuilds are present"""
559 file_layout = (
560 D(
561 self.package,
562 [self.ebuild, self.ebuild_template % "1.2.1", self.manifest],
563 ),
564 )
565 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000566
Alex Klein1699fab2022-09-08 08:46:06 -0600567 package_path = os.path.join(self.tempdir, self.package)
Fergus Dall2209d0b2020-08-06 11:51:43 +1000568
Alex Klein1699fab2022-09-08 08:46:06 -0600569 ebuild_path = os.path.join(package_path, self.ebuild)
570 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000571
Alex Klein1699fab2022-09-08 08:46:06 -0600572 ebuild_path = os.path.join(package_path, self.ebuild_template % "1.2.1")
573 self.WriteTempFile(ebuild_path, 'KEYWORDS="*"\n')
Fergus Dall2209d0b2020-08-06 11:51:43 +1000574
Alex Klein1699fab2022-09-08 08:46:06 -0600575 with self.assertRaises(uprev_lib.EbuildUprevError):
576 uprev_lib.uprev_ebuild_from_pin(
577 package_path, self.new_version, chroot=Chroot()
578 )
Fergus Dall2209d0b2020-08-06 11:51:43 +1000579
Alex Klein1699fab2022-09-08 08:46:06 -0600580 def test_multiple_unstable_ebuilds(self):
581 """Tests assertion is raised if multiple unstable ebuilds are present"""
582 file_layout = (
583 D(
584 self.package,
585 [self.ebuild, self.ebuild_template % "1.2.1", self.manifest],
586 ),
587 )
588 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Trent Begin315d9d92019-12-03 21:55:53 -0700589
Alex Klein1699fab2022-09-08 08:46:06 -0600590 package_path = os.path.join(self.tempdir, self.package)
Trent Begin315d9d92019-12-03 21:55:53 -0700591
Alex Klein1699fab2022-09-08 08:46:06 -0600592 with self.assertRaises(uprev_lib.EbuildUprevError):
593 uprev_lib.uprev_ebuild_from_pin(
594 package_path, self.new_version, chroot=Chroot()
595 )
Trent Begin315d9d92019-12-03 21:55:53 -0700596
597
Andrew Lamb9563a152019-12-04 11:42:18 -0700598class ReplicatePrivateConfigTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600599 """replicate_private_config tests."""
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700600
Alex Klein1699fab2022-09-08 08:46:06 -0600601 def setUp(self):
602 # Set up fake public and private chromeos-config overlays.
603 private_package_root = (
604 "src/private-overlays/overlay-coral-private/chromeos-base/"
605 "chromeos-config-bsp"
606 )
607 self.public_package_root = (
608 "src/overlays/overlay-coral/chromeos-base/chromeos-config-bsp"
609 )
610 file_layout = (
611 D(
612 os.path.join(private_package_root, "files"),
613 ["build_config.json"],
614 ),
615 D(private_package_root, ["replication_config.jsonpb"]),
616 D(
617 os.path.join(self.public_package_root, "files"),
618 ["build_config.json"],
619 ),
620 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700621
Alex Klein1699fab2022-09-08 08:46:06 -0600622 cros_test_lib.CreateOnDiskHierarchy(self.tempdir, file_layout)
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700623
Alex Klein1699fab2022-09-08 08:46:06 -0600624 # Private config contains 'a' and 'b' fields.
625 self.private_config_path = os.path.join(
626 private_package_root, "files", "build_config.json"
627 )
628 self.WriteTempFile(
629 self.private_config_path,
630 json.dumps({"chromeos": {"configs": [{"a": 3, "b": 2}]}}),
631 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700632
Alex Kleinfee86da2023-01-20 18:40:06 -0700633 # Public config only contains the 'a' field. Note that the value of 'a'
634 # is 1 in the public config; it will get updated to 3 when the private
635 # config is replicated.
Alex Klein1699fab2022-09-08 08:46:06 -0600636 self.public_config_path = os.path.join(
637 self.public_package_root, "files", "build_config.json"
638 )
639 self.WriteTempFile(
640 self.public_config_path,
641 json.dumps({"chromeos": {"configs": [{"a": 1}]}}),
642 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700643
Alex Klein1699fab2022-09-08 08:46:06 -0600644 # Put a ReplicationConfig JSONPB in the private package. Note that it
645 # specifies only the 'a' field is replicated.
646 self.replication_config_path = os.path.join(
647 self.tempdir, private_package_root, "replication_config.jsonpb"
648 )
649 replication_config = ReplicationConfig(
650 file_replication_rules=[
651 FileReplicationRule(
652 source_path=self.private_config_path,
653 destination_path=self.public_config_path,
654 file_type=FILE_TYPE_JSON,
655 replication_type=REPLICATION_TYPE_FILTER,
656 destination_fields=FieldMask(paths=["a"]),
657 )
658 ]
659 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700660
Alex Klein1699fab2022-09-08 08:46:06 -0600661 osutils.WriteFile(
662 self.replication_config_path,
663 json_format.MessageToJson(replication_config),
664 )
665 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700666
Alex Klein1699fab2022-09-08 08:46:06 -0600667 self.rc.SetDefaultCmdResult(side_effect=self._write_generated_c_files)
Andrew Lamb9563a152019-12-04 11:42:18 -0700668
Alex Klein1699fab2022-09-08 08:46:06 -0600669 def _write_generated_c_files(self, *_args, **_kwargs):
670 """Write fake generated C files to the public output dir.
Andrew Lamb9563a152019-12-04 11:42:18 -0700671
Alex Kleinfee86da2023-01-20 18:40:06 -0700672 Note that this function accepts args and kwargs so it can be used as a
673 side effect.
Alex Klein1699fab2022-09-08 08:46:06 -0600674 """
675 output_dir = os.path.join(self.public_package_root, "files")
676 self.WriteTempFile(os.path.join(output_dir, "config.c"), "")
677 self.WriteTempFile(os.path.join(output_dir, "ec_config.c"), "")
678 self.WriteTempFile(os.path.join(output_dir, "ec_config.h"), "")
Andrew Lamb9563a152019-12-04 11:42:18 -0700679
Alex Klein1699fab2022-09-08 08:46:06 -0600680 def _write_incorrect_generated_c_files(self, *_args, **_kwargs):
681 """Similar to _write_generated_c_files, with an expected file missing.
Andrew Lamb9563a152019-12-04 11:42:18 -0700682
Alex Kleinfee86da2023-01-20 18:40:06 -0700683 Note that this function accepts args and kwargs so it can be used as a
684 side effect.
Alex Klein1699fab2022-09-08 08:46:06 -0600685 """
686 output_dir = os.path.join(self.public_package_root, "files")
687 self.WriteTempFile(os.path.join(output_dir, "config.c"), "")
688 self.WriteTempFile(os.path.join(output_dir, "ec_config.c"), "")
Andrew Lamb9563a152019-12-04 11:42:18 -0700689
Alex Klein1699fab2022-09-08 08:46:06 -0600690 def test_replicate_private_config(self):
691 """Basic replication test."""
692 refs = [
693 GitRef(
694 path="/chromeos/overlays/overlay-coral-private",
695 ref="main",
696 revision="123",
697 )
698 ]
699 chroot = Chroot()
700 result = packages.replicate_private_config(
701 _build_targets=None, refs=refs, chroot=chroot
702 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700703
Alex Klein1699fab2022-09-08 08:46:06 -0600704 self.assertCommandContains(
705 [
706 "cros_config_schema",
707 "-m",
708 os.path.join(
709 constants.CHROOT_SOURCE_ROOT, self.public_config_path
710 ),
711 "-g",
712 os.path.join(
713 constants.CHROOT_SOURCE_ROOT,
714 self.public_package_root,
715 "files",
716 ),
717 "-f",
718 '"TRUE"',
719 ],
720 enter_chroot=True,
721 chroot_args=chroot.get_enter_args(),
722 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700723
Alex Klein1699fab2022-09-08 08:46:06 -0600724 self.assertEqual(len(result.modified), 1)
725 # The public build_config.json and generated C files were modified.
726 expected_modified_files = [
727 os.path.join(self.tempdir, self.public_config_path),
728 os.path.join(
729 self.tempdir, self.public_package_root, "files", "config.c"
730 ),
731 os.path.join(
732 self.tempdir, self.public_package_root, "files", "ec_config.c"
733 ),
734 os.path.join(
735 self.tempdir, self.public_package_root, "files", "ec_config.h"
736 ),
737 ]
738 self.assertEqual(result.modified[0].files, expected_modified_files)
739 self.assertEqual(result.modified[0].new_version, "123")
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700740
Alex Kleinfee86da2023-01-20 18:40:06 -0700741 # The update from the private build_config.json was copied to the
742 # public. Note that only the 'a' field is present, as per
743 # destination_fields.
Alex Klein1699fab2022-09-08 08:46:06 -0600744 self.assertEqual(
745 json.loads(self.ReadTempFile(self.public_config_path)),
746 {"chromeos": {"configs": [{"a": 3}]}},
747 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700748
Alex Klein1699fab2022-09-08 08:46:06 -0600749 def test_replicate_private_config_no_build_config(self):
750 """If there is no build config, don't generate C files."""
Alex Kleinfee86da2023-01-20 18:40:06 -0700751 # Modify the replication config to write to "other_config.json" instead
752 # of "build_config.json"
Alex Klein1699fab2022-09-08 08:46:06 -0600753 modified_destination_path = self.public_config_path.replace(
754 "build_config", "other_config"
755 )
756 replication_config = ReplicationConfig(
757 file_replication_rules=[
758 FileReplicationRule(
759 source_path=self.private_config_path,
760 destination_path=modified_destination_path,
761 file_type=FILE_TYPE_JSON,
762 replication_type=REPLICATION_TYPE_FILTER,
763 destination_fields=FieldMask(paths=["a"]),
764 )
765 ]
766 )
767 osutils.WriteFile(
768 self.replication_config_path,
769 json_format.MessageToJson(replication_config),
770 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700771
Alex Klein1699fab2022-09-08 08:46:06 -0600772 refs = [
773 GitRef(
774 path="/chromeos/overlays/overlay-coral-private",
775 ref="main",
776 revision="123",
777 )
778 ]
779 result = packages.replicate_private_config(
780 _build_targets=None, refs=refs, chroot=Chroot()
781 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700782
Alex Klein1699fab2022-09-08 08:46:06 -0600783 self.assertEqual(len(result.modified), 1)
784 self.assertEqual(
785 result.modified[0].files,
786 [os.path.join(self.tempdir, modified_destination_path)],
787 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700788
Alex Klein1699fab2022-09-08 08:46:06 -0600789 def test_replicate_private_config_multiple_build_configs(self):
790 """An error is thrown if there is more than one build config."""
791 replication_config = ReplicationConfig(
792 file_replication_rules=[
793 FileReplicationRule(
794 source_path=self.private_config_path,
795 destination_path=self.public_config_path,
796 file_type=FILE_TYPE_JSON,
797 replication_type=REPLICATION_TYPE_FILTER,
798 destination_fields=FieldMask(paths=["a"]),
799 ),
800 FileReplicationRule(
801 source_path=self.private_config_path,
802 destination_path=self.public_config_path,
803 file_type=FILE_TYPE_JSON,
804 replication_type=REPLICATION_TYPE_FILTER,
805 destination_fields=FieldMask(paths=["a"]),
806 ),
807 ]
808 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700809
Alex Klein1699fab2022-09-08 08:46:06 -0600810 osutils.WriteFile(
811 self.replication_config_path,
812 json_format.MessageToJson(replication_config),
813 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700814
Alex Klein1699fab2022-09-08 08:46:06 -0600815 refs = [
816 GitRef(
817 path="/chromeos/overlays/overlay-coral-private",
818 ref="main",
819 revision="123",
820 )
821 ]
822 with self.assertRaisesRegex(
823 ValueError,
824 "Expected at most one build_config.json destination path.",
825 ):
826 packages.replicate_private_config(
827 _build_targets=None, refs=refs, chroot=Chroot()
828 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700829
Alex Klein1699fab2022-09-08 08:46:06 -0600830 def test_replicate_private_config_generated_files_incorrect(self):
831 """An error is thrown if generated C files are missing."""
832 self.rc.SetDefaultCmdResult(
833 side_effect=self._write_incorrect_generated_c_files
834 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700835
Alex Klein1699fab2022-09-08 08:46:06 -0600836 refs = [
837 GitRef(
838 path="/chromeos/overlays/overlay-coral-private",
839 ref="main",
840 revision="123",
841 )
842 ]
843 chroot = Chroot()
Andrew Lamb9563a152019-12-04 11:42:18 -0700844
Alex Klein1699fab2022-09-08 08:46:06 -0600845 with self.assertRaisesRegex(
846 packages.GeneratedCrosConfigFilesError,
847 "Expected to find generated C files",
848 ):
849 packages.replicate_private_config(
850 _build_targets=None, refs=refs, chroot=chroot
851 )
Andrew Lamb9563a152019-12-04 11:42:18 -0700852
Alex Klein1699fab2022-09-08 08:46:06 -0600853 def test_replicate_private_config_wrong_number_of_refs(self):
854 """An error is thrown if there is not exactly one ref."""
855 with self.assertRaisesRegex(ValueError, "Expected exactly one ref"):
856 packages.replicate_private_config(
857 _build_targets=None, refs=[], chroot=None
858 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700859
Alex Klein1699fab2022-09-08 08:46:06 -0600860 with self.assertRaisesRegex(ValueError, "Expected exactly one ref"):
861 refs = [
862 GitRef(path="a", ref="main", revision="1"),
863 GitRef(path="a", ref="main", revision="2"),
864 ]
865 packages.replicate_private_config(
866 _build_targets=None, refs=refs, chroot=None
867 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700868
Alex Klein1699fab2022-09-08 08:46:06 -0600869 def test_replicate_private_config_replication_config_missing(self):
870 """An error is thrown if there is not a replication config."""
871 os.remove(self.replication_config_path)
872 with self.assertRaisesRegex(
873 ValueError,
874 "Expected ReplicationConfig missing at %s"
875 % self.replication_config_path,
876 ):
877 refs = [
878 GitRef(
879 path="/chromeos/overlays/overlay-coral-private",
880 ref="main",
881 revision="123",
882 )
883 ]
884 packages.replicate_private_config(
885 _build_targets=None, refs=refs, chroot=None
886 )
Andrew Lambe836f222019-12-09 12:27:38 -0700887
Alex Klein1699fab2022-09-08 08:46:06 -0600888 def test_replicate_private_config_wrong_git_ref_path(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700889 """Git ref that doesn't point to a private overlay throws error."""
Alex Klein1699fab2022-09-08 08:46:06 -0600890 with self.assertRaisesRegex(
891 ValueError, "ref.path must match the pattern"
892 ):
893 refs = [GitRef(path="a/b/c", ref="main", revision="123")]
894 packages.replicate_private_config(
895 _build_targets=None, refs=refs, chroot=None
896 )
Andrew Lamb2bde9e42019-11-04 13:24:09 -0700897
898
Alex Klein5caab872021-09-10 11:44:37 -0600899class GetBestVisibleTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600900 """get_best_visible tests."""
David Burger1e0fe232019-07-01 14:52:07 -0600901
Alex Klein1699fab2022-09-08 08:46:06 -0600902 def test_empty_atom_fails(self):
903 """Test empty atom raises an error."""
904 with self.assertRaises(AssertionError):
905 packages.get_best_visible("")
Alex Kleinda39c6d2019-09-16 14:36:36 -0600906
907
Alex Klein149fd3b2019-12-16 16:01:05 -0700908class HasPrebuiltTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600909 """has_prebuilt tests."""
Alex Kleinda39c6d2019-09-16 14:36:36 -0600910
Alex Klein1699fab2022-09-08 08:46:06 -0600911 def test_empty_atom_fails(self):
912 """Test an empty atom results in an error."""
913 with self.assertRaises(AssertionError):
914 packages.has_prebuilt("")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600915
Alex Klein1699fab2022-09-08 08:46:06 -0600916 def test_use_flags(self):
917 """Test use flags get propagated correctly."""
918 # We don't really care about the result, just the env handling.
919 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
920 # Ignore any flags that may be in the environment.
921 self.PatchObject(os.environ, "get", return_value="")
Alex Klein149fd3b2019-12-16 16:01:05 -0700922
Alex Klein1699fab2022-09-08 08:46:06 -0600923 packages.has_prebuilt("cat/pkg-1.2.3", useflags="useflag")
924 patch.assert_called_with(
925 "cat/pkg-1.2.3", board=None, extra_env={"USE": "useflag"}
926 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700927
Alex Klein1699fab2022-09-08 08:46:06 -0600928 def test_env_use_flags(self):
929 """Test env use flags get propagated correctly with passed useflags."""
930 # We don't really care about the result, just the env handling.
931 patch = self.PatchObject(portage_util, "HasPrebuilt", return_value=True)
932 # Add some flags to the environment.
933 existing_flags = "already set flags"
934 self.PatchObject(os.environ, "get", return_value=existing_flags)
Alex Klein149fd3b2019-12-16 16:01:05 -0700935
Alex Klein1699fab2022-09-08 08:46:06 -0600936 new_flags = "useflag"
937 packages.has_prebuilt("cat/pkg-1.2.3", useflags=new_flags)
938 expected = "%s %s" % (existing_flags, new_flags)
939 patch.assert_called_with(
940 "cat/pkg-1.2.3", board=None, extra_env={"USE": expected}
941 )
Alex Klein149fd3b2019-12-16 16:01:05 -0700942
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600943
944class AndroidVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600945 """Tests getting android versions."""
Michael Mortensen14960d02019-10-18 07:53:59 -0600946
Alex Klein1699fab2022-09-08 08:46:06 -0600947 def setUp(self):
948 package_result = [
949 "chromeos-base/android-container-nyc-4717008-r1",
950 "chromeos-base/update_engine-0.0.3-r3408",
951 ]
952 self.PatchObject(
953 portage_util, "GetPackageDependencies", return_value=package_result
954 )
955 self.board = "board"
956 self.PatchObject(
957 portage_util,
958 "FindEbuildForBoardPackage",
959 return_value="chromeos-base/android-container-nyc",
960 )
961 FakeEnvironment = {
962 "ARM_TARGET": "3-linux-target",
963 }
964 self.PatchObject(
965 osutils, "SourceEnvironment", return_value=FakeEnvironment
966 )
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600967
Alex Kleinfee86da2023-01-20 18:40:06 -0700968 # Clear the LRU cache for the function. We mock the function that
969 # provides the data this function processes to produce its result, so we
970 # need to clear it manually.
Alex Klein1699fab2022-09-08 08:46:06 -0600971 packages.determine_android_package.cache_clear()
Alex Klein68a28712021-11-08 11:08:30 -0700972
Alex Klein1699fab2022-09-08 08:46:06 -0600973 def test_determine_android_version(self):
974 """Tests that a valid android version is returned."""
975 version = packages.determine_android_version(self.board)
976 self.assertEqual(version, "4717008")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600977
Alex Klein1699fab2022-09-08 08:46:06 -0600978 def test_determine_android_version_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700979 """Test None is returned for version when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -0600980 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
981 self.PatchObject(
982 portage_util, "GetPackageDependencies", return_value=package_result
983 )
984 version = packages.determine_android_version(self.board)
985 self.assertEqual(version, None)
Michael Mortensenedf76532019-10-16 14:22:37 -0600986
Alex Klein1699fab2022-09-08 08:46:06 -0600987 def test_determine_android_branch(self):
988 """Tests that a valid android branch is returned."""
989 branch = packages.determine_android_branch(self.board)
990 self.assertEqual(branch, "3")
Michael Mortensenb70e8a82019-10-10 18:43:41 -0600991
Alex Klein1699fab2022-09-08 08:46:06 -0600992 def test_determine_android_branch_64bit_targets(self):
Alex Kleinfee86da2023-01-20 18:40:06 -0700993 """Tests a valid android branch is returned with only 64bit targets."""
Alex Klein1699fab2022-09-08 08:46:06 -0600994 self.PatchObject(
995 osutils,
996 "SourceEnvironment",
997 return_value={"ARM64_TARGET": "3-linux-target"},
998 )
999 branch = packages.determine_android_branch(self.board)
1000 self.assertEqual(branch, "3")
Federico 'Morg' Pareschicd9165a2020-05-29 09:45:55 +09001001
Alex Klein1699fab2022-09-08 08:46:06 -06001002 def test_determine_android_branch_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001003 """Tests a None is returned for branch when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -06001004 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
1005 self.PatchObject(
1006 portage_util, "GetPackageDependencies", return_value=package_result
1007 )
1008 branch = packages.determine_android_branch(self.board)
1009 self.assertEqual(branch, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001010
Alex Klein1699fab2022-09-08 08:46:06 -06001011 def test_determine_android_target(self):
1012 """Tests that a valid android target is returned."""
1013 target = packages.determine_android_target(self.board)
1014 self.assertEqual(target, "cheets")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001015
Alex Klein1699fab2022-09-08 08:46:06 -06001016 def test_determine_android_target_when_not_present(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001017 """Tests a None is returned for target when android is not present."""
Alex Klein1699fab2022-09-08 08:46:06 -06001018 package_result = ["chromeos-base/update_engine-0.0.3-r3408"]
1019 self.PatchObject(
1020 portage_util, "GetPackageDependencies", return_value=package_result
1021 )
1022 target = packages.determine_android_target(self.board)
1023 self.assertEqual(target, None)
Michael Mortensenedf76532019-10-16 14:22:37 -06001024
Alex Klein1699fab2022-09-08 08:46:06 -06001025 def test_determine_android_version_handle_exception(self):
1026 """Tests handling RunCommandError inside determine_android_version."""
Alex Kleinfee86da2023-01-20 18:40:06 -07001027 # Mock what happens when portage returns that bubbles up (via
1028 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001029 self.PatchObject(
1030 portage_util,
1031 "GetPackageDependencies",
1032 side_effect=cros_build_lib.RunCommandError("error"),
1033 )
1034 target = packages.determine_android_version(self.board)
1035 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -06001036
Alex Klein1699fab2022-09-08 08:46:06 -06001037 def test_determine_android_package_handle_exception(self):
1038 """Tests handling RunCommandError inside determine_android_package."""
Alex Kleinfee86da2023-01-20 18:40:06 -07001039 # Mock what happens when portage returns that bubbles up (via
1040 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001041 self.PatchObject(
1042 portage_util,
1043 "GetPackageDependencies",
1044 side_effect=cros_build_lib.RunCommandError("error"),
1045 )
1046 target = packages.determine_android_package(self.board)
1047 self.assertEqual(target, None)
Michael Mortensene0f4b542019-10-24 15:30:23 -06001048
Alex Klein1699fab2022-09-08 08:46:06 -06001049 def test_determine_android_package_callers_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001050 """Tests RunCommandError caught by determine_android_package callers."""
1051 # Mock what happens when portage returns that bubbles up (via
1052 # RunCommand) inside portage_util.GetPackageDependencies.
Alex Klein1699fab2022-09-08 08:46:06 -06001053 self.PatchObject(
1054 portage_util,
1055 "GetPackageDependencies",
1056 side_effect=cros_build_lib.RunCommandError("error"),
1057 )
1058 # Verify that target is None, as expected.
1059 target = packages.determine_android_package(self.board)
1060 self.assertEqual(target, None)
1061 # determine_android_branch calls determine_android_package
1062 branch = packages.determine_android_branch(self.board)
1063 self.assertEqual(branch, None)
1064 # determine_android_target calls determine_android_package
1065 target = packages.determine_android_target(self.board)
1066 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001067
Michael Mortensene0f4b542019-10-24 15:30:23 -06001068
Alex Klein1699fab2022-09-08 08:46:06 -06001069@pytest.mark.usefixtures("testcase_caplog", "testcase_monkeypatch")
Michael Mortensende716a12020-05-15 11:27:00 -06001070class FindFingerprintsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001071 """Tests for find_fingerprints."""
Michael Mortensende716a12020-05-15 11:27:00 -06001072
Alex Klein1699fab2022-09-08 08:46:06 -06001073 def setUp(self):
1074 self.board = "test-board"
1075 # Create cheets-fingerprints.txt based on tempdir/src...
1076 self.fingerprint_contents = (
1077 "google/test-board/test-board_cheets"
1078 ":9/R99-12345.0.9999/123456:user/release-keys"
1079 )
1080 fingerprint_path = os.path.join(
1081 self.tempdir,
1082 "src/build/images/test-board/latest/cheets-fingerprint.txt",
1083 )
Brian Norris4f251e42023-03-09 15:53:26 -08001084 self.chroot = Chroot(
1085 path=self.tempdir / "chroot", out_path=self.tempdir / "out"
1086 )
Alex Klein1699fab2022-09-08 08:46:06 -06001087 osutils.WriteFile(
1088 fingerprint_path, self.fingerprint_contents, makedirs=True
1089 )
Michael Mortensende716a12020-05-15 11:27:00 -06001090
Alex Klein1699fab2022-09-08 08:46:06 -06001091 def test_find_fingerprints_with_test_path(self):
1092 """Tests get_firmware_versions with mocked output."""
1093 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1094 build_target = build_target_lib.BuildTarget(self.board)
1095 result = packages.find_fingerprints(build_target)
1096 self.assertEqual(result, [self.fingerprint_contents])
1097 self.assertIn("Reading fingerprint file", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001098
Alex Klein1699fab2022-09-08 08:46:06 -06001099 def test_find_fingerprints(self):
1100 """Tests get_firmware_versions with mocked output."""
1101 # Use board name whose path for fingerprint file does not exist.
1102 # Verify that fingerprint file is not found and None is returned.
1103 build_target = build_target_lib.BuildTarget("wrong-boardname")
1104 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1105 result = packages.find_fingerprints(build_target)
1106 self.assertEqual(result, [])
1107 self.assertIn("Fingerprint file not found", self.caplog.text)
Michael Mortensende716a12020-05-15 11:27:00 -06001108
1109
Michael Mortensen59e30872020-05-18 14:12:49 -06001110class GetAllFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001111 """Tests for get_firmware_versions."""
Michael Mortensen59e30872020-05-18 14:12:49 -06001112
Alex Klein1699fab2022-09-08 08:46:06 -06001113 def setUp(self):
1114 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001115 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001116 self.rc.SetDefaultCmdResult(
1117 stdout="""
Michael Mortensen59e30872020-05-18 14:12:49 -06001118
1119flashrom(8): 68935ee2fcfcffa47af81b966269cd2b */build/reef/usr/sbin/flashrom
1120 ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=e102cc98d45300b50088999d53775acbeff407dc, stripped
1121 0.9.9 : bbb2d6a : Jul 28 2017 15:12:34 UTC
1122
1123Model: reef
1124BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1125BIOS version: Google_Reef.9042.87.1
1126BIOS (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
1127BIOS (RW) version: Google_Reef.9042.110.0
1128EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1129EC version: reef_v1.1.5900-ab1ee51
1130EC (RW) version: reef_v1.1.5909-bd1f0c9
1131
1132Model: pyro
1133BIOS image: 9e62447ebf22a724a4a835018ab6234e */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/image.bin
1134BIOS version: Google_Pyro.9042.87.1
1135BIOS (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
1136BIOS (RW) version: Google_Pyro.9042.110.0
1137EC image: 44b93ed591733519e752e05aa0529eb5 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/pyro/ec.bin
1138EC version: pyro_v1.1.5900-ab1ee51
1139EC (RW) version: pyro_v1.1.5909-bd1f0c9
1140
1141Model: snappy
1142BIOS image: 3ab63ff080596bd7de4e7619f003bb64 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/image.bin
1143BIOS version: Google_Snappy.9042.110.0
1144EC image: c4db159e84428391d2ee25368c5fe5b6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/snappy/ec.bin
1145EC version: snappy_v1.1.5909-bd1f0c9
1146
1147Model: sand
1148BIOS image: 387da034a4f0a3f53e278ebfdcc2a412 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/image.bin
1149BIOS version: Google_Sand.9042.110.0
1150EC image: 411562e0589dacec131f5fdfbe95a561 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/sand/ec.bin
1151EC version: sand_v1.1.5909-bd1f0c9
1152
1153Model: electro
1154BIOS image: 1b535280fe688ac284d95276492b06f6 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/image.bin
1155BIOS version: Google_Reef.9042.87.1
1156BIOS (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
1157BIOS (RW) version: Google_Reef.9042.110.0
1158EC image: 2e8b4b5fa73cc5dbca4496de97a917a9 */build/reef/tmp/portage/chromeos-base/chromeos-firmware-reef-0.0.1-r79/temp/tmp7rHApL.pack_firmware-99001/models/reef/ec.bin
1159EC version: reef_v1.1.5900-ab1ee51
1160EC (RW) version: reef_v1.1.5909-bd1f0c9
1161
1162Package Content:
1163612e7bb6ed1fb0a05abf2ebdc834c18b *./updater4.sh
11640eafbee07282315829d0f42135ec7c0c *./gbb_utility
11656074e3ca424cb30a67c378c1d9681f9c *./mosys
116668935ee2fcfcffa47af81b966269cd2b *./flashrom
11670eafbee07282315829d0f42135ec7c0c *./dump_fmap
1168490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
116960899148600b8673ddb711faa55aee40 *./common.sh
11703c3a99346d1ca1273cbcd86c104851ff *./shflags
1171de7ce035e1f82a89f8909d888ee402c0 *./crosutil.sh
1172f9334372bdb9036ba09a6fd9bf30e7a2 *./crossystem
117322257a8d5f0adc1f50a1916c3a4a35dd *./models/reef/ec.bin
1174faf12dbb7cdaf21ce153bdffb67841fd *./models/reef/bios.bin
1175c9bbb417b7921b85a7ed999ee42f550e *./models/reef/setvars.sh
117629823d46f1ec1491ecacd7b830fd2686 *./models/pyro/ec.bin
11772320463aba8b22eb5ea836f094d281b3 *./models/pyro/bios.bin
117881614833ad77c9cd093360ba7bea76b8 *./models/pyro/setvars.sh
1179411562e0589dacec131f5fdfbe95a561 *./models/sand/ec.bin
1180387da034a4f0a3f53e278ebfdcc2a412 *./models/sand/bios.bin
1181fcd8cb0ac0e2ed6be220aaae435d43ff *./models/sand/setvars.sh
1182c4db159e84428391d2ee25368c5fe5b6 *./models/snappy/ec.bin
11833ab63ff080596bd7de4e7619f003bb64 *./models/snappy/bios.bin
1184fe5d699f2e9e4a7de031497953313dbd *./models/snappy/setvars.sh
118579aabd7cd8a215a54234c53d7bb2e6fb *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001186"""
1187 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001188 # pylint: enable=line-too-long
Michael Mortensen59e30872020-05-18 14:12:49 -06001189
Alex Klein1699fab2022-09-08 08:46:06 -06001190 def test_get_firmware_versions(self):
1191 """Tests get_firmware_versions with mocked output."""
1192 build_target = build_target_lib.BuildTarget(self.board)
1193 result = packages.get_all_firmware_versions(build_target)
1194 self.assertEqual(len(result), 5)
1195 self.assertEqual(
1196 result["reef"],
1197 packages.FirmwareVersions(
1198 "reef",
1199 "Google_Reef.9042.87.1",
1200 "Google_Reef.9042.110.0",
1201 "reef_v1.1.5900-ab1ee51",
1202 "reef_v1.1.5909-bd1f0c9",
1203 ),
1204 )
1205 self.assertEqual(
1206 result["pyro"],
1207 packages.FirmwareVersions(
1208 "pyro",
1209 "Google_Pyro.9042.87.1",
1210 "Google_Pyro.9042.110.0",
1211 "pyro_v1.1.5900-ab1ee51",
1212 "pyro_v1.1.5909-bd1f0c9",
1213 ),
1214 )
1215 self.assertEqual(
1216 result["snappy"],
1217 packages.FirmwareVersions(
1218 "snappy",
1219 "Google_Snappy.9042.110.0",
1220 None,
1221 "snappy_v1.1.5909-bd1f0c9",
1222 None,
1223 ),
1224 )
1225 self.assertEqual(
1226 result["sand"],
1227 packages.FirmwareVersions(
1228 "sand",
1229 "Google_Sand.9042.110.0",
1230 None,
1231 "sand_v1.1.5909-bd1f0c9",
1232 None,
1233 ),
1234 )
1235 self.assertEqual(
1236 result["electro"],
1237 packages.FirmwareVersions(
1238 "electro",
1239 "Google_Reef.9042.87.1",
1240 "Google_Reef.9042.110.0",
1241 "reef_v1.1.5900-ab1ee51",
1242 "reef_v1.1.5909-bd1f0c9",
1243 ),
1244 )
Michael Mortensen59e30872020-05-18 14:12:49 -06001245
Alex Klein1699fab2022-09-08 08:46:06 -06001246 def test_get_firmware_versions_error(self):
1247 """Tests get_firmware_versions with no output."""
1248 # Throw an exception when running the command.
1249 self.PatchObject(
1250 cros_build_lib,
1251 "run",
1252 side_effect=cros_build_lib.RunCommandError("error"),
1253 )
1254 build_target = build_target_lib.BuildTarget(self.board)
1255 result = packages.get_all_firmware_versions(build_target)
1256 self.assertEqual(result, {})
Benjamin Shai12c767e2022-01-12 15:17:44 +00001257
Michael Mortensen59e30872020-05-18 14:12:49 -06001258
Michael Mortensen71ef5682020-05-07 14:29:24 -06001259class GetFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001260 """Tests for get_firmware_versions."""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001261
Alex Klein1699fab2022-09-08 08:46:06 -06001262 def setUp(self):
1263 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001264 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001265 self.rc.SetDefaultCmdResult(
1266 stdout="""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001267
1268flashrom(8): a8f99c2e61e7dc09c4b25ef5a76ef692 */build/kevin/usr/sbin/flashrom
1269 ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.d
1270 0.9.4 : 860875a : Apr 10 2017 23:54:29 UTC
1271
1272BIOS image: 6b5b855a0b8fd1657546d1402c15b206 *chromeos-firmware-kevin-0.0.1/.dist/kevin_fw_8785.178.0.n
1273BIOS version: Google_Kevin.8785.178.0
1274EC image: 1ebfa9518e6cac0558a80b7ab2f5b489 *chromeos-firmware-kevin-0.0.1/.dist/kevin_ec_8785.178.0.n
1275EC version:kevin_v1.10.184-459421c
1276
1277Package Content:
1278a8f99c2e61e7dc09c4b25ef5a76ef692 *./flashrom
12793c3a99346d1ca1273cbcd86c104851ff *./shflags
1280457a8dc8546764affc9700f8da328d23 *./dump_fmap
1281c392980ddb542639edf44a965a59361a *./updater5.sh
1282490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
12836b5b855a0b8fd1657546d1402c15b206 *./bios.bin
12847b5bef0d2da90c23ff2e157250edf0fa *./crosutil.sh
1285d78722e4f1a0dc2d8c3d6b0bc7010ae3 *./crossystem
1286457a8dc8546764affc9700f8da328d23 *./gbb_utility
12871ebfa9518e6cac0558a80b7ab2f5b489 *./ec.bin
1288c98ca54db130886142ad582a58e90ddc *./common.sh
12895ba978bdec0f696f47f0f0de90936880 *./mosys
1290312e8ee6122057f2a246d7bcf1572f49 *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001291"""
1292 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001293 # pylint: enable=line-too-long
Michael Mortensen71ef5682020-05-07 14:29:24 -06001294
Alex Klein1699fab2022-09-08 08:46:06 -06001295 def test_get_firmware_versions(self):
1296 """Tests get_firmware_versions with mocked output."""
1297 build_target = build_target_lib.BuildTarget(self.board)
1298 result = packages.get_firmware_versions(build_target)
1299 versions = packages.FirmwareVersions(
1300 None,
1301 "Google_Kevin.8785.178.0",
1302 None,
1303 "kevin_v1.10.184-459421c",
1304 None,
1305 )
1306 self.assertEqual(result, versions)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001307
1308
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001309class DetermineKernelVersionTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001310 """Tests for determine_kernel_version."""
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001311
Alex Klein1699fab2022-09-08 08:46:06 -06001312 def setUp(self):
1313 self.board = "test-board"
1314 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001315
Alex Klein1699fab2022-09-08 08:46:06 -06001316 def test_determine_kernel_version(self):
1317 """Tests that a valid kernel version is returned."""
Lizzy Presland0b978e62022-09-09 16:55:29 +00001318 kernel_candidates = [
1319 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
Alex Klein1699fab2022-09-08 08:46:06 -06001320 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001321 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1322 "sys-kernel/upstream-kernel-next-9999",
1323 "sys-kernel/socfpga-kernel-4.20-r34",
Alex Klein1699fab2022-09-08 08:46:06 -06001324 ]
1325 self.PatchObject(
Lizzy Presland0b978e62022-09-09 16:55:29 +00001326 portage_util,
1327 "GetFlattenedDepsForPackage",
1328 return_value=kernel_candidates,
1329 )
1330
1331 installed_pkgs = [
1332 "sys-kernel/linux-firmware-0.0.1-r594",
1333 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1334 "virtual/linux-sources-1-r30",
1335 ]
1336 self.PatchObject(
1337 portage_util,
1338 "GetPackageDependencies",
1339 return_value=installed_pkgs,
Alex Klein1699fab2022-09-08 08:46:06 -06001340 )
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001341
Alex Klein1699fab2022-09-08 08:46:06 -06001342 result = packages.determine_kernel_version(self.build_target)
1343 self.assertEqual(result, "4.4.223-r2209")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001344
Lizzy Presland0b978e62022-09-09 16:55:29 +00001345 def test_determine_kernel_version_ignores_exact_duplicates(self):
1346 """Tests that multiple results for candidates is ignored."""
1347 # Depgraph is evaluated for version as well as revision, so graph will
1348 # return all results twice.
1349 kernel_candidates = [
1350 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1351 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1352 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1353 "sys-kernel/upstream-kernel-next-9999",
1354 "sys-kernel/socfpga-kernel-4.20-r34",
1355 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1356 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1357 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1358 "sys-kernel/upstream-kernel-next-9999",
1359 "sys-kernel/socfpga-kernel-4.20-r34",
1360 ]
1361 self.PatchObject(
1362 portage_util,
1363 "GetFlattenedDepsForPackage",
1364 return_value=kernel_candidates,
1365 )
1366
1367 installed_pkgs = [
1368 "sys-kernel/linux-firmware-0.0.1-r594",
1369 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1370 "virtual/linux-sources-1-r30",
1371 ]
Alex Klein1699fab2022-09-08 08:46:06 -06001372 self.PatchObject(
1373 portage_util,
1374 "GetPackageDependencies",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001375 return_value=installed_pkgs,
1376 )
1377
1378 result = packages.determine_kernel_version(self.build_target)
1379 self.assertEqual(result, "4.4.223-r2209")
1380
1381 def test_determine_kernel_version_ignores_virtual_package(self):
1382 """Tests that top-level package is ignored as potential kernel pkg."""
1383 # Depgraph results include the named package at level 0 as well as its
1384 # first-order dependencies, so verify that the virtual package is not
1385 # included as a kernel package.
1386 kernel_candidates = [
1387 "virtual/linux-sources-1",
1388 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1389 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1390 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1391 "sys-kernel/upstream-kernel-next-9999",
1392 "sys-kernel/socfpga-kernel-4.20-r34",
1393 "virtual/linux-sources-1-r30",
1394 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1395 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1396 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1397 "sys-kernel/upstream-kernel-next-9999",
1398 "sys-kernel/socfpga-kernel-4.20-r34",
1399 ]
1400 self.PatchObject(
1401 portage_util,
1402 "GetFlattenedDepsForPackage",
1403 return_value=kernel_candidates,
1404 )
1405
1406 installed_pkgs = [
1407 "sys-kernel/linux-firmware-0.0.1-r594",
1408 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1409 "virtual/linux-sources-1-r30",
1410 ]
1411 self.PatchObject(
1412 portage_util,
1413 "GetPackageDependencies",
1414 return_value=installed_pkgs,
1415 )
1416
1417 result = packages.determine_kernel_version(self.build_target)
1418 self.assertEqual(result, "4.4.223-r2209")
1419
1420 def test_determine_kernel_version_too_many(self):
1421 """Tests that an exception is thrown with too many matching packages."""
1422 package_result = [
1423 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1424 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1425 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1426 "sys-kernel/upstream-kernel-next-9999",
1427 "sys-kernel/socfpga-kernel-4.20-r34",
1428 ]
1429 self.PatchObject(
1430 portage_util,
1431 "GetFlattenedDepsForPackage",
1432 return_value=package_result,
1433 )
1434
1435 installed_pkgs = [
1436 "sys-kernel/linux-firmware-0.0.1-r594",
1437 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1438 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1439 "virtual/linux-sources-1-r30",
1440 ]
1441 self.PatchObject(
1442 portage_util,
1443 "GetPackageDependencies",
1444 return_value=installed_pkgs,
1445 )
1446
1447 with self.assertRaises(packages.KernelVersionError):
1448 packages.determine_kernel_version(self.build_target)
1449
1450 def test_determine_kernel_version_no_kernel_match(self):
1451 """Tests that an exception is thrown with 0-sized intersection."""
1452 package_result = [
1453 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1454 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1455 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1456 "sys-kernel/upstream-kernel-next-9999",
1457 ]
1458 self.PatchObject(
1459 portage_util,
1460 "GetFlattenedDepsForPackage",
1461 return_value=package_result,
1462 )
1463
1464 installed_pkgs = [
1465 "sys-kernel/linux-firmware-0.0.1-r594",
1466 "sys-kernel/socfpga-kernel-4.20-r34",
1467 "virtual/linux-sources-1-r30",
1468 ]
1469 self.PatchObject(
1470 portage_util,
1471 "GetPackageDependencies",
1472 return_value=installed_pkgs,
1473 )
1474
1475 with self.assertRaises(packages.KernelVersionError):
1476 packages.determine_kernel_version(self.build_target)
1477
1478 def test_determine_kernel_version_exception(self):
1479 """Tests that portage_util exceptions result in returning empty str."""
1480 self.PatchObject(
1481 portage_util,
1482 "GetFlattenedDepsForPackage",
Alex Klein1699fab2022-09-08 08:46:06 -06001483 side_effect=cros_build_lib.RunCommandError("error"),
1484 )
1485 result = packages.determine_kernel_version(self.build_target)
Lizzy Presland0b978e62022-09-09 16:55:29 +00001486 self.assertEqual(result, "")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001487
Alex Klein627e04c2021-11-10 15:56:47 -07001488
Michael Mortensenc2615b72019-10-15 08:12:24 -06001489class ChromeVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001490 """Tests getting chrome version."""
Michael Mortensen14960d02019-10-18 07:53:59 -06001491
Alex Klein1699fab2022-09-08 08:46:06 -06001492 def setUp(self):
1493 self.build_target = build_target_lib.BuildTarget("board")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001494
Alex Klein1699fab2022-09-08 08:46:06 -06001495 def test_determine_chrome_version(self):
1496 """Tests that a valid chrome version is returned."""
1497 # Mock PortageqBestVisible to return a valid chrome version string.
1498 r1_cpf = "chromeos-base/chromeos-chrome-78.0.3900.0_rc-r1"
1499 r1_cpv = package_info.SplitCPV(r1_cpf)
1500 self.PatchObject(
1501 portage_util, "PortageqBestVisible", return_value=r1_cpv
1502 )
Michael Mortensenc2615b72019-10-15 08:12:24 -06001503
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001504 chrome_version = packages.determine_package_version(
1505 constants.CHROME_CP, self.build_target
1506 )
Alex Klein1699fab2022-09-08 08:46:06 -06001507 version_numbers = chrome_version.split(".")
1508 self.assertEqual(len(version_numbers), 4)
1509 self.assertEqual(int(version_numbers[0]), 78)
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001510
Alex Klein1699fab2022-09-08 08:46:06 -06001511 def test_determine_chrome_version_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001512 # Mock what happens when portage throws an exception that bubbles up
1513 # (via RunCommand)inside portage_util.PortageqBestVisible.
Alex Klein1699fab2022-09-08 08:46:06 -06001514 self.PatchObject(
1515 portage_util,
1516 "PortageqBestVisible",
1517 side_effect=cros_build_lib.RunCommandError("error"),
1518 )
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001519 target = packages.determine_package_version(
1520 constants.CHROME_CP, self.build_target
1521 )
Alex Klein1699fab2022-09-08 08:46:06 -06001522 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001523
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001524
1525class PlatformVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001526 """Tests getting platform version."""
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001527
Alex Klein1699fab2022-09-08 08:46:06 -06001528 def test_determine_platform_version(self):
1529 """Test checking that a valid platform version is returned."""
1530 platform_version = packages.determine_platform_version()
1531 # The returned platform version is something like 12603.0.0.
1532 version_string_list = platform_version.split(".")
1533 self.assertEqual(len(version_string_list), 3)
Alex Kleinfee86da2023-01-20 18:40:06 -07001534 # We don't want to check an exact version, but the first number should
1535 # be non-zero.
Alex Klein1699fab2022-09-08 08:46:06 -06001536 self.assertGreaterEqual(int(version_string_list[0]), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001537
Alex Klein1699fab2022-09-08 08:46:06 -06001538 def test_determine_milestone_version(self):
1539 """Test checking that a valid milestone version is returned."""
1540 milestone_version = packages.determine_milestone_version()
1541 # Milestone version should be non-zero
1542 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001543
Alex Klein1699fab2022-09-08 08:46:06 -06001544 def test_determine_full_version(self):
1545 """Test checking that a valid full version is returned."""
1546 full_version = packages.determine_full_version()
1547 pattern = r"^R(\d+)-(\d+.\d+.\d+(-rc\d+)*)"
1548 m = re.match(pattern, full_version)
1549 self.assertTrue(m)
1550 milestone_version = m.group(1)
1551 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001552
Alex Klein1699fab2022-09-08 08:46:06 -06001553 def test_versions_based_on_mock(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001554 # Create a test version_info object, and then mock VersionInfo.from_repo
Alex Klein1699fab2022-09-08 08:46:06 -06001555 # return it.
1556 test_platform_version = "12575.0.0"
1557 test_chrome_branch = "75"
1558 version_info_mock = chromeos_version.VersionInfo(test_platform_version)
1559 version_info_mock.chrome_branch = test_chrome_branch
1560 self.PatchObject(
1561 chromeos_version.VersionInfo,
1562 "from_repo",
1563 return_value=version_info_mock,
1564 )
1565 test_full_version = (
1566 "R" + test_chrome_branch + "-" + test_platform_version
1567 )
1568 platform_version = packages.determine_platform_version()
1569 milestone_version = packages.determine_milestone_version()
1570 full_version = packages.determine_full_version()
1571 self.assertEqual(platform_version, test_platform_version)
1572 self.assertEqual(milestone_version, test_chrome_branch)
1573 self.assertEqual(full_version, test_full_version)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001574
1575
1576# Each of the columns in the following table is a separate dimension along
1577# which Chrome uprev test cases can vary in behavior. The full test space would
1578# be the Cartesian product of the possible values of each column.
1579# 'CHROME_EBUILD' refers to the relationship between the version of the existing
1580# Chrome ebuild vs. the requested uprev version. 'FOLLOWER_EBUILDS' refers to
1581# the same relationship but for the packages defined in OTHER_CHROME_PACKAGES.
1582# 'EBUILDS MODIFIED' refers to whether any of the existing 9999 ebuilds have
1583# modified contents relative to their corresponding stable ebuilds.
1584#
1585# CHROME_EBUILD FOLLOWER_EBUILDS EBUILDS_MODIFIED
1586#
1587# HIGHER HIGHER YES
1588# SAME SAME NO
1589# LOWER LOWER
1590# DOESN'T EXIST YET
1591
1592# These test cases cover both CHROME & FOLLOWER ebuilds being identically
1593# higher, lower, or the same versions, with no modified ebuilds.
1594UPREV_VERSION_CASES = (
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001595 # Uprev.
Chris McDonaldea0312c2020-05-04 23:33:15 -06001596 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001597 "80.0.8080.0",
1598 "81.0.8181.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001599 # One added and one deleted for chrome and each "other" package.
1600 2 * (1 + len(constants.OTHER_CHROME_PACKAGES)),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001601 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001602 id="newer_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001603 ),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001604 # Revbump.
1605 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001606 "80.0.8080.0",
1607 "80.0.8080.0",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001608 2,
1609 True,
Alex Klein1699fab2022-09-08 08:46:06 -06001610 id="chrome_revbump",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001611 ),
Chris McDonaldea0312c2020-05-04 23:33:15 -06001612 # No files should be changed in these cases.
1613 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001614 "80.0.8080.0",
1615 "80.0.8080.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001616 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001617 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001618 id="same_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001619 ),
1620 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001621 "80.0.8080.0",
1622 "79.0.7979.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001623 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001624 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001625 id="older_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001626 ),
1627)
1628
1629
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001630@pytest.mark.parametrize(
Alex Klein1699fab2022-09-08 08:46:06 -06001631 "old_version, new_version, expected_count, modify_unstable",
1632 UPREV_VERSION_CASES,
1633)
1634def test_uprev_chrome_all_files_already_exist(
1635 old_version,
1636 new_version,
1637 expected_count,
1638 modify_unstable,
1639 monkeypatch,
1640 overlay_stack,
1641):
1642 """Test Chrome uprevs work as expected when all packages already exist."""
1643 (overlay,) = overlay_stack(1)
1644 monkeypatch.setattr(uprev_lib, "_CHROME_OVERLAY_PATH", overlay.path)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001645
Alex Klein1699fab2022-09-08 08:46:06 -06001646 unstable_chrome = cr.test.Package(
1647 "chromeos-base", "chromeos-chrome", version="9999", keywords="~*"
1648 )
1649 if modify_unstable:
1650 # Add some field not set in stable.
1651 unstable_chrome.depend = "foo/bar"
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001652
Alex Klein1699fab2022-09-08 08:46:06 -06001653 stable_chrome = cr.test.Package(
1654 "chromeos-base", "chromeos-chrome", version=f"{old_version}_rc-r1"
1655 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001656
Alex Klein1699fab2022-09-08 08:46:06 -06001657 overlay.add_package(unstable_chrome)
1658 overlay.add_package(stable_chrome)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001659
Alex Klein1699fab2022-09-08 08:46:06 -06001660 for pkg_str in constants.OTHER_CHROME_PACKAGES:
1661 category, pkg_name = pkg_str.split("/")
1662 unstable_pkg = cr.test.Package(
1663 category, pkg_name, version="9999", keywords="~*"
1664 )
1665 stable_pkg = cr.test.Package(
1666 category, pkg_name, version=f"{old_version}_rc-r1"
1667 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001668
Alex Klein1699fab2022-09-08 08:46:06 -06001669 overlay.add_package(unstable_pkg)
1670 overlay.add_package(stable_pkg)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001671
Alex Klein1699fab2022-09-08 08:46:06 -06001672 git_refs = [
1673 GitRef(
1674 path="/foo", ref=f"refs/tags/{new_version}", revision="stubcommit"
1675 )
1676 ]
1677 res = packages.uprev_chrome_from_ref(None, git_refs, None)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001678
Alex Klein1699fab2022-09-08 08:46:06 -06001679 modified_file_count = sum(len(m.files) for m in res.modified)
1680 assert modified_file_count == expected_count
Michael Mortensen125bb012020-05-21 14:02:10 -06001681
1682
Alex Klein1699fab2022-09-08 08:46:06 -06001683@pytest.mark.usefixtures("testcase_monkeypatch")
Michael Mortensen125bb012020-05-21 14:02:10 -06001684class GetModelsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001685 """Tests for get_models."""
Michael Mortensen125bb012020-05-21 14:02:10 -06001686
Alex Klein1699fab2022-09-08 08:46:06 -06001687 def setUp(self):
1688 self.board = "test-board"
1689 self.rc.SetDefaultCmdResult(stdout="pyro\nreef\nsnappy\n")
1690 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1691 build_bin = os.path.join(
1692 self.tempdir, constants.DEFAULT_CHROOT_DIR, "usr", "bin"
1693 )
1694 osutils.Touch(
1695 os.path.join(build_bin, "cros_config_host"), makedirs=True
1696 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001697
Alex Klein1699fab2022-09-08 08:46:06 -06001698 def testGetModels(self):
1699 """Test get_models."""
1700 build_target = build_target_lib.BuildTarget(self.board)
1701 result = packages.get_models(build_target)
1702 self.assertEqual(result, ["pyro", "reef", "snappy"])
Michael Mortensen359c1f32020-05-28 19:35:42 -06001703
1704
1705class GetKeyIdTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001706 """Tests for get_key_id."""
Michael Mortensen359c1f32020-05-28 19:35:42 -06001707
Alex Klein1699fab2022-09-08 08:46:06 -06001708 def setUp(self):
1709 self.board = "test-board"
1710 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensen359c1f32020-05-28 19:35:42 -06001711
Alex Klein1699fab2022-09-08 08:46:06 -06001712 def testGetKeyId(self):
1713 """Test get_key_id when _run_cros_config_host returns a key."""
1714 self.PatchObject(
1715 packages, "_run_cros_config_host", return_value=["key"]
1716 )
1717 result = packages.get_key_id(self.build_target, "model")
1718 self.assertEqual(result, "key")
Michael Mortensen359c1f32020-05-28 19:35:42 -06001719
Alex Klein1699fab2022-09-08 08:46:06 -06001720 def testGetKeyIdNoKey(self):
1721 """Test get_key_id when None should be returned."""
1722 self.PatchObject(
1723 packages, "_run_cros_config_host", return_value=["key1", "key2"]
1724 )
1725 result = packages.get_key_id(self.build_target, "model")
1726 self.assertEqual(result, None)
Ben Reiche779cf42020-12-15 03:21:31 +00001727
1728
Harvey Yang3eee06c2021-03-18 15:47:56 +08001729class GetLatestVersionTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001730 """Tests for get_latest_version_from_refs."""
Ben Reiche779cf42020-12-15 03:21:31 +00001731
Alex Klein1699fab2022-09-08 08:46:06 -06001732 def setUp(self):
1733 self.prefix = "refs/tags/drivefs_"
1734 # The tag ref template.
1735 ref_tpl = self.prefix + "%s"
Ben Reiche779cf42020-12-15 03:21:31 +00001736
Alex Klein1699fab2022-09-08 08:46:06 -06001737 self.latest = "44.0.20"
1738 self.versions = ["42.0.1", self.latest, "44.0.19", "39.0.15"]
1739 self.latest_ref = uprev_lib.GitRef(
1740 "/path", ref_tpl % self.latest, "abc123"
1741 )
1742 self.refs = [
1743 uprev_lib.GitRef("/path", ref_tpl % v, "abc123")
1744 for v in self.versions
1745 ]
Ben Reiche779cf42020-12-15 03:21:31 +00001746
Alex Klein1699fab2022-09-08 08:46:06 -06001747 def test_single_ref(self):
1748 """Test a single ref is supplied."""
1749 # pylint: disable=protected-access
1750 self.assertEqual(
1751 self.latest,
1752 packages._get_latest_version_from_refs(
1753 self.prefix, [self.latest_ref]
1754 ),
1755 )
Ben Reiche779cf42020-12-15 03:21:31 +00001756
Alex Klein1699fab2022-09-08 08:46:06 -06001757 def test_multiple_ref_versions(self):
1758 """Test multiple refs supplied."""
1759 # pylint: disable=protected-access
1760 self.assertEqual(
1761 self.latest,
1762 packages._get_latest_version_from_refs(self.prefix, self.refs),
1763 )
Ben Reiche779cf42020-12-15 03:21:31 +00001764
Alex Klein1699fab2022-09-08 08:46:06 -06001765 def test_no_refs_returns_none(self):
1766 """Test no refs supplied."""
1767 # pylint: disable=protected-access
1768 self.assertEqual(
1769 packages._get_latest_version_from_refs(self.prefix, []), None
1770 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001771
Chinglin Yu84818732022-10-03 12:03:43 +08001772 def test_ref_prefix(self):
1773 """Test refs with a different prefix isn't used"""
1774 # pylint: disable=protected-access
1775 # Add refs/tags/foo_100.0.0 to the refs, which should be ignored in
1776 # _get_latest_version_from_refs because the prefix doesn't match, even
1777 # if its version number is larger.
1778 refs = self.refs + [
1779 uprev_lib.GitRef("/path", "refs/tags/foo_100.0.0", "abc123")
1780 ]
1781 self.assertEqual(
1782 self.latest,
1783 packages._get_latest_version_from_refs(self.prefix, refs),
1784 )
1785
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001786
Alex Klein6becabc2020-09-11 14:03:05 -06001787class NeedsChromeSourceTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001788 """Tests for needs_chrome_source."""
Alex Klein6becabc2020-09-11 14:03:05 -06001789
Alex Klein1699fab2022-09-08 08:46:06 -06001790 def _build_graph(self, with_chrome: bool, with_followers: bool):
1791 root = "/build/build_target"
1792 foo_bar = package_info.parse("foo/bar-1")
1793 chrome = package_info.parse(f"{constants.CHROME_CP}-1.2.3.4")
1794 followers = [
1795 package_info.parse(f"{pkg}-1.2.3.4")
1796 for pkg in constants.OTHER_CHROME_PACKAGES
1797 ]
1798 nodes = [dependency_graph.PackageNode(foo_bar, root)]
1799 root_pkgs = ["foo/bar-1"]
1800 if with_chrome:
1801 nodes.append(dependency_graph.PackageNode(chrome, root))
1802 root_pkgs.append(chrome.cpvr)
1803 if with_followers:
1804 nodes.extend(
1805 [dependency_graph.PackageNode(f, root) for f in followers]
1806 )
1807 root_pkgs.extend([f.cpvr for f in followers])
Alex Klein6becabc2020-09-11 14:03:05 -06001808
Alex Klein1699fab2022-09-08 08:46:06 -06001809 return dependency_graph.DependencyGraph(nodes, root, root_pkgs)
Alex Klein6becabc2020-09-11 14:03:05 -06001810
Alex Klein1699fab2022-09-08 08:46:06 -06001811 def test_needs_all(self):
1812 """Verify we need source when we have no prebuilts."""
1813 graph = self._build_graph(with_chrome=True, with_followers=True)
1814 self.PatchObject(
1815 depgraph, "get_sysroot_dependency_graph", return_value=graph
1816 )
1817 self.PatchObject(packages, "has_prebuilt", return_value=False)
1818 self.PatchObject(
1819 packages,
1820 "uprev_chrome",
1821 return_value=uprev_lib.UprevVersionedPackageResult(),
1822 )
Alex Klein6becabc2020-09-11 14:03:05 -06001823
Alex Klein1699fab2022-09-08 08:46:06 -06001824 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001825
Alex Klein1699fab2022-09-08 08:46:06 -06001826 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001827
Alex Klein1699fab2022-09-08 08:46:06 -06001828 self.assertTrue(result.needs_chrome_source)
1829 self.assertTrue(result.builds_chrome)
1830 self.assertTrue(result.packages)
1831 self.assertEqual(
1832 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1833 )
1834 self.assertTrue(result.missing_chrome_prebuilt)
1835 self.assertTrue(result.missing_follower_prebuilt)
1836 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001837
Alex Klein1699fab2022-09-08 08:46:06 -06001838 def test_needs_none(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001839 """Verify not building any chrome packages prevents needing it."""
Alex Klein1699fab2022-09-08 08:46:06 -06001840 graph = self._build_graph(with_chrome=False, with_followers=False)
1841 self.PatchObject(
1842 depgraph, "get_sysroot_dependency_graph", return_value=graph
1843 )
1844 self.PatchObject(packages, "has_prebuilt", return_value=False)
1845 self.PatchObject(
1846 packages,
1847 "uprev_chrome",
1848 return_value=uprev_lib.UprevVersionedPackageResult(),
1849 )
Alex Klein6becabc2020-09-11 14:03:05 -06001850
Alex Klein1699fab2022-09-08 08:46:06 -06001851 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001852
Alex Klein1699fab2022-09-08 08:46:06 -06001853 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001854
Alex Klein1699fab2022-09-08 08:46:06 -06001855 self.assertFalse(result.needs_chrome_source)
1856 self.assertFalse(result.builds_chrome)
1857 self.assertFalse(result.packages)
1858 self.assertFalse(result.missing_chrome_prebuilt)
1859 self.assertFalse(result.missing_follower_prebuilt)
1860 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001861
Alex Klein1699fab2022-09-08 08:46:06 -06001862 def test_needs_chrome_only(self):
1863 """Verify only chrome triggers needs chrome source."""
1864 graph = self._build_graph(with_chrome=True, with_followers=False)
1865 self.PatchObject(
1866 depgraph, "get_sysroot_dependency_graph", return_value=graph
1867 )
1868 self.PatchObject(packages, "has_prebuilt", return_value=False)
1869 self.PatchObject(
1870 packages,
1871 "uprev_chrome",
1872 return_value=uprev_lib.UprevVersionedPackageResult(),
1873 )
Alex Klein6becabc2020-09-11 14:03:05 -06001874
Alex Klein1699fab2022-09-08 08:46:06 -06001875 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001876
Alex Klein1699fab2022-09-08 08:46:06 -06001877 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001878
Alex Klein1699fab2022-09-08 08:46:06 -06001879 self.assertTrue(result.needs_chrome_source)
1880 self.assertTrue(result.builds_chrome)
1881 self.assertTrue(result.packages)
1882 self.assertEqual(
1883 set([p.atom for p in result.packages]), {constants.CHROME_CP}
1884 )
1885 self.assertTrue(result.missing_chrome_prebuilt)
1886 self.assertFalse(result.missing_follower_prebuilt)
1887 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001888
Alex Klein1699fab2022-09-08 08:46:06 -06001889 def test_needs_followers_only(self):
1890 """Verify only chrome followers triggers needs chrome source."""
1891 graph = self._build_graph(with_chrome=False, with_followers=True)
1892 self.PatchObject(
1893 depgraph, "get_sysroot_dependency_graph", return_value=graph
1894 )
1895 self.PatchObject(packages, "has_prebuilt", return_value=False)
1896 self.PatchObject(
1897 packages,
1898 "uprev_chrome",
1899 return_value=uprev_lib.UprevVersionedPackageResult(),
1900 )
Alex Klein6becabc2020-09-11 14:03:05 -06001901
Alex Klein1699fab2022-09-08 08:46:06 -06001902 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001903
Alex Klein1699fab2022-09-08 08:46:06 -06001904 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001905
Alex Klein1699fab2022-09-08 08:46:06 -06001906 self.assertTrue(result.needs_chrome_source)
1907 self.assertFalse(result.builds_chrome)
1908 self.assertTrue(result.packages)
1909 self.assertEqual(
1910 set([p.atom for p in result.packages]),
1911 set(constants.OTHER_CHROME_PACKAGES),
1912 )
1913 self.assertFalse(result.missing_chrome_prebuilt)
1914 self.assertTrue(result.missing_follower_prebuilt)
1915 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001916
Alex Klein1699fab2022-09-08 08:46:06 -06001917 def test_has_prebuilts(self):
1918 """Test prebuilts prevent us from needing chrome source."""
1919 graph = self._build_graph(with_chrome=True, with_followers=True)
1920 self.PatchObject(
1921 depgraph, "get_sysroot_dependency_graph", return_value=graph
1922 )
1923 self.PatchObject(packages, "has_prebuilt", return_value=True)
1924 self.PatchObject(
1925 packages,
1926 "uprev_chrome",
1927 return_value=uprev_lib.UprevVersionedPackageResult(),
1928 )
Alex Klein6becabc2020-09-11 14:03:05 -06001929
Alex Klein1699fab2022-09-08 08:46:06 -06001930 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001931
Alex Klein1699fab2022-09-08 08:46:06 -06001932 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001933
Alex Klein1699fab2022-09-08 08:46:06 -06001934 self.assertFalse(result.needs_chrome_source)
1935 self.assertTrue(result.builds_chrome)
1936 self.assertFalse(result.packages)
1937 self.assertFalse(result.missing_chrome_prebuilt)
1938 self.assertFalse(result.missing_follower_prebuilt)
1939 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001940
Alex Klein1699fab2022-09-08 08:46:06 -06001941 def test_compile_source(self):
1942 """Test compile source ignores prebuilts."""
1943 graph = self._build_graph(with_chrome=True, with_followers=True)
1944 self.PatchObject(
1945 depgraph, "get_sysroot_dependency_graph", return_value=graph
1946 )
1947 self.PatchObject(packages, "has_prebuilt", return_value=True)
1948 self.PatchObject(
1949 packages,
1950 "uprev_chrome",
1951 return_value=uprev_lib.UprevVersionedPackageResult(),
1952 )
Alex Klein6becabc2020-09-11 14:03:05 -06001953
Alex Klein1699fab2022-09-08 08:46:06 -06001954 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001955
Alex Klein1699fab2022-09-08 08:46:06 -06001956 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Klein6becabc2020-09-11 14:03:05 -06001957
Alex Klein1699fab2022-09-08 08:46:06 -06001958 self.assertTrue(result.needs_chrome_source)
1959 self.assertTrue(result.builds_chrome)
1960 self.assertTrue(result.packages)
1961 self.assertEqual(
1962 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1963 )
1964 self.assertTrue(result.missing_chrome_prebuilt)
1965 self.assertTrue(result.missing_follower_prebuilt)
1966 self.assertFalse(result.local_uprev)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001967
Alex Klein1699fab2022-09-08 08:46:06 -06001968 def test_local_uprev(self):
1969 """Test compile source ignores prebuilts."""
1970 graph = self._build_graph(with_chrome=True, with_followers=True)
1971 self.PatchObject(
1972 depgraph, "get_sysroot_dependency_graph", return_value=graph
1973 )
1974 self.PatchObject(packages, "has_prebuilt", return_value=False)
Alex Klein75110572021-07-14 10:44:39 -06001975
Alex Klein1699fab2022-09-08 08:46:06 -06001976 uprev_result = uprev_lib.UprevVersionedPackageResult()
1977 uprev_result.add_result("1.2.3.4", ["/tmp/foo"])
1978 self.PatchObject(packages, "uprev_chrome", return_value=uprev_result)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001979
Alex Klein1699fab2022-09-08 08:46:06 -06001980 build_target = build_target_lib.BuildTarget("build_target")
Alex Kleinde7b76d2021-07-12 12:28:44 -06001981
Alex Klein1699fab2022-09-08 08:46:06 -06001982 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001983
Alex Klein1699fab2022-09-08 08:46:06 -06001984 self.assertTrue(result.needs_chrome_source)
1985 self.assertTrue(result.builds_chrome)
1986 self.assertTrue(result.packages)
1987 self.assertEqual(
1988 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1989 )
1990 self.assertTrue(result.missing_chrome_prebuilt)
1991 self.assertTrue(result.missing_follower_prebuilt)
1992 self.assertTrue(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001993
1994
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001995class UprevDrivefsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001996 """Tests for uprev_drivefs."""
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001997
Alex Klein1699fab2022-09-08 08:46:06 -06001998 def setUp(self):
1999 self.refs = [
2000 GitRef(
2001 path="/chromeos/platform/drivefs-google3/",
2002 ref="refs/tags/drivefs_45.0.2",
2003 revision="123",
2004 )
2005 ]
2006 self.MOCK_DRIVEFS_EBUILD_PATH = "drivefs.45.0.2-r1.ebuild"
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002007
Alex Klein1699fab2022-09-08 08:46:06 -06002008 def revisionBumpOutcome(self, ebuild_path):
2009 return uprev_lib.UprevResult(
2010 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2011 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002012
Alex Klein1699fab2022-09-08 08:46:06 -06002013 def majorBumpOutcome(self, ebuild_path):
2014 return uprev_lib.UprevResult(
2015 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2016 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002017
Alex Klein1699fab2022-09-08 08:46:06 -06002018 def sameVersionOutcome(self):
2019 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002020
Alex Klein1699fab2022-09-08 08:46:06 -06002021 def test_latest_version_returns_none(self):
2022 """Test no refs were supplied"""
2023 output = packages.uprev_drivefs(None, [], None)
2024 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002025
Alex Klein1699fab2022-09-08 08:46:06 -06002026 def test_drivefs_uprev_fails(self):
2027 """Test a single ref is supplied."""
2028 self.PatchObject(
2029 uprev_lib,
2030 "uprev_workon_ebuild_to_version",
2031 side_effect=[None, None],
2032 )
2033 output = packages.uprev_drivefs(None, self.refs, None)
2034 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002035
Alex Klein1699fab2022-09-08 08:46:06 -06002036 def test_same_version_exists(self):
2037 """Test the same version exists uprev should not happen."""
2038 drivefs_outcome = self.sameVersionOutcome()
2039 self.PatchObject(
2040 uprev_lib,
2041 "uprev_workon_ebuild_to_version",
2042 side_effect=[drivefs_outcome],
2043 )
2044 output = packages.uprev_drivefs(None, self.refs, None)
2045 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002046
Alex Klein1699fab2022-09-08 08:46:06 -06002047 def test_revision_bump_both_packages(self):
2048 """Test both packages uprev, should succeed."""
2049 drivefs_outcome = self.revisionBumpOutcome(
2050 self.MOCK_DRIVEFS_EBUILD_PATH
2051 )
2052 self.PatchObject(
2053 uprev_lib,
2054 "uprev_workon_ebuild_to_version",
2055 side_effect=[drivefs_outcome],
2056 )
2057 output = packages.uprev_drivefs(None, self.refs, None)
2058 self.assertTrue(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002059
Alex Klein1699fab2022-09-08 08:46:06 -06002060 def test_major_bump_both_packages(self):
2061 """Test both packages uprev, should succeed."""
2062 drivefs_outcome = self.majorBumpOutcome(self.MOCK_DRIVEFS_EBUILD_PATH)
2063 self.PatchObject(
2064 uprev_lib,
2065 "uprev_workon_ebuild_to_version",
2066 side_effect=[drivefs_outcome],
2067 )
2068 output = packages.uprev_drivefs(None, self.refs, None)
2069 self.assertTrue(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002070
2071
Denis Nikitin63613e32022-09-09 22:26:50 -07002072class UprevKernelAfdo(cros_test_lib.RunCommandTempDirTestCase):
2073 """Tests for uprev_kernel_afdo."""
2074
2075 def setUp(self):
2076 # patch_ebuild_vars is tested separately.
2077 self.mock_patch = self.PatchObject(packages, "patch_ebuild_vars")
Denis Nikitin88ad5132022-09-28 12:10:01 -07002078 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Denis Nikitin63613e32022-09-09 22:26:50 -07002079 self.metadata_dir = os.path.join(
Denis Nikitin63613e32022-09-09 22:26:50 -07002080 "src",
2081 "third_party",
2082 "toolchain-utils",
2083 "afdo_metadata",
2084 )
Denis Nikitin88ad5132022-09-28 12:10:01 -07002085 osutils.SafeMakedirs(os.path.join(self.tempdir, self.metadata_dir))
Denis Nikitin63613e32022-09-09 22:26:50 -07002086
2087 def test_uprev_kernel_afdo_version(self):
2088 """Test kernel afdo version uprev."""
2089 json_files = {
2090 "kernel_afdo.json": (
2091 "{\n"
2092 ' "chromeos-kernel-5_4": {\n'
2093 ' "name": "R106-12345.0-0123456789"\n'
2094 " }\n"
2095 "}"
2096 ),
2097 "kernel_arm_afdo.json": (
2098 "{\n"
2099 ' "chromeos-kernel-5_15": {\n'
2100 ' "name": "R107-67890.0-0123456789"\n'
2101 " }\n"
2102 "}"
2103 ),
2104 }
2105 for f, contents in json_files.items():
2106 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2107
2108 returned_output = packages.uprev_kernel_afdo()
2109
Denis Nikitin88ad5132022-09-28 12:10:01 -07002110 package_root = os.path.join(
2111 constants.SOURCE_ROOT,
2112 constants.CHROMIUMOS_OVERLAY_DIR,
2113 "sys-kernel",
Denis Nikitin63613e32022-09-09 22:26:50 -07002114 )
2115 expect_result = [
2116 uprev_lib.UprevVersionedPackageModifications(
2117 new_version="R106-12345.0-0123456789",
2118 files=[
2119 os.path.join(
2120 package_root,
2121 "chromeos-kernel-5_4",
2122 "chromeos-kernel-5_4-9999.ebuild",
2123 ),
2124 os.path.join(
2125 package_root, "chromeos-kernel-5_4", "Manifest"
2126 ),
2127 ],
2128 ),
2129 uprev_lib.UprevVersionedPackageModifications(
2130 new_version="R107-67890.0-0123456789",
2131 files=[
2132 os.path.join(
2133 package_root,
2134 "chromeos-kernel-5_15",
2135 "chromeos-kernel-5_15-9999.ebuild",
2136 ),
2137 os.path.join(
2138 package_root, "chromeos-kernel-5_15", "Manifest"
2139 ),
2140 ],
2141 ),
2142 ]
2143 self.assertTrue(returned_output.uprevved)
2144 self.assertEqual(returned_output.modified, expect_result)
2145
2146 def test_uprev_kernel_afdo_empty_json(self):
2147 """Test kernel afdo version unchanged."""
2148 json_files = {
2149 "kernel_afdo.json": "{}",
2150 "kernel_arm_afdo.json": "{}",
2151 }
2152 for f, contents in json_files.items():
2153 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2154
2155 returned_output = packages.uprev_kernel_afdo()
2156 self.assertFalse(returned_output.uprevved)
2157
2158 def test_uprev_kernel_afdo_empty_file(self):
2159 """Test malformed json raises."""
2160 json_files = {
2161 "kernel_afdo.json": "",
2162 "kernel_arm_afdo.json": "",
2163 }
2164 for f, contents in json_files.items():
2165 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2166
2167 with self.assertRaisesRegex(
2168 json.decoder.JSONDecodeError, "Expecting value"
2169 ):
2170 packages.uprev_kernel_afdo()
2171
2172 def test_uprev_kernel_afdo_manifest_raises(self):
2173 """Test manifest update raises."""
2174 json_files = {
2175 "kernel_afdo.json": (
2176 "{\n"
2177 ' "chromeos-kernel-5_4": {\n'
2178 ' "name": "R106-12345.0-0123456789"\n'
2179 " }\n"
2180 "}"
2181 ),
2182 }
2183 for f, contents in json_files.items():
2184 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2185 # run() raises exception.
2186 self.rc.SetDefaultCmdResult(
2187 side_effect=cros_build_lib.RunCommandError("error")
2188 )
2189
2190 with self.assertRaises(uprev_lib.EbuildManifestError):
2191 packages.uprev_kernel_afdo()
2192
2193
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002194# TODO(chenghaoyang): Shouldn't use uprev_workon_ebuild_to_version.
2195class UprevPerfettoTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002196 """Tests for uprev_perfetto."""
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002197
Alex Klein1699fab2022-09-08 08:46:06 -06002198 def setUp(self):
2199 self.refs = [GitRef(path="/foo", ref="refs/tags/v12.0", revision="123")]
2200 self.MOCK_PERFETTO_EBUILD_PATH = "perfetto-12.0-r1.ebuild"
Chinglin Yufa728552023-04-13 03:12:04 +00002201 self.MOCK_PERFETTO_PROTO_EBUILD_PATH = "perfetto-protos-12.0-r1.ebuild"
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002202
Chinglin Yufa728552023-04-13 03:12:04 +00002203 def revisionBumpOutcome(self):
2204 return [
2205 uprev_lib.UprevResult(
2206 uprev_lib.Outcome.REVISION_BUMP,
2207 [self.MOCK_PERFETTO_EBUILD_PATH],
2208 ),
2209 uprev_lib.UprevResult(
2210 uprev_lib.Outcome.REVISION_BUMP,
2211 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2212 ),
2213 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002214
Chinglin Yufa728552023-04-13 03:12:04 +00002215 def majorBumpOutcome(self):
2216 return [
2217 uprev_lib.UprevResult(
2218 uprev_lib.Outcome.VERSION_BUMP, [self.MOCK_PERFETTO_EBUILD_PATH]
2219 ),
2220 uprev_lib.UprevResult(
2221 uprev_lib.Outcome.VERSION_BUMP,
2222 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2223 ),
2224 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002225
Alex Klein1699fab2022-09-08 08:46:06 -06002226 def newerVersionOutcome(self):
2227 return uprev_lib.UprevResult(uprev_lib.Outcome.NEWER_VERSION_EXISTS)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002228
Alex Klein1699fab2022-09-08 08:46:06 -06002229 def sameVersionOutcome(self):
2230 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002231
Alex Klein1699fab2022-09-08 08:46:06 -06002232 def test_latest_version_returns_none(self):
2233 """Test no refs were supplied"""
2234 output = packages.uprev_perfetto(None, [], None)
2235 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002236
Alex Klein1699fab2022-09-08 08:46:06 -06002237 def test_perfetto_uprev_fails(self):
2238 """Test a single ref is supplied."""
2239 self.PatchObject(
2240 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2241 )
2242 output = packages.uprev_perfetto(None, self.refs, None)
2243 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002244
Alex Klein1699fab2022-09-08 08:46:06 -06002245 def test_newer_version_exists(self):
2246 """Test the newer version exists uprev should not happen."""
2247 perfetto_outcome = self.newerVersionOutcome()
2248 self.PatchObject(
2249 uprev_lib,
2250 "uprev_workon_ebuild_to_version",
2251 side_effect=[perfetto_outcome],
2252 )
2253 output = packages.uprev_perfetto(None, self.refs, None)
2254 self.assertFalse(output.uprevved)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002255
Alex Klein1699fab2022-09-08 08:46:06 -06002256 def test_same_version_exists(self):
2257 """Test the same version exists uprev should not happen."""
2258 perfetto_outcome = self.sameVersionOutcome()
2259 self.PatchObject(
2260 uprev_lib,
2261 "uprev_workon_ebuild_to_version",
2262 side_effect=[perfetto_outcome],
2263 )
2264 output = packages.uprev_perfetto(None, self.refs, None)
2265 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002266
Alex Klein1699fab2022-09-08 08:46:06 -06002267 def test_revision_bump_perfetto_package(self):
2268 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002269 self.PatchObject(
2270 uprev_lib,
2271 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002272 side_effect=self.revisionBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002273 )
2274 output = packages.uprev_perfetto(None, self.refs, None)
2275 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002276 self.assertEqual(
2277 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2278 )
2279 self.assertEqual(
2280 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2281 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002282
Alex Klein1699fab2022-09-08 08:46:06 -06002283 def test_major_bump_perfetto_package(self):
2284 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002285 self.PatchObject(
2286 uprev_lib,
2287 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002288 side_effect=self.majorBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002289 )
2290 output = packages.uprev_perfetto(None, self.refs, None)
2291 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002292 self.assertEqual(
2293 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2294 )
2295 self.assertEqual(
2296 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2297 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002298
Chinglin Yuad12a512022-10-07 17:26:12 +08002299 def test_revision_bump_trunk(self):
2300 """Test revision bump on receiving non-versioned trunk refs."""
Chinglin Yu5de28a42022-11-11 19:52:21 +08002301 refs = [
2302 GitRef(
2303 path="/foo", ref="refs/heads/main", revision="0123456789abcdef"
2304 )
2305 ]
Chinglin Yuad12a512022-10-07 17:26:12 +08002306 self.PatchObject(
2307 uprev_lib, "get_stable_ebuild_version", return_value="12.0"
2308 )
2309 self.PatchObject(
2310 uprev_lib,
2311 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002312 side_effect=self.revisionBumpOutcome(),
Chinglin Yuad12a512022-10-07 17:26:12 +08002313 )
2314 output = packages.uprev_perfetto(None, refs, None)
Chinglin Yufa728552023-04-13 03:12:04 +00002315
Chinglin Yuad12a512022-10-07 17:26:12 +08002316 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002317 self.assertEqual(
2318 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2319 )
Chinglin Yu5de28a42022-11-11 19:52:21 +08002320 self.assertEqual(output.modified[0].new_version, "12.0-012345678")
Chinglin Yufa728552023-04-13 03:12:04 +00002321 self.assertEqual(
2322 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2323 )
2324 self.assertEqual(output.modified[1].new_version, "12.0-012345678")
Chinglin Yuad12a512022-10-07 17:26:12 +08002325
Alex Klein627e04c2021-11-10 15:56:47 -07002326
Julio Hurtadof1befec2021-05-05 21:34:26 +00002327class UprevLacrosTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002328 """Tests for uprev_lacros"""
Julio Hurtadof1befec2021-05-05 21:34:26 +00002329
Alex Klein1699fab2022-09-08 08:46:06 -06002330 def setUp(self):
2331 self.refs = [
2332 GitRef(
2333 path="/lacros", ref="refs/heads/main", revision="123.456.789.0"
2334 )
2335 ]
2336 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtadof1befec2021-05-05 21:34:26 +00002337
Alex Klein1699fab2022-09-08 08:46:06 -06002338 def revisionBumpOutcome(self, ebuild_path):
2339 return uprev_lib.UprevResult(
2340 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2341 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002342
Alex Klein1699fab2022-09-08 08:46:06 -06002343 def majorBumpOutcome(self, ebuild_path):
2344 return uprev_lib.UprevResult(
2345 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2346 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002347
Alex Klein1699fab2022-09-08 08:46:06 -06002348 def newerVersionOutcome(self, ebuild_path):
2349 return uprev_lib.UprevResult(
2350 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2351 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002352
Alex Klein1699fab2022-09-08 08:46:06 -06002353 def sameVersionOutcome(self, ebuild_path):
2354 return uprev_lib.UprevResult(
2355 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2356 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002357
Alex Klein1699fab2022-09-08 08:46:06 -06002358 def newEbuildCreatedOutcome(self, ebuild_path):
2359 return uprev_lib.UprevResult(
2360 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2361 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002362
Alex Klein1699fab2022-09-08 08:46:06 -06002363 def test_lacros_uprev_fails(self):
2364 """Test a lacros package uprev with no triggers"""
2365 self.PatchObject(
2366 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2367 )
2368 with self.assertRaises(IndexError):
2369 packages.uprev_lacros(None, [], None)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002370
Alex Klein1699fab2022-09-08 08:46:06 -06002371 def test_lacros_uprev_revision_bump(self):
2372 """Test lacros package uprev."""
2373 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2374 self.PatchObject(
2375 uprev_lib,
2376 "uprev_workon_ebuild_to_version",
2377 side_effect=[lacros_outcome],
2378 )
2379 output = packages.uprev_lacros(None, self.refs, None)
2380 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002381
Alex Klein1699fab2022-09-08 08:46:06 -06002382 def test_lacros_uprev_version_bump(self):
2383 """Test lacros package uprev."""
2384 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2385 self.PatchObject(
2386 uprev_lib,
2387 "uprev_workon_ebuild_to_version",
2388 side_effect=[lacros_outcome],
2389 )
2390 output = packages.uprev_lacros(None, self.refs, None)
2391 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002392
Alex Klein1699fab2022-09-08 08:46:06 -06002393 def test_lacros_uprev_new_ebuild_created(self):
2394 """Test lacros package uprev."""
2395 lacros_outcome = self.newEbuildCreatedOutcome(
2396 self.MOCK_LACROS_EBUILD_PATH
2397 )
2398 self.PatchObject(
2399 uprev_lib,
2400 "uprev_workon_ebuild_to_version",
2401 side_effect=[lacros_outcome],
2402 )
2403 output = packages.uprev_lacros(None, self.refs, None)
2404 self.assertTrue(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002405
Alex Klein1699fab2022-09-08 08:46:06 -06002406 def test_lacros_uprev_newer_version_exist(self):
2407 """Test the newer version exists uprev should not happen."""
2408 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2409 self.PatchObject(
2410 uprev_lib,
2411 "uprev_workon_ebuild_to_version",
2412 side_effect=[lacros_outcome],
2413 )
2414 output = packages.uprev_lacros(None, self.refs, None)
2415 self.assertFalse(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002416
Alex Klein1699fab2022-09-08 08:46:06 -06002417 def test_lacros_uprev_same_version_exist(self):
2418 """Test the same version exists uprev should not happen."""
2419 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2420 self.PatchObject(
2421 uprev_lib,
2422 "uprev_workon_ebuild_to_version",
2423 side_effect=[lacros_outcome],
2424 )
2425 output = packages.uprev_lacros(None, self.refs, None)
2426 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002427
2428
2429class UprevLacrosInParallelTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002430 """Tests for uprev_lacros"""
Julio Hurtado870ed322021-12-03 18:22:40 +00002431
Alex Klein1699fab2022-09-08 08:46:06 -06002432 def setUp(self):
2433 self.refs = [
2434 GitRef(
2435 path="/lacros", revision="abc123", ref="refs/tags/123.456.789.0"
2436 )
2437 ]
2438 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtado870ed322021-12-03 18:22:40 +00002439
Alex Klein1699fab2022-09-08 08:46:06 -06002440 def revisionBumpOutcome(self, ebuild_path):
2441 return uprev_lib.UprevResult(
2442 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2443 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002444
Alex Klein1699fab2022-09-08 08:46:06 -06002445 def majorBumpOutcome(self, ebuild_path):
2446 return uprev_lib.UprevResult(
2447 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2448 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002449
Alex Klein1699fab2022-09-08 08:46:06 -06002450 def newerVersionOutcome(self, ebuild_path):
2451 return uprev_lib.UprevResult(
2452 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2453 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002454
Alex Klein1699fab2022-09-08 08:46:06 -06002455 def sameVersionOutcome(self, ebuild_path):
2456 return uprev_lib.UprevResult(
2457 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2458 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002459
Alex Klein1699fab2022-09-08 08:46:06 -06002460 def newEbuildCreatedOutcome(self, ebuild_path):
2461 return uprev_lib.UprevResult(
2462 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2463 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002464
Alex Klein1699fab2022-09-08 08:46:06 -06002465 def test_lacros_uprev_fails(self):
2466 """Test a lacros package uprev with no triggers"""
2467 self.PatchObject(
2468 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2469 )
Alex Klein314fb5d2022-10-24 14:56:31 -06002470 with self.assertRaises(uprev_lib.NoRefsError):
Alex Klein1699fab2022-09-08 08:46:06 -06002471 packages.uprev_lacros_in_parallel(None, [], None)
Julio Hurtado870ed322021-12-03 18:22:40 +00002472
Alex Klein1699fab2022-09-08 08:46:06 -06002473 def test_lacros_uprev_revision_bump(self):
2474 """Test lacros package uprev."""
2475 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2476 self.PatchObject(
2477 uprev_lib,
2478 "uprev_workon_ebuild_to_version",
2479 side_effect=[lacros_outcome],
2480 )
2481 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2482 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002483
Alex Klein1699fab2022-09-08 08:46:06 -06002484 def test_lacros_uprev_version_bump(self):
2485 """Test lacros package uprev."""
2486 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2487 self.PatchObject(
2488 uprev_lib,
2489 "uprev_workon_ebuild_to_version",
2490 side_effect=[lacros_outcome],
2491 )
2492 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2493 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002494
Alex Klein1699fab2022-09-08 08:46:06 -06002495 def test_lacros_uprev_new_ebuild_created(self):
2496 """Test lacros package uprev."""
2497 lacros_outcome = self.newEbuildCreatedOutcome(
2498 self.MOCK_LACROS_EBUILD_PATH
2499 )
2500 self.PatchObject(
2501 uprev_lib,
2502 "uprev_workon_ebuild_to_version",
2503 side_effect=[lacros_outcome],
2504 )
2505 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2506 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002507
Alex Klein1699fab2022-09-08 08:46:06 -06002508 def test_lacros_uprev_newer_version_exist(self):
2509 """Test the newer version exists uprev should not happen."""
2510 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2511 self.PatchObject(
2512 uprev_lib,
2513 "uprev_workon_ebuild_to_version",
2514 side_effect=[lacros_outcome],
2515 )
2516 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2517 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002518
Alex Klein1699fab2022-09-08 08:46:06 -06002519 def test_lacros_uprev_same_version_exist(self):
2520 """Test the same version exists uprev should not happen."""
2521 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2522 self.PatchObject(
2523 uprev_lib,
2524 "uprev_workon_ebuild_to_version",
2525 side_effect=[lacros_outcome],
2526 )
2527 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2528 self.assertFalse(output.uprevved)