Skip to content

Commit f72a4b3

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 ff3192a commit f72a4b3

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
@@ -339,3 +339,26 @@ def test_check_source_dir_no_equal(self, mock_glob, mock_isdir,
339339
mock_cmp_dir.assert_called_once_with(
340340
'/var/lib/kolla/config_files/bar', '/foo')
341341
mock_cmp_file.assert_not_called()
342+
343+
@mock.patch('grp.getgrgid', autospec=True)
344+
@mock.patch('pwd.getpwuid', autospec=True)
345+
@mock.patch('os.stat', autospec=True)
346+
@mock.patch('builtins.open', new_callable=mock.mock_open)
347+
@mock.patch('os.path.exists', autospec=True)
348+
def test_cmp_file_opens_both_files_rb(self, mock_os_exists, mock_open,
349+
mock_os_stat, mock_pwd_getpwuid,
350+
mock_grp_getgrgid):
351+
config_file = set_configs.ConfigFile(
352+
'/var/lib/kolla/config_files/bar', '/foo', 'user1', '0644')
353+
354+
mock_os_exists.return_value = True
355+
mock_os_stat.return_value.st_mode = int('0o100644', 8)
356+
mock_pwd_getpwuid.return_value.pw_name = 'user1'
357+
mock_grp_getgrgid.return_value.gr_name = 'user1'
358+
359+
self.assertIs(True,
360+
config_file._cmp_file('/fake/file1', '/fake/file2'))
361+
362+
self.assertEqual([mock.call('/fake/file1', 'rb'),
363+
mock.call('/fake/file2', 'rb')],
364+
mock_open.call_args_list)

0 commit comments

Comments
 (0)