fixes how os-apply-config handles invalid json

If invalid json is included as the last element in
os_config_files.json it can silently break the file
customizations managed by os-apply-config.

More details:
https://bugzilla.redhat.com/show_bug.cgi?id=1493303

Closes-Bug: #1722321

Change-Id: I0ad89fcde101ba6f3723ab2fa5a348c40452bae5
This commit is contained in:
Matthew Flusche 2017-09-21 18:13:42 +00:00
parent 8a1ff8bde5
commit f462d6fec8
2 changed files with 8 additions and 2 deletions

View File

@ -58,7 +58,7 @@ def merge_configs(parsed_configs):
'''Returns deep-merged dict from passed list of dicts.'''
final_conf = {}
for conf in parsed_configs:
if conf:
if conf and isinstance(conf, dict):
final_conf = _deep_merge_dict(final_conf, conf)
return final_conf

View File

@ -109,6 +109,11 @@ class TestMergeConfigs(testtools.TestCase):
result = collect_config.merge_configs(type_conflict)
self.assertEqual({'a': [7, 8, 9]}, result)
def test_merge_configs_nested_type_conflict(self):
type_conflict = [{'a': {'foo': 'bar'}}, {'a': 'shazam'}]
result = collect_config.merge_configs(type_conflict)
self.assertEqual({'a': 'shazam'}, result)
def test_merge_configs_list_conflict(self):
list_conflict = [{'a': [1, 2, 3]},
{'a': [4, 5, 6]}]
@ -116,6 +121,7 @@ class TestMergeConfigs(testtools.TestCase):
self.assertEqual({'a': [4, 5, 6]}, result)
def test_merge_configs_empty_notdict(self):
list_conflict = [[], {'a': '1'}, '', None, {'b': '2'}, {}]
list_conflict = [[], {'a': '1'}, '', None, 'tacocat',
{'b': '2'}, {}, 'baseball']
result = collect_config.merge_configs(list_conflict)
self.assertEqual({'a': '1', 'b': '2'}, result)