Browse Source

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
John L. Villalovos 2 years ago
parent
commit
ef2c8818e6
2 changed files with 31 additions and 3 deletions
  1. 2
    1
      virtualbmc/manager.py
  2. 29
    2
      virtualbmc/tests/unit/test_manager.py

+ 2
- 1
virtualbmc/manager.py View File

@@ -15,6 +15,7 @@ import os
15 15
 import shutil
16 16
 import signal
17 17
 
18
+import six
18 19
 from six.moves import configparser
19 20
 
20 21
 from virtualbmc import config as vbmc_config
@@ -109,7 +110,7 @@ class VirtualBMCManager(object):
109 110
             config.add_section(DEFAULT_SECTION)
110 111
             config.set(DEFAULT_SECTION, 'username', username)
111 112
             config.set(DEFAULT_SECTION, 'password', password)
112
-            config.set(DEFAULT_SECTION, 'port', port)
113
+            config.set(DEFAULT_SECTION, 'port', six.text_type(port))
113 114
             config.set(DEFAULT_SECTION, 'address', address)
114 115
             config.set(DEFAULT_SECTION, 'domain_name', domain_name)
115 116
             config.set(DEFAULT_SECTION, 'libvirt_uri', libvirt_uri)

+ 29
- 2
virtualbmc/tests/unit/test_manager.py View File

@@ -13,6 +13,7 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 
16
+import copy
16 17
 import errno
17 18
 import os
18 19
 import shutil
@@ -44,7 +45,7 @@ class VirtualBMCManagerTestCase(base.TestCase):
44 45
         self.domain_path0 = os.path.join(_CONFIG_PATH, self.domain_name0)
45 46
         self.domain_path1 = os.path.join(_CONFIG_PATH, self.domain_name1)
46 47
         self.add_params = {'username': 'admin', 'password': 'pass',
47
-                           'port': 777, 'address': '::',
48
+                           'port': '777', 'address': '::',
48 49
                            'domain_name': 'Squidward Tentacles',
49 50
                            'libvirt_uri': 'foo://bar',
50 51
                            'libvirt_sasl_username': 'sasl_admin',
@@ -118,7 +119,33 @@ class VirtualBMCManagerTestCase(base.TestCase):
118 119
     def test_add(self, mock_check_conn, mock_makedirs, mock_configparser,
119 120
                  mock_open):
120 121
         config = mock_configparser.return_value
121
-        self.manager.add(**self.add_params)
122
+        params = copy.copy(self.add_params)
123
+        self.manager.add(**params)
124
+
125
+        expected_calls = [mock.call('VirtualBMC', i, self.add_params[i])
126
+                          for i in self.add_params]
127
+        self.assertEqual(sorted(expected_calls),
128
+                         sorted(config.set.call_args_list))
129
+        config.add_section.assert_called_once_with('VirtualBMC')
130
+        config.write.assert_called_once_with(mock.ANY)
131
+        mock_check_conn.assert_called_once_with(
132
+            self.add_params['libvirt_uri'], self.add_params['domain_name'],
133
+            sasl_username=self.add_params['libvirt_sasl_username'],
134
+            sasl_password=self.add_params['libvirt_sasl_password'])
135
+        mock_makedirs.assert_called_once_with(
136
+            os.path.join(_CONFIG_PATH, self.add_params['domain_name']))
137
+        mock_configparser.assert_called_once_with()
138
+
139
+    @mock.patch.object(builtins, 'open')
140
+    @mock.patch.object(configparser, 'ConfigParser')
141
+    @mock.patch.object(os, 'makedirs')
142
+    @mock.patch.object(utils, 'check_libvirt_connection_and_domain')
143
+    def test_add_with_port_as_int(self, mock_check_conn, mock_makedirs,
144
+                                  mock_configparser, mock_open):
145
+        config = mock_configparser.return_value
146
+        params = copy.copy(self.add_params)
147
+        params['port'] = int(params['port'])
148
+        self.manager.add(**params)
122 149
 
123 150
         expected_calls = [mock.call('VirtualBMC', i, self.add_params[i])
124 151
                           for i in self.add_params]

Loading…
Cancel
Save