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