summaryrefslogtreecommitdiff
path: root/tests/unittests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unittests')
-rw-r--r--tests/unittests/helpers.py33
-rw-r--r--tests/unittests/test_data.py51
2 files changed, 84 insertions, 0 deletions
diff --git a/tests/unittests/helpers.py b/tests/unittests/helpers.py
index a2355a79..a73e3f61 100644
--- a/tests/unittests/helpers.py
+++ b/tests/unittests/helpers.py
@@ -300,6 +300,39 @@ def dir2dict(startdir, prefix=None):
return flist
+def wrap_and_call(prefix, mocks, func, *args, **kwargs):
+ """
+ call func(args, **kwargs) with mocks applied, then unapplies mocks
+ nicer to read than repeating dectorators on each function
+
+ prefix: prefix for mock names (e.g. 'cloudinit.stages.util') or None
+ mocks: dictionary of names (under 'prefix') to mock and either
+ a return value or a dictionary to pass to the mock.patch call
+ func: function to call with mocks applied
+ *args,**kwargs: arguments for 'func'
+
+ return_value: return from 'func'
+ """
+ delim = '.'
+ if prefix is None:
+ prefix = ''
+ prefix = prefix.rstrip(delim)
+ unwraps = []
+ for fname, kw in mocks.items():
+ if prefix:
+ fname = delim.join((prefix, fname))
+ if not isinstance(kw, dict):
+ kw = {'return_value': kw}
+ p = mock.patch(fname, **kw)
+ p.start()
+ unwraps.append(p)
+ try:
+ return func(*args, **kwargs)
+ finally:
+ for p in unwraps:
+ p.stop()
+
+
try:
skipIf = unittest.skipIf
except AttributeError:
diff --git a/tests/unittests/test_data.py b/tests/unittests/test_data.py
index 55d9b93f..581b581a 100644
--- a/tests/unittests/test_data.py
+++ b/tests/unittests/test_data.py
@@ -559,3 +559,54 @@ class TestConvertString(helpers.TestCase):
text = "hi mom"
msg = ud.convert_string(text)
self.assertEqual(text, msg.get_payload(decode=False))
+
+
+class TestFetchBaseConfig(helpers.TestCase):
+
+ def test_only_builtin_gets_builtin2(self):
+ ret = helpers.wrap_and_call(
+ 'cloudinit.stages.util',
+ {'read_conf_with_confd': None,
+ 'read_conf_from_cmdline': None},
+ stages.fetch_base_config)
+ self.assertEqual(util.get_builtin_cfg(), ret)
+
+ def test_conf_d_overrides_defaults(self):
+ builtin = util.get_builtin_cfg()
+ test_key = sorted(builtin)[0]
+ test_value = 'test'
+ ret = helpers.wrap_and_call(
+ 'cloudinit.stages.util',
+ {'read_conf_with_confd': {'return_value': {test_key: test_value}},
+ 'read_conf_from_cmdline': None},
+ stages.fetch_base_config)
+ self.assertEqual(ret.get(test_key), test_value)
+ builtin[test_key] = test_value
+ self.assertEqual(ret, builtin)
+
+ def test_cmdline_overrides_defaults(self):
+ builtin = util.get_builtin_cfg()
+ test_key = sorted(builtin)[0]
+ test_value = 'test'
+ cmdline = {test_key: test_value}
+ ret = helpers.wrap_and_call(
+ 'cloudinit.stages.util',
+ {'read_conf_from_cmdline': {'return_value': cmdline},
+ 'read_conf_with_confd': None},
+ stages.fetch_base_config)
+ self.assertEqual(ret.get(test_key), test_value)
+ builtin[test_key] = test_value
+ self.assertEqual(ret, builtin)
+
+ def test_cmdline_overrides_conf_d_and_defaults(self):
+ builtin = {'key1': 'value0', 'key3': 'other2'}
+ conf_d = {'key1': 'value1', 'key2': 'other1'}
+ cmdline = {'key3': 'other3', 'key2': 'other2'}
+ ret = helpers.wrap_and_call(
+ 'cloudinit.stages.util',
+ {'read_conf_with_confd': {'return_value': conf_d},
+ 'get_builtin_cfg': {'return_value': builtin},
+ 'read_conf_from_cmdline': {'return_value': cmdline}},
+ stages.fetch_base_config)
+ self.assertEqual(ret, {'key1': 'value1', 'key2': 'other2',
+ 'key3': 'other3'})