Nir Soffer has uploaded a new change for review.

Change subject: jobs: Add Job.__repr__ for easier debugging
......................................................................

jobs: Add Job.__repr__ for easier debugging

Every object should have __repr__ to make it easy to have more detailed
logs, or for debugging interactively using the manhole debugging shell.

Example output:

    <ProgressingJob id=9cf0ae08-d0e3-45ba-886c-3a28c00e02b3
    status=running progress=32% at 0x140170561474000>

Change-Id: I8317fde241d064ae230093610f9aaac750fcf08f
Signed-off-by: Nir Soffer <nsof...@redhat.com>
---
M lib/vdsm/jobs.py
M tests/jobsTests.py
2 files changed, 23 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/17/51217/1

diff --git a/lib/vdsm/jobs.py b/lib/vdsm/jobs.py
index cdf1fa8..84e9691 100644
--- a/lib/vdsm/jobs.py
+++ b/lib/vdsm/jobs.py
@@ -122,6 +122,13 @@
         """
         raise AbortNotSupported()
 
+    def __repr__(self):
+        s = "<{self.__class__.__name__} id={self.id} status={self.status} "
+        if self.progress is not None:
+            s += "progress={self.progress}% "
+        s += "at 0x{id}>"
+        return s.format(self=self, id=id(self))
+
 
 # This helper should only be called by test code.  Everything else should be
 # using the public APIs.
diff --git a/tests/jobsTests.py b/tests/jobsTests.py
index 65f5c80..c541303 100644
--- a/tests/jobsTests.py
+++ b/tests/jobsTests.py
@@ -211,3 +211,19 @@
         job._error = error
         self.assertEqual(job.error, error)
         self.assertEqual(error.info(), job.info()['error'])
+
+    def test_job_repr(self):
+        job = TestingJob()
+        rep = repr(job)
+        self.assertIn("TestingJob", rep)
+        self.assertIn("id=%s" % job.id, rep)
+        self.assertIn("status=pending", rep)
+        self.assertNotIn("progress=", rep)
+
+    def test_job_repr_with_progress(self):
+        job = ProgressingJob()
+        job._status = jobs.STATUS.RUNNING
+        job._progress = 32
+        rep = repr(job)
+        self.assertIn("status=running", rep)
+        self.assertIn("progress=32%", rep)


-- 
To view, visit https://gerrit.ovirt.org/51217
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8317fde241d064ae230093610f9aaac750fcf08f
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsof...@redhat.com>
_______________________________________________
vdsm-patches mailing list
vdsm-patches@lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to