Fix small prefix bug + jsonp tests.

Fix the wrong usage of the prefix removal array action
by just using the new util function that does these
actions correctly. 

Add in a couple of unit tests to verify the jsonp merging
and usage works as expected.
This commit is contained in:
Joshua Harlow 2013-07-24 08:39:03 -07:00
parent 750438ad7f
commit 44a651a7f9
2 changed files with 64 additions and 6 deletions

View File

@ -116,13 +116,12 @@ class CloudConfigPartHandler(handlers.Handler):
return (payload_yaml, all_mergers)
def _merge_patch(self, payload):
# JSON doesn't handle comments in this manner, so ensure that
# if we started with this 'type' that we remove it before
# attempting to load it as json (which the jsonpatch library will
# attempt to do).
payload = payload.lstrip()
if payload.lower().startswith(JSONP_PREFIX):
# JSON doesn't handle comments in this manner, so ensure that
# if we started with this 'type' that we remove it before
# attempting to load it as json (which the jsonpatch library will
# attempt to do).
payload = payload[JSONP_PREFIX:]
payload = util.strip_prefix_suffix(payload, prefix=JSONP_PREFIX)
patch = jsonpatch.JsonPatch.from_string(payload)
LOG.debug("Merging by applying json patch %s", patch)
self.cloud_buf = patch.apply(self.cloud_buf, in_place=False)

View File

@ -6,6 +6,7 @@ import logging
import os
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from cloudinit import handlers
from cloudinit import helpers as c_helpers
@ -50,6 +51,64 @@ class TestConsumeUserData(helpers.FilesystemMockingTestCase):
self._log.addHandler(self._log_handler)
return log_file
def test_simple_jsonp(self):
blob = '''
#cloud-config-jsonp
[
{ "op": "add", "path": "/baz", "value": "qux" },
{ "op": "add", "path": "/bar", "value": "qux2" }
]
'''
ci = stages.Init()
ci.datasource = FakeDataSource(blob)
new_root = self.makeDir()
self.patchUtils(new_root)
self.patchOS(new_root)
ci.fetch()
ci.consume_userdata()
cc_contents = util.load_file(ci.paths.get_ipath("cloud_config"))
cc = util.load_yaml(cc_contents)
self.assertEquals(2, len(cc))
self.assertEquals('qux', cc['baz'])
self.assertEquals('qux2', cc['bar'])
def test_mixed_cloud_config(self):
blob_cc = '''
#cloud-config
a: b
c: d
'''
message_cc = MIMEBase("text", "cloud-config")
message_cc.set_payload(blob_cc)
blob_jp = '''
#cloud-config-jsonp
[
{ "op": "replace", "path": "/a", "value": "c" },
{ "op": "remove", "path": "/c" }
]
'''
message_jp = MIMEBase('text', "cloud-config-jsonp")
message_jp.set_payload(blob_jp)
message = MIMEMultipart()
message.attach(message_cc)
message.attach(message_jp)
ci = stages.Init()
ci.datasource = FakeDataSource(str(message))
new_root = self.makeDir()
self.patchUtils(new_root)
self.patchOS(new_root)
ci.fetch()
ci.consume_userdata()
cc_contents = util.load_file(ci.paths.get_ipath("cloud_config"))
cc = util.load_yaml(cc_contents)
self.assertEquals(1, len(cc))
self.assertEquals('c', cc['a'])
def test_merging_cloud_config(self):
blob = '''
#cloud-config