From 6d8b6b754f266903ac5a93b20642d7bcd758e843 Mon Sep 17 00:00:00 2001 From: ekosareva Date: Mon, 10 Aug 2015 23:36:55 +0400 Subject: [PATCH] Add support of unicode characters for node labels Need to work with normalized string to support correct comparison. Decode bytes from terminal as unicode string. Change-Id: Ic217abb7a1c3a39fcb6c741bec14242a2117d2ae Closes-Bug: #1477598 --- fuelclient/commands/node.py | 1 + fuelclient/tests/common/unit/test_utils.py | 17 +++++++++++++++++ fuelclient/tests/v2/unit/cli/test_node.py | 3 +++ fuelclient/utils.py | 12 ++++++++++++ 4 files changed, 33 insertions(+) diff --git a/fuelclient/commands/node.py b/fuelclient/commands/node.py index e51567f..871f337 100644 --- a/fuelclient/commands/node.py +++ b/fuelclient/commands/node.py @@ -48,6 +48,7 @@ class NodeList(NodeMixIn, base.BaseListCommand): parser.add_argument( '-l', '--labels', + type=utils.str_to_unicode, nargs='+', help='Show only nodes that have specific labels') diff --git a/fuelclient/tests/common/unit/test_utils.py b/fuelclient/tests/common/unit/test_utils.py index a25314f..ac86e98 100644 --- a/fuelclient/tests/common/unit/test_utils.py +++ b/fuelclient/tests/common/unit/test_utils.py @@ -16,6 +16,7 @@ import json import os +import six import subprocess import mock @@ -220,3 +221,19 @@ class TestUtils(base.UnitTestCase): result = data_utils.get_display_data_multi(fields, test_data) self.assertEqual([[2, 3], [8, 9]], result) + + @mock.patch('sys.getfilesystemencoding', return_value='utf-8') + def test_str_to_unicode(self, _): + test_data = 'тест' + expected_data = test_data if six.PY3 else u'тест' + result = utils.str_to_unicode(test_data) + self.assertIsInstance(result, six.text_type) + self.assertEqual(result, expected_data) + + @mock.patch('sys.getfilesystemencoding', return_value='iso-8859-16') + def test_latin_str_to_unicode(self, _): + test_data = 'czegoś' if six.PY3 else u'czegoś'.encode('iso-8859-16') + expected_data = test_data if six.PY3 else u'czegoś' + result = utils.str_to_unicode(test_data) + self.assertIsInstance(result, six.text_type) + self.assertEqual(result, expected_data) diff --git a/fuelclient/tests/v2/unit/cli/test_node.py b/fuelclient/tests/v2/unit/cli/test_node.py index e9f119e..4eb8804 100644 --- a/fuelclient/tests/v2/unit/cli/test_node.py +++ b/fuelclient/tests/v2/unit/cli/test_node.py @@ -15,6 +15,7 @@ # under the License. import mock +import six from fuelclient.tests.utils import fake_node from fuelclient.tests.v2.unit.cli import test_engine @@ -71,6 +72,8 @@ class TestNodeCommand(test_engine.BaseCLITest): self.m_get_client.assert_called_once_with('node', mock.ANY) self.m_client.get_all.assert_called_once_with( environment_id=env_id, labels=labels) + self.assertIsInstance( + self.m_client.get_all.call_args[1].get('labels')[0], six.text_type) def test_node_show(self): node_id = 42 diff --git a/fuelclient/utils.py b/fuelclient/utils.py index 8c269e0..6aba841 100644 --- a/fuelclient/utils.py +++ b/fuelclient/utils.py @@ -18,7 +18,9 @@ import glob import io import json import os +import six import subprocess +import sys import yaml from distutils.version import StrictVersion @@ -149,3 +151,13 @@ def parse_to_list_of_dicts(str_list): 'Not valid JSON data: {0}'.format(json_str)) dict_list.append(json_str) return dict_list + + +def str_to_unicode(string): + """Normalize input string from command line to unicode standard. + + :param str string: string to normalize + :returns: normalized string + + """ + return string if six.PY3 else string.decode(sys.getfilesystemencoding())