Files
cloudbase-init/cloudbaseinit/tests/plugins/windows/userdata_plugins/test_shellscript.py
2014-01-19 22:12:01 +02:00

85 lines
3.2 KiB
Python

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# 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 importlib
import mock
import os
import unittest
from cloudbaseinit.openstack.common import cfg
from cloudbaseinit.plugins.windows import userdata_plugins
#the name of the module includes "-", importlib.import_module is needed:
shellscript = importlib.import_module("cloudbaseinit.plugins.windows"
".userdata-plugins.shellscript")
CONF = cfg.CONF
class ShellScriptHandlerTests(unittest.TestCase):
def setUp(self):
parent_set = userdata_plugins.PluginSet('fake_path')
self._shellscript = shellscript.ShellScriptHandler(parent_set)
@mock.patch('cloudbaseinit.osutils.factory.OSUtilsFactory.get_os_utils')
@mock.patch('tempfile.gettempdir')
def _test_process(self, mock_gettempdir, mock_get_os_utils, filename,
exception=False):
fake_dir_path = os.path.join("fake", "dir")
mock_osutils = mock.MagicMock()
mock_part = mock.MagicMock()
mock_part.get_filename.return_value = filename
mock_gettempdir.return_value = fake_dir_path
mock_get_os_utils.return_value = mock_osutils
if exception:
mock_osutils.execute_process.side_effect = [Exception]
with mock.patch("cloudbaseinit.plugins.windows.userdata-plugins."
"shellscript.open", mock.mock_open(), create=True):
response = self._shellscript.process(mock_part)
mock_part.get_filename.assert_called_once_with()
mock_gettempdir.assert_called_once_with()
if filename.endswith(".cmd"):
mock_osutils.execute_process.assert_called_with(
[os.path.join(fake_dir_path, filename)], True)
elif filename.endswith(".sh"):
mock_osutils.execute_process.assert_called_with(
['bash.exe', os.path.join(fake_dir_path, filename)], False)
elif filename.endswith(".ps1"):
mock_osutils.execute_process.assert_called_with(
['powershell.exe', '-ExecutionPolicy', 'RemoteSigned',
'-NonInteractive', os.path.join(fake_dir_path, filename)],
False)
self.assertFalse(response)
def test_process_cmd(self):
self._test_process(filename='fake.cmd')
def test_process_sh(self):
self._test_process(filename='fake.cmd')
def test_process_ps1(self):
self._test_process(filename='fake.cmd')
def test_process_other(self):
self._test_process(filename='fake.other')
def test_process_exception(self):
self._test_process(filename='fake.cmd', exception=True)