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 id in the representation and in the getFormat logging text.
Signed-off-by: mulhern <amulhern(a)redhat.com>
---
blivet/formats/__init__.py | 11 +++++++----
tests/formats_test/init_test.py | 11 +++++++++++
2 files changed, 18 insertions(+), 4 deletions(-)
diff --git a/blivet/formats/__init__.py b/blivet/formats/__init__.py
index 1637bae..882723c 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.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.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..2e41dfb 100644
--- a/tests/formats_test/init_test.py
+++ b/tests/formats_test/init_test.py
@@ -1,4 +1,5 @@
#!/usr/bin/python
+import copy
import unittest
import blivet.formats as formats
@@ -27,9 +28,19 @@ 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 objects have consecutive ids
+ names = [key for key in format_pairs.keys() if format_pairs[key] is not None]
+ objs = [formats.getFormat(name) for name in names]
+ ids = [obj.id for obj in objs]
+ self.assertEqual(ids, range(ids[0], ids[0] + len(ids)))
+
+ ## Copy or deepcopy should preserve the id
+ self.assertEqual(ids, [copy.copy(obj).id for obj in objs])
+ self.assertEqual(ids, [copy.deepcopy(obj).id for obj in objs])
def suite():
return unittest.TestLoader().loadTestsFromTestCase(FormatsTestCase)
--
1.8.3.1