deb-heat/heat_integrationtests/functional/test_unicode_template.py
Ethan Lynn 6cf8dc6e56 Fix problems in unicode template support
If we use unicode as resource name or parameter,
we are likely to encounter unicode problems.
This patch aims to fix unicode problems, and
there are several things to be clear:

1. For the class wrap with six.python_2_unicode_compatible, in
python2 their original __str__ function will map to __unicode__,
and new __str__ function comes from __unicode__.encode.
So we should always return unicode in __str__() if wrap
with python_2_unicode_compatible.

2. python_2_unicode_compatible will not handle __repr__,
__repr__ should return str in all versions of python.

Co-Authored-By: Ethan Lynn <xjunlin@cn.ibm.com>
Closes-Bug: #1524194
Change-Id: Ib4af43d153e0edf9d66308bf8e7b93a3be501d2e
2016-03-04 01:59:40 +00:00

114 lines
4.2 KiB
Python

# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from heat_integrationtests.functional import functional_base
class StackUnicodeTemplateTest(functional_base.FunctionalTestsBase):
random_template = u'''
heat_template_version: 2014-10-16
description: \u8fd9\u662f\u4e00\u4e2a\u63cf\u8ff0
parameters:
\u53c2\u6570:
type: number
default: 10
label: \u6807\u7b7e
description: \u8fd9\u662f\u4e00\u4e2a\u63cf\u8ff0
resources:
\u8d44\u6e90:
type: OS::Heat::RandomString
properties:
length: {get_param: \u53c2\u6570}
outputs:
\u8f93\u51fa:
description: \u8fd9\u662f\u4e00\u4e2a\u63cf\u8ff0
value: {get_attr: [\u8d44\u6e90, value]}
'''
def setUp(self):
super(StackUnicodeTemplateTest, self).setUp()
def _assert_results(self, result):
self.assertTrue(result['disable_rollback'])
self.assertIsNone(result['parent'])
self.assertEqual(u'\u8fd9\u662f\u4e00\u4e2a\u63cf\u8ff0',
result['template_description'])
self.assertEqual(u'10', result['parameters'][u'\u53c2\u6570'])
def _assert_preview_results(self, result):
self._assert_results(result)
res = result['resources'][0]
self.assertEqual('/resources/%s' % res['resource_name'],
res['resource_identity']['path'])
def _assert_create_results(self, result):
self._assert_results(result)
output = result['outputs'][0]
self.assertEqual(u'\u8fd9\u662f\u4e00\u4e2a\u63cf\u8ff0',
output['description'])
self.assertEqual(u'\u8f93\u51fa', output['output_key'])
self.assertIsNotNone(output['output_value'])
def _assert_resource_results(self, result):
self.assertEqual(u'\u8d44\u6e90', result['resource_name'])
self.assertEqual('OS::Heat::RandomString',
result['resource_type'])
def test_template_validate_basic(self):
ret = self.client.stacks.validate(template=self.random_template)
expected = {
'Description': u'\u8fd9\u662f\u4e00\u4e2a\u63cf\u8ff0',
'Parameters': {
u'\u53c2\u6570': {
'Default': 10,
'Description': u'\u8fd9\u662f\u4e00\u4e2a\u63cf\u8ff0',
'Label': u'\u6807\u7b7e',
'NoEcho': 'false',
'Type': 'Number'}
}
}
self.assertEqual(expected, ret)
def test_template_validate_override_default(self):
env = {'parameters': {u'\u53c2\u6570': 5}}
ret = self.client.stacks.validate(template=self.random_template,
environment=env)
expected = {
'Description': u'\u8fd9\u662f\u4e00\u4e2a\u63cf\u8ff0',
'Parameters': {
u'\u53c2\u6570': {
'Default': 10,
'Value': 5,
'Description': u'\u8fd9\u662f\u4e00\u4e2a\u63cf\u8ff0',
'Label': u'\u6807\u7b7e',
'NoEcho': 'false',
'Type': 'Number'}
}
}
self.assertEqual(expected, ret)
def test_stack_preview(self):
result = self.client.stacks.preview(
template=self.random_template,
stack_name=self._stack_rand_name(),
disable_rollback=True).to_dict()
self._assert_preview_results(result)
def test_create_stack(self):
stack_identifier = self.stack_create(template=self.random_template)
stack = self.client.stacks.get(stack_identifier)
self._assert_create_results(stack.to_dict())
rl = self.client.resources.list(stack_identifier)
self.assertEqual(1, len(rl))
self._assert_resource_results(rl[0].to_dict())