Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
No related merge requests found
...@@ -70,6 +70,17 @@ class _QaInstance(object): ...@@ -70,6 +70,17 @@ class _QaInstance(object):
return cls(name=data["name"], nicmac=nicmac) 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): def Use(self):
"""Marks instance as being in use. """Marks instance as being in use.
...@@ -150,6 +161,17 @@ class _QaNode(object): ...@@ -150,6 +161,17 @@ class _QaNode(object):
""" """
return cls(primary=data["primary"], secondary=data.get("secondary")) 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): def Use(self):
"""Marks a node as being in use. """Marks a node as being in use.
......
...@@ -350,5 +350,59 @@ class TestQaConfig(unittest.TestCase): ...@@ -350,5 +350,59 @@ class TestQaConfig(unittest.TestCase):
exclude=acquired, _cfg=self.config) 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__": if __name__ == "__main__":
testutils.GanetiTestProgram() testutils.GanetiTestProgram()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment