blob: 8683bafcbec15849596d6d3a39e5ac22e84a92c7 [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):
Mike Frysingerc0780a62022-08-29 04:41:56 -0400209 self.device.agent.remote_sh_output = json.dumps(vartree_pkgs)
David Pursell9476bf42015-03-30 13:34:27 -0700210
Alex Klein1699fab2022-09-08 08:46:06 -0600211 def SetupBintree(self, bintree_pkgs):
212 bintree = PortageTreeFake(DbApiFake(bintree_pkgs))
213 build_root = os.path.join(self._BUILD_ROOT, "")
214 portage_db = {build_root: {"bintree": bintree}}
215 self.PatchObject(portage, "create_trees", return_value=portage_db)
David Pursell9476bf42015-03-30 13:34:27 -0700216
Alex Klein1699fab2022-09-08 08:46:06 -0600217 def ValidatePkgs(self, actual, expected, constraints=None):
218 # Containing exactly the same packages.
219 self.assertEqual(sorted(expected), sorted(actual))
220 # Packages appear in the right order.
221 if constraints is not None:
222 for needs, needed in constraints:
223 self.assertGreater(actual.index(needs), actual.index(needed))
David Pursell9476bf42015-03-30 13:34:27 -0700224
Alex Klein1699fab2022-09-08 08:46:06 -0600225 def testRunUpdatedVersion(self):
226 self.SetupVartree(self._VARTREE)
227 app1 = "foo/app1-1.2.5-r4"
228 self.SetupBintree(
229 [
Tim Baine4a783b2023-04-21 20:05:51 +0000230 (app1, "0", "foo/app2 !foo/app3", "1413309336", "cros-debug"),
231 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600232 ]
233 )
Tim Baine4a783b2023-04-21 20:05:51 +0000234 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600235 self.device, "/", ["app1"], True, True, True
236 )
237 self.ValidatePkgs(installs, [app1])
238 self.ValidatePkgs(listed, [app1])
239 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700240
Tim Baine4a783b2023-04-21 20:05:51 +0000241 def testRunUpdatedVersionWithUseMismatch(self):
242 self.SetupVartree(self._VARTREE)
243 app1 = "foo/app1-1.2.5-r4"
244 # Setup the bintree with packages that don't have USE=cros-debug.
245 self.SetupBintree(
246 [
247 (app1, "0", "foo/app2 !foo/app3", "1413309336", ""),
248 ("foo/app2-4.5.6-r7", "0", "", "1413309336", ""),
249 ]
250 )
251 with self.assertLogs(level="WARN") as cm:
252 installs, listed, num_updates, _, _ = self.scanner.Run(
253 self.device, "/", ["app1"], True, True, True
254 )
255 self.ValidatePkgs(installs, [app1])
256 self.ValidatePkgs(listed, [app1])
257 self.assertEqual(num_updates, 1)
258 testline = "USE flags for package foo/app1 do not match"
259 matching_logs = [
260 logline for logline in cm.output if testline in logline
261 ]
262 self.assertTrue(
263 matching_logs, "Failed to detect USE flag mismatch."
264 )
265
Alex Klein1699fab2022-09-08 08:46:06 -0600266 def testRunUpdatedBuildTime(self):
267 self.SetupVartree(self._VARTREE)
268 app1 = "foo/app1-1.2.3-r4"
269 self.SetupBintree(
270 [
Tim Baine4a783b2023-04-21 20:05:51 +0000271 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
272 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600273 ]
274 )
Tim Baine4a783b2023-04-21 20:05:51 +0000275 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600276 self.device, "/", ["app1"], True, True, True
277 )
278 self.ValidatePkgs(installs, [app1])
279 self.ValidatePkgs(listed, [app1])
280 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700281
Alex Klein1699fab2022-09-08 08:46:06 -0600282 def testRunExistingDepUpdated(self):
283 self.SetupVartree(self._VARTREE)
284 app1 = "foo/app1-1.2.5-r2"
285 app2 = "foo/app2-4.5.8-r3"
286 self.SetupBintree(
287 [
Tim Baine4a783b2023-04-21 20:05:51 +0000288 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
289 (app2, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600290 ]
291 )
Tim Baine4a783b2023-04-21 20:05:51 +0000292 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600293 self.device, "/", ["app1"], True, True, True
294 )
295 self.ValidatePkgs(installs, [app1, app2], constraints=[(app1, app2)])
296 self.ValidatePkgs(listed, [app1])
297 self.assertEqual(num_updates, 2)
David Pursell9476bf42015-03-30 13:34:27 -0700298
Alex Klein1699fab2022-09-08 08:46:06 -0600299 def testRunMissingDepUpdated(self):
300 self.SetupVartree(self._VARTREE)
301 app1 = "foo/app1-1.2.5-r2"
302 app6 = "foo/app6-1.0.0-r1"
303 self.SetupBintree(
304 [
Tim Baine4a783b2023-04-21 20:05:51 +0000305 (
306 app1,
307 "0",
308 "foo/app2 !foo/app3 foo/app6",
309 "1413309350",
310 "cros-debug",
311 ),
312 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
313 (app6, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600314 ]
315 )
Tim Baine4a783b2023-04-21 20:05:51 +0000316 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600317 self.device, "/", ["app1"], True, True, True
318 )
319 self.ValidatePkgs(installs, [app1, app6], constraints=[(app1, app6)])
320 self.ValidatePkgs(listed, [app1])
321 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700322
Alex Klein1699fab2022-09-08 08:46:06 -0600323 def testRunExistingRevDepUpdated(self):
324 self.SetupVartree(self._VARTREE)
325 app1 = "foo/app1-1.2.5-r2"
326 app4 = "foo/app4-2.0.1-r3"
327 self.SetupBintree(
328 [
Tim Baine4a783b2023-04-21 20:05:51 +0000329 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
330 (app4, "0", "foo/app1 foo/app5", "1413309350", "cros-debug"),
331 ("foo/app5-3.0.7-r3", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600332 ]
333 )
Tim Baine4a783b2023-04-21 20:05:51 +0000334 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600335 self.device, "/", ["app1"], True, True, True
336 )
337 self.ValidatePkgs(installs, [app1, app4], constraints=[(app4, app1)])
338 self.ValidatePkgs(listed, [app1])
339 self.assertEqual(num_updates, 2)
David Pursell9476bf42015-03-30 13:34:27 -0700340
Alex Klein1699fab2022-09-08 08:46:06 -0600341 def testRunMissingRevDepNotUpdated(self):
342 self.SetupVartree(self._VARTREE)
343 app1 = "foo/app1-1.2.5-r2"
344 app6 = "foo/app6-1.0.0-r1"
345 self.SetupBintree(
346 [
Tim Baine4a783b2023-04-21 20:05:51 +0000347 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
348 (app6, "0", "foo/app1", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600349 ]
350 )
Tim Baine4a783b2023-04-21 20:05:51 +0000351 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600352 self.device, "/", ["app1"], True, True, True
353 )
354 self.ValidatePkgs(installs, [app1])
355 self.ValidatePkgs(listed, [app1])
356 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700357
Alex Klein1699fab2022-09-08 08:46:06 -0600358 def testRunTransitiveDepsUpdated(self):
359 self.SetupVartree(self._VARTREE)
360 app1 = "foo/app1-1.2.5-r2"
361 app2 = "foo/app2-4.5.8-r3"
362 app4 = "foo/app4-2.0.0-r1"
363 app5 = "foo/app5-3.0.8-r2"
364 self.SetupBintree(
365 [
Tim Baine4a783b2023-04-21 20:05:51 +0000366 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
367 (app2, "0", "", "1413309350", "cros-debug"),
368 (app4, "0", "foo/app1 foo/app5", "1413309350", "cros-debug"),
369 (app5, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600370 ]
371 )
Tim Baine4a783b2023-04-21 20:05:51 +0000372 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600373 self.device, "/", ["app1"], True, True, True
374 )
375 self.ValidatePkgs(
376 installs,
377 [app1, app2, app4, app5],
378 constraints=[(app1, app2), (app4, app1), (app4, app5)],
379 )
380 self.ValidatePkgs(listed, [app1])
381 self.assertEqual(num_updates, 4)
David Pursell9476bf42015-03-30 13:34:27 -0700382
Alex Klein1699fab2022-09-08 08:46:06 -0600383 def testRunDisjunctiveDepsExistingUpdated(self):
384 self.SetupVartree(self._VARTREE)
385 app1 = "foo/app1-1.2.5-r2"
386 self.SetupBintree(
387 [
Tim Baine4a783b2023-04-21 20:05:51 +0000388 (
389 app1,
390 "0",
391 "|| ( foo/app6 foo/app2 ) !foo/app3",
392 "1413309350",
393 "cros-debug",
394 ),
395 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600396 ]
397 )
Tim Baine4a783b2023-04-21 20:05:51 +0000398 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600399 self.device, "/", ["app1"], True, True, True
400 )
401 self.ValidatePkgs(installs, [app1])
402 self.ValidatePkgs(listed, [app1])
403 self.assertEqual(num_updates, 1)
404
405 def testRunDisjunctiveDepsDefaultUpdated(self):
406 self.SetupVartree(self._VARTREE)
407 app1 = "foo/app1-1.2.5-r2"
408 app7 = "foo/app7-1.0.0-r1"
409 self.SetupBintree(
410 [
Tim Baine4a783b2023-04-21 20:05:51 +0000411 (
412 app1,
413 "0",
414 "|| ( foo/app6 foo/app7 ) !foo/app3",
415 "1413309350",
416 "cros-debug",
417 ),
418 (app7, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600419 ]
420 )
Tim Baine4a783b2023-04-21 20:05:51 +0000421 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600422 self.device, "/", ["app1"], True, True, True
423 )
424 self.ValidatePkgs(installs, [app1, app7], constraints=[(app1, app7)])
425 self.ValidatePkgs(listed, [app1])
426 self.assertEqual(num_updates, 1)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700427
428
Alex Klein1699fab2022-09-08 08:46:06 -0600429class TestDeploy(
430 cros_test_lib.ProgressBarTestCase, cros_test_lib.MockTempDirTestCase
431):
432 """Test deploy.Deploy."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700433
Alex Klein1699fab2022-09-08 08:46:06 -0600434 @staticmethod
435 def FakeGetPackagesByCPV(cpvs, _strip, _sysroot):
436 return ["/path/to/%s.tbz2" % cpv.pv for cpv in cpvs]
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700437
Alex Klein1699fab2022-09-08 08:46:06 -0600438 def setUp(self):
439 # Fake being root to avoid running filesystem commands with sudo_run.
440 self.PatchObject(osutils, "IsRootUser", return_value=True)
441 self._sysroot = os.path.join(self.tempdir, "sysroot")
442 osutils.SafeMakedirs(self._sysroot)
443 self.device = ChromiumOSDeviceHandlerFake()
444 self.PatchObject(
445 remote_access, "ChromiumOSDeviceHandler", return_value=self.device
446 )
447 self.PatchObject(cros_build_lib, "GetBoard", return_value=None)
448 self.PatchObject(
449 build_target_lib,
450 "get_default_sysroot_path",
451 return_value=self._sysroot,
452 )
453 self.package_scanner = self.PatchObject(
454 deploy, "_InstallPackageScanner"
455 )
456 self.get_packages_paths = self.PatchObject(
457 deploy, "_GetPackagesByCPV", side_effect=self.FakeGetPackagesByCPV
458 )
459 self.emerge = self.PatchObject(deploy, "_Emerge", return_value=None)
460 self.unmerge = self.PatchObject(deploy, "_Unmerge", return_value=None)
461 self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None))
462 # Avoid running the portageq command.
463 sysroot_lib.Sysroot(self._sysroot).WriteConfig(
464 'ARCH="amd64"\nPORTDIR_OVERLAY="%s"' % "/nothing/here"
465 )
466 # make.conf needs to exist to correctly read back config.
467 unittest_lib.create_stub_make_conf(self._sysroot)
Brian Norris2eee8892021-04-06 16:23:23 -0700468
Alex Klein1699fab2022-09-08 08:46:06 -0600469 def testDeployEmerge(self):
470 """Test that deploy._Emerge is called for each package."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700471
Alex Klein1699fab2022-09-08 08:46:06 -0600472 _BINPKG = "/path/to/bar-1.2.5.tbz2"
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700473
Alex Klein1699fab2022-09-08 08:46:06 -0600474 def FakeIsFile(fname):
475 return fname == _BINPKG
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700476
Alex Klein1699fab2022-09-08 08:46:06 -0600477 packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"]
478 cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"]
479 self.package_scanner.return_value = PackageScannerFake(
480 packages,
481 {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}},
482 cpvs,
483 )
484 self.PatchObject(os.path, "isfile", side_effect=FakeIsFile)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700485
Alex Klein1699fab2022-09-08 08:46:06 -0600486 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700487
Alex Klein1699fab2022-09-08 08:46:06 -0600488 # Check that package names were correctly resolved into binary packages.
489 self.get_packages_paths.assert_called_once_with(
490 [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot
491 )
492 # Check that deploy._Emerge is called the right number of times.
493 self.emerge.assert_called_once_with(
494 mock.ANY,
495 [
496 "/path/to/foo-1.2.3.tbz2",
497 "/path/to/bar-1.2.5.tbz2",
498 "/path/to/foobar-2.0.tbz2",
499 ],
500 "/",
501 extra_args=None,
502 )
503 self.assertEqual(self.unmerge.call_count, 0)
Qijiang Fan8a945032019-04-25 20:53:29 +0900504
Alex Klein1699fab2022-09-08 08:46:06 -0600505 def testDeployEmergeDLC(self):
506 """Test that deploy._Emerge installs images for DLC packages."""
507 packages = ["some/foodlc-1.0", "some/bardlc-2.0"]
508 cpvs = ["some/foodlc-1.0", "some/bardlc-2.0"]
509 self.package_scanner.return_value = PackageScannerFake(
510 packages, {"some/foodlc-1.0": {}, "some/bardlc-2.0": {}}, cpvs
511 )
512 self.PatchObject(
513 deploy, "_GetDLCInfo", return_value=("foo_id", "foo_package")
514 )
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700515
Alex Klein1699fab2022-09-08 08:46:06 -0600516 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
517 # Check that dlcservice is restarted (DLC modules are deployed).
518 self.assertTrue(["restart", "dlcservice"] in self.device.device.cmds)
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700519
Jae Hoon Kimdf220852023-04-14 19:20:13 +0000520 def testDeployDLCLoadPinMissingDeviceDigests(self):
521 """Test that _DeployDLCLoadPin works with missing device digests."""
522 osutils.WriteFile(
523 self.tempdir
524 / dlc_lib.DLC_META_DIR
525 / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
526 LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
527 makedirs=True,
528 )
529 with self.device as d:
530 deploy._DeployDLCLoadPin(self.tempdir, d)
531 self.assertEqual(
532 d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
533 )
534 self.assertFalse(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
535 self.assertTrue(
536 "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
537 in d.copy_store.splitlines()
538 )
539
540 def testDeployDLCLoadPinFeedNewDigests(self):
541 """Test that _DeployDLCLoadPin works with digest format file."""
542 osutils.WriteFile(
543 self.tempdir
544 / dlc_lib.DLC_META_DIR
545 / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
546 LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
547 makedirs=True,
548 )
549 with self.device as d:
550 d.cat_file_output = DLC_LOADPIN_DIGEST
551 deploy._DeployDLCLoadPin(self.tempdir, d)
552 self.assertEqual(
553 d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
554 )
555 self.assertTrue(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
556 self.assertTrue(
557 "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
558 in d.copy_store.splitlines()
559 )
560
Alex Klein1699fab2022-09-08 08:46:06 -0600561 def testDeployEmergeSELinux(self):
562 """Test deploy progress when the device has SELinux"""
Qijiang Fan8a945032019-04-25 20:53:29 +0900563
Alex Klein1699fab2022-09-08 08:46:06 -0600564 _BINPKG = "/path/to/bar-1.2.5.tbz2"
Qijiang Fan8a945032019-04-25 20:53:29 +0900565
Alex Klein1699fab2022-09-08 08:46:06 -0600566 def FakeIsFile(fname):
567 return fname == _BINPKG
Qijiang Fan8a945032019-04-25 20:53:29 +0900568
Alex Klein1699fab2022-09-08 08:46:06 -0600569 def GetRestoreconCommand(pkgfile):
570 remote_path = os.path.join("/testdir/packages/to/", pkgfile)
571 return [
572 [
573 "cd",
574 "/",
575 "&&",
576 "tar",
577 "tf",
578 remote_path,
579 "|",
580 "restorecon",
581 "-i",
582 "-f",
583 "-",
584 ]
585 ]
Qijiang Fan8a945032019-04-25 20:53:29 +0900586
Alex Klein1699fab2022-09-08 08:46:06 -0600587 self.device.device.selinux_available = True
588 packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"]
589 cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"]
590 self.package_scanner.return_value = PackageScannerFake(
591 packages,
592 {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}},
593 cpvs,
594 )
595 self.PatchObject(os.path, "isfile", side_effect=FakeIsFile)
Qijiang Fan8a945032019-04-25 20:53:29 +0900596
Alex Klein1699fab2022-09-08 08:46:06 -0600597 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Qijiang Fan8a945032019-04-25 20:53:29 +0900598
Alex Klein1699fab2022-09-08 08:46:06 -0600599 # Check that package names were correctly resolved into binary packages.
600 self.get_packages_paths.assert_called_once_with(
601 [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot
602 )
603 # Check that deploy._Emerge is called the right number of times.
604 self.assertEqual(self.emerge.call_count, 1)
605 self.assertEqual(self.unmerge.call_count, 0)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700606
Alex Klein1699fab2022-09-08 08:46:06 -0600607 self.assertEqual(
608 self.device.device.cmds,
609 [["setenforce", "0"]]
610 + GetRestoreconCommand("foo-1.2.3.tbz2")
611 + GetRestoreconCommand("bar-1.2.5.tbz2")
612 + GetRestoreconCommand("foobar-2.0.tbz2")
613 + [["setenforce", "1"]],
614 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700615
Alex Klein1699fab2022-09-08 08:46:06 -0600616 def testDeployUnmerge(self):
617 """Test that deploy._Unmerge is called for each package."""
618 packages = ["foo", "bar", "foobar", "foodlc"]
619 self.package_scanner.return_value = PackageScannerFake(
620 packages,
621 {
622 "foo": {},
623 "bar": {},
624 "foobar": {},
625 "foodlc": {
626 deploy._DLC_ID: "foodlc",
627 deploy._DLC_PACKAGE: "foopackage",
628 },
629 },
630 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700631
Alex Klein1699fab2022-09-08 08:46:06 -0600632 deploy.Deploy(
633 None, ["package"], force=True, clean_binpkg=False, emerge=False
634 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700635
Alex Klein1699fab2022-09-08 08:46:06 -0600636 # Check that deploy._Unmerge is called the right number of times.
637 self.assertEqual(self.emerge.call_count, 0)
638 self.unmerge.assert_called_once_with(mock.ANY, packages, "/")
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700639
Alex Klein1699fab2022-09-08 08:46:06 -0600640 self.assertEqual(
641 self.device.device.cmds,
642 [
643 ["dlcservice_util", "--uninstall", "--id=foodlc"],
644 ["restart", "dlcservice"],
645 ],
646 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700647
Alex Klein1699fab2022-09-08 08:46:06 -0600648 def testDeployMergeWithProgressBar(self):
649 """Test that BrilloDeployOperation.Run() is called for merge."""
650 packages = ["foo", "bar", "foobar"]
651 self.package_scanner.return_value = PackageScannerFake(
652 packages, {"foo": {}, "bar": {}, "foobar": {}}
653 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700654
Alex Klein1699fab2022-09-08 08:46:06 -0600655 run = self.PatchObject(
656 deploy.BrilloDeployOperation, "Run", return_value=None
657 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700658
Alex Klein1699fab2022-09-08 08:46:06 -0600659 self.PatchObject(command, "UseProgressBar", return_value=True)
660 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700661
Alex Klein1699fab2022-09-08 08:46:06 -0600662 # Check that BrilloDeployOperation.Run was called.
663 self.assertTrue(run.called)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700664
Alex Klein1699fab2022-09-08 08:46:06 -0600665 def testDeployUnmergeWithProgressBar(self):
666 """Test that BrilloDeployOperation.Run() is called for unmerge."""
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(
678 None, ["package"], force=True, clean_binpkg=False, emerge=False
679 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700680
Alex Klein1699fab2022-09-08 08:46:06 -0600681 # Check that BrilloDeployOperation.Run was called.
682 self.assertTrue(run.called)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700683
Alex Klein1699fab2022-09-08 08:46:06 -0600684 def testBrilloDeployMergeOperation(self):
685 """Test that BrilloDeployOperation works for merge."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700686
Alex Klein1699fab2022-09-08 08:46:06 -0600687 def func(queue):
688 for event in op.MERGE_EVENTS:
689 queue.get()
690 print(event)
691 sys.stdout.flush()
Ralph Nathane01ccf12015-04-16 10:40:32 -0700692
Alex Klein1699fab2022-09-08 08:46:06 -0600693 queue = multiprocessing.Queue()
694 # Emerge one package.
695 op = BrilloDeployOperationFake(True, queue)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700696
Alex Klein1699fab2022-09-08 08:46:06 -0600697 with self.OutputCapturer():
698 op.Run(func, queue)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700699
Alex Klein1699fab2022-09-08 08:46:06 -0600700 # Check that the progress bar prints correctly.
701 self.AssertProgressBarAllEvents(len(op.MERGE_EVENTS))
Ralph Nathane01ccf12015-04-16 10:40:32 -0700702
Alex Klein1699fab2022-09-08 08:46:06 -0600703 def testBrilloDeployUnmergeOperation(self):
704 """Test that BrilloDeployOperation works for unmerge."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700705
Alex Klein1699fab2022-09-08 08:46:06 -0600706 def func(queue):
707 for event in op.UNMERGE_EVENTS:
708 queue.get()
709 print(event)
710 sys.stdout.flush()
711
712 queue = multiprocessing.Queue()
713 # Unmerge one package.
714 op = BrilloDeployOperationFake(False, queue)
715
716 with self.OutputCapturer():
717 op.Run(func, queue)
718
719 # Check that the progress bar prints correctly.
720 self.AssertProgressBarAllEvents(len(op.UNMERGE_EVENTS))