Merge "Improve code coverage"
This commit is contained in:
47
cloudbaseinit/tests/utils/test_classloader.py
Normal file
47
cloudbaseinit/tests/utils/test_classloader.py
Normal file
@@ -0,0 +1,47 @@
|
||||
# Copyright 2016 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 tempfile
|
||||
import unittest
|
||||
|
||||
try:
|
||||
import unittest.mock as mock
|
||||
except ImportError:
|
||||
import mock
|
||||
|
||||
from cloudbaseinit.utils import classloader
|
||||
|
||||
|
||||
def _create_tempfile():
|
||||
fd, tmp = tempfile.mkstemp()
|
||||
os.close(fd)
|
||||
return tmp
|
||||
|
||||
|
||||
class ClassLoaderTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self._loader = classloader.ClassLoader()
|
||||
|
||||
@mock.patch('imp.load_compiled')
|
||||
@mock.patch('imp.load_source')
|
||||
def test_load_module_py(self, mock_source, mock_compiled):
|
||||
mock_py = os.path.join(_create_tempfile(), "mock.py")
|
||||
mock_pyc = os.path.join(_create_tempfile(), "mock.pyc")
|
||||
mock_source.return_value = mock_compiled.return_value = None
|
||||
result_module_py = self._loader.load_module(mock_py)
|
||||
result_module_pyc = self._loader.load_module(mock_pyc)
|
||||
self.assertIsNone(result_module_py)
|
||||
self.assertIsNone(result_module_pyc)
|
||||
40
cloudbaseinit/tests/utils/test_crypt.py
Normal file
40
cloudbaseinit/tests/utils/test_crypt.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# Copyright 2016 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 unittest
|
||||
|
||||
from cloudbaseinit.utils import crypt
|
||||
|
||||
|
||||
class TestOpenSSLException(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self._openssl = crypt.OpenSSLException()
|
||||
|
||||
def test_get_openssl_error_msg(self):
|
||||
expected_error_msg = u'error:00000000:lib(0):func(0):reason(0)'
|
||||
error_msg = self._openssl._get_openssl_error_msg()
|
||||
self.assertEqual(expected_error_msg, error_msg)
|
||||
|
||||
|
||||
class TestCryptManager(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self._crypt_manager = crypt.CryptManager()
|
||||
|
||||
def test_load_ssh_rsa_public_key_invalid(self):
|
||||
ssh_pub_key = "ssh"
|
||||
exc = Exception
|
||||
self.assertRaises(exc, self._crypt_manager.load_ssh_rsa_public_key,
|
||||
ssh_pub_key)
|
||||
@@ -92,6 +92,14 @@ class DHCPUtilsTests(unittest.TestCase):
|
||||
self._test_parse_dhcp_reply(message_type=2, id_reply=9999,
|
||||
equals_cookie=True)
|
||||
|
||||
def test_parse_dhcp_reply_false(self):
|
||||
self._test_parse_dhcp_reply(message_type=2, id_reply=111,
|
||||
equals_cookie=True)
|
||||
|
||||
def test_parse_dhcp_reply_cookie_false(self):
|
||||
self._test_parse_dhcp_reply(message_type=2, id_reply=9999,
|
||||
equals_cookie=False)
|
||||
|
||||
def test_parse_dhcp_reply_other_message_type(self):
|
||||
self._test_parse_dhcp_reply(message_type=3, id_reply=9999,
|
||||
equals_cookie=True)
|
||||
@@ -161,6 +169,12 @@ class DHCPUtilsTests(unittest.TestCase):
|
||||
mock_socket().close.assert_called_once_with()
|
||||
self.assertEqual('fake replied options', response)
|
||||
|
||||
@mock.patch('cloudbaseinit.utils.dhcp._bind_dhcp_client_socket')
|
||||
def test_get_dhcp_options_timeout(self, mock_client_socket):
|
||||
mock_client_socket.side_effect = [socket.timeout]
|
||||
dhcp.get_dhcp_options(dhcp_host='fake host',
|
||||
requested_options=['fake option'])
|
||||
|
||||
def test__bind_dhcp_client_socket_bind_succeeds(self):
|
||||
mock_socket = mock.Mock()
|
||||
dhcp._bind_dhcp_client_socket(mock_socket, 0, 0)
|
||||
|
||||
@@ -24,6 +24,7 @@ from cloudbaseinit.utils import encoding
|
||||
class TestEncoding(unittest.TestCase):
|
||||
|
||||
def test_get_as_string(self):
|
||||
self.assertIsNone(encoding.get_as_string(None))
|
||||
content_map = [
|
||||
("data", "data"),
|
||||
(b"data", "data"),
|
||||
@@ -40,7 +41,8 @@ class TestEncoding(unittest.TestCase):
|
||||
(("w", "r"), "my test\ndata\n\n", False),
|
||||
(("wb", "rb"), "\r\n".join((chr(x) for x in
|
||||
(32, 125, 0))).encode(), False),
|
||||
(("wb", "rb"), "my test\ndata\n\n", True)
|
||||
(("wb", "rb"), "my test\ndata\n\n", True),
|
||||
(("wb", "rb"), u"my test\n data", True)
|
||||
]
|
||||
with testutils.create_tempdir() as temp:
|
||||
fd, path = tempfile.mkstemp(dir=temp)
|
||||
|
||||
@@ -29,6 +29,11 @@ CONF = cloudbaseinit_conf.CONF
|
||||
|
||||
class NetworkUtilsTest(unittest.TestCase):
|
||||
|
||||
@mock.patch('six.moves.urllib.request.urlopen')
|
||||
def test_check_url(self, mock_url_open):
|
||||
mock_url_open.return_value = None
|
||||
self.assertTrue(network.check_url("fake_url"))
|
||||
|
||||
@mock.patch('sys.platform', new='win32')
|
||||
@mock.patch('cloudbaseinit.osutils.factory.get_os_utils')
|
||||
@mock.patch('six.moves.urllib.parse.urlparse')
|
||||
|
||||
Reference in New Issue
Block a user