blob: 91b468924799b9ab959f77043fbefd3f1b9f60db [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.
Mike Frysinger5d85a542023-07-06 16:05:54 -04001249 self.rc.SetDefaultCmdResult(returncode=1)
Alex Klein1699fab2022-09-08 08:46:06 -06001250 build_target = build_target_lib.BuildTarget(self.board)
1251 result = packages.get_all_firmware_versions(build_target)
1252 self.assertEqual(result, {})
Benjamin Shai12c767e2022-01-12 15:17:44 +00001253
Michael Mortensen59e30872020-05-18 14:12:49 -06001254
Michael Mortensen71ef5682020-05-07 14:29:24 -06001255class GetFirmwareVersionsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001256 """Tests for get_firmware_versions."""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001257
Alex Klein1699fab2022-09-08 08:46:06 -06001258 def setUp(self):
1259 self.board = "test-board"
Alex Kleinfee86da2023-01-20 18:40:06 -07001260 # pylint: disable=line-too-long
Alex Klein1699fab2022-09-08 08:46:06 -06001261 self.rc.SetDefaultCmdResult(
1262 stdout="""
Michael Mortensen71ef5682020-05-07 14:29:24 -06001263
1264flashrom(8): a8f99c2e61e7dc09c4b25ef5a76ef692 */build/kevin/usr/sbin/flashrom
1265 ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, for GNU/Linux 2.d
1266 0.9.4 : 860875a : Apr 10 2017 23:54:29 UTC
1267
1268BIOS image: 6b5b855a0b8fd1657546d1402c15b206 *chromeos-firmware-kevin-0.0.1/.dist/kevin_fw_8785.178.0.n
1269BIOS version: Google_Kevin.8785.178.0
1270EC image: 1ebfa9518e6cac0558a80b7ab2f5b489 *chromeos-firmware-kevin-0.0.1/.dist/kevin_ec_8785.178.0.n
1271EC version:kevin_v1.10.184-459421c
1272
1273Package Content:
1274a8f99c2e61e7dc09c4b25ef5a76ef692 *./flashrom
12753c3a99346d1ca1273cbcd86c104851ff *./shflags
1276457a8dc8546764affc9700f8da328d23 *./dump_fmap
1277c392980ddb542639edf44a965a59361a *./updater5.sh
1278490c95d6123c208d20d84d7c16857c7c *./crosfw.sh
12796b5b855a0b8fd1657546d1402c15b206 *./bios.bin
12807b5bef0d2da90c23ff2e157250edf0fa *./crosutil.sh
1281d78722e4f1a0dc2d8c3d6b0bc7010ae3 *./crossystem
1282457a8dc8546764affc9700f8da328d23 *./gbb_utility
12831ebfa9518e6cac0558a80b7ab2f5b489 *./ec.bin
1284c98ca54db130886142ad582a58e90ddc *./common.sh
12855ba978bdec0f696f47f0f0de90936880 *./mosys
1286312e8ee6122057f2a246d7bcf1572f49 *./vpd
Alex Klein1699fab2022-09-08 08:46:06 -06001287"""
1288 )
Alex Kleinfee86da2023-01-20 18:40:06 -07001289 # pylint: enable=line-too-long
Michael Mortensen71ef5682020-05-07 14:29:24 -06001290
Alex Klein1699fab2022-09-08 08:46:06 -06001291 def test_get_firmware_versions(self):
1292 """Tests get_firmware_versions with mocked output."""
1293 build_target = build_target_lib.BuildTarget(self.board)
1294 result = packages.get_firmware_versions(build_target)
1295 versions = packages.FirmwareVersions(
1296 None,
1297 "Google_Kevin.8785.178.0",
1298 None,
1299 "kevin_v1.10.184-459421c",
1300 None,
1301 )
1302 self.assertEqual(result, versions)
Michael Mortensen71ef5682020-05-07 14:29:24 -06001303
1304
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001305class DetermineKernelVersionTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001306 """Tests for determine_kernel_version."""
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001307
Alex Klein1699fab2022-09-08 08:46:06 -06001308 def setUp(self):
1309 self.board = "test-board"
1310 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001311
Alex Klein1699fab2022-09-08 08:46:06 -06001312 def test_determine_kernel_version(self):
1313 """Tests that a valid kernel version is returned."""
Lizzy Presland0b978e62022-09-09 16:55:29 +00001314 kernel_candidates = [
1315 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
Alex Klein1699fab2022-09-08 08:46:06 -06001316 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001317 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1318 "sys-kernel/upstream-kernel-next-9999",
1319 "sys-kernel/socfpga-kernel-4.20-r34",
Alex Klein1699fab2022-09-08 08:46:06 -06001320 ]
1321 self.PatchObject(
Lizzy Presland0b978e62022-09-09 16:55:29 +00001322 portage_util,
1323 "GetFlattenedDepsForPackage",
1324 return_value=kernel_candidates,
1325 )
1326
1327 installed_pkgs = [
1328 "sys-kernel/linux-firmware-0.0.1-r594",
1329 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1330 "virtual/linux-sources-1-r30",
1331 ]
1332 self.PatchObject(
1333 portage_util,
1334 "GetPackageDependencies",
1335 return_value=installed_pkgs,
Alex Klein1699fab2022-09-08 08:46:06 -06001336 )
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001337
Alex Klein1699fab2022-09-08 08:46:06 -06001338 result = packages.determine_kernel_version(self.build_target)
1339 self.assertEqual(result, "4.4.223-r2209")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001340
Lizzy Presland0b978e62022-09-09 16:55:29 +00001341 def test_determine_kernel_version_ignores_exact_duplicates(self):
1342 """Tests that multiple results for candidates is ignored."""
1343 # Depgraph is evaluated for version as well as revision, so graph will
1344 # return all results twice.
1345 kernel_candidates = [
1346 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1347 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1348 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1349 "sys-kernel/upstream-kernel-next-9999",
1350 "sys-kernel/socfpga-kernel-4.20-r34",
1351 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1352 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1353 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1354 "sys-kernel/upstream-kernel-next-9999",
1355 "sys-kernel/socfpga-kernel-4.20-r34",
1356 ]
1357 self.PatchObject(
1358 portage_util,
1359 "GetFlattenedDepsForPackage",
1360 return_value=kernel_candidates,
1361 )
1362
1363 installed_pkgs = [
1364 "sys-kernel/linux-firmware-0.0.1-r594",
1365 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1366 "virtual/linux-sources-1-r30",
1367 ]
Alex Klein1699fab2022-09-08 08:46:06 -06001368 self.PatchObject(
1369 portage_util,
1370 "GetPackageDependencies",
Lizzy Presland0b978e62022-09-09 16:55:29 +00001371 return_value=installed_pkgs,
1372 )
1373
1374 result = packages.determine_kernel_version(self.build_target)
1375 self.assertEqual(result, "4.4.223-r2209")
1376
1377 def test_determine_kernel_version_ignores_virtual_package(self):
1378 """Tests that top-level package is ignored as potential kernel pkg."""
1379 # Depgraph results include the named package at level 0 as well as its
1380 # first-order dependencies, so verify that the virtual package is not
1381 # included as a kernel package.
1382 kernel_candidates = [
1383 "virtual/linux-sources-1",
1384 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1385 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1386 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1387 "sys-kernel/upstream-kernel-next-9999",
1388 "sys-kernel/socfpga-kernel-4.20-r34",
1389 "virtual/linux-sources-1-r30",
1390 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1391 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1392 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1393 "sys-kernel/upstream-kernel-next-9999",
1394 "sys-kernel/socfpga-kernel-4.20-r34",
1395 ]
1396 self.PatchObject(
1397 portage_util,
1398 "GetFlattenedDepsForPackage",
1399 return_value=kernel_candidates,
1400 )
1401
1402 installed_pkgs = [
1403 "sys-kernel/linux-firmware-0.0.1-r594",
1404 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1405 "virtual/linux-sources-1-r30",
1406 ]
1407 self.PatchObject(
1408 portage_util,
1409 "GetPackageDependencies",
1410 return_value=installed_pkgs,
1411 )
1412
1413 result = packages.determine_kernel_version(self.build_target)
1414 self.assertEqual(result, "4.4.223-r2209")
1415
1416 def test_determine_kernel_version_too_many(self):
1417 """Tests that an exception is thrown with too many matching packages."""
1418 package_result = [
1419 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1420 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1421 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1422 "sys-kernel/upstream-kernel-next-9999",
1423 "sys-kernel/socfpga-kernel-4.20-r34",
1424 ]
1425 self.PatchObject(
1426 portage_util,
1427 "GetFlattenedDepsForPackage",
1428 return_value=package_result,
1429 )
1430
1431 installed_pkgs = [
1432 "sys-kernel/linux-firmware-0.0.1-r594",
1433 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1434 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1435 "virtual/linux-sources-1-r30",
1436 ]
1437 self.PatchObject(
1438 portage_util,
1439 "GetPackageDependencies",
1440 return_value=installed_pkgs,
1441 )
1442
1443 with self.assertRaises(packages.KernelVersionError):
1444 packages.determine_kernel_version(self.build_target)
1445
1446 def test_determine_kernel_version_no_kernel_match(self):
1447 """Tests that an exception is thrown with 0-sized intersection."""
1448 package_result = [
1449 "sys-kernel/chromeos-kernel-experimental-4.18_rc2-r23",
1450 "sys-kernel/chromeos-kernel-4_4-4.4.223-r2209",
1451 "sys-kernel/chromeos-kernel-5_15-5.15.65-r869",
1452 "sys-kernel/upstream-kernel-next-9999",
1453 ]
1454 self.PatchObject(
1455 portage_util,
1456 "GetFlattenedDepsForPackage",
1457 return_value=package_result,
1458 )
1459
1460 installed_pkgs = [
1461 "sys-kernel/linux-firmware-0.0.1-r594",
1462 "sys-kernel/socfpga-kernel-4.20-r34",
1463 "virtual/linux-sources-1-r30",
1464 ]
1465 self.PatchObject(
1466 portage_util,
1467 "GetPackageDependencies",
1468 return_value=installed_pkgs,
1469 )
1470
1471 with self.assertRaises(packages.KernelVersionError):
1472 packages.determine_kernel_version(self.build_target)
1473
1474 def test_determine_kernel_version_exception(self):
1475 """Tests that portage_util exceptions result in returning empty str."""
1476 self.PatchObject(
1477 portage_util,
1478 "GetFlattenedDepsForPackage",
Alex Klein1699fab2022-09-08 08:46:06 -06001479 side_effect=cros_build_lib.RunCommandError("error"),
1480 )
1481 result = packages.determine_kernel_version(self.build_target)
Lizzy Presland0b978e62022-09-09 16:55:29 +00001482 self.assertEqual(result, "")
Michael Mortensenfbf2b2d2020-05-14 16:33:06 -06001483
Alex Klein627e04c2021-11-10 15:56:47 -07001484
Michael Mortensenc2615b72019-10-15 08:12:24 -06001485class ChromeVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001486 """Tests getting chrome version."""
Michael Mortensen14960d02019-10-18 07:53:59 -06001487
Alex Klein1699fab2022-09-08 08:46:06 -06001488 def setUp(self):
1489 self.build_target = build_target_lib.BuildTarget("board")
Michael Mortensenc2615b72019-10-15 08:12:24 -06001490
Alex Klein1699fab2022-09-08 08:46:06 -06001491 def test_determine_chrome_version(self):
1492 """Tests that a valid chrome version is returned."""
1493 # Mock PortageqBestVisible to return a valid chrome version string.
1494 r1_cpf = "chromeos-base/chromeos-chrome-78.0.3900.0_rc-r1"
1495 r1_cpv = package_info.SplitCPV(r1_cpf)
1496 self.PatchObject(
1497 portage_util, "PortageqBestVisible", return_value=r1_cpv
1498 )
Michael Mortensenc2615b72019-10-15 08:12:24 -06001499
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001500 chrome_version = packages.determine_package_version(
1501 constants.CHROME_CP, self.build_target
1502 )
Alex Klein1699fab2022-09-08 08:46:06 -06001503 version_numbers = chrome_version.split(".")
1504 self.assertEqual(len(version_numbers), 4)
1505 self.assertEqual(int(version_numbers[0]), 78)
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001506
Alex Klein1699fab2022-09-08 08:46:06 -06001507 def test_determine_chrome_version_handle_exception(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001508 # Mock what happens when portage throws an exception that bubbles up
1509 # (via RunCommand)inside portage_util.PortageqBestVisible.
Alex Klein1699fab2022-09-08 08:46:06 -06001510 self.PatchObject(
1511 portage_util,
1512 "PortageqBestVisible",
1513 side_effect=cros_build_lib.RunCommandError("error"),
1514 )
Gilberto Contreras4f2d1452023-01-30 23:22:58 +00001515 target = packages.determine_package_version(
1516 constants.CHROME_CP, self.build_target
1517 )
Alex Klein1699fab2022-09-08 08:46:06 -06001518 self.assertEqual(target, None)
Michael Mortensen9fe740c2019-10-29 14:42:48 -06001519
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001520
1521class PlatformVersionsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001522 """Tests getting platform version."""
Michael Mortensen9fdb14b2019-10-17 11:17:30 -06001523
Alex Klein1699fab2022-09-08 08:46:06 -06001524 def test_determine_platform_version(self):
1525 """Test checking that a valid platform version is returned."""
1526 platform_version = packages.determine_platform_version()
1527 # The returned platform version is something like 12603.0.0.
1528 version_string_list = platform_version.split(".")
1529 self.assertEqual(len(version_string_list), 3)
Alex Kleinfee86da2023-01-20 18:40:06 -07001530 # We don't want to check an exact version, but the first number should
1531 # be non-zero.
Alex Klein1699fab2022-09-08 08:46:06 -06001532 self.assertGreaterEqual(int(version_string_list[0]), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001533
Alex Klein1699fab2022-09-08 08:46:06 -06001534 def test_determine_milestone_version(self):
1535 """Test checking that a valid milestone version is returned."""
1536 milestone_version = packages.determine_milestone_version()
1537 # Milestone version should be non-zero
1538 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001539
Alex Klein1699fab2022-09-08 08:46:06 -06001540 def test_determine_full_version(self):
1541 """Test checking that a valid full version is returned."""
1542 full_version = packages.determine_full_version()
1543 pattern = r"^R(\d+)-(\d+.\d+.\d+(-rc\d+)*)"
1544 m = re.match(pattern, full_version)
1545 self.assertTrue(m)
1546 milestone_version = m.group(1)
1547 self.assertGreaterEqual(int(milestone_version), 1)
Michael Mortensen009cb662019-10-21 11:38:43 -06001548
Alex Klein1699fab2022-09-08 08:46:06 -06001549 def test_versions_based_on_mock(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001550 # Create a test version_info object, and then mock VersionInfo.from_repo
Alex Klein1699fab2022-09-08 08:46:06 -06001551 # return it.
1552 test_platform_version = "12575.0.0"
1553 test_chrome_branch = "75"
1554 version_info_mock = chromeos_version.VersionInfo(test_platform_version)
1555 version_info_mock.chrome_branch = test_chrome_branch
1556 self.PatchObject(
1557 chromeos_version.VersionInfo,
1558 "from_repo",
1559 return_value=version_info_mock,
1560 )
1561 test_full_version = (
1562 "R" + test_chrome_branch + "-" + test_platform_version
1563 )
1564 platform_version = packages.determine_platform_version()
1565 milestone_version = packages.determine_milestone_version()
1566 full_version = packages.determine_full_version()
1567 self.assertEqual(platform_version, test_platform_version)
1568 self.assertEqual(milestone_version, test_chrome_branch)
1569 self.assertEqual(full_version, test_full_version)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001570
1571
1572# Each of the columns in the following table is a separate dimension along
1573# which Chrome uprev test cases can vary in behavior. The full test space would
1574# be the Cartesian product of the possible values of each column.
1575# 'CHROME_EBUILD' refers to the relationship between the version of the existing
1576# Chrome ebuild vs. the requested uprev version. 'FOLLOWER_EBUILDS' refers to
1577# the same relationship but for the packages defined in OTHER_CHROME_PACKAGES.
1578# 'EBUILDS MODIFIED' refers to whether any of the existing 9999 ebuilds have
1579# modified contents relative to their corresponding stable ebuilds.
1580#
1581# CHROME_EBUILD FOLLOWER_EBUILDS EBUILDS_MODIFIED
1582#
1583# HIGHER HIGHER YES
1584# SAME SAME NO
1585# LOWER LOWER
1586# DOESN'T EXIST YET
1587
1588# These test cases cover both CHROME & FOLLOWER ebuilds being identically
1589# higher, lower, or the same versions, with no modified ebuilds.
1590UPREV_VERSION_CASES = (
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001591 # Uprev.
Chris McDonaldea0312c2020-05-04 23:33:15 -06001592 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001593 "80.0.8080.0",
1594 "81.0.8181.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001595 # One added and one deleted for chrome and each "other" package.
1596 2 * (1 + len(constants.OTHER_CHROME_PACKAGES)),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001597 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001598 id="newer_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001599 ),
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001600 # Revbump.
1601 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001602 "80.0.8080.0",
1603 "80.0.8080.0",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001604 2,
1605 True,
Alex Klein1699fab2022-09-08 08:46:06 -06001606 id="chrome_revbump",
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001607 ),
Chris McDonaldea0312c2020-05-04 23:33:15 -06001608 # No files should be changed in these cases.
1609 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001610 "80.0.8080.0",
1611 "80.0.8080.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001612 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001613 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001614 id="same_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001615 ),
1616 pytest.param(
Alex Klein1699fab2022-09-08 08:46:06 -06001617 "80.0.8080.0",
1618 "79.0.7979.0",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001619 0,
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001620 False,
Alex Klein1699fab2022-09-08 08:46:06 -06001621 id="older_chrome_version",
Chris McDonaldea0312c2020-05-04 23:33:15 -06001622 ),
1623)
1624
1625
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001626@pytest.mark.parametrize(
Alex Klein1699fab2022-09-08 08:46:06 -06001627 "old_version, new_version, expected_count, modify_unstable",
1628 UPREV_VERSION_CASES,
1629)
1630def test_uprev_chrome_all_files_already_exist(
1631 old_version,
1632 new_version,
1633 expected_count,
1634 modify_unstable,
1635 monkeypatch,
1636 overlay_stack,
1637):
1638 """Test Chrome uprevs work as expected when all packages already exist."""
1639 (overlay,) = overlay_stack(1)
1640 monkeypatch.setattr(uprev_lib, "_CHROME_OVERLAY_PATH", overlay.path)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001641
Alex Klein1699fab2022-09-08 08:46:06 -06001642 unstable_chrome = cr.test.Package(
1643 "chromeos-base", "chromeos-chrome", version="9999", keywords="~*"
1644 )
1645 if modify_unstable:
1646 # Add some field not set in stable.
1647 unstable_chrome.depend = "foo/bar"
Alex Klein0b2ec2d2021-06-23 15:56:45 -06001648
Alex Klein1699fab2022-09-08 08:46:06 -06001649 stable_chrome = cr.test.Package(
1650 "chromeos-base", "chromeos-chrome", version=f"{old_version}_rc-r1"
1651 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001652
Alex Klein1699fab2022-09-08 08:46:06 -06001653 overlay.add_package(unstable_chrome)
1654 overlay.add_package(stable_chrome)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001655
Alex Klein1699fab2022-09-08 08:46:06 -06001656 for pkg_str in constants.OTHER_CHROME_PACKAGES:
1657 category, pkg_name = pkg_str.split("/")
1658 unstable_pkg = cr.test.Package(
1659 category, pkg_name, version="9999", keywords="~*"
1660 )
1661 stable_pkg = cr.test.Package(
1662 category, pkg_name, version=f"{old_version}_rc-r1"
1663 )
Chris McDonaldea0312c2020-05-04 23:33:15 -06001664
Alex Klein1699fab2022-09-08 08:46:06 -06001665 overlay.add_package(unstable_pkg)
1666 overlay.add_package(stable_pkg)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001667
Alex Klein1699fab2022-09-08 08:46:06 -06001668 git_refs = [
1669 GitRef(
1670 path="/foo", ref=f"refs/tags/{new_version}", revision="stubcommit"
1671 )
1672 ]
1673 res = packages.uprev_chrome_from_ref(None, git_refs, None)
Chris McDonaldea0312c2020-05-04 23:33:15 -06001674
Alex Klein1699fab2022-09-08 08:46:06 -06001675 modified_file_count = sum(len(m.files) for m in res.modified)
1676 assert modified_file_count == expected_count
Michael Mortensen125bb012020-05-21 14:02:10 -06001677
1678
Alex Klein1699fab2022-09-08 08:46:06 -06001679@pytest.mark.usefixtures("testcase_monkeypatch")
Michael Mortensen125bb012020-05-21 14:02:10 -06001680class GetModelsTest(cros_test_lib.RunCommandTempDirTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001681 """Tests for get_models."""
Michael Mortensen125bb012020-05-21 14:02:10 -06001682
Alex Klein1699fab2022-09-08 08:46:06 -06001683 def setUp(self):
1684 self.board = "test-board"
1685 self.rc.SetDefaultCmdResult(stdout="pyro\nreef\nsnappy\n")
1686 self.monkeypatch.setattr(constants, "SOURCE_ROOT", self.tempdir)
1687 build_bin = os.path.join(
1688 self.tempdir, constants.DEFAULT_CHROOT_DIR, "usr", "bin"
1689 )
1690 osutils.Touch(
1691 os.path.join(build_bin, "cros_config_host"), makedirs=True
1692 )
Michael Mortensen125bb012020-05-21 14:02:10 -06001693
Alex Klein1699fab2022-09-08 08:46:06 -06001694 def testGetModels(self):
1695 """Test get_models."""
1696 build_target = build_target_lib.BuildTarget(self.board)
1697 result = packages.get_models(build_target)
1698 self.assertEqual(result, ["pyro", "reef", "snappy"])
Michael Mortensen359c1f32020-05-28 19:35:42 -06001699
1700
1701class GetKeyIdTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001702 """Tests for get_key_id."""
Michael Mortensen359c1f32020-05-28 19:35:42 -06001703
Alex Klein1699fab2022-09-08 08:46:06 -06001704 def setUp(self):
1705 self.board = "test-board"
1706 self.build_target = build_target_lib.BuildTarget(self.board)
Michael Mortensen359c1f32020-05-28 19:35:42 -06001707
Alex Klein1699fab2022-09-08 08:46:06 -06001708 def testGetKeyId(self):
1709 """Test get_key_id when _run_cros_config_host returns a key."""
1710 self.PatchObject(
1711 packages, "_run_cros_config_host", return_value=["key"]
1712 )
1713 result = packages.get_key_id(self.build_target, "model")
1714 self.assertEqual(result, "key")
Michael Mortensen359c1f32020-05-28 19:35:42 -06001715
Alex Klein1699fab2022-09-08 08:46:06 -06001716 def testGetKeyIdNoKey(self):
1717 """Test get_key_id when None should be returned."""
1718 self.PatchObject(
1719 packages, "_run_cros_config_host", return_value=["key1", "key2"]
1720 )
1721 result = packages.get_key_id(self.build_target, "model")
1722 self.assertEqual(result, None)
Ben Reiche779cf42020-12-15 03:21:31 +00001723
1724
Harvey Yang3eee06c2021-03-18 15:47:56 +08001725class GetLatestVersionTest(cros_test_lib.TestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001726 """Tests for get_latest_version_from_refs."""
Ben Reiche779cf42020-12-15 03:21:31 +00001727
Alex Klein1699fab2022-09-08 08:46:06 -06001728 def setUp(self):
1729 self.prefix = "refs/tags/drivefs_"
1730 # The tag ref template.
1731 ref_tpl = self.prefix + "%s"
Ben Reiche779cf42020-12-15 03:21:31 +00001732
Alex Klein1699fab2022-09-08 08:46:06 -06001733 self.latest = "44.0.20"
1734 self.versions = ["42.0.1", self.latest, "44.0.19", "39.0.15"]
1735 self.latest_ref = uprev_lib.GitRef(
1736 "/path", ref_tpl % self.latest, "abc123"
1737 )
1738 self.refs = [
1739 uprev_lib.GitRef("/path", ref_tpl % v, "abc123")
1740 for v in self.versions
1741 ]
Ben Reiche779cf42020-12-15 03:21:31 +00001742
Alex Klein1699fab2022-09-08 08:46:06 -06001743 def test_single_ref(self):
1744 """Test a single ref is supplied."""
1745 # pylint: disable=protected-access
1746 self.assertEqual(
1747 self.latest,
1748 packages._get_latest_version_from_refs(
1749 self.prefix, [self.latest_ref]
1750 ),
1751 )
Ben Reiche779cf42020-12-15 03:21:31 +00001752
Alex Klein1699fab2022-09-08 08:46:06 -06001753 def test_multiple_ref_versions(self):
1754 """Test multiple refs supplied."""
1755 # pylint: disable=protected-access
1756 self.assertEqual(
1757 self.latest,
1758 packages._get_latest_version_from_refs(self.prefix, self.refs),
1759 )
Ben Reiche779cf42020-12-15 03:21:31 +00001760
Alex Klein1699fab2022-09-08 08:46:06 -06001761 def test_no_refs_returns_none(self):
1762 """Test no refs supplied."""
1763 # pylint: disable=protected-access
1764 self.assertEqual(
1765 packages._get_latest_version_from_refs(self.prefix, []), None
1766 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001767
Chinglin Yu84818732022-10-03 12:03:43 +08001768 def test_ref_prefix(self):
1769 """Test refs with a different prefix isn't used"""
1770 # pylint: disable=protected-access
1771 # Add refs/tags/foo_100.0.0 to the refs, which should be ignored in
1772 # _get_latest_version_from_refs because the prefix doesn't match, even
1773 # if its version number is larger.
1774 refs = self.refs + [
1775 uprev_lib.GitRef("/path", "refs/tags/foo_100.0.0", "abc123")
1776 ]
1777 self.assertEqual(
1778 self.latest,
1779 packages._get_latest_version_from_refs(self.prefix, refs),
1780 )
1781
Harvey Yang9c61e9c2021-03-02 16:32:43 +08001782
Alex Klein6becabc2020-09-11 14:03:05 -06001783class NeedsChromeSourceTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001784 """Tests for needs_chrome_source."""
Alex Klein6becabc2020-09-11 14:03:05 -06001785
Alex Klein1699fab2022-09-08 08:46:06 -06001786 def _build_graph(self, with_chrome: bool, with_followers: bool):
1787 root = "/build/build_target"
1788 foo_bar = package_info.parse("foo/bar-1")
1789 chrome = package_info.parse(f"{constants.CHROME_CP}-1.2.3.4")
1790 followers = [
1791 package_info.parse(f"{pkg}-1.2.3.4")
1792 for pkg in constants.OTHER_CHROME_PACKAGES
1793 ]
1794 nodes = [dependency_graph.PackageNode(foo_bar, root)]
1795 root_pkgs = ["foo/bar-1"]
1796 if with_chrome:
1797 nodes.append(dependency_graph.PackageNode(chrome, root))
1798 root_pkgs.append(chrome.cpvr)
1799 if with_followers:
1800 nodes.extend(
1801 [dependency_graph.PackageNode(f, root) for f in followers]
1802 )
1803 root_pkgs.extend([f.cpvr for f in followers])
Alex Klein6becabc2020-09-11 14:03:05 -06001804
Alex Klein1699fab2022-09-08 08:46:06 -06001805 return dependency_graph.DependencyGraph(nodes, root, root_pkgs)
Alex Klein6becabc2020-09-11 14:03:05 -06001806
Alex Klein1699fab2022-09-08 08:46:06 -06001807 def test_needs_all(self):
1808 """Verify we need source when we have no prebuilts."""
1809 graph = self._build_graph(with_chrome=True, with_followers=True)
1810 self.PatchObject(
1811 depgraph, "get_sysroot_dependency_graph", return_value=graph
1812 )
1813 self.PatchObject(packages, "has_prebuilt", return_value=False)
1814 self.PatchObject(
1815 packages,
1816 "uprev_chrome",
1817 return_value=uprev_lib.UprevVersionedPackageResult(),
1818 )
Alex Klein6becabc2020-09-11 14:03:05 -06001819
Alex Klein1699fab2022-09-08 08:46:06 -06001820 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001821
Alex Klein1699fab2022-09-08 08:46:06 -06001822 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001823
Alex Klein1699fab2022-09-08 08:46:06 -06001824 self.assertTrue(result.needs_chrome_source)
1825 self.assertTrue(result.builds_chrome)
1826 self.assertTrue(result.packages)
1827 self.assertEqual(
1828 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1829 )
1830 self.assertTrue(result.missing_chrome_prebuilt)
1831 self.assertTrue(result.missing_follower_prebuilt)
1832 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001833
Alex Klein1699fab2022-09-08 08:46:06 -06001834 def test_needs_none(self):
Alex Kleinfee86da2023-01-20 18:40:06 -07001835 """Verify not building any chrome packages prevents needing it."""
Alex Klein1699fab2022-09-08 08:46:06 -06001836 graph = self._build_graph(with_chrome=False, with_followers=False)
1837 self.PatchObject(
1838 depgraph, "get_sysroot_dependency_graph", return_value=graph
1839 )
1840 self.PatchObject(packages, "has_prebuilt", return_value=False)
1841 self.PatchObject(
1842 packages,
1843 "uprev_chrome",
1844 return_value=uprev_lib.UprevVersionedPackageResult(),
1845 )
Alex Klein6becabc2020-09-11 14:03:05 -06001846
Alex Klein1699fab2022-09-08 08:46:06 -06001847 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001848
Alex Klein1699fab2022-09-08 08:46:06 -06001849 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001850
Alex Klein1699fab2022-09-08 08:46:06 -06001851 self.assertFalse(result.needs_chrome_source)
1852 self.assertFalse(result.builds_chrome)
1853 self.assertFalse(result.packages)
1854 self.assertFalse(result.missing_chrome_prebuilt)
1855 self.assertFalse(result.missing_follower_prebuilt)
1856 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001857
Alex Klein1699fab2022-09-08 08:46:06 -06001858 def test_needs_chrome_only(self):
1859 """Verify only chrome triggers needs chrome source."""
1860 graph = self._build_graph(with_chrome=True, with_followers=False)
1861 self.PatchObject(
1862 depgraph, "get_sysroot_dependency_graph", return_value=graph
1863 )
1864 self.PatchObject(packages, "has_prebuilt", return_value=False)
1865 self.PatchObject(
1866 packages,
1867 "uprev_chrome",
1868 return_value=uprev_lib.UprevVersionedPackageResult(),
1869 )
Alex Klein6becabc2020-09-11 14:03:05 -06001870
Alex Klein1699fab2022-09-08 08:46:06 -06001871 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001872
Alex Klein1699fab2022-09-08 08:46:06 -06001873 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001874
Alex Klein1699fab2022-09-08 08:46:06 -06001875 self.assertTrue(result.needs_chrome_source)
1876 self.assertTrue(result.builds_chrome)
1877 self.assertTrue(result.packages)
1878 self.assertEqual(
Alex Klein041edd82023-04-17 12:23:23 -06001879 {p.atom for p in result.packages}, {constants.CHROME_CP}
Alex Klein1699fab2022-09-08 08:46:06 -06001880 )
1881 self.assertTrue(result.missing_chrome_prebuilt)
1882 self.assertFalse(result.missing_follower_prebuilt)
1883 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001884
Alex Klein1699fab2022-09-08 08:46:06 -06001885 def test_needs_followers_only(self):
1886 """Verify only chrome followers triggers needs chrome source."""
1887 graph = self._build_graph(with_chrome=False, with_followers=True)
1888 self.PatchObject(
1889 depgraph, "get_sysroot_dependency_graph", return_value=graph
1890 )
1891 self.PatchObject(packages, "has_prebuilt", return_value=False)
1892 self.PatchObject(
1893 packages,
1894 "uprev_chrome",
1895 return_value=uprev_lib.UprevVersionedPackageResult(),
1896 )
Alex Klein6becabc2020-09-11 14:03:05 -06001897
Alex Klein1699fab2022-09-08 08:46:06 -06001898 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001899
Alex Klein1699fab2022-09-08 08:46:06 -06001900 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001901
Alex Klein1699fab2022-09-08 08:46:06 -06001902 self.assertTrue(result.needs_chrome_source)
1903 self.assertFalse(result.builds_chrome)
1904 self.assertTrue(result.packages)
1905 self.assertEqual(
Alex Klein041edd82023-04-17 12:23:23 -06001906 {p.atom for p in result.packages},
Alex Klein1699fab2022-09-08 08:46:06 -06001907 set(constants.OTHER_CHROME_PACKAGES),
1908 )
1909 self.assertFalse(result.missing_chrome_prebuilt)
1910 self.assertTrue(result.missing_follower_prebuilt)
1911 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001912
Alex Klein1699fab2022-09-08 08:46:06 -06001913 def test_has_prebuilts(self):
1914 """Test prebuilts prevent us from needing chrome source."""
1915 graph = self._build_graph(with_chrome=True, with_followers=True)
1916 self.PatchObject(
1917 depgraph, "get_sysroot_dependency_graph", return_value=graph
1918 )
1919 self.PatchObject(packages, "has_prebuilt", return_value=True)
1920 self.PatchObject(
1921 packages,
1922 "uprev_chrome",
1923 return_value=uprev_lib.UprevVersionedPackageResult(),
1924 )
Alex Klein6becabc2020-09-11 14:03:05 -06001925
Alex Klein1699fab2022-09-08 08:46:06 -06001926 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001927
Alex Klein1699fab2022-09-08 08:46:06 -06001928 result = packages.needs_chrome_source(build_target)
Alex Klein6becabc2020-09-11 14:03:05 -06001929
Alex Klein1699fab2022-09-08 08:46:06 -06001930 self.assertFalse(result.needs_chrome_source)
1931 self.assertTrue(result.builds_chrome)
1932 self.assertFalse(result.packages)
1933 self.assertFalse(result.missing_chrome_prebuilt)
1934 self.assertFalse(result.missing_follower_prebuilt)
1935 self.assertFalse(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001936
Alex Klein1699fab2022-09-08 08:46:06 -06001937 def test_compile_source(self):
1938 """Test compile source ignores prebuilts."""
1939 graph = self._build_graph(with_chrome=True, with_followers=True)
1940 self.PatchObject(
1941 depgraph, "get_sysroot_dependency_graph", return_value=graph
1942 )
1943 self.PatchObject(packages, "has_prebuilt", return_value=True)
1944 self.PatchObject(
1945 packages,
1946 "uprev_chrome",
1947 return_value=uprev_lib.UprevVersionedPackageResult(),
1948 )
Alex Klein6becabc2020-09-11 14:03:05 -06001949
Alex Klein1699fab2022-09-08 08:46:06 -06001950 build_target = build_target_lib.BuildTarget("build_target")
Alex Klein6becabc2020-09-11 14:03:05 -06001951
Alex Klein1699fab2022-09-08 08:46:06 -06001952 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Klein6becabc2020-09-11 14:03:05 -06001953
Alex Klein1699fab2022-09-08 08:46:06 -06001954 self.assertTrue(result.needs_chrome_source)
1955 self.assertTrue(result.builds_chrome)
1956 self.assertTrue(result.packages)
1957 self.assertEqual(
1958 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1959 )
1960 self.assertTrue(result.missing_chrome_prebuilt)
1961 self.assertTrue(result.missing_follower_prebuilt)
1962 self.assertFalse(result.local_uprev)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001963
Alex Klein1699fab2022-09-08 08:46:06 -06001964 def test_local_uprev(self):
1965 """Test compile source ignores prebuilts."""
1966 graph = self._build_graph(with_chrome=True, with_followers=True)
1967 self.PatchObject(
1968 depgraph, "get_sysroot_dependency_graph", return_value=graph
1969 )
1970 self.PatchObject(packages, "has_prebuilt", return_value=False)
Alex Klein75110572021-07-14 10:44:39 -06001971
Alex Klein1699fab2022-09-08 08:46:06 -06001972 uprev_result = uprev_lib.UprevVersionedPackageResult()
1973 uprev_result.add_result("1.2.3.4", ["/tmp/foo"])
1974 self.PatchObject(packages, "uprev_chrome", return_value=uprev_result)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001975
Alex Klein1699fab2022-09-08 08:46:06 -06001976 build_target = build_target_lib.BuildTarget("build_target")
Alex Kleinde7b76d2021-07-12 12:28:44 -06001977
Alex Klein1699fab2022-09-08 08:46:06 -06001978 result = packages.needs_chrome_source(build_target, compile_source=True)
Alex Kleinde7b76d2021-07-12 12:28:44 -06001979
Alex Klein1699fab2022-09-08 08:46:06 -06001980 self.assertTrue(result.needs_chrome_source)
1981 self.assertTrue(result.builds_chrome)
1982 self.assertTrue(result.packages)
1983 self.assertEqual(
1984 len(result.packages), len(constants.OTHER_CHROME_PACKAGES) + 1
1985 )
1986 self.assertTrue(result.missing_chrome_prebuilt)
1987 self.assertTrue(result.missing_follower_prebuilt)
1988 self.assertTrue(result.local_uprev)
Alex Klein6becabc2020-09-11 14:03:05 -06001989
1990
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001991class UprevDrivefsTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06001992 """Tests for uprev_drivefs."""
Ben Reich4f3fa1b2020-12-19 08:21:26 +00001993
Alex Klein1699fab2022-09-08 08:46:06 -06001994 def setUp(self):
1995 self.refs = [
1996 GitRef(
1997 path="/chromeos/platform/drivefs-google3/",
1998 ref="refs/tags/drivefs_45.0.2",
1999 revision="123",
2000 )
2001 ]
2002 self.MOCK_DRIVEFS_EBUILD_PATH = "drivefs.45.0.2-r1.ebuild"
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002003
Alex Klein1699fab2022-09-08 08:46:06 -06002004 def revisionBumpOutcome(self, ebuild_path):
2005 return uprev_lib.UprevResult(
2006 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2007 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002008
Alex Klein1699fab2022-09-08 08:46:06 -06002009 def majorBumpOutcome(self, ebuild_path):
2010 return uprev_lib.UprevResult(
2011 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2012 )
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002013
Alex Klein1699fab2022-09-08 08:46:06 -06002014 def sameVersionOutcome(self):
2015 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002016
Alex Klein1699fab2022-09-08 08:46:06 -06002017 def test_latest_version_returns_none(self):
2018 """Test no refs were supplied"""
2019 output = packages.uprev_drivefs(None, [], None)
2020 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002021
Alex Klein1699fab2022-09-08 08:46:06 -06002022 def test_drivefs_uprev_fails(self):
2023 """Test a single ref is supplied."""
2024 self.PatchObject(
2025 uprev_lib,
2026 "uprev_workon_ebuild_to_version",
2027 side_effect=[None, None],
2028 )
2029 output = packages.uprev_drivefs(None, self.refs, None)
2030 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002031
Alex Klein1699fab2022-09-08 08:46:06 -06002032 def test_same_version_exists(self):
2033 """Test the same version exists uprev should not happen."""
2034 drivefs_outcome = self.sameVersionOutcome()
2035 self.PatchObject(
2036 uprev_lib,
2037 "uprev_workon_ebuild_to_version",
2038 side_effect=[drivefs_outcome],
2039 )
2040 output = packages.uprev_drivefs(None, self.refs, None)
2041 self.assertFalse(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002042
Alex Klein1699fab2022-09-08 08:46:06 -06002043 def test_revision_bump_both_packages(self):
2044 """Test both packages uprev, should succeed."""
2045 drivefs_outcome = self.revisionBumpOutcome(
2046 self.MOCK_DRIVEFS_EBUILD_PATH
2047 )
2048 self.PatchObject(
2049 uprev_lib,
2050 "uprev_workon_ebuild_to_version",
2051 side_effect=[drivefs_outcome],
2052 )
2053 output = packages.uprev_drivefs(None, self.refs, None)
2054 self.assertTrue(output.uprevved)
Ben Reich4f3fa1b2020-12-19 08:21:26 +00002055
Alex Klein1699fab2022-09-08 08:46:06 -06002056 def test_major_bump_both_packages(self):
2057 """Test both packages uprev, should succeed."""
2058 drivefs_outcome = self.majorBumpOutcome(self.MOCK_DRIVEFS_EBUILD_PATH)
2059 self.PatchObject(
2060 uprev_lib,
2061 "uprev_workon_ebuild_to_version",
2062 side_effect=[drivefs_outcome],
2063 )
2064 output = packages.uprev_drivefs(None, self.refs, None)
2065 self.assertTrue(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002066
2067
Denis Nikitin63613e32022-09-09 22:26:50 -07002068class UprevKernelAfdo(cros_test_lib.RunCommandTempDirTestCase):
2069 """Tests for uprev_kernel_afdo."""
2070
2071 def setUp(self):
2072 # patch_ebuild_vars is tested separately.
2073 self.mock_patch = self.PatchObject(packages, "patch_ebuild_vars")
Denis Nikitin88ad5132022-09-28 12:10:01 -07002074 self.PatchObject(constants, "SOURCE_ROOT", new=self.tempdir)
Denis Nikitin63613e32022-09-09 22:26:50 -07002075 self.metadata_dir = os.path.join(
Denis Nikitin63613e32022-09-09 22:26:50 -07002076 "src",
2077 "third_party",
2078 "toolchain-utils",
2079 "afdo_metadata",
2080 )
Denis Nikitin88ad5132022-09-28 12:10:01 -07002081 osutils.SafeMakedirs(os.path.join(self.tempdir, self.metadata_dir))
Denis Nikitin63613e32022-09-09 22:26:50 -07002082
2083 def test_uprev_kernel_afdo_version(self):
2084 """Test kernel afdo version uprev."""
2085 json_files = {
2086 "kernel_afdo.json": (
2087 "{\n"
2088 ' "chromeos-kernel-5_4": {\n'
2089 ' "name": "R106-12345.0-0123456789"\n'
2090 " }\n"
2091 "}"
2092 ),
2093 "kernel_arm_afdo.json": (
2094 "{\n"
2095 ' "chromeos-kernel-5_15": {\n'
2096 ' "name": "R107-67890.0-0123456789"\n'
2097 " }\n"
2098 "}"
2099 ),
2100 }
2101 for f, contents in json_files.items():
2102 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2103
Brian Norrisf0c68572023-07-12 17:32:20 -07002104 returned_output = packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002105
Denis Nikitin88ad5132022-09-28 12:10:01 -07002106 package_root = os.path.join(
2107 constants.SOURCE_ROOT,
2108 constants.CHROMIUMOS_OVERLAY_DIR,
2109 "sys-kernel",
Denis Nikitin63613e32022-09-09 22:26:50 -07002110 )
2111 expect_result = [
2112 uprev_lib.UprevVersionedPackageModifications(
2113 new_version="R106-12345.0-0123456789",
2114 files=[
2115 os.path.join(
2116 package_root,
2117 "chromeos-kernel-5_4",
2118 "chromeos-kernel-5_4-9999.ebuild",
2119 ),
2120 os.path.join(
2121 package_root, "chromeos-kernel-5_4", "Manifest"
2122 ),
2123 ],
2124 ),
2125 uprev_lib.UprevVersionedPackageModifications(
2126 new_version="R107-67890.0-0123456789",
2127 files=[
2128 os.path.join(
2129 package_root,
2130 "chromeos-kernel-5_15",
2131 "chromeos-kernel-5_15-9999.ebuild",
2132 ),
2133 os.path.join(
2134 package_root, "chromeos-kernel-5_15", "Manifest"
2135 ),
2136 ],
2137 ),
2138 ]
2139 self.assertTrue(returned_output.uprevved)
2140 self.assertEqual(returned_output.modified, expect_result)
2141
2142 def test_uprev_kernel_afdo_empty_json(self):
2143 """Test kernel afdo version unchanged."""
2144 json_files = {
2145 "kernel_afdo.json": "{}",
2146 "kernel_arm_afdo.json": "{}",
2147 }
2148 for f, contents in json_files.items():
2149 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2150
Brian Norrisf0c68572023-07-12 17:32:20 -07002151 returned_output = packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002152 self.assertFalse(returned_output.uprevved)
2153
2154 def test_uprev_kernel_afdo_empty_file(self):
2155 """Test malformed json raises."""
2156 json_files = {
2157 "kernel_afdo.json": "",
2158 "kernel_arm_afdo.json": "",
2159 }
2160 for f, contents in json_files.items():
2161 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2162
2163 with self.assertRaisesRegex(
2164 json.decoder.JSONDecodeError, "Expecting value"
2165 ):
Brian Norrisf0c68572023-07-12 17:32:20 -07002166 packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002167
2168 def test_uprev_kernel_afdo_manifest_raises(self):
2169 """Test manifest update raises."""
2170 json_files = {
2171 "kernel_afdo.json": (
2172 "{\n"
2173 ' "chromeos-kernel-5_4": {\n'
2174 ' "name": "R106-12345.0-0123456789"\n'
2175 " }\n"
2176 "}"
2177 ),
2178 }
2179 for f, contents in json_files.items():
2180 self.WriteTempFile(os.path.join(self.metadata_dir, f), contents)
2181 # run() raises exception.
2182 self.rc.SetDefaultCmdResult(
2183 side_effect=cros_build_lib.RunCommandError("error")
2184 )
2185
2186 with self.assertRaises(uprev_lib.EbuildManifestError):
Brian Norrisf0c68572023-07-12 17:32:20 -07002187 packages.uprev_kernel_afdo(None, [], Chroot())
Denis Nikitin63613e32022-09-09 22:26:50 -07002188
2189
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002190# TODO(chenghaoyang): Shouldn't use uprev_workon_ebuild_to_version.
2191class UprevPerfettoTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002192 """Tests for uprev_perfetto."""
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002193
Alex Klein1699fab2022-09-08 08:46:06 -06002194 def setUp(self):
2195 self.refs = [GitRef(path="/foo", ref="refs/tags/v12.0", revision="123")]
2196 self.MOCK_PERFETTO_EBUILD_PATH = "perfetto-12.0-r1.ebuild"
Chinglin Yufa728552023-04-13 03:12:04 +00002197 self.MOCK_PERFETTO_PROTO_EBUILD_PATH = "perfetto-protos-12.0-r1.ebuild"
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002198
Chinglin Yufa728552023-04-13 03:12:04 +00002199 def revisionBumpOutcome(self):
2200 return [
2201 uprev_lib.UprevResult(
2202 uprev_lib.Outcome.REVISION_BUMP,
2203 [self.MOCK_PERFETTO_EBUILD_PATH],
2204 ),
2205 uprev_lib.UprevResult(
2206 uprev_lib.Outcome.REVISION_BUMP,
2207 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2208 ),
2209 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002210
Chinglin Yufa728552023-04-13 03:12:04 +00002211 def majorBumpOutcome(self):
2212 return [
2213 uprev_lib.UprevResult(
2214 uprev_lib.Outcome.VERSION_BUMP, [self.MOCK_PERFETTO_EBUILD_PATH]
2215 ),
2216 uprev_lib.UprevResult(
2217 uprev_lib.Outcome.VERSION_BUMP,
2218 [self.MOCK_PERFETTO_PROTO_EBUILD_PATH],
2219 ),
2220 ]
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002221
Alex Klein1699fab2022-09-08 08:46:06 -06002222 def newerVersionOutcome(self):
2223 return uprev_lib.UprevResult(uprev_lib.Outcome.NEWER_VERSION_EXISTS)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002224
Alex Klein1699fab2022-09-08 08:46:06 -06002225 def sameVersionOutcome(self):
2226 return uprev_lib.UprevResult(uprev_lib.Outcome.SAME_VERSION_EXISTS)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002227
Alex Klein1699fab2022-09-08 08:46:06 -06002228 def test_latest_version_returns_none(self):
2229 """Test no refs were supplied"""
2230 output = packages.uprev_perfetto(None, [], None)
2231 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002232
Alex Klein1699fab2022-09-08 08:46:06 -06002233 def test_perfetto_uprev_fails(self):
2234 """Test a single ref is supplied."""
2235 self.PatchObject(
2236 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2237 )
2238 output = packages.uprev_perfetto(None, self.refs, None)
2239 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002240
Alex Klein1699fab2022-09-08 08:46:06 -06002241 def test_newer_version_exists(self):
2242 """Test the newer version exists uprev should not happen."""
2243 perfetto_outcome = self.newerVersionOutcome()
2244 self.PatchObject(
2245 uprev_lib,
2246 "uprev_workon_ebuild_to_version",
2247 side_effect=[perfetto_outcome],
2248 )
2249 output = packages.uprev_perfetto(None, self.refs, None)
2250 self.assertFalse(output.uprevved)
Harvey Yang3eee06c2021-03-18 15:47:56 +08002251
Alex Klein1699fab2022-09-08 08:46:06 -06002252 def test_same_version_exists(self):
2253 """Test the same version exists uprev should not happen."""
2254 perfetto_outcome = self.sameVersionOutcome()
2255 self.PatchObject(
2256 uprev_lib,
2257 "uprev_workon_ebuild_to_version",
2258 side_effect=[perfetto_outcome],
2259 )
2260 output = packages.uprev_perfetto(None, self.refs, None)
2261 self.assertFalse(output.uprevved)
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002262
Alex Klein1699fab2022-09-08 08:46:06 -06002263 def test_revision_bump_perfetto_package(self):
2264 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002265 self.PatchObject(
2266 uprev_lib,
2267 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002268 side_effect=self.revisionBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002269 )
2270 output = packages.uprev_perfetto(None, self.refs, None)
2271 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002272 self.assertEqual(
2273 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2274 )
2275 self.assertEqual(
2276 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2277 )
Harvey Yang9c61e9c2021-03-02 16:32:43 +08002278
Alex Klein1699fab2022-09-08 08:46:06 -06002279 def test_major_bump_perfetto_package(self):
2280 """Test perfetto package uprev."""
Alex Klein1699fab2022-09-08 08:46:06 -06002281 self.PatchObject(
2282 uprev_lib,
2283 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002284 side_effect=self.majorBumpOutcome(),
Alex Klein1699fab2022-09-08 08:46:06 -06002285 )
2286 output = packages.uprev_perfetto(None, self.refs, None)
2287 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002288 self.assertEqual(
2289 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2290 )
2291 self.assertEqual(
2292 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2293 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002294
Chinglin Yuad12a512022-10-07 17:26:12 +08002295 def test_revision_bump_trunk(self):
2296 """Test revision bump on receiving non-versioned trunk refs."""
Chinglin Yu5de28a42022-11-11 19:52:21 +08002297 refs = [
2298 GitRef(
2299 path="/foo", ref="refs/heads/main", revision="0123456789abcdef"
2300 )
2301 ]
Chinglin Yuad12a512022-10-07 17:26:12 +08002302 self.PatchObject(
2303 uprev_lib, "get_stable_ebuild_version", return_value="12.0"
2304 )
2305 self.PatchObject(
2306 uprev_lib,
2307 "uprev_workon_ebuild_to_version",
Chinglin Yufa728552023-04-13 03:12:04 +00002308 side_effect=self.revisionBumpOutcome(),
Chinglin Yuad12a512022-10-07 17:26:12 +08002309 )
2310 output = packages.uprev_perfetto(None, refs, None)
Chinglin Yufa728552023-04-13 03:12:04 +00002311
Chinglin Yuad12a512022-10-07 17:26:12 +08002312 self.assertTrue(output.uprevved)
Chinglin Yufa728552023-04-13 03:12:04 +00002313 self.assertEqual(
2314 output.modified[0].files, [self.MOCK_PERFETTO_EBUILD_PATH]
2315 )
Chinglin Yu5de28a42022-11-11 19:52:21 +08002316 self.assertEqual(output.modified[0].new_version, "12.0-012345678")
Chinglin Yufa728552023-04-13 03:12:04 +00002317 self.assertEqual(
2318 output.modified[1].files, [self.MOCK_PERFETTO_PROTO_EBUILD_PATH]
2319 )
2320 self.assertEqual(output.modified[1].new_version, "12.0-012345678")
Chinglin Yuad12a512022-10-07 17:26:12 +08002321
Alex Klein627e04c2021-11-10 15:56:47 -07002322
Julio Hurtadof1befec2021-05-05 21:34:26 +00002323class UprevLacrosTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002324 """Tests for uprev_lacros"""
Julio Hurtadof1befec2021-05-05 21:34:26 +00002325
Alex Klein1699fab2022-09-08 08:46:06 -06002326 def setUp(self):
2327 self.refs = [
2328 GitRef(
2329 path="/lacros", ref="refs/heads/main", revision="123.456.789.0"
2330 )
2331 ]
2332 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtadof1befec2021-05-05 21:34:26 +00002333
Alex Klein1699fab2022-09-08 08:46:06 -06002334 def revisionBumpOutcome(self, ebuild_path):
2335 return uprev_lib.UprevResult(
2336 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2337 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002338
Alex Klein1699fab2022-09-08 08:46:06 -06002339 def majorBumpOutcome(self, ebuild_path):
2340 return uprev_lib.UprevResult(
2341 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2342 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002343
Alex Klein1699fab2022-09-08 08:46:06 -06002344 def newerVersionOutcome(self, ebuild_path):
2345 return uprev_lib.UprevResult(
2346 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2347 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002348
Alex Klein1699fab2022-09-08 08:46:06 -06002349 def sameVersionOutcome(self, ebuild_path):
2350 return uprev_lib.UprevResult(
2351 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2352 )
Julio Hurtadoa994e002021-07-07 17:57:45 +00002353
Alex Klein1699fab2022-09-08 08:46:06 -06002354 def newEbuildCreatedOutcome(self, ebuild_path):
2355 return uprev_lib.UprevResult(
2356 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2357 )
Julio Hurtadof1befec2021-05-05 21:34:26 +00002358
Alex Klein1699fab2022-09-08 08:46:06 -06002359 def test_lacros_uprev_fails(self):
2360 """Test a lacros package uprev with no triggers"""
2361 self.PatchObject(
2362 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2363 )
2364 with self.assertRaises(IndexError):
2365 packages.uprev_lacros(None, [], None)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002366
Alex Klein1699fab2022-09-08 08:46:06 -06002367 def test_lacros_uprev_revision_bump(self):
2368 """Test lacros package uprev."""
2369 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2370 self.PatchObject(
2371 uprev_lib,
2372 "uprev_workon_ebuild_to_version",
2373 side_effect=[lacros_outcome],
2374 )
2375 output = packages.uprev_lacros(None, self.refs, None)
2376 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002377
Alex Klein1699fab2022-09-08 08:46:06 -06002378 def test_lacros_uprev_version_bump(self):
2379 """Test lacros package uprev."""
2380 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2381 self.PatchObject(
2382 uprev_lib,
2383 "uprev_workon_ebuild_to_version",
2384 side_effect=[lacros_outcome],
2385 )
2386 output = packages.uprev_lacros(None, self.refs, None)
2387 self.assertTrue(output.uprevved)
Julio Hurtadof1befec2021-05-05 21:34:26 +00002388
Alex Klein1699fab2022-09-08 08:46:06 -06002389 def test_lacros_uprev_new_ebuild_created(self):
2390 """Test lacros package uprev."""
2391 lacros_outcome = self.newEbuildCreatedOutcome(
2392 self.MOCK_LACROS_EBUILD_PATH
2393 )
2394 self.PatchObject(
2395 uprev_lib,
2396 "uprev_workon_ebuild_to_version",
2397 side_effect=[lacros_outcome],
2398 )
2399 output = packages.uprev_lacros(None, self.refs, None)
2400 self.assertTrue(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002401
Alex Klein1699fab2022-09-08 08:46:06 -06002402 def test_lacros_uprev_newer_version_exist(self):
2403 """Test the newer version exists uprev should not happen."""
2404 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2405 self.PatchObject(
2406 uprev_lib,
2407 "uprev_workon_ebuild_to_version",
2408 side_effect=[lacros_outcome],
2409 )
2410 output = packages.uprev_lacros(None, self.refs, None)
2411 self.assertFalse(output.uprevved)
Julio Hurtadoa994e002021-07-07 17:57:45 +00002412
Alex Klein1699fab2022-09-08 08:46:06 -06002413 def test_lacros_uprev_same_version_exist(self):
2414 """Test the same version exists uprev should not happen."""
2415 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2416 self.PatchObject(
2417 uprev_lib,
2418 "uprev_workon_ebuild_to_version",
2419 side_effect=[lacros_outcome],
2420 )
2421 output = packages.uprev_lacros(None, self.refs, None)
2422 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002423
2424
2425class UprevLacrosInParallelTest(cros_test_lib.MockTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -06002426 """Tests for uprev_lacros"""
Julio Hurtado870ed322021-12-03 18:22:40 +00002427
Alex Klein1699fab2022-09-08 08:46:06 -06002428 def setUp(self):
2429 self.refs = [
2430 GitRef(
2431 path="/lacros", revision="abc123", ref="refs/tags/123.456.789.0"
2432 )
2433 ]
2434 self.MOCK_LACROS_EBUILD_PATH = "chromeos-lacros-123.456.789.0-r1.ebuild"
Julio Hurtado870ed322021-12-03 18:22:40 +00002435
Alex Klein1699fab2022-09-08 08:46:06 -06002436 def revisionBumpOutcome(self, ebuild_path):
2437 return uprev_lib.UprevResult(
2438 uprev_lib.Outcome.REVISION_BUMP, [ebuild_path]
2439 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002440
Alex Klein1699fab2022-09-08 08:46:06 -06002441 def majorBumpOutcome(self, ebuild_path):
2442 return uprev_lib.UprevResult(
2443 uprev_lib.Outcome.VERSION_BUMP, [ebuild_path]
2444 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002445
Alex Klein1699fab2022-09-08 08:46:06 -06002446 def newerVersionOutcome(self, ebuild_path):
2447 return uprev_lib.UprevResult(
2448 uprev_lib.Outcome.NEWER_VERSION_EXISTS, [ebuild_path]
2449 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002450
Alex Klein1699fab2022-09-08 08:46:06 -06002451 def sameVersionOutcome(self, ebuild_path):
2452 return uprev_lib.UprevResult(
2453 uprev_lib.Outcome.SAME_VERSION_EXISTS, [ebuild_path]
2454 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002455
Alex Klein1699fab2022-09-08 08:46:06 -06002456 def newEbuildCreatedOutcome(self, ebuild_path):
2457 return uprev_lib.UprevResult(
2458 uprev_lib.Outcome.NEW_EBUILD_CREATED, [ebuild_path]
2459 )
Julio Hurtado870ed322021-12-03 18:22:40 +00002460
Alex Klein1699fab2022-09-08 08:46:06 -06002461 def test_lacros_uprev_fails(self):
2462 """Test a lacros package uprev with no triggers"""
2463 self.PatchObject(
2464 uprev_lib, "uprev_workon_ebuild_to_version", side_effect=[None]
2465 )
Alex Klein314fb5d2022-10-24 14:56:31 -06002466 with self.assertRaises(uprev_lib.NoRefsError):
Alex Klein1699fab2022-09-08 08:46:06 -06002467 packages.uprev_lacros_in_parallel(None, [], None)
Julio Hurtado870ed322021-12-03 18:22:40 +00002468
Alex Klein1699fab2022-09-08 08:46:06 -06002469 def test_lacros_uprev_revision_bump(self):
2470 """Test lacros package uprev."""
2471 lacros_outcome = self.revisionBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2472 self.PatchObject(
2473 uprev_lib,
2474 "uprev_workon_ebuild_to_version",
2475 side_effect=[lacros_outcome],
2476 )
2477 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2478 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002479
Alex Klein1699fab2022-09-08 08:46:06 -06002480 def test_lacros_uprev_version_bump(self):
2481 """Test lacros package uprev."""
2482 lacros_outcome = self.majorBumpOutcome(self.MOCK_LACROS_EBUILD_PATH)
2483 self.PatchObject(
2484 uprev_lib,
2485 "uprev_workon_ebuild_to_version",
2486 side_effect=[lacros_outcome],
2487 )
2488 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2489 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002490
Alex Klein1699fab2022-09-08 08:46:06 -06002491 def test_lacros_uprev_new_ebuild_created(self):
2492 """Test lacros package uprev."""
2493 lacros_outcome = self.newEbuildCreatedOutcome(
2494 self.MOCK_LACROS_EBUILD_PATH
2495 )
2496 self.PatchObject(
2497 uprev_lib,
2498 "uprev_workon_ebuild_to_version",
2499 side_effect=[lacros_outcome],
2500 )
2501 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2502 self.assertTrue(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002503
Alex Klein1699fab2022-09-08 08:46:06 -06002504 def test_lacros_uprev_newer_version_exist(self):
2505 """Test the newer version exists uprev should not happen."""
2506 lacros_outcome = self.newerVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2507 self.PatchObject(
2508 uprev_lib,
2509 "uprev_workon_ebuild_to_version",
2510 side_effect=[lacros_outcome],
2511 )
2512 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2513 self.assertFalse(output.uprevved)
Julio Hurtado870ed322021-12-03 18:22:40 +00002514
Alex Klein1699fab2022-09-08 08:46:06 -06002515 def test_lacros_uprev_same_version_exist(self):
2516 """Test the same version exists uprev should not happen."""
2517 lacros_outcome = self.sameVersionOutcome(self.MOCK_LACROS_EBUILD_PATH)
2518 self.PatchObject(
2519 uprev_lib,
2520 "uprev_workon_ebuild_to_version",
2521 side_effect=[lacros_outcome],
2522 )
2523 output = packages.uprev_lacros_in_parallel(None, self.refs, None)
2524 self.assertFalse(output.uprevved)