|
| 1 | +"""Tests for handling of userdata within cloud init""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import StringIO |
| 5 | + |
| 6 | +from email.mime.base import MIMEBase |
| 7 | + |
| 8 | +from mocker import MockerTestCase |
| 9 | + |
| 10 | +import cloudinit |
| 11 | +from cloudinit.DataSource import DataSource |
| 12 | + |
| 13 | + |
| 14 | +instance_id = "i-testing" |
| 15 | + |
| 16 | + |
| 17 | +class FakeDataSource(DataSource): |
| 18 | + |
| 19 | + def __init__(self, userdata): |
| 20 | + self.metadata = {'instance-id': instance_id} |
| 21 | + self.userdata_raw = userdata |
| 22 | + |
| 23 | + |
| 24 | +class TestConsumeUserData(MockerTestCase): |
| 25 | + |
| 26 | + def setUp(self): |
| 27 | + self.mock_write = self.mocker.replace("cloudinit.util.write_file", |
| 28 | + passthrough=False) |
| 29 | + self.mock_write(self.get_ipath("cloud_config"), "", 0600) |
| 30 | + self.capture_log() |
| 31 | + |
| 32 | + def tearDown(self): |
| 33 | + self._log.removeHandler(self._log_handler) |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def get_ipath(name): |
| 37 | + return "%s/instances/%s%s" % (cloudinit.varlibdir, instance_id, |
| 38 | + cloudinit.pathmap[name]) |
| 39 | + |
| 40 | + def capture_log(self): |
| 41 | + self.log_file = StringIO.StringIO() |
| 42 | + self._log_handler = logging.StreamHandler(self.log_file) |
| 43 | + self._log_handler.setLevel(logging.DEBUG) |
| 44 | + self._log = logging.getLogger(cloudinit.logger_name) |
| 45 | + self._log.addHandler(self._log_handler) |
| 46 | + |
| 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""" |
| 69 | + script = "#!/bin/sh\necho hello\n" |
| 70 | + outpath = cloudinit.get_ipath_cur("scripts") + "/part-001" |
| 71 | + self.mock_write(outpath, script, 0700) |
| 72 | + self.mocker.replay() |
| 73 | + ci = cloudinit.CloudInit() |
| 74 | + ci.datasource = FakeDataSource(script) |
| 75 | + ci.consume_userdata() |
| 76 | + self.assertEqual("", self.log_file.getvalue()) |
| 77 | + |
| 78 | + def test_mime_text_x_shellscript(self): |
| 79 | + """Mime message of type text/x-shellscript is treated as script""" |
| 80 | + script = "#!/bin/sh\necho hello\n" |
| 81 | + outpath = cloudinit.get_ipath_cur("scripts") + "/part-001" |
| 82 | + self.mock_write(outpath, script, 0700) |
| 83 | + self.mocker.replay() |
| 84 | + ci = cloudinit.CloudInit() |
| 85 | + message = MIMEBase("text", "x-shellscript") |
| 86 | + message.set_payload(script) |
| 87 | + ci.datasource = FakeDataSource(message.as_string()) |
| 88 | + ci.consume_userdata() |
| 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\necho 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