Merge lp:~harlowja/cloud-init/test-jsonp into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Joshua Harlow
Status: Merged
Merged at revision: 843
Proposed branch: lp:~harlowja/cloud-init/test-jsonp
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 99 lines (+64/-6)
2 files modified
cloudinit/handlers/cloud_config.py (+5/-6)
tests/unittests/test_userdata.py (+59/-0)
To merge this branch: bzr merge lp:~harlowja/cloud-init/test-jsonp
Reviewer Review Type Date Requested Status
cloud-init Commiters Pending
Review via email: mp+176714@code.launchpad.net

Description of the change

Small fix for jsonp prefix removal + jsonp tests.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== 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 @@
116 return (payload_yaml, all_mergers)116 return (payload_yaml, all_mergers)
117117
118 def _merge_patch(self, payload):118 def _merge_patch(self, payload):
119 # JSON doesn't handle comments in this manner, so ensure that
120 # if we started with this 'type' that we remove it before
121 # attempting to load it as json (which the jsonpatch library will
122 # attempt to do).
119 payload = payload.lstrip()123 payload = payload.lstrip()
120 if payload.lower().startswith(JSONP_PREFIX):124 payload = util.strip_prefix_suffix(payload, prefix=JSONP_PREFIX)
121 # JSON doesn't handle comments in this manner, so ensure that
122 # if we started with this 'type' that we remove it before
123 # attempting to load it as json (which the jsonpatch library will
124 # attempt to do).
125 payload = payload[JSONP_PREFIX:]
126 patch = jsonpatch.JsonPatch.from_string(payload)125 patch = jsonpatch.JsonPatch.from_string(payload)
127 LOG.debug("Merging by applying json patch %s", patch)126 LOG.debug("Merging by applying json patch %s", patch)
128 self.cloud_buf = patch.apply(self.cloud_buf, in_place=False)127 self.cloud_buf = patch.apply(self.cloud_buf, in_place=False)
129128
=== 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 @@
6import os6import os
77
8from email.mime.base import MIMEBase8from email.mime.base import MIMEBase
9from email.mime.multipart import MIMEMultipart
910
10from cloudinit import handlers11from cloudinit import handlers
11from cloudinit import helpers as c_helpers12from cloudinit import helpers as c_helpers
@@ -50,6 +51,64 @@
50 self._log.addHandler(self._log_handler)51 self._log.addHandler(self._log_handler)
51 return log_file52 return log_file
5253
54 def test_simple_jsonp(self):
55 blob = '''
56#cloud-config-jsonp
57[
58 { "op": "add", "path": "/baz", "value": "qux" },
59 { "op": "add", "path": "/bar", "value": "qux2" }
60]
61'''
62
63 ci = stages.Init()
64 ci.datasource = FakeDataSource(blob)
65 new_root = self.makeDir()
66 self.patchUtils(new_root)
67 self.patchOS(new_root)
68 ci.fetch()
69 ci.consume_userdata()
70 cc_contents = util.load_file(ci.paths.get_ipath("cloud_config"))
71 cc = util.load_yaml(cc_contents)
72 self.assertEquals(2, len(cc))
73 self.assertEquals('qux', cc['baz'])
74 self.assertEquals('qux2', cc['bar'])
75
76 def test_mixed_cloud_config(self):
77 blob_cc = '''
78#cloud-config
79a: b
80c: d
81'''
82 message_cc = MIMEBase("text", "cloud-config")
83 message_cc.set_payload(blob_cc)
84
85 blob_jp = '''
86#cloud-config-jsonp
87[
88 { "op": "replace", "path": "/a", "value": "c" },
89 { "op": "remove", "path": "/c" }
90]
91'''
92
93 message_jp = MIMEBase('text', "cloud-config-jsonp")
94 message_jp.set_payload(blob_jp)
95
96 message = MIMEMultipart()
97 message.attach(message_cc)
98 message.attach(message_jp)
99
100 ci = stages.Init()
101 ci.datasource = FakeDataSource(str(message))
102 new_root = self.makeDir()
103 self.patchUtils(new_root)
104 self.patchOS(new_root)
105 ci.fetch()
106 ci.consume_userdata()
107 cc_contents = util.load_file(ci.paths.get_ipath("cloud_config"))
108 cc = util.load_yaml(cc_contents)
109 self.assertEquals(1, len(cc))
110 self.assertEquals('c', cc['a'])
111
53 def test_merging_cloud_config(self):112 def test_merging_cloud_config(self):
54 blob = '''113 blob = '''
55#cloud-config114#cloud-config