Francesco Romani has uploaded a new change for review.

Change subject: tests: type checking for primitive types
......................................................................

tests: type checking for primitive types

This patch adds type checking for the return types of the API.
Only primitive types (bool, *int, float, str) are supported;
full type checking, including enum values and types defined
in the schema, will be implemented in future patches.

This will help avoiding future mistakes like the one fixed in
http://gerrit.ovirt.org/#/c/28781/1

Change-Id: I8463b5148e710bbb3b336d4b79fae183a71d89da
Signed-off-by: Francesco Romani <from...@redhat.com>
---
M tests/vmApiTests.py
1 file changed, 54 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/02/28802/1

diff --git a/tests/vmApiTests.py b/tests/vmApiTests.py
index a616d45..4c84d59 100644
--- a/tests/vmApiTests.py
+++ b/tests/vmApiTests.py
@@ -40,6 +40,36 @@
         vm._vmStats.stop()
 
 
+def isBool(value):
+    return value in ('true', 'false')
+
+
+def isInt(value):
+    try:
+        return str(int(value)) == value
+    except ValueError:
+        return False
+
+
+def isFloat(value):
+    try:
+        float(value)
+        return True
+    except ValueError:
+        return False
+
+
+def isStr(value):
+    return isinstance(value, str)
+
+
+PRIMITIVE_TYPES = {
+    'bool': isBool,
+    'int': isInt, 'uint': isInt,
+    'float': isFloat,
+    'str':  isStr}
+
+
 class TestVmStats(TestCaseBase):
     @utils.memoized
     def _getAPI(self):
@@ -50,16 +80,36 @@
         return vdsmapi.get_api(apiPath)
 
     def assertVmStatsSchemaCompliancy(self, schema, stats):
+        def isOptional(apiItem):
+            return apiItem[0] == '*'
+
+        def getApiItemName(apiItem):
+            return apiItem[1:] if isOptional(apiItem) else apiItem
+
+        def assertTypeCompliancy(stats, apiName, apiType):
+            if apiName in ('displayPort', 'displaySecurePort',
+                           'guestCPUCount'):
+                # known exceptions. FIXME: fix these.
+                return
+            if isinstance(apiType, str) and apiType in PRIMITIVE_TYPES:
+                self.assertTrue(
+                    PRIMITIVE_TYPES[apiType](stats[apiName]),
+                    "%s (%s) is not %s" % (
+                        apiName, str(stats[apiName]), apiType))
+        # TODO: more type checking
+
         api = self._getAPI()
         ref = api['types'][schema]['data']
         for apiItem, apiType in ref.items():
-            if apiItem[0] == '*':
+            apiName = getApiItemName(apiItem)
+            if isOptional(apiItem):
                 # optional, may be absent and it is fine
-                self.assertTrue(stats.get(apiItem[1:], True))
+                if apiName in stats:
+                    assertTypeCompliancy(stats, apiName, apiType)
             else:
                 # mandatory
-                self.assertIn(apiItem, stats)
-        # TODO: type checking
+                self.assertIn(apiName, stats)
+                assertTypeCompliancy(stats, apiName, apiType)
 
     def testDownStats(self):
         with FakeVM() as fake:


-- 
To view, visit http://gerrit.ovirt.org/28802
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8463b5148e710bbb3b336d4b79fae183a71d89da
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/mailman/listinfo/vdsm-patches

Reply via email to