Commit e80edd3b authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

qa_config: Add __repr__ for instance/node classes



Having a readable representation makes debugging easier.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarBernardo Dal Seno <bdalseno@google.com>
parent 1b9e1f42
......@@ -70,6 +70,17 @@ class _QaInstance(object):
return cls(name=data["name"], nicmac=nicmac)
def __repr__(self):
status = [
"%s.%s" % (self.__class__.__module__, self.__class__.__name__),
"name=%s" % self.name,
"nicmac=%s" % self.nicmac,
"used=%s" % self._used,
"disk_template=%s" % self._disk_template,
]
return "<%s at %#x>" % (" ".join(status), id(self))
def Use(self):
"""Marks instance as being in use.
......@@ -150,6 +161,17 @@ class _QaNode(object):
"""
return cls(primary=data["primary"], secondary=data.get("secondary"))
def __repr__(self):
status = [
"%s.%s" % (self.__class__.__module__, self.__class__.__name__),
"primary=%s" % self.primary,
"secondary=%s" % self.secondary,
"added=%s" % self._added,
"use_count=%s" % self._use_count,
]
return "<%s at %#x>" % (" ".join(status), id(self))
def Use(self):
"""Marks a node as being in use.
......
......@@ -350,5 +350,59 @@ class TestQaConfig(unittest.TestCase):
exclude=acquired, _cfg=self.config)
class TestRepresentation(unittest.TestCase):
def _Check(self, target, part):
self.assertTrue(part in repr(target).split())
def testQaInstance(self):
inst = qa_config._QaInstance("inst1.example.com", [])
self._Check(inst, "name=inst1.example.com")
self._Check(inst, "nicmac=[]")
# Default values
self._Check(inst, "disk_template=None")
self._Check(inst, "used=None")
# Use instance
inst.Use()
self._Check(inst, "used=True")
# Disk template
inst.SetDiskTemplate(constants.DT_DRBD8)
self._Check(inst, "disk_template=%s" % constants.DT_DRBD8)
# Release instance
inst.Release()
self._Check(inst, "used=False")
self._Check(inst, "disk_template=None")
def testQaNode(self):
node = qa_config._QaNode("primary.example.com", "192.0.2.1")
self._Check(node, "primary=primary.example.com")
self._Check(node, "secondary=192.0.2.1")
self._Check(node, "added=False")
self._Check(node, "use_count=0")
# Mark as added
node.MarkAdded()
self._Check(node, "added=True")
# Use node
for i in range(1, 5):
node.Use()
self._Check(node, "use_count=%s" % i)
# Release node
for i in reversed(range(1, 5)):
node.Release()
self._Check(node, "use_count=%s" % (i - 1))
self._Check(node, "use_count=0")
# Mark as added
node.MarkRemoved()
self._Check(node, "added=False")
if __name__ == "__main__":
testutils.GanetiTestProgram()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment