]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
qa/tasks/mgr/dashboard: rbd: use JSON schema validator 21360/head
authorRicardo Dias <rdias@suse.com>
Fri, 13 Apr 2018 11:23:37 +0000 (12:23 +0100)
committerRicardo Dias <rdias@suse.com>
Fri, 13 Apr 2018 14:58:50 +0000 (15:58 +0100)
Signed-off-by: Ricardo Dias <rdias@suse.com>
qa/tasks/mgr/dashboard/helper.py
qa/tasks/mgr/dashboard/test_rbd.py

index 0fcfd80060622b1ef2bc0793177df0fd11e72d7d..b49126c9043935750d6aab65c0c647b82b42dcb3 100644 (file)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-
-# pylint: disable=W0212
+# pylint: disable=W0212,too-many-return-statements
 from __future__ import absolute_import
 
 import json
@@ -301,14 +301,14 @@ JList = namedtuple('JList', ['elem_typ'])
 JTuple = namedtuple('JList', ['elem_typs'])
 
 
-class JObj(namedtuple('JObj', ['sub_elems', 'allow_unknown'])):
-    def __new__(cls, sub_elems, allow_unknown=False):
+class JObj(namedtuple('JObj', ['sub_elems', 'allow_unknown', 'none'])):
+    def __new__(cls, sub_elems, allow_unknown=False, none=False):
         """
         :type sub_elems: dict[str, JAny | JLeaf | JList | JObj]
         :type allow_unknown: bool
         :return:
         """
-        return super(JObj, cls).__new__(cls, sub_elems, allow_unknown)
+        return super(JObj, cls).__new__(cls, sub_elems, allow_unknown, none)
 
 
 JAny = namedtuple('JAny', ['none'])
@@ -344,6 +344,10 @@ def _validate_json(val, schema, path=[]):
         return all(_validate_json(val[i], typ, path + [i])
                    for i, typ in enumerate(schema.elem_typs))
     if isinstance(schema, JObj):
+        if val is None and schema.none:
+            return True
+        elif val is None:
+            raise _ValError('val is None', path)
         missing_keys = set(schema.sub_elems.keys()).difference(set(val.keys()))
         if missing_keys:
             raise _ValError('missing keys: {}'.format(missing_keys), path)
index e9c8f8d97651b051856ccf42a672c51dd9f9fa08..127b1f59e72ff3d42f81debab7473219d49de9c7 100644 (file)
@@ -3,7 +3,7 @@
 
 from __future__ import absolute_import
 
-from .helper import DashboardTestCase
+from .helper import DashboardTestCase, JObj, JLeaf, JList
 
 
 class RbdTest(DashboardTestCase):
@@ -118,24 +118,29 @@ class RbdTest(DashboardTestCase):
                               "object-map"]
         }
         """
-        self.assertIn('size', img)
-        self.assertIn('obj_size', img)
-        self.assertIn('num_objs', img)
-        self.assertIn('order', img)
-        self.assertIn('block_name_prefix', img)
-        self.assertIn('name', img)
-        self.assertIn('id', img)
-        self.assertIn('pool_name', img)
-        self.assertIn('features', img)
-        self.assertIn('features_name', img)
-        self.assertIn('stripe_count', img)
-        self.assertIn('stripe_unit', img)
-        self.assertIn('parent', img)
-        self.assertIn('data_pool', img)
-        self.assertIn('snapshots', img)
-        self.assertIn('timestamp', img)
-        self.assertIn('disk_usage', img)
-        self.assertIn('total_disk_usage', img)
+        schema = JObj(sub_elems={
+            'size': JLeaf(int),
+            'obj_size': JLeaf(int),
+            'num_objs': JLeaf(int),
+            'order': JLeaf(int),
+            'block_name_prefix': JLeaf(str),
+            'name': JLeaf(str),
+            'id': JLeaf(str),
+            'pool_name': JLeaf(str),
+            'features': JLeaf(int),
+            'features_name': JList(JLeaf(str)),
+            'stripe_count': JLeaf(int, none=True),
+            'stripe_unit': JLeaf(int, none=True),
+            'parent': JObj(sub_elems={'pool_name': JLeaf(str),
+                                      'image_name': JLeaf(str),
+                                      'snap_name': JLeaf(str)}, none=True),
+            'data_pool': JLeaf(str, none=True),
+            'snapshots': JList(JLeaf(dict)),
+            'timestamp': JLeaf(str, none=True),
+            'disk_usage': JLeaf(int, none=True),
+            'total_disk_usage': JLeaf(int, none=True),
+        })
+        self.assertSchema(img, schema)
 
         for k, v in kwargs.items():
             if isinstance(v, list):