Fixes various tests for Python 3.x support

This commit is contained in:
Robert Tingirica 2014-09-08 18:17:01 +03:00
parent fd8beed0ce
commit b42f4c2890
9 changed files with 53 additions and 40 deletions

View File

@ -17,7 +17,6 @@
import importlib
import mock
import os
import sys
import unittest
import uuid

View File

@ -17,7 +17,6 @@
import importlib
import mock
import re
import sys
import unittest
from oslo.config import cfg

View File

@ -29,11 +29,13 @@ class NTPClientPluginTests(unittest.TestCase):
def setUp(self):
self._ntpclient = ntpclient.NTPClientPlugin()
def _test_check_w32time_svc_status(self, start_mode, fail_service_start):
@mock.patch('time.sleep')
def _test_check_w32time_svc_status(self, mock_sleep, start_mode,
fail_service_start):
# TODO(rtingirica): use _W32TIME_SERVICE when it will be moved outside
# of method declaration
mock_osutils = mock.MagicMock()
mock_osutils.SERVICE_START_MODE_AUTOMATIC = "automatic"
mock_osutils.SERVICE_START_MODE_AUTOMATIC = "Automatic"
mock_osutils.SERVICE_STATUS_RUNNING = "running"
mock_osutils.SERVICE_STATUS_STOPPED = "stopped"
mock_osutils.get_service_start_mode.return_value = start_mode
@ -47,17 +49,22 @@ class NTPClientPluginTests(unittest.TestCase):
else:
mock_osutils.get_service_status.side_effect = [
"stopped", mock_osutils.SERVICE_STATUS_RUNNING]
self._ntpclient._check_w32time_svc_status(osutils=mock_osutils)
mock_osutils.get_service_start_mode.assert_called_once_with(
"w32time")
if start_mode != mock_osutils.SERVICE_START_MODE_AUTOMATIC:
mock_osutils.set_service_start_mode.assert_called_once_with(
"w32time", mock_osutils.SERVICE_START_MODE_AUTOMATIC)
ntpclient._W32TIME_SERVICE,
mock_osutils.SERVICE_START_MODE_AUTOMATIC)
mock_osutils.start_service.assert_called_once_with("w32time")
mock_sleep.assert_called_once_with(1)
mock_osutils.start_service.assert_called_once_with(
ntpclient._W32TIME_SERVICE)
mock_osutils.get_service_status.assert_called_with("w32time")
mock_osutils.get_service_start_mode.assert_called_once_with(
ntpclient._W32TIME_SERVICE)
mock_osutils.get_service_status.assert_called_with(
ntpclient._W32TIME_SERVICE)
def test_check_w32time_svc_status_other_start_mode(self):
self._test_check_w32time_svc_status(start_mode="not automatic",

View File

@ -36,43 +36,54 @@ class UserDataPluginTest(unittest.TestCase):
@mock.patch('cloudbaseinit.plugins.windows.userdata.UserDataPlugin'
'._process_user_data')
def _test_execute(self, mock_process_user_data, user_data_in,
user_data_out=None):
@mock.patch('cloudbaseinit.plugins.windows.userdata.UserDataPlugin'
'._check_gzip_compression')
def _test_execute(self, mock_check_gzip_compression,
mock_process_user_data, ret_val):
mock_service = mock.MagicMock()
mock_service.get_user_data.side_effect = [user_data_in]
mock_service.get_user_data.side_effect = [ret_val]
response = self._userdata.execute(service=mock_service,
shared_data=None)
mock_service.get_user_data.assert_called_once_with()
if user_data_in is metadata_services_base.NotExistingMetadataException:
if ret_val is metadata_services_base.NotExistingMetadataException:
self.assertEqual(response, (base.PLUGIN_EXECUTION_DONE, False))
elif user_data_in is None:
elif ret_val is None:
self.assertEqual(response, (base.PLUGIN_EXECUTION_DONE, False))
else:
if user_data_out:
user_data = user_data_out
else:
user_data = user_data_in
mock_process_user_data.assert_called_once_with(user_data)
self.assertEqual(response, mock_process_user_data())
mock_check_gzip_compression.assert_called_once_with(ret_val)
mock_process_user_data.assert_called_once_with(
mock_check_gzip_compression.return_value)
self.assertEqual(response, mock_process_user_data.return_value)
def test_execute(self):
self._test_execute(user_data_in='fake data')
self._test_execute(ret_val=mock.sentinel.fake_data)
def test_execute_gzipped_user_data(self):
fake_user_data_in = (b'\x1f\x8b\x08\x00\x8c\xdc\x14S\x02\xffKK'
b'\xccNUHI,I\x04\x00(\xc9\xcfI\t\x00\x00\x00')
fake_user_data_out = b'fake data'
self._test_execute(user_data_in=fake_user_data_in,
user_data_out=fake_user_data_out)
def test_execute_no_data(self):
self._test_execute(ret_val=None)
def test_execute_NotExistingMetadataException(self):
self._test_execute(
user_data_in=metadata_services_base.NotExistingMetadataException)
ret_val=metadata_services_base.NotExistingMetadataException)
def test_execute_not_user_data(self):
self._test_execute(user_data_in=None)
self._test_execute(ret_val=None)
@mock.patch('io.BytesIO')
@mock.patch('gzip.GzipFile')
def test_check_gzip_compression(self, mock_GzipFile, mock_BytesIO):
fake_userdata = b'\x1f\x8b'
fake_userdata += self._userdata._GZIP_MAGIC_NUMBER
response = self._userdata._check_gzip_compression(fake_userdata)
mock_BytesIO.assert_called_once_with(fake_userdata)
mock_GzipFile.assert_called_once_with(
fileobj=mock_BytesIO.return_value, mode='rb')
data = mock_GzipFile().__enter__().read.return_value
self.assertEqual(data, response)
@mock.patch('email.message_from_string')
def test_parse_mime(self, mock_message_from_string):

View File

@ -16,7 +16,6 @@
import importlib
import mock
import sys
import unittest
from cloudbaseinit.plugins import base

View File

@ -16,7 +16,6 @@
import importlib
import mock
import sys
import unittest
from cloudbaseinit.plugins import base

View File

@ -105,10 +105,10 @@ class InitManagerTest(unittest.TestCase):
fake_name, status)
self.assertTrue(response)
def test_test_exec_plugin_execution_done(self):
def test_exec_plugin_execution_done(self):
self._test_exec_plugin(base.PLUGIN_EXECUTION_DONE)
def test_test_exec_plugin(self):
def test_exec_plugin(self):
self._test_exec_plugin(base.PLUGIN_EXECUTE_ON_NEXT_BOOT)
def _test_check_plugin_os_requirements(self, requirements):

View File

@ -364,10 +364,10 @@ class WinRMConfigTests(unittest.TestCase):
certificate='certificate', credSSP='credSSP',
cbt_hardening_level='cbt_hardening_level')
self.assertEqual(sorted(expected_find),
sorted(mock_tree.find.call_args_list))
self.assertEqual(sorted(expected_get_xml_bool),
sorted(mock_get_xml_bool.call_args_list))
self.assertEqual(sorted(mock_tree.find.call_args_list),
sorted(expected_find))
self.assertEqual(sorted(mock_get_xml_bool.call_args_list),
sorted(expected_get_xml_bool))
mock_get_wsman_session.assert_called_once_with()
mock_session.Get.assert_called_with(

View File

@ -103,7 +103,6 @@ class CryptoAPICertManagerTests(unittest.TestCase):
self.assertRaises(cryptoapi.CryptoAPIException,
self._x509._generate_key, 'fake container',
True)
mock_byref.assert_called_with(mock_HANDLE())
else:
self._x509._generate_key('fake container', True)
mock_CryptAcquireContext.assert_called_with(