update blockdev exec and state module unit tests

This commit is contained in:
Justin Findlay 2015-10-23 15:36:45 -06:00
parent 07253cb5fb
commit 0dc72135de
3 changed files with 60 additions and 13 deletions

View file

@ -230,6 +230,7 @@ def fstype(device):
return ''
@decorators.which('resize2fs')
def resize2fs(device):
'''
Resizes the filesystem.

View file

@ -21,37 +21,79 @@ blockdev.__salt__ = {
@skipIf(NO_MOCK, NO_MOCK_REASON)
class TestBlockdevModule(TestCase):
def test_dump(self):
device = '/dev/sdX'
cmd = ['blockdev',
'--getro',
'--getsz',
'--getss',
'--getpbsz',
'--getiomin',
'--getioopt',
'--getalignoff',
'--getmaxsect',
'--getsize',
'--getsize64',
'--getra',
'--getfra',
device]
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(blockdev.__salt__, {'cmd.run_all': mock}):
blockdev.dump('/dev/sda')
mock.assert_called_once_with(
'blockdev --getro --getsz --getss --getpbsz --getiomin '
'--getioopt --getalignoff --getmaxsect --getsize '
'--getsize64 --getra --getfra /dev/sda',
python_shell=False
)
blockdev.dump(device)
mock.assert_called_once_with(cmd, python_shell=False)
def test_wipe(self):
device = '/dev/sdX'
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(blockdev.__salt__, {'cmd.run_all': mock}):
blockdev.wipe('/dev/sda')
blockdev.wipe(device)
mock.assert_called_once_with(
'wipefs /dev/sda',
'wipefs {0}'.format(device),
python_shell=False
)
def test_tune(self):
device = '/dev/sdX'
mock = MagicMock(return_value='712971264\n512\n512\n512\n0\n0\n88\n712971264\n365041287168\n512\n512')
with patch.dict(blockdev.__salt__, {'cmd.run': mock}):
mock_dump = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch('salt.modules.blockdev.dump', mock_dump):
kwargs = {'read-ahead': 512, 'filesystem-read-ahead': 512}
blockdev.tune('/dev/sda', **kwargs)
blockdev.tune(device, **kwargs)
mock.assert_called_once_with(
'blockdev --setra 512 --setfra 512 /dev/sda',
'blockdev --setra 512 --setfra 512 {0}'.format(device),
python_shell=False
)
def test_format(self):
'''
unit tests for blockdev.format
'''
device = '/dev/sdX1'
fs_type = 'ext4'
mock = MagicMock(return_value=0)
with patch.dict(blockdev.__salt__, {'cmd.retcode': mock}):
self.assertEqual(blockdev.format_(device), True)
def test_fstype(self):
'''
unit tests for blockdev.fstype
'''
device = '/dev/sdX1'
fs_type = 'ext4'
mock = MagicMock(return_value='FSTYPE\n{0}'.format(fs_type))
with patch.dict(blockdev.__salt__, {'cmd.run': mock}):
self.assertEqual(blockdev.fstype(device), fs_type)
def test_resize2fs(self):
'''
unit tests for blockdev.resize2fs
'''
device = '/dev/sdX1'
mock = MagicMock()
with patch.dict(blockdev.__salt__, {'cmd.run_all': mock}):
blockdev.resize2fs(device)
mock.assert_called_once_with('resize2fs {0}'.format(device), python_shell=False)
if __name__ == '__main__':
from integration import run_tests

View file

@ -76,8 +76,12 @@ class BlockdevTestCase(TestCase):
ret.update({'comment': comt})
self.assertDictEqual(blockdev.formatted(name), ret)
mock = MagicMock(return_value='ext4')
with patch.dict(blockdev.__salt__, {'cmd.run': mock}):
mock_ext4 = MagicMock(return_value='ext4')
mock_t = MagicMock(return_value=True)
mock_e = MagicMock(return_value='')
with patch.dict(blockdev.__salt__, {'cmd.run': mock_ext4,
'blockdev.format': mock_t,
'blockdev.fstype': mock_e}):
comt = ('{0} already formatted with '.format(name))
ret.update({'comment': comt, 'result': True})
self.assertDictEqual(blockdev.formatted(name, fs_type=''), ret)