diff options
author | Joshua Harlow <harlowja@gmail.com> | 2013-07-24 08:39:03 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@gmail.com> | 2013-07-24 08:39:03 -0700 |
commit | 851906b6acde33fddcbdd1d41f366d6652d1e84d (patch) | |
tree | 0a715c1ee75bb730ae7f1e3dd6f1746413db801c | |
parent | 2939f498f51025ebd0c9bdf302b578dfcbf54d0c (diff) | |
download | vyos-cloud-init-851906b6acde33fddcbdd1d41f366d6652d1e84d.tar.gz vyos-cloud-init-851906b6acde33fddcbdd1d41f366d6652d1e84d.zip |
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.
-rw-r--r-- | cloudinit/handlers/cloud_config.py | 11 | ||||
-rw-r--r-- | tests/unittests/test_userdata.py | 59 |
2 files changed, 64 insertions, 6 deletions
diff --git a/cloudinit/handlers/cloud_config.py b/cloudinit/handlers/cloud_config.py index 7edae13d..34a73115 100644 --- a/cloudinit/handlers/cloud_config.py +++ b/cloudinit/handlers/cloud_config.py @@ -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) diff --git a/tests/unittests/test_userdata.py b/tests/unittests/test_userdata.py index 0ebb0484..5cd50f4f 100644 --- a/tests/unittests/test_userdata.py +++ b/tests/unittests/test_userdata.py @@ -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 |