David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 1 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | # 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 Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 7 | import json |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 8 | import multiprocessing |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 9 | import os |
Mike Frysinger | 050fa5a | 2019-12-01 16:16:29 -0500 | [diff] [blame] | 10 | import sys |
Mike Frysinger | 166fea0 | 2021-02-12 05:30:33 -0500 | [diff] [blame] | 11 | from unittest import mock |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 12 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 13 | from chromite.cli import command |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 14 | from chromite.cli import deploy |
Mike Frysinger | 06a51c8 | 2021-04-06 11:39:17 -0400 | [diff] [blame] | 15 | from chromite.lib import build_target_lib |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib |
| 17 | from chromite.lib import cros_test_lib |
Brian Norris | 2eee889 | 2021-04-06 16:23:23 -0700 | [diff] [blame] | 18 | from chromite.lib import osutils |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 19 | from chromite.lib import remote_access |
Brian Norris | 2eee889 | 2021-04-06 16:23:23 -0700 | [diff] [blame] | 20 | from chromite.lib import sysroot_lib |
Sloan Johnson | a85640f | 2021-10-01 22:32:40 +0000 | [diff] [blame] | 21 | from chromite.lib import unittest_lib |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 22 | from chromite.lib.parser import package_info |
Mike Frysinger | 40ffb53 | 2021-02-12 07:36:08 -0500 | [diff] [blame] | 23 | |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 24 | |
Chris McDonald | 186b9ee | 2020-04-16 05:56:49 -0600 | [diff] [blame] | 25 | pytestmark = [cros_test_lib.pytestmark_inside_only] |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 26 | |
| 27 | |
| 28 | if cros_build_lib.IsInsideChroot(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 29 | import portage # pylint: disable=import-error |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 30 | |
| 31 | |
| 32 | # pylint: disable=protected-access |
| 33 | |
| 34 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 35 | class ChromiumOSDeviceFake(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 36 | """Fake for device.""" |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 37 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | def __init__(self): |
| 39 | self.board = "board" |
| 40 | self.hostname = None |
| 41 | self.username = None |
| 42 | self.port = None |
| 43 | self.lsb_release = None |
| 44 | self.cmds = [] |
| 45 | self.work_dir = "/testdir/" |
| 46 | self.selinux_available = False |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 47 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 48 | def MountRootfsReadWrite(self): |
| 49 | return True |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 50 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | def IsSELinuxAvailable(self): |
| 52 | return self.selinux_available |
Ben Pastene | 5f03b05 | 2019-08-12 18:03:24 -0700 | [diff] [blame] | 53 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | def IsSELinuxEnforced(self): |
| 55 | return True |
Ben Pastene | 5f03b05 | 2019-08-12 18:03:24 -0700 | [diff] [blame] | 56 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | def run(self, cmd, **_kwargs): |
| 58 | self.cmds.append(cmd) |
Andrew | c7e1c6b | 2020-02-27 16:03:53 -0800 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | def CopyToDevice(self, _src, _dest, _mode="rsync", **_kwargs): |
| 61 | return True |
Andrew | c7e1c6b | 2020-02-27 16:03:53 -0800 | [diff] [blame] | 62 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 63 | |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 64 | class ChromiumOSDeviceHandlerFake(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | """Fake for chromite.lib.remote_access.ChomiumOSDeviceHandler.""" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 66 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | class RemoteAccessFake(object): |
| 68 | """Fake for chromite.lib.remote_access.RemoteAccess.""" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 69 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 70 | def __init__(self): |
| 71 | self.remote_sh_output = None |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | def RemoteSh(self, *_args, **_kwargs): |
| 74 | return cros_build_lib.CompletedProcess(stdout=self.remote_sh_output) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 75 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 76 | def __init__(self, *_args, **_kwargs): |
| 77 | self._agent = self.RemoteAccessFake() |
| 78 | self.device = ChromiumOSDeviceFake() |
David Pursell | 67a8276 | 2015-04-30 17:26:59 -0700 | [diff] [blame] | 79 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 80 | # TODO(dpursell): Mock remote access object in cros_test_lib (brbug.com/986). |
| 81 | def GetAgent(self): |
| 82 | return self._agent |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 83 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 84 | def __exit__(self, _type, _value, _traceback): |
| 85 | pass |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 86 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | def __enter__(self): |
| 88 | return self.device |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 89 | |
| 90 | |
| 91 | class BrilloDeployOperationFake(deploy.BrilloDeployOperation): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | """Fake for deploy.BrilloDeployOperation.""" |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 93 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 94 | def __init__(self, emerge, queue): |
| 95 | super().__init__(emerge) |
| 96 | self._queue = queue |
| 97 | |
| 98 | def ParseOutput(self, output=None): |
| 99 | super().ParseOutput(output) |
| 100 | self._queue.put("advance") |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 101 | |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 102 | |
| 103 | class DbApiFake(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 104 | """Fake for Portage dbapi.""" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 105 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | def __init__(self, pkgs): |
| 107 | self.pkg_db = {} |
| 108 | for cpv, slot, rdeps_raw, build_time in pkgs: |
| 109 | self.pkg_db[cpv] = { |
| 110 | "SLOT": slot, |
| 111 | "RDEPEND": rdeps_raw, |
| 112 | "BUILD_TIME": build_time, |
| 113 | } |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 114 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 115 | def cpv_all(self): |
| 116 | return list(self.pkg_db) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | def aux_get(self, cpv, keys): |
| 119 | pkg_info = self.pkg_db[cpv] |
| 120 | return [pkg_info[key] for key in keys] |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 121 | |
| 122 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 123 | class PackageScannerFake(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 124 | """Fake for PackageScanner.""" |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 125 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | def __init__(self, packages, pkgs_attrs, packages_cpvs=None): |
| 127 | self.pkgs = packages |
| 128 | self.cpvs = packages_cpvs or packages |
| 129 | self.listed = [] |
| 130 | self.num_updates = 0 |
| 131 | self.pkgs_attrs = pkgs_attrs |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | def Run(self, _device, _root, _packages, _update, _deep, _deep_rev): |
| 134 | return self.cpvs, self.listed, self.num_updates, self.pkgs_attrs |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 135 | |
| 136 | |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 137 | class PortageTreeFake(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 138 | """Fake for Portage tree.""" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 139 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 140 | def __init__(self, dbapi): |
| 141 | self.dbapi = dbapi |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 142 | |
| 143 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 144 | class TestInstallPackageScanner(cros_test_lib.MockOutputTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | """Test the update package scanner.""" |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 146 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | _BOARD = "foo_board" |
| 148 | _BUILD_ROOT = "/build/%s" % _BOARD |
| 149 | _VARTREE = [ |
| 150 | ("foo/app1-1.2.3-r4", "0", "foo/app2 !foo/app3", "1413309336"), |
| 151 | ("foo/app2-4.5.6-r7", "0", "", "1413309336"), |
| 152 | ("foo/app4-2.0.0-r1", "0", "foo/app1 foo/app5", "1413309336"), |
| 153 | ("foo/app5-3.0.7-r3", "0", "", "1413309336"), |
| 154 | ] |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 155 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 156 | def setUp(self): |
| 157 | """Patch imported modules.""" |
| 158 | self.PatchObject(cros_build_lib, "GetChoice", return_value=0) |
| 159 | self.device = ChromiumOSDeviceHandlerFake() |
| 160 | self.scanner = deploy._InstallPackageScanner(self._BUILD_ROOT) |
| 161 | self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 162 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | def SetupVartree(self, vartree_pkgs): |
| 164 | self.device.GetAgent().remote_sh_output = json.dumps(vartree_pkgs) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 165 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 166 | def SetupBintree(self, bintree_pkgs): |
| 167 | bintree = PortageTreeFake(DbApiFake(bintree_pkgs)) |
| 168 | build_root = os.path.join(self._BUILD_ROOT, "") |
| 169 | portage_db = {build_root: {"bintree": bintree}} |
| 170 | self.PatchObject(portage, "create_trees", return_value=portage_db) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 171 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | def ValidatePkgs(self, actual, expected, constraints=None): |
| 173 | # Containing exactly the same packages. |
| 174 | self.assertEqual(sorted(expected), sorted(actual)) |
| 175 | # Packages appear in the right order. |
| 176 | if constraints is not None: |
| 177 | for needs, needed in constraints: |
| 178 | self.assertGreater(actual.index(needs), actual.index(needed)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 179 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 180 | def testRunUpdatedVersion(self): |
| 181 | self.SetupVartree(self._VARTREE) |
| 182 | app1 = "foo/app1-1.2.5-r4" |
| 183 | self.SetupBintree( |
| 184 | [ |
| 185 | (app1, "0", "foo/app2 !foo/app3", "1413309336"), |
| 186 | ("foo/app2-4.5.6-r7", "0", "", "1413309336"), |
| 187 | ] |
| 188 | ) |
| 189 | installs, listed, num_updates, _ = self.scanner.Run( |
| 190 | self.device, "/", ["app1"], True, True, True |
| 191 | ) |
| 192 | self.ValidatePkgs(installs, [app1]) |
| 193 | self.ValidatePkgs(listed, [app1]) |
| 194 | self.assertEqual(num_updates, 1) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 195 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 196 | def testRunUpdatedBuildTime(self): |
| 197 | self.SetupVartree(self._VARTREE) |
| 198 | app1 = "foo/app1-1.2.3-r4" |
| 199 | self.SetupBintree( |
| 200 | [ |
| 201 | (app1, "0", "foo/app2 !foo/app3", "1413309350"), |
| 202 | ("foo/app2-4.5.6-r7", "0", "", "1413309336"), |
| 203 | ] |
| 204 | ) |
| 205 | installs, listed, num_updates, _ = self.scanner.Run( |
| 206 | self.device, "/", ["app1"], True, True, True |
| 207 | ) |
| 208 | self.ValidatePkgs(installs, [app1]) |
| 209 | self.ValidatePkgs(listed, [app1]) |
| 210 | self.assertEqual(num_updates, 1) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 211 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | def testRunExistingDepUpdated(self): |
| 213 | self.SetupVartree(self._VARTREE) |
| 214 | app1 = "foo/app1-1.2.5-r2" |
| 215 | app2 = "foo/app2-4.5.8-r3" |
| 216 | self.SetupBintree( |
| 217 | [ |
| 218 | (app1, "0", "foo/app2 !foo/app3", "1413309350"), |
| 219 | (app2, "0", "", "1413309350"), |
| 220 | ] |
| 221 | ) |
| 222 | installs, listed, num_updates, _ = self.scanner.Run( |
| 223 | self.device, "/", ["app1"], True, True, True |
| 224 | ) |
| 225 | self.ValidatePkgs(installs, [app1, app2], constraints=[(app1, app2)]) |
| 226 | self.ValidatePkgs(listed, [app1]) |
| 227 | self.assertEqual(num_updates, 2) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 228 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 229 | def testRunMissingDepUpdated(self): |
| 230 | self.SetupVartree(self._VARTREE) |
| 231 | app1 = "foo/app1-1.2.5-r2" |
| 232 | app6 = "foo/app6-1.0.0-r1" |
| 233 | self.SetupBintree( |
| 234 | [ |
| 235 | (app1, "0", "foo/app2 !foo/app3 foo/app6", "1413309350"), |
| 236 | ("foo/app2-4.5.6-r7", "0", "", "1413309336"), |
| 237 | (app6, "0", "", "1413309350"), |
| 238 | ] |
| 239 | ) |
| 240 | installs, listed, num_updates, _ = self.scanner.Run( |
| 241 | self.device, "/", ["app1"], True, True, True |
| 242 | ) |
| 243 | self.ValidatePkgs(installs, [app1, app6], constraints=[(app1, app6)]) |
| 244 | self.ValidatePkgs(listed, [app1]) |
| 245 | self.assertEqual(num_updates, 1) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 246 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 247 | def testRunExistingRevDepUpdated(self): |
| 248 | self.SetupVartree(self._VARTREE) |
| 249 | app1 = "foo/app1-1.2.5-r2" |
| 250 | app4 = "foo/app4-2.0.1-r3" |
| 251 | self.SetupBintree( |
| 252 | [ |
| 253 | (app1, "0", "foo/app2 !foo/app3", "1413309350"), |
| 254 | (app4, "0", "foo/app1 foo/app5", "1413309350"), |
| 255 | ("foo/app5-3.0.7-r3", "0", "", "1413309336"), |
| 256 | ] |
| 257 | ) |
| 258 | installs, listed, num_updates, _ = self.scanner.Run( |
| 259 | self.device, "/", ["app1"], True, True, True |
| 260 | ) |
| 261 | self.ValidatePkgs(installs, [app1, app4], constraints=[(app4, app1)]) |
| 262 | self.ValidatePkgs(listed, [app1]) |
| 263 | self.assertEqual(num_updates, 2) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 264 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 265 | def testRunMissingRevDepNotUpdated(self): |
| 266 | self.SetupVartree(self._VARTREE) |
| 267 | app1 = "foo/app1-1.2.5-r2" |
| 268 | app6 = "foo/app6-1.0.0-r1" |
| 269 | self.SetupBintree( |
| 270 | [ |
| 271 | (app1, "0", "foo/app2 !foo/app3", "1413309350"), |
| 272 | (app6, "0", "foo/app1", "1413309350"), |
| 273 | ] |
| 274 | ) |
| 275 | installs, listed, num_updates, _ = self.scanner.Run( |
| 276 | self.device, "/", ["app1"], True, True, True |
| 277 | ) |
| 278 | self.ValidatePkgs(installs, [app1]) |
| 279 | self.ValidatePkgs(listed, [app1]) |
| 280 | self.assertEqual(num_updates, 1) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 281 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 282 | def testRunTransitiveDepsUpdated(self): |
| 283 | self.SetupVartree(self._VARTREE) |
| 284 | app1 = "foo/app1-1.2.5-r2" |
| 285 | app2 = "foo/app2-4.5.8-r3" |
| 286 | app4 = "foo/app4-2.0.0-r1" |
| 287 | app5 = "foo/app5-3.0.8-r2" |
| 288 | self.SetupBintree( |
| 289 | [ |
| 290 | (app1, "0", "foo/app2 !foo/app3", "1413309350"), |
| 291 | (app2, "0", "", "1413309350"), |
| 292 | (app4, "0", "foo/app1 foo/app5", "1413309350"), |
| 293 | (app5, "0", "", "1413309350"), |
| 294 | ] |
| 295 | ) |
| 296 | installs, listed, num_updates, _ = self.scanner.Run( |
| 297 | self.device, "/", ["app1"], True, True, True |
| 298 | ) |
| 299 | self.ValidatePkgs( |
| 300 | installs, |
| 301 | [app1, app2, app4, app5], |
| 302 | constraints=[(app1, app2), (app4, app1), (app4, app5)], |
| 303 | ) |
| 304 | self.ValidatePkgs(listed, [app1]) |
| 305 | self.assertEqual(num_updates, 4) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 306 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 307 | def testRunDisjunctiveDepsExistingUpdated(self): |
| 308 | self.SetupVartree(self._VARTREE) |
| 309 | app1 = "foo/app1-1.2.5-r2" |
| 310 | self.SetupBintree( |
| 311 | [ |
| 312 | (app1, "0", "|| ( foo/app6 foo/app2 ) !foo/app3", "1413309350"), |
| 313 | ("foo/app2-4.5.6-r7", "0", "", "1413309336"), |
| 314 | ] |
| 315 | ) |
| 316 | installs, listed, num_updates, _ = self.scanner.Run( |
| 317 | self.device, "/", ["app1"], True, True, True |
| 318 | ) |
| 319 | self.ValidatePkgs(installs, [app1]) |
| 320 | self.ValidatePkgs(listed, [app1]) |
| 321 | self.assertEqual(num_updates, 1) |
| 322 | |
| 323 | def testRunDisjunctiveDepsDefaultUpdated(self): |
| 324 | self.SetupVartree(self._VARTREE) |
| 325 | app1 = "foo/app1-1.2.5-r2" |
| 326 | app7 = "foo/app7-1.0.0-r1" |
| 327 | self.SetupBintree( |
| 328 | [ |
| 329 | (app1, "0", "|| ( foo/app6 foo/app7 ) !foo/app3", "1413309350"), |
| 330 | (app7, "0", "", "1413309350"), |
| 331 | ] |
| 332 | ) |
| 333 | installs, listed, num_updates, _ = self.scanner.Run( |
| 334 | self.device, "/", ["app1"], True, True, True |
| 335 | ) |
| 336 | self.ValidatePkgs(installs, [app1, app7], constraints=[(app1, app7)]) |
| 337 | self.ValidatePkgs(listed, [app1]) |
| 338 | self.assertEqual(num_updates, 1) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 339 | |
| 340 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 341 | class TestDeploy( |
| 342 | cros_test_lib.ProgressBarTestCase, cros_test_lib.MockTempDirTestCase |
| 343 | ): |
| 344 | """Test deploy.Deploy.""" |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 345 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 346 | @staticmethod |
| 347 | def FakeGetPackagesByCPV(cpvs, _strip, _sysroot): |
| 348 | return ["/path/to/%s.tbz2" % cpv.pv for cpv in cpvs] |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 349 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 350 | def setUp(self): |
| 351 | # Fake being root to avoid running filesystem commands with sudo_run. |
| 352 | self.PatchObject(osutils, "IsRootUser", return_value=True) |
| 353 | self._sysroot = os.path.join(self.tempdir, "sysroot") |
| 354 | osutils.SafeMakedirs(self._sysroot) |
| 355 | self.device = ChromiumOSDeviceHandlerFake() |
| 356 | self.PatchObject( |
| 357 | remote_access, "ChromiumOSDeviceHandler", return_value=self.device |
| 358 | ) |
| 359 | self.PatchObject(cros_build_lib, "GetBoard", return_value=None) |
| 360 | self.PatchObject( |
| 361 | build_target_lib, |
| 362 | "get_default_sysroot_path", |
| 363 | return_value=self._sysroot, |
| 364 | ) |
| 365 | self.package_scanner = self.PatchObject( |
| 366 | deploy, "_InstallPackageScanner" |
| 367 | ) |
| 368 | self.get_packages_paths = self.PatchObject( |
| 369 | deploy, "_GetPackagesByCPV", side_effect=self.FakeGetPackagesByCPV |
| 370 | ) |
| 371 | self.emerge = self.PatchObject(deploy, "_Emerge", return_value=None) |
| 372 | self.unmerge = self.PatchObject(deploy, "_Unmerge", return_value=None) |
| 373 | self.PatchObject(deploy, "_GetDLCInfo", return_value=(None, None)) |
| 374 | # Avoid running the portageq command. |
| 375 | sysroot_lib.Sysroot(self._sysroot).WriteConfig( |
| 376 | 'ARCH="amd64"\nPORTDIR_OVERLAY="%s"' % "/nothing/here" |
| 377 | ) |
| 378 | # make.conf needs to exist to correctly read back config. |
| 379 | unittest_lib.create_stub_make_conf(self._sysroot) |
Brian Norris | 2eee889 | 2021-04-06 16:23:23 -0700 | [diff] [blame] | 380 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 381 | def testDeployEmerge(self): |
| 382 | """Test that deploy._Emerge is called for each package.""" |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 383 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 384 | _BINPKG = "/path/to/bar-1.2.5.tbz2" |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 385 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 386 | def FakeIsFile(fname): |
| 387 | return fname == _BINPKG |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 388 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 389 | packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"] |
| 390 | cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"] |
| 391 | self.package_scanner.return_value = PackageScannerFake( |
| 392 | packages, |
| 393 | {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}}, |
| 394 | cpvs, |
| 395 | ) |
| 396 | self.PatchObject(os.path, "isfile", side_effect=FakeIsFile) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 397 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 398 | deploy.Deploy(None, ["package"], force=True, clean_binpkg=False) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 399 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 400 | # Check that package names were correctly resolved into binary packages. |
| 401 | self.get_packages_paths.assert_called_once_with( |
| 402 | [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot |
| 403 | ) |
| 404 | # Check that deploy._Emerge is called the right number of times. |
| 405 | self.emerge.assert_called_once_with( |
| 406 | mock.ANY, |
| 407 | [ |
| 408 | "/path/to/foo-1.2.3.tbz2", |
| 409 | "/path/to/bar-1.2.5.tbz2", |
| 410 | "/path/to/foobar-2.0.tbz2", |
| 411 | ], |
| 412 | "/", |
| 413 | extra_args=None, |
| 414 | ) |
| 415 | self.assertEqual(self.unmerge.call_count, 0) |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 416 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 417 | def testDeployEmergeDLC(self): |
| 418 | """Test that deploy._Emerge installs images for DLC packages.""" |
| 419 | packages = ["some/foodlc-1.0", "some/bardlc-2.0"] |
| 420 | cpvs = ["some/foodlc-1.0", "some/bardlc-2.0"] |
| 421 | self.package_scanner.return_value = PackageScannerFake( |
| 422 | packages, {"some/foodlc-1.0": {}, "some/bardlc-2.0": {}}, cpvs |
| 423 | ) |
| 424 | self.PatchObject( |
| 425 | deploy, "_GetDLCInfo", return_value=("foo_id", "foo_package") |
| 426 | ) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 427 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 428 | deploy.Deploy(None, ["package"], force=True, clean_binpkg=False) |
| 429 | # Check that dlcservice is restarted (DLC modules are deployed). |
| 430 | self.assertTrue(["restart", "dlcservice"] in self.device.device.cmds) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 431 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 432 | def testDeployEmergeSELinux(self): |
| 433 | """Test deploy progress when the device has SELinux""" |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 434 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 435 | _BINPKG = "/path/to/bar-1.2.5.tbz2" |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 436 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 437 | def FakeIsFile(fname): |
| 438 | return fname == _BINPKG |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 439 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 440 | def GetRestoreconCommand(pkgfile): |
| 441 | remote_path = os.path.join("/testdir/packages/to/", pkgfile) |
| 442 | return [ |
| 443 | [ |
| 444 | "cd", |
| 445 | "/", |
| 446 | "&&", |
| 447 | "tar", |
| 448 | "tf", |
| 449 | remote_path, |
| 450 | "|", |
| 451 | "restorecon", |
| 452 | "-i", |
| 453 | "-f", |
| 454 | "-", |
| 455 | ] |
| 456 | ] |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 457 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 458 | self.device.device.selinux_available = True |
| 459 | packages = ["some/foo-1.2.3", _BINPKG, "some/foobar-2.0"] |
| 460 | cpvs = ["some/foo-1.2.3", "to/bar-1.2.5", "some/foobar-2.0"] |
| 461 | self.package_scanner.return_value = PackageScannerFake( |
| 462 | packages, |
| 463 | {"some/foo-1.2.3": {}, _BINPKG: {}, "some/foobar-2.0": {}}, |
| 464 | cpvs, |
| 465 | ) |
| 466 | self.PatchObject(os.path, "isfile", side_effect=FakeIsFile) |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 467 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 468 | deploy.Deploy(None, ["package"], force=True, clean_binpkg=False) |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 469 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 470 | # Check that package names were correctly resolved into binary packages. |
| 471 | self.get_packages_paths.assert_called_once_with( |
| 472 | [package_info.SplitCPV(p) for p in cpvs], True, self._sysroot |
| 473 | ) |
| 474 | # Check that deploy._Emerge is called the right number of times. |
| 475 | self.assertEqual(self.emerge.call_count, 1) |
| 476 | self.assertEqual(self.unmerge.call_count, 0) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 477 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 478 | self.assertEqual( |
| 479 | self.device.device.cmds, |
| 480 | [["setenforce", "0"]] |
| 481 | + GetRestoreconCommand("foo-1.2.3.tbz2") |
| 482 | + GetRestoreconCommand("bar-1.2.5.tbz2") |
| 483 | + GetRestoreconCommand("foobar-2.0.tbz2") |
| 484 | + [["setenforce", "1"]], |
| 485 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 486 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 487 | def testDeployUnmerge(self): |
| 488 | """Test that deploy._Unmerge is called for each package.""" |
| 489 | packages = ["foo", "bar", "foobar", "foodlc"] |
| 490 | self.package_scanner.return_value = PackageScannerFake( |
| 491 | packages, |
| 492 | { |
| 493 | "foo": {}, |
| 494 | "bar": {}, |
| 495 | "foobar": {}, |
| 496 | "foodlc": { |
| 497 | deploy._DLC_ID: "foodlc", |
| 498 | deploy._DLC_PACKAGE: "foopackage", |
| 499 | }, |
| 500 | }, |
| 501 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 502 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 503 | deploy.Deploy( |
| 504 | None, ["package"], force=True, clean_binpkg=False, emerge=False |
| 505 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 506 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 507 | # Check that deploy._Unmerge is called the right number of times. |
| 508 | self.assertEqual(self.emerge.call_count, 0) |
| 509 | self.unmerge.assert_called_once_with(mock.ANY, packages, "/") |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 510 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 511 | self.assertEqual( |
| 512 | self.device.device.cmds, |
| 513 | [ |
| 514 | ["dlcservice_util", "--uninstall", "--id=foodlc"], |
| 515 | ["restart", "dlcservice"], |
| 516 | ], |
| 517 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 518 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 519 | def testDeployMergeWithProgressBar(self): |
| 520 | """Test that BrilloDeployOperation.Run() is called for merge.""" |
| 521 | packages = ["foo", "bar", "foobar"] |
| 522 | self.package_scanner.return_value = PackageScannerFake( |
| 523 | packages, {"foo": {}, "bar": {}, "foobar": {}} |
| 524 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 525 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 526 | run = self.PatchObject( |
| 527 | deploy.BrilloDeployOperation, "Run", return_value=None |
| 528 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 529 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 530 | self.PatchObject(command, "UseProgressBar", return_value=True) |
| 531 | deploy.Deploy(None, ["package"], force=True, clean_binpkg=False) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 532 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 533 | # Check that BrilloDeployOperation.Run was called. |
| 534 | self.assertTrue(run.called) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 535 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 536 | def testDeployUnmergeWithProgressBar(self): |
| 537 | """Test that BrilloDeployOperation.Run() is called for unmerge.""" |
| 538 | packages = ["foo", "bar", "foobar"] |
| 539 | self.package_scanner.return_value = PackageScannerFake( |
| 540 | packages, {"foo": {}, "bar": {}, "foobar": {}} |
| 541 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 542 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 543 | run = self.PatchObject( |
| 544 | deploy.BrilloDeployOperation, "Run", return_value=None |
| 545 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 546 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 547 | self.PatchObject(command, "UseProgressBar", return_value=True) |
| 548 | deploy.Deploy( |
| 549 | None, ["package"], force=True, clean_binpkg=False, emerge=False |
| 550 | ) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 551 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 552 | # Check that BrilloDeployOperation.Run was called. |
| 553 | self.assertTrue(run.called) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 554 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 555 | def testBrilloDeployMergeOperation(self): |
| 556 | """Test that BrilloDeployOperation works for merge.""" |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 557 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 558 | def func(queue): |
| 559 | for event in op.MERGE_EVENTS: |
| 560 | queue.get() |
| 561 | print(event) |
| 562 | sys.stdout.flush() |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 563 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 564 | queue = multiprocessing.Queue() |
| 565 | # Emerge one package. |
| 566 | op = BrilloDeployOperationFake(True, queue) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 567 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 568 | with self.OutputCapturer(): |
| 569 | op.Run(func, queue) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 570 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 571 | # Check that the progress bar prints correctly. |
| 572 | self.AssertProgressBarAllEvents(len(op.MERGE_EVENTS)) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 573 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 574 | def testBrilloDeployUnmergeOperation(self): |
| 575 | """Test that BrilloDeployOperation works for unmerge.""" |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 576 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 577 | def func(queue): |
| 578 | for event in op.UNMERGE_EVENTS: |
| 579 | queue.get() |
| 580 | print(event) |
| 581 | sys.stdout.flush() |
| 582 | |
| 583 | queue = multiprocessing.Queue() |
| 584 | # Unmerge one package. |
| 585 | op = BrilloDeployOperationFake(False, queue) |
| 586 | |
| 587 | with self.OutputCapturer(): |
| 588 | op.Run(func, queue) |
| 589 | |
| 590 | # Check that the progress bar prints correctly. |
| 591 | self.AssertProgressBarAllEvents(len(op.UNMERGE_EVENTS)) |