unittest: add tests, filesystemmodel, blockdev

- add blockdev tests on available_partitions
- fix type issue in Disk.partition OrderedDict keys, always use integers
- Add __repr__ for Disk related Actions
- Fix issue with available partition logic, ensure we and them together
- Filter out 'leave unformatted' fstype, set to None as needed.
- Whitespace cleanup in unittests
- add fsm test on get_partitions

Signed-off-by: Ryan Harper <ryan.harper@canonical.com>
This commit is contained in:
Ryan Harper 2015-10-07 15:24:19 -05:00
parent 3e605f3b3b
commit 410ff1da07
4 changed files with 72 additions and 9 deletions

View File

@ -135,6 +135,9 @@ class DiskAction():
action.update({'wipe': self._wipe})
return action
def __repr__(self):
return str(self.get())
def dump(self):
return yaml.dump(self.get(), default_flow_style=False)

View File

@ -119,7 +119,7 @@ class Disk():
'size': self.size,
'partitions': self.partitions
}
return yaml.dump(o, default_flow_format=False)
return yaml.dump(o, default_flow_style=False)
@property
def devpath(self):
@ -251,13 +251,13 @@ class Blockdev():
@property
def available_partitions(self):
''' return list of non-zero sized partitions
defined but not mounted or formatted or used in
''' return list of non-zero sized partitions that are
defined but not mounted, not formatted, and not used in
raid, lvm, bcache'''
return [part.devpath for (num, part) in self.partitions.items()
if part.size > 0 and
part.flags not in ['raid', 'lvm', 'bcache'] and
(part.devpath not in self._mounts.keys() or
(part.devpath not in self._mounts.keys() and
part.devpath not in self._filesystems.keys())]
@property
@ -298,6 +298,9 @@ class Blockdev():
raise Exception('Not enough space (requested:{} free:{}'.format(
size, self.freespace))
# ensure we always use integers for partitions
partnum = int(partnum)
if len(self.disk.partitions) == 0:
offset = FIRST_PARTITION_OFFSET
else:
@ -321,7 +324,7 @@ class Blockdev():
partpath = "{}{}".format(self.disk.devpath, partnum)
# record filesystem formating
if fstype:
if fstype and fstype not in ['leave unformatted']:
fs_action = FormatAction(part_action, fstype)
log.debug('Adding filesystem on {}'.format(partpath))
log.debug('FormatAction:\n{}'.format(fs_action.get()))

View File

@ -314,7 +314,7 @@ class FilesystemModel(ModelPolicy):
log.debug('probe_storage: get_partitions()')
partitions = []
for dev in self.devices.values():
partnames = [part.path for (num, part) in
partnames = [part.devpath for (num, part) in
dev.disk.partitions.items()]
partitions += partnames

View File

@ -23,10 +23,10 @@ class TestFilesystemModel(testtools.TestCase):
logging.disable(logging.CRITICAL)
self.make_fsm()
# mocking the reading of the fake data saves on IO
# mocking the reading of the fake data saves on IO
@patch.object(Prober, 'get_storage')
def make_fsm(self, _get_storage):
_get_storage.return_value = fakes.FAKE_MACHINE_STORAGE_DATA
_get_storage.return_value = fakes.FAKE_MACHINE_STORAGE_DATA
self.opts = argparse.Namespace()
self.opts.machine_config = fakes.FAKE_MACHINE_JSON
self.opts.dry_run = True
@ -56,7 +56,7 @@ class TestFilesystemModel(testtools.TestCase):
def test_filesystemmodel_probe_storage(self):
'''sd[b..i]'''
disks = [d for d in self.storage.keys()
if self.storage[d]['DEVTYPE'] == 'disk' and
if self.storage[d]['DEVTYPE'] == 'disk' and
self.storage[d]['MAJOR'] in ['8', '253']]
self.fsm.probe_storage()
self.assertNotEqual(self.fsm.storage, {})
@ -122,6 +122,27 @@ class TestFilesystemModel(testtools.TestCase):
self.fsm.add_device(devname, disk)
self.assertTrue(devname in self.fsm.devices)
def test_filesystemmodel_get_partitions(self):
self.fsm.probe_storage()
# no partitions
partitions = self.fsm.get_partitions()
self.assertEqual(len(partitions), 0)
# add one to a random disk
diskname = random.choice(list(self.fsm.info.keys()))
disk = self.fsm.get_disk(diskname)
disk.add_partition(1, disk.freespace, None, None, flag='raid')
# we added one, we should get one
partitions = self.fsm.get_partitions()
self.assertEqual(len(partitions), 1)
# it should have the same base device name
print(partitions, diskname)
self.assertTrue(partitions[0].startswith(diskname))
class TestBlockdev(testtools.TestCase):
def setUp(self):
super(TestBlockdev, self).setUp()
@ -229,6 +250,23 @@ class TestBlockdev(testtools.TestCase):
part2 = self.bd.get_partition(partpath)
self.assertEqual(new_part, part2)
def test_blockdev_get_partition_with_string(self):
''' attempt to add a partition with number as a string type '''
partnum = '1'
self.add_partition(partnum=partnum)
# format the partpath with devpath and partnum
partpath='{}{}'.format(self.devpath, partnum)
# we shouldn't be able to get it via a string index
self.assertRaises(KeyError, lambda x: self.bd.partitions[x], partnum)
# check that we did create the partition and store it
# with an integer as the key in the partitions dictionary
new_part = self.bd.partitions[int(partnum)]
part2 = self.bd.get_partition(partpath)
self.assertEqual(new_part, part2)
def test_blockdev_get_actions(self):
self.add_partition()
actions = self.bd.get_actions()
@ -295,3 +333,22 @@ class TestBlockdev(testtools.TestCase):
self.assertEqual(partsize, fs_table[1][1])
self.assertEqual(fstype, fs_table[1][2])
self.assertEqual(partpath, fs_table[1][3])
def test_blockdev_available_partitions(self):
# add a non-empty partition
self.add_partition()
# we shouldn't have any empty partitions
empty = self.bd.available_partitions
self.assertEqual(empty, [])
partnum=2
self.add_partition(partnum=partnum, partsize=1 * GB,
fstype='leave unformatted',
mountpoint=None, flag=None)
# we should have one empty partition
empty = self.bd.available_partitions
print(empty)
self.assertEqual(len(empty), 1)