blob: 6b104cbace3798beb61eecb28860100b5a454b39 [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
Mike Frysinger40ffb532021-02-12 07:36:08 -050024
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070025
Chris McDonald186b9ee2020-04-16 05:56:49 -060026pytestmark = [cros_test_lib.pytestmark_inside_only]
Greg Edelstona4c9b3b2020-01-07 17:51:13 -070027
28
29if cros_build_lib.IsInsideChroot():
Alex Klein1699fab2022-09-08 08:46:06 -060030 import portage # pylint: disable=import-error
David Pursell9476bf42015-03-30 13:34:27 -070031
32
33# pylint: disable=protected-access
34
35
Jae Hoon Kimdf220852023-04-14 19:20:13 +000036# Example DLC LoadPin digests to test with.
37LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS = """# LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS
3875a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431
39"""
40
41# An example LoadPin digest.
42DLC_LOADPIN_DIGEST = (
43 "feeddeadc0de0000000000000000000000000000000000000000000000000000"
44)
45
46
Alex Klein074f94f2023-06-22 10:32:06 -060047class ChromiumOSDeviceFake:
Alex Klein1699fab2022-09-08 08:46:06 -060048 """Fake for device."""
Ralph Nathane01ccf12015-04-16 10:40:32 -070049
Alex Klein1699fab2022-09-08 08:46:06 -060050 def __init__(self):
51 self.board = "board"
52 self.hostname = None
53 self.username = None
54 self.port = None
55 self.lsb_release = None
56 self.cmds = []
57 self.work_dir = "/testdir/"
58 self.selinux_available = False
Jae Hoon Kimdf220852023-04-14 19:20:13 +000059 self.copy_store = None
60 self.cat_file_output = ""
Ralph Nathane01ccf12015-04-16 10:40:32 -070061
Alex Klein1699fab2022-09-08 08:46:06 -060062 def MountRootfsReadWrite(self):
63 return True
Ralph Nathane01ccf12015-04-16 10:40:32 -070064
Alex Klein1699fab2022-09-08 08:46:06 -060065 def IsSELinuxAvailable(self):
66 return self.selinux_available
Ben Pastene5f03b052019-08-12 18:03:24 -070067
Alex Klein1699fab2022-09-08 08:46:06 -060068 def IsSELinuxEnforced(self):
69 return True
Ben Pastene5f03b052019-08-12 18:03:24 -070070
Mike Frysinger445f6bf2023-06-27 11:43:33 -040071 def mkdir(self, _path):
72 return None
73
Alex Klein1699fab2022-09-08 08:46:06 -060074 def run(self, cmd, **_kwargs):
75 self.cmds.append(cmd)
Andrewc7e1c6b2020-02-27 16:03:53 -080076
Alex Klein1699fab2022-09-08 08:46:06 -060077 def CopyToDevice(self, _src, _dest, _mode="rsync", **_kwargs):
Jae Hoon Kimdf220852023-04-14 19:20:13 +000078 if os.path.exists(_src):
79 self.copy_store = osutils.ReadFile(_src)
Alex Klein1699fab2022-09-08 08:46:06 -060080 return True
Andrewc7e1c6b2020-02-27 16:03:53 -080081
Jae Hoon Kimdf220852023-04-14 19:20:13 +000082 def CatFile(self, _src):
83 return self.cat_file_output
84
Ralph Nathane01ccf12015-04-16 10:40:32 -070085
Alex Klein074f94f2023-06-22 10:32:06 -060086class ChromiumOSDeviceHandlerFake:
Alex Klein1699fab2022-09-08 08:46:06 -060087 """Fake for chromite.lib.remote_access.ChomiumOSDeviceHandler."""
David Pursell9476bf42015-03-30 13:34:27 -070088
Alex Klein074f94f2023-06-22 10:32:06 -060089 class RemoteAccessFake:
Alex Klein1699fab2022-09-08 08:46:06 -060090 """Fake for chromite.lib.remote_access.RemoteAccess."""
David Pursell9476bf42015-03-30 13:34:27 -070091
Alex Klein1699fab2022-09-08 08:46:06 -060092 def __init__(self):
93 self.remote_sh_output = None
David Pursell9476bf42015-03-30 13:34:27 -070094
Alex Klein1699fab2022-09-08 08:46:06 -060095 def RemoteSh(self, *_args, **_kwargs):
96 return cros_build_lib.CompletedProcess(stdout=self.remote_sh_output)
David Pursell9476bf42015-03-30 13:34:27 -070097
Alex Klein1699fab2022-09-08 08:46:06 -060098 def __init__(self, *_args, **_kwargs):
99 self._agent = self.RemoteAccessFake()
100 self.device = ChromiumOSDeviceFake()
David Pursell67a82762015-04-30 17:26:59 -0700101
Mike Frysingerc0780a62022-08-29 04:41:56 -0400102 @property
103 def agent(self):
Alex Klein1699fab2022-09-08 08:46:06 -0600104 return self._agent
David Pursell9476bf42015-03-30 13:34:27 -0700105
Alex Klein1699fab2022-09-08 08:46:06 -0600106 def __exit__(self, _type, _value, _traceback):
107 pass
Ralph Nathane01ccf12015-04-16 10:40:32 -0700108
Alex Klein1699fab2022-09-08 08:46:06 -0600109 def __enter__(self):
110 return self.device
Ralph Nathane01ccf12015-04-16 10:40:32 -0700111
112
113class BrilloDeployOperationFake(deploy.BrilloDeployOperation):
Alex Klein1699fab2022-09-08 08:46:06 -0600114 """Fake for deploy.BrilloDeployOperation."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700115
Alex Klein1699fab2022-09-08 08:46:06 -0600116 def __init__(self, emerge, queue):
117 super().__init__(emerge)
118 self._queue = queue
119
120 def ParseOutput(self, output=None):
121 super().ParseOutput(output)
122 self._queue.put("advance")
Ralph Nathane01ccf12015-04-16 10:40:32 -0700123
David Pursell9476bf42015-03-30 13:34:27 -0700124
Alex Klein074f94f2023-06-22 10:32:06 -0600125class DbApiFake:
Alex Klein1699fab2022-09-08 08:46:06 -0600126 """Fake for Portage dbapi."""
David Pursell9476bf42015-03-30 13:34:27 -0700127
Alex Klein1699fab2022-09-08 08:46:06 -0600128 def __init__(self, pkgs):
129 self.pkg_db = {}
Tim Baine4a783b2023-04-21 20:05:51 +0000130 for cpv, slot, rdeps_raw, build_time, use in pkgs:
Alex Klein1699fab2022-09-08 08:46:06 -0600131 self.pkg_db[cpv] = {
132 "SLOT": slot,
133 "RDEPEND": rdeps_raw,
134 "BUILD_TIME": build_time,
Tim Baine4a783b2023-04-21 20:05:51 +0000135 "USE": use,
Alex Klein1699fab2022-09-08 08:46:06 -0600136 }
David Pursell9476bf42015-03-30 13:34:27 -0700137
Alex Klein1699fab2022-09-08 08:46:06 -0600138 def cpv_all(self):
139 return list(self.pkg_db)
David Pursell9476bf42015-03-30 13:34:27 -0700140
Alex Klein1699fab2022-09-08 08:46:06 -0600141 def aux_get(self, cpv, keys):
142 pkg_info = self.pkg_db[cpv]
143 return [pkg_info[key] for key in keys]
David Pursell9476bf42015-03-30 13:34:27 -0700144
145
Alex Klein074f94f2023-06-22 10:32:06 -0600146class PackageScannerFake:
Alex Klein1699fab2022-09-08 08:46:06 -0600147 """Fake for PackageScanner."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700148
Alex Klein1699fab2022-09-08 08:46:06 -0600149 def __init__(self, packages, pkgs_attrs, packages_cpvs=None):
150 self.pkgs = packages
151 self.cpvs = packages_cpvs or packages
152 self.listed = []
153 self.num_updates = 0
154 self.pkgs_attrs = pkgs_attrs
Tim Baine4a783b2023-04-21 20:05:51 +0000155 self.warnings_shown = False
Ralph Nathane01ccf12015-04-16 10:40:32 -0700156
Alex Klein1699fab2022-09-08 08:46:06 -0600157 def Run(self, _device, _root, _packages, _update, _deep, _deep_rev):
Tim Baine4a783b2023-04-21 20:05:51 +0000158 return (
159 self.cpvs,
160 self.listed,
161 self.num_updates,
162 self.pkgs_attrs,
163 self.warnings_shown,
164 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700165
166
Alex Klein074f94f2023-06-22 10:32:06 -0600167class PortageTreeFake:
Alex Klein1699fab2022-09-08 08:46:06 -0600168 """Fake for Portage tree."""
David Pursell9476bf42015-03-30 13:34:27 -0700169
Alex Klein1699fab2022-09-08 08:46:06 -0600170 def __init__(self, dbapi):
171 self.dbapi = dbapi
David Pursell9476bf42015-03-30 13:34:27 -0700172
173
Ralph Nathane01ccf12015-04-16 10:40:32 -0700174class TestInstallPackageScanner(cros_test_lib.MockOutputTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600175 """Test the update package scanner."""
David Pursell9476bf42015-03-30 13:34:27 -0700176
Alex Klein1699fab2022-09-08 08:46:06 -0600177 _BOARD = "foo_board"
178 _BUILD_ROOT = "/build/%s" % _BOARD
179 _VARTREE = [
Tim Baine4a783b2023-04-21 20:05:51 +0000180 (
181 "foo/app1-1.2.3-r4",
182 "0",
183 "foo/app2 !foo/app3",
184 "1413309336",
185 "cros-debug",
186 ),
187 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
188 (
189 "foo/app4-2.0.0-r1",
190 "0",
191 "foo/app1 foo/app5",
192 "1413309336",
193 "cros-debug",
194 ),
195 ("foo/app5-3.0.7-r3", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600196 ]
David Pursell9476bf42015-03-30 13:34:27 -0700197
Alex Klein1699fab2022-09-08 08:46:06 -0600198 def setUp(self):
199 """Patch imported modules."""
200 self.PatchObject(cros_build_lib, "GetChoice", return_value=0)
201 self.device = ChromiumOSDeviceHandlerFake()
202 self.scanner = deploy._InstallPackageScanner(self._BUILD_ROOT)
203 self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None))
Tim Baine4a783b2023-04-21 20:05:51 +0000204 self.PatchObject(
205 deploy, "_ConfirmUpdateDespiteWarnings", return_value=True
206 )
David Pursell9476bf42015-03-30 13:34:27 -0700207
Alex Klein1699fab2022-09-08 08:46:06 -0600208 def SetupVartree(self, vartree_pkgs):
Jack Rosenthal2aff1af2023-07-13 18:34:28 -0600209 self.PatchObject(
210 self.scanner,
211 "_get_portage_interpreter",
212 return_value="FAKE_PYTHON",
213 )
Mike Frysingerc0780a62022-08-29 04:41:56 -0400214 self.device.agent.remote_sh_output = json.dumps(vartree_pkgs)
David Pursell9476bf42015-03-30 13:34:27 -0700215
Alex Klein1699fab2022-09-08 08:46:06 -0600216 def SetupBintree(self, bintree_pkgs):
217 bintree = PortageTreeFake(DbApiFake(bintree_pkgs))
218 build_root = os.path.join(self._BUILD_ROOT, "")
219 portage_db = {build_root: {"bintree": bintree}}
220 self.PatchObject(portage, "create_trees", return_value=portage_db)
David Pursell9476bf42015-03-30 13:34:27 -0700221
Alex Klein1699fab2022-09-08 08:46:06 -0600222 def ValidatePkgs(self, actual, expected, constraints=None):
223 # Containing exactly the same packages.
224 self.assertEqual(sorted(expected), sorted(actual))
225 # Packages appear in the right order.
226 if constraints is not None:
227 for needs, needed in constraints:
228 self.assertGreater(actual.index(needs), actual.index(needed))
David Pursell9476bf42015-03-30 13:34:27 -0700229
Alex Klein1699fab2022-09-08 08:46:06 -0600230 def testRunUpdatedVersion(self):
231 self.SetupVartree(self._VARTREE)
232 app1 = "foo/app1-1.2.5-r4"
233 self.SetupBintree(
234 [
Tim Baine4a783b2023-04-21 20:05:51 +0000235 (app1, "0", "foo/app2 !foo/app3", "1413309336", "cros-debug"),
236 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600237 ]
238 )
Tim Baine4a783b2023-04-21 20:05:51 +0000239 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600240 self.device, "/", ["app1"], True, True, True
241 )
242 self.ValidatePkgs(installs, [app1])
243 self.ValidatePkgs(listed, [app1])
244 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700245
Tim Baine4a783b2023-04-21 20:05:51 +0000246 def testRunUpdatedVersionWithUseMismatch(self):
247 self.SetupVartree(self._VARTREE)
248 app1 = "foo/app1-1.2.5-r4"
249 # Setup the bintree with packages that don't have USE=cros-debug.
250 self.SetupBintree(
251 [
252 (app1, "0", "foo/app2 !foo/app3", "1413309336", ""),
253 ("foo/app2-4.5.6-r7", "0", "", "1413309336", ""),
254 ]
255 )
256 with self.assertLogs(level="WARN") as cm:
257 installs, listed, num_updates, _, _ = self.scanner.Run(
258 self.device, "/", ["app1"], True, True, True
259 )
260 self.ValidatePkgs(installs, [app1])
261 self.ValidatePkgs(listed, [app1])
262 self.assertEqual(num_updates, 1)
263 testline = "USE flags for package foo/app1 do not match"
264 matching_logs = [
265 logline for logline in cm.output if testline in logline
266 ]
267 self.assertTrue(
268 matching_logs, "Failed to detect USE flag mismatch."
269 )
270
Alex Klein1699fab2022-09-08 08:46:06 -0600271 def testRunUpdatedBuildTime(self):
272 self.SetupVartree(self._VARTREE)
273 app1 = "foo/app1-1.2.3-r4"
274 self.SetupBintree(
275 [
Tim Baine4a783b2023-04-21 20:05:51 +0000276 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
277 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600278 ]
279 )
Tim Baine4a783b2023-04-21 20:05:51 +0000280 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600281 self.device, "/", ["app1"], True, True, True
282 )
283 self.ValidatePkgs(installs, [app1])
284 self.ValidatePkgs(listed, [app1])
285 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700286
Alex Klein1699fab2022-09-08 08:46:06 -0600287 def testRunExistingDepUpdated(self):
288 self.SetupVartree(self._VARTREE)
289 app1 = "foo/app1-1.2.5-r2"
290 app2 = "foo/app2-4.5.8-r3"
291 self.SetupBintree(
292 [
Tim Baine4a783b2023-04-21 20:05:51 +0000293 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
294 (app2, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600295 ]
296 )
Tim Baine4a783b2023-04-21 20:05:51 +0000297 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600298 self.device, "/", ["app1"], True, True, True
299 )
300 self.ValidatePkgs(installs, [app1, app2], constraints=[(app1, app2)])
301 self.ValidatePkgs(listed, [app1])
302 self.assertEqual(num_updates, 2)
David Pursell9476bf42015-03-30 13:34:27 -0700303
Alex Klein1699fab2022-09-08 08:46:06 -0600304 def testRunMissingDepUpdated(self):
305 self.SetupVartree(self._VARTREE)
306 app1 = "foo/app1-1.2.5-r2"
307 app6 = "foo/app6-1.0.0-r1"
308 self.SetupBintree(
309 [
Tim Baine4a783b2023-04-21 20:05:51 +0000310 (
311 app1,
312 "0",
313 "foo/app2 !foo/app3 foo/app6",
314 "1413309350",
315 "cros-debug",
316 ),
317 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
318 (app6, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600319 ]
320 )
Tim Baine4a783b2023-04-21 20:05:51 +0000321 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600322 self.device, "/", ["app1"], True, True, True
323 )
324 self.ValidatePkgs(installs, [app1, app6], constraints=[(app1, app6)])
325 self.ValidatePkgs(listed, [app1])
326 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700327
Alex Klein1699fab2022-09-08 08:46:06 -0600328 def testRunExistingRevDepUpdated(self):
329 self.SetupVartree(self._VARTREE)
330 app1 = "foo/app1-1.2.5-r2"
331 app4 = "foo/app4-2.0.1-r3"
332 self.SetupBintree(
333 [
Tim Baine4a783b2023-04-21 20:05:51 +0000334 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
335 (app4, "0", "foo/app1 foo/app5", "1413309350", "cros-debug"),
336 ("foo/app5-3.0.7-r3", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600337 ]
338 )
Tim Baine4a783b2023-04-21 20:05:51 +0000339 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600340 self.device, "/", ["app1"], True, True, True
341 )
342 self.ValidatePkgs(installs, [app1, app4], constraints=[(app4, app1)])
343 self.ValidatePkgs(listed, [app1])
344 self.assertEqual(num_updates, 2)
David Pursell9476bf42015-03-30 13:34:27 -0700345
Alex Klein1699fab2022-09-08 08:46:06 -0600346 def testRunMissingRevDepNotUpdated(self):
347 self.SetupVartree(self._VARTREE)
348 app1 = "foo/app1-1.2.5-r2"
349 app6 = "foo/app6-1.0.0-r1"
350 self.SetupBintree(
351 [
Tim Baine4a783b2023-04-21 20:05:51 +0000352 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
353 (app6, "0", "foo/app1", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600354 ]
355 )
Tim Baine4a783b2023-04-21 20:05:51 +0000356 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600357 self.device, "/", ["app1"], True, True, True
358 )
359 self.ValidatePkgs(installs, [app1])
360 self.ValidatePkgs(listed, [app1])
361 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700362
Alex Klein1699fab2022-09-08 08:46:06 -0600363 def testRunTransitiveDepsUpdated(self):
364 self.SetupVartree(self._VARTREE)
365 app1 = "foo/app1-1.2.5-r2"
366 app2 = "foo/app2-4.5.8-r3"
367 app4 = "foo/app4-2.0.0-r1"
368 app5 = "foo/app5-3.0.8-r2"
369 self.SetupBintree(
370 [
Tim Baine4a783b2023-04-21 20:05:51 +0000371 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
372 (app2, "0", "", "1413309350", "cros-debug"),
373 (app4, "0", "foo/app1 foo/app5", "1413309350", "cros-debug"),
374 (app5, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600375 ]
376 )
Tim Baine4a783b2023-04-21 20:05:51 +0000377 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600378 self.device, "/", ["app1"], True, True, True
379 )
380 self.ValidatePkgs(
381 installs,
382 [app1, app2, app4, app5],
383 constraints=[(app1, app2), (app4, app1), (app4, app5)],
384 )
385 self.ValidatePkgs(listed, [app1])
386 self.assertEqual(num_updates, 4)
David Pursell9476bf42015-03-30 13:34:27 -0700387
Alex Klein1699fab2022-09-08 08:46:06 -0600388 def testRunDisjunctiveDepsExistingUpdated(self):
389 self.SetupVartree(self._VARTREE)
390 app1 = "foo/app1-1.2.5-r2"
391 self.SetupBintree(
392 [
Tim Baine4a783b2023-04-21 20:05:51 +0000393 (
394 app1,
395 "0",
396 "|| ( foo/app6 foo/app2 ) !foo/app3",
397 "1413309350",
398 "cros-debug",
399 ),
400 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600401 ]
402 )
Tim Baine4a783b2023-04-21 20:05:51 +0000403 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600404 self.device, "/", ["app1"], True, True, True
405 )
406 self.ValidatePkgs(installs, [app1])
407 self.ValidatePkgs(listed, [app1])
408 self.assertEqual(num_updates, 1)
409
410 def testRunDisjunctiveDepsDefaultUpdated(self):
411 self.SetupVartree(self._VARTREE)
412 app1 = "foo/app1-1.2.5-r2"
413 app7 = "foo/app7-1.0.0-r1"
414 self.SetupBintree(
415 [
Tim Baine4a783b2023-04-21 20:05:51 +0000416 (
417 app1,
418 "0",
419 "|| ( foo/app6 foo/app7 ) !foo/app3",
420 "1413309350",
421 "cros-debug",
422 ),
423 (app7, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600424 ]
425 )
Tim Baine4a783b2023-04-21 20:05:51 +0000426 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600427 self.device, "/", ["app1"], True, True, True
428 )
429 self.ValidatePkgs(installs, [app1, app7], constraints=[(app1, app7)])
430 self.ValidatePkgs(listed, [app1])
431 self.assertEqual(num_updates, 1)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700432
Jack Rosenthal2aff1af2023-07-13 18:34:28 -0600433 def test_get_portage_interpreter(self):
434 """Test getting the portage interpreter from the device."""
435 self.device.agent.remote_sh_output = """\
436/usr/lib/python-exec/python3.6/emerge
437/usr/lib/python-exec/python3.8/emerge
438/usr/lib/python-exec/python3.11/emerge
439"""
440 self.assertEqual(
441 self.scanner._get_portage_interpreter(self.device),
442 "python3.11",
443 )
444
Ralph Nathane01ccf12015-04-16 10:40:32 -0700445
Alex Klein1699fab2022-09-08 08:46:06 -0600446class TestDeploy(
447 cros_test_lib.ProgressBarTestCase, cros_test_lib.MockTempDirTestCase
448):
449 """Test deploy.Deploy."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700450
Alex Klein1699fab2022-09-08 08:46:06 -0600451 @staticmethod
452 def FakeGetPackagesByCPV(cpvs, _strip, _sysroot):
453 return ["/path/to/%s.tbz2" % cpv.pv for cpv in cpvs]
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700454
Alex Klein1699fab2022-09-08 08:46:06 -0600455 def setUp(self):
456 # Fake being root to avoid running filesystem commands with sudo_run.
457 self.PatchObject(osutils, "IsRootUser", return_value=True)
458 self._sysroot = os.path.join(self.tempdir, "sysroot")
459 osutils.SafeMakedirs(self._sysroot)
460 self.device = ChromiumOSDeviceHandlerFake()
461 self.PatchObject(
462 remote_access, "ChromiumOSDeviceHandler", return_value=self.device
463 )
464 self.PatchObject(cros_build_lib, "GetBoard", return_value=None)
465 self.PatchObject(
466 build_target_lib,
467 "get_default_sysroot_path",
468 return_value=self._sysroot,
469 )
470 self.package_scanner = self.PatchObject(
471 deploy, "_InstallPackageScanner"
472 )
473 self.get_packages_paths = self.PatchObject(
474 deploy, "_GetPackagesByCPV", side_effect=self.FakeGetPackagesByCPV
475 )
476 self.emerge = self.PatchObject(deploy, "_Emerge", return_value=None)
477 self.unmerge = self.PatchObject(deploy, "_Unmerge", return_value=None)
478 self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None))
479 # Avoid running the portageq command.
480 sysroot_lib.Sysroot(self._sysroot).WriteConfig(
481 'ARCH="amd64"\nPORTDIR_OVERLAY="%s"' % "/nothing/here"
482 )
483 # make.conf needs to exist to correctly read back config.
484 unittest_lib.create_stub_make_conf(self._sysroot)
Brian Norris2eee8892021-04-06 16:23:23 -0700485
Alex Klein1699fab2022-09-08 08:46:06 -0600486 def testDeployEmerge(self):
487 """Test that deploy._Emerge is called for each package."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700488
Alex Klein1699fab2022-09-08 08:46:06 -0600489 _BINPKG = "/path/to/bar-1.2.5.tbz2"
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700490
Alex Klein1699fab2022-09-08 08:46:06 -0600491 def FakeIsFile(fname):
492 return fname == _BINPKG
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700493
Alex Klein1699fab2022-09-08 08:46:06 -0600494 packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"]
495 cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"]
496 self.package_scanner.return_value = PackageScannerFake(
497 packages,
498 {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}},
499 cpvs,
500 )
501 self.PatchObject(os.path, "isfile", side_effect=FakeIsFile)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700502
Alex Klein1699fab2022-09-08 08:46:06 -0600503 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700504
Alex Klein1699fab2022-09-08 08:46:06 -0600505 # Check that package names were correctly resolved into binary packages.
506 self.get_packages_paths.assert_called_once_with(
507 [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot
508 )
509 # Check that deploy._Emerge is called the right number of times.
510 self.emerge.assert_called_once_with(
511 mock.ANY,
512 [
513 "/path/to/foo-1.2.3.tbz2",
514 "/path/to/bar-1.2.5.tbz2",
515 "/path/to/foobar-2.0.tbz2",
516 ],
517 "/",
518 extra_args=None,
519 )
520 self.assertEqual(self.unmerge.call_count, 0)
Qijiang Fan8a945032019-04-25 20:53:29 +0900521
Alex Klein1699fab2022-09-08 08:46:06 -0600522 def testDeployEmergeDLC(self):
523 """Test that deploy._Emerge installs images for DLC packages."""
524 packages = ["some/foodlc-1.0", "some/bardlc-2.0"]
525 cpvs = ["some/foodlc-1.0", "some/bardlc-2.0"]
526 self.package_scanner.return_value = PackageScannerFake(
527 packages, {"some/foodlc-1.0": {}, "some/bardlc-2.0": {}}, cpvs
528 )
529 self.PatchObject(
530 deploy, "_GetDLCInfo", return_value=("foo_id", "foo_package")
531 )
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700532
Alex Klein1699fab2022-09-08 08:46:06 -0600533 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
534 # Check that dlcservice is restarted (DLC modules are deployed).
535 self.assertTrue(["restart", "dlcservice"] in self.device.device.cmds)
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700536
Jae Hoon Kimdf220852023-04-14 19:20:13 +0000537 def testDeployDLCLoadPinMissingDeviceDigests(self):
538 """Test that _DeployDLCLoadPin works with missing device digests."""
539 osutils.WriteFile(
540 self.tempdir
541 / dlc_lib.DLC_META_DIR
542 / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
543 LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
544 makedirs=True,
545 )
546 with self.device as d:
547 deploy._DeployDLCLoadPin(self.tempdir, d)
548 self.assertEqual(
549 d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
550 )
551 self.assertFalse(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
552 self.assertTrue(
553 "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
554 in d.copy_store.splitlines()
555 )
556
557 def testDeployDLCLoadPinFeedNewDigests(self):
558 """Test that _DeployDLCLoadPin works with digest format file."""
559 osutils.WriteFile(
560 self.tempdir
561 / dlc_lib.DLC_META_DIR
562 / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
563 LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
564 makedirs=True,
565 )
566 with self.device as d:
567 d.cat_file_output = DLC_LOADPIN_DIGEST
568 deploy._DeployDLCLoadPin(self.tempdir, d)
569 self.assertEqual(
570 d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
571 )
572 self.assertTrue(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
573 self.assertTrue(
574 "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
575 in d.copy_store.splitlines()
576 )
577
Alex Klein1699fab2022-09-08 08:46:06 -0600578 def testDeployEmergeSELinux(self):
579 """Test deploy progress when the device has SELinux"""
Qijiang Fan8a945032019-04-25 20:53:29 +0900580
Alex Klein1699fab2022-09-08 08:46:06 -0600581 _BINPKG = "/path/to/bar-1.2.5.tbz2"
Qijiang Fan8a945032019-04-25 20:53:29 +0900582
Alex Klein1699fab2022-09-08 08:46:06 -0600583 def FakeIsFile(fname):
584 return fname == _BINPKG
Qijiang Fan8a945032019-04-25 20:53:29 +0900585
Alex Klein1699fab2022-09-08 08:46:06 -0600586 def GetRestoreconCommand(pkgfile):
587 remote_path = os.path.join("/testdir/packages/to/", pkgfile)
588 return [
589 [
590 "cd",
591 "/",
592 "&&",
593 "tar",
594 "tf",
595 remote_path,
596 "|",
597 "restorecon",
598 "-i",
599 "-f",
600 "-",
601 ]
602 ]
Qijiang Fan8a945032019-04-25 20:53:29 +0900603
Alex Klein1699fab2022-09-08 08:46:06 -0600604 self.device.device.selinux_available = True
605 packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"]
606 cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"]
607 self.package_scanner.return_value = PackageScannerFake(
608 packages,
609 {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}},
610 cpvs,
611 )
612 self.PatchObject(os.path, "isfile", side_effect=FakeIsFile)
Qijiang Fan8a945032019-04-25 20:53:29 +0900613
Alex Klein1699fab2022-09-08 08:46:06 -0600614 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Qijiang Fan8a945032019-04-25 20:53:29 +0900615
Alex Klein1699fab2022-09-08 08:46:06 -0600616 # Check that package names were correctly resolved into binary packages.
617 self.get_packages_paths.assert_called_once_with(
618 [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot
619 )
620 # Check that deploy._Emerge is called the right number of times.
621 self.assertEqual(self.emerge.call_count, 1)
622 self.assertEqual(self.unmerge.call_count, 0)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700623
Alex Klein1699fab2022-09-08 08:46:06 -0600624 self.assertEqual(
625 self.device.device.cmds,
626 [["setenforce", "0"]]
627 + GetRestoreconCommand("foo-1.2.3.tbz2")
628 + GetRestoreconCommand("bar-1.2.5.tbz2")
629 + GetRestoreconCommand("foobar-2.0.tbz2")
630 + [["setenforce", "1"]],
631 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700632
Alex Klein1699fab2022-09-08 08:46:06 -0600633 def testDeployUnmerge(self):
634 """Test that deploy._Unmerge is called for each package."""
635 packages = ["foo", "bar", "foobar", "foodlc"]
636 self.package_scanner.return_value = PackageScannerFake(
637 packages,
638 {
639 "foo": {},
640 "bar": {},
641 "foobar": {},
642 "foodlc": {
643 deploy._DLC_ID: "foodlc",
644 deploy._DLC_PACKAGE: "foopackage",
645 },
646 },
647 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700648
Alex Klein1699fab2022-09-08 08:46:06 -0600649 deploy.Deploy(
650 None, ["package"], force=True, clean_binpkg=False, emerge=False
651 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700652
Alex Klein1699fab2022-09-08 08:46:06 -0600653 # Check that deploy._Unmerge is called the right number of times.
654 self.assertEqual(self.emerge.call_count, 0)
655 self.unmerge.assert_called_once_with(mock.ANY, packages, "/")
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700656
Alex Klein1699fab2022-09-08 08:46:06 -0600657 self.assertEqual(
658 self.device.device.cmds,
659 [
660 ["dlcservice_util", "--uninstall", "--id=foodlc"],
661 ["restart", "dlcservice"],
662 ],
663 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700664
Alex Klein1699fab2022-09-08 08:46:06 -0600665 def testDeployMergeWithProgressBar(self):
666 """Test that BrilloDeployOperation.Run() is called for merge."""
667 packages = ["foo", "bar", "foobar"]
668 self.package_scanner.return_value = PackageScannerFake(
669 packages, {"foo": {}, "bar": {}, "foobar": {}}
670 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700671
Alex Klein1699fab2022-09-08 08:46:06 -0600672 run = self.PatchObject(
673 deploy.BrilloDeployOperation, "Run", return_value=None
674 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700675
Alex Klein1699fab2022-09-08 08:46:06 -0600676 self.PatchObject(command, "UseProgressBar", return_value=True)
677 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700678
Alex Klein1699fab2022-09-08 08:46:06 -0600679 # Check that BrilloDeployOperation.Run was called.
680 self.assertTrue(run.called)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700681
Alex Klein1699fab2022-09-08 08:46:06 -0600682 def testDeployUnmergeWithProgressBar(self):
683 """Test that BrilloDeployOperation.Run() is called for unmerge."""
684 packages = ["foo", "bar", "foobar"]
685 self.package_scanner.return_value = PackageScannerFake(
686 packages, {"foo": {}, "bar": {}, "foobar": {}}
687 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700688
Alex Klein1699fab2022-09-08 08:46:06 -0600689 run = self.PatchObject(
690 deploy.BrilloDeployOperation, "Run", return_value=None
691 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700692
Alex Klein1699fab2022-09-08 08:46:06 -0600693 self.PatchObject(command, "UseProgressBar", return_value=True)
694 deploy.Deploy(
695 None, ["package"], force=True, clean_binpkg=False, emerge=False
696 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700697
Alex Klein1699fab2022-09-08 08:46:06 -0600698 # Check that BrilloDeployOperation.Run was called.
699 self.assertTrue(run.called)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700700
Alex Klein1699fab2022-09-08 08:46:06 -0600701 def testBrilloDeployMergeOperation(self):
702 """Test that BrilloDeployOperation works for merge."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700703
Alex Klein1699fab2022-09-08 08:46:06 -0600704 def func(queue):
705 for event in op.MERGE_EVENTS:
706 queue.get()
707 print(event)
708 sys.stdout.flush()
Ralph Nathane01ccf12015-04-16 10:40:32 -0700709
Alex Klein1699fab2022-09-08 08:46:06 -0600710 queue = multiprocessing.Queue()
711 # Emerge one package.
712 op = BrilloDeployOperationFake(True, queue)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700713
Alex Klein1699fab2022-09-08 08:46:06 -0600714 with self.OutputCapturer():
715 op.Run(func, queue)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700716
Alex Klein1699fab2022-09-08 08:46:06 -0600717 # Check that the progress bar prints correctly.
718 self.AssertProgressBarAllEvents(len(op.MERGE_EVENTS))
Ralph Nathane01ccf12015-04-16 10:40:32 -0700719
Alex Klein1699fab2022-09-08 08:46:06 -0600720 def testBrilloDeployUnmergeOperation(self):
721 """Test that BrilloDeployOperation works for unmerge."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700722
Alex Klein1699fab2022-09-08 08:46:06 -0600723 def func(queue):
724 for event in op.UNMERGE_EVENTS:
725 queue.get()
726 print(event)
727 sys.stdout.flush()
728
729 queue = multiprocessing.Queue()
730 # Unmerge one package.
731 op = BrilloDeployOperationFake(False, queue)
732
733 with self.OutputCapturer():
734 op.Run(func, queue)
735
736 # Check that the progress bar prints correctly.
737 self.AssertProgressBarAllEvents(len(op.UNMERGE_EVENTS))