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