Skip to content

Commit dc5fe2f

Browse files
committed
Make kolla_set_configs open files in binary mode
This fixes comparisons when files are not Unicode-encoded. A relevant unit test is included. It can be used as a base for other _cmp_file method unit tests if the need arises. Change-Id: Ic638516eb92d24ad247a7866fd1b5e2ac0400388 Closes-Bug: #1913952 (cherry picked from commit 8e3027c)
1 parent 0e260fe commit dc5fe2f

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

docker/base/set_configs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def _cmp_file(self, source, dest):
164164
not os.path.exists(dest)):
165165
return False
166166
# check content
167-
with open(source) as f1, open(dest) as f2:
167+
with open(source, 'rb') as f1, open(dest, 'rb') as f2:
168168
if f1.read() != f2.read():
169169
LOG.error('The content of source file(%s) and'
170170
' dest file(%s) are not equal.', source, dest)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
Fixes an issue with the ``kolla_set_configs --check`` command when
5+
the compared files are non-Unicode.
6+
`LP#1913952 <https://launchpad.net/bugs/1913952>`__

tests/test_set_config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,26 @@ def test_check_source_dir_no_equal(self, mock_glob, mock_isdir,
329329
mock_cmp_dir.assert_called_once_with(
330330
'/var/lib/kolla/config_files/bar', '/foo')
331331
mock_cmp_file.assert_not_called()
332+
333+
@mock.patch('grp.getgrgid', autospec=True)
334+
@mock.patch('pwd.getpwuid', autospec=True)
335+
@mock.patch('os.stat', autospec=True)
336+
@mock.patch('builtins.open', new_callable=mock.mock_open)
337+
@mock.patch('os.path.exists', autospec=True)
338+
def test_cmp_file_opens_both_files_rb(self, mock_os_exists, mock_open,
339+
mock_os_stat, mock_pwd_getpwuid,
340+
mock_grp_getgrgid):
341+
config_file = set_configs.ConfigFile(
342+
'/var/lib/kolla/config_files/bar', '/foo', 'user1', '0644')
343+
344+
mock_os_exists.return_value = True
345+
mock_os_stat.return_value.st_mode = int('0o100644', 8)
346+
mock_pwd_getpwuid.return_value.pw_name = 'user1'
347+
mock_grp_getgrgid.return_value.gr_name = 'user1'
348+
349+
self.assertIs(True,
350+
config_file._cmp_file('/fake/file1', '/fake/file2'))
351+
352+
self.assertEqual([mock.call('/fake/file1', 'rb'),
353+
mock.call('/fake/file2', 'rb')],
354+
mock_open.call_args_list)

0 commit comments

Comments
 (0)