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
1=== modified file 'tests/unittests/test_userdata.py'
2--- tests/unittests/test_userdata.py 2013-05-10 05:34:31 +0000
3+++ tests/unittests/test_userdata.py 2013-07-24 07:36:31 +0000
4@@ -2,10 +2,14 @@
5
6 import StringIO
7
8+import gzip
9 import logging
10+import mocker
11 import os
12
13 from email.mime.base import MIMEBase
14+from email.mime.multipart import MIMEMultipart
15+from email.mime.application import MIMEApplication
16
17 from cloudinit import handlers
18 from cloudinit import helpers as c_helpers
19@@ -118,7 +122,7 @@
20 ci.datasource = FakeDataSource(data)
21
22 mock_write = self.mocker.replace("cloudinit.util.write_file",
23- passthrough=False)
24+ passthrough=False)
25 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)
26 self.mocker.replay()
27
28@@ -129,6 +133,46 @@
29 "Unhandled non-multipart (text/x-not-multipart) userdata:",
30 log_file.getvalue())
31
32+ def test_mime_gzip_compressed(self):
33+ """Tests that individual message gzip encoding works."""
34+
35+ def gzip_part(text):
36+ contents = StringIO.StringIO()
37+ f = gzip.GzipFile(fileobj=contents, mode='w')
38+ f.write(str(text))
39+ f.flush()
40+ f.close()
41+ return MIMEApplication(contents.getvalue(), 'gzip')
42+
43+ base_content1 = '''
44+#cloud-config
45+a: 2
46+'''
47+
48+ base_content2 = '''
49+#cloud-config
50+b: 3
51+c: 4
52+'''
53+
54+ message = MIMEMultipart('test')
55+ message.attach(gzip_part(base_content1))
56+ message.attach(gzip_part(base_content2))
57+ ci = stages.Init()
58+ ci.datasource = FakeDataSource(str(message))
59+ new_root = self.makeDir()
60+ self.patchUtils(new_root)
61+ self.patchOS(new_root)
62+ ci.fetch()
63+ ci.consume_userdata()
64+ contents = util.load_file(ci.paths.get_ipath("cloud_config"))
65+ contents = util.load_yaml(contents)
66+ self.assertTrue(isinstance(contents, dict))
67+ self.assertEquals(3, len(contents))
68+ self.assertEquals(2, contents['a'])
69+ self.assertEquals(3, contents['b'])
70+ self.assertEquals(4, contents['c'])
71+
72 def test_mime_text_plain(self):
73 """Mime message of type text/plain is ignored but shows warning."""
74 ci = stages.Init()
75@@ -137,7 +181,7 @@
76 ci.datasource = FakeDataSource(message.as_string())
77
78 mock_write = self.mocker.replace("cloudinit.util.write_file",
79- passthrough=False)
80+ passthrough=False)
81 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)
82 self.mocker.replay()
83
84@@ -156,7 +200,7 @@
85
86 outpath = os.path.join(ci.paths.get_ipath_cur("scripts"), "part-001")
87 mock_write = self.mocker.replace("cloudinit.util.write_file",
88- passthrough=False)
89+ passthrough=False)
90 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)
91 mock_write(outpath, script, 0700)
92 self.mocker.replay()
93@@ -176,7 +220,7 @@
94
95 outpath = os.path.join(ci.paths.get_ipath_cur("scripts"), "part-001")
96 mock_write = self.mocker.replace("cloudinit.util.write_file",
97- passthrough=False)
98+ passthrough=False)
99 mock_write(ci.paths.get_ipath("cloud_config"), "", 0600)
100 mock_write(outpath, script, 0700)
101 self.mocker.replay()