[blivet:rhel7/master 2/2] Give DeviceFormat objects an object_id (#1043763)
mulhern
amulhern at redhat.com
Mon Jan 6 21:16:14 UTC 2014
Related: rhbz#1043763
The motivation here is to track DeviceFormat objects across copies
mostly for tracking down where a default DeviceFormat object which lacks
a particular field came from.
Makes use of the object id in the representation and in the getFormat
logging text.
Signed-off-by: mulhern <amulhern at redhat.com>
---
blivet/formats/__init__.py | 11 +++++++----
tests/formats_test/init_test.py | 5 +++++
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/blivet/formats/__init__.py b/blivet/formats/__init__.py
index 1637bae..87a4607 100644
--- a/blivet/formats/__init__.py
+++ b/blivet/formats/__init__.py
@@ -26,6 +26,7 @@ from ..udev import udev_get_device
from ..util import notify_kernel
from ..util import get_sysfs_path_by_name
from ..util import run_program
+from ..util import ObjectID
from ..storage_log import log_method_call
from ..errors import *
from ..devicelibs.dm import dm_node_from_name
@@ -96,8 +97,8 @@ def getFormat(fmt_type, *args, **kwargs):
fmt._type = fmt_type
fmt._name = None
- log.debug("getFormat('%s') returning %s instance" % (fmt_type,
- fmt.__class__.__name__))
+ log.debug("getFormat('%s') returning %s instance with object id %d" %
+ (fmt_type, fmt.__class__.__name__, fmt.object_id))
return fmt
def collect_device_format_classes():
@@ -140,7 +141,7 @@ def get_device_format_class(fmt_type):
return fmt
-class DeviceFormat(object):
+class DeviceFormat(ObjectID):
""" Generic device format. """
_type = None
_name = _("Unknown")
@@ -170,6 +171,7 @@ class DeviceFormat(object):
exists -- indicates whether this is an existing format
"""
+ ObjectID.__init__(self)
self.device = kwargs.get("device")
self.uuid = kwargs.get("uuid")
self.exists = kwargs.get("exists")
@@ -181,12 +183,13 @@ class DeviceFormat(object):
# self.exists = True
def __repr__(self):
- s = ("%(classname)s instance (%(id)s) --\n"
+ s = ("%(classname)s instance (%(id)s) object id %(object_id)d--\n"
" type = %(type)s name = %(name)s status = %(status)s\n"
" device = %(device)s uuid = %(uuid)s exists = %(exists)s\n"
" options = %(options)s supported = %(supported)s"
" formattable = %(format)s resizable = %(resize)s\n" %
{"classname": self.__class__.__name__, "id": "%#x" % id(self),
+ "object_id": self.object_id,
"type": self.type, "name": self.name, "status": self.status,
"device": self.device, "uuid": self.uuid, "exists": self.exists,
"options": self.options, "supported": self.supported,
diff --git a/tests/formats_test/init_test.py b/tests/formats_test/init_test.py
index 55f527b..934a1a6 100644
--- a/tests/formats_test/init_test.py
+++ b/tests/formats_test/init_test.py
@@ -27,9 +27,14 @@ class FormatsTestCase(unittest.TestCase):
[formats.get_device_format_class(x) for x in format_names],
format_values)
+ ## A DeviceFormat object is returned if lookup by name fails
for name in format_names:
self.assertIs(formats.getFormat(name).__class__,
formats.DeviceFormat if format_pairs[name] is None else format_pairs[name])
+ ## Consecutively constructed DeviceFormat object have consecutive ids
+ names = [key for key in format_pairs.keys() if format_pairs[key] is not None]
+ ids = [formats.getFormat(name).object_id for name in names]
+ self.assertEqual(ids, range(ids[0], ids[0] + len(ids)))
def suite():
return unittest.TestLoader().loadTestsFromTestCase(FormatsTestCase)
--
1.8.3.1
More information about the anaconda-patches
mailing list