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
1=== modified file 'cloudinit/handlers/cloud_config.py'
2--- cloudinit/handlers/cloud_config.py 2013-07-24 15:07:55 +0000
3+++ cloudinit/handlers/cloud_config.py 2013-07-24 15:41:39 +0000
4@@ -116,13 +116,12 @@
5 return (payload_yaml, all_mergers)
6
7 def _merge_patch(self, payload):
8+ # JSON doesn't handle comments in this manner, so ensure that
9+ # if we started with this 'type' that we remove it before
10+ # attempting to load it as json (which the jsonpatch library will
11+ # attempt to do).
12 payload = payload.lstrip()
13- if payload.lower().startswith(JSONP_PREFIX):
14- # JSON doesn't handle comments in this manner, so ensure that
15- # if we started with this 'type' that we remove it before
16- # attempting to load it as json (which the jsonpatch library will
17- # attempt to do).
18- payload = payload[JSONP_PREFIX:]
19+ payload = util.strip_prefix_suffix(payload, prefix=JSONP_PREFIX)
20 patch = jsonpatch.JsonPatch.from_string(payload)
21 LOG.debug("Merging by applying json patch %s", patch)
22 self.cloud_buf = patch.apply(self.cloud_buf, in_place=False)
23
24=== modified file 'tests/unittests/test_userdata.py'
25--- tests/unittests/test_userdata.py 2013-05-10 05:34:31 +0000
26+++ tests/unittests/test_userdata.py 2013-07-24 15:41:39 +0000
27@@ -6,6 +6,7 @@
28 import os
29
30 from email.mime.base import MIMEBase
31+from email.mime.multipart import MIMEMultipart
32
33 from cloudinit import handlers
34 from cloudinit import helpers as c_helpers
35@@ -50,6 +51,64 @@
36 self._log.addHandler(self._log_handler)
37 return log_file
38
39+ def test_simple_jsonp(self):
40+ blob = '''
41+#cloud-config-jsonp
42+[
43+ { "op": "add", "path": "/baz", "value": "qux" },
44+ { "op": "add", "path": "/bar", "value": "qux2" }
45+]
46+'''
47+
48+ ci = stages.Init()
49+ ci.datasource = FakeDataSource(blob)
50+ new_root = self.makeDir()
51+ self.patchUtils(new_root)
52+ self.patchOS(new_root)
53+ ci.fetch()
54+ ci.consume_userdata()
55+ cc_contents = util.load_file(ci.paths.get_ipath("cloud_config"))
56+ cc = util.load_yaml(cc_contents)
57+ self.assertEquals(2, len(cc))
58+ self.assertEquals('qux', cc['baz'])
59+ self.assertEquals('qux2', cc['bar'])
60+
61+ def test_mixed_cloud_config(self):
62+ blob_cc = '''
63+#cloud-config
64+a: b
65+c: d
66+'''
67+ message_cc = MIMEBase("text", "cloud-config")
68+ message_cc.set_payload(blob_cc)
69+
70+ blob_jp = '''
71+#cloud-config-jsonp
72+[
73+ { "op": "replace", "path": "/a", "value": "c" },
74+ { "op": "remove", "path": "/c" }
75+]
76+'''
77+
78+ message_jp = MIMEBase('text', "cloud-config-jsonp")
79+ message_jp.set_payload(blob_jp)
80+
81+ message = MIMEMultipart()
82+ message.attach(message_cc)
83+ message.attach(message_jp)
84+
85+ ci = stages.Init()
86+ ci.datasource = FakeDataSource(str(message))
87+ new_root = self.makeDir()
88+ self.patchUtils(new_root)
89+ self.patchOS(new_root)
90+ ci.fetch()
91+ ci.consume_userdata()
92+ cc_contents = util.load_file(ci.paths.get_ipath("cloud_config"))
93+ cc = util.load_yaml(cc_contents)
94+ self.assertEquals(1, len(cc))
95+ self.assertEquals('c', cc['a'])
96+
97 def test_merging_cloud_config(self):
98 blob = '''
99 #cloud-config