From: Ricardo Dias Date: Fri, 13 Apr 2018 11:23:37 +0000 (+0100) Subject: qa/tasks/mgr/dashboard: rbd: use JSON schema validator X-Git-Tag: v13.1.0~234^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=fb41b943e0265b515ad4e6b6d64c72423079c229;p=ceph.git qa/tasks/mgr/dashboard: rbd: use JSON schema validator Signed-off-by: Ricardo Dias --- diff --git a/qa/tasks/mgr/dashboard/helper.py b/qa/tasks/mgr/dashboard/helper.py index 0fcfd8006062..b49126c90439 100644 --- a/qa/tasks/mgr/dashboard/helper.py +++ b/qa/tasks/mgr/dashboard/helper.py @@ -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) diff --git a/qa/tasks/mgr/dashboard/test_rbd.py b/qa/tasks/mgr/dashboard/test_rbd.py index e9c8f8d97651..127b1f59e72f 100644 --- a/qa/tasks/mgr/dashboard/test_rbd.py +++ b/qa/tasks/mgr/dashboard/test_rbd.py @@ -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):