blob: dceec2deda23eef898c8bce70eb2d032eb2a868b [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
Ralph Nathane01ccf12015-04-16 10:40:32 -070047class ChromiumOSDeviceFake(object):
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
Alex Klein1699fab2022-09-08 08:46:06 -060071 def run(self, cmd, **_kwargs):
72 self.cmds.append(cmd)
Andrewc7e1c6b2020-02-27 16:03:53 -080073
Alex Klein1699fab2022-09-08 08:46:06 -060074 def CopyToDevice(self, _src, _dest, _mode="rsync", **_kwargs):
Jae Hoon Kimdf220852023-04-14 19:20:13 +000075 if os.path.exists(_src):
76 self.copy_store = osutils.ReadFile(_src)
Alex Klein1699fab2022-09-08 08:46:06 -060077 return True
Andrewc7e1c6b2020-02-27 16:03:53 -080078
Jae Hoon Kimdf220852023-04-14 19:20:13 +000079 def CatFile(self, _src):
80 return self.cat_file_output
81
Ralph Nathane01ccf12015-04-16 10:40:32 -070082
David Pursell9476bf42015-03-30 13:34:27 -070083class ChromiumOSDeviceHandlerFake(object):
Alex Klein1699fab2022-09-08 08:46:06 -060084 """Fake for chromite.lib.remote_access.ChomiumOSDeviceHandler."""
David Pursell9476bf42015-03-30 13:34:27 -070085
Alex Klein1699fab2022-09-08 08:46:06 -060086 class RemoteAccessFake(object):
87 """Fake for chromite.lib.remote_access.RemoteAccess."""
David Pursell9476bf42015-03-30 13:34:27 -070088
Alex Klein1699fab2022-09-08 08:46:06 -060089 def __init__(self):
90 self.remote_sh_output = None
David Pursell9476bf42015-03-30 13:34:27 -070091
Alex Klein1699fab2022-09-08 08:46:06 -060092 def RemoteSh(self, *_args, **_kwargs):
93 return cros_build_lib.CompletedProcess(stdout=self.remote_sh_output)
David Pursell9476bf42015-03-30 13:34:27 -070094
Alex Klein1699fab2022-09-08 08:46:06 -060095 def __init__(self, *_args, **_kwargs):
96 self._agent = self.RemoteAccessFake()
97 self.device = ChromiumOSDeviceFake()
David Pursell67a82762015-04-30 17:26:59 -070098
Mike Frysingerc0780a62022-08-29 04:41:56 -040099 @property
100 def agent(self):
Alex Klein1699fab2022-09-08 08:46:06 -0600101 return self._agent
David Pursell9476bf42015-03-30 13:34:27 -0700102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 def __exit__(self, _type, _value, _traceback):
104 pass
Ralph Nathane01ccf12015-04-16 10:40:32 -0700105
Alex Klein1699fab2022-09-08 08:46:06 -0600106 def __enter__(self):
107 return self.device
Ralph Nathane01ccf12015-04-16 10:40:32 -0700108
109
110class BrilloDeployOperationFake(deploy.BrilloDeployOperation):
Alex Klein1699fab2022-09-08 08:46:06 -0600111 """Fake for deploy.BrilloDeployOperation."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700112
Alex Klein1699fab2022-09-08 08:46:06 -0600113 def __init__(self, emerge, queue):
114 super().__init__(emerge)
115 self._queue = queue
116
117 def ParseOutput(self, output=None):
118 super().ParseOutput(output)
119 self._queue.put("advance")
Ralph Nathane01ccf12015-04-16 10:40:32 -0700120
David Pursell9476bf42015-03-30 13:34:27 -0700121
122class DbApiFake(object):
Alex Klein1699fab2022-09-08 08:46:06 -0600123 """Fake for Portage dbapi."""
David Pursell9476bf42015-03-30 13:34:27 -0700124
Alex Klein1699fab2022-09-08 08:46:06 -0600125 def __init__(self, pkgs):
126 self.pkg_db = {}
Tim Baine4a783b2023-04-21 20:05:51 +0000127 for cpv, slot, rdeps_raw, build_time, use in pkgs:
Alex Klein1699fab2022-09-08 08:46:06 -0600128 self.pkg_db[cpv] = {
129 "SLOT": slot,
130 "RDEPEND": rdeps_raw,
131 "BUILD_TIME": build_time,
Tim Baine4a783b2023-04-21 20:05:51 +0000132 "USE": use,
Alex Klein1699fab2022-09-08 08:46:06 -0600133 }
David Pursell9476bf42015-03-30 13:34:27 -0700134
Alex Klein1699fab2022-09-08 08:46:06 -0600135 def cpv_all(self):
136 return list(self.pkg_db)
David Pursell9476bf42015-03-30 13:34:27 -0700137
Alex Klein1699fab2022-09-08 08:46:06 -0600138 def aux_get(self, cpv, keys):
139 pkg_info = self.pkg_db[cpv]
140 return [pkg_info[key] for key in keys]
David Pursell9476bf42015-03-30 13:34:27 -0700141
142
Ralph Nathane01ccf12015-04-16 10:40:32 -0700143class PackageScannerFake(object):
Alex Klein1699fab2022-09-08 08:46:06 -0600144 """Fake for PackageScanner."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700145
Alex Klein1699fab2022-09-08 08:46:06 -0600146 def __init__(self, packages, pkgs_attrs, packages_cpvs=None):
147 self.pkgs = packages
148 self.cpvs = packages_cpvs or packages
149 self.listed = []
150 self.num_updates = 0
151 self.pkgs_attrs = pkgs_attrs
Tim Baine4a783b2023-04-21 20:05:51 +0000152 self.warnings_shown = False
Ralph Nathane01ccf12015-04-16 10:40:32 -0700153
Alex Klein1699fab2022-09-08 08:46:06 -0600154 def Run(self, _device, _root, _packages, _update, _deep, _deep_rev):
Tim Baine4a783b2023-04-21 20:05:51 +0000155 return (
156 self.cpvs,
157 self.listed,
158 self.num_updates,
159 self.pkgs_attrs,
160 self.warnings_shown,
161 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700162
163
David Pursell9476bf42015-03-30 13:34:27 -0700164class PortageTreeFake(object):
Alex Klein1699fab2022-09-08 08:46:06 -0600165 """Fake for Portage tree."""
David Pursell9476bf42015-03-30 13:34:27 -0700166
Alex Klein1699fab2022-09-08 08:46:06 -0600167 def __init__(self, dbapi):
168 self.dbapi = dbapi
David Pursell9476bf42015-03-30 13:34:27 -0700169
170
Ralph Nathane01ccf12015-04-16 10:40:32 -0700171class TestInstallPackageScanner(cros_test_lib.MockOutputTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600172 """Test the update package scanner."""
David Pursell9476bf42015-03-30 13:34:27 -0700173
Alex Klein1699fab2022-09-08 08:46:06 -0600174 _BOARD = "foo_board"
175 _BUILD_ROOT = "/build/%s" % _BOARD
176 _VARTREE = [
Tim Baine4a783b2023-04-21 20:05:51 +0000177 (
178 "foo/app1-1.2.3-r4",
179 "0",
180 "foo/app2 !foo/app3",
181 "1413309336",
182 "cros-debug",
183 ),
184 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
185 (
186 "foo/app4-2.0.0-r1",
187 "0",
188 "foo/app1 foo/app5",
189 "1413309336",
190 "cros-debug",
191 ),
192 ("foo/app5-3.0.7-r3", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600193 ]
David Pursell9476bf42015-03-30 13:34:27 -0700194
Alex Klein1699fab2022-09-08 08:46:06 -0600195 def setUp(self):
196 """Patch imported modules."""
197 self.PatchObject(cros_build_lib, "GetChoice", return_value=0)
198 self.device = ChromiumOSDeviceHandlerFake()
199 self.scanner = deploy._InstallPackageScanner(self._BUILD_ROOT)
200 self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None))
Tim Baine4a783b2023-04-21 20:05:51 +0000201 self.PatchObject(
202 deploy, "_ConfirmUpdateDespiteWarnings", return_value=True
203 )
David Pursell9476bf42015-03-30 13:34:27 -0700204
Alex Klein1699fab2022-09-08 08:46:06 -0600205 def SetupVartree(self, vartree_pkgs):
Mike Frysingerc0780a62022-08-29 04:41:56 -0400206 self.device.agent.remote_sh_output = json.dumps(vartree_pkgs)
David Pursell9476bf42015-03-30 13:34:27 -0700207
Alex Klein1699fab2022-09-08 08:46:06 -0600208 def SetupBintree(self, bintree_pkgs):
209 bintree = PortageTreeFake(DbApiFake(bintree_pkgs))
210 build_root = os.path.join(self._BUILD_ROOT, "")
211 portage_db = {build_root: {"bintree": bintree}}
212 self.PatchObject(portage, "create_trees", return_value=portage_db)
David Pursell9476bf42015-03-30 13:34:27 -0700213
Alex Klein1699fab2022-09-08 08:46:06 -0600214 def ValidatePkgs(self, actual, expected, constraints=None):
215 # Containing exactly the same packages.
216 self.assertEqual(sorted(expected), sorted(actual))
217 # Packages appear in the right order.
218 if constraints is not None:
219 for needs, needed in constraints:
220 self.assertGreater(actual.index(needs), actual.index(needed))
David Pursell9476bf42015-03-30 13:34:27 -0700221
Alex Klein1699fab2022-09-08 08:46:06 -0600222 def testRunUpdatedVersion(self):
223 self.SetupVartree(self._VARTREE)
224 app1 = "foo/app1-1.2.5-r4"
225 self.SetupBintree(
226 [
Tim Baine4a783b2023-04-21 20:05:51 +0000227 (app1, "0", "foo/app2 !foo/app3", "1413309336", "cros-debug"),
228 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600229 ]
230 )
Tim Baine4a783b2023-04-21 20:05:51 +0000231 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600232 self.device, "/", ["app1"], True, True, True
233 )
234 self.ValidatePkgs(installs, [app1])
235 self.ValidatePkgs(listed, [app1])
236 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700237
Tim Baine4a783b2023-04-21 20:05:51 +0000238 def testRunUpdatedVersionWithUseMismatch(self):
239 self.SetupVartree(self._VARTREE)
240 app1 = "foo/app1-1.2.5-r4"
241 # Setup the bintree with packages that don't have USE=cros-debug.
242 self.SetupBintree(
243 [
244 (app1, "0", "foo/app2 !foo/app3", "1413309336", ""),
245 ("foo/app2-4.5.6-r7", "0", "", "1413309336", ""),
246 ]
247 )
248 with self.assertLogs(level="WARN") as cm:
249 installs, listed, num_updates, _, _ = self.scanner.Run(
250 self.device, "/", ["app1"], True, True, True
251 )
252 self.ValidatePkgs(installs, [app1])
253 self.ValidatePkgs(listed, [app1])
254 self.assertEqual(num_updates, 1)
255 testline = "USE flags for package foo/app1 do not match"
256 matching_logs = [
257 logline for logline in cm.output if testline in logline
258 ]
259 self.assertTrue(
260 matching_logs, "Failed to detect USE flag mismatch."
261 )
262
Alex Klein1699fab2022-09-08 08:46:06 -0600263 def testRunUpdatedBuildTime(self):
264 self.SetupVartree(self._VARTREE)
265 app1 = "foo/app1-1.2.3-r4"
266 self.SetupBintree(
267 [
Tim Baine4a783b2023-04-21 20:05:51 +0000268 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
269 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600270 ]
271 )
Tim Baine4a783b2023-04-21 20:05:51 +0000272 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600273 self.device, "/", ["app1"], True, True, True
274 )
275 self.ValidatePkgs(installs, [app1])
276 self.ValidatePkgs(listed, [app1])
277 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700278
Alex Klein1699fab2022-09-08 08:46:06 -0600279 def testRunExistingDepUpdated(self):
280 self.SetupVartree(self._VARTREE)
281 app1 = "foo/app1-1.2.5-r2"
282 app2 = "foo/app2-4.5.8-r3"
283 self.SetupBintree(
284 [
Tim Baine4a783b2023-04-21 20:05:51 +0000285 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
286 (app2, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600287 ]
288 )
Tim Baine4a783b2023-04-21 20:05:51 +0000289 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600290 self.device, "/", ["app1"], True, True, True
291 )
292 self.ValidatePkgs(installs, [app1, app2], constraints=[(app1, app2)])
293 self.ValidatePkgs(listed, [app1])
294 self.assertEqual(num_updates, 2)
David Pursell9476bf42015-03-30 13:34:27 -0700295
Alex Klein1699fab2022-09-08 08:46:06 -0600296 def testRunMissingDepUpdated(self):
297 self.SetupVartree(self._VARTREE)
298 app1 = "foo/app1-1.2.5-r2"
299 app6 = "foo/app6-1.0.0-r1"
300 self.SetupBintree(
301 [
Tim Baine4a783b2023-04-21 20:05:51 +0000302 (
303 app1,
304 "0",
305 "foo/app2 !foo/app3 foo/app6",
306 "1413309350",
307 "cros-debug",
308 ),
309 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
310 (app6, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600311 ]
312 )
Tim Baine4a783b2023-04-21 20:05:51 +0000313 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600314 self.device, "/", ["app1"], True, True, True
315 )
316 self.ValidatePkgs(installs, [app1, app6], constraints=[(app1, app6)])
317 self.ValidatePkgs(listed, [app1])
318 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700319
Alex Klein1699fab2022-09-08 08:46:06 -0600320 def testRunExistingRevDepUpdated(self):
321 self.SetupVartree(self._VARTREE)
322 app1 = "foo/app1-1.2.5-r2"
323 app4 = "foo/app4-2.0.1-r3"
324 self.SetupBintree(
325 [
Tim Baine4a783b2023-04-21 20:05:51 +0000326 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
327 (app4, "0", "foo/app1 foo/app5", "1413309350", "cros-debug"),
328 ("foo/app5-3.0.7-r3", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600329 ]
330 )
Tim Baine4a783b2023-04-21 20:05:51 +0000331 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600332 self.device, "/", ["app1"], True, True, True
333 )
334 self.ValidatePkgs(installs, [app1, app4], constraints=[(app4, app1)])
335 self.ValidatePkgs(listed, [app1])
336 self.assertEqual(num_updates, 2)
David Pursell9476bf42015-03-30 13:34:27 -0700337
Alex Klein1699fab2022-09-08 08:46:06 -0600338 def testRunMissingRevDepNotUpdated(self):
339 self.SetupVartree(self._VARTREE)
340 app1 = "foo/app1-1.2.5-r2"
341 app6 = "foo/app6-1.0.0-r1"
342 self.SetupBintree(
343 [
Tim Baine4a783b2023-04-21 20:05:51 +0000344 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
345 (app6, "0", "foo/app1", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600346 ]
347 )
Tim Baine4a783b2023-04-21 20:05:51 +0000348 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600349 self.device, "/", ["app1"], True, True, True
350 )
351 self.ValidatePkgs(installs, [app1])
352 self.ValidatePkgs(listed, [app1])
353 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700354
Alex Klein1699fab2022-09-08 08:46:06 -0600355 def testRunTransitiveDepsUpdated(self):
356 self.SetupVartree(self._VARTREE)
357 app1 = "foo/app1-1.2.5-r2"
358 app2 = "foo/app2-4.5.8-r3"
359 app4 = "foo/app4-2.0.0-r1"
360 app5 = "foo/app5-3.0.8-r2"
361 self.SetupBintree(
362 [
Tim Baine4a783b2023-04-21 20:05:51 +0000363 (app1, "0", "foo/app2 !foo/app3", "1413309350", "cros-debug"),
364 (app2, "0", "", "1413309350", "cros-debug"),
365 (app4, "0", "foo/app1 foo/app5", "1413309350", "cros-debug"),
366 (app5, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600367 ]
368 )
Tim Baine4a783b2023-04-21 20:05:51 +0000369 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600370 self.device, "/", ["app1"], True, True, True
371 )
372 self.ValidatePkgs(
373 installs,
374 [app1, app2, app4, app5],
375 constraints=[(app1, app2), (app4, app1), (app4, app5)],
376 )
377 self.ValidatePkgs(listed, [app1])
378 self.assertEqual(num_updates, 4)
David Pursell9476bf42015-03-30 13:34:27 -0700379
Alex Klein1699fab2022-09-08 08:46:06 -0600380 def testRunDisjunctiveDepsExistingUpdated(self):
381 self.SetupVartree(self._VARTREE)
382 app1 = "foo/app1-1.2.5-r2"
383 self.SetupBintree(
384 [
Tim Baine4a783b2023-04-21 20:05:51 +0000385 (
386 app1,
387 "0",
388 "|| ( foo/app6 foo/app2 ) !foo/app3",
389 "1413309350",
390 "cros-debug",
391 ),
392 ("foo/app2-4.5.6-r7", "0", "", "1413309336", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600393 ]
394 )
Tim Baine4a783b2023-04-21 20:05:51 +0000395 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600396 self.device, "/", ["app1"], True, True, True
397 )
398 self.ValidatePkgs(installs, [app1])
399 self.ValidatePkgs(listed, [app1])
400 self.assertEqual(num_updates, 1)
401
402 def testRunDisjunctiveDepsDefaultUpdated(self):
403 self.SetupVartree(self._VARTREE)
404 app1 = "foo/app1-1.2.5-r2"
405 app7 = "foo/app7-1.0.0-r1"
406 self.SetupBintree(
407 [
Tim Baine4a783b2023-04-21 20:05:51 +0000408 (
409 app1,
410 "0",
411 "|| ( foo/app6 foo/app7 ) !foo/app3",
412 "1413309350",
413 "cros-debug",
414 ),
415 (app7, "0", "", "1413309350", "cros-debug"),
Alex Klein1699fab2022-09-08 08:46:06 -0600416 ]
417 )
Tim Baine4a783b2023-04-21 20:05:51 +0000418 installs, listed, num_updates, _, _ = self.scanner.Run(
Alex Klein1699fab2022-09-08 08:46:06 -0600419 self.device, "/", ["app1"], True, True, True
420 )
421 self.ValidatePkgs(installs, [app1, app7], constraints=[(app1, app7)])
422 self.ValidatePkgs(listed, [app1])
423 self.assertEqual(num_updates, 1)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700424
425
Alex Klein1699fab2022-09-08 08:46:06 -0600426class TestDeploy(
427 cros_test_lib.ProgressBarTestCase, cros_test_lib.MockTempDirTestCase
428):
429 """Test deploy.Deploy."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700430
Alex Klein1699fab2022-09-08 08:46:06 -0600431 @staticmethod
432 def FakeGetPackagesByCPV(cpvs, _strip, _sysroot):
433 return ["/path/to/%s.tbz2" % cpv.pv for cpv in cpvs]
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700434
Alex Klein1699fab2022-09-08 08:46:06 -0600435 def setUp(self):
436 # Fake being root to avoid running filesystem commands with sudo_run.
437 self.PatchObject(osutils, "IsRootUser", return_value=True)
438 self._sysroot = os.path.join(self.tempdir, "sysroot")
439 osutils.SafeMakedirs(self._sysroot)
440 self.device = ChromiumOSDeviceHandlerFake()
441 self.PatchObject(
442 remote_access, "ChromiumOSDeviceHandler", return_value=self.device
443 )
444 self.PatchObject(cros_build_lib, "GetBoard", return_value=None)
445 self.PatchObject(
446 build_target_lib,
447 "get_default_sysroot_path",
448 return_value=self._sysroot,
449 )
450 self.package_scanner = self.PatchObject(
451 deploy, "_InstallPackageScanner"
452 )
453 self.get_packages_paths = self.PatchObject(
454 deploy, "_GetPackagesByCPV", side_effect=self.FakeGetPackagesByCPV
455 )
456 self.emerge = self.PatchObject(deploy, "_Emerge", return_value=None)
457 self.unmerge = self.PatchObject(deploy, "_Unmerge", return_value=None)
458 self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None))
459 # Avoid running the portageq command.
460 sysroot_lib.Sysroot(self._sysroot).WriteConfig(
461 'ARCH="amd64"\nPORTDIR_OVERLAY="%s"' % "/nothing/here"
462 )
463 # make.conf needs to exist to correctly read back config.
464 unittest_lib.create_stub_make_conf(self._sysroot)
Brian Norris2eee8892021-04-06 16:23:23 -0700465
Alex Klein1699fab2022-09-08 08:46:06 -0600466 def testDeployEmerge(self):
467 """Test that deploy._Emerge is called for each package."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700468
Alex Klein1699fab2022-09-08 08:46:06 -0600469 _BINPKG = "/path/to/bar-1.2.5.tbz2"
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700470
Alex Klein1699fab2022-09-08 08:46:06 -0600471 def FakeIsFile(fname):
472 return fname == _BINPKG
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700473
Alex Klein1699fab2022-09-08 08:46:06 -0600474 packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"]
475 cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"]
476 self.package_scanner.return_value = PackageScannerFake(
477 packages,
478 {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}},
479 cpvs,
480 )
481 self.PatchObject(os.path, "isfile", side_effect=FakeIsFile)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700482
Alex Klein1699fab2022-09-08 08:46:06 -0600483 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700484
Alex Klein1699fab2022-09-08 08:46:06 -0600485 # Check that package names were correctly resolved into binary packages.
486 self.get_packages_paths.assert_called_once_with(
487 [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot
488 )
489 # Check that deploy._Emerge is called the right number of times.
490 self.emerge.assert_called_once_with(
491 mock.ANY,
492 [
493 "/path/to/foo-1.2.3.tbz2",
494 "/path/to/bar-1.2.5.tbz2",
495 "/path/to/foobar-2.0.tbz2",
496 ],
497 "/",
498 extra_args=None,
499 )
500 self.assertEqual(self.unmerge.call_count, 0)
Qijiang Fan8a945032019-04-25 20:53:29 +0900501
Alex Klein1699fab2022-09-08 08:46:06 -0600502 def testDeployEmergeDLC(self):
503 """Test that deploy._Emerge installs images for DLC packages."""
504 packages = ["some/foodlc-1.0", "some/bardlc-2.0"]
505 cpvs = ["some/foodlc-1.0", "some/bardlc-2.0"]
506 self.package_scanner.return_value = PackageScannerFake(
507 packages, {"some/foodlc-1.0": {}, "some/bardlc-2.0": {}}, cpvs
508 )
509 self.PatchObject(
510 deploy, "_GetDLCInfo", return_value=("foo_id", "foo_package")
511 )
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700512
Alex Klein1699fab2022-09-08 08:46:06 -0600513 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
514 # Check that dlcservice is restarted (DLC modules are deployed).
515 self.assertTrue(["restart", "dlcservice"] in self.device.device.cmds)
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700516
Jae Hoon Kimdf220852023-04-14 19:20:13 +0000517 def testDeployDLCLoadPinMissingDeviceDigests(self):
518 """Test that _DeployDLCLoadPin works with missing device digests."""
519 osutils.WriteFile(
520 self.tempdir
521 / dlc_lib.DLC_META_DIR
522 / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
523 LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
524 makedirs=True,
525 )
526 with self.device as d:
527 deploy._DeployDLCLoadPin(self.tempdir, d)
528 self.assertEqual(
529 d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
530 )
531 self.assertFalse(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
532 self.assertTrue(
533 "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
534 in d.copy_store.splitlines()
535 )
536
537 def testDeployDLCLoadPinFeedNewDigests(self):
538 """Test that _DeployDLCLoadPin works with digest format file."""
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 d.cat_file_output = DLC_LOADPIN_DIGEST
548 deploy._DeployDLCLoadPin(self.tempdir, d)
549 self.assertEqual(
550 d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
551 )
552 self.assertTrue(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
553 self.assertTrue(
554 "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
555 in d.copy_store.splitlines()
556 )
557
Alex Klein1699fab2022-09-08 08:46:06 -0600558 def testDeployEmergeSELinux(self):
559 """Test deploy progress when the device has SELinux"""
Qijiang Fan8a945032019-04-25 20:53:29 +0900560
Alex Klein1699fab2022-09-08 08:46:06 -0600561 _BINPKG = "/path/to/bar-1.2.5.tbz2"
Qijiang Fan8a945032019-04-25 20:53:29 +0900562
Alex Klein1699fab2022-09-08 08:46:06 -0600563 def FakeIsFile(fname):
564 return fname == _BINPKG
Qijiang Fan8a945032019-04-25 20:53:29 +0900565
Alex Klein1699fab2022-09-08 08:46:06 -0600566 def GetRestoreconCommand(pkgfile):
567 remote_path = os.path.join("/testdir/packages/to/", pkgfile)
568 return [
569 [
570 "cd",
571 "/",
572 "&&",
573 "tar",
574 "tf",
575 remote_path,
576 "|",
577 "restorecon",
578 "-i",
579 "-f",
580 "-",
581 ]
582 ]
Qijiang Fan8a945032019-04-25 20:53:29 +0900583
Alex Klein1699fab2022-09-08 08:46:06 -0600584 self.device.device.selinux_available = True
585 packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"]
586 cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"]
587 self.package_scanner.return_value = PackageScannerFake(
588 packages,
589 {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}},
590 cpvs,
591 )
592 self.PatchObject(os.path, "isfile", side_effect=FakeIsFile)
Qijiang Fan8a945032019-04-25 20:53:29 +0900593
Alex Klein1699fab2022-09-08 08:46:06 -0600594 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Qijiang Fan8a945032019-04-25 20:53:29 +0900595
Alex Klein1699fab2022-09-08 08:46:06 -0600596 # Check that package names were correctly resolved into binary packages.
597 self.get_packages_paths.assert_called_once_with(
598 [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot
599 )
600 # Check that deploy._Emerge is called the right number of times.
601 self.assertEqual(self.emerge.call_count, 1)
602 self.assertEqual(self.unmerge.call_count, 0)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700603
Alex Klein1699fab2022-09-08 08:46:06 -0600604 self.assertEqual(
605 self.device.device.cmds,
606 [["setenforce", "0"]]
607 + GetRestoreconCommand("foo-1.2.3.tbz2")
608 + GetRestoreconCommand("bar-1.2.5.tbz2")
609 + GetRestoreconCommand("foobar-2.0.tbz2")
610 + [["setenforce", "1"]],
611 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700612
Alex Klein1699fab2022-09-08 08:46:06 -0600613 def testDeployUnmerge(self):
614 """Test that deploy._Unmerge is called for each package."""
615 packages = ["foo", "bar", "foobar", "foodlc"]
616 self.package_scanner.return_value = PackageScannerFake(
617 packages,
618 {
619 "foo": {},
620 "bar": {},
621 "foobar": {},
622 "foodlc": {
623 deploy._DLC_ID: "foodlc",
624 deploy._DLC_PACKAGE: "foopackage",
625 },
626 },
627 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700628
Alex Klein1699fab2022-09-08 08:46:06 -0600629 deploy.Deploy(
630 None, ["package"], force=True, clean_binpkg=False, emerge=False
631 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700632
Alex Klein1699fab2022-09-08 08:46:06 -0600633 # Check that deploy._Unmerge is called the right number of times.
634 self.assertEqual(self.emerge.call_count, 0)
635 self.unmerge.assert_called_once_with(mock.ANY, packages, "/")
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700636
Alex Klein1699fab2022-09-08 08:46:06 -0600637 self.assertEqual(
638 self.device.device.cmds,
639 [
640 ["dlcservice_util", "--uninstall", "--id=foodlc"],
641 ["restart", "dlcservice"],
642 ],
643 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700644
Alex Klein1699fab2022-09-08 08:46:06 -0600645 def testDeployMergeWithProgressBar(self):
646 """Test that BrilloDeployOperation.Run() is called for merge."""
647 packages = ["foo", "bar", "foobar"]
648 self.package_scanner.return_value = PackageScannerFake(
649 packages, {"foo": {}, "bar": {}, "foobar": {}}
650 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700651
Alex Klein1699fab2022-09-08 08:46:06 -0600652 run = self.PatchObject(
653 deploy.BrilloDeployOperation, "Run", return_value=None
654 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700655
Alex Klein1699fab2022-09-08 08:46:06 -0600656 self.PatchObject(command, "UseProgressBar", return_value=True)
657 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700658
Alex Klein1699fab2022-09-08 08:46:06 -0600659 # Check that BrilloDeployOperation.Run was called.
660 self.assertTrue(run.called)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700661
Alex Klein1699fab2022-09-08 08:46:06 -0600662 def testDeployUnmergeWithProgressBar(self):
663 """Test that BrilloDeployOperation.Run() is called for unmerge."""
664 packages = ["foo", "bar", "foobar"]
665 self.package_scanner.return_value = PackageScannerFake(
666 packages, {"foo": {}, "bar": {}, "foobar": {}}
667 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700668
Alex Klein1699fab2022-09-08 08:46:06 -0600669 run = self.PatchObject(
670 deploy.BrilloDeployOperation, "Run", return_value=None
671 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700672
Alex Klein1699fab2022-09-08 08:46:06 -0600673 self.PatchObject(command, "UseProgressBar", return_value=True)
674 deploy.Deploy(
675 None, ["package"], force=True, clean_binpkg=False, emerge=False
676 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700677
Alex Klein1699fab2022-09-08 08:46:06 -0600678 # Check that BrilloDeployOperation.Run was called.
679 self.assertTrue(run.called)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700680
Alex Klein1699fab2022-09-08 08:46:06 -0600681 def testBrilloDeployMergeOperation(self):
682 """Test that BrilloDeployOperation works for merge."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700683
Alex Klein1699fab2022-09-08 08:46:06 -0600684 def func(queue):
685 for event in op.MERGE_EVENTS:
686 queue.get()
687 print(event)
688 sys.stdout.flush()
Ralph Nathane01ccf12015-04-16 10:40:32 -0700689
Alex Klein1699fab2022-09-08 08:46:06 -0600690 queue = multiprocessing.Queue()
691 # Emerge one package.
692 op = BrilloDeployOperationFake(True, queue)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700693
Alex Klein1699fab2022-09-08 08:46:06 -0600694 with self.OutputCapturer():
695 op.Run(func, queue)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700696
Alex Klein1699fab2022-09-08 08:46:06 -0600697 # Check that the progress bar prints correctly.
698 self.AssertProgressBarAllEvents(len(op.MERGE_EVENTS))
Ralph Nathane01ccf12015-04-16 10:40:32 -0700699
Alex Klein1699fab2022-09-08 08:46:06 -0600700 def testBrilloDeployUnmergeOperation(self):
701 """Test that BrilloDeployOperation works for unmerge."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700702
Alex Klein1699fab2022-09-08 08:46:06 -0600703 def func(queue):
704 for event in op.UNMERGE_EVENTS:
705 queue.get()
706 print(event)
707 sys.stdout.flush()
708
709 queue = multiprocessing.Queue()
710 # Unmerge one package.
711 op = BrilloDeployOperationFake(False, queue)
712
713 with self.OutputCapturer():
714 op.Run(func, queue)
715
716 # Check that the progress bar prints correctly.
717 self.AssertProgressBarAllEvents(len(op.UNMERGE_EVENTS))