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 | """ |
| 114 | if path.startswith(flash.DEVSERVER_STATIC_DIR): |
| 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 | ee1336f | 2020-04-18 11:44:09 +0000 | [diff] [blame^] | 158 | [mock.call('/path/to/image/full_payload', mock.ANY, mock.ANY, |
Achuith Bhandarkar | 12f43c7 | 2019-11-21 16:44:24 -0800 | [diff] [blame] | 159 | static_dir=flash.DEVSERVER_STATIC_DIR, silent=True), |
Achuith Bhandarkar | ee1336f | 2020-04-18 11:44:09 +0000 | [diff] [blame^] | 160 | mock.call('/path/to/image/stateful', mock.ANY, mock.ANY, |
Achuith Bhandarkar | 12f43c7 | 2019-11-21 16:44:24 -0800 | [diff] [blame] | 161 | static_dir=flash.DEVSERVER_STATIC_DIR, silent=True)]) |
Achuith Bhandarkar | 4d4275f | 2019-10-01 17:07:23 +0200 | [diff] [blame] | 162 | self.assertEqual(mock_xbuddy.call_count, 2) |
| 163 | |
| 164 | def testTestImage(self): |
| 165 | """Tests that we download the test image when the full payload fails.""" |
| 166 | with mock.patch.object( |
| 167 | dev_server_wrapper, |
| 168 | 'GetImagePathWithXbuddy', |
| 169 | side_effect=(dev_server_wrapper.ImagePathError, |
| 170 | ('translated/xbuddy/path', |
| 171 | 'resolved/xbuddy/path'))) as mock_xbuddy: |
Achuith Bhandarkar | 1f54a21 | 2019-12-09 12:08:35 -0800 | [diff] [blame] | 172 | with mock.patch('os.path.exists', side_effect=self._ExistsMock): |
Achuith Bhandarkar | 4d4275f | 2019-10-01 17:07:23 +0200 | [diff] [blame] | 173 | flash.Flash(self.DEVICE, self.IMAGE) |
| 174 | |
| 175 | # Call to download full_payload and image. No other calls. |
| 176 | mock_xbuddy.assert_has_calls( |
Achuith Bhandarkar | ee1336f | 2020-04-18 11:44:09 +0000 | [diff] [blame^] | 177 | [mock.call('/path/to/image/full_payload', mock.ANY, mock.ANY, |
Achuith Bhandarkar | 12f43c7 | 2019-11-21 16:44:24 -0800 | [diff] [blame] | 178 | static_dir=flash.DEVSERVER_STATIC_DIR, silent=True), |
Achuith Bhandarkar | ee1336f | 2020-04-18 11:44:09 +0000 | [diff] [blame^] | 179 | mock.call('/path/to/image', mock.ANY, mock.ANY, |
Achuith Bhandarkar | 4d4275f | 2019-10-01 17:07:23 +0200 | [diff] [blame] | 180 | static_dir=flash.DEVSERVER_STATIC_DIR)]) |
| 181 | self.assertEqual(mock_xbuddy.call_count, 2) |
| 182 | |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 183 | |
| 184 | class USBImagerMock(partial_mock.PartialCmdMock): |
| 185 | """Mock out USBImager.""" |
| 186 | TARGET = 'chromite.cli.flash.USBImager' |
| 187 | ATTRS = ('CopyImageToDevice', 'InstallImageToDevice', |
| 188 | 'ChooseRemovableDevice', 'ListAllRemovableDevices', |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 189 | 'GetRemovableDeviceDescription') |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 190 | VALID_IMAGE = True |
| 191 | |
| 192 | def __init__(self): |
| 193 | partial_mock.PartialCmdMock.__init__(self) |
| 194 | |
| 195 | def CopyImageToDevice(self, _inst, *_args, **_kwargs): |
| 196 | """Mock out CopyImageToDevice.""" |
| 197 | |
| 198 | def InstallImageToDevice(self, _inst, *_args, **_kwargs): |
| 199 | """Mock out InstallImageToDevice.""" |
| 200 | |
| 201 | def ChooseRemovableDevice(self, _inst, *_args, **_kwargs): |
| 202 | """Mock out ChooseRemovableDevice.""" |
| 203 | |
| 204 | def ListAllRemovableDevices(self, _inst, *_args, **_kwargs): |
| 205 | """Mock out ListAllRemovableDevices.""" |
| 206 | return ['foo', 'taco', 'milk'] |
| 207 | |
| 208 | def GetRemovableDeviceDescription(self, _inst, *_args, **_kwargs): |
| 209 | """Mock out GetRemovableDeviceDescription.""" |
| 210 | |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 211 | |
| 212 | class USBImagerTest(cros_test_lib.MockTempDirTestCase): |
| 213 | """Test the flow of flash.Flash() with USBImager.""" |
| 214 | IMAGE = '/path/to/image' |
| 215 | |
| 216 | def Device(self, path): |
| 217 | """Create a USB device for passing to flash.Flash().""" |
| 218 | return commandline.Device(scheme=commandline.DEVICE_SCHEME_USB, |
| 219 | path=path) |
| 220 | |
| 221 | def setUp(self): |
| 222 | """Patches objects.""" |
| 223 | self.usb_mock = USBImagerMock() |
| 224 | self.imager_mock = self.StartPatcher(self.usb_mock) |
| 225 | self.PatchObject(dev_server_wrapper, 'GenerateXbuddyRequest', |
| 226 | return_value='xbuddy/local/latest') |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 227 | self.PatchObject(dev_server_wrapper, 'GetImagePathWithXbuddy', |
Gilad Arnold | e62ec90 | 2015-04-24 14:41:02 -0700 | [diff] [blame] | 228 | return_value=('taco-paladin/R36/chromiumos_test_image.bin', |
| 229 | 'remote/taco-paladin/R36/test')) |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 230 | self.PatchObject(os.path, 'exists', return_value=True) |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 231 | self.isgpt_mock = self.PatchObject(flash, '_IsFilePathGPTDiskImage', |
| 232 | return_value=True) |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 233 | |
| 234 | def testLocalImagePathCopy(self): |
| 235 | """Tests that imaging methods are called correctly.""" |
| 236 | with mock.patch('os.path.isfile', return_value=True): |
| 237 | flash.Flash(self.Device('/dev/foo'), self.IMAGE) |
| 238 | self.assertTrue(self.imager_mock.patched['CopyImageToDevice'].called) |
| 239 | |
| 240 | def testLocalImagePathInstall(self): |
| 241 | """Tests that imaging methods are called correctly.""" |
| 242 | with mock.patch('os.path.isfile', return_value=True): |
| 243 | flash.Flash(self.Device('/dev/foo'), self.IMAGE, board='taco', |
| 244 | install=True) |
| 245 | self.assertTrue(self.imager_mock.patched['InstallImageToDevice'].called) |
| 246 | |
| 247 | def testLocalBadImagePath(self): |
| 248 | """Tests that using an image not having the magic bytes has prompt.""" |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 249 | self.isgpt_mock.return_value = False |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 250 | with mock.patch('os.path.isfile', return_value=True): |
| 251 | with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt: |
| 252 | mock_prompt.return_value = False |
| 253 | flash.Flash(self.Device('/dev/foo'), self.IMAGE) |
| 254 | self.assertTrue(mock_prompt.called) |
| 255 | |
| 256 | def testNonLocalImagePath(self): |
| 257 | """Tests that we try to get the image path using xbuddy.""" |
| 258 | with mock.patch.object( |
| 259 | dev_server_wrapper, |
| 260 | 'GetImagePathWithXbuddy', |
Gilad Arnold | e62ec90 | 2015-04-24 14:41:02 -0700 | [diff] [blame] | 261 | return_value=('translated/xbuddy/path', |
| 262 | 'resolved/xbuddy/path')) as mock_xbuddy: |
David Pursell | f1d16a6 | 2015-03-25 13:31:04 -0700 | [diff] [blame] | 263 | with mock.patch('os.path.isfile', return_value=False): |
| 264 | with mock.patch('os.path.isdir', return_value=False): |
| 265 | flash.Flash(self.Device('/dev/foo'), self.IMAGE) |
| 266 | self.assertTrue(mock_xbuddy.called) |
| 267 | |
| 268 | def testConfirmNonRemovableDevice(self): |
| 269 | """Tests that we ask user to confirm if the device is not removable.""" |
| 270 | with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt: |
| 271 | flash.Flash(self.Device('/dev/dummy'), self.IMAGE) |
| 272 | self.assertTrue(mock_prompt.called) |
| 273 | |
| 274 | def testSkipPromptNonRemovableDevice(self): |
| 275 | """Tests that we skip the prompt for non-removable with --yes.""" |
| 276 | with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt: |
| 277 | flash.Flash(self.Device('/dev/dummy'), self.IMAGE, yes=True) |
| 278 | self.assertFalse(mock_prompt.called) |
| 279 | |
| 280 | def testChooseRemovableDevice(self): |
| 281 | """Tests that we ask user to choose a device if none is given.""" |
| 282 | flash.Flash(self.Device(''), self.IMAGE) |
| 283 | self.assertTrue(self.imager_mock.patched['ChooseRemovableDevice'].called) |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 284 | |
| 285 | |
Benjamin Gordon | 121a2aa | 2018-05-04 16:24:45 -0600 | [diff] [blame] | 286 | class UsbImagerOperationTest(cros_test_lib.RunCommandTestCase): |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 287 | """Tests for flash.UsbImagerOperation.""" |
| 288 | # pylint: disable=protected-access |
| 289 | |
| 290 | def setUp(self): |
| 291 | self.PatchObject(flash.UsbImagerOperation, '__init__', return_value=None) |
| 292 | |
| 293 | def testUsbImagerOperationCalled(self): |
| 294 | """Test that flash.UsbImagerOperation is called when log level <= NOTICE.""" |
| 295 | expected_cmd = ['dd', 'if=foo', 'of=bar', 'bs=4M', 'iflag=fullblock', |
Frank Huang | 8e62643 | 2019-06-24 19:51:08 +0800 | [diff] [blame] | 296 | 'oflag=direct', 'conv=fdatasync'] |
Achuith Bhandarkar | ee1336f | 2020-04-18 11:44:09 +0000 | [diff] [blame^] | 297 | usb_imager = flash.USBImager('dummy_device', 'board', 'foo', 'latest') |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 298 | run_mock = self.PatchObject(flash.UsbImagerOperation, 'Run') |
| 299 | self.PatchObject(logging.Logger, 'getEffectiveLevel', |
| 300 | return_value=logging.NOTICE) |
| 301 | usb_imager.CopyImageToDevice('foo', 'bar') |
| 302 | |
| 303 | # Check that flash.UsbImagerOperation.Run() is called correctly. |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 304 | run_mock.assert_called_with(cros_build_lib.sudo_run, expected_cmd, |
Mike Frysinger | 3d5de8f | 2019-10-23 00:48:39 -0400 | [diff] [blame] | 305 | debug_level=logging.NOTICE, encoding='utf-8', |
| 306 | update_period=0.5) |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 307 | |
| 308 | def testSudoRunCommandCalled(self): |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 309 | """Test that sudo_run is called when log level > NOTICE.""" |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 310 | expected_cmd = ['dd', 'if=foo', 'of=bar', 'bs=4M', 'iflag=fullblock', |
Frank Huang | 8e62643 | 2019-06-24 19:51:08 +0800 | [diff] [blame] | 311 | 'oflag=direct', 'conv=fdatasync'] |
Achuith Bhandarkar | ee1336f | 2020-04-18 11:44:09 +0000 | [diff] [blame^] | 312 | usb_imager = flash.USBImager('dummy_device', 'board', 'foo', 'latest') |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 313 | run_mock = self.PatchObject(cros_build_lib, 'sudo_run') |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 314 | self.PatchObject(logging.Logger, 'getEffectiveLevel', |
| 315 | return_value=logging.WARNING) |
| 316 | usb_imager.CopyImageToDevice('foo', 'bar') |
| 317 | |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 318 | # Check that sudo_run() is called correctly. |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 319 | run_mock.assert_any_call(expected_cmd, debug_level=logging.NOTICE, |
| 320 | print_cmd=False) |
| 321 | |
| 322 | def testPingDD(self): |
| 323 | """Test that UsbImagerOperation._PingDD() sends the correct signal.""" |
| 324 | expected_cmd = ['kill', '-USR1', '5'] |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 325 | run_mock = self.PatchObject(cros_build_lib, 'sudo_run') |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 326 | op = flash.UsbImagerOperation('foo') |
| 327 | op._PingDD(5) |
| 328 | |
Mike Frysinger | 45602c7 | 2019-09-22 02:15:11 -0400 | [diff] [blame] | 329 | # Check that sudo_run was called correctly. |
Ralph Nathan | 9b99723 | 2015-05-15 13:13:12 -0700 | [diff] [blame] | 330 | run_mock.assert_called_with(expected_cmd, print_cmd=False) |
| 331 | |
| 332 | def testGetDDPidFound(self): |
| 333 | """Check that the expected pid is returned for _GetDDPid().""" |
| 334 | expected_pid = 5 |
| 335 | op = flash.UsbImagerOperation('foo') |
| 336 | self.PatchObject(osutils, 'IsChildProcess', return_value=True) |
| 337 | self.rc.AddCmdResult(partial_mock.Ignore(), |
| 338 | output='%d\n10\n' % expected_pid) |
| 339 | |
| 340 | pid = op._GetDDPid() |
| 341 | |
| 342 | # Check that the correct pid was returned. |
| 343 | self.assertEqual(pid, expected_pid) |
| 344 | |
| 345 | def testGetDDPidNotFound(self): |
| 346 | """Check that -1 is returned for _GetDDPid() if the pids aren't valid.""" |
| 347 | expected_pid = -1 |
| 348 | op = flash.UsbImagerOperation('foo') |
| 349 | self.PatchObject(osutils, 'IsChildProcess', return_value=False) |
| 350 | self.rc.AddCmdResult(partial_mock.Ignore(), output='5\n10\n') |
| 351 | |
| 352 | pid = op._GetDDPid() |
| 353 | |
| 354 | # Check that the correct pid was returned. |
| 355 | self.assertEqual(pid, expected_pid) |
| 356 | |
| 357 | |
Bertrand SIMONNET | 56f773d | 2015-05-04 14:02:39 -0700 | [diff] [blame] | 358 | class FlashUtilTest(cros_test_lib.MockTempDirTestCase): |
| 359 | """Tests the helpers from cli.flash.""" |
| 360 | |
| 361 | def testChooseImage(self): |
| 362 | """Tests that we can detect a GPT image.""" |
| 363 | # pylint: disable=protected-access |
| 364 | |
| 365 | with self.PatchObject(flash, '_IsFilePathGPTDiskImage', return_value=True): |
| 366 | # No images defined. Choosing the image should raise an error. |
| 367 | with self.assertRaises(ValueError): |
| 368 | flash._ChooseImageFromDirectory(self.tempdir) |
| 369 | |
| 370 | file_a = os.path.join(self.tempdir, 'a') |
| 371 | osutils.Touch(file_a) |
| 372 | # Only one image available, it should be selected automatically. |
| 373 | self.assertEqual(file_a, flash._ChooseImageFromDirectory(self.tempdir)) |
| 374 | |
| 375 | osutils.Touch(os.path.join(self.tempdir, 'b')) |
| 376 | file_c = os.path.join(self.tempdir, 'c') |
| 377 | osutils.Touch(file_c) |
| 378 | osutils.Touch(os.path.join(self.tempdir, 'd')) |
| 379 | |
| 380 | # Multiple images available, we should ask the user to select the right |
| 381 | # image. |
| 382 | with self.PatchObject(cros_build_lib, 'GetChoice', return_value=2): |
| 383 | self.assertEqual(file_c, flash._ChooseImageFromDirectory(self.tempdir)) |
Mike Frysinger | 32759e4 | 2016-12-21 18:40:16 -0500 | [diff] [blame] | 384 | |
| 385 | def testIsFilePathGPTDiskImage(self): |
| 386 | """Tests the GPT image probing.""" |
| 387 | # pylint: disable=protected-access |
| 388 | |
Mike Frysinger | 3d5de8f | 2019-10-23 00:48:39 -0400 | [diff] [blame] | 389 | INVALID_PMBR = b' ' * 0x200 |
| 390 | INVALID_GPT = b' ' * 0x200 |
| 391 | VALID_PMBR = (b' ' * 0x1fe) + b'\x55\xaa' |
| 392 | VALID_GPT = b'EFI PART' + (b' ' * 0x1f8) |
Mike Frysinger | 32759e4 | 2016-12-21 18:40:16 -0500 | [diff] [blame] | 393 | TESTCASES = ( |
| 394 | (False, False, INVALID_PMBR + INVALID_GPT), |
| 395 | (False, False, VALID_PMBR + INVALID_GPT), |
| 396 | (False, True, INVALID_PMBR + VALID_GPT), |
| 397 | (True, True, VALID_PMBR + VALID_GPT), |
| 398 | ) |
| 399 | |
| 400 | img = os.path.join(self.tempdir, 'img.bin') |
| 401 | for exp_pmbr_t, exp_pmbr_f, data in TESTCASES: |
Mike Frysinger | 3d5de8f | 2019-10-23 00:48:39 -0400 | [diff] [blame] | 402 | osutils.WriteFile(img, data, mode='wb') |
Mike Frysinger | 32759e4 | 2016-12-21 18:40:16 -0500 | [diff] [blame] | 403 | self.assertEqual( |
| 404 | flash._IsFilePathGPTDiskImage(img, require_pmbr=True), exp_pmbr_t) |
| 405 | self.assertEqual( |
| 406 | flash._IsFilePathGPTDiskImage(img, require_pmbr=False), exp_pmbr_f) |