Francesco Romani has uploaded a new change for review.

Change subject: lib: response: helper to detect valid responses
......................................................................

lib: response: helper to detect valid responses

Ideally, we should build responses in the API layer, from
simpler return values, or from exception raised, in our
application layer code.
But due to the lefacy code, we need a gradual modernization,
so we need to cope with response objects built in the
application layer. For the short/mid-term, the API layer
needs to cope with both flows, modern and old.

So we need an utility to detect which kind the response
the application layer returns to the API layer.

This patch adds this utility, which will be erased once
the transition is complete, with the other scaffolding.

Change-Id: Ifab6a25bea1b4a187d8425275e86bdb2fecf4c7d
Signed-off-by: Francesco Romani <from...@redhat.com>
---
M lib/vdsm/response.py
M tests/responseTests.py
2 files changed, 33 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/60/63760/1

diff --git a/lib/vdsm/response.py b/lib/vdsm/response.py
index 9a6f57f..66fd590 100644
--- a/lib/vdsm/response.py
+++ b/lib/vdsm/response.py
@@ -84,3 +84,15 @@
             return code == errCode[err]["status"]["code"]
         else:
             return code != doneCode["code"]
+
+
+def is_valid(res):
+    # catching AttributeError is even uglier
+    if not isinstance(res, dict):
+        return False
+    try:
+        status = res["status"]
+        return all(k in status for k in
+                   ("code", "message"))
+    except KeyError:
+        return False
diff --git a/tests/responseTests.py b/tests/responseTests.py
index b4a5879..d8ada50 100644
--- a/tests/responseTests.py
+++ b/tests/responseTests.py
@@ -97,6 +97,27 @@
                           response.is_error,
                           {'status': {}})
 
+    @permutations([
+        # res
+        [response.success()],
+        [response.success(foo='bar', a=42)],
+        [response.error('noVM')],
+        [{'status': {'code': '0', 'message': 'ok', 'foo': 'bar'}}],
+    ])
+    def test_is_valid(self, res):
+        self.assertTrue(response.is_valid(res))
+
+    @permutations([
+        # res
+        [('foo', 'bar')],
+        [['foo', 'bar']],
+        [{'code': 42}],
+        [{'message': 'foobar'}],
+        [{'success': True}],
+    ])
+    def test_is_not_valid(self, res):
+        self.assertFalse(response.is_valid(res))
+
     def test_malformed_exception_contains_response(self):
         bad_res = {}
         try:


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifab6a25bea1b4a187d8425275e86bdb2fecf4c7d
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Francesco Romani <from...@redhat.com>
_______________________________________________
vdsm-patches mailing list
vdsm-patches@lists.fedorahosted.org
https://lists.fedorahosted.org/admin/lists/vdsm-patches@lists.fedorahosted.org

Reply via email to