From ef2c8818e6139904027830435171aff14c6b2407 Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Mon, 6 Mar 2017 09:48:14 -0800 Subject: [PATCH] Fix TypeError when writing config file in Python 3 When using Python 3 would have a type error when writing an integer value to the config file. Convert it to a string before writing it. Change-Id: I89f9f4983d497adc7ac89f9e89e648f58bed3510 Closes-Bug: #1670441 --- virtualbmc/manager.py | 3 ++- virtualbmc/tests/unit/test_manager.py | 31 +++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/virtualbmc/manager.py b/virtualbmc/manager.py index 2ae7e79..ed4c86b 100644 --- a/virtualbmc/manager.py +++ b/virtualbmc/manager.py @@ -15,6 +15,7 @@ import os import shutil import signal +import six from six.moves import configparser from virtualbmc import config as vbmc_config @@ -109,7 +110,7 @@ class VirtualBMCManager(object): config.add_section(DEFAULT_SECTION) config.set(DEFAULT_SECTION, 'username', username) config.set(DEFAULT_SECTION, 'password', password) - config.set(DEFAULT_SECTION, 'port', port) + config.set(DEFAULT_SECTION, 'port', six.text_type(port)) config.set(DEFAULT_SECTION, 'address', address) config.set(DEFAULT_SECTION, 'domain_name', domain_name) config.set(DEFAULT_SECTION, 'libvirt_uri', libvirt_uri) diff --git a/virtualbmc/tests/unit/test_manager.py b/virtualbmc/tests/unit/test_manager.py index 061a2db..ec28c1a 100644 --- a/virtualbmc/tests/unit/test_manager.py +++ b/virtualbmc/tests/unit/test_manager.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +import copy import errno import os import shutil @@ -44,7 +45,7 @@ class VirtualBMCManagerTestCase(base.TestCase): self.domain_path0 = os.path.join(_CONFIG_PATH, self.domain_name0) self.domain_path1 = os.path.join(_CONFIG_PATH, self.domain_name1) self.add_params = {'username': 'admin', 'password': 'pass', - 'port': 777, 'address': '::', + 'port': '777', 'address': '::', 'domain_name': 'Squidward Tentacles', 'libvirt_uri': 'foo://bar', 'libvirt_sasl_username': 'sasl_admin', @@ -118,7 +119,33 @@ class VirtualBMCManagerTestCase(base.TestCase): def test_add(self, mock_check_conn, mock_makedirs, mock_configparser, mock_open): config = mock_configparser.return_value - self.manager.add(**self.add_params) + params = copy.copy(self.add_params) + self.manager.add(**params) + + expected_calls = [mock.call('VirtualBMC', i, self.add_params[i]) + for i in self.add_params] + self.assertEqual(sorted(expected_calls), + sorted(config.set.call_args_list)) + config.add_section.assert_called_once_with('VirtualBMC') + config.write.assert_called_once_with(mock.ANY) + mock_check_conn.assert_called_once_with( + self.add_params['libvirt_uri'], self.add_params['domain_name'], + sasl_username=self.add_params['libvirt_sasl_username'], + sasl_password=self.add_params['libvirt_sasl_password']) + mock_makedirs.assert_called_once_with( + os.path.join(_CONFIG_PATH, self.add_params['domain_name'])) + mock_configparser.assert_called_once_with() + + @mock.patch.object(builtins, 'open') + @mock.patch.object(configparser, 'ConfigParser') + @mock.patch.object(os, 'makedirs') + @mock.patch.object(utils, 'check_libvirt_connection_and_domain') + def test_add_with_port_as_int(self, mock_check_conn, mock_makedirs, + mock_configparser, mock_open): + config = mock_configparser.return_value + params = copy.copy(self.add_params) + params['port'] = int(params['port']) + self.manager.add(**params) expected_calls = [mock.call('VirtualBMC', i, self.add_params[i]) for i in self.add_params]