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