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

Proposed by Joshua Harlow
Status: Merged
Merged at revision: 844
Proposed branch: lp:~harlowja/cloud-init/mime-gzip-test
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 101 lines (+48/-4)
1 file modified
tests/unittests/test_userdata.py (+48/-4)
To merge this branch: bzr merge lp:~harlowja/cloud-init/mime-gzip-test
Reviewer Review Type Date Requested Status
cloud-init Commiters Pending
Review via email: mp+176614@code.launchpad.net

Description of the change

Test for mime gzipped segments to ensure decompression is occuring.

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 '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 07:36:31 +0000
@@ -2,10 +2,14 @@
22
3import StringIO3import StringIO
44
5import gzip
5import logging6import logging
7import mocker
6import os8import os
79
8from email.mime.base import MIMEBase10from email.mime.base import MIMEBase
11from email.mime.multipart import MIMEMultipart
12from email.mime.application import MIMEApplication
913
10from cloudinit import handlers14from cloudinit import handlers
11from cloudinit import helpers as c_helpers15from cloudinit import helpers as c_helpers
@@ -118,7 +122,7 @@
118 ci.datasource = FakeDataSource(data)122 ci.datasource = FakeDataSource(data)
119123
120 mock_write = self.mocker.replace("cloudinit.util.write_file",124 mock_write = self.mocker.replace("cloudinit.util.write_file",
121 passthrough=False)125 passthrough=False)
122 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)126 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)
123 self.mocker.replay()127 self.mocker.replay()
124128
@@ -129,6 +133,46 @@
129 "Unhandled non-multipart (text/x-not-multipart) userdata:",133 "Unhandled non-multipart (text/x-not-multipart) userdata:",
130 log_file.getvalue())134 log_file.getvalue())
131135
136 def test_mime_gzip_compressed(self):
137 """Tests that individual message gzip encoding works."""
138
139 def gzip_part(text):
140 contents = StringIO.StringIO()
141 f = gzip.GzipFile(fileobj=contents, mode='w')
142 f.write(str(text))
143 f.flush()
144 f.close()
145 return MIMEApplication(contents.getvalue(), 'gzip')
146
147 base_content1 = '''
148#cloud-config
149a: 2
150'''
151
152 base_content2 = '''
153#cloud-config
154b: 3
155c: 4
156'''
157
158 message = MIMEMultipart('test')
159 message.attach(gzip_part(base_content1))
160 message.attach(gzip_part(base_content2))
161 ci = stages.Init()
162 ci.datasource = FakeDataSource(str(message))
163 new_root = self.makeDir()
164 self.patchUtils(new_root)
165 self.patchOS(new_root)
166 ci.fetch()
167 ci.consume_userdata()
168 contents = util.load_file(ci.paths.get_ipath("cloud_config"))
169 contents = util.load_yaml(contents)
170 self.assertTrue(isinstance(contents, dict))
171 self.assertEquals(3, len(contents))
172 self.assertEquals(2, contents['a'])
173 self.assertEquals(3, contents['b'])
174 self.assertEquals(4, contents['c'])
175
132 def test_mime_text_plain(self):176 def test_mime_text_plain(self):
133 """Mime message of type text/plain is ignored but shows warning."""177 """Mime message of type text/plain is ignored but shows warning."""
134 ci = stages.Init()178 ci = stages.Init()
@@ -137,7 +181,7 @@
137 ci.datasource = FakeDataSource(message.as_string())181 ci.datasource = FakeDataSource(message.as_string())
138182
139 mock_write = self.mocker.replace("cloudinit.util.write_file",183 mock_write = self.mocker.replace("cloudinit.util.write_file",
140 passthrough=False)184 passthrough=False)
141 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)185 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)
142 self.mocker.replay()186 self.mocker.replay()
143187
@@ -156,7 +200,7 @@
156200
157 outpath = os.path.join(ci.paths.get_ipath_cur("scripts"), "part-001")201 outpath = os.path.join(ci.paths.get_ipath_cur("scripts"), "part-001")
158 mock_write = self.mocker.replace("cloudinit.util.write_file",202 mock_write = self.mocker.replace("cloudinit.util.write_file",
159 passthrough=False)203 passthrough=False)
160 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)204 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)
161 mock_write(outpath, script, 0700)205 mock_write(outpath, script, 0700)
162 self.mocker.replay()206 self.mocker.replay()
@@ -176,7 +220,7 @@
176220
177 outpath = os.path.join(ci.paths.get_ipath_cur("scripts"), "part-001")221 outpath = os.path.join(ci.paths.get_ipath_cur("scripts"), "part-001")
178 mock_write = self.mocker.replace("cloudinit.util.write_file",222 mock_write = self.mocker.replace("cloudinit.util.write_file",
179 passthrough=False)223 passthrough=False)
180 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)224 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)
181 mock_write(outpath, script, 0700)225 mock_write(outpath, script, 0700)
182 self.mocker.replay()226 self.mocker.replay()