From 07971152413c3aa94e87b16f562acf686b9c5f48 Mon Sep 17 00:00:00 2001
From: Kevin_Zheng <zhengzhenyu@huawei.com>
Date: Fri, 3 Mar 2017 14:54:11 +0800
Subject: [PATCH] Tags and Metadata fields with unicode cannot be correctly
 displayed

Currently, Tags(list) and Metadata(dict) fields of instance will
firstly transformed to str using jsondump first when display. And
cannot be correctly transfomed and displayed afterwards.

This patch adds ensure_ascii=False to the transform function thus
those fields can be correctly tranformed and displayed afterwards.

Change-Id: Ib4e7a34f3b19db89280cc73053acbac8c8816f85
Closes-Bug: #1669683
---
 .../tests/functional/v2/test_servers.py       | 19 +++++++++++++++++--
 novaclient/utils.py                           |  2 +-
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/novaclient/tests/functional/v2/test_servers.py b/novaclient/tests/functional/v2/test_servers.py
index 6adf1e052..7027413de 100644
--- a/novaclient/tests/functional/v2/test_servers.py
+++ b/novaclient/tests/functional/v2/test_servers.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
 #    not use this file except in compliance with the License. You may obtain
 #    a copy of the License at
@@ -134,9 +135,9 @@ class TestServersTagsV226(base.ClientTestBase):
 
     COMPUTE_API_VERSION = "2.26"
 
-    def _boot_server_with_tags(self):
+    def _boot_server_with_tags(self, tags=["t1", "t2"]):
         uuid = self._create_server().id
-        self.client.servers.set_tags(uuid, ["t1", "t2"])
+        self.client.servers.set_tags(uuid, tags)
         return uuid
 
     def test_show(self):
@@ -145,6 +146,20 @@ class TestServersTagsV226(base.ClientTestBase):
         self.assertEqual('["t1", "t2"]', self._get_value_from_the_table(
             output, "tags"))
 
+    def test_unicode_tag_correctly_displayed(self):
+        """Regression test for bug #1669683.
+
+        List and dict fields with unicode cannot be correctly
+        displayed.
+
+        Ensure that once we fix this it doesn't regress.
+        """
+        # create an instance with chinese tag
+        uuid = self._boot_server_with_tags(tags=["中文标签"])
+        output = self.nova("show %s" % uuid)
+        self.assertEqual('["中文标签"]', self._get_value_from_the_table(
+            output, "tags"))
+
     def test_list(self):
         uuid = self._boot_server_with_tags()
         output = self.nova("server-tag-list %s" % uuid)
diff --git a/novaclient/utils.py b/novaclient/utils.py
index 7486ab29c..e93df6999 100644
--- a/novaclient/utils.py
+++ b/novaclient/utils.py
@@ -246,7 +246,7 @@ def print_dict(d, dict_property="Property", dict_value="Value", wrap=0):
     for k, v in sorted(d.items()):
         # convert dict to str to check length
         if isinstance(v, (dict, list)):
-            v = jsonutils.dumps(v)
+            v = jsonutils.dumps(v, ensure_ascii=False)
         if wrap > 0:
             v = textwrap.fill(six.text_type(v), wrap)
         # if value has a newline, add in multiple rows