blob: b462e41af5d3acb10ae11a6340e328f08b344d3b [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 = {}
127 for cpv, slot, rdeps_raw, build_time in pkgs:
128 self.pkg_db[cpv] = {
129 "SLOT": slot,
130 "RDEPEND": rdeps_raw,
131 "BUILD_TIME": build_time,
132 }
David Pursell9476bf42015-03-30 13:34:27 -0700133
Alex Klein1699fab2022-09-08 08:46:06 -0600134 def cpv_all(self):
135 return list(self.pkg_db)
David Pursell9476bf42015-03-30 13:34:27 -0700136
Alex Klein1699fab2022-09-08 08:46:06 -0600137 def aux_get(self, cpv, keys):
138 pkg_info = self.pkg_db[cpv]
139 return [pkg_info[key] for key in keys]
David Pursell9476bf42015-03-30 13:34:27 -0700140
141
Ralph Nathane01ccf12015-04-16 10:40:32 -0700142class PackageScannerFake(object):
Alex Klein1699fab2022-09-08 08:46:06 -0600143 """Fake for PackageScanner."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700144
Alex Klein1699fab2022-09-08 08:46:06 -0600145 def __init__(self, packages, pkgs_attrs, packages_cpvs=None):
146 self.pkgs = packages
147 self.cpvs = packages_cpvs or packages
148 self.listed = []
149 self.num_updates = 0
150 self.pkgs_attrs = pkgs_attrs
Ralph Nathane01ccf12015-04-16 10:40:32 -0700151
Alex Klein1699fab2022-09-08 08:46:06 -0600152 def Run(self, _device, _root, _packages, _update, _deep, _deep_rev):
153 return self.cpvs, self.listed, self.num_updates, self.pkgs_attrs
Ralph Nathane01ccf12015-04-16 10:40:32 -0700154
155
David Pursell9476bf42015-03-30 13:34:27 -0700156class PortageTreeFake(object):
Alex Klein1699fab2022-09-08 08:46:06 -0600157 """Fake for Portage tree."""
David Pursell9476bf42015-03-30 13:34:27 -0700158
Alex Klein1699fab2022-09-08 08:46:06 -0600159 def __init__(self, dbapi):
160 self.dbapi = dbapi
David Pursell9476bf42015-03-30 13:34:27 -0700161
162
Ralph Nathane01ccf12015-04-16 10:40:32 -0700163class TestInstallPackageScanner(cros_test_lib.MockOutputTestCase):
Alex Klein1699fab2022-09-08 08:46:06 -0600164 """Test the update package scanner."""
David Pursell9476bf42015-03-30 13:34:27 -0700165
Alex Klein1699fab2022-09-08 08:46:06 -0600166 _BOARD = "foo_board"
167 _BUILD_ROOT = "/build/%s" % _BOARD
168 _VARTREE = [
169 ("foo/app1-1.2.3-r4", "0", "foo/app2 !foo/app3", "1413309336"),
170 ("foo/app2-4.5.6-r7", "0", "", "1413309336"),
171 ("foo/app4-2.0.0-r1", "0", "foo/app1 foo/app5", "1413309336"),
172 ("foo/app5-3.0.7-r3", "0", "", "1413309336"),
173 ]
David Pursell9476bf42015-03-30 13:34:27 -0700174
Alex Klein1699fab2022-09-08 08:46:06 -0600175 def setUp(self):
176 """Patch imported modules."""
177 self.PatchObject(cros_build_lib, "GetChoice", return_value=0)
178 self.device = ChromiumOSDeviceHandlerFake()
179 self.scanner = deploy._InstallPackageScanner(self._BUILD_ROOT)
180 self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None))
David Pursell9476bf42015-03-30 13:34:27 -0700181
Alex Klein1699fab2022-09-08 08:46:06 -0600182 def SetupVartree(self, vartree_pkgs):
Mike Frysingerc0780a62022-08-29 04:41:56 -0400183 self.device.agent.remote_sh_output = json.dumps(vartree_pkgs)
David Pursell9476bf42015-03-30 13:34:27 -0700184
Alex Klein1699fab2022-09-08 08:46:06 -0600185 def SetupBintree(self, bintree_pkgs):
186 bintree = PortageTreeFake(DbApiFake(bintree_pkgs))
187 build_root = os.path.join(self._BUILD_ROOT, "")
188 portage_db = {build_root: {"bintree": bintree}}
189 self.PatchObject(portage, "create_trees", return_value=portage_db)
David Pursell9476bf42015-03-30 13:34:27 -0700190
Alex Klein1699fab2022-09-08 08:46:06 -0600191 def ValidatePkgs(self, actual, expected, constraints=None):
192 # Containing exactly the same packages.
193 self.assertEqual(sorted(expected), sorted(actual))
194 # Packages appear in the right order.
195 if constraints is not None:
196 for needs, needed in constraints:
197 self.assertGreater(actual.index(needs), actual.index(needed))
David Pursell9476bf42015-03-30 13:34:27 -0700198
Alex Klein1699fab2022-09-08 08:46:06 -0600199 def testRunUpdatedVersion(self):
200 self.SetupVartree(self._VARTREE)
201 app1 = "foo/app1-1.2.5-r4"
202 self.SetupBintree(
203 [
204 (app1, "0", "foo/app2 !foo/app3", "1413309336"),
205 ("foo/app2-4.5.6-r7", "0", "", "1413309336"),
206 ]
207 )
208 installs, listed, num_updates, _ = self.scanner.Run(
209 self.device, "/", ["app1"], True, True, True
210 )
211 self.ValidatePkgs(installs, [app1])
212 self.ValidatePkgs(listed, [app1])
213 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700214
Alex Klein1699fab2022-09-08 08:46:06 -0600215 def testRunUpdatedBuildTime(self):
216 self.SetupVartree(self._VARTREE)
217 app1 = "foo/app1-1.2.3-r4"
218 self.SetupBintree(
219 [
220 (app1, "0", "foo/app2 !foo/app3", "1413309350"),
221 ("foo/app2-4.5.6-r7", "0", "", "1413309336"),
222 ]
223 )
224 installs, listed, num_updates, _ = self.scanner.Run(
225 self.device, "/", ["app1"], True, True, True
226 )
227 self.ValidatePkgs(installs, [app1])
228 self.ValidatePkgs(listed, [app1])
229 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700230
Alex Klein1699fab2022-09-08 08:46:06 -0600231 def testRunExistingDepUpdated(self):
232 self.SetupVartree(self._VARTREE)
233 app1 = "foo/app1-1.2.5-r2"
234 app2 = "foo/app2-4.5.8-r3"
235 self.SetupBintree(
236 [
237 (app1, "0", "foo/app2 !foo/app3", "1413309350"),
238 (app2, "0", "", "1413309350"),
239 ]
240 )
241 installs, listed, num_updates, _ = self.scanner.Run(
242 self.device, "/", ["app1"], True, True, True
243 )
244 self.ValidatePkgs(installs, [app1, app2], constraints=[(app1, app2)])
245 self.ValidatePkgs(listed, [app1])
246 self.assertEqual(num_updates, 2)
David Pursell9476bf42015-03-30 13:34:27 -0700247
Alex Klein1699fab2022-09-08 08:46:06 -0600248 def testRunMissingDepUpdated(self):
249 self.SetupVartree(self._VARTREE)
250 app1 = "foo/app1-1.2.5-r2"
251 app6 = "foo/app6-1.0.0-r1"
252 self.SetupBintree(
253 [
254 (app1, "0", "foo/app2 !foo/app3 foo/app6", "1413309350"),
255 ("foo/app2-4.5.6-r7", "0", "", "1413309336"),
256 (app6, "0", "", "1413309350"),
257 ]
258 )
259 installs, listed, num_updates, _ = self.scanner.Run(
260 self.device, "/", ["app1"], True, True, True
261 )
262 self.ValidatePkgs(installs, [app1, app6], constraints=[(app1, app6)])
263 self.ValidatePkgs(listed, [app1])
264 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700265
Alex Klein1699fab2022-09-08 08:46:06 -0600266 def testRunExistingRevDepUpdated(self):
267 self.SetupVartree(self._VARTREE)
268 app1 = "foo/app1-1.2.5-r2"
269 app4 = "foo/app4-2.0.1-r3"
270 self.SetupBintree(
271 [
272 (app1, "0", "foo/app2 !foo/app3", "1413309350"),
273 (app4, "0", "foo/app1 foo/app5", "1413309350"),
274 ("foo/app5-3.0.7-r3", "0", "", "1413309336"),
275 ]
276 )
277 installs, listed, num_updates, _ = self.scanner.Run(
278 self.device, "/", ["app1"], True, True, True
279 )
280 self.ValidatePkgs(installs, [app1, app4], constraints=[(app4, app1)])
281 self.ValidatePkgs(listed, [app1])
282 self.assertEqual(num_updates, 2)
David Pursell9476bf42015-03-30 13:34:27 -0700283
Alex Klein1699fab2022-09-08 08:46:06 -0600284 def testRunMissingRevDepNotUpdated(self):
285 self.SetupVartree(self._VARTREE)
286 app1 = "foo/app1-1.2.5-r2"
287 app6 = "foo/app6-1.0.0-r1"
288 self.SetupBintree(
289 [
290 (app1, "0", "foo/app2 !foo/app3", "1413309350"),
291 (app6, "0", "foo/app1", "1413309350"),
292 ]
293 )
294 installs, listed, num_updates, _ = self.scanner.Run(
295 self.device, "/", ["app1"], True, True, True
296 )
297 self.ValidatePkgs(installs, [app1])
298 self.ValidatePkgs(listed, [app1])
299 self.assertEqual(num_updates, 1)
David Pursell9476bf42015-03-30 13:34:27 -0700300
Alex Klein1699fab2022-09-08 08:46:06 -0600301 def testRunTransitiveDepsUpdated(self):
302 self.SetupVartree(self._VARTREE)
303 app1 = "foo/app1-1.2.5-r2"
304 app2 = "foo/app2-4.5.8-r3"
305 app4 = "foo/app4-2.0.0-r1"
306 app5 = "foo/app5-3.0.8-r2"
307 self.SetupBintree(
308 [
309 (app1, "0", "foo/app2 !foo/app3", "1413309350"),
310 (app2, "0", "", "1413309350"),
311 (app4, "0", "foo/app1 foo/app5", "1413309350"),
312 (app5, "0", "", "1413309350"),
313 ]
314 )
315 installs, listed, num_updates, _ = self.scanner.Run(
316 self.device, "/", ["app1"], True, True, True
317 )
318 self.ValidatePkgs(
319 installs,
320 [app1, app2, app4, app5],
321 constraints=[(app1, app2), (app4, app1), (app4, app5)],
322 )
323 self.ValidatePkgs(listed, [app1])
324 self.assertEqual(num_updates, 4)
David Pursell9476bf42015-03-30 13:34:27 -0700325
Alex Klein1699fab2022-09-08 08:46:06 -0600326 def testRunDisjunctiveDepsExistingUpdated(self):
327 self.SetupVartree(self._VARTREE)
328 app1 = "foo/app1-1.2.5-r2"
329 self.SetupBintree(
330 [
331 (app1, "0", "|| ( foo/app6 foo/app2 ) !foo/app3", "1413309350"),
332 ("foo/app2-4.5.6-r7", "0", "", "1413309336"),
333 ]
334 )
335 installs, listed, num_updates, _ = self.scanner.Run(
336 self.device, "/", ["app1"], True, True, True
337 )
338 self.ValidatePkgs(installs, [app1])
339 self.ValidatePkgs(listed, [app1])
340 self.assertEqual(num_updates, 1)
341
342 def testRunDisjunctiveDepsDefaultUpdated(self):
343 self.SetupVartree(self._VARTREE)
344 app1 = "foo/app1-1.2.5-r2"
345 app7 = "foo/app7-1.0.0-r1"
346 self.SetupBintree(
347 [
348 (app1, "0", "|| ( foo/app6 foo/app7 ) !foo/app3", "1413309350"),
349 (app7, "0", "", "1413309350"),
350 ]
351 )
352 installs, listed, num_updates, _ = self.scanner.Run(
353 self.device, "/", ["app1"], True, True, True
354 )
355 self.ValidatePkgs(installs, [app1, app7], constraints=[(app1, app7)])
356 self.ValidatePkgs(listed, [app1])
357 self.assertEqual(num_updates, 1)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700358
359
Alex Klein1699fab2022-09-08 08:46:06 -0600360class TestDeploy(
361 cros_test_lib.ProgressBarTestCase, cros_test_lib.MockTempDirTestCase
362):
363 """Test deploy.Deploy."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700364
Alex Klein1699fab2022-09-08 08:46:06 -0600365 @staticmethod
366 def FakeGetPackagesByCPV(cpvs, _strip, _sysroot):
367 return ["/path/to/%s.tbz2" % cpv.pv for cpv in cpvs]
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700368
Alex Klein1699fab2022-09-08 08:46:06 -0600369 def setUp(self):
370 # Fake being root to avoid running filesystem commands with sudo_run.
371 self.PatchObject(osutils, "IsRootUser", return_value=True)
372 self._sysroot = os.path.join(self.tempdir, "sysroot")
373 osutils.SafeMakedirs(self._sysroot)
374 self.device = ChromiumOSDeviceHandlerFake()
375 self.PatchObject(
376 remote_access, "ChromiumOSDeviceHandler", return_value=self.device
377 )
378 self.PatchObject(cros_build_lib, "GetBoard", return_value=None)
379 self.PatchObject(
380 build_target_lib,
381 "get_default_sysroot_path",
382 return_value=self._sysroot,
383 )
384 self.package_scanner = self.PatchObject(
385 deploy, "_InstallPackageScanner"
386 )
387 self.get_packages_paths = self.PatchObject(
388 deploy, "_GetPackagesByCPV", side_effect=self.FakeGetPackagesByCPV
389 )
390 self.emerge = self.PatchObject(deploy, "_Emerge", return_value=None)
391 self.unmerge = self.PatchObject(deploy, "_Unmerge", return_value=None)
392 self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None))
393 # Avoid running the portageq command.
394 sysroot_lib.Sysroot(self._sysroot).WriteConfig(
395 'ARCH="amd64"\nPORTDIR_OVERLAY="%s"' % "/nothing/here"
396 )
397 # make.conf needs to exist to correctly read back config.
398 unittest_lib.create_stub_make_conf(self._sysroot)
Brian Norris2eee8892021-04-06 16:23:23 -0700399
Alex Klein1699fab2022-09-08 08:46:06 -0600400 def testDeployEmerge(self):
401 """Test that deploy._Emerge is called for each package."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700402
Alex Klein1699fab2022-09-08 08:46:06 -0600403 _BINPKG = "/path/to/bar-1.2.5.tbz2"
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700404
Alex Klein1699fab2022-09-08 08:46:06 -0600405 def FakeIsFile(fname):
406 return fname == _BINPKG
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700407
Alex Klein1699fab2022-09-08 08:46:06 -0600408 packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"]
409 cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"]
410 self.package_scanner.return_value = PackageScannerFake(
411 packages,
412 {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}},
413 cpvs,
414 )
415 self.PatchObject(os.path, "isfile", side_effect=FakeIsFile)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700416
Alex Klein1699fab2022-09-08 08:46:06 -0600417 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700418
Alex Klein1699fab2022-09-08 08:46:06 -0600419 # Check that package names were correctly resolved into binary packages.
420 self.get_packages_paths.assert_called_once_with(
421 [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot
422 )
423 # Check that deploy._Emerge is called the right number of times.
424 self.emerge.assert_called_once_with(
425 mock.ANY,
426 [
427 "/path/to/foo-1.2.3.tbz2",
428 "/path/to/bar-1.2.5.tbz2",
429 "/path/to/foobar-2.0.tbz2",
430 ],
431 "/",
432 extra_args=None,
433 )
434 self.assertEqual(self.unmerge.call_count, 0)
Qijiang Fan8a945032019-04-25 20:53:29 +0900435
Alex Klein1699fab2022-09-08 08:46:06 -0600436 def testDeployEmergeDLC(self):
437 """Test that deploy._Emerge installs images for DLC packages."""
438 packages = ["some/foodlc-1.0", "some/bardlc-2.0"]
439 cpvs = ["some/foodlc-1.0", "some/bardlc-2.0"]
440 self.package_scanner.return_value = PackageScannerFake(
441 packages, {"some/foodlc-1.0": {}, "some/bardlc-2.0": {}}, cpvs
442 )
443 self.PatchObject(
444 deploy, "_GetDLCInfo", return_value=("foo_id", "foo_package")
445 )
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700446
Alex Klein1699fab2022-09-08 08:46:06 -0600447 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
448 # Check that dlcservice is restarted (DLC modules are deployed).
449 self.assertTrue(["restart", "dlcservice"] in self.device.device.cmds)
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700450
Jae Hoon Kimdf220852023-04-14 19:20:13 +0000451 def testDeployDLCLoadPinMissingDeviceDigests(self):
452 """Test that _DeployDLCLoadPin works with missing device digests."""
453 osutils.WriteFile(
454 self.tempdir
455 / dlc_lib.DLC_META_DIR
456 / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
457 LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
458 makedirs=True,
459 )
460 with self.device as d:
461 deploy._DeployDLCLoadPin(self.tempdir, d)
462 self.assertEqual(
463 d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
464 )
465 self.assertFalse(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
466 self.assertTrue(
467 "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
468 in d.copy_store.splitlines()
469 )
470
471 def testDeployDLCLoadPinFeedNewDigests(self):
472 """Test that _DeployDLCLoadPin works with digest format file."""
473 osutils.WriteFile(
474 self.tempdir
475 / dlc_lib.DLC_META_DIR
476 / dlc_lib.DLC_LOADPIN_TRUSTED_VERITY_DIGESTS,
477 LOADPIN_TRUSTED_VERITY_ROOT_DIGESTS,
478 makedirs=True,
479 )
480 with self.device as d:
481 d.cat_file_output = DLC_LOADPIN_DIGEST
482 deploy._DeployDLCLoadPin(self.tempdir, d)
483 self.assertEqual(
484 d.copy_store.splitlines()[0], dlc_lib.DLC_LOADPIN_FILE_HEADER
485 )
486 self.assertTrue(DLC_LOADPIN_DIGEST in d.copy_store.splitlines())
487 self.assertTrue(
488 "75a799de83eee0ef0f028ea94643d1b2021261e77b8f76fee1d5749847fef431"
489 in d.copy_store.splitlines()
490 )
491
Alex Klein1699fab2022-09-08 08:46:06 -0600492 def testDeployEmergeSELinux(self):
493 """Test deploy progress when the device has SELinux"""
Qijiang Fan8a945032019-04-25 20:53:29 +0900494
Alex Klein1699fab2022-09-08 08:46:06 -0600495 _BINPKG = "/path/to/bar-1.2.5.tbz2"
Qijiang Fan8a945032019-04-25 20:53:29 +0900496
Alex Klein1699fab2022-09-08 08:46:06 -0600497 def FakeIsFile(fname):
498 return fname == _BINPKG
Qijiang Fan8a945032019-04-25 20:53:29 +0900499
Alex Klein1699fab2022-09-08 08:46:06 -0600500 def GetRestoreconCommand(pkgfile):
501 remote_path = os.path.join("/testdir/packages/to/", pkgfile)
502 return [
503 [
504 "cd",
505 "/",
506 "&&",
507 "tar",
508 "tf",
509 remote_path,
510 "|",
511 "restorecon",
512 "-i",
513 "-f",
514 "-",
515 ]
516 ]
Qijiang Fan8a945032019-04-25 20:53:29 +0900517
Alex Klein1699fab2022-09-08 08:46:06 -0600518 self.device.device.selinux_available = True
519 packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"]
520 cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"]
521 self.package_scanner.return_value = PackageScannerFake(
522 packages,
523 {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}},
524 cpvs,
525 )
526 self.PatchObject(os.path, "isfile", side_effect=FakeIsFile)
Qijiang Fan8a945032019-04-25 20:53:29 +0900527
Alex Klein1699fab2022-09-08 08:46:06 -0600528 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Qijiang Fan8a945032019-04-25 20:53:29 +0900529
Alex Klein1699fab2022-09-08 08:46:06 -0600530 # Check that package names were correctly resolved into binary packages.
531 self.get_packages_paths.assert_called_once_with(
532 [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot
533 )
534 # Check that deploy._Emerge is called the right number of times.
535 self.assertEqual(self.emerge.call_count, 1)
536 self.assertEqual(self.unmerge.call_count, 0)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700537
Alex Klein1699fab2022-09-08 08:46:06 -0600538 self.assertEqual(
539 self.device.device.cmds,
540 [["setenforce", "0"]]
541 + GetRestoreconCommand("foo-1.2.3.tbz2")
542 + GetRestoreconCommand("bar-1.2.5.tbz2")
543 + GetRestoreconCommand("foobar-2.0.tbz2")
544 + [["setenforce", "1"]],
545 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700546
Alex Klein1699fab2022-09-08 08:46:06 -0600547 def testDeployUnmerge(self):
548 """Test that deploy._Unmerge is called for each package."""
549 packages = ["foo", "bar", "foobar", "foodlc"]
550 self.package_scanner.return_value = PackageScannerFake(
551 packages,
552 {
553 "foo": {},
554 "bar": {},
555 "foobar": {},
556 "foodlc": {
557 deploy._DLC_ID: "foodlc",
558 deploy._DLC_PACKAGE: "foopackage",
559 },
560 },
561 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700562
Alex Klein1699fab2022-09-08 08:46:06 -0600563 deploy.Deploy(
564 None, ["package"], force=True, clean_binpkg=False, emerge=False
565 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700566
Alex Klein1699fab2022-09-08 08:46:06 -0600567 # Check that deploy._Unmerge is called the right number of times.
568 self.assertEqual(self.emerge.call_count, 0)
569 self.unmerge.assert_called_once_with(mock.ANY, packages, "/")
Xiaochu Liu2726e7c2019-07-18 10:28:10 -0700570
Alex Klein1699fab2022-09-08 08:46:06 -0600571 self.assertEqual(
572 self.device.device.cmds,
573 [
574 ["dlcservice_util", "--uninstall", "--id=foodlc"],
575 ["restart", "dlcservice"],
576 ],
577 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700578
Alex Klein1699fab2022-09-08 08:46:06 -0600579 def testDeployMergeWithProgressBar(self):
580 """Test that BrilloDeployOperation.Run() is called for merge."""
581 packages = ["foo", "bar", "foobar"]
582 self.package_scanner.return_value = PackageScannerFake(
583 packages, {"foo": {}, "bar": {}, "foobar": {}}
584 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700585
Alex Klein1699fab2022-09-08 08:46:06 -0600586 run = self.PatchObject(
587 deploy.BrilloDeployOperation, "Run", return_value=None
588 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700589
Alex Klein1699fab2022-09-08 08:46:06 -0600590 self.PatchObject(command, "UseProgressBar", return_value=True)
591 deploy.Deploy(None, ["package"], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700592
Alex Klein1699fab2022-09-08 08:46:06 -0600593 # Check that BrilloDeployOperation.Run was called.
594 self.assertTrue(run.called)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700595
Alex Klein1699fab2022-09-08 08:46:06 -0600596 def testDeployUnmergeWithProgressBar(self):
597 """Test that BrilloDeployOperation.Run() is called for unmerge."""
598 packages = ["foo", "bar", "foobar"]
599 self.package_scanner.return_value = PackageScannerFake(
600 packages, {"foo": {}, "bar": {}, "foobar": {}}
601 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700602
Alex Klein1699fab2022-09-08 08:46:06 -0600603 run = self.PatchObject(
604 deploy.BrilloDeployOperation, "Run", return_value=None
605 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700606
Alex Klein1699fab2022-09-08 08:46:06 -0600607 self.PatchObject(command, "UseProgressBar", return_value=True)
608 deploy.Deploy(
609 None, ["package"], force=True, clean_binpkg=False, emerge=False
610 )
Ralph Nathane01ccf12015-04-16 10:40:32 -0700611
Alex Klein1699fab2022-09-08 08:46:06 -0600612 # Check that BrilloDeployOperation.Run was called.
613 self.assertTrue(run.called)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700614
Alex Klein1699fab2022-09-08 08:46:06 -0600615 def testBrilloDeployMergeOperation(self):
616 """Test that BrilloDeployOperation works for merge."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700617
Alex Klein1699fab2022-09-08 08:46:06 -0600618 def func(queue):
619 for event in op.MERGE_EVENTS:
620 queue.get()
621 print(event)
622 sys.stdout.flush()
Ralph Nathane01ccf12015-04-16 10:40:32 -0700623
Alex Klein1699fab2022-09-08 08:46:06 -0600624 queue = multiprocessing.Queue()
625 # Emerge one package.
626 op = BrilloDeployOperationFake(True, queue)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700627
Alex Klein1699fab2022-09-08 08:46:06 -0600628 with self.OutputCapturer():
629 op.Run(func, queue)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700630
Alex Klein1699fab2022-09-08 08:46:06 -0600631 # Check that the progress bar prints correctly.
632 self.AssertProgressBarAllEvents(len(op.MERGE_EVENTS))
Ralph Nathane01ccf12015-04-16 10:40:32 -0700633
Alex Klein1699fab2022-09-08 08:46:06 -0600634 def testBrilloDeployUnmergeOperation(self):
635 """Test that BrilloDeployOperation works for unmerge."""
Ralph Nathane01ccf12015-04-16 10:40:32 -0700636
Alex Klein1699fab2022-09-08 08:46:06 -0600637 def func(queue):
638 for event in op.UNMERGE_EVENTS:
639 queue.get()
640 print(event)
641 sys.stdout.flush()
642
643 queue = multiprocessing.Queue()
644 # Unmerge one package.
645 op = BrilloDeployOperationFake(False, queue)
646
647 with self.OutputCapturer():
648 op.Run(func, queue)
649
650 # Check that the progress bar prints correctly.
651 self.AssertProgressBarAllEvents(len(op.UNMERGE_EVENTS))