1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Cleaning up VariableManager tests (v2)

This commit is contained in:
James Cammarata 2015-05-14 20:10:31 -05:00
parent a0509cda1e
commit 48d62fd934
2 changed files with 33 additions and 17 deletions

View file

@ -72,7 +72,8 @@ class VariableManager:
''' ensures a clean copy of the extra_vars are made '''
return self._extra_vars.copy()
def set_extra_vars(self, value):
@extra_vars.setter
def extra_vars(self, value):
''' ensures a clean copy of the extra_vars are used to set the value '''
assert isinstance(value, MutableMapping)
self._extra_vars = value.copy()
@ -123,7 +124,7 @@ class VariableManager:
return result
def get_vars(self, loader, play=None, host=None, task=None):
def get_vars(self, loader, play=None, host=None, task=None, use_cache=True):
'''
Returns the variables, with optional "context" given via the parameters
for the play, host, and task (which could possibly result in different
@ -145,7 +146,7 @@ class VariableManager:
debug("in VariableManager get_vars()")
cache_entry = self._get_cache_entry(play=play, host=host, task=task)
if cache_entry in CACHED_VARS:
if cache_entry in CACHED_VARS and use_cache:
debug("vars are cached, returning them now")
return CACHED_VARS[cache_entry]
@ -229,7 +230,7 @@ class VariableManager:
# the 'omit' value alows params to be left out if the variable they are based on is undefined
all_vars['omit'] = self._omit_token
CACHED_VARS[cache_entry] = all_vars
#CACHED_VARS[cache_entry] = all_vars
debug("done with get_vars()")
return all_vars

View file

@ -38,7 +38,11 @@ class TestVariableManager(unittest.TestCase):
fake_loader = DictDataLoader({})
v = VariableManager()
self.assertEqual(v.get_vars(loader=fake_loader), dict())
vars = v.get_vars(loader=fake_loader, use_cache=False)
if 'omit' in vars:
del vars['omit']
self.assertEqual(vars, dict())
self.assertEqual(
v._merge_dicts(
@ -59,11 +63,14 @@ class TestVariableManager(unittest.TestCase):
extra_vars = dict(a=1, b=2, c=3)
v = VariableManager()
v.set_extra_vars(extra_vars)
v.extra_vars = extra_vars
vars = v.get_vars(loader=fake_loader, use_cache=False)
for (key, val) in extra_vars.iteritems():
self.assertEqual(v.get_vars(loader=fake_loader).get(key), val)
self.assertIsNot(v.extra_vars.get(key), val)
self.assertEqual(vars.get(key), val)
self.assertIsNot(v.extra_vars, extra_vars)
def test_variable_manager_host_vars_file(self):
fake_loader = DictDataLoader({
@ -82,30 +89,38 @@ class TestVariableManager(unittest.TestCase):
mock_host.get_vars.return_value = dict()
mock_host.get_groups.return_value = ()
self.assertEqual(v.get_vars(loader=fake_loader, host=mock_host).get("foo"), "bar")
self.assertEqual(v.get_vars(loader=fake_loader, host=mock_host, use_cache=False).get("foo"), "bar")
def test_variable_manager_group_vars_file(self):
fake_loader = DictDataLoader({
"group_vars/somegroup.yml": """
"group_vars/all.yml": """
foo: bar
""",
"group_vars/somegroup.yml": """
bam: baz
"""
})
v = VariableManager()
v.add_group_vars_file("group_vars/all.yml", loader=fake_loader)
v.add_group_vars_file("group_vars/somegroup.yml", loader=fake_loader)
self.assertIn("somegroup", v._group_vars_files)
self.assertEqual(v._group_vars_files["somegroup"], dict(foo="bar"))
self.assertEqual(v._group_vars_files["all"], dict(foo="bar"))
self.assertEqual(v._group_vars_files["somegroup"], dict(bam="baz"))
mock_group = MagicMock()
mock_group.name.return_value = "somegroup"
mock_group.name = "somegroup"
mock_group.get_ancestors.return_value = ()
mock_group.get_vars.return_value = dict()
mock_host = MagicMock()
mock_host.get_name.return_value = "hostname1"
mock_host.get_vars.return_value = dict()
mock_host.get_groups.return_value = (mock_group)
mock_host.get_groups.return_value = (mock_group,)
self.assertEqual(v.get_vars(loader=fake_loader, host=mock_host).get("foo"), "bar")
vars = v.get_vars(loader=fake_loader, host=mock_host, use_cache=False)
self.assertEqual(vars.get("foo"), "bar")
self.assertEqual(vars.get("bam"), "baz")
def test_variable_manager_play_vars(self):
fake_loader = DictDataLoader({})
@ -116,7 +131,7 @@ class TestVariableManager(unittest.TestCase):
mock_play.get_vars_files.return_value = []
v = VariableManager()
self.assertEqual(v.get_vars(loader=fake_loader, play=mock_play).get("foo"), "bar")
self.assertEqual(v.get_vars(loader=fake_loader, play=mock_play, use_cache=False).get("foo"), "bar")
def test_variable_manager_play_vars_files(self):
fake_loader = DictDataLoader({
@ -131,7 +146,7 @@ class TestVariableManager(unittest.TestCase):
mock_play.get_vars_files.return_value = ['/path/to/somefile.yml']
v = VariableManager()
self.assertEqual(v.get_vars(loader=fake_loader, play=mock_play).get("foo"), "bar")
self.assertEqual(v.get_vars(loader=fake_loader, play=mock_play, use_cache=False).get("foo"), "bar")
def test_variable_manager_task_vars(self):
fake_loader = DictDataLoader({})
@ -141,5 +156,5 @@ class TestVariableManager(unittest.TestCase):
mock_task.get_vars.return_value = dict(foo="bar")
v = VariableManager()
self.assertEqual(v.get_vars(loader=fake_loader, task=mock_task).get("foo"), "bar")
self.assertEqual(v.get_vars(loader=fake_loader, task=mock_task, use_cache=False).get("foo"), "bar")