Claudiu Popa 50365affc1 Normalize the payload to bytes before delegating to userdatautils
userdatautils is expected to operate on bytes, but on Python 3,
email.message.Message.get_payload returns a string, not a byte string
as for Python 2. This patch also includes a change to parthandler.py,
where we're using encoding.write_file instead of writing the file manually,
since encoding.write_file knows how to normalize its arguments properly.

Change-Id: Ic0b75a18d0a13bdf1dcb5b0b6430a5ac8f99ab74
2015-09-10 12:56:19 +03:00

56 lines
2.1 KiB
Python

# Copyright 2013 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import unittest
try:
import unittest.mock as mock
except ImportError:
import mock
from cloudbaseinit.plugins.common.userdataplugins import parthandler
class PartHandlerPluginTests(unittest.TestCase):
def setUp(self):
self._parthandler = parthandler.PartHandlerPlugin()
@mock.patch('cloudbaseinit.utils.encoding.write_file')
@mock.patch('tempfile.gettempdir')
@mock.patch('cloudbaseinit.utils.classloader.ClassLoader.load_module')
def test_process(self, mock_load_module, mock_gettempdir,
mock_write_file):
mock_part = mock.MagicMock()
mock_part_handler = mock.MagicMock()
mock_part.get_filename.return_value = 'fake_name'
mock_gettempdir.return_value = 'fake_directory'
mock_load_module.return_value = mock_part_handler
mock_part_handler.list_types.return_value = ['fake part']
response = self._parthandler.process(mock_part)
mock_part.get_filename.assert_called_once_with()
part_handler_path = os.path.join(mock_gettempdir.return_value,
mock_part.get_filename.return_value)
mock_write_file.assert_called_once_with(
part_handler_path, mock_part.get_payload.return_value)
mock_load_module.assert_called_once_with(os.path.join(
'fake_directory', 'fake_name'))
mock_part_handler.list_types.assert_called_once_with()
self.assertEqual({'fake part': mock_part_handler.handle_part},
response)