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