From b42f4c2890bcc25fa53de660493e0bd8c6e26e69 Mon Sep 17 00:00:00 2001 From: Robert Tingirica Date: Mon, 8 Sep 2014 18:17:01 +0300 Subject: [PATCH] Fixes various tests for Python 3.x support --- .../metadata/services/test_configdrive.py | 1 - .../plugins/windows/test_extendvolumes.py | 1 - .../tests/plugins/windows/test_ntpclient.py | 21 ++++--- .../tests/plugins/windows/test_userdata.py | 55 +++++++++++-------- .../windows/test_winrmcertificateauth.py | 1 - .../plugins/windows/test_winrmlistener.py | 1 - cloudbaseinit/tests/test_init.py | 4 +- .../tests/utils/windows/test_winrmconfig.py | 8 +-- .../tests/utils/windows/test_x509.py | 1 - 9 files changed, 53 insertions(+), 40 deletions(-) diff --git a/cloudbaseinit/tests/metadata/services/test_configdrive.py b/cloudbaseinit/tests/metadata/services/test_configdrive.py index 2fccafe2..b1e178e0 100644 --- a/cloudbaseinit/tests/metadata/services/test_configdrive.py +++ b/cloudbaseinit/tests/metadata/services/test_configdrive.py @@ -17,7 +17,6 @@ import importlib import mock import os -import sys import unittest import uuid diff --git a/cloudbaseinit/tests/plugins/windows/test_extendvolumes.py b/cloudbaseinit/tests/plugins/windows/test_extendvolumes.py index cc088982..95af8fa9 100644 --- a/cloudbaseinit/tests/plugins/windows/test_extendvolumes.py +++ b/cloudbaseinit/tests/plugins/windows/test_extendvolumes.py @@ -17,7 +17,6 @@ import importlib import mock import re -import sys import unittest from oslo.config import cfg diff --git a/cloudbaseinit/tests/plugins/windows/test_ntpclient.py b/cloudbaseinit/tests/plugins/windows/test_ntpclient.py index 72f8ce40..87b8f0e5 100644 --- a/cloudbaseinit/tests/plugins/windows/test_ntpclient.py +++ b/cloudbaseinit/tests/plugins/windows/test_ntpclient.py @@ -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", diff --git a/cloudbaseinit/tests/plugins/windows/test_userdata.py b/cloudbaseinit/tests/plugins/windows/test_userdata.py index bfae94ff..23d46e62 100644 --- a/cloudbaseinit/tests/plugins/windows/test_userdata.py +++ b/cloudbaseinit/tests/plugins/windows/test_userdata.py @@ -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): diff --git a/cloudbaseinit/tests/plugins/windows/test_winrmcertificateauth.py b/cloudbaseinit/tests/plugins/windows/test_winrmcertificateauth.py index 09f6881a..24fa6722 100644 --- a/cloudbaseinit/tests/plugins/windows/test_winrmcertificateauth.py +++ b/cloudbaseinit/tests/plugins/windows/test_winrmcertificateauth.py @@ -16,7 +16,6 @@ import importlib import mock -import sys import unittest from cloudbaseinit.plugins import base diff --git a/cloudbaseinit/tests/plugins/windows/test_winrmlistener.py b/cloudbaseinit/tests/plugins/windows/test_winrmlistener.py index 14026791..7bfce55d 100644 --- a/cloudbaseinit/tests/plugins/windows/test_winrmlistener.py +++ b/cloudbaseinit/tests/plugins/windows/test_winrmlistener.py @@ -16,7 +16,6 @@ import importlib import mock -import sys import unittest from cloudbaseinit.plugins import base diff --git a/cloudbaseinit/tests/test_init.py b/cloudbaseinit/tests/test_init.py index 49db2432..33b81374 100644 --- a/cloudbaseinit/tests/test_init.py +++ b/cloudbaseinit/tests/test_init.py @@ -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): diff --git a/cloudbaseinit/tests/utils/windows/test_winrmconfig.py b/cloudbaseinit/tests/utils/windows/test_winrmconfig.py index 671105f4..dd3af11a 100644 --- a/cloudbaseinit/tests/utils/windows/test_winrmconfig.py +++ b/cloudbaseinit/tests/utils/windows/test_winrmconfig.py @@ -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( diff --git a/cloudbaseinit/tests/utils/windows/test_x509.py b/cloudbaseinit/tests/utils/windows/test_x509.py index cccf17af..3075ce2d 100644 --- a/cloudbaseinit/tests/utils/windows/test_x509.py +++ b/cloudbaseinit/tests/utils/windows/test_x509.py @@ -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(