From 6b6eddaa2d438396849e7fb3e043c7b74e6b904c Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 31 Mar 2021 19:00:59 +0800 Subject: [PATCH] mgr/dashboard: encode non-ascii string before passing it to exec_cmd() because on Python3, tempfile.TemporaryFile() is opened in binary mode by default, we need to encode non-ascii string before write to it. otherwise, we have following failure: self = def test_unicode_password(self): self.test_create_user() password = '\u7ae0\u9c7c\u4e0d\u662f\u5bc6\u7801' with tempfile.TemporaryFile(mode='w+') as pwd_file: > pwd_file.write(password) E UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-5: ordinal not in range(256) tests/test_access_control.py:576: UnicodeEncodeError this change is not cherry-picked from master, as master has dropped python2 support. Signed-off-by: Kefu Chai --- src/pybind/mgr/dashboard/tests/test_access_control.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/pybind/mgr/dashboard/tests/test_access_control.py b/src/pybind/mgr/dashboard/tests/test_access_control.py index 34452267558..2ff48517ab9 100644 --- a/src/pybind/mgr/dashboard/tests/test_access_control.py +++ b/src/pybind/mgr/dashboard/tests/test_access_control.py @@ -572,13 +572,10 @@ class AccessControlTest(unittest.TestCase, CLICommandTestMixin): def test_unicode_password(self): self.test_create_user() password = '章鱼不是密码' - with tempfile.TemporaryFile(mode='w+') as pwd_file: - pwd_file.write(password) - pwd_file.seek(0) - user = self.exec_cmd('ac-user-set-password', username='admin', - inbuf=pwd_file.read(), force_password=True) - pass_hash = password_hash(password, user['password']) - self.assertEqual(user['password'], pass_hash) + user = self.exec_cmd('ac-user-set-password', username='admin', + inbuf=password.encode(), force_password=True) + pass_hash = password_hash(password, user['password']) + self.assertEqual(user['password'], pass_hash) def test_set_user_password_nonexistent_user(self): with self.assertRaises(CmdException) as ctx: -- 2.47.3