refactor moudule utils of hwc_utils.py (#55858)

* use navigate_value instead navigate_hash

* add async wait method

* update dict compare

* remove unuse methods

* not all modules have timeouts parameter

* navigate_value, the input data may be None
This commit is contained in:
zengchen 2019-05-09 21:04:52 +08:00 committed by jctanner
parent 34a8594c91
commit d8314e1a45
6 changed files with 305 additions and 327 deletions

View file

@ -1,109 +1,34 @@
# -*- coding: utf-8 -*-
import os
import sys
from units.compat import unittest
from ansible.module_utils.hwc_utils import (navigate_hash,
remove_empty_from_dict,
remove_nones_from_dict,
replace_resource_dict)
from ansible.module_utils.hwc_utils import (HwcModuleException, navigate_value)
class HwcUtilsTestCase(unittest.TestCase):
def test_navigate_hash(self):
def test_navigate_value(self):
value = {
'foo': {
'quiet': {
'tree': 'test'
'tree': 'test',
"trees": [0, 1]
},
}
}
self.assertEquals(navigate_hash(value, ["foo", "quiet", "tree"]),
self.assertEquals(navigate_value(value, ["foo", "quiet", "tree"]),
"test")
self.assertEquals(navigate_hash(value, ["foo", "q", "tree"], 123),
123)
self.assertEquals(
navigate_value(value, ["foo", "quiet", "trees"],
{"foo.quiet.trees": 1}),
1)
self.assertIsNone(navigate_hash(value, [], 123))
self.assertRaisesRegexp(HwcModuleException,
r".* key\(q\) is not exist in dict",
navigate_value, value, ["foo", "q", "tree"])
def test_remove_empty_from_dict(self):
value = {
'foo': {
'quiet': {
'tree': 'test',
'tree1': [
None,
{},
[],
'test'
],
'tree2': {},
'tree3': []
},
},
'foo1': [],
'foo2': {},
'foo3': None,
}
expect = {
'foo': {
'quiet': {
'tree': 'test',
'tree1': [
'test'
]
},
},
}
self.assertEqual(remove_empty_from_dict(value), expect)
def test_remove_nones_from_dict(self):
value = {
'foo': {
'quiet': {
'tree': 'test',
'tree1': [
None,
{},
[],
'test'
],
'tree2': {},
'tree3': []
},
},
'foo1': [],
'foo2': {},
'foo3': None,
}
expect = {
'foo': {
'quiet': {
'tree': 'test',
'tree1': [
{},
[],
'test'
],
'tree2': {},
'tree3': []
},
},
'foo1': [],
'foo2': {},
}
self.assertEqual(remove_nones_from_dict(value), expect)
def test_replace_resource_dict(self):
self.assertEqual(replace_resource_dict({'foo': 'quiet'}, 'foo'), 'quiet')
self.assertEqual(replace_resource_dict({}, 'foo'), {})
self.assertEqual(replace_resource_dict([[{'foo': 'quiet'}]], 'foo'),
[['quiet']])
self.assertRaisesRegexp(HwcModuleException,
r".* the index is out of list",
navigate_value, value,
["foo", "quiet", "trees"],
{"foo.quiet.trees": 2})