Mike Frysinger | e58c0e2 | 2017-10-04 15:43:30 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -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 flash module.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 10 | import os |
Mike Frysinger | 3f087aa | 2020-03-20 06:03:16 -0400 | [diff] [blame] | 11 | import sys |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 12 | |
Mike Frysinger | 6db648e | 2018-07-24 19:57:58 -0400 | [diff] [blame] | 13 | import mock |
| 14 | |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 15 | from chromite.cli import flash |
Sanika Kulkarni | a8c4e3a | 2019-09-20 16:47:25 -0700 | [diff] [blame] | 16 | from chromite.lib import auto_updater_transfer |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 17 | from chromite.lib import commandline |
| 18 | from chromite.lib import cros_build_lib |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_logging as logging |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 20 | from chromite.lib import cros_test_lib |
| 21 | from chromite.lib import dev_server_wrapper |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 22 | from chromite.lib import osutils |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 23 | from chromite.lib import partial_mock |
| 24 | from chromite.lib import remote_access |
Mike Frysinger | dda695b | 2019-11-23 20:58:59 -0500 | [diff] [blame] | 25 | from chromite.lib import remote_access_unittest |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 26 | |
Amin Hassani | c0f06fa | 2019-01-28 15:24:47 -0800 | [diff] [blame] | 27 | from chromite.lib.paygen import paygen_payload_lib |
| 28 | from chromite.lib.paygen import paygen_stateful_payload_lib |
| 29 | |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 30 | |
Mike Frysinger | 3f087aa | 2020-03-20 06:03:16 -0400 | [diff] [blame] | 31 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 32 | |
| 33 | |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 34 | class RemoteDeviceUpdaterMock(partial_mock.PartialCmdMock): |
| 35 | """Mock out RemoteDeviceUpdater.""" |
Amin Hassani | 9800d43 | 2019-07-24 14:23:39 -0700 | [diff] [blame] | 36 | TARGET = 'chromite.lib.auto_updater.ChromiumOSUpdater' |
xixuan | e851dfb | 2016-05-02 18:02:37 -0700 | [diff] [blame] | 37 | ATTRS = ('UpdateStateful', 'UpdateRootfs', 'SetupRootfsUpdate', |
Sanika Kulkarni | e3b177b | 2019-11-26 14:42:48 -0800 | [diff] [blame] | 38 | 'RebootAndVerify', 'PreparePayloadPropsFile', |
| 39 | '_FixPayloadPropertiesFile') |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 40 | |
| 41 | def __init__(self): |
| 42 | partial_mock.PartialCmdMock.__init__(self) |
| 43 | |
| 44 | def UpdateStateful(self, _inst, *_args, **_kwargs): |
| 45 | """Mock out UpdateStateful.""" |
| 46 | |
| 47 | def UpdateRootfs(self, _inst, *_args, **_kwargs): |
| 48 | """Mock out UpdateRootfs.""" |
| 49 | |
| 50 | def SetupRootfsUpdate(self, _inst, *_args, **_kwargs): |
| 51 | """Mock out SetupRootfsUpdate.""" |
| 52 | |
xixuan | e851dfb | 2016-05-02 18:02:37 -0700 | [diff] [blame] | 53 | def RebootAndVerify(self, _inst, *_args, **_kwargs): |
| 54 | """Mock out RebootAndVerify.""" |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 55 | |
Sanika Kulkarni | 00b9d68 | 2019-11-26 09:43:20 -0800 | [diff] [blame] | 56 | def PreparePayloadPropsFile(self, _inst, *_args, **_kwargs): |
| 57 | """Mock out PreparePayloadPropsFile.""" |
Achuith Bhandarkar | 4d4275f | 2019-10-01 17:07:23 +0200 | [diff] [blame] | 58 | |
Sanika Kulkarni | e3b177b | 2019-11-26 14:42:48 -0800 | [diff] [blame] | 59 | def _FixPayloadPropertiesFile(self, _inst, *_args, **_kwargs): |
| 60 | """Mock out _FixPayloadPropertiesFile.""" |
| 61 | |
| 62 | |
Mike Frysinger | dda695b | 2019-11-23 20:58:59 -0500 | [diff] [blame] | 63 | class RemoteAccessMock(remote_access_unittest.RemoteShMock): |
| 64 | """Mock out RemoteAccess.""" |
| 65 | |
| 66 | ATTRS = ('RemoteSh', 'Rsync', 'Scp') |
| 67 | |
| 68 | def Rsync(self, *_args, **_kwargs): |
| 69 | return cros_build_lib.CommandResult(returncode=0) |
| 70 | |
| 71 | def Scp(self, *_args, **_kwargs): |
| 72 | return cros_build_lib.CommandResult(returncode=0) |
| 73 | |
| 74 | |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 75 | class RemoteDeviceUpdaterTest(cros_test_lib.MockTempDirTestCase): |
| 76 | """Test the flow of flash.Flash() with RemoteDeviceUpdater.""" |
| 77 | |
| 78 | IMAGE = '/path/to/image' |
| 79 | DEVICE = commandline.Device(scheme=commandline.DEVICE_SCHEME_SSH, |
Mike Frysinger | b5a297f | 2019-11-23 21:17:41 -0500 | [diff] [blame] | 80 | hostname=remote_access.TEST_IP) |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 81 | |
| 82 | def setUp(self): |
| 83 | """Patches objects.""" |
| 84 | self.updater_mock = self.StartPatcher(RemoteDeviceUpdaterMock()) |
| 85 | self.PatchObject(dev_server_wrapper, 'GenerateXbuddyRequest', |
| 86 | return_value='xbuddy/local/latest') |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 87 | self.PatchObject(dev_server_wrapper, 'GetImagePathWithXbuddy', |
Gilad Arnold | e62ec90 | 2015-04-24 14:41:02 -0700 | [diff] [blame] | 88 | return_value=('taco-paladin/R36/chromiumos_test_image.bin', |
| 89 | 'remote/taco-paladin/R36/test')) |
Amin Hassani | c0f06fa | 2019-01-28 15:24:47 -0800 | [diff] [blame] | 90 | self.PatchObject(paygen_payload_lib, 'GenerateUpdatePayload') |
| 91 | self.PatchObject(paygen_stateful_payload_lib, 'GenerateStatefulPayload') |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 92 | self.PatchObject(remote_access, 'CHECK_INTERVAL', new=0) |
Mike Frysinger | dda695b | 2019-11-23 20:58:59 -0500 | [diff] [blame] | 93 | self.PatchObject(remote_access.ChromiumOSDevice, 'Pingable', |
| 94 | return_value=True) |
| 95 | m = self.StartPatcher(RemoteAccessMock()) |
| 96 | m.AddCmdResult(['cat', '/etc/lsb-release'], |
| 97 | stdout='CHROMEOS_RELEASE_BOARD=board') |
| 98 | m.SetDefaultCmdResult() |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 99 | |
Achuith Bhandarkar | 1f54a21 | 2019-12-09 12:08:35 -0800 | [diff] [blame] | 100 | def _ExistsMock(self, path, ret=True): |
| 101 | """Mock function for os.path.exists. |
| 102 | |
| 103 | os.path.exists is used a lot; we only want to mock it for devserver/static, |
| 104 | and actually check if the file exists in all other cases (using os.access). |
| 105 | |
| 106 | Args: |
| 107 | path: path to check. |
| 108 | ret: return value of mock. |
| 109 | |
| 110 | Returns: |
| 111 | ret for paths under devserver/static, and the expected value of |
| 112 | os.path.exists otherwise. |
| 113 | """ |
Achuith Bhandarkar | eda9b22 | 2020-05-02 10:36:16 +0000 | [diff] [blame^] | 114 | if path.startswith(dev_server_wrapper.DEFAULT_STATIC_DIR): |
Achuith Bhandarkar | 1f54a21 | 2019-12-09 12:08:35 -0800 | [diff] [blame] | 115 | return ret |
| 116 | return os.access(path, os.F_OK) |
| 117 | |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 118 | def testUpdateAll(self): |
| 119 | """Tests that update methods are called correctly.""" |
Achuith Bhandarkar | 1f54a21 | 2019-12-09 12:08:35 -0800 | [diff] [blame] | 120 | with mock.patch('os.path.exists', side_effect=self._ExistsMock): |
Bertrand SIMONNET | b34a98b | 2015-04-22 14:30:04 -0700 | [diff] [blame] | 121 | flash.Flash(self.DEVICE, self.IMAGE) |
| 122 | self.assertTrue(self.updater_mock.patched['UpdateStateful'].called) |
| 123 | self.assertTrue(self.updater_mock.patched['UpdateRootfs'].called) |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 124 | |
| 125 | def testUpdateStateful(self): |
| 126 | """Tests that update methods are called correctly.""" |
Achuith Bhandarkar | 1f54a21 | 2019-12-09 12:08:35 -0800 | [diff] [blame] | 127 | with mock.patch('os.path.exists', side_effect=self._ExistsMock): |
Bertrand SIMONNET | b34a98b | 2015-04-22 14:30:04 -0700 | [diff] [blame] | 128 | flash.Flash(self.DEVICE, self.IMAGE, rootfs_update=False) |
| 129 | self.assertTrue(self.updater_mock.patched['UpdateStateful'].called) |
| 130 | self.assertFalse(self.updater_mock.patched['UpdateRootfs'].called) |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 131 | |
| 132 | def testUpdateRootfs(self): |
| 133 | """Tests that update methods are called correctly.""" |
Achuith Bhandarkar | 1f54a21 | 2019-12-09 12:08:35 -0800 | [diff] [blame] | 134 | with mock.patch('os.path.exists', side_effect=self._ExistsMock): |
Bertrand SIMONNET | b34a98b | 2015-04-22 14:30:04 -0700 | [diff] [blame] | 135 | flash.Flash(self.DEVICE, self.IMAGE, stateful_update=False) |
| 136 | self.assertFalse(self.updater_mock.patched['UpdateStateful'].called) |
| 137 | self.assertTrue(self.updater_mock.patched['UpdateRootfs'].called) |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 138 | |
| 139 | def testMissingPayloads(self): |
| 140 | """Tests we raise FlashError when payloads are missing.""" |
Achuith Bhandarkar | 1f54a21 | 2019-12-09 12:08:35 -0800 | [diff] [blame] | 141 | with mock.patch('os.path.exists', |
| 142 | side_effect=lambda p: self._ExistsMock(p, ret=False)): |
Sanika Kulkarni | a8c4e3a | 2019-09-20 16:47:25 -0700 | [diff] [blame] | 143 | self.assertRaises(auto_updater_transfer.ChromiumOSTransferError, |
| 144 | flash.Flash, self.DEVICE, self.IMAGE) |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 145 | |
Achuith Bhandarkar | 4d4275f | 2019-10-01 17:07:23 +0200 | [diff] [blame] | 146 | def testFullPayload(self): |
| 147 | """Tests that we download full_payload and stateful using xBuddy.""" |
| 148 | with mock.patch.object( |
| 149 | dev_server_wrapper, |
| 150 | 'GetImagePathWithXbuddy', |
| 151 | return_value=('translated/xbuddy/path', |
| 152 | 'resolved/xbuddy/path')) as mock_xbuddy: |
Achuith Bhandarkar | 1f54a21 | 2019-12-09 12:08:35 -0800 | [diff] [blame] | 153 | with mock.patch('os.path.exists', side_effect=self._ExistsMock): |
Achuith Bhandarkar | 4d4275f | 2019-10-01 17:07:23 +0200 | [diff] [blame] | 154 | flash.Flash(self.DEVICE, self.IMAGE) |
| 155 | |
| 156 | # Call to download full_payload and stateful. No other calls. |
| 157 | mock_xbuddy.assert_has_calls( |
Achuith Bhandarkar | eda9b22 | 2020-05-02 10:36:16 +0000 | [diff] [blame^] | 158 | [mock.call('/path/to/image/full_payload', 'board', None, silent=True), |
| 159 | mock.call('/path/to/image/stateful', 'board', None, silent=True)]) |
Achuith Bhandarkar | 4d4275f | 2019-10-01 17:07:23 +0200 | [diff] [blame] | 160 | self.assertEqual(mock_xbuddy.call_count, 2) |
| 161 | |
| 162 | def testTestImage(self): |
| 163 | """Tests that we download the test image when the full payload fails.""" |
| 164 | with mock.patch.object( |
| 165 | dev_server_wrapper, |
| 166 | 'GetImagePathWithXbuddy', |
| 167 | side_effect=(dev_server_wrapper.ImagePathError, |
| 168 | ('translated/xbuddy/path', |
| 169 | 'resolved/xbuddy/path'))) as mock_xbuddy: |
Achuith Bhandarkar | 1f54a21 | 2019-12-09 12:08:35 -0800 | [diff] [blame] | 170 | with mock.patch('os.path.exists', side_effect=self._ExistsMock): |
Achuith Bhandarkar | 4d4275f | 2019-10-01 17:07:23 +0200 | [diff] [blame] | 171 | flash.Flash(self.DEVICE, self.IMAGE) |
| 172 | |
| 173 | # Call to download full_payload and image. No other calls. |
| 174 | mock_xbuddy.assert_has_calls( |
Achuith Bhandarkar | eda9b22 | 2020-05-02 10:36:16 +0000 | [diff] [blame^] | 175 | [mock.call('/path/to/image/full_payload', 'board', None, silent=True), |
| 176 | mock.call('/path/to/image', 'board', None)]) |
Achuith Bhandarkar | 4d4275f | 2019-10-01 17:07:23 +0200 | [diff] [blame] | 177 | self.assertEqual(mock_xbuddy.call_count, 2) |
| 178 | |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 179 | |
| 180 | class USBImagerMock(partial_mock.PartialCmdMock): |
| 181 | """Mock out USBImager.""" |
| 182 | TARGET = 'chromite.cli.flash.USBImager' |
| 183 | ATTRS = ('CopyImageToDevice', 'InstallImageToDevice', |
| 184 | 'ChooseRemovableDevice', 'ListAllRemovableDevices', |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 185 | 'GetRemovableDeviceDescription') |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 186 | VALID_IMAGE = True |
| 187 | |
| 188 | def __init__(self): |
| 189 | partial_mock.PartialCmdMock.__init__(self) |
| 190 | |
| 191 | def CopyImageToDevice(self, _inst, *_args, **_kwargs): |
| 192 | """Mock out CopyImageToDevice.""" |
| 193 | |
| 194 | def InstallImageToDevice(self, _inst, *_args, **_kwargs): |
| 195 | """Mock out InstallImageToDevice.""" |
| 196 | |
| 197 | def ChooseRemovableDevice(self, _inst, *_args, **_kwargs): |
| 198 | """Mock out ChooseRemovableDevice.""" |
| 199 | |
| 200 | def ListAllRemovableDevices(self, _inst, *_args, **_kwargs): |
| 201 | """Mock out ListAllRemovableDevices.""" |
| 202 | return ['foo', 'taco', 'milk'] |
| 203 | |
| 204 | def GetRemovableDeviceDescription(self, _inst, *_args, **_kwargs): |
| 205 | """Mock out GetRemovableDeviceDescription.""" |
| 206 | |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 207 | |
| 208 | class USBImagerTest(cros_test_lib.MockTempDirTestCase): |
| 209 | """Test the flow of flash.Flash() with USBImager.""" |
| 210 | IMAGE = '/path/to/image' |
| 211 | |
| 212 | def Device(self, path): |
| 213 | """Create a USB device for passing to flash.Flash().""" |
| 214 | return commandline.Device(scheme=commandline.DEVICE_SCHEME_USB, |
| 215 | path=path) |
| 216 | |
| 217 | def setUp(self): |
| 218 | """Patches objects.""" |
| 219 | self.usb_mock = USBImagerMock() |
| 220 | self.imager_mock = self.StartPatcher(self.usb_mock) |
| 221 | self.PatchObject(dev_server_wrapper, 'GenerateXbuddyRequest', |
| 222 | return_value='xbuddy/local/latest') |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 223 | self.PatchObject(dev_server_wrapper, 'GetImagePathWithXbuddy', |
Gilad Arnold | e62ec90 | 2015-04-24 14:41:02 -0700 | [diff] [blame] | 224 | return_value=('taco-paladin/R36/chromiumos_test_image.bin', |
| 225 | 'remote/taco-paladin/R36/test')) |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 226 | self.PatchObject(os.path, 'exists', return_value=True) |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 227 | self.isgpt_mock = self.PatchObject(flash, '_IsFilePathGPTDiskImage', |
| 228 | return_value=True) |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 229 | |
| 230 | def testLocalImagePathCopy(self): |
| 231 | """Tests that imaging methods are called correctly.""" |
| 232 | with mock.patch('os.path.isfile', return_value=True): |
| 233 | flash.Flash(self.Device('/dev/foo'), self.IMAGE) |
| 234 | self.assertTrue(self.imager_mock.patched['CopyImageToDevice'].called) |
| 235 | |
| 236 | def testLocalImagePathInstall(self): |
| 237 | """Tests that imaging methods are called correctly.""" |
| 238 | with mock.patch('os.path.isfile', return_value=True): |
| 239 | flash.Flash(self.Device('/dev/foo'), self.IMAGE, board='taco', |
| 240 | install=True) |
| 241 | self.assertTrue(self.imager_mock.patched['InstallImageToDevice'].called) |
| 242 | |
| 243 | def testLocalBadImagePath(self): |
| 244 | """Tests that using an image not having the magic bytes has prompt.""" |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 245 | self.isgpt_mock.return_value = False |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 246 | with mock.patch('os.path.isfile', return_value=True): |
| 247 | with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt: |
| 248 | mock_prompt.return_value = False |
| 249 | flash.Flash(self.Device('/dev/foo'), self.IMAGE) |
| 250 | self.assertTrue(mock_prompt.called) |
| 251 | |
| 252 | def testNonLocalImagePath(self): |
| 253 | """Tests that we try to get the image path using xbuddy.""" |
| 254 | with mock.patch.object( |
| 255 | dev_server_wrapper, |
| 256 | 'GetImagePathWithXbuddy', |
Gilad Arnold | e62ec90 | 2015-04-24 14:41:02 -0700 | [diff] [blame] | 257 | return_value=('translated/xbuddy/path', |
| 258 | 'resolved/xbuddy/path')) as mock_xbuddy: |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 259 | with mock.patch('os.path.isfile', return_value=False): |
| 260 | with mock.patch('os.path.isdir', return_value=False): |
| 261 | flash.Flash(self.Device('/dev/foo'), self.IMAGE) |
| 262 | self.assertTrue(mock_xbuddy.called) |
| 263 | |
| 264 | def testConfirmNonRemovableDevice(self): |
| 265 | """Tests that we ask user to confirm if the device is not removable.""" |
| 266 | with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt: |
| 267 | flash.Flash(self.Device('/dev/dummy'), self.IMAGE) |
| 268 | self.assertTrue(mock_prompt.called) |
| 269 | |
| 270 | def testSkipPromptNonRemovableDevice(self): |
| 271 | """Tests that we skip the prompt for non-removable with --yes.""" |
| 272 | with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt: |
| 273 | flash.Flash(self.Device('/dev/dummy'), self.IMAGE, yes=True) |
| 274 | self.assertFalse(mock_prompt.called) |
| 275 | |
| 276 | def testChooseRemovableDevice(self): |
| 277 | """Tests that we ask user to choose a device if none is given.""" |
| 278 | flash.Flash(self.Device(''), self.IMAGE) |
| 279 | self.assertTrue(self.imager_mock.patched['ChooseRemovableDevice'].called) |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 280 | |
| 281 | |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 282 | class UsbImagerOperationTest(cros_test_lib.RunCommandTestCase): |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 283 | """Tests for flash.UsbImagerOperation.""" |
| 284 | # pylint: disable=protected-access |
| 285 | |
| 286 | def setUp(self): |
| 287 | self.PatchObject(flash.UsbImagerOperation, '__init__', return_value=None) |
| 288 | |
| 289 | def testUsbImagerOperationCalled(self): |
| 290 | """Test that flash.UsbImagerOperation is called when log level <= NOTICE.""" |
| 291 | expected_cmd = ['dd', 'if=foo', 'of=bar', 'bs=4M', 'iflag=fullblock', |
Frank Huang | 8e62643 | 2019-06-24 19:51:08 +0800 | [diff] [blame] | 292 | 'oflag=direct', 'conv=fdatasync'] |
Achuith Bhandarkar | ee1336f | 2020-04-18 11:44:09 +0000 | [diff] [blame] | 293 | usb_imager = flash.USBImager('dummy_device', 'board', 'foo', 'latest') |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 294 | run_mock = self.PatchObject(flash.UsbImagerOperation, 'Run') |
| 295 | self.PatchObject(logging.Logger, 'getEffectiveLevel', |
| 296 | return_value=logging.NOTICE) |
| 297 | usb_imager.CopyImageToDevice('foo', 'bar') |
| 298 | |
| 299 | # Check that flash.UsbImagerOperation.Run() is called correctly. |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 300 | run_mock.assert_called_with(cros_build_lib.sudo_run, expected_cmd, |
Mike Frysinger | 3d5de8f | 2019-10-23 00:48:39 -0400 | [diff] [blame] | 301 | debug_level=logging.NOTICE, encoding='utf-8', |
| 302 | update_period=0.5) |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 303 | |
| 304 | def testSudoRunCommandCalled(self): |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 305 | """Test that sudo_run is called when log level > NOTICE.""" |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 306 | expected_cmd = ['dd', 'if=foo', 'of=bar', 'bs=4M', 'iflag=fullblock', |
Frank Huang | 8e62643 | 2019-06-24 19:51:08 +0800 | [diff] [blame] | 307 | 'oflag=direct', 'conv=fdatasync'] |
Achuith Bhandarkar | ee1336f | 2020-04-18 11:44:09 +0000 | [diff] [blame] | 308 | usb_imager = flash.USBImager('dummy_device', 'board', 'foo', 'latest') |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 309 | run_mock = self.PatchObject(cros_build_lib, 'sudo_run') |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 310 | self.PatchObject(logging.Logger, 'getEffectiveLevel', |
| 311 | return_value=logging.WARNING) |
| 312 | usb_imager.CopyImageToDevice('foo', 'bar') |
| 313 | |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 314 | # Check that sudo_run() is called correctly. |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 315 | run_mock.assert_any_call(expected_cmd, debug_level=logging.NOTICE, |
| 316 | print_cmd=False) |
| 317 | |
| 318 | def testPingDD(self): |
| 319 | """Test that UsbImagerOperation._PingDD() sends the correct signal.""" |
| 320 | expected_cmd = ['kill', '-USR1', '5'] |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 321 | run_mock = self.PatchObject(cros_build_lib, 'sudo_run') |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 322 | op = flash.UsbImagerOperation('foo') |
| 323 | op._PingDD(5) |
| 324 | |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 325 | # Check that sudo_run was called correctly. |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 326 | run_mock.assert_called_with(expected_cmd, print_cmd=False) |
| 327 | |
| 328 | def testGetDDPidFound(self): |
| 329 | """Check that the expected pid is returned for _GetDDPid().""" |
| 330 | expected_pid = 5 |
| 331 | op = flash.UsbImagerOperation('foo') |
| 332 | self.PatchObject(osutils, 'IsChildProcess', return_value=True) |
| 333 | self.rc.AddCmdResult(partial_mock.Ignore(), |
| 334 | output='%d\n10\n' % expected_pid) |
| 335 | |
| 336 | pid = op._GetDDPid() |
| 337 | |
| 338 | # Check that the correct pid was returned. |
| 339 | self.assertEqual(pid, expected_pid) |
| 340 | |
| 341 | def testGetDDPidNotFound(self): |
| 342 | """Check that -1 is returned for _GetDDPid() if the pids aren't valid.""" |
| 343 | expected_pid = -1 |
| 344 | op = flash.UsbImagerOperation('foo') |
| 345 | self.PatchObject(osutils, 'IsChildProcess', return_value=False) |
| 346 | self.rc.AddCmdResult(partial_mock.Ignore(), output='5\n10\n') |
| 347 | |
| 348 | pid = op._GetDDPid() |
| 349 | |
| 350 | # Check that the correct pid was returned. |
| 351 | self.assertEqual(pid, expected_pid) |
| 352 | |
| 353 | |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 354 | class FlashUtilTest(cros_test_lib.MockTempDirTestCase): |
| 355 | """Tests the helpers from cli.flash.""" |
| 356 | |
| 357 | def testChooseImage(self): |
| 358 | """Tests that we can detect a GPT image.""" |
| 359 | # pylint: disable=protected-access |
| 360 | |
| 361 | with self.PatchObject(flash, '_IsFilePathGPTDiskImage', return_value=True): |
| 362 | # No images defined. Choosing the image should raise an error. |
| 363 | with self.assertRaises(ValueError): |
| 364 | flash._ChooseImageFromDirectory(self.tempdir) |
| 365 | |
| 366 | file_a = os.path.join(self.tempdir, 'a') |
| 367 | osutils.Touch(file_a) |
| 368 | # Only one image available, it should be selected automatically. |
| 369 | self.assertEqual(file_a, flash._ChooseImageFromDirectory(self.tempdir)) |
| 370 | |
| 371 | osutils.Touch(os.path.join(self.tempdir, 'b')) |
| 372 | file_c = os.path.join(self.tempdir, 'c') |
| 373 | osutils.Touch(file_c) |
| 374 | osutils.Touch(os.path.join(self.tempdir, 'd')) |
| 375 | |
| 376 | # Multiple images available, we should ask the user to select the right |
| 377 | # image. |
| 378 | with self.PatchObject(cros_build_lib, 'GetChoice', return_value=2): |
| 379 | self.assertEqual(file_c, flash._ChooseImageFromDirectory(self.tempdir)) |
Mike Frysinger | 32759e4 | 2016-12-21 18:40:16 -0500 | [diff] [blame] | 380 | |
| 381 | def testIsFilePathGPTDiskImage(self): |
| 382 | """Tests the GPT image probing.""" |
| 383 | # pylint: disable=protected-access |
| 384 | |
Mike Frysinger | 3d5de8f | 2019-10-23 00:48:39 -0400 | [diff] [blame] | 385 | INVALID_PMBR = b' ' * 0x200 |
| 386 | INVALID_GPT = b' ' * 0x200 |
| 387 | VALID_PMBR = (b' ' * 0x1fe) + b'\x55\xaa' |
| 388 | VALID_GPT = b'EFI PART' + (b' ' * 0x1f8) |
Mike Frysinger | 32759e4 | 2016-12-21 18:40:16 -0500 | [diff] [blame] | 389 | TESTCASES = ( |
| 390 | (False, False, INVALID_PMBR + INVALID_GPT), |
| 391 | (False, False, VALID_PMBR + INVALID_GPT), |
| 392 | (False, True, INVALID_PMBR + VALID_GPT), |
| 393 | (True, True, VALID_PMBR + VALID_GPT), |
| 394 | ) |
| 395 | |
| 396 | img = os.path.join(self.tempdir, 'img.bin') |
| 397 | for exp_pmbr_t, exp_pmbr_f, data in TESTCASES: |
Mike Frysinger | 3d5de8f | 2019-10-23 00:48:39 -0400 | [diff] [blame] | 398 | osutils.WriteFile(img, data, mode='wb') |
Mike Frysinger | 32759e4 | 2016-12-21 18:40:16 -0500 | [diff] [blame] | 399 | self.assertEqual( |
| 400 | flash._IsFilePathGPTDiskImage(img, require_pmbr=True), exp_pmbr_t) |
| 401 | self.assertEqual( |
| 402 | flash._IsFilePathGPTDiskImage(img, require_pmbr=False), exp_pmbr_f) |