Fix the cloud config merging so that it is backwards compat.
The new change for merging works well in the mergedict case but the default merging type for cloud config needs to reflect how yaml was loaded in bulk, which is the same as the replacing keys merging type that is now provided.
This commit is contained in:
parent
23b56e5ef8
commit
8867c8096b
@ -30,7 +30,13 @@ from cloudinit.settings import (PER_ALWAYS)
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
MERGE_HEADER = 'Merge-Type'
|
||||
DEF_MERGERS = mergers.default_mergers()
|
||||
|
||||
# Due to the way the loading of yaml configuration was done previously,
|
||||
# where previously each cloud config part was appended to a larger yaml
|
||||
# file and then finally that file was loaded as one big yaml file we need
|
||||
# to mimic that behavior by altering the default strategy to be replacing
|
||||
# keys of later mergers.
|
||||
DEF_MERGERS = mergers.string_extract_mergers('dict(replace)+list()+str()')
|
||||
|
||||
|
||||
class CloudConfigPartHandler(handlers.Handler):
|
||||
@ -53,6 +59,8 @@ class CloudConfigPartHandler(handlers.Handler):
|
||||
if self.file_names:
|
||||
file_lines.append("# from %s files" % (len(self.file_names)))
|
||||
for fn in self.file_names:
|
||||
if not fn:
|
||||
fn = '?'
|
||||
file_lines.append("# %s" % (fn))
|
||||
file_lines.append("")
|
||||
if self.cloud_buf is not None:
|
||||
@ -111,7 +119,10 @@ class CloudConfigPartHandler(handlers.Handler):
|
||||
return
|
||||
try:
|
||||
self._merge_part(payload, headers)
|
||||
self.file_names.append(filename)
|
||||
# Ensure filename is ok to store
|
||||
for i in ("\n", "\r", "\t"):
|
||||
filename = filename.replace(i, " ")
|
||||
self.file_names.append(filename.strip())
|
||||
except:
|
||||
util.logexc(LOG, "Failed at merging in cloud config part from %s",
|
||||
filename)
|
||||
|
5
tests/data/merge_sources/expected11.yaml
Normal file
5
tests/data/merge_sources/expected11.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
#cloud-config
|
||||
|
||||
a: 22
|
||||
b: 4
|
||||
c: 3
|
5
tests/data/merge_sources/expected12.yaml
Normal file
5
tests/data/merge_sources/expected12.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
#cloud-config
|
||||
|
||||
a:
|
||||
e:
|
||||
y: 2
|
@ -1,3 +1,3 @@
|
||||
Blah: 1
|
||||
Blah: 3
|
||||
Blah2: 2
|
||||
Blah3: 3
|
||||
Blah3: [1]
|
||||
|
5
tests/data/merge_sources/source11-1.yaml
Normal file
5
tests/data/merge_sources/source11-1.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
#cloud-config
|
||||
|
||||
a: 1
|
||||
b: 2
|
||||
c: 3
|
3
tests/data/merge_sources/source11-2.yaml
Normal file
3
tests/data/merge_sources/source11-2.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
#cloud-config
|
||||
|
||||
b: 4
|
3
tests/data/merge_sources/source11-3.yaml
Normal file
3
tests/data/merge_sources/source11-3.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
#cloud-config
|
||||
|
||||
a: 22
|
8
tests/data/merge_sources/source12-1.yaml
Normal file
8
tests/data/merge_sources/source12-1.yaml
Normal file
@ -0,0 +1,8 @@
|
||||
#cloud-config
|
||||
|
||||
a:
|
||||
c: 1
|
||||
d: 2
|
||||
e:
|
||||
z: a
|
||||
y: b
|
5
tests/data/merge_sources/source12-2.yaml
Normal file
5
tests/data/merge_sources/source12-2.yaml
Normal file
@ -0,0 +1,5 @@
|
||||
#cloud-config
|
||||
|
||||
a:
|
||||
e:
|
||||
y: 2
|
@ -167,6 +167,21 @@ class TestSimpleRun(helpers.ResourceUsingTestCase):
|
||||
d = util.mergemanydict([a, b])
|
||||
self.assertEquals(c, d)
|
||||
|
||||
def test_compat_merges_dict(self):
|
||||
a = {
|
||||
'Blah': 1,
|
||||
'Blah2': 2,
|
||||
'Blah3': 3,
|
||||
}
|
||||
b = {
|
||||
'Blah': 1,
|
||||
'Blah2': 2,
|
||||
'Blah3': [1],
|
||||
}
|
||||
c = _old_mergedict(a, b)
|
||||
d = util.mergemanydict([a, b])
|
||||
self.assertEquals(c, d)
|
||||
|
||||
def test_compat_merges_list(self):
|
||||
a = {'b': [1, 2, 3]}
|
||||
b = {'b': [4, 5]}
|
||||
|
@ -108,7 +108,7 @@ p: 1
|
||||
contents = util.load_yaml(contents)
|
||||
self.assertEquals(contents['run'], ['b', 'c', 'stuff', 'morestuff'])
|
||||
self.assertEquals(contents['a'], 'be')
|
||||
self.assertEquals(contents['e'], 'fg')
|
||||
self.assertEquals(contents['e'], [1, 2, 3])
|
||||
self.assertEquals(contents['p'], 1)
|
||||
|
||||
def test_unhandled_type_warning(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user