← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~harlowja/cloud-init/test-jsonp into lp:cloud-init

 

Joshua Harlow has proposed merging lp:~harlowja/cloud-init/test-jsonp into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~harlowja/cloud-init/test-jsonp/+merge/176714

Small fix for jsonp prefix removal + jsonp tests.
-- 
https://code.launchpad.net/~harlowja/cloud-init/test-jsonp/+merge/176714
Your team cloud init development team is requested to review the proposed merge of lp:~harlowja/cloud-init/test-jsonp into lp:cloud-init.
=== modified file 'cloudinit/handlers/cloud_config.py'
--- cloudinit/handlers/cloud_config.py	2013-07-24 15:07:55 +0000
+++ cloudinit/handlers/cloud_config.py	2013-07-24 15:41:39 +0000
@@ -116,13 +116,12 @@
         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)

=== modified file 'tests/unittests/test_userdata.py'
--- tests/unittests/test_userdata.py	2013-05-10 05:34:31 +0000
+++ tests/unittests/test_userdata.py	2013-07-24 15:41:39 +0000
@@ -6,6 +6,7 @@
 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 @@
         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


Follow ups