py34: test_hot and test_validate

* use hashlib.algorithms_available in PY3 and hashlib.algorithms in PY2
* use e.args[0] instead of six.text_type(e) which will otherwise
  result in the unicode literal being flattened.
* add stubs to prevent actual API calls in tests.
* constraint the test template to help in more accurate testing which
  otherwise results in iterating through other unnecessary items.

partial blueprint heat-python34-support

Change-Id: I7480555330161cf6f197c83f3245685511284307
This commit is contained in:
Sirushti Murugesan 2015-08-05 01:18:35 +05:30
parent 12d535f3ba
commit 800f38aac4
7 changed files with 48 additions and 44 deletions

View File

@ -122,7 +122,7 @@ class HeatException(Exception):
raise_(exc_info[0], exc_info[1], exc_info[2])
def __str__(self):
return six.text_type(self.message)
return self.message
def __deepcopy__(self, memo):
return self.__class__(**self.kwargs)

View File

@ -408,13 +408,18 @@ class Digest(function.Function):
msg = _('Function "%s" usage: ["<algorithm>", "<value>"]')
raise ValueError(msg % self.fn_name)
if args[0].lower() not in hashlib.algorithms:
if six.PY3:
algorithms = hashlib.algorithms_available
else:
algorithms = hashlib.algorithms
if args[0].lower() not in algorithms:
msg = _('Algorithm must be one of %s')
raise ValueError(msg % six.text_type(hashlib.algorithms))
raise ValueError(msg % six.text_type(algorithms))
def digest(self, algorithm, value):
_hash = hashlib.new(algorithm)
_hash.update(value)
_hash.update(six.b(value))
return _hash.hexdigest()

View File

@ -136,7 +136,7 @@ class HOTemplate20130523(template.Template):
obj_name = section[:-1]
err_msg = _('"%%s" is not a valid keyword inside a %s '
'definition') % obj_name
for name, attrs in six.iteritems(data):
for name, attrs in sorted(data.items()):
cfn_object = {}
if not attrs:
@ -159,7 +159,7 @@ class HOTemplate20130523(template.Template):
raise exception.StackValidationFailed(message=message)
except KeyError as e:
# an invalid keyword was found
raise exception.StackValidationFailed(message=six.text_type(e))
raise exception.StackValidationFailed(message=e.args[0])
return cfn_objects

View File

@ -2852,12 +2852,8 @@ class StackServiceTest(common.HeatTestCase):
self.eng._validate_new_stack,
self.ctx, 'test_existing_stack',
parsed_template)
msg = (u'u\'"Type" is not a valid keyword '
'inside a resource definition\'')
if six.PY3:
msg = ('\'"Type" is not a valid keyword inside a '
'resource definition\'')
msg = (u'"Type" is not a valid keyword '
'inside a resource definition')
self.assertEqual(msg, six.text_type(ex))

View File

@ -203,8 +203,8 @@ class HOTemplateTest(common.HeatTestCase):
tmpl = template.Template(hot_tpl)
err = self.assertRaises(exception.StackValidationFailed,
tmpl.__getitem__, tmpl.RESOURCES)
self.assertEqual('u\'"Type" is not a valid keyword '
'inside a resource definition\'',
self.assertEqual('"Type" is not a valid keyword '
'inside a resource definition',
six.text_type(err))
def test_translate_resources_bad_properties(self):
@ -228,8 +228,8 @@ class HOTemplateTest(common.HeatTestCase):
tmpl = template.Template(hot_tpl)
err = self.assertRaises(exception.StackValidationFailed,
tmpl.__getitem__, tmpl.RESOURCES)
self.assertEqual('u\'"Properties" is not a valid keyword '
'inside a resource definition\'',
self.assertEqual('"Properties" is not a valid keyword '
'inside a resource definition',
six.text_type(err))
def test_translate_resources_resources_without_name(self):
@ -272,8 +272,9 @@ class HOTemplateTest(common.HeatTestCase):
tmpl = template.Template(hot_tpl)
err = self.assertRaises(exception.StackValidationFailed,
tmpl.__getitem__, tmpl.RESOURCES)
self.assertEqual('u\'"Metadata" is not a valid keyword '
'inside a resource definition\'',
self.assertEqual('"Metadata" is not a valid keyword '
'inside a resource definition',
six.text_type(err))
def test_translate_resources_bad_depends_on(self):
@ -297,8 +298,8 @@ class HOTemplateTest(common.HeatTestCase):
tmpl = template.Template(hot_tpl)
err = self.assertRaises(exception.StackValidationFailed,
tmpl.__getitem__, tmpl.RESOURCES)
self.assertEqual('u\'"DependsOn" is not a valid keyword '
'inside a resource definition\'',
self.assertEqual('"DependsOn" is not a valid keyword '
'inside a resource definition',
six.text_type(err))
def test_translate_resources_bad_deletion_policy(self):
@ -322,8 +323,8 @@ class HOTemplateTest(common.HeatTestCase):
tmpl = template.Template(hot_tpl)
err = self.assertRaises(exception.StackValidationFailed,
tmpl.__getitem__, tmpl.RESOURCES)
self.assertEqual('u\'"DeletionPolicy" is not a valid keyword '
'inside a resource definition\'',
self.assertEqual('"DeletionPolicy" is not a valid keyword '
'inside a resource definition',
six.text_type(err))
def test_translate_resources_bad_update_policy(self):
@ -347,8 +348,8 @@ class HOTemplateTest(common.HeatTestCase):
tmpl = template.Template(hot_tpl)
err = self.assertRaises(exception.StackValidationFailed,
tmpl.__getitem__, tmpl.RESOURCES)
self.assertEqual('u\'"UpdatePolicy" is not a valid keyword '
'inside a resource definition\'',
self.assertEqual('"UpdatePolicy" is not a valid keyword '
'inside a resource definition',
six.text_type(err))
def test_translate_outputs_good(self):

View File

@ -218,13 +218,7 @@ test_template_invalid_resources = '''
}
},
"Resources" : {
"Type" : "AWS::EC2::Instance",
"Metadata" : {
},
"Properties" : {
"ImageId" : { "Ref" : "centos-6.4-20130701-0" },
"InstanceType" : { "Ref" : "InstanceType" }
}
"Type" : "AWS::EC2::Instance"
}
}
'''
@ -1226,8 +1220,8 @@ class ValidateTest(common.HeatTestCase):
engine = service.EngineService('a', 't')
res = dict(engine.validate_template(None, hot_tpl, {}))
self.assertEqual({'Error': 'u\'"Type" is not a valid keyword '
'inside a resource definition\''}, res)
self.assertEqual({'Error': '"Type" is not a valid keyword '
'inside a resource definition'}, res)
def test_validate_template_with_invalid_resource_properties(self):
hot_tpl = template_format.parse('''
@ -1247,8 +1241,8 @@ class ValidateTest(common.HeatTestCase):
engine = service.EngineService('a', 't')
res = dict(engine.validate_template(None, hot_tpl, {}))
self.assertEqual({'Error': 'u\'"Properties" is not a valid keyword '
'inside a resource definition\''}, res)
self.assertEqual({'Error': '"Properties" is not a valid keyword '
'inside a resource definition'}, res)
def test_validate_template_with_invalid_resource_matadata(self):
hot_tpl = template_format.parse('''
@ -1268,8 +1262,8 @@ class ValidateTest(common.HeatTestCase):
engine = service.EngineService('a', 't')
res = dict(engine.validate_template(None, hot_tpl, {}))
self.assertEqual({'Error': 'u\'"Metadata" is not a valid keyword '
'inside a resource definition\''}, res)
self.assertEqual({'Error': '"Metadata" is not a valid keyword '
'inside a resource definition'}, res)
def test_validate_template_with_invalid_resource_depends_on(self):
hot_tpl = template_format.parse('''
@ -1289,8 +1283,8 @@ class ValidateTest(common.HeatTestCase):
engine = service.EngineService('a', 't')
res = dict(engine.validate_template(None, hot_tpl, {}))
self.assertEqual({'Error': 'u\'"DependsOn" is not a valid keyword '
'inside a resource definition\''}, res)
self.assertEqual({'Error': '"DependsOn" is not a valid keyword '
'inside a resource definition'}, res)
def test_validate_template_with_invalid_resource_deletion_policy(self):
hot_tpl = template_format.parse('''
@ -1310,8 +1304,8 @@ class ValidateTest(common.HeatTestCase):
engine = service.EngineService('a', 't')
res = dict(engine.validate_template(None, hot_tpl, {}))
self.assertEqual({'Error': 'u\'"DeletionPolicy" is not a valid '
'keyword inside a resource definition\''},
self.assertEqual({'Error': '"DeletionPolicy" is not a valid '
'keyword inside a resource definition'},
res)
def test_validate_template_with_invalid_resource_update_policy(self):
@ -1332,8 +1326,8 @@ class ValidateTest(common.HeatTestCase):
engine = service.EngineService('a', 't')
res = dict(engine.validate_template(None, hot_tpl, {}))
self.assertEqual({'Error': 'u\'"UpdatePolicy" is not a valid '
'keyword inside a resource definition\''},
self.assertEqual({'Error': '"UpdatePolicy" is not a valid '
'keyword inside a resource definition'},
res)
def test_unregistered_key(self):
@ -1342,7 +1336,8 @@ class ValidateTest(common.HeatTestCase):
template = tmpl.Template(t, env=environment.Environment(params))
stack = parser.Stack(self.ctx, 'test_stack', template)
self._mock_get_image_id_success('image_name', 'image_id')
self.stub_FlavorConstraint_validate()
self.stub_ImageConstraint_validate()
self.m.ReplayAll()
resource = stack['Instance']
@ -1361,6 +1356,8 @@ class ValidateTest(common.HeatTestCase):
exception.EntityNotFound(
entity='Image',
name='image_name'))
self.stub_KeypairConstraint_validate()
self.stub_FlavorConstraint_validate()
self.m.ReplayAll()
resource = stack['Instance']
@ -1380,6 +1377,8 @@ class ValidateTest(common.HeatTestCase):
exception.PhysicalResourceNameAmbiguity(
name='image_name'))
self.stub_KeypairConstraint_validate()
self.stub_FlavorConstraint_validate()
self.m.ReplayAll()
resource = stack['Instance']
@ -1433,6 +1432,7 @@ class ValidateTest(common.HeatTestCase):
glance_exceptions.ClientException(500))
self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create')
glance.GlanceClientPlugin._create().AndReturn(self.gc)
self.stub_FlavorConstraint_validate()
self.m.ReplayAll()
self.assertRaises(exception.StackValidationFailed, stack.validate)

View File

@ -107,6 +107,7 @@ heat.tests.test_exception
heat.tests.test_function
heat.tests.test_glance_image
heat.tests.test_grouputils
heat.tests.test_hot
heat.tests.test_identifier
heat.tests.test_instance_group
heat.tests.test_instance_group_update_policy
@ -156,6 +157,7 @@ heat.tests.test_template_format
heat.tests.test_timeutils
heat.tests.test_trove_cluster
heat.tests.test_urlfetch
heat.tests.test_validate
heat.tests.test_version
heat.tests.test_vpc
heat.tests.test_watch