From: Kefu Chai Date: Wed, 31 Mar 2021 11:00:59 +0000 (+0800) Subject: mgr/dashboard: encode non-ascii string before passing it to exec_cmd() X-Git-Tag: v14.2.22~27^2~16^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6b6eddaa2d438396849e7fb3e043c7b74e6b904c;p=ceph.git 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 --- diff --git a/src/pybind/mgr/dashboard/tests/test_access_control.py b/src/pybind/mgr/dashboard/tests/test_access_control.py index 34452267558c7..2ff48517ab9bb 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: