Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 2 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Unit tests for the deploy module.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import json |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 11 | import multiprocessing |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 12 | import os |
Mike Frysinger | 050fa5a | 2019-12-01 16:16:29 -0500 | [diff] [blame] | 13 | import sys |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 14 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 15 | from chromite.cli import command |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 16 | from chromite.cli import deploy |
| 17 | from chromite.lib import cros_build_lib |
| 18 | from chromite.lib import cros_test_lib |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 19 | from chromite.lib import remote_access |
Alex Klein | 18a60af | 2020-06-11 12:08:47 -0600 | [diff] [blame] | 20 | from chromite.lib.parser import package_info |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 21 | |
Chris McDonald | 186b9ee | 2020-04-16 05:56:49 -0600 | [diff] [blame] | 22 | pytestmark = [cros_test_lib.pytestmark_inside_only] |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 23 | |
| 24 | |
Mike Frysinger | 3f087aa | 2020-03-20 06:03:16 -0400 | [diff] [blame] | 25 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 26 | |
| 27 | |
Greg Edelston | a4c9b3b | 2020-01-07 17:51:13 -0700 | [diff] [blame] | 28 | if cros_build_lib.IsInsideChroot(): |
| 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): |
| 36 | """Fake for device.""" |
| 37 | |
| 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 |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 44 | self.cmds = [] |
| 45 | self.work_dir = '/testdir/' |
Ben Pastene | 5f03b05 | 2019-08-12 18:03:24 -0700 | [diff] [blame] | 46 | self.selinux_available = False |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 47 | |
Achuith Bhandarkar | 0487c31 | 2019-04-22 12:19:25 -0700 | [diff] [blame] | 48 | def MountRootfsReadWrite(self): |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 49 | return True |
| 50 | |
Ben Pastene | 5f03b05 | 2019-08-12 18:03:24 -0700 | [diff] [blame] | 51 | def IsSELinuxAvailable(self): |
| 52 | return self.selinux_available |
| 53 | |
| 54 | def IsSELinuxEnforced(self): |
| 55 | return True |
| 56 | |
Andrew | c7e1c6b | 2020-02-27 16:03:53 -0800 | [diff] [blame] | 57 | def run(self, cmd, **_kwargs): |
| 58 | self.cmds.append(cmd) |
| 59 | |
| 60 | def CopyToDevice(self, _src, _dest, _mode='rsync', **_kwargs): |
| 61 | return True |
| 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): |
| 65 | """Fake for chromite.lib.remote_access.ChomiumOSDeviceHandler.""" |
| 66 | |
| 67 | class RemoteAccessFake(object): |
| 68 | """Fake for chromite.lib.remote_access.RemoteAccess.""" |
| 69 | |
| 70 | def __init__(self): |
| 71 | self.remote_sh_output = None |
| 72 | |
| 73 | def RemoteSh(self, *_args, **_kwargs): |
| 74 | return cros_build_lib.CommandResult(output=self.remote_sh_output) |
| 75 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 76 | def __init__(self, *_args, **_kwargs): |
David Pursell | 67a8276 | 2015-04-30 17:26:59 -0700 | [diff] [blame] | 77 | self._agent = self.RemoteAccessFake() |
Mike Frysinger | 539db51 | 2015-05-21 18:14:01 -0400 | [diff] [blame] | 78 | self.device = ChromiumOSDeviceFake() |
David Pursell | 67a8276 | 2015-04-30 17:26:59 -0700 | [diff] [blame] | 79 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 80 | # TODO(dpursell): Mock remote access object in cros_test_lib (brbug.com/986). |
David Pursell | 67a8276 | 2015-04-30 17:26:59 -0700 | [diff] [blame] | 81 | def GetAgent(self): |
| 82 | return self._agent |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 83 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 84 | def __exit__(self, _type, _value, _traceback): |
| 85 | pass |
| 86 | |
| 87 | def __enter__(self): |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 88 | return self.device |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 89 | |
| 90 | |
| 91 | class BrilloDeployOperationFake(deploy.BrilloDeployOperation): |
| 92 | """Fake for deploy.BrilloDeployOperation.""" |
| 93 | def __init__(self, pkg_count, emerge, queue): |
| 94 | super(BrilloDeployOperationFake, self).__init__(pkg_count, emerge) |
| 95 | self._queue = queue |
| 96 | |
Ralph Nathan | dc14ed9 | 2015-04-22 11:17:40 -0700 | [diff] [blame] | 97 | def ParseOutput(self, output=None): |
| 98 | super(BrilloDeployOperationFake, self).ParseOutput(output) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 99 | self._queue.put('advance') |
| 100 | |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 101 | |
| 102 | class DbApiFake(object): |
| 103 | """Fake for Portage dbapi.""" |
| 104 | |
| 105 | def __init__(self, pkgs): |
| 106 | self.pkg_db = {} |
| 107 | for cpv, slot, rdeps_raw, build_time in pkgs: |
| 108 | self.pkg_db[cpv] = { |
| 109 | 'SLOT': slot, 'RDEPEND': rdeps_raw, 'BUILD_TIME': build_time} |
| 110 | |
| 111 | def cpv_all(self): |
Mike Frysinger | 1f4478c | 2019-10-20 18:33:17 -0400 | [diff] [blame] | 112 | return list(self.pkg_db) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 113 | |
| 114 | def aux_get(self, cpv, keys): |
| 115 | pkg_info = self.pkg_db[cpv] |
| 116 | return [pkg_info[key] for key in keys] |
| 117 | |
| 118 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 119 | class PackageScannerFake(object): |
| 120 | """Fake for PackageScanner.""" |
| 121 | |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 122 | def __init__(self, packages, pkgs_attrs, packages_cpvs=None): |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 123 | self.pkgs = packages |
Ned Nguyen | d0db407 | 2019-02-22 14:19:21 -0700 | [diff] [blame] | 124 | self.cpvs = packages_cpvs or packages |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 125 | self.listed = [] |
Mike Frysinger | cce4f83 | 2019-11-14 22:44:38 -0500 | [diff] [blame] | 126 | self.num_updates = 0 |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 127 | self.pkgs_attrs = pkgs_attrs |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 128 | |
| 129 | def Run(self, _device, _root, _packages, _update, _deep, _deep_rev): |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 130 | return self.cpvs, self.listed, self.num_updates, self.pkgs_attrs |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 131 | |
| 132 | |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 133 | class PortageTreeFake(object): |
| 134 | """Fake for Portage tree.""" |
| 135 | |
| 136 | def __init__(self, dbapi): |
| 137 | self.dbapi = dbapi |
| 138 | |
| 139 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 140 | class TestInstallPackageScanner(cros_test_lib.MockOutputTestCase): |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 141 | """Test the update package scanner.""" |
| 142 | _BOARD = 'foo_board' |
| 143 | _BUILD_ROOT = '/build/%s' % _BOARD |
| 144 | _VARTREE = [ |
| 145 | ('foo/app1-1.2.3-r4', '0', 'foo/app2 !foo/app3', '1413309336'), |
| 146 | ('foo/app2-4.5.6-r7', '0', '', '1413309336'), |
| 147 | ('foo/app4-2.0.0-r1', '0', 'foo/app1 foo/app5', '1413309336'), |
| 148 | ('foo/app5-3.0.7-r3', '0', '', '1413309336'), |
| 149 | ] |
| 150 | |
| 151 | def setUp(self): |
| 152 | """Patch imported modules.""" |
| 153 | self.PatchObject(cros_build_lib, 'GetChoice', return_value=0) |
| 154 | self.device = ChromiumOSDeviceHandlerFake() |
| 155 | self.scanner = deploy._InstallPackageScanner(self._BUILD_ROOT) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 156 | self.PatchObject(deploy, '_GetDLCInfo', return_value=(None, None)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 157 | |
| 158 | def SetupVartree(self, vartree_pkgs): |
David Pursell | 67a8276 | 2015-04-30 17:26:59 -0700 | [diff] [blame] | 159 | self.device.GetAgent().remote_sh_output = json.dumps(vartree_pkgs) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 160 | |
| 161 | def SetupBintree(self, bintree_pkgs): |
| 162 | bintree = PortageTreeFake(DbApiFake(bintree_pkgs)) |
| 163 | build_root = os.path.join(self._BUILD_ROOT, '') |
| 164 | portage_db = {build_root: {'bintree': bintree}} |
| 165 | self.PatchObject(portage, 'create_trees', return_value=portage_db) |
| 166 | |
| 167 | def ValidatePkgs(self, actual, expected, constraints=None): |
| 168 | # Containing exactly the same packages. |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 169 | self.assertEqual(sorted(expected), sorted(actual)) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 170 | # Packages appear in the right order. |
| 171 | if constraints is not None: |
| 172 | for needs, needed in constraints: |
| 173 | self.assertGreater(actual.index(needs), actual.index(needed)) |
| 174 | |
| 175 | def testRunUpdatedVersion(self): |
| 176 | self.SetupVartree(self._VARTREE) |
| 177 | app1 = 'foo/app1-1.2.5-r4' |
| 178 | self.SetupBintree([ |
| 179 | (app1, '0', 'foo/app2 !foo/app3', '1413309336'), |
| 180 | ('foo/app2-4.5.6-r7', '0', '', '1413309336'), |
| 181 | ]) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 182 | installs, listed, num_updates, _ = self.scanner.Run( |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 183 | self.device, '/', ['app1'], True, True, True) |
| 184 | self.ValidatePkgs(installs, [app1]) |
| 185 | self.ValidatePkgs(listed, [app1]) |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 186 | self.assertEqual(num_updates, 1) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 187 | |
| 188 | def testRunUpdatedBuildTime(self): |
| 189 | self.SetupVartree(self._VARTREE) |
| 190 | app1 = 'foo/app1-1.2.3-r4' |
| 191 | self.SetupBintree([ |
| 192 | (app1, '0', 'foo/app2 !foo/app3', '1413309350'), |
| 193 | ('foo/app2-4.5.6-r7', '0', '', '1413309336'), |
| 194 | ]) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 195 | installs, listed, num_updates, _ = self.scanner.Run( |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 196 | self.device, '/', ['app1'], True, True, True) |
| 197 | self.ValidatePkgs(installs, [app1]) |
| 198 | self.ValidatePkgs(listed, [app1]) |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 199 | self.assertEqual(num_updates, 1) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 200 | |
| 201 | def testRunExistingDepUpdated(self): |
| 202 | self.SetupVartree(self._VARTREE) |
| 203 | app1 = 'foo/app1-1.2.5-r2' |
| 204 | app2 = 'foo/app2-4.5.8-r3' |
| 205 | self.SetupBintree([ |
| 206 | (app1, '0', 'foo/app2 !foo/app3', '1413309350'), |
| 207 | (app2, '0', '', '1413309350'), |
| 208 | ]) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 209 | installs, listed, num_updates, _ = self.scanner.Run( |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 210 | self.device, '/', ['app1'], True, True, True) |
| 211 | self.ValidatePkgs(installs, [app1, app2], constraints=[(app1, app2)]) |
| 212 | self.ValidatePkgs(listed, [app1]) |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 213 | self.assertEqual(num_updates, 2) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 214 | |
| 215 | def testRunMissingDepUpdated(self): |
| 216 | self.SetupVartree(self._VARTREE) |
| 217 | app1 = 'foo/app1-1.2.5-r2' |
| 218 | app6 = 'foo/app6-1.0.0-r1' |
| 219 | self.SetupBintree([ |
| 220 | (app1, '0', 'foo/app2 !foo/app3 foo/app6', '1413309350'), |
| 221 | ('foo/app2-4.5.6-r7', '0', '', '1413309336'), |
| 222 | (app6, '0', '', '1413309350'), |
| 223 | ]) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 224 | installs, listed, num_updates, _ = self.scanner.Run( |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 225 | self.device, '/', ['app1'], True, True, True) |
| 226 | self.ValidatePkgs(installs, [app1, app6], constraints=[(app1, app6)]) |
| 227 | self.ValidatePkgs(listed, [app1]) |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 228 | self.assertEqual(num_updates, 1) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 229 | |
| 230 | def testRunExistingRevDepUpdated(self): |
| 231 | self.SetupVartree(self._VARTREE) |
| 232 | app1 = 'foo/app1-1.2.5-r2' |
| 233 | app4 = 'foo/app4-2.0.1-r3' |
| 234 | self.SetupBintree([ |
| 235 | (app1, '0', 'foo/app2 !foo/app3', '1413309350'), |
| 236 | (app4, '0', 'foo/app1 foo/app5', '1413309350'), |
| 237 | ('foo/app5-3.0.7-r3', '0', '', '1413309336'), |
| 238 | ]) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 239 | installs, listed, num_updates, _ = self.scanner.Run( |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 240 | self.device, '/', ['app1'], True, True, True) |
| 241 | self.ValidatePkgs(installs, [app1, app4], constraints=[(app4, app1)]) |
| 242 | self.ValidatePkgs(listed, [app1]) |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 243 | self.assertEqual(num_updates, 2) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 244 | |
| 245 | def testRunMissingRevDepNotUpdated(self): |
| 246 | self.SetupVartree(self._VARTREE) |
| 247 | app1 = 'foo/app1-1.2.5-r2' |
| 248 | app6 = 'foo/app6-1.0.0-r1' |
| 249 | self.SetupBintree([ |
| 250 | (app1, '0', 'foo/app2 !foo/app3', '1413309350'), |
| 251 | (app6, '0', 'foo/app1', '1413309350'), |
| 252 | ]) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 253 | installs, listed, num_updates, _ = self.scanner.Run( |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 254 | self.device, '/', ['app1'], True, True, True) |
| 255 | self.ValidatePkgs(installs, [app1]) |
| 256 | self.ValidatePkgs(listed, [app1]) |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 257 | self.assertEqual(num_updates, 1) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 258 | |
| 259 | def testRunTransitiveDepsUpdated(self): |
| 260 | self.SetupVartree(self._VARTREE) |
| 261 | app1 = 'foo/app1-1.2.5-r2' |
| 262 | app2 = 'foo/app2-4.5.8-r3' |
| 263 | app4 = 'foo/app4-2.0.0-r1' |
| 264 | app5 = 'foo/app5-3.0.8-r2' |
| 265 | self.SetupBintree([ |
| 266 | (app1, '0', 'foo/app2 !foo/app3', '1413309350'), |
| 267 | (app2, '0', '', '1413309350'), |
| 268 | (app4, '0', 'foo/app1 foo/app5', '1413309350'), |
| 269 | (app5, '0', '', '1413309350'), |
| 270 | ]) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 271 | installs, listed, num_updates, _ = self.scanner.Run( |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 272 | self.device, '/', ['app1'], True, True, True) |
| 273 | self.ValidatePkgs(installs, [app1, app2, app4, app5], |
| 274 | constraints=[(app1, app2), (app4, app1), (app4, app5)]) |
| 275 | self.ValidatePkgs(listed, [app1]) |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 276 | self.assertEqual(num_updates, 4) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 277 | |
| 278 | def testRunDisjunctiveDepsExistingUpdated(self): |
| 279 | self.SetupVartree(self._VARTREE) |
| 280 | app1 = 'foo/app1-1.2.5-r2' |
| 281 | self.SetupBintree([ |
| 282 | (app1, '0', '|| ( foo/app6 foo/app2 ) !foo/app3', '1413309350'), |
| 283 | ('foo/app2-4.5.6-r7', '0', '', '1413309336'), |
| 284 | ]) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 285 | installs, listed, num_updates, _ = self.scanner.Run( |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 286 | self.device, '/', ['app1'], True, True, True) |
| 287 | self.ValidatePkgs(installs, [app1]) |
| 288 | self.ValidatePkgs(listed, [app1]) |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 289 | self.assertEqual(num_updates, 1) |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 290 | |
| 291 | def testRunDisjunctiveDepsDefaultUpdated(self): |
| 292 | self.SetupVartree(self._VARTREE) |
| 293 | app1 = 'foo/app1-1.2.5-r2' |
| 294 | app7 = 'foo/app7-1.0.0-r1' |
| 295 | self.SetupBintree([ |
| 296 | (app1, '0', '|| ( foo/app6 foo/app7 ) !foo/app3', '1413309350'), |
| 297 | (app7, '0', '', '1413309350'), |
| 298 | ]) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 299 | installs, listed, num_updates, _ = self.scanner.Run( |
David Pursell | 9476bf4 | 2015-03-30 13:34:27 -0700 | [diff] [blame] | 300 | self.device, '/', ['app1'], True, True, True) |
| 301 | self.ValidatePkgs(installs, [app1, app7], constraints=[(app1, app7)]) |
| 302 | self.ValidatePkgs(listed, [app1]) |
Mike Frysinger | 2d589a1 | 2019-08-25 14:15:12 -0400 | [diff] [blame] | 303 | self.assertEqual(num_updates, 1) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 304 | |
| 305 | |
| 306 | class TestDeploy(cros_test_lib.ProgressBarTestCase): |
| 307 | """Test deploy.Deploy.""" |
| 308 | |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 309 | @staticmethod |
| 310 | def FakeGetPackagesByCPV(cpvs, _strip, _sysroot): |
| 311 | return ['/path/to/%s.tbz2' % cpv.pv for cpv in cpvs] |
| 312 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 313 | def setUp(self): |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 314 | self.device = ChromiumOSDeviceHandlerFake() |
| 315 | self.PatchObject( |
| 316 | remote_access, 'ChromiumOSDeviceHandler', return_value=self.device) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 317 | self.PatchObject(cros_build_lib, 'GetBoard', return_value=None) |
| 318 | self.PatchObject(cros_build_lib, 'GetSysroot', return_value='sysroot') |
| 319 | self.package_scanner = self.PatchObject(deploy, '_InstallPackageScanner') |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 320 | self.get_packages_paths = self.PatchObject( |
| 321 | deploy, '_GetPackagesByCPV', side_effect=self.FakeGetPackagesByCPV) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 322 | self.emerge = self.PatchObject(deploy, '_Emerge', return_value=None) |
| 323 | self.unmerge = self.PatchObject(deploy, '_Unmerge', return_value=None) |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 324 | self.PatchObject(deploy, '_GetDLCInfo', return_value=(None, None)) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 325 | |
| 326 | def testDeployEmerge(self): |
| 327 | """Test that deploy._Emerge is called for each package.""" |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 328 | |
| 329 | _BINPKG = '/path/to/bar-1.2.5.tbz2' |
| 330 | def FakeIsFile(fname): |
| 331 | return fname == _BINPKG |
| 332 | |
| 333 | packages = ['some/foo-1.2.3', _BINPKG, 'some/foobar-2.0'] |
Ned Nguyen | d0db407 | 2019-02-22 14:19:21 -0700 | [diff] [blame] | 334 | cpvs = ['some/foo-1.2.3', 'to/bar-1.2.5', 'some/foobar-2.0'] |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 335 | self.package_scanner.return_value = PackageScannerFake( |
| 336 | packages, |
| 337 | {'some/foo-1.2.3': {}, _BINPKG: {}, 'some/foobar-2.0': {}}, |
| 338 | cpvs) |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 339 | self.PatchObject(os.path, 'isfile', side_effect=FakeIsFile) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 340 | |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 341 | deploy.Deploy(None, ['package'], force=True, clean_binpkg=False) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 342 | |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 343 | # Check that package names were correctly resolved into binary packages. |
| 344 | self.get_packages_paths.assert_called_once_with( |
Alex Klein | 9742cb6 | 2020-10-12 19:22:10 +0000 | [diff] [blame] | 345 | [package_info.SplitCPV(p) for p in cpvs], True, 'sysroot') |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 346 | # Check that deploy._Emerge is called the right number of times. |
| 347 | self.assertEqual(self.emerge.call_count, len(packages)) |
| 348 | self.assertEqual(self.unmerge.call_count, 0) |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 349 | |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 350 | def testDeployEmergeDLC(self): |
| 351 | """Test that deploy._Emerge installs images for DLC packages.""" |
| 352 | packages = ['some/foodlc-1.0', 'some/bardlc-2.0'] |
| 353 | cpvs = ['some/foodlc-1.0', 'some/bardlc-2.0'] |
| 354 | self.package_scanner.return_value = PackageScannerFake( |
| 355 | packages, {'some/foodlc-1.0': {}, 'some/bardlc-2.0': {}}, cpvs) |
| 356 | self.PatchObject(deploy, '_GetDLCInfo', |
| 357 | return_value=('foo_id', 'foo_package')) |
| 358 | |
| 359 | deploy.Deploy(None, ['package'], force=True, clean_binpkg=False) |
| 360 | # Check that dlcservice is restarted (DLC modules are deployed). |
| 361 | self.assertTrue(['restart', 'dlcservice'] in self.device.device.cmds) |
| 362 | |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 363 | def testDeployEmergeSELinux(self): |
| 364 | """Test deploy progress when the device has SELinux""" |
| 365 | |
| 366 | _BINPKG = '/path/to/bar-1.2.5.tbz2' |
| 367 | def FakeIsFile(fname): |
| 368 | return fname == _BINPKG |
| 369 | |
| 370 | def GetRestoreconCommand(pkgfile): |
| 371 | remote_path = os.path.join('/testdir/packages/to/', pkgfile) |
| 372 | return [['setenforce', '0'], |
| 373 | ['cd', '/', '&&', |
| 374 | 'tar', 'tf', remote_path, '|', |
| 375 | 'restorecon', '-i', '-f', '-'], |
| 376 | ['setenforce', '1']] |
| 377 | |
Ben Pastene | 5f03b05 | 2019-08-12 18:03:24 -0700 | [diff] [blame] | 378 | self.device.device.selinux_available = True |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 379 | packages = ['some/foo-1.2.3', _BINPKG, 'some/foobar-2.0'] |
| 380 | cpvs = ['some/foo-1.2.3', 'to/bar-1.2.5', 'some/foobar-2.0'] |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 381 | self.package_scanner.return_value = PackageScannerFake( |
| 382 | packages, |
| 383 | {'some/foo-1.2.3': {}, _BINPKG: {}, 'some/foobar-2.0': {}}, |
| 384 | cpvs) |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 385 | self.PatchObject(os.path, 'isfile', side_effect=FakeIsFile) |
| 386 | |
| 387 | deploy.Deploy(None, ['package'], force=True, clean_binpkg=False) |
| 388 | |
| 389 | # Check that package names were correctly resolved into binary packages. |
| 390 | self.get_packages_paths.assert_called_once_with( |
Alex Klein | 9742cb6 | 2020-10-12 19:22:10 +0000 | [diff] [blame] | 391 | [package_info.SplitCPV(p) for p in cpvs], True, 'sysroot') |
Qijiang Fan | 8a94503 | 2019-04-25 20:53:29 +0900 | [diff] [blame] | 392 | # Check that deploy._Emerge is called the right number of times. |
| 393 | self.assertEqual(self.emerge.call_count, len(packages)) |
| 394 | self.assertEqual(self.unmerge.call_count, 0) |
| 395 | |
| 396 | self.assertEqual(self.device.device.cmds, |
| 397 | GetRestoreconCommand('foo-1.2.3.tbz2') + |
| 398 | GetRestoreconCommand('bar-1.2.5.tbz2') + |
| 399 | GetRestoreconCommand('foobar-2.0.tbz2')) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 400 | |
| 401 | def testDeployUnmerge(self): |
| 402 | """Test that deploy._Unmerge is called for each package.""" |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 403 | packages = ['foo', 'bar', 'foobar', 'foodlc'] |
| 404 | self.package_scanner.return_value = PackageScannerFake( |
| 405 | packages, {'foo': {}, 'bar': {}, 'foobar': {}, |
| 406 | 'foodlc': {deploy._DLC_ID: 'foodlc', |
| 407 | deploy._DLC_PACKAGE: 'foopackage'}}) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 408 | |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 409 | deploy.Deploy(None, ['package'], force=True, clean_binpkg=False, |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 410 | emerge=False) |
| 411 | |
| 412 | # Check that deploy._Unmerge is called the right number of times. |
| 413 | self.assertEqual(self.emerge.call_count, 0) |
| 414 | self.assertEqual(self.unmerge.call_count, len(packages)) |
| 415 | |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 416 | self.assertEqual( |
| 417 | self.device.device.cmds, |
Jae Hoon Kim | 964ed7e | 2020-05-15 13:59:23 -0700 | [diff] [blame] | 418 | [['dlcservice_util', '--uninstall', '--id=foodlc'], |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 419 | ['restart', 'dlcservice']]) |
| 420 | |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 421 | def testDeployMergeWithProgressBar(self): |
| 422 | """Test that BrilloDeployOperation.Run() is called for merge.""" |
| 423 | packages = ['foo', 'bar', 'foobar'] |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 424 | self.package_scanner.return_value = PackageScannerFake( |
| 425 | packages, {'foo': {}, 'bar': {}, 'foobar': {}}) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 426 | |
| 427 | run = self.PatchObject(deploy.BrilloDeployOperation, 'Run', |
| 428 | return_value=None) |
| 429 | |
| 430 | self.PatchObject(command, 'UseProgressBar', return_value=True) |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 431 | deploy.Deploy(None, ['package'], force=True, clean_binpkg=False) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 432 | |
| 433 | # Check that BrilloDeployOperation.Run was called. |
| 434 | self.assertTrue(run.called) |
| 435 | |
| 436 | def testDeployUnmergeWithProgressBar(self): |
| 437 | """Test that BrilloDeployOperation.Run() is called for unmerge.""" |
| 438 | packages = ['foo', 'bar', 'foobar'] |
Xiaochu Liu | 2726e7c | 2019-07-18 10:28:10 -0700 | [diff] [blame] | 439 | self.package_scanner.return_value = PackageScannerFake( |
| 440 | packages, {'foo': {}, 'bar': {}, 'foobar': {}}) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 441 | |
| 442 | run = self.PatchObject(deploy.BrilloDeployOperation, 'Run', |
| 443 | return_value=None) |
| 444 | |
| 445 | self.PatchObject(command, 'UseProgressBar', return_value=True) |
Gilad Arnold | 0e1b1da | 2015-06-10 06:41:05 -0700 | [diff] [blame] | 446 | deploy.Deploy(None, ['package'], force=True, clean_binpkg=False, |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 447 | emerge=False) |
| 448 | |
| 449 | # Check that BrilloDeployOperation.Run was called. |
| 450 | self.assertTrue(run.called) |
| 451 | |
| 452 | def testBrilloDeployMergeOperation(self): |
| 453 | """Test that BrilloDeployOperation works for merge.""" |
| 454 | def func(queue): |
Ralph Nathan | 90475a1 | 2015-05-20 13:19:01 -0700 | [diff] [blame] | 455 | for event in op.MERGE_EVENTS: |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 456 | queue.get() |
| 457 | print(event) |
Mike Frysinger | 050fa5a | 2019-12-01 16:16:29 -0500 | [diff] [blame] | 458 | sys.stdout.flush() |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 459 | |
| 460 | queue = multiprocessing.Queue() |
| 461 | # Emerge one package. |
| 462 | op = BrilloDeployOperationFake(1, True, queue) |
| 463 | |
| 464 | with self.OutputCapturer(): |
| 465 | op.Run(func, queue) |
| 466 | |
| 467 | # Check that the progress bar prints correctly. |
Ralph Nathan | 90475a1 | 2015-05-20 13:19:01 -0700 | [diff] [blame] | 468 | self.AssertProgressBarAllEvents(len(op.MERGE_EVENTS)) |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 469 | |
| 470 | def testBrilloDeployUnmergeOperation(self): |
| 471 | """Test that BrilloDeployOperation works for unmerge.""" |
| 472 | def func(queue): |
Ralph Nathan | 90475a1 | 2015-05-20 13:19:01 -0700 | [diff] [blame] | 473 | for event in op.UNMERGE_EVENTS: |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 474 | queue.get() |
| 475 | print(event) |
Mike Frysinger | 050fa5a | 2019-12-01 16:16:29 -0500 | [diff] [blame] | 476 | sys.stdout.flush() |
Ralph Nathan | e01ccf1 | 2015-04-16 10:40:32 -0700 | [diff] [blame] | 477 | |
| 478 | queue = multiprocessing.Queue() |
| 479 | # Unmerge one package. |
| 480 | op = BrilloDeployOperationFake(1, False, queue) |
| 481 | |
| 482 | with self.OutputCapturer(): |
| 483 | op.Run(func, queue) |
| 484 | |
| 485 | # Check that the progress bar prints correctly. |
Ralph Nathan | 90475a1 | 2015-05-20 13:19:01 -0700 | [diff] [blame] | 486 | self.AssertProgressBarAllEvents(len(op.UNMERGE_EVENTS)) |