33import logging
44import StringIO
55
6+ from email .mime .base import MIMEBase
7+
68from mocker import MockerTestCase
79
810import cloudinit
@@ -42,7 +44,28 @@ def capture_log(self):
4244 self ._log = logging .getLogger (cloudinit .logger_name )
4345 self ._log .addHandler (self ._log_handler )
4446
45- def test_script (self ):
47+ def test_unhandled_type_warning (self ):
48+ """Raw text without magic is ignored but shows warning"""
49+ self .mocker .replay ()
50+ ci = cloudinit .CloudInit ()
51+ ci .datasource = FakeDataSource ("arbitrary text\n " )
52+ ci .consume_userdata ()
53+ self .assertEqual (
54+ "Unhandled non-multipart userdata starting 'arbitrary text...'\n " ,
55+ self .log_file .getvalue ())
56+
57+ def test_mime_text_plain (self ):
58+ """Mime message of type text/plain is ignored without warning"""
59+ self .mocker .replay ()
60+ ci = cloudinit .CloudInit ()
61+ message = MIMEBase ("text" , "plain" )
62+ message .set_payload ("Just text" )
63+ ci .datasource = FakeDataSource (message .as_string ())
64+ ci .consume_userdata ()
65+ self .assertEqual ("" , self .log_file .getvalue ())
66+
67+ def test_shellscript (self ):
68+ """Raw text starting #!/bin/sh is treated as script"""
4669 script = "#!/bin/sh\n echo hello\n "
4770 outpath = cloudinit .get_ipath_cur ("scripts" ) + "/part-001"
4871 self .mock_write (outpath , script , 0700 )
@@ -52,9 +75,28 @@ def test_script(self):
5275 ci .consume_userdata ()
5376 self .assertEqual ("" , self .log_file .getvalue ())
5477
55- def test_unhandled_type_warning (self ):
78+ def test_mime_text_x_shellscript (self ):
79+ """Mime message of type text/x-shellscript is treated as script"""
80+ script = "#!/bin/sh\n echo hello\n "
81+ outpath = cloudinit .get_ipath_cur ("scripts" ) + "/part-001"
82+ self .mock_write (outpath , script , 0700 )
5683 self .mocker .replay ()
5784 ci = cloudinit .CloudInit ()
58- ci .datasource = FakeDataSource ("arbitrary text\n " )
85+ message = MIMEBase ("text" , "x-shellscript" )
86+ message .set_payload (script )
87+ ci .datasource = FakeDataSource (message .as_string ())
5988 ci .consume_userdata ()
60- self .assertIn ("Unhandled userdata part" , self .log_file .getvalue ())
89+ self .assertEqual ("" , self .log_file .getvalue ())
90+
91+ def test_mime_text_plain_shell (self ):
92+ """Mime type text/plain starting #!/bin/sh is treated as script"""
93+ script = "#!/bin/sh\n echo hello\n "
94+ outpath = cloudinit .get_ipath_cur ("scripts" ) + "/part-001"
95+ self .mock_write (outpath , script , 0700 )
96+ self .mocker .replay ()
97+ ci = cloudinit .CloudInit ()
98+ message = MIMEBase ("text" , "plain" )
99+ message .set_payload (script )
100+ ci .datasource = FakeDataSource (message .as_string ())
101+ ci .consume_userdata ()
102+ self .assertEqual ("" , self .log_file .getvalue ())
0 commit comments