blob: 40e90934e50131170a3aabb6c9cf88144e3668f0 [file] [log] [blame]
David Pursell9476bf42015-03-30 13:34:27 -07001# Copyright 2015 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Unit tests for the deploy module."""
6
7from __future__ import print_function
8
9import json
Ralph Nathane01ccf12015-04-16 10:40:32 -070010import multiprocessing
David Pursell9476bf42015-03-30 13:34:27 -070011import os
12
Ralph Nathane01ccf12015-04-16 10:40:32 -070013from chromite.cli import command
David Pursell9476bf42015-03-30 13:34:27 -070014from chromite.cli import deploy
15from chromite.lib import cros_build_lib
16from chromite.lib import cros_test_lib
Ralph Nathane01ccf12015-04-16 10:40:32 -070017from chromite.lib import remote_access
David Pursell9476bf42015-03-30 13:34:27 -070018try:
19 import portage
20except ImportError:
21 if cros_build_lib.IsInsideChroot():
22 raise
23
24
25# pylint: disable=protected-access
26
27
Ralph Nathane01ccf12015-04-16 10:40:32 -070028class ChromiumOSDeviceFake(object):
29 """Fake for device."""
30
31 def __init__(self):
32 self.board = 'board'
33 self.hostname = None
34 self.username = None
35 self.port = None
36 self.lsb_release = None
37
Mike Frysinger74ccd572015-05-21 21:18:20 -040038 def IsDirWritable(self, _):
Ralph Nathane01ccf12015-04-16 10:40:32 -070039 return True
40
41
David Pursell9476bf42015-03-30 13:34:27 -070042class ChromiumOSDeviceHandlerFake(object):
43 """Fake for chromite.lib.remote_access.ChomiumOSDeviceHandler."""
44
45 class RemoteAccessFake(object):
46 """Fake for chromite.lib.remote_access.RemoteAccess."""
47
48 def __init__(self):
49 self.remote_sh_output = None
50
51 def RemoteSh(self, *_args, **_kwargs):
52 return cros_build_lib.CommandResult(output=self.remote_sh_output)
53
Ralph Nathane01ccf12015-04-16 10:40:32 -070054 def __init__(self, *_args, **_kwargs):
David Pursell67a82762015-04-30 17:26:59 -070055 self._agent = self.RemoteAccessFake()
Mike Frysinger539db512015-05-21 18:14:01 -040056 self.device = ChromiumOSDeviceFake()
David Pursell67a82762015-04-30 17:26:59 -070057
Ralph Nathane01ccf12015-04-16 10:40:32 -070058 # TODO(dpursell): Mock remote access object in cros_test_lib (brbug.com/986).
David Pursell67a82762015-04-30 17:26:59 -070059 def GetAgent(self):
60 return self._agent
David Pursell9476bf42015-03-30 13:34:27 -070061
Ralph Nathane01ccf12015-04-16 10:40:32 -070062 def __exit__(self, _type, _value, _traceback):
63 pass
64
65 def __enter__(self):
66 return ChromiumOSDeviceFake()
67
68
69class BrilloDeployOperationFake(deploy.BrilloDeployOperation):
70 """Fake for deploy.BrilloDeployOperation."""
71 def __init__(self, pkg_count, emerge, queue):
72 super(BrilloDeployOperationFake, self).__init__(pkg_count, emerge)
73 self._queue = queue
74
Ralph Nathandc14ed92015-04-22 11:17:40 -070075 def ParseOutput(self, output=None):
76 super(BrilloDeployOperationFake, self).ParseOutput(output)
Ralph Nathane01ccf12015-04-16 10:40:32 -070077 self._queue.put('advance')
78
David Pursell9476bf42015-03-30 13:34:27 -070079
80class DbApiFake(object):
81 """Fake for Portage dbapi."""
82
83 def __init__(self, pkgs):
84 self.pkg_db = {}
85 for cpv, slot, rdeps_raw, build_time in pkgs:
86 self.pkg_db[cpv] = {
87 'SLOT': slot, 'RDEPEND': rdeps_raw, 'BUILD_TIME': build_time}
88
89 def cpv_all(self):
90 return self.pkg_db.keys()
91
92 def aux_get(self, cpv, keys):
93 pkg_info = self.pkg_db[cpv]
94 return [pkg_info[key] for key in keys]
95
96
Ralph Nathane01ccf12015-04-16 10:40:32 -070097class PackageScannerFake(object):
98 """Fake for PackageScanner."""
99
100 def __init__(self, packages):
101 self.pkgs = packages
102 self.listed = []
103 self.num_updates = None
104
105 def Run(self, _device, _root, _packages, _update, _deep, _deep_rev):
106 return self.pkgs, self.listed, self.num_updates
107
108
David Pursell9476bf42015-03-30 13:34:27 -0700109class PortageTreeFake(object):
110 """Fake for Portage tree."""
111
112 def __init__(self, dbapi):
113 self.dbapi = dbapi
114
115
Ralph Nathane01ccf12015-04-16 10:40:32 -0700116class TestInstallPackageScanner(cros_test_lib.MockOutputTestCase):
David Pursell9476bf42015-03-30 13:34:27 -0700117 """Test the update package scanner."""
118 _BOARD = 'foo_board'
119 _BUILD_ROOT = '/build/%s' % _BOARD
120 _VARTREE = [
121 ('foo/app1-1.2.3-r4', '0', 'foo/app2 !foo/app3', '1413309336'),
122 ('foo/app2-4.5.6-r7', '0', '', '1413309336'),
123 ('foo/app4-2.0.0-r1', '0', 'foo/app1 foo/app5', '1413309336'),
124 ('foo/app5-3.0.7-r3', '0', '', '1413309336'),
125 ]
126
127 def setUp(self):
128 """Patch imported modules."""
129 self.PatchObject(cros_build_lib, 'GetChoice', return_value=0)
130 self.device = ChromiumOSDeviceHandlerFake()
131 self.scanner = deploy._InstallPackageScanner(self._BUILD_ROOT)
132
133 def SetupVartree(self, vartree_pkgs):
David Pursell67a82762015-04-30 17:26:59 -0700134 self.device.GetAgent().remote_sh_output = json.dumps(vartree_pkgs)
David Pursell9476bf42015-03-30 13:34:27 -0700135
136 def SetupBintree(self, bintree_pkgs):
137 bintree = PortageTreeFake(DbApiFake(bintree_pkgs))
138 build_root = os.path.join(self._BUILD_ROOT, '')
139 portage_db = {build_root: {'bintree': bintree}}
140 self.PatchObject(portage, 'create_trees', return_value=portage_db)
141
142 def ValidatePkgs(self, actual, expected, constraints=None):
143 # Containing exactly the same packages.
144 self.assertEquals(sorted(expected), sorted(actual))
145 # Packages appear in the right order.
146 if constraints is not None:
147 for needs, needed in constraints:
148 self.assertGreater(actual.index(needs), actual.index(needed))
149
150 def testRunUpdatedVersion(self):
151 self.SetupVartree(self._VARTREE)
152 app1 = 'foo/app1-1.2.5-r4'
153 self.SetupBintree([
154 (app1, '0', 'foo/app2 !foo/app3', '1413309336'),
155 ('foo/app2-4.5.6-r7', '0', '', '1413309336'),
156 ])
157 installs, listed, num_updates = self.scanner.Run(
158 self.device, '/', ['app1'], True, True, True)
159 self.ValidatePkgs(installs, [app1])
160 self.ValidatePkgs(listed, [app1])
161 self.assertEquals(num_updates, 1)
162
163 def testRunUpdatedBuildTime(self):
164 self.SetupVartree(self._VARTREE)
165 app1 = 'foo/app1-1.2.3-r4'
166 self.SetupBintree([
167 (app1, '0', 'foo/app2 !foo/app3', '1413309350'),
168 ('foo/app2-4.5.6-r7', '0', '', '1413309336'),
169 ])
170 installs, listed, num_updates = self.scanner.Run(
171 self.device, '/', ['app1'], True, True, True)
172 self.ValidatePkgs(installs, [app1])
173 self.ValidatePkgs(listed, [app1])
174 self.assertEquals(num_updates, 1)
175
176 def testRunExistingDepUpdated(self):
177 self.SetupVartree(self._VARTREE)
178 app1 = 'foo/app1-1.2.5-r2'
179 app2 = 'foo/app2-4.5.8-r3'
180 self.SetupBintree([
181 (app1, '0', 'foo/app2 !foo/app3', '1413309350'),
182 (app2, '0', '', '1413309350'),
183 ])
184 installs, listed, num_updates = self.scanner.Run(
185 self.device, '/', ['app1'], True, True, True)
186 self.ValidatePkgs(installs, [app1, app2], constraints=[(app1, app2)])
187 self.ValidatePkgs(listed, [app1])
188 self.assertEquals(num_updates, 2)
189
190 def testRunMissingDepUpdated(self):
191 self.SetupVartree(self._VARTREE)
192 app1 = 'foo/app1-1.2.5-r2'
193 app6 = 'foo/app6-1.0.0-r1'
194 self.SetupBintree([
195 (app1, '0', 'foo/app2 !foo/app3 foo/app6', '1413309350'),
196 ('foo/app2-4.5.6-r7', '0', '', '1413309336'),
197 (app6, '0', '', '1413309350'),
198 ])
199 installs, listed, num_updates = self.scanner.Run(
200 self.device, '/', ['app1'], True, True, True)
201 self.ValidatePkgs(installs, [app1, app6], constraints=[(app1, app6)])
202 self.ValidatePkgs(listed, [app1])
203 self.assertEquals(num_updates, 1)
204
205 def testRunExistingRevDepUpdated(self):
206 self.SetupVartree(self._VARTREE)
207 app1 = 'foo/app1-1.2.5-r2'
208 app4 = 'foo/app4-2.0.1-r3'
209 self.SetupBintree([
210 (app1, '0', 'foo/app2 !foo/app3', '1413309350'),
211 (app4, '0', 'foo/app1 foo/app5', '1413309350'),
212 ('foo/app5-3.0.7-r3', '0', '', '1413309336'),
213 ])
214 installs, listed, num_updates = self.scanner.Run(
215 self.device, '/', ['app1'], True, True, True)
216 self.ValidatePkgs(installs, [app1, app4], constraints=[(app4, app1)])
217 self.ValidatePkgs(listed, [app1])
218 self.assertEquals(num_updates, 2)
219
220 def testRunMissingRevDepNotUpdated(self):
221 self.SetupVartree(self._VARTREE)
222 app1 = 'foo/app1-1.2.5-r2'
223 app6 = 'foo/app6-1.0.0-r1'
224 self.SetupBintree([
225 (app1, '0', 'foo/app2 !foo/app3', '1413309350'),
226 (app6, '0', 'foo/app1', '1413309350'),
227 ])
228 installs, listed, num_updates = self.scanner.Run(
229 self.device, '/', ['app1'], True, True, True)
230 self.ValidatePkgs(installs, [app1])
231 self.ValidatePkgs(listed, [app1])
232 self.assertEquals(num_updates, 1)
233
234 def testRunTransitiveDepsUpdated(self):
235 self.SetupVartree(self._VARTREE)
236 app1 = 'foo/app1-1.2.5-r2'
237 app2 = 'foo/app2-4.5.8-r3'
238 app4 = 'foo/app4-2.0.0-r1'
239 app5 = 'foo/app5-3.0.8-r2'
240 self.SetupBintree([
241 (app1, '0', 'foo/app2 !foo/app3', '1413309350'),
242 (app2, '0', '', '1413309350'),
243 (app4, '0', 'foo/app1 foo/app5', '1413309350'),
244 (app5, '0', '', '1413309350'),
245 ])
246 installs, listed, num_updates = self.scanner.Run(
247 self.device, '/', ['app1'], True, True, True)
248 self.ValidatePkgs(installs, [app1, app2, app4, app5],
249 constraints=[(app1, app2), (app4, app1), (app4, app5)])
250 self.ValidatePkgs(listed, [app1])
251 self.assertEquals(num_updates, 4)
252
253 def testRunDisjunctiveDepsExistingUpdated(self):
254 self.SetupVartree(self._VARTREE)
255 app1 = 'foo/app1-1.2.5-r2'
256 self.SetupBintree([
257 (app1, '0', '|| ( foo/app6 foo/app2 ) !foo/app3', '1413309350'),
258 ('foo/app2-4.5.6-r7', '0', '', '1413309336'),
259 ])
260 installs, listed, num_updates = self.scanner.Run(
261 self.device, '/', ['app1'], True, True, True)
262 self.ValidatePkgs(installs, [app1])
263 self.ValidatePkgs(listed, [app1])
264 self.assertEquals(num_updates, 1)
265
266 def testRunDisjunctiveDepsDefaultUpdated(self):
267 self.SetupVartree(self._VARTREE)
268 app1 = 'foo/app1-1.2.5-r2'
269 app7 = 'foo/app7-1.0.0-r1'
270 self.SetupBintree([
271 (app1, '0', '|| ( foo/app6 foo/app7 ) !foo/app3', '1413309350'),
272 (app7, '0', '', '1413309350'),
273 ])
274 installs, listed, num_updates = self.scanner.Run(
275 self.device, '/', ['app1'], True, True, True)
276 self.ValidatePkgs(installs, [app1, app7], constraints=[(app1, app7)])
277 self.ValidatePkgs(listed, [app1])
278 self.assertEquals(num_updates, 1)
Ralph Nathane01ccf12015-04-16 10:40:32 -0700279
280
281class TestDeploy(cros_test_lib.ProgressBarTestCase):
282 """Test deploy.Deploy."""
283
284 def setUp(self):
285 self.PatchObject(remote_access, 'ChromiumOSDeviceHandler',
286 side_effect=ChromiumOSDeviceHandlerFake)
287 self.PatchObject(cros_build_lib, 'GetBoard', return_value=None)
288 self.PatchObject(cros_build_lib, 'GetSysroot', return_value='sysroot')
289 self.package_scanner = self.PatchObject(deploy, '_InstallPackageScanner')
290 self.emerge = self.PatchObject(deploy, '_Emerge', return_value=None)
291 self.unmerge = self.PatchObject(deploy, '_Unmerge', return_value=None)
292
293 def testDeployEmerge(self):
294 """Test that deploy._Emerge is called for each package."""
295 packages = ['foo', 'bar', 'foobar']
296 self.package_scanner.return_value = PackageScannerFake(packages)
297
298 deploy.Deploy(None, 'package', force=True, clean_binpkg=False)
299
300 # Check that deploy._Emerge is called the right number of times.
301 self.assertEqual(self.emerge.call_count, len(packages))
302 self.assertEqual(self.unmerge.call_count, 0)
303
304 def testDeployUnmerge(self):
305 """Test that deploy._Unmerge is called for each package."""
306 packages = ['foo', 'bar', 'foobar']
307 self.package_scanner.return_value = PackageScannerFake(packages)
308
309 deploy.Deploy(None, 'package', force=True, clean_binpkg=False,
310 emerge=False)
311
312 # Check that deploy._Unmerge is called the right number of times.
313 self.assertEqual(self.emerge.call_count, 0)
314 self.assertEqual(self.unmerge.call_count, len(packages))
315
316 def testDeployMergeWithProgressBar(self):
317 """Test that BrilloDeployOperation.Run() is called for merge."""
318 packages = ['foo', 'bar', 'foobar']
319 self.package_scanner.return_value = PackageScannerFake(packages)
320
321 run = self.PatchObject(deploy.BrilloDeployOperation, 'Run',
322 return_value=None)
323
324 self.PatchObject(command, 'UseProgressBar', return_value=True)
325 deploy.Deploy(None, 'package', force=True, clean_binpkg=False)
326
327 # Check that BrilloDeployOperation.Run was called.
328 self.assertTrue(run.called)
329
330 def testDeployUnmergeWithProgressBar(self):
331 """Test that BrilloDeployOperation.Run() is called for unmerge."""
332 packages = ['foo', 'bar', 'foobar']
333 self.package_scanner.return_value = PackageScannerFake(packages)
334
335 run = self.PatchObject(deploy.BrilloDeployOperation, 'Run',
336 return_value=None)
337
338 self.PatchObject(command, 'UseProgressBar', return_value=True)
339 deploy.Deploy(None, 'package', force=True, clean_binpkg=False,
340 emerge=False)
341
342 # Check that BrilloDeployOperation.Run was called.
343 self.assertTrue(run.called)
344
345 def testBrilloDeployMergeOperation(self):
346 """Test that BrilloDeployOperation works for merge."""
347 def func(queue):
Ralph Nathan90475a12015-05-20 13:19:01 -0700348 for event in op.MERGE_EVENTS:
Ralph Nathane01ccf12015-04-16 10:40:32 -0700349 queue.get()
350 print(event)
351
352 queue = multiprocessing.Queue()
353 # Emerge one package.
354 op = BrilloDeployOperationFake(1, True, queue)
355
356 with self.OutputCapturer():
357 op.Run(func, queue)
358
359 # Check that the progress bar prints correctly.
Ralph Nathan90475a12015-05-20 13:19:01 -0700360 self.AssertProgressBarAllEvents(len(op.MERGE_EVENTS))
Ralph Nathane01ccf12015-04-16 10:40:32 -0700361
362 def testBrilloDeployUnmergeOperation(self):
363 """Test that BrilloDeployOperation works for unmerge."""
364 def func(queue):
Ralph Nathan90475a12015-05-20 13:19:01 -0700365 for event in op.UNMERGE_EVENTS:
Ralph Nathane01ccf12015-04-16 10:40:32 -0700366 queue.get()
367 print(event)
368
369 queue = multiprocessing.Queue()
370 # Unmerge one package.
371 op = BrilloDeployOperationFake(1, False, queue)
372
373 with self.OutputCapturer():
374 op.Run(func, queue)
375
376 # Check that the progress bar prints correctly.
Ralph Nathan90475a12015-05-20 13:19:01 -0700377 self.AssertProgressBarAllEvents(len(op.UNMERGE_EVENTS))