blob: cdd8afdf99fe63bf2a4c5a680bb4bd9785194ba0 [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
David Pursell9476bf42015-03-30 13:34:27 -07002# 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
8from __future__ import print_function
9
10import json
Ralph Nathane01ccf12015-04-16 10:40:32 -070011import multiprocessing
David Pursell9476bf42015-03-30 13:34:27 -070012import os
13
Ralph Nathane01ccf12015-04-16 10:40:32 -070014from chromite.cli import command
David Pursell9476bf42015-03-30 13:34:27 -070015from chromite.cli import deploy
16from chromite.lib import cros_build_lib
17from chromite.lib import cros_test_lib
Gilad Arnold0e1b1da2015-06-10 06:41:05 -070018from chromite.lib import portage_util
Ralph Nathane01ccf12015-04-16 10:40:32 -070019from chromite.lib import remote_access
David Pursell9476bf42015-03-30 13:34:27 -070020try:
21 import portage
22except ImportError:
23 if cros_build_lib.IsInsideChroot():
24 raise
25
26
27# pylint: disable=protected-access
28
29
Ralph Nathane01ccf12015-04-16 10:40:32 -070030class 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
39
Mike Frysinger74ccd572015-05-21 21:18:20 -040040 def IsDirWritable(self, _):
Ralph Nathane01ccf12015-04-16 10:40:32 -070041 return True
42
43
David Pursell9476bf42015-03-30 13:34:27 -070044class ChromiumOSDeviceHandlerFake(object):
45 """Fake for chromite.lib.remote_access.ChomiumOSDeviceHandler."""
46
47 class RemoteAccessFake(object):
48 """Fake for chromite.lib.remote_access.RemoteAccess."""
49
50 def __init__(self):
51 self.remote_sh_output = None
52
53 def RemoteSh(self, *_args, **_kwargs):
54 return cros_build_lib.CommandResult(output=self.remote_sh_output)
55
Ralph Nathane01ccf12015-04-16 10:40:32 -070056 def __init__(self, *_args, **_kwargs):
David Pursell67a82762015-04-30 17:26:59 -070057 self._agent = self.RemoteAccessFake()
Mike Frysinger539db512015-05-21 18:14:01 -040058 self.device = ChromiumOSDeviceFake()
David Pursell67a82762015-04-30 17:26:59 -070059
Ralph Nathane01ccf12015-04-16 10:40:32 -070060 # TODO(dpursell): Mock remote access object in cros_test_lib (brbug.com/986).
David Pursell67a82762015-04-30 17:26:59 -070061 def GetAgent(self):
62 return self._agent
David Pursell9476bf42015-03-30 13:34:27 -070063
Ralph Nathane01ccf12015-04-16 10:40:32 -070064 def __exit__(self, _type, _value, _traceback):
65 pass
66
67 def __enter__(self):
68 return ChromiumOSDeviceFake()
69
70
71class BrilloDeployOperationFake(deploy.BrilloDeployOperation):
72 """Fake for deploy.BrilloDeployOperation."""
73 def __init__(self, pkg_count, emerge, queue):
74 super(BrilloDeployOperationFake, self).__init__(pkg_count, emerge)
75 self._queue = queue
76
Ralph Nathandc14ed92015-04-22 11:17:40 -070077 def ParseOutput(self, output=None):
78 super(BrilloDeployOperationFake, self).ParseOutput(output)
Ralph Nathane01ccf12015-04-16 10:40:32 -070079 self._queue.put('advance')
80
David Pursell9476bf42015-03-30 13:34:27 -070081
82class DbApiFake(object):
83 """Fake for Portage dbapi."""
84
85 def __init__(self, pkgs):
86 self.pkg_db = {}
87 for cpv, slot, rdeps_raw, build_time in pkgs:
88 self.pkg_db[cpv] = {
89 'SLOT': slot, 'RDEPEND': rdeps_raw, 'BUILD_TIME': build_time}
90
91 def cpv_all(self):
92 return self.pkg_db.keys()
93
94 def aux_get(self, cpv, keys):
95 pkg_info = self.pkg_db[cpv]
96 return [pkg_info[key] for key in keys]
97
98
Ralph Nathane01ccf12015-04-16 10:40:32 -070099class PackageScannerFake(object):
100 """Fake for PackageScanner."""
101
102 def __init__(self, packages):
103 self.pkgs = packages
104 self.listed = []
105 self.num_updates = None
106
107 def Run(self, _device, _root, _packages, _update, _deep, _deep_rev):
108 return self.pkgs, self.listed, self.num_updates
109
110
David Pursell9476bf42015-03-30 13:34:27 -0700111class PortageTreeFake(object):
112 """Fake for Portage tree."""
113
114 def __init__(self, dbapi):
115 self.dbapi = dbapi
116
117
Ralph Nathane01ccf12015-04-16 10:40:32 -0700118class TestInstallPackageScanner(cros_test_lib.MockOutputTestCase):
David Pursell9476bf42015-03-30 13:34:27 -0700119 """Test the update package scanner."""
120 _BOARD = 'foo_board'
121 _BUILD_ROOT = '/build/%s' % _BOARD
122 _VARTREE = [
123 ('foo/app1-1.2.3-r4', '0', 'foo/app2 !foo/app3', '1413309336'),
124 ('foo/app2-4.5.6-r7', '0', '', '1413309336'),
125 ('foo/app4-2.0.0-r1', '0', 'foo/app1 foo/app5', '1413309336'),
126 ('foo/app5-3.0.7-r3', '0', '', '1413309336'),
127 ]
128
129 def setUp(self):
130 """Patch imported modules."""
131 self.PatchObject(cros_build_lib, 'GetChoice', return_value=0)
132 self.device = ChromiumOSDeviceHandlerFake()
133 self.scanner = deploy._InstallPackageScanner(self._BUILD_ROOT)
134
135 def SetupVartree(self, vartree_pkgs):
David Pursell67a82762015-04-30 17:26:59 -0700136 self.device.GetAgent().remote_sh_output = json.dumps(vartree_pkgs)
David Pursell9476bf42015-03-30 13:34:27 -0700137
138 def SetupBintree(self, bintree_pkgs):
139 bintree = PortageTreeFake(DbApiFake(bintree_pkgs))
140 build_root = os.path.join(self._BUILD_ROOT, '')
141 portage_db = {build_root: {'bintree': bintree}}
142 self.PatchObject(portage, 'create_trees', return_value=portage_db)
143
144 def ValidatePkgs(self, actual, expected, constraints=None):
145 # Containing exactly the same packages.
146 self.assertEquals(sorted(expected), sorted(actual))
147 # Packages appear in the right order.
148 if constraints is not None:
149 for needs, needed in constraints:
150 self.assertGreater(actual.index(needs), actual.index(needed))
151
152 def testRunUpdatedVersion(self):
153 self.SetupVartree(self._VARTREE)
154 app1 = 'foo/app1-1.2.5-r4'
155 self.SetupBintree([
156 (app1, '0', 'foo/app2 !foo/app3', '1413309336'),
157 ('foo/app2-4.5.6-r7', '0', '', '1413309336'),
158 ])
159 installs, listed, num_updates = self.scanner.Run(
160 self.device, '/', ['app1'], True, True, True)
161 self.ValidatePkgs(installs, [app1])
162 self.ValidatePkgs(listed, [app1])
163 self.assertEquals(num_updates, 1)
164
165 def testRunUpdatedBuildTime(self):
166 self.SetupVartree(self._VARTREE)
167 app1 = 'foo/app1-1.2.3-r4'
168 self.SetupBintree([
169 (app1, '0', 'foo/app2 !foo/app3', '1413309350'),
170 ('foo/app2-4.5.6-r7', '0', '', '1413309336'),
171 ])
172 installs, listed, num_updates = self.scanner.Run(
173 self.device, '/', ['app1'], True, True, True)
174 self.ValidatePkgs(installs, [app1])
175 self.ValidatePkgs(listed, [app1])
176 self.assertEquals(num_updates, 1)
177
178 def testRunExistingDepUpdated(self):
179 self.SetupVartree(self._VARTREE)
180 app1 = 'foo/app1-1.2.5-r2'
181 app2 = 'foo/app2-4.5.8-r3'
182 self.SetupBintree([
183 (app1, '0', 'foo/app2 !foo/app3', '1413309350'),
184 (app2, '0', '', '1413309350'),
185 ])
186 installs, listed, num_updates = self.scanner.Run(
187 self.device, '/', ['app1'], True, True, True)
188 self.ValidatePkgs(installs, [app1, app2], constraints=[(app1, app2)])
189 self.ValidatePkgs(listed, [app1])
190 self.assertEquals(num_updates, 2)
191
192 def testRunMissingDepUpdated(self):
193 self.SetupVartree(self._VARTREE)
194 app1 = 'foo/app1-1.2.5-r2'
195 app6 = 'foo/app6-1.0.0-r1'
196 self.SetupBintree([
197 (app1, '0', 'foo/app2 !foo/app3 foo/app6', '1413309350'),
198 ('foo/app2-4.5.6-r7', '0', '', '1413309336'),
199 (app6, '0', '', '1413309350'),
200 ])
201 installs, listed, num_updates = self.scanner.Run(
202 self.device, '/', ['app1'], True, True, True)
203 self.ValidatePkgs(installs, [app1, app6], constraints=[(app1, app6)])
204 self.ValidatePkgs(listed, [app1])
205 self.assertEquals(num_updates, 1)
206
207 def testRunExistingRevDepUpdated(self):
208 self.SetupVartree(self._VARTREE)
209 app1 = 'foo/app1-1.2.5-r2'
210 app4 = 'foo/app4-2.0.1-r3'
211 self.SetupBintree([
212 (app1, '0', 'foo/app2 !foo/app3', '1413309350'),
213 (app4, '0', 'foo/app1 foo/app5', '1413309350'),
214 ('foo/app5-3.0.7-r3', '0', '', '1413309336'),
215 ])
216 installs, listed, num_updates = self.scanner.Run(
217 self.device, '/', ['app1'], True, True, True)
218 self.ValidatePkgs(installs, [app1, app4], constraints=[(app4, app1)])
219 self.ValidatePkgs(listed, [app1])
220 self.assertEquals(num_updates, 2)
221
222 def testRunMissingRevDepNotUpdated(self):
223 self.SetupVartree(self._VARTREE)
224 app1 = 'foo/app1-1.2.5-r2'
225 app6 = 'foo/app6-1.0.0-r1'
226 self.SetupBintree([
227 (app1, '0', 'foo/app2 !foo/app3', '1413309350'),
228 (app6, '0', 'foo/app1', '1413309350'),
229 ])
230 installs, listed, num_updates = self.scanner.Run(
231 self.device, '/', ['app1'], True, True, True)
232 self.ValidatePkgs(installs, [app1])
233 self.ValidatePkgs(listed, [app1])
234 self.assertEquals(num_updates, 1)
235
236 def testRunTransitiveDepsUpdated(self):
237 self.SetupVartree(self._VARTREE)
238 app1 = 'foo/app1-1.2.5-r2'
239 app2 = 'foo/app2-4.5.8-r3'
240 app4 = 'foo/app4-2.0.0-r1'
241 app5 = 'foo/app5-3.0.8-r2'
242 self.SetupBintree([
243 (app1, '0', 'foo/app2 !foo/app3', '1413309350'),
244 (app2, '0', '', '1413309350'),
245 (app4, '0', 'foo/app1 foo/app5', '1413309350'),
246 (app5, '0', '', '1413309350'),
247 ])
248 installs, listed, num_updates = self.scanner.Run(
249 self.device, '/', ['app1'], True, True, True)
250 self.ValidatePkgs(installs, [app1, app2, app4, app5],
251 constraints=[(app1, app2), (app4, app1), (app4, app5)])
252 self.ValidatePkgs(listed, [app1])
253 self.assertEquals(num_updates, 4)
254
255 def testRunDisjunctiveDepsExistingUpdated(self):
256 self.SetupVartree(self._VARTREE)
257 app1 = 'foo/app1-1.2.5-r2'
258 self.SetupBintree([
259 (app1, '0', '|| ( foo/app6 foo/app2 ) !foo/app3', '1413309350'),
260 ('foo/app2-4.5.6-r7', '0', '', '1413309336'),
261 ])
262 installs, listed, num_updates = self.scanner.Run(
263 self.device, '/', ['app1'], True, True, True)
264 self.ValidatePkgs(installs, [app1])
265 self.ValidatePkgs(listed, [app1])
266 self.assertEquals(num_updates, 1)
267
268 def testRunDisjunctiveDepsDefaultUpdated(self):
269 self.SetupVartree(self._VARTREE)
270 app1 = 'foo/app1-1.2.5-r2'
271 app7 = 'foo/app7-1.0.0-r1'
272 self.SetupBintree([
273 (app1, '0', '|| ( foo/app6 foo/app7 ) !foo/app3', '1413309350'),
274 (app7, '0', '', '1413309350'),
275 ])
276 installs, listed, num_updates = self.scanner.Run(
277 self.device, '/', ['app1'], True, True, True)
278 self.ValidatePkgs(installs, [app1, app7], constraints=[(app1, app7)])
279 self.ValidatePkgs(listed, [app1])
280 self.assertEquals(num_updates, 1)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700281
282
283class TestDeploy(cros_test_lib.ProgressBarTestCase):
284 """Test deploy.Deploy."""
285
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700286 @staticmethod
287 def FakeGetPackagesByCPV(cpvs, _strip, _sysroot):
288 return ['/path/to/%s.tbz2' % cpv.pv for cpv in cpvs]
289
Ralph Nathane01ccf12015-04-16 10:40:32 -0700290 def setUp(self):
291 self.PatchObject(remote_access, 'ChromiumOSDeviceHandler',
292 side_effect=ChromiumOSDeviceHandlerFake)
293 self.PatchObject(cros_build_lib, 'GetBoard', return_value=None)
294 self.PatchObject(cros_build_lib, 'GetSysroot', return_value='sysroot')
295 self.package_scanner = self.PatchObject(deploy, '_InstallPackageScanner')
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700296 self.get_packages_paths = self.PatchObject(
297 deploy, '_GetPackagesByCPV', side_effect=self.FakeGetPackagesByCPV)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700298 self.emerge = self.PatchObject(deploy, '_Emerge', return_value=None)
299 self.unmerge = self.PatchObject(deploy, '_Unmerge', return_value=None)
300
301 def testDeployEmerge(self):
302 """Test that deploy._Emerge is called for each package."""
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700303
304 _BINPKG = '/path/to/bar-1.2.5.tbz2'
305 def FakeIsFile(fname):
306 return fname == _BINPKG
307
308 packages = ['some/foo-1.2.3', _BINPKG, 'some/foobar-2.0']
Ralph Nathane01ccf12015-04-16 10:40:32 -0700309 self.package_scanner.return_value = PackageScannerFake(packages)
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700310 self.PatchObject(os.path, 'isfile', side_effect=FakeIsFile)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700311
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700312 deploy.Deploy(None, ['package'], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700313
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700314 # Check that package names were correctly resolved into binary packages.
315 self.get_packages_paths.assert_called_once_with(
316 [portage_util.SplitCPV(p) for p in packages if p != _BINPKG],
317 True, 'sysroot')
Ralph Nathane01ccf12015-04-16 10:40:32 -0700318 # Check that deploy._Emerge is called the right number of times.
319 self.assertEqual(self.emerge.call_count, len(packages))
320 self.assertEqual(self.unmerge.call_count, 0)
321
322 def testDeployUnmerge(self):
323 """Test that deploy._Unmerge is called for each package."""
324 packages = ['foo', 'bar', 'foobar']
325 self.package_scanner.return_value = PackageScannerFake(packages)
326
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700327 deploy.Deploy(None, ['package'], force=True, clean_binpkg=False,
Ralph Nathane01ccf12015-04-16 10:40:32 -0700328 emerge=False)
329
330 # Check that deploy._Unmerge is called the right number of times.
331 self.assertEqual(self.emerge.call_count, 0)
332 self.assertEqual(self.unmerge.call_count, len(packages))
333
334 def testDeployMergeWithProgressBar(self):
335 """Test that BrilloDeployOperation.Run() is called for merge."""
336 packages = ['foo', 'bar', 'foobar']
337 self.package_scanner.return_value = PackageScannerFake(packages)
338
339 run = self.PatchObject(deploy.BrilloDeployOperation, 'Run',
340 return_value=None)
341
342 self.PatchObject(command, 'UseProgressBar', return_value=True)
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700343 deploy.Deploy(None, ['package'], force=True, clean_binpkg=False)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700344
345 # Check that BrilloDeployOperation.Run was called.
346 self.assertTrue(run.called)
347
348 def testDeployUnmergeWithProgressBar(self):
349 """Test that BrilloDeployOperation.Run() is called for unmerge."""
350 packages = ['foo', 'bar', 'foobar']
351 self.package_scanner.return_value = PackageScannerFake(packages)
352
353 run = self.PatchObject(deploy.BrilloDeployOperation, 'Run',
354 return_value=None)
355
356 self.PatchObject(command, 'UseProgressBar', return_value=True)
Gilad Arnold0e1b1da2015-06-10 06:41:05 -0700357 deploy.Deploy(None, ['package'], force=True, clean_binpkg=False,
Ralph Nathane01ccf12015-04-16 10:40:32 -0700358 emerge=False)
359
360 # Check that BrilloDeployOperation.Run was called.
361 self.assertTrue(run.called)
362
363 def testBrilloDeployMergeOperation(self):
364 """Test that BrilloDeployOperation works for merge."""
365 def func(queue):
Ralph Nathan90475a12015-05-20 13:19:01 -0700366 for event in op.MERGE_EVENTS:
Ralph Nathane01ccf12015-04-16 10:40:32 -0700367 queue.get()
368 print(event)
369
370 queue = multiprocessing.Queue()
371 # Emerge one package.
372 op = BrilloDeployOperationFake(1, True, queue)
373
374 with self.OutputCapturer():
375 op.Run(func, queue)
376
377 # Check that the progress bar prints correctly.
Ralph Nathan90475a12015-05-20 13:19:01 -0700378 self.AssertProgressBarAllEvents(len(op.MERGE_EVENTS))
Ralph Nathane01ccf12015-04-16 10:40:32 -0700379
380 def testBrilloDeployUnmergeOperation(self):
381 """Test that BrilloDeployOperation works for unmerge."""
382 def func(queue):
Ralph Nathan90475a12015-05-20 13:19:01 -0700383 for event in op.UNMERGE_EVENTS:
Ralph Nathane01ccf12015-04-16 10:40:32 -0700384 queue.get()
385 print(event)
386
387 queue = multiprocessing.Queue()
388 # Unmerge one package.
389 op = BrilloDeployOperationFake(1, False, queue)
390
391 with self.OutputCapturer():
392 op.Run(func, queue)
393
394 # Check that the progress bar prints correctly.
Ralph Nathan90475a12015-05-20 13:19:01 -0700395 self.AssertProgressBarAllEvents(len(op.UNMERGE_EVENTS))