diff options
| author | Phil Day <philip.day@hp.com> | 2013-02-11 10:52:35 +0000 |
|---|---|---|
| committer | Phil Day <philip.day@hp.com> | 2013-02-12 13:06:52 +0000 |
| commit | a94b9b4b25cd1e7d39ebef0b60f8c4367765777f (patch) | |
| tree | e6b9b96ea2d861db7f943dd54bd855f7fce21490 /tests | |
| parent | e650657862b0d9c070f70fe078af56c55fb02e73 (diff) | |
to_primitive imposes what seems to be an arbitary data structure
depth of 3, but there is at least on case in Nova (Security
group Rules) which requires a depth beyond this.
https://bugs.launchpad.net/nova/+bug/1118608
Specifically security_group_rule_get_by_security_group
returns a set of rules which have the structure:
rule -> grantee_group -> Instance -> Instance_type
Rather than just bumping the depth limit, which might break some
other user of to_primitive we make it a specific parameter that
defaults to the current value but can be over-ridden when
required and log a warning when the depth is exceeded
Change-Id: I1eaebd484e20cb2eae09a693289709973de9943c
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/unit/test_jsonutils.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/unit/test_jsonutils.py b/tests/unit/test_jsonutils.py index 87ac367..276ae97 100644 --- a/tests/unit/test_jsonutils.py +++ b/tests/unit/test_jsonutils.py @@ -143,3 +143,29 @@ class ToPrimitiveTestCase(utils.BaseTestCase): self.assertTrue(ret[0].startswith(u"<module 'datetime' from ")) self.assertTrue(ret[1].startswith('<function foo at 0x')) self.assertEquals(ret[2], '<built-in function dir>') + + def test_depth(self): + class LevelsGenerator(object): + def __init__(self, levels): + self._levels = levels + + def iteritems(self): + if self._levels == 0: + return iter([]) + else: + return iter([(0, LevelsGenerator(self._levels - 1))]) + + l4_obj = LevelsGenerator(4) + + json_l2 = {0: {0: '?'}} + json_l3 = {0: {0: {0: '?'}}} + json_l4 = {0: {0: {0: {0: '?'}}}} + + ret = jsonutils.to_primitive(l4_obj, max_depth=2) + self.assertEquals(ret, json_l2) + + ret = jsonutils.to_primitive(l4_obj, max_depth=3) + self.assertEquals(ret, json_l3) + + ret = jsonutils.to_primitive(l4_obj, max_depth=4) + self.assertEquals(ret, json_l4) |
