blob: 1fa20f307d0a62b1063e8815f6a1394d299d4cb0 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2015 The ChromiumOS Authors
David Pursell9476bf42015-03-30 13:34:27 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unit tests for the deploy module."""
6
David Pursell9476bf42015-03-30 13:34:27 -07007import json
Ralph Nathane01ccf12015-04-16 10:40:32 -07008import multiprocessing
David Pursell9476bf42015-03-30 13:34:27 -07009import os
Mike Frysinger050fa5a2019-12-01 16:16:29 -050010import sys
Mike Frysinger166fea02021-02-12 05:30:33 -050011from unittest import mock
David Pursell9476bf42015-03-30 13:34:27 -070012
Ralph Nathane01ccf12015-04-16 10:40:32 -070013from chromite.cli import command
David Pursell9476bf42015-03-30 13:34:27 -070014from chromite.cli import deploy
Mike Frysinger06a51c82021-04-06 11:39:17 -040015from chromite.lib import build_target_lib
David Pursell9476bf42015-03-30 13:34:27 -070016from chromite.lib import cros_build_lib
17from chromite.lib import cros_test_lib
Jae Hoon Kimdf220852023-04-14 19:20:13 +000018from chromite.lib import dlc_lib
Brian Norris2eee8892021-04-06 16:23:23 -070019from chromite.lib import osutils
Ralph Nathane01ccf12015-04-16 10:40:32 -070020from chromite.lib import remote_access
Brian Norris2eee8892021-04-06 16:23:23 -070021from chromite.lib import sysroot_lib
Sloan Johnsona85640f2021-10-01 22:32:40 +000022from chromite.lib import unittest_lib
Alex Klein18a60af2020-06-11 12:08:47 -060023from chromite.lib.parser import package_info
Alex Klein32223fd2023-08-22 11:19:25 -060024from chromite.utils import os_util
Mike Frysinger40ffb532021-02-12 07:36:08 -050025
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070026
Chris McDonald186b9ee2020-04-16 05:56:49 -060027pytestmark = [cros_test_lib.pytestmark_inside_only]
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070028
29
30if cros_build_lib.IsInsideChroot():
Alex Klein1699fab2022-09-08 08:46:06 -060031 import portage # pylint: disable=import-error
David Pursell9476bf42015-03-30 13:34:27 -070032
33
34# pylint: disable=protected-access
35
36
Jae Hoon Kimdf220852023-04-14 19:20:13 +000037# Example DLC LoadPin digests to test with.
38LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS = """# LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS
3975a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431
40"""
41
42# An example LoadPin digest.
43DLC_LOADPIN_DIGEST = (
44 "feeddeadc0de0000000000000000000000000000000000000000000000000000"
45)
46
47
Alex Klein074f94f2023-06-22 10:32:06 -060048class ChromiumOSDeviceFake:
Alex Klein1699fab2022-09-08 08:46:06 -060049 """Fake for device."""
Ralph Nathane01ccf12015-04-16 10:40:32 -070050
Alex Klein1699fab2022-09-08 08:46:06 -060051 def __init__(self):
52 self.board = "board"
53 self.hostname = None
54 self.username = None
55 self.port = None
56 self.lsb_release = None
57 self.cmds = []
58 self.work_dir = "/testdir/"
59 self.selinux_available = False
Jae Hoon Kimdf220852023-04-14 19:20:13 +000060 self.copy_store = None
61 self.cat_file_output = ""
Yuanpeng Ni81c944a2023-06-10 17:48:05 -070062 self.cmd_disallowed = []
Ralph Nathane01ccf12015-04-16 10:40:32 -070063
Alex Klein1699fab2022-09-08 08:46:06 -060064 def MountRootfsReadWrite(self):
65 return True
Ralph Nathane01ccf12015-04-16 10:40:32 -070066
Alex Klein1699fab2022-09-08 08:46:06 -060067 def IsSELinuxAvailable(self):
68 return self.selinux_available
Ben Pastene5f03b052019-08-12 18:03:24 -070069
Alex Klein1699fab2022-09-08 08:46:06 -060070 def IsSELinuxEnforced(self):
71 return True
Ben Pastene5f03b052019-08-12 18:03:24 -070072
Mike Frysinger445f6bf2023-06-27 11:43:33 -040073 def mkdir(self, _path):
74 return None
75
Alex Klein1699fab2022-09-08 08:46:06 -060076 def run(self, cmd, **_kwargs):
Yuanpeng Ni81c944a2023-06-10 17:48:05 -070077 if cmd in self.cmd_disallowed:
78 raise cros_build_lib.RunCommandError("Command disallowed")
79 else:
80 self.cmds.append(cmd)
Andrewc7e1c6b2020-02-27 16:03:53 -080081
Alex Klein1699fab2022-09-08 08:46:06 -060082 def CopyToDevice(self, _src, _dest, _mode="rsync", **_kwargs):
Jae Hoon Kimdf220852023-04-14 19:20:13 +000083 if os.path.exists(_src):
84 self.copy_store = osutils.ReadFile(_src)
Alex Klein1699fab2022-09-08 08:46:06 -060085 return True
Andrewc7e1c6b2020-02-27 16:03:53 -080086
Jae Hoon Kimdf220852023-04-14 19:20:13 +000087 def CatFile(self, _src):
88 return self.cat_file_output
89
Ralph Nathane01ccf12015-04-16 10:40:32 -070090
Alex Klein074f94f2023-06-22 10:32:06 -060091class ChromiumOSDeviceHandlerFake:
Alex Klein1699fab2022-09-08 08:46:06 -060092 """Fake for chromite.lib.remote_access.ChomiumOSDeviceHandler."""
David Pursell9476bf42015-03-30 13:34:27 -070093
Alex Klein074f94f2023-06-22 10:32:06 -060094 class RemoteAccessFake:
Alex Klein1699fab2022-09-08 08:46:06 -060095 """Fake for chromite.lib.remote_access.RemoteAccess."""
David Pursell9476bf42015-03-30 13:34:27 -070096
Alex Klein1699fab2022-09-08 08:46:06 -060097 def __init__(self):
98 self.remote_sh_output = None
David Pursell9476bf42015-03-30 13:34:27 -070099
Alex Klein1699fab2022-09-08 08:46:06 -0600100 def RemoteSh(self, *_args, **_kwargs):
101 return cros_build_lib.CompletedProcess(stdout=self.remote_sh_output)
David Pursell9476bf42015-03-30 13:34:27 -0700102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 def __init__(self, *_args, **_kwargs):
104 self._agent = self.RemoteAccessFake()
105 self.device = ChromiumOSDeviceFake()
David Pursell67a82762015-04-30 17:26:59 -0700106
Mike Frysingerc0780a62022-08-29 04:41:56 -0400107 @property
108 def agent(self):
Alex Klein1699fab2022-09-08 08:46:06 -0600109 return self._agent
David Pursell9476bf42015-03-30 13:34:27 -0700110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 def __exit__(self, _type, _value, _traceback):
112 pass
Ralph Nathane01ccf12015-04-16 10:40:32 -0700113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 def __enter__(self):
115 return self.device
Ralph Nathane01ccf12015-04-16 10:40:32 -0700116
117
118class BrilloDeployOperationFake(deploy.BrilloDeployOperation):
Alex Klein1699fab2022-09-08 08:46:06 -0600119 """Fake for deploy.BrilloDeployOperation."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700120
Alex Klein1699fab2022-09-08 08:46:06 -0600121 def __init__(self, emerge, queue):
122 super().__init__(emerge)
123 self._queue = queue
124
125 def ParseOutput(self, output=None):
126 super().ParseOutput(output)
127 self._queue.put("advance")
Ralph Nathane01ccf12015-04-16 10:40:32 -0700128
David Pursell9476bf42015-03-30 13:34:27 -0700129
Alex Klein074f94f2023-06-22 10:32:06 -0600130class DbApiFake:
Alex Klein1699fab2022-09-08 08:46:06 -0600131 """Fake for Portage dbapi."""
David Pursell9476bf42015-03-30 13:34:27 -0700132
Alex Klein1699fab2022-09-08 08:46:06 -0600133 def __init__(self, pkgs):
134 self.pkg_db = {}
Tim Baine4a783b2023-04-21 20:05:51 +0000135 for cpv, slot, rdeps_raw, build_time, use in pkgs:
Alex Klein1699fab2022-09-08 08:46:06 -0600136 self.pkg_db[cpv] = {
137 "SLOT": slot,
138 "RDEPEND": rdeps_raw,
139 "BUILD_TIME": build_time,
Tim Baine4a783b2023-04-21 20:05:51 +0000140 "USE": use,
Alex Klein1699fab2022-09-08 08:46:06 -0600141 }
David Pursell9476bf42015-03-30 13:34:27 -0700142
Alex Klein1699fab2022-09-08 08:46:06 -0600143 def cpv_all(self):
144 return list(self.pkg_db)
David Pursell9476bf42015-03-30 13:34:27 -0700145
Alex Klein1699fab2022-09-08 08:46:06 -0600146 def aux_get(self, cpv, keys):
147 pkg_info = self.pkg_db[cpv]
148 return [pkg_info[key] for key in keys]
David Pursell9476bf42015-03-30 13:34:27 -0700149
150
Alex Klein074f94f2023-06-22 10:32:06 -0600151class PackageScannerFake:
Alex Klein1699fab2022-09-08 08:46:06 -0600152 """Fake for PackageScanner."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700153
Alex Klein1699fab2022-09-08 08:46:06 -0600154 def __init__(self, packages, pkgs_attrs, packages_cpvs=None):
155 self.pkgs = packages
156 self.cpvs = packages_cpvs or packages
157 self.listed = []
158 self.num_updates = 0
159 self.pkgs_attrs = pkgs_attrs
Tim Baine4a783b2023-04-21 20:05:51 +0000160 self.warnings_shown = False
Ralph Nathane01ccf12015-04-16 10:40:32 -0700161
Alex Klein1699fab2022-09-08 08:46:06 -0600162 def Run(self, _device, _root, _packages, _update, _deep, _deep_rev):
Tim Baine4a783b2023-04-21 20:05:51 +0000163 return (
164 self.cpvs,
165 self.listed,
166 self.num_updates,
167 self.pkgs_attrs,
168 self.warnings_shown,
169 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700170
171
Alex Klein074f94f2023-06-22 10:32:06 -0600172class PortageTreeFake:
Alex Klein1699fab2022-09-08 08:46:06 -0600173 """Fake for Portage tree."""
David Pursell9476bf42015-03-30 13:34:27 -0700174
Alex Klein1699fab2022-09-08 08:46:06 -0600175 def __init__(self, dbapi):
176 self.dbapi = dbapi
David Pursell9476bf42015-03-30 13:34:27 -0700177
178
Ralph Nathane01ccf12015-04-16 10:40:32 -0700179class TestInstallPackageScanner(cros_test_lib.MockOutputTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600180 """Test the update package scanner."""
David Pursell9476bf42015-03-30 13:34:27 -0700181
Alex Klein1699fab2022-09-08 08:46:06 -0600182 _BOARD = "foo_board"
183 _BUILD_ROOT = "/build/%s" % _BOARD
184 _VARTREE = [
Tim Baine4a783b2023-04-21 20:05:51 +0000185 (
186 "foo/app1-1.2.3-r4",
187 "0",
188 "foo/app2 !foo/app3",
189 "1413309336",
190 "cros-debug",
191 ),
192 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
193 (
194 "foo/app4-2.0.0-r1",
195 "0",
196 "foo/app1 foo/app5",
197 "1413309336",
198 "cros-debug",
199 ),
200 ("foo/app5-3.0.7-r3", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600201 ]
David Pursell9476bf42015-03-30 13:34:27 -0700202
Alex Klein1699fab2022-09-08 08:46:06 -0600203 def setUp(self):
204 """Patch imported modules."""
205 self.PatchObject(cros_build_lib, "GetChoice", return_value=0)
206 self.device = ChromiumOSDeviceHandlerFake()
207 self.scanner = deploy._InstallPackageScanner(self._BUILD_ROOT)
208 self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None))
Tim Baine4a783b2023-04-21 20:05:51 +0000209 self.PatchObject(
210 deploy, "_ConfirmUpdateDespiteWarnings", return_value=True
211 )
David Pursell9476bf42015-03-30 13:34:27 -0700212
Alex Klein1699fab2022-09-08 08:46:06 -0600213 def SetupVartree(self, vartree_pkgs):
Jack Rosenthal2aff1af2023-07-13 18:34:28 -0600214 self.PatchObject(
215 self.scanner,
216 "_get_portage_interpreter",
217 return_value="FAKE_PYTHON",
218 )
Mike Frysingerc0780a62022-08-29 04:41:56 -0400219 self.device.agent.remote_sh_output = json.dumps(vartree_pkgs)
David Pursell9476bf42015-03-30 13:34:27 -0700220
Alex Klein1699fab2022-09-08 08:46:06 -0600221 def SetupBintree(self, bintree_pkgs):
222 bintree = PortageTreeFake(DbApiFake(bintree_pkgs))
223 build_root = os.path.join(self._BUILD_ROOT, "")
224 portage_db = {build_root: {"bintree": bintree}}
225 self.PatchObject(portage, "create_trees", return_value=portage_db)
David Pursell9476bf42015-03-30 13:34:27 -0700226
Alex Klein1699fab2022-09-08 08:46:06 -0600227 def ValidatePkgs(self, actual, expected, constraints=None):
228 # Containing exactly the same packages.
229 self.assertEqual(sorted(expected), sorted(actual))
230 # Packages appear in the right order.
231 if constraints is not None:
232 for needs, needed in constraints:
233 self.assertGreater(actual.index(needs), actual.index(needed))
David Pursell9476bf42015-03-30 13:34:27 -0700234
Alex Klein1699fab2022-09-08 08:46:06 -0600235 def testRunUpdatedVersion(self):
236 self.SetupVartree(self._VARTREE)
237 app1 = "foo/app1-1.2.5-r4"
238 self.SetupBintree(
239 [
Tim Baine4a783b2023-04-21 20:05:51 +0000240 (app1, "0", "foo/app2 !foo/app3", "1413309336", "cros-debug"),
241 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600242 ]
243 )
Tim Baine4a783b2023-04-21 20:05:51 +0000244 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600245 self.device, "/", ["app1"], True, True, True
246 )
247 self.ValidatePkgs(installs, [app1])
248 self.ValidatePkgs(listed, [app1])
249 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700250
Tim Baine4a783b2023-04-21 20:05:51 +0000251 def testRunUpdatedVersionWithUseMismatch(self):
252 self.SetupVartree(self._VARTREE)
253 app1 = "foo/app1-1.2.5-r4"
254 # Setup the bintree with packages that don't have USE=cros-debug.
255 self.SetupBintree(
256 [
257 (app1, "0", "foo/app2 !foo/app3", "1413309336", ""),
258 ("foo/app2-4.5.6-r7", "0", "", "1413309336", ""),
259 ]
260 )
261 with self.assertLogs(level="WARN") as cm:
262 installs, listed, num_updates, _, _ = self.scanner.Run(
263 self.device, "/", ["app1"], True, True, True
264 )
265 self.ValidatePkgs(installs, [app1])
266 self.ValidatePkgs(listed, [app1])
267 self.assertEqual(num_updates, 1)
268 testline = "USE flags for package foo/app1 do not match"
269 matching_logs = [
270 logline for logline in cm.output if testline in logline
271 ]
272 self.assertTrue(
273 matching_logs, "Failed to detect USE flag mismatch."
274 )
275
Alex Klein1699fab2022-09-08 08:46:06 -0600276 def testRunUpdatedBuildTime(self):
277 self.SetupVartree(self._VARTREE)
278 app1 = "foo/app1-1.2.3-r4"
279 self.SetupBintree(
280 [
Tim Baine4a783b2023-04-21 20:05:51 +0000281 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
282 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600283 ]
284 )
Tim Baine4a783b2023-04-21 20:05:51 +0000285 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600286 self.device, "/", ["app1"], True, True, True
287 )
288 self.ValidatePkgs(installs, [app1])
289 self.ValidatePkgs(listed, [app1])
290 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700291
Alex Klein1699fab2022-09-08 08:46:06 -0600292 def testRunExistingDepUpdated(self):
293 self.SetupVartree(self._VARTREE)
294 app1 = "foo/app1-1.2.5-r2"
295 app2 = "foo/app2-4.5.8-r3"
296 self.SetupBintree(
297 [
Tim Baine4a783b2023-04-21 20:05:51 +0000298 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
299 (app2, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600300 ]
301 )
Tim Baine4a783b2023-04-21 20:05:51 +0000302 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600303 self.device, "/", ["app1"], True, True, True
304 )
305 self.ValidatePkgs(installs, [app1, app2], constraints=[(app1, app2)])
306 self.ValidatePkgs(listed, [app1])
307 self.assertEqual(num_updates, 2)
David Pursell9476bf42015-03-30 13:34:27 -0700308
Alex Klein1699fab2022-09-08 08:46:06 -0600309 def testRunMissingDepUpdated(self):
310 self.SetupVartree(self._VARTREE)
311 app1 = "foo/app1-1.2.5-r2"
312 app6 = "foo/app6-1.0.0-r1"
313 self.SetupBintree(
314 [
Tim Baine4a783b2023-04-21 20:05:51 +0000315 (
316 app1,
317 "0",
318 "foo/app2 !foo/app3 foo/app6",
319 "1413309350",
320 "cros-debug",
321 ),
322 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
323 (app6, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600324 ]
325 )
Tim Baine4a783b2023-04-21 20:05:51 +0000326 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600327 self.device, "/", ["app1"], True, True, True
328 )
329 self.ValidatePkgs(installs, [app1, app6], constraints=[(app1, app6)])
330 self.ValidatePkgs(listed, [app1])
331 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700332
Alex Klein1699fab2022-09-08 08:46:06 -0600333 def testRunExistingRevDepUpdated(self):
334 self.SetupVartree(self._VARTREE)
335 app1 = "foo/app1-1.2.5-r2"
336 app4 = "foo/app4-2.0.1-r3"
337 self.SetupBintree(
338 [
Tim Baine4a783b2023-04-21 20:05:51 +0000339 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
340 (app4, "0", "foo/app1 foo/app5", "1413309350", "cros-debug"),
341 ("foo/app5-3.0.7-r3", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600342 ]
343 )
Tim Baine4a783b2023-04-21 20:05:51 +0000344 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600345 self.device, "/", ["app1"], True, True, True
346 )
347 self.ValidatePkgs(installs, [app1, app4], constraints=[(app4, app1)])
348 self.ValidatePkgs(listed, [app1])
349 self.assertEqual(num_updates, 2)
David Pursell9476bf42015-03-30 13:34:27 -0700350
Alex Klein1699fab2022-09-08 08:46:06 -0600351 def testRunMissingRevDepNotUpdated(self):
352 self.SetupVartree(self._VARTREE)
353 app1 = "foo/app1-1.2.5-r2"
354 app6 = "foo/app6-1.0.0-r1"
355 self.SetupBintree(
356 [
Tim Baine4a783b2023-04-21 20:05:51 +0000357 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
358 (app6, "0", "foo/app1", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600359 ]
360 )
Tim Baine4a783b2023-04-21 20:05:51 +0000361 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600362 self.device, "/", ["app1"], True, True, True
363 )
364 self.ValidatePkgs(installs, [app1])
365 self.ValidatePkgs(listed, [app1])
366 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700367
Alex Klein1699fab2022-09-08 08:46:06 -0600368 def testRunTransitiveDepsUpdated(self):
369 self.SetupVartree(self._VARTREE)
370 app1 = "foo/app1-1.2.5-r2"
371 app2 = "foo/app2-4.5.8-r3"
372 app4 = "foo/app4-2.0.0-r1"
373 app5 = "foo/app5-3.0.8-r2"
374 self.SetupBintree(
375 [
Tim Baine4a783b2023-04-21 20:05:51 +0000376 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
377 (app2, "0", "", "1413309350", "cros-debug"),
378 (app4, "0", "foo/app1 foo/app5", "1413309350", "cros-debug"),
379 (app5, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600380 ]
381 )
Tim Baine4a783b2023-04-21 20:05:51 +0000382 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600383 self.device, "/", ["app1"], True, True, True
384 )
385 self.ValidatePkgs(
386 installs,
387 [app1, app2, app4, app5],
388 constraints=[(app1, app2), (app4, app1), (app4, app5)],
389 )
390 self.ValidatePkgs(listed, [app1])
391 self.assertEqual(num_updates, 4)
David Pursell9476bf42015-03-30 13:34:27 -0700392
Alex Klein1699fab2022-09-08 08:46:06 -0600393 def testRunDisjunctiveDepsExistingUpdated(self):
394 self.SetupVartree(self._VARTREE)
395 app1 = "foo/app1-1.2.5-r2"
396 self.SetupBintree(
397 [
Tim Baine4a783b2023-04-21 20:05:51 +0000398 (
399 app1,
400 "0",
401 "|| ( foo/app6 foo/app2 ) !foo/app3",
402 "1413309350",
403 "cros-debug",
404 ),
405 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600406 ]
407 )
Tim Baine4a783b2023-04-21 20:05:51 +0000408 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600409 self.device, "/", ["app1"], True, True, True
410 )
411 self.ValidatePkgs(installs, [app1])
412 self.ValidatePkgs(listed, [app1])
413 self.assertEqual(num_updates, 1)
414
415 def testRunDisjunctiveDepsDefaultUpdated(self):
416 self.SetupVartree(self._VARTREE)
417 app1 = "foo/app1-1.2.5-r2"
418 app7 = "foo/app7-1.0.0-r1"
419 self.SetupBintree(
420 [
Tim Baine4a783b2023-04-21 20:05:51 +0000421 (
422 app1,
423 "0",
424 "|| ( foo/app6 foo/app7 ) !foo/app3",
425 "1413309350",
426 "cros-debug",
427 ),
428 (app7, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600429 ]
430 )
Tim Baine4a783b2023-04-21 20:05:51 +0000431 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600432 self.device, "/", ["app1"], True, True, True
433 )
434 self.ValidatePkgs(installs, [app1, app7], constraints=[(app1, app7)])
435 self.ValidatePkgs(listed, [app1])
436 self.assertEqual(num_updates, 1)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700437
Jack Rosenthal2aff1af2023-07-13 18:34:28 -0600438 def test_get_portage_interpreter(self):
439 """Test getting the portage interpreter from the device."""
440 self.device.agent.remote_sh_output = """\
441/usr/lib/python-exec/python3.6/emerge
442/usr/lib/python-exec/python3.8/emerge
443/usr/lib/python-exec/python3.11/emerge
444"""
445 self.assertEqual(
446 self.scanner._get_portage_interpreter(self.device),
447 "python3.11",
448 )
449
Ralph Nathane01ccf12015-04-16 10:40:32 -0700450
Alex Klein1699fab2022-09-08 08:46:06 -0600451class TestDeploy(
452 cros_test_lib.ProgressBarTestCase, cros_test_lib.MockTempDirTestCase
453):
454 """Test deploy.Deploy."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700455
Alex Klein1699fab2022-09-08 08:46:06 -0600456 @staticmethod
457 def FakeGetPackagesByCPV(cpvs, _strip, _sysroot):
458 return ["/path/to/%s.tbz2" % cpv.pv for cpv in cpvs]
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700459
Alex Klein1699fab2022-09-08 08:46:06 -0600460 def setUp(self):
461 # Fake being root to avoid running filesystem commands with sudo_run.
Alex Klein32223fd2023-08-22 11:19:25 -0600462 self.PatchObject(os_util, "is_root_user", return_value=True)
Alex Klein1699fab2022-09-08 08:46:06 -0600463 self._sysroot = os.path.join(self.tempdir, "sysroot")
464 osutils.SafeMakedirs(self._sysroot)
465 self.device = ChromiumOSDeviceHandlerFake()
466 self.PatchObject(
467 remote_access, "ChromiumOSDeviceHandler", return_value=self.device
468 )
469 self.PatchObject(cros_build_lib, "GetBoard", return_value=None)
470 self.PatchObject(
471 build_target_lib,
472 "get_default_sysroot_path",
473 return_value=self._sysroot,
474 )
475 self.package_scanner = self.PatchObject(
476 deploy, "_InstallPackageScanner"
477 )
478 self.get_packages_paths = self.PatchObject(
479 deploy, "_GetPackagesByCPV", side_effect=self.FakeGetPackagesByCPV
480 )
481 self.emerge = self.PatchObject(deploy, "_Emerge", return_value=None)
482 self.unmerge = self.PatchObject(deploy, "_Unmerge", return_value=None)
483 self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None))
484 # Avoid running the portageq command.
485 sysroot_lib.Sysroot(self._sysroot).WriteConfig(
486 'ARCH="amd64"\nPORTDIR_OVERLAY="%s"' % "/nothing/here"
487 )
488 # make.conf needs to exist to correctly read back config.
489 unittest_lib.create_stub_make_conf(self._sysroot)
Brian Norris2eee8892021-04-06 16:23:23 -0700490
Alex Klein1699fab2022-09-08 08:46:06 -0600491 def testDeployEmerge(self):
492 """Test that deploy._Emerge is called for each package."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700493
Alex Klein1699fab2022-09-08 08:46:06 -0600494 _BINPKG = "/path/to/bar-1.2.5.tbz2"
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700495
Alex Klein1699fab2022-09-08 08:46:06 -0600496 def FakeIsFile(fname):
497 return fname == _BINPKG
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700498
Alex Klein1699fab2022-09-08 08:46:06 -0600499 packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"]
500 cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"]
501 self.package_scanner.return_value = PackageScannerFake(
502 packages,
503 {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}},
504 cpvs,
505 )
506 self.PatchObject(os.path, "isfile", side_effect=FakeIsFile)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700507
Alex Klein1699fab2022-09-08 08:46:06 -0600508 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700509
Alex Klein1699fab2022-09-08 08:46:06 -0600510 # Check that package names were correctly resolved into binary packages.
511 self.get_packages_paths.assert_called_once_with(
512 [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot
513 )
514 # Check that deploy._Emerge is called the right number of times.
515 self.emerge.assert_called_once_with(
516 mock.ANY,
517 [
518 "/path/to/foo-1.2.3.tbz2",
519 "/path/to/bar-1.2.5.tbz2",
520 "/path/to/foobar-2.0.tbz2",
521 ],
522 "/",
523 extra_args=None,
524 )
525 self.assertEqual(self.unmerge.call_count, 0)
Qijiang Fan8a945032019-04-25 20:53:29 +0900526
Alex Klein1699fab2022-09-08 08:46:06 -0600527 def testDeployEmergeDLC(self):
528 """Test that deploy._Emerge installs images for DLC packages."""
529 packages = ["some/foodlc-1.0", "some/bardlc-2.0"]
530 cpvs = ["some/foodlc-1.0", "some/bardlc-2.0"]
531 self.package_scanner.return_value = PackageScannerFake(
532 packages, {"some/foodlc-1.0": {}, "some/bardlc-2.0": {}}, cpvs
533 )
Yuanpeng Ni81c944a2023-06-10 17:48:05 -0700534 dlc_id = "foo_id"
Alex Klein1699fab2022-09-08 08:46:06 -0600535 self.PatchObject(
Yuanpeng Ni81c944a2023-06-10 17:48:05 -0700536 deploy, "_GetDLCInfo", return_value=(dlc_id, "foo_package")
Alex Klein1699fab2022-09-08 08:46:06 -0600537 )
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700538
Alex Klein1699fab2022-09-08 08:46:06 -0600539 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
540 # Check that dlcservice is restarted (DLC modules are deployed).
Yuanpeng Ni81c944a2023-06-10 17:48:05 -0700541 self.assertTrue(
542 ["dlcservice_util", "--deploy", f"--id={dlc_id}"]
543 in self.device.device.cmds
544 )
545 self.assertTrue(["restart", "dlcservice"] in self.device.device.cmds)
546
547 def testDeployEmergeDLCFallback(self):
548 """Test that deploy._Emerge installs images for DLC packages."""
549 packages = ["some/foodlc-1.0", "some/bardlc-2.0"]
550 cpvs = ["some/foodlc-1.0", "some/bardlc-2.0"]
551 self.package_scanner.return_value = PackageScannerFake(
552 packages, {"some/foodlc-1.0": {}, "some/bardlc-2.0": {}}, cpvs
553 )
554 dlc_id = "foo_id"
555 self.PatchObject(
556 deploy, "_GetDLCInfo", return_value=(dlc_id, "foo_package")
557 )
558 deploy_cmd = ["dlcservice_util", "--deploy", f"--id={dlc_id}"]
559 # Fails to run the dlcservice_util command to trigger fallback.
560 self.device.device.cmd_disallowed.append(deploy_cmd)
561
562 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
563 # Check that dlcservice is restarted (DLC modules are deployed).
564 self.assertFalse(deploy_cmd in self.device.device.cmds)
Alex Klein1699fab2022-09-08 08:46:06 -0600565 self.assertTrue(["restart", "dlcservice"] in self.device.device.cmds)
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700566
Jae Hoon Kimdf220852023-04-14 19:20:13 +0000567 def testDeployDLCLoadPinMissingDeviceDigests(self):
568 """Test that _DeployDLCLoadPin works with missing device digests."""
569 osutils.WriteFile(
570 self.tempdir
571 / dlc_lib.DLC_META_DIR
572 / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
573 LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
574 makedirs=True,
575 )
576 with self.device as d:
577 deploy._DeployDLCLoadPin(self.tempdir, d)
578 self.assertEqual(
579 d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
580 )
581 self.assertFalse(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
582 self.assertTrue(
583 "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
584 in d.copy_store.splitlines()
585 )
586
587 def testDeployDLCLoadPinFeedNewDigests(self):
588 """Test that _DeployDLCLoadPin works with digest format file."""
589 osutils.WriteFile(
590 self.tempdir
591 / dlc_lib.DLC_META_DIR
592 / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
593 LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
594 makedirs=True,
595 )
596 with self.device as d:
597 d.cat_file_output = DLC_LOADPIN_DIGEST
598 deploy._DeployDLCLoadPin(self.tempdir, d)
599 self.assertEqual(
600 d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
601 )
602 self.assertTrue(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
603 self.assertTrue(
604 "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
605 in d.copy_store.splitlines()
606 )
607
Alex Klein1699fab2022-09-08 08:46:06 -0600608 def testDeployEmergeSELinux(self):
609 """Test deploy progress when the device has SELinux"""
Qijiang Fan8a945032019-04-25 20:53:29 +0900610
Alex Klein1699fab2022-09-08 08:46:06 -0600611 _BINPKG = "/path/to/bar-1.2.5.tbz2"
Qijiang Fan8a945032019-04-25 20:53:29 +0900612
Alex Klein1699fab2022-09-08 08:46:06 -0600613 def FakeIsFile(fname):
614 return fname == _BINPKG
Qijiang Fan8a945032019-04-25 20:53:29 +0900615
Alex Klein1699fab2022-09-08 08:46:06 -0600616 def GetRestoreconCommand(pkgfile):
617 remote_path = os.path.join("/testdir/packages/to/", pkgfile)
618 return [
619 [
620 "cd",
621 "/",
622 "&&",
623 "tar",
624 "tf",
625 remote_path,
626 "|",
627 "restorecon",
628 "-i",
629 "-f",
630 "-",
631 ]
632 ]
Qijiang Fan8a945032019-04-25 20:53:29 +0900633
Alex Klein1699fab2022-09-08 08:46:06 -0600634 self.device.device.selinux_available = True
635 packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"]
636 cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"]
637 self.package_scanner.return_value = PackageScannerFake(
638 packages,
639 {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}},
640 cpvs,
641 )
642 self.PatchObject(os.path, "isfile", side_effect=FakeIsFile)
Qijiang Fan8a945032019-04-25 20:53:29 +0900643
Alex Klein1699fab2022-09-08 08:46:06 -0600644 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Qijiang Fan8a945032019-04-25 20:53:29 +0900645
Alex Klein1699fab2022-09-08 08:46:06 -0600646 # Check that package names were correctly resolved into binary packages.
647 self.get_packages_paths.assert_called_once_with(
648 [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot
649 )
650 # Check that deploy._Emerge is called the right number of times.
651 self.assertEqual(self.emerge.call_count, 1)
652 self.assertEqual(self.unmerge.call_count, 0)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700653
Alex Klein1699fab2022-09-08 08:46:06 -0600654 self.assertEqual(
655 self.device.device.cmds,
656 [["setenforce", "0"]]
657 + GetRestoreconCommand("foo-1.2.3.tbz2")
658 + GetRestoreconCommand("bar-1.2.5.tbz2")
659 + GetRestoreconCommand("foobar-2.0.tbz2")
660 + [["setenforce", "1"]],
661 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700662
Alex Klein1699fab2022-09-08 08:46:06 -0600663 def testDeployUnmerge(self):
664 """Test that deploy._Unmerge is called for each package."""
665 packages = ["foo", "bar", "foobar", "foodlc"]
666 self.package_scanner.return_value = PackageScannerFake(
667 packages,
668 {
669 "foo": {},
670 "bar": {},
671 "foobar": {},
672 "foodlc": {
673 deploy._DLC_ID: "foodlc",
674 deploy._DLC_PACKAGE: "foopackage",
675 },
676 },
677 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700678
Alex Klein1699fab2022-09-08 08:46:06 -0600679 deploy.Deploy(
680 None, ["package"], force=True, clean_binpkg=False, emerge=False
681 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700682
Alex Klein1699fab2022-09-08 08:46:06 -0600683 # Check that deploy._Unmerge is called the right number of times.
684 self.assertEqual(self.emerge.call_count, 0)
685 self.unmerge.assert_called_once_with(mock.ANY, packages, "/")
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700686
Alex Klein1699fab2022-09-08 08:46:06 -0600687 self.assertEqual(
688 self.device.device.cmds,
689 [
690 ["dlcservice_util", "--uninstall", "--id=foodlc"],
691 ["restart", "dlcservice"],
692 ],
693 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700694
Alex Klein1699fab2022-09-08 08:46:06 -0600695 def testDeployMergeWithProgressBar(self):
696 """Test that BrilloDeployOperation.Run() is called for merge."""
697 packages = ["foo", "bar", "foobar"]
698 self.package_scanner.return_value = PackageScannerFake(
699 packages, {"foo": {}, "bar": {}, "foobar": {}}
700 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700701
Alex Klein1699fab2022-09-08 08:46:06 -0600702 run = self.PatchObject(
703 deploy.BrilloDeployOperation, "Run", return_value=None
704 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700705
Alex Klein1699fab2022-09-08 08:46:06 -0600706 self.PatchObject(command, "UseProgressBar", return_value=True)
707 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700708
Alex Klein1699fab2022-09-08 08:46:06 -0600709 # Check that BrilloDeployOperation.Run was called.
710 self.assertTrue(run.called)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700711
Alex Klein1699fab2022-09-08 08:46:06 -0600712 def testDeployUnmergeWithProgressBar(self):
713 """Test that BrilloDeployOperation.Run() is called for unmerge."""
714 packages = ["foo", "bar", "foobar"]
715 self.package_scanner.return_value = PackageScannerFake(
716 packages, {"foo": {}, "bar": {}, "foobar": {}}
717 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700718
Alex Klein1699fab2022-09-08 08:46:06 -0600719 run = self.PatchObject(
720 deploy.BrilloDeployOperation, "Run", return_value=None
721 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700722
Alex Klein1699fab2022-09-08 08:46:06 -0600723 self.PatchObject(command, "UseProgressBar", return_value=True)
724 deploy.Deploy(
725 None, ["package"], force=True, clean_binpkg=False, emerge=False
726 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700727
Alex Klein1699fab2022-09-08 08:46:06 -0600728 # Check that BrilloDeployOperation.Run was called.
729 self.assertTrue(run.called)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700730
Alex Klein1699fab2022-09-08 08:46:06 -0600731 def testBrilloDeployMergeOperation(self):
732 """Test that BrilloDeployOperation works for merge."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700733
Alex Klein1699fab2022-09-08 08:46:06 -0600734 def func(queue):
735 for event in op.MERGE_EVENTS:
736 queue.get()
737 print(event)
738 sys.stdout.flush()
Ralph Nathane01ccf12015-04-16 10:40:32 -0700739
Alex Klein1699fab2022-09-08 08:46:06 -0600740 queue = multiprocessing.Queue()
741 # Emerge one package.
742 op = BrilloDeployOperationFake(True, queue)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700743
Alex Klein1699fab2022-09-08 08:46:06 -0600744 with self.OutputCapturer():
745 op.Run(func, queue)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700746
Alex Klein1699fab2022-09-08 08:46:06 -0600747 # Check that the progress bar prints correctly.
748 self.AssertProgressBarAllEvents(len(op.MERGE_EVENTS))
Ralph Nathane01ccf12015-04-16 10:40:32 -0700749
Alex Klein1699fab2022-09-08 08:46:06 -0600750 def testBrilloDeployUnmergeOperation(self):
751 """Test that BrilloDeployOperation works for unmerge."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700752
Alex Klein1699fab2022-09-08 08:46:06 -0600753 def func(queue):
754 for event in op.UNMERGE_EVENTS:
755 queue.get()
756 print(event)
757 sys.stdout.flush()
758
759 queue = multiprocessing.Queue()
760 # Unmerge one package.
761 op = BrilloDeployOperationFake(False, queue)
762
763 with self.OutputCapturer():
764 op.Run(func, queue)
765
766 # Check that the progress bar prints correctly.
767 self.AssertProgressBarAllEvents(len(op.UNMERGE_EVENTS))