Add equality for Actions, Disks and Blockdev classes

Unittest on filesystem model require equality methods for
the various storage classes.

Signed-off-by: Ryan Harper <ryan.harper@canonical.com>
This commit is contained in:
Ryan Harper 2015-10-06 15:23:29 -05:00
parent 0017527e56
commit c6e1f547df
4 changed files with 147 additions and 9 deletions

View File

@ -111,6 +111,19 @@ class DiskAction():
self._wipe = wipe
self._type = 'disk'
__hash__ = None
def __eq__(self, other):
if isinstance(other, self.__class__):
return (self._action_id == other._action_id and
self.parent == other.parent and
self._ptable == other._ptable and
self._model == other._model and
self._serial == other._serial and
self._wipe == other._wipe and
self._type == other._type)
else:
return False
def get_parent(self):
return self.parent
@ -147,6 +160,18 @@ class RaidAction(DiskAction):
self._spares = spare_ids
self._type = 'raid'
__hash__ = None
def __eq__(self, other):
if isinstance(other, self.__class__):
return (self._action_id == other._action_id and
self.parent == other.parent and
self._raidlevel == other._raidlevel and
self._devices == other._devices and
self._spares == other._spares and
self._type == other._type)
else:
return False
def get(self):
action = {
'id': self.action_id,
@ -174,6 +199,20 @@ class PartitionAction(DiskAction):
if self.flags in ['bios_grub']:
self._action_id = 'bios_boot_partition'
__hash__ = None
def __eq__(self, other):
if isinstance(other, self.__class__):
return (self._action_id == other._action_id and
self.parent == other.parent and
self.partnum == other.partnum and
self._offset == other._offset and
self._size == other._size and
self.flags == other.flags and
self._type == other._type)
else:
return False
@property
def path(self):
return "{}{}".format(self.parent.action_id, self.partnum)
@ -230,6 +269,17 @@ class FormatAction(DiskAction):
if fstype.startswith('fat'):
self._action_id = self._action_id[:11]
__hash__ = None
def __eq__(self, other):
if isinstance(other, self.__class__):
return (self._action_id == other._action_id and
self.parent == other.parent and
self._fstype == other._fstype and
self._type == other._type)
else:
return False
@property
def fstype(self):
return self._fstype
@ -250,6 +300,16 @@ class MountAction(DiskAction):
self._action_id = "{}_mnt".format(self.parent.action_id)
self._type = 'mount'
__hash__ = None
def __eq__(self, other):
if isinstance(other, self.__class__):
return (self._action_id == other._action_id and
self.parent == other.parent and
self._path == other._path and
self._type == other._type)
else:
return False
@property
def path(self):
return self._path

View File

@ -77,6 +77,21 @@ class Disk():
self._size = self._get_size(devpath, size)
self._partitions = OrderedDict()
def __eq__(self, other):
if isinstance(other, self.__class__):
print('disk same class, checking members')
return (self._devpath == other._devpath and
self._serial == other._serial and
self._parttype == other._parttype and
self._model == other._model and
self._size == other._size and
self._partitions == other._partitions)
else:
return False
__hash__ = None
def __ne__(self, other):
return not self.__eq__(other)
def _get_size(self, devpath, size):
if size:
return size
@ -95,6 +110,17 @@ class Disk():
return nr_blocks * block_sz
def __repr__(self):
o = {
'devpath': self.devpath,
'serial': self.serial,
'model': self.model,
'parttype': self.parttype,
'size': self.size,
'partitions': self.partitions
}
return yaml.dump(o, default_flow_format=False)
@property
def devpath(self):
return self._devpath
@ -136,6 +162,25 @@ class Blockdev():
self.disk.model, self.disk.serial,
self.disk.parttype)
def __eq__(self, other):
if isinstance(other, self.__class__):
return (self.disk == other.disk and
self._filesystems == other._filesystems and
self._mounts == other._mounts and
self.bcache == other.bcache and
self.lvm == other.lvm and
self.holder == other.holder and
self.baseaction == other.baseaction)
else:
return False
__hash__ = None
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return str(self.get_actions())
def reset(self):
''' Wipe out any actions queued for this disk '''
self.disk.reset()

View File

@ -1,7 +1,7 @@
import os
import yaml
import json
TOP_DIR = os.path.join('/'.join(__file__.split('/')[:-3]))
TEST_DATA = os.path.join(TOP_DIR, 'subiquity', 'tests', 'data')
FAKE_MACHINE_JSON = os.path.join(TEST_DATA, 'fake_machine.json')
FAKE_MACHINE_STORAGE_DATA = json.load(open(FAKE_MACHINE_JSON)).get('storage')

View File

@ -1,7 +1,10 @@
import testtools
import random
import argparse
import logging
import random
import testtools
import yaml
from mock import patch
from subiquity.models.blockdev import (Blockdev,
blockdev_align_up,
FIRST_PARTITION_OFFSET,
@ -16,13 +19,21 @@ GB = 1 << 40
class TestFilesystemModel(testtools.TestCase):
def setUp(self):
super(TestFilesystemModel, self).setUp()
# don't show logging messages while testing
logging.disable(logging.CRITICAL)
self.make_fsm()
# 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
self.opts = argparse.Namespace()
self.opts.machine_config = fakes.FAKE_MACHINE_JSON
self.opts.dry_run = True
self.prober = Prober(self.opts)
self.storage = fakes.FAKE_MACHINE_STORAGE_DATA
self.fsm = FilesystemModel(self.prober, self.opts)
def test_filesystemmodel_init(self):
self.assertNotEqual(self.fsm, None)
self.assertEqual(self.fsm.info, {})
@ -36,12 +47,34 @@ class TestFilesystemModel(testtools.TestCase):
def test_filesystemmodel_get_signal_by_name(self):
for (name, signal, method) in self.fsm.get_signals():
self.assertEqual(signal,
self.fsm.get_signal_by_name(name))
self.assertEqual(self.fsm.get_signal_by_name(name), signal)
def test_filesystemmodel_get_menu(self):
self.assertEqual(sorted(self.fsm.fs_menu),
sorted(self.fsm.get_menu()))
self.assertEqual(sorted(self.fsm.get_menu()),
sorted(self.fsm.fs_menu))
def test_filesystemmodel_probe_storage(self):
'''sd[b..i]'''
disks = [d for d in self.storage.keys()
if self.storage[d]['DEVTYPE'] == 'disk' and
self.storage[d]['MAJOR'] in ['8', '253']]
self.fsm.probe_storage()
self.assertNotEqual(self.fsm.storage, {})
self.assertEqual(sorted(self.fsm.info.keys()),
sorted(disks))
def test_filesystemmodel_get_disk(self):
self.fsm.probe_storage()
diskname = random.choice(list(self.fsm.info.keys()))
disk = Blockdev(diskname,
self.fsm.info[diskname].serial,
self.fsm.info[diskname].model,
size=self.fsm.info[diskname].size)
test_disk = self.fsm.get_disk(diskname)
print(disk)
print(test_disk)
self.assertEqual(test_disk, disk)
class TestBlockdev(testtools.TestCase):