Merge pull request #49 from trobert2/configdrive_tests_and_factory_tests

Adds configdrive factory test configdrive load test
This commit is contained in:
Alessandro Pilotti 2014-02-27 22:09:54 +02:00
commit a9bc0cd7f0
4 changed files with 76 additions and 7 deletions

View File

@ -1,6 +1,6 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 Cloudbase Solutions Srl
# Copyright 2014 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

View File

@ -0,0 +1,44 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2014 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 mock
import sys
import unittest
from cloudbaseinit.metadata.services.osconfigdrive import factory
class ClassloaderTest(unittest.TestCase):
@mock.patch('cloudbaseinit.utils.classloader.ClassLoader.load_class')
def _test_get_config_drive_manager(self, mock_load_class, platform):
sys.platform = platform
if platform is not "win32":
self.assertRaises(NotImplementedError,
factory.get_config_drive_manager)
else:
response = factory.get_config_drive_manager()
mock_load_class.assert_called_once_with(
'cloudbaseinit.metadata.services.osconfigdrive.'
'windows.WindowsConfigDriveManager')
self.assertIsNotNone(response)
def test_get_config_drive_manager(self):
self._test_get_config_drive_manager(platform="win32")
def test_get_config_drive_manager_exception(self):
self._test_get_config_drive_manager(platform="other")

View File

@ -19,6 +19,7 @@ import mock
import os
import sys
import unittest
import uuid
from oslo.config import cfg
@ -43,7 +44,28 @@ class ConfigDriveServiceTest(unittest.TestCase):
self._config_drive = configdrive.ConfigDriveService()
def tearDown(self):
reload(sys)
reload(uuid)
@mock.patch('tempfile.gettempdir')
@mock.patch('cloudbaseinit.metadata.services.osconfigdrive.factory.'
'get_config_drive_manager')
def test_load(self, mock_get_config_drive_manager,
mock_gettempdir):
mock_manager = mock.MagicMock()
mock_manager.get_config_drive_files.return_value = True
mock_get_config_drive_manager.return_value = mock_manager
mock_gettempdir.return_value = 'fake'
uuid.uuid4 = mock.MagicMock(return_value='fake_id')
fake_path = os.path.join('fake', str('fake_id'))
response = self._config_drive.load()
mock_gettempdir.assert_called_once_with()
mock_get_config_drive_manager.assert_called_once_with()
mock_manager.get_config_drive_files.assert_called_once_with(
fake_path, CONF.config_drive_raw_hhd, CONF.config_drive_cdrom)
self.assertEqual(response, True)
self.assertEqual(self._config_drive._metadata_path, fake_path)
@mock.patch('os.path.normpath')
@mock.patch('os.path.join')

View File

@ -31,6 +31,9 @@ class UserDataUtilsTest(unittest.TestCase):
self.fake_data = fake_json_response.get_fake_metadata_json(
'2013-04-04')
def tearDown(self):
reload(uuid)
@mock.patch('re.search')
@mock.patch('tempfile.gettempdir')
@mock.patch('os.remove')
@ -55,26 +58,26 @@ class UserDataUtilsTest(unittest.TestCase):
side_effect = [match_instance]
number_of_calls = 1
extension = '.cmd'
args = [path+extension]
args = [path + extension]
shell = True
elif fake_user_data == '^#!/usr/bin/env\spython\s':
side_effect = [None, match_instance]
number_of_calls = 2
extension = '.py'
args = ['python.exe', path+extension]
args = ['python.exe', path + extension]
shell = False
elif fake_user_data == '#!':
side_effect = [None, None, match_instance]
number_of_calls = 3
extension = '.sh'
args = ['bash.exe', path+extension]
args = ['bash.exe', path + extension]
shell = False
elif fake_user_data == '#ps1\s':
side_effect = [None, None, None, match_instance]
number_of_calls = 4
extension = '.ps1'
args = ['powershell.exe', '-ExecutionPolicy', 'RemoteSigned',
'-NonInteractive', '-File', path+extension]
'-NonInteractive', '-File', path + extension]
shell = False
else:
side_effect = [None, None, None, None, match_instance]
@ -87,7 +90,7 @@ class UserDataUtilsTest(unittest.TestCase):
'powershell.exe'),
'-ExecutionPolicy',
'RemoteSigned', '-NonInteractive', '-File',
path+extension]
path + extension]
mock_path_isdir.return_value = True
else:
mock_path_isdir.return_value = False