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}) action.update({'wipe': self._wipe})
return action return action
def __repr__(self):
return str(self.get())
def dump(self): def dump(self):
return yaml.dump(self.get(), default_flow_style=False) return yaml.dump(self.get(), default_flow_style=False)

View File

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

View File

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

View File

@ -122,6 +122,27 @@ class TestFilesystemModel(testtools.TestCase):
self.fsm.add_device(devname, disk) self.fsm.add_device(devname, disk)
self.assertTrue(devname in self.fsm.devices) 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): class TestBlockdev(testtools.TestCase):
def setUp(self): def setUp(self):
super(TestBlockdev, self).setUp() super(TestBlockdev, self).setUp()
@ -229,6 +250,23 @@ class TestBlockdev(testtools.TestCase):
part2 = self.bd.get_partition(partpath) part2 = self.bd.get_partition(partpath)
self.assertEqual(new_part, part2) 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): def test_blockdev_get_actions(self):
self.add_partition() self.add_partition()
actions = self.bd.get_actions() actions = self.bd.get_actions()
@ -295,3 +333,22 @@ class TestBlockdev(testtools.TestCase):
self.assertEqual(partsize, fs_table[1][1]) self.assertEqual(partsize, fs_table[1][1])
self.assertEqual(fstype, fs_table[1][2]) self.assertEqual(fstype, fs_table[1][2])
self.assertEqual(partpath, fs_table[1][3]) 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)