# -*- coding: utf-8 -*-
-# pylint: disable=W0212
+# pylint: disable=W0212,too-many-return-statements
from __future__ import absolute_import
import json
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'])
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)
from __future__ import absolute_import
-from .helper import DashboardTestCase
+from .helper import DashboardTestCase, JObj, JLeaf, JList
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):