blob: d4b400d4e98d8cea2dff8fe7c7290ca0f427227f [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
David Pursellf1d16a62015-03-25 13:31:04 -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 flash module."""
7
8from __future__ import print_function
9
David Pursellf1d16a62015-03-25 13:31:04 -070010import os
Mike Frysinger3f087aa2020-03-20 06:03:16 -040011import sys
David Pursellf1d16a62015-03-25 13:31:04 -070012
Mike Frysinger6db648e2018-07-24 19:57:58 -040013import mock
14
David Pursellf1d16a62015-03-25 13:31:04 -070015from chromite.cli import flash
Sanika Kulkarnia8c4e3a2019-09-20 16:47:25 -070016from chromite.lib import auto_updater_transfer
David Pursellf1d16a62015-03-25 13:31:04 -070017from chromite.lib import commandline
18from chromite.lib import cros_build_lib
Ralph Nathan9b997232015-05-15 13:13:12 -070019from chromite.lib import cros_logging as logging
David Pursellf1d16a62015-03-25 13:31:04 -070020from chromite.lib import cros_test_lib
21from chromite.lib import dev_server_wrapper
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -070022from chromite.lib import osutils
David Pursellf1d16a62015-03-25 13:31:04 -070023from chromite.lib import partial_mock
24from chromite.lib import remote_access
Mike Frysingerdda695b2019-11-23 20:58:59 -050025from chromite.lib import remote_access_unittest
David Pursellf1d16a62015-03-25 13:31:04 -070026
Amin Hassanic0f06fa2019-01-28 15:24:47 -080027from chromite.lib.paygen import paygen_payload_lib
28from chromite.lib.paygen import paygen_stateful_payload_lib
29
David Pursellf1d16a62015-03-25 13:31:04 -070030
Mike Frysinger3f087aa2020-03-20 06:03:16 -040031assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
32
33
David Pursellf1d16a62015-03-25 13:31:04 -070034class RemoteDeviceUpdaterMock(partial_mock.PartialCmdMock):
35 """Mock out RemoteDeviceUpdater."""
Amin Hassani9800d432019-07-24 14:23:39 -070036 TARGET = 'chromite.lib.auto_updater.ChromiumOSUpdater'
xixuane851dfb2016-05-02 18:02:37 -070037 ATTRS = ('UpdateStateful', 'UpdateRootfs', 'SetupRootfsUpdate',
Amin Hassani3e87ce12020-10-22 10:39:36 -070038 'RebootAndVerify')
David Pursellf1d16a62015-03-25 13:31:04 -070039
40 def __init__(self):
41 partial_mock.PartialCmdMock.__init__(self)
42
43 def UpdateStateful(self, _inst, *_args, **_kwargs):
44 """Mock out UpdateStateful."""
45
46 def UpdateRootfs(self, _inst, *_args, **_kwargs):
47 """Mock out UpdateRootfs."""
48
49 def SetupRootfsUpdate(self, _inst, *_args, **_kwargs):
50 """Mock out SetupRootfsUpdate."""
51
xixuane851dfb2016-05-02 18:02:37 -070052 def RebootAndVerify(self, _inst, *_args, **_kwargs):
53 """Mock out RebootAndVerify."""
David Pursellf1d16a62015-03-25 13:31:04 -070054
Sanika Kulkarnie3b177b2019-11-26 14:42:48 -080055
Mike Frysingerdda695b2019-11-23 20:58:59 -050056class RemoteAccessMock(remote_access_unittest.RemoteShMock):
57 """Mock out RemoteAccess."""
58
59 ATTRS = ('RemoteSh', 'Rsync', 'Scp')
60
61 def Rsync(self, *_args, **_kwargs):
62 return cros_build_lib.CommandResult(returncode=0)
63
64 def Scp(self, *_args, **_kwargs):
65 return cros_build_lib.CommandResult(returncode=0)
66
67
David Pursellf1d16a62015-03-25 13:31:04 -070068class RemoteDeviceUpdaterTest(cros_test_lib.MockTempDirTestCase):
69 """Test the flow of flash.Flash() with RemoteDeviceUpdater."""
70
71 IMAGE = '/path/to/image'
72 DEVICE = commandline.Device(scheme=commandline.DEVICE_SCHEME_SSH,
Mike Frysingerb5a297f2019-11-23 21:17:41 -050073 hostname=remote_access.TEST_IP)
David Pursellf1d16a62015-03-25 13:31:04 -070074
75 def setUp(self):
76 """Patches objects."""
77 self.updater_mock = self.StartPatcher(RemoteDeviceUpdaterMock())
David Pursellf1d16a62015-03-25 13:31:04 -070078 self.PatchObject(dev_server_wrapper, 'GetImagePathWithXbuddy',
Gilad Arnolde62ec902015-04-24 14:41:02 -070079 return_value=('taco-paladin/R36/chromiumos_test_image.bin',
80 'remote/taco-paladin/R36/test'))
Amin Hassanic0f06fa2019-01-28 15:24:47 -080081 self.PatchObject(paygen_payload_lib, 'GenerateUpdatePayload')
82 self.PatchObject(paygen_stateful_payload_lib, 'GenerateStatefulPayload')
David Pursellf1d16a62015-03-25 13:31:04 -070083 self.PatchObject(remote_access, 'CHECK_INTERVAL', new=0)
Mike Frysingerdda695b2019-11-23 20:58:59 -050084 self.PatchObject(remote_access.ChromiumOSDevice, 'Pingable',
85 return_value=True)
86 m = self.StartPatcher(RemoteAccessMock())
87 m.AddCmdResult(['cat', '/etc/lsb-release'],
88 stdout='CHROMEOS_RELEASE_BOARD=board')
89 m.SetDefaultCmdResult()
David Pursellf1d16a62015-03-25 13:31:04 -070090
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -080091 def _ExistsMock(self, path, ret=True):
92 """Mock function for os.path.exists.
93
94 os.path.exists is used a lot; we only want to mock it for devserver/static,
95 and actually check if the file exists in all other cases (using os.access).
96
97 Args:
98 path: path to check.
99 ret: return value of mock.
100
101 Returns:
102 ret for paths under devserver/static, and the expected value of
103 os.path.exists otherwise.
104 """
Achuith Bhandarkareda9b222020-05-02 10:36:16 +0000105 if path.startswith(dev_server_wrapper.DEFAULT_STATIC_DIR):
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800106 return ret
107 return os.access(path, os.F_OK)
108
David Pursellf1d16a62015-03-25 13:31:04 -0700109 def testUpdateAll(self):
110 """Tests that update methods are called correctly."""
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800111 with mock.patch('os.path.exists', side_effect=self._ExistsMock):
Bertrand SIMONNETb34a98b2015-04-22 14:30:04 -0700112 flash.Flash(self.DEVICE, self.IMAGE)
113 self.assertTrue(self.updater_mock.patched['UpdateStateful'].called)
114 self.assertTrue(self.updater_mock.patched['UpdateRootfs'].called)
David Pursellf1d16a62015-03-25 13:31:04 -0700115
116 def testUpdateStateful(self):
117 """Tests that update methods are called correctly."""
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800118 with mock.patch('os.path.exists', side_effect=self._ExistsMock):
Bertrand SIMONNETb34a98b2015-04-22 14:30:04 -0700119 flash.Flash(self.DEVICE, self.IMAGE, rootfs_update=False)
120 self.assertTrue(self.updater_mock.patched['UpdateStateful'].called)
121 self.assertFalse(self.updater_mock.patched['UpdateRootfs'].called)
David Pursellf1d16a62015-03-25 13:31:04 -0700122
123 def testUpdateRootfs(self):
124 """Tests that update methods are called correctly."""
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800125 with mock.patch('os.path.exists', side_effect=self._ExistsMock):
Bertrand SIMONNETb34a98b2015-04-22 14:30:04 -0700126 flash.Flash(self.DEVICE, self.IMAGE, stateful_update=False)
127 self.assertFalse(self.updater_mock.patched['UpdateStateful'].called)
128 self.assertTrue(self.updater_mock.patched['UpdateRootfs'].called)
David Pursellf1d16a62015-03-25 13:31:04 -0700129
130 def testMissingPayloads(self):
131 """Tests we raise FlashError when payloads are missing."""
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800132 with mock.patch('os.path.exists',
133 side_effect=lambda p: self._ExistsMock(p, ret=False)):
Sanika Kulkarnia8c4e3a2019-09-20 16:47:25 -0700134 self.assertRaises(auto_updater_transfer.ChromiumOSTransferError,
135 flash.Flash, self.DEVICE, self.IMAGE)
David Pursellf1d16a62015-03-25 13:31:04 -0700136
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +0200137 def testFullPayload(self):
138 """Tests that we download full_payload and stateful using xBuddy."""
139 with mock.patch.object(
140 dev_server_wrapper,
141 'GetImagePathWithXbuddy',
142 return_value=('translated/xbuddy/path',
143 'resolved/xbuddy/path')) as mock_xbuddy:
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800144 with mock.patch('os.path.exists', side_effect=self._ExistsMock):
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +0200145 flash.Flash(self.DEVICE, self.IMAGE)
146
147 # Call to download full_payload and stateful. No other calls.
148 mock_xbuddy.assert_has_calls(
Achuith Bhandarkareda9b222020-05-02 10:36:16 +0000149 [mock.call('/path/to/image/full_payload', 'board', None, silent=True),
150 mock.call('/path/to/image/stateful', 'board', None, silent=True)])
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +0200151 self.assertEqual(mock_xbuddy.call_count, 2)
152
153 def testTestImage(self):
154 """Tests that we download the test image when the full payload fails."""
155 with mock.patch.object(
156 dev_server_wrapper,
157 'GetImagePathWithXbuddy',
158 side_effect=(dev_server_wrapper.ImagePathError,
159 ('translated/xbuddy/path',
160 'resolved/xbuddy/path'))) as mock_xbuddy:
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800161 with mock.patch('os.path.exists', side_effect=self._ExistsMock):
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +0200162 flash.Flash(self.DEVICE, self.IMAGE)
163
164 # Call to download full_payload and image. No other calls.
165 mock_xbuddy.assert_has_calls(
Achuith Bhandarkareda9b222020-05-02 10:36:16 +0000166 [mock.call('/path/to/image/full_payload', 'board', None, silent=True),
167 mock.call('/path/to/image', 'board', None)])
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +0200168 self.assertEqual(mock_xbuddy.call_count, 2)
169
David Pursellf1d16a62015-03-25 13:31:04 -0700170
171class USBImagerMock(partial_mock.PartialCmdMock):
172 """Mock out USBImager."""
173 TARGET = 'chromite.cli.flash.USBImager'
Amin Hassani04314b12020-12-15 15:59:54 -0800174 ATTRS = ('CopyImageToDevice', 'ChooseRemovableDevice',
175 'ListAllRemovableDevices', 'GetRemovableDeviceDescription')
David Pursellf1d16a62015-03-25 13:31:04 -0700176 VALID_IMAGE = True
177
178 def __init__(self):
179 partial_mock.PartialCmdMock.__init__(self)
180
181 def CopyImageToDevice(self, _inst, *_args, **_kwargs):
182 """Mock out CopyImageToDevice."""
183
David Pursellf1d16a62015-03-25 13:31:04 -0700184 def ChooseRemovableDevice(self, _inst, *_args, **_kwargs):
185 """Mock out ChooseRemovableDevice."""
186
187 def ListAllRemovableDevices(self, _inst, *_args, **_kwargs):
188 """Mock out ListAllRemovableDevices."""
189 return ['foo', 'taco', 'milk']
190
191 def GetRemovableDeviceDescription(self, _inst, *_args, **_kwargs):
192 """Mock out GetRemovableDeviceDescription."""
193
David Pursellf1d16a62015-03-25 13:31:04 -0700194
195class USBImagerTest(cros_test_lib.MockTempDirTestCase):
196 """Test the flow of flash.Flash() with USBImager."""
197 IMAGE = '/path/to/image'
198
199 def Device(self, path):
200 """Create a USB device for passing to flash.Flash()."""
201 return commandline.Device(scheme=commandline.DEVICE_SCHEME_USB,
202 path=path)
203
204 def setUp(self):
205 """Patches objects."""
206 self.usb_mock = USBImagerMock()
207 self.imager_mock = self.StartPatcher(self.usb_mock)
David Pursellf1d16a62015-03-25 13:31:04 -0700208 self.PatchObject(dev_server_wrapper, 'GetImagePathWithXbuddy',
Gilad Arnolde62ec902015-04-24 14:41:02 -0700209 return_value=('taco-paladin/R36/chromiumos_test_image.bin',
210 'remote/taco-paladin/R36/test'))
David Pursellf1d16a62015-03-25 13:31:04 -0700211 self.PatchObject(os.path, 'exists', return_value=True)
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -0700212 self.isgpt_mock = self.PatchObject(flash, '_IsFilePathGPTDiskImage',
213 return_value=True)
David Pursellf1d16a62015-03-25 13:31:04 -0700214
215 def testLocalImagePathCopy(self):
216 """Tests that imaging methods are called correctly."""
217 with mock.patch('os.path.isfile', return_value=True):
218 flash.Flash(self.Device('/dev/foo'), self.IMAGE)
219 self.assertTrue(self.imager_mock.patched['CopyImageToDevice'].called)
220
David Pursellf1d16a62015-03-25 13:31:04 -0700221 def testLocalBadImagePath(self):
222 """Tests that using an image not having the magic bytes has prompt."""
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -0700223 self.isgpt_mock.return_value = False
David Pursellf1d16a62015-03-25 13:31:04 -0700224 with mock.patch('os.path.isfile', return_value=True):
225 with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt:
226 mock_prompt.return_value = False
227 flash.Flash(self.Device('/dev/foo'), self.IMAGE)
228 self.assertTrue(mock_prompt.called)
229
230 def testNonLocalImagePath(self):
231 """Tests that we try to get the image path using xbuddy."""
232 with mock.patch.object(
233 dev_server_wrapper,
234 'GetImagePathWithXbuddy',
Gilad Arnolde62ec902015-04-24 14:41:02 -0700235 return_value=('translated/xbuddy/path',
236 'resolved/xbuddy/path')) as mock_xbuddy:
David Pursellf1d16a62015-03-25 13:31:04 -0700237 with mock.patch('os.path.isfile', return_value=False):
238 with mock.patch('os.path.isdir', return_value=False):
239 flash.Flash(self.Device('/dev/foo'), self.IMAGE)
240 self.assertTrue(mock_xbuddy.called)
241
242 def testConfirmNonRemovableDevice(self):
243 """Tests that we ask user to confirm if the device is not removable."""
244 with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt:
245 flash.Flash(self.Device('/dev/dummy'), self.IMAGE)
246 self.assertTrue(mock_prompt.called)
247
248 def testSkipPromptNonRemovableDevice(self):
249 """Tests that we skip the prompt for non-removable with --yes."""
250 with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt:
251 flash.Flash(self.Device('/dev/dummy'), self.IMAGE, yes=True)
252 self.assertFalse(mock_prompt.called)
253
254 def testChooseRemovableDevice(self):
255 """Tests that we ask user to choose a device if none is given."""
256 flash.Flash(self.Device(''), self.IMAGE)
257 self.assertTrue(self.imager_mock.patched['ChooseRemovableDevice'].called)
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -0700258
259
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600260class UsbImagerOperationTest(cros_test_lib.RunCommandTestCase):
Ralph Nathan9b997232015-05-15 13:13:12 -0700261 """Tests for flash.UsbImagerOperation."""
262 # pylint: disable=protected-access
263
264 def setUp(self):
265 self.PatchObject(flash.UsbImagerOperation, '__init__', return_value=None)
266
267 def testUsbImagerOperationCalled(self):
268 """Test that flash.UsbImagerOperation is called when log level <= NOTICE."""
269 expected_cmd = ['dd', 'if=foo', 'of=bar', 'bs=4M', 'iflag=fullblock',
Frank Huang8e626432019-06-24 19:51:08 +0800270 'oflag=direct', 'conv=fdatasync']
Achuith Bhandarkaree1336f2020-04-18 11:44:09 +0000271 usb_imager = flash.USBImager('dummy_device', 'board', 'foo', 'latest')
Ralph Nathan9b997232015-05-15 13:13:12 -0700272 run_mock = self.PatchObject(flash.UsbImagerOperation, 'Run')
273 self.PatchObject(logging.Logger, 'getEffectiveLevel',
274 return_value=logging.NOTICE)
275 usb_imager.CopyImageToDevice('foo', 'bar')
276
277 # Check that flash.UsbImagerOperation.Run() is called correctly.
Mike Frysinger45602c72019-09-22 02:15:11 -0400278 run_mock.assert_called_with(cros_build_lib.sudo_run, expected_cmd,
Mike Frysinger3d5de8f2019-10-23 00:48:39 -0400279 debug_level=logging.NOTICE, encoding='utf-8',
280 update_period=0.5)
Ralph Nathan9b997232015-05-15 13:13:12 -0700281
282 def testSudoRunCommandCalled(self):
Mike Frysinger45602c72019-09-22 02:15:11 -0400283 """Test that sudo_run is called when log level > NOTICE."""
Ralph Nathan9b997232015-05-15 13:13:12 -0700284 expected_cmd = ['dd', 'if=foo', 'of=bar', 'bs=4M', 'iflag=fullblock',
Frank Huang8e626432019-06-24 19:51:08 +0800285 'oflag=direct', 'conv=fdatasync']
Achuith Bhandarkaree1336f2020-04-18 11:44:09 +0000286 usb_imager = flash.USBImager('dummy_device', 'board', 'foo', 'latest')
Mike Frysinger45602c72019-09-22 02:15:11 -0400287 run_mock = self.PatchObject(cros_build_lib, 'sudo_run')
Ralph Nathan9b997232015-05-15 13:13:12 -0700288 self.PatchObject(logging.Logger, 'getEffectiveLevel',
289 return_value=logging.WARNING)
290 usb_imager.CopyImageToDevice('foo', 'bar')
291
Mike Frysinger45602c72019-09-22 02:15:11 -0400292 # Check that sudo_run() is called correctly.
Ralph Nathan9b997232015-05-15 13:13:12 -0700293 run_mock.assert_any_call(expected_cmd, debug_level=logging.NOTICE,
294 print_cmd=False)
295
296 def testPingDD(self):
297 """Test that UsbImagerOperation._PingDD() sends the correct signal."""
298 expected_cmd = ['kill', '-USR1', '5']
Mike Frysinger45602c72019-09-22 02:15:11 -0400299 run_mock = self.PatchObject(cros_build_lib, 'sudo_run')
Ralph Nathan9b997232015-05-15 13:13:12 -0700300 op = flash.UsbImagerOperation('foo')
301 op._PingDD(5)
302
Mike Frysinger45602c72019-09-22 02:15:11 -0400303 # Check that sudo_run was called correctly.
Ralph Nathan9b997232015-05-15 13:13:12 -0700304 run_mock.assert_called_with(expected_cmd, print_cmd=False)
305
306 def testGetDDPidFound(self):
307 """Check that the expected pid is returned for _GetDDPid()."""
308 expected_pid = 5
309 op = flash.UsbImagerOperation('foo')
310 self.PatchObject(osutils, 'IsChildProcess', return_value=True)
311 self.rc.AddCmdResult(partial_mock.Ignore(),
312 output='%d\n10\n' % expected_pid)
313
314 pid = op._GetDDPid()
315
316 # Check that the correct pid was returned.
317 self.assertEqual(pid, expected_pid)
318
319 def testGetDDPidNotFound(self):
320 """Check that -1 is returned for _GetDDPid() if the pids aren't valid."""
321 expected_pid = -1
322 op = flash.UsbImagerOperation('foo')
323 self.PatchObject(osutils, 'IsChildProcess', return_value=False)
324 self.rc.AddCmdResult(partial_mock.Ignore(), output='5\n10\n')
325
326 pid = op._GetDDPid()
327
328 # Check that the correct pid was returned.
329 self.assertEqual(pid, expected_pid)
330
331
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -0700332class FlashUtilTest(cros_test_lib.MockTempDirTestCase):
333 """Tests the helpers from cli.flash."""
334
335 def testChooseImage(self):
336 """Tests that we can detect a GPT image."""
337 # pylint: disable=protected-access
338
339 with self.PatchObject(flash, '_IsFilePathGPTDiskImage', return_value=True):
340 # No images defined. Choosing the image should raise an error.
341 with self.assertRaises(ValueError):
342 flash._ChooseImageFromDirectory(self.tempdir)
343
344 file_a = os.path.join(self.tempdir, 'a')
345 osutils.Touch(file_a)
346 # Only one image available, it should be selected automatically.
347 self.assertEqual(file_a, flash._ChooseImageFromDirectory(self.tempdir))
348
349 osutils.Touch(os.path.join(self.tempdir, 'b'))
350 file_c = os.path.join(self.tempdir, 'c')
351 osutils.Touch(file_c)
352 osutils.Touch(os.path.join(self.tempdir, 'd'))
353
354 # Multiple images available, we should ask the user to select the right
355 # image.
356 with self.PatchObject(cros_build_lib, 'GetChoice', return_value=2):
357 self.assertEqual(file_c, flash._ChooseImageFromDirectory(self.tempdir))
Mike Frysinger32759e42016-12-21 18:40:16 -0500358
359 def testIsFilePathGPTDiskImage(self):
360 """Tests the GPT image probing."""
361 # pylint: disable=protected-access
362
Mike Frysinger3d5de8f2019-10-23 00:48:39 -0400363 INVALID_PMBR = b' ' * 0x200
364 INVALID_GPT = b' ' * 0x200
365 VALID_PMBR = (b' ' * 0x1fe) + b'\x55\xaa'
366 VALID_GPT = b'EFI PART' + (b' ' * 0x1f8)
Mike Frysinger32759e42016-12-21 18:40:16 -0500367 TESTCASES = (
368 (False, False, INVALID_PMBR + INVALID_GPT),
369 (False, False, VALID_PMBR + INVALID_GPT),
370 (False, True, INVALID_PMBR + VALID_GPT),
371 (True, True, VALID_PMBR + VALID_GPT),
372 )
373
374 img = os.path.join(self.tempdir, 'img.bin')
375 for exp_pmbr_t, exp_pmbr_f, data in TESTCASES:
Mike Frysinger3d5de8f2019-10-23 00:48:39 -0400376 osutils.WriteFile(img, data, mode='wb')
Mike Frysinger32759e42016-12-21 18:40:16 -0500377 self.assertEqual(
378 flash._IsFilePathGPTDiskImage(img, require_pmbr=True), exp_pmbr_t)
379 self.assertEqual(
380 flash._IsFilePathGPTDiskImage(img, require_pmbr=False), exp_pmbr_f)