"""This module tests the cros flash command."""
+from __future__ import print_function
+
import mock
import os
import sys
def testUpdateAll(self):
"""Tests that update methods are called correctly."""
self.SetupCommandMock([self.DEVICE, self.IMAGE])
- with mock.patch('os.path.exists', return_value=True) as _m:
+ with mock.patch('os.path.exists', return_value=True):
self.cmd_mock.inst.Run()
self.assertTrue(self.updater_mock.patched['UpdateStateful'].called)
self.assertTrue(self.updater_mock.patched['UpdateRootfs'].called)
def testUpdateStateful(self):
"""Tests that update methods are called correctly."""
self.SetupCommandMock(['--no-rootfs-update', self.DEVICE, self.IMAGE])
- with mock.patch('os.path.exists', return_value=True) as _m:
+ with mock.patch('os.path.exists', return_value=True):
self.cmd_mock.inst.Run()
self.assertTrue(self.updater_mock.patched['UpdateStateful'].called)
self.assertFalse(self.updater_mock.patched['UpdateRootfs'].called)
def testUpdateRootfs(self):
"""Tests that update methods are called correctly."""
self.SetupCommandMock(['--no-stateful-update', self.DEVICE, self.IMAGE])
- with mock.patch('os.path.exists', return_value=True) as _m:
+ with mock.patch('os.path.exists', return_value=True):
self.cmd_mock.inst.Run()
self.assertFalse(self.updater_mock.patched['UpdateStateful'].called)
self.assertTrue(self.updater_mock.patched['UpdateRootfs'].called)
def testMissingPayloads(self):
"""Tests we exit when payloads are missing."""
self.SetupCommandMock([self.DEVICE, self.IMAGE])
- with mock.patch('os.path.exists', return_value=False) as _m1:
+ with mock.patch('os.path.exists', return_value=False):
self.assertRaises(cros_build_lib.DieSystemExit, self.cmd_mock.inst.Run)
"""Mock out USBImager."""
TARGET = 'chromite.cros.commands.cros_flash.USBImager'
ATTRS = ('GetImagePathFromDevserver', 'CopyImageToDevice',
- 'ChooseRemovableDevice', 'ListAllRemovableDevices',
- 'GetRemovableDeviceDescription')
+ 'InstallImageToDevice', 'ChooseRemovableDevice',
+ 'ListAllRemovableDevices', 'GetRemovableDeviceDescription',
+ 'IsFilePathGPTDiskImage')
+ VALID_IMAGE = True
def __init__(self):
partial_mock.PartialCmdMock.__init__(self)
def CopyImageToDevice(self, _inst, *_args, **_kwargs):
"""Mock out CopyImageToDevice."""
+ def InstallImageToDevice(self, _inst, *_args, **_kwargs):
+ """Mock out InstallImageToDevice."""
+
def ChooseRemovableDevice(self, _inst, *_args, **_kwargs):
"""Mock out ChooseRemovableDevice."""
def GetRemovableDeviceDescription(self, _inst, *_args, **_kwargs):
"""Mock out GetRemovableDeviceDescription."""
+ def IsFilePathGPTDiskImage(self, _inst, *_args, **_kwargs):
+ """Mock out IsFilePathGPTDiskImage."""
+ return self.VALID_IMAGE
+
class ImagingRunThroughTest(cros_test_lib.MockTempDirTestCase,
cros_test_lib.LoggingTestCase):
def setUp(self):
"""Patches objects."""
self.cmd_mock = None
- self.imager_mock = self.StartPatcher(USBImagerMock())
+ self.usb_mock = USBImagerMock()
+ self.imager_mock = self.StartPatcher(self.usb_mock)
self.PatchObject(cros_flash, 'GenerateXbuddyRequest',
return_value='xbuddy/local/latest')
self.PatchObject(dev_server_wrapper, 'DevServerWrapper')
return_value='taco-paladin/R36/chromiumos_test_image.bin')
self.PatchObject(os.path, 'exists', return_value=True)
- def testLocalImagePath(self):
+ def testLocalImagePathCopy(self):
"""Tests that imaging methods are called correctly."""
self.SetupCommandMock(['usb:///dev/foo', self.IMAGE])
- with mock.patch('os.path.isfile', return_value=True) as _m:
+ with mock.patch('os.path.isfile', return_value=True):
self.cmd_mock.inst.Run()
self.assertTrue(self.imager_mock.patched['CopyImageToDevice'].called)
+ def testLocalImagePathInstall(self):
+ """Tests that imaging methods are called correctly."""
+ self.SetupCommandMock(['--board=taco', '--install', 'usb:///dev/foo',
+ self.IMAGE])
+ with mock.patch('os.path.isfile', return_value=True):
+ self.cmd_mock.inst.Run()
+ self.assertTrue(self.imager_mock.patched['InstallImageToDevice'].called)
+
+ def testLocalBadImagePath(self):
+ """Tests that using an image not having the magic bytes has prompt."""
+ self.usb_mock.VALID_IMAGE = False
+ self.SetupCommandMock(['usb:///dev/foo', self.IMAGE])
+ with mock.patch('os.path.isfile', return_value=True):
+ with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt:
+ mock_prompt.return_value = False
+ self.cmd_mock.inst.Run()
+ self.assertTrue(mock_prompt.called)
+
def testNonLocalImgePath(self):
"""Tests that we try to get the image path from devserver."""
self.SetupCommandMock(['usb:///dev/foo', self.IMAGE])
- with mock.patch('os.path.isfile', return_value=False) as _m1:
- with mock.patch('os.path.isdir', return_value=False) as _m2:
+ with mock.patch('os.path.isfile', return_value=False):
+ with mock.patch('os.path.isdir', return_value=False):
self.cmd_mock.inst.Run()
self.assertTrue(
self.imager_mock.patched['GetImagePathFromDevserver'].called)