Fix test cases pass dict schema directly as parameter schema object

Filter the parameter schema snippet through a Template object and out
the other side completely unchanged before passing it to Parameter.

Implements blueprint hot-parameters

Change-Id: I8c3dd99c1a2f460f50a92565d9098c70c9e77a8a
This commit is contained in:
JUN JIE NAN 2013-08-01 11:14:12 +08:00
parent 171cf95bb4
commit 4368dccc08
2 changed files with 68 additions and 56 deletions

View File

@ -385,6 +385,9 @@ class Template(collections.Mapping):
handle_resource_facade,
s)
def param_schemata(self):
return self[PARAMETERS]
def _resolve(match, handle, snippet):
'''

View File

@ -22,43 +22,52 @@ from heat.engine import template
class ParameterTest(testtools.TestCase):
def new_parameter(self, name, schema, value=None,
validate_value=True):
tmpl = template.Template({template.PARAMETERS: {name:
schema}})
schema = tmpl.param_schemata()[name]
return parameters.Parameter(name, schema, value,
validate_value)
def test_new_string(self):
p = parameters.Parameter('p', {'Type': 'String'}, validate_value=False)
p = self.new_parameter('p', {'Type': 'String'}, validate_value=False)
self.assertTrue(isinstance(p, parameters.StringParam))
def test_new_number(self):
p = parameters.Parameter('p', {'Type': 'Number'}, validate_value=False)
p = self.new_parameter('p', {'Type': 'Number'}, validate_value=False)
self.assertTrue(isinstance(p, parameters.NumberParam))
def test_new_list(self):
p = parameters.Parameter('p', {'Type': 'CommaDelimitedList'},
validate_value=False)
p = self.new_parameter('p', {'Type': 'CommaDelimitedList'},
validate_value=False)
self.assertTrue(isinstance(p, parameters.CommaDelimitedListParam))
def test_new_json(self):
p = parameters.Parameter('p', {'Type': 'Json'}, validate_value=False)
p = self.new_parameter('p', {'Type': 'Json'}, validate_value=False)
self.assertTrue(isinstance(p, parameters.JsonParam))
def test_new_bad_type(self):
self.assertRaises(ValueError, parameters.Parameter,
'p', {'Type': 'List'})
self.assertRaises(ValueError, self.new_parameter, 'p',
{'Type': 'List'})
def test_new_no_type(self):
self.assertRaises(KeyError, parameters.Parameter,
self.assertRaises(KeyError, self.new_parameter,
'p', {'Default': 'blarg'})
def test_default_no_override(self):
p = parameters.Parameter('defaulted', {'Type': 'String',
'Default': 'blarg'})
p = self.new_parameter('defaulted', {'Type': 'String',
'Default': 'blarg'})
self.assertTrue(p.has_default())
self.assertEqual(p.default(), 'blarg')
self.assertEqual(p.value(), 'blarg')
def test_default_override(self):
p = parameters.Parameter('defaulted',
{'Type': 'String',
'Default': 'blarg'},
'wibble')
p = self.new_parameter('defaulted',
{'Type': 'String',
'Default': 'blarg'},
'wibble')
self.assertTrue(p.has_default())
self.assertEqual(p.default(), 'blarg')
self.assertEqual(p.value(), 'wibble')
@ -69,7 +78,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble',
'Default': 'bar'}
try:
parameters.Parameter('p', schema, 'foo')
self.new_parameter('p', schema, 'foo')
except ValueError as ve:
msg = str(ve)
self.assertNotEqual(msg.find('wibble'), -1)
@ -77,45 +86,45 @@ class ParameterTest(testtools.TestCase):
self.fail('ValueError not raised')
def test_no_echo_true(self):
p = parameters.Parameter('anechoic',
{'Type': 'String',
'NoEcho': 'true'},
'wibble')
p = self.new_parameter('anechoic',
{'Type': 'String',
'NoEcho': 'true'},
'wibble')
self.assertTrue(p.no_echo())
self.assertNotEqual(str(p), 'wibble')
def test_no_echo_true_caps(self):
p = parameters.Parameter('anechoic',
{'Type': 'String',
'NoEcho': 'TrUe'},
'wibble')
p = self.new_parameter('anechoic',
{'Type': 'String',
'NoEcho': 'TrUe'},
'wibble')
self.assertTrue(p.no_echo())
self.assertNotEqual(str(p), 'wibble')
def test_no_echo_false(self):
p = parameters.Parameter('echoic',
{'Type': 'String',
'NoEcho': 'false'},
'wibble')
p = self.new_parameter('echoic',
{'Type': 'String',
'NoEcho': 'false'},
'wibble')
self.assertFalse(p.no_echo())
self.assertEqual(str(p), 'wibble')
def test_description(self):
description = 'Description of the parameter'
p = parameters.Parameter('p', {'Type': 'String',
'Description': description},
validate_value=False)
p = self.new_parameter('p', {'Type': 'String',
'Description': description},
validate_value=False)
self.assertEqual(p.description(), description)
def test_no_description(self):
p = parameters.Parameter('p', {'Type': 'String'}, validate_value=False)
p = self.new_parameter('p', {'Type': 'String'}, validate_value=False)
self.assertEqual(p.description(), '')
def test_string_len_good(self):
schema = {'Type': 'String',
'MinLength': '3',
'MaxLength': '3'}
p = parameters.Parameter('p', schema, 'foo')
p = self.new_parameter('p', schema, 'foo')
self.assertEqual(p.value(), 'foo')
def test_string_underflow(self):
@ -123,7 +132,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble',
'MinLength': '4'}
try:
parameters.Parameter('p', schema, 'foo')
self.new_parameter('p', schema, 'foo')
except ValueError as ve:
msg = str(ve)
self.assertNotEqual(msg.find('wibble'), -1)
@ -135,7 +144,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble',
'MaxLength': '2'}
try:
parameters.Parameter('p', schema, 'foo')
self.new_parameter('p', schema, 'foo')
except ValueError as ve:
msg = str(ve)
self.assertNotEqual(msg.find('wibble'), -1)
@ -145,7 +154,7 @@ class ParameterTest(testtools.TestCase):
def test_string_pattern_good(self):
schema = {'Type': 'String',
'AllowedPattern': '[a-z]*'}
p = parameters.Parameter('p', schema, 'foo')
p = self.new_parameter('p', schema, 'foo')
self.assertEqual(p.value(), 'foo')
def test_string_pattern_bad_prefix(self):
@ -153,7 +162,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble',
'AllowedPattern': '[a-z]*'}
try:
parameters.Parameter('p', schema, '1foo')
self.new_parameter('p', schema, '1foo')
except ValueError as ve:
msg = str(ve)
self.assertNotEqual(msg.find('wibble'), -1)
@ -165,7 +174,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble',
'AllowedPattern': '[a-z]*'}
try:
parameters.Parameter('p', schema, 'foo1')
self.new_parameter('p', schema, 'foo1')
except ValueError as ve:
msg = str(ve)
self.assertNotEqual(msg.find('wibble'), -1)
@ -175,7 +184,7 @@ class ParameterTest(testtools.TestCase):
def test_string_value_list_good(self):
schema = {'Type': 'String',
'AllowedValues': ['foo', 'bar', 'baz']}
p = parameters.Parameter('p', schema, 'bar')
p = self.new_parameter('p', schema, 'bar')
self.assertEqual(p.value(), 'bar')
def test_string_value_list_bad(self):
@ -183,7 +192,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble',
'AllowedValues': ['foo', 'bar', 'baz']}
try:
parameters.Parameter('p', schema, 'blarg')
self.new_parameter('p', schema, 'blarg')
except ValueError as ve:
msg = str(ve)
self.assertNotEqual(msg.find('wibble'), -1)
@ -194,14 +203,14 @@ class ParameterTest(testtools.TestCase):
schema = {'Type': 'Number',
'MinValue': '3',
'MaxValue': '3'}
p = parameters.Parameter('p', schema, '3')
p = self.new_parameter('p', schema, '3')
self.assertEqual(p.value(), '3')
def test_number_float_good(self):
schema = {'Type': 'Number',
'MinValue': '3.0',
'MaxValue': '3.0'}
p = parameters.Parameter('p', schema, '3.0')
p = self.new_parameter('p', schema, '3.0')
self.assertEqual(p.value(), '3.0')
def test_number_low(self):
@ -209,7 +218,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble',
'MinValue': '4'}
try:
parameters.Parameter('p', schema, '3')
self.new_parameter('p', schema, '3')
except ValueError as ve:
msg = str(ve)
self.assertNotEqual(msg.find('wibble'), -1)
@ -221,7 +230,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble',
'MaxValue': '2'}
try:
parameters.Parameter('p', schema, '3')
self.new_parameter('p', schema, '3')
except ValueError as ve:
msg = str(ve)
self.assertNotEqual(msg.find('wibble'), -1)
@ -231,7 +240,7 @@ class ParameterTest(testtools.TestCase):
def test_number_value_list_good(self):
schema = {'Type': 'Number',
'AllowedValues': ['1', '3', '5']}
p = parameters.Parameter('p', schema, '5')
p = self.new_parameter('p', schema, '5')
self.assertEqual(p.value(), '5')
def test_number_value_list_bad(self):
@ -239,7 +248,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble',
'AllowedValues': ['1', '3', '5']}
try:
parameters.Parameter('p', schema, '2')
self.new_parameter('p', schema, '2')
except ValueError as ve:
msg = str(ve)
self.assertNotEqual(msg.find('wibble'), -1)
@ -249,7 +258,7 @@ class ParameterTest(testtools.TestCase):
def test_list_value_list_good(self):
schema = {'Type': 'CommaDelimitedList',
'AllowedValues': ['foo', 'bar', 'baz']}
p = parameters.Parameter('p', schema, 'baz,foo,bar')
p = self.new_parameter('p', schema, 'baz,foo,bar')
self.assertEqual(p.value(), 'baz,foo,bar')
def test_list_value_list_bad(self):
@ -257,7 +266,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble',
'AllowedValues': ['foo', 'bar', 'baz']}
try:
parameters.Parameter('p', schema, 'foo,baz,blarg')
self.new_parameter('p', schema, 'foo,baz,blarg')
except ValueError as ve:
msg = str(ve)
self.assertNotEqual(msg.find('wibble'), -1)
@ -269,7 +278,7 @@ class ParameterTest(testtools.TestCase):
schema = {'Type': 'Json'}
val = {"foo": "bar", "items": [1, 2, 3]}
val_s = json.dumps(val)
p = parameters.Parameter('p', schema, val)
p = self.new_parameter('p', schema, val)
self.assertEqual(val_s, p.value())
self.assertEqual(val, p.parsed)
@ -279,7 +288,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble'}
val = {"foo": "bar", "not_json": len}
try:
parameters.Parameter('p', schema, val)
self.new_parameter('p', schema, val)
except ValueError as verr:
self.assertIn('Value must be valid JSON', str(verr))
else:
@ -290,7 +299,7 @@ class ParameterTest(testtools.TestCase):
schema = {'Type': 'Json'}
val = {"foo": "bar", "items": [1, 2, 3]}
val_s = json.dumps(val)
p = parameters.Parameter('p', schema, val_s)
p = self.new_parameter('p', schema, val_s)
self.assertEqual(val_s, p.value())
self.assertEqual(val, p.parsed)
@ -300,7 +309,7 @@ class ParameterTest(testtools.TestCase):
'ConstraintDescription': 'wibble'}
val = "I am not a map"
try:
parameters.Parameter('p', schema, val)
self.new_parameter('p', schema, val)
except ValueError as verr:
self.assertIn('Value must be valid JSON', str(verr))
else:
@ -312,7 +321,7 @@ class ParameterTest(testtools.TestCase):
'AllowedValues': ["foo", "bar", "baz"]}
val = {"foo": "bar", "baz": [1, 2, 3]}
val_s = json.dumps(val)
p = parameters.Parameter('p', schema, val_s)
p = self.new_parameter('p', schema, val_s)
self.assertEqual(val_s, p.value())
self.assertEqual(val, p.parsed)
@ -322,7 +331,7 @@ class ParameterTest(testtools.TestCase):
'AllowedValues': ["foo", "bar", "baz"]}
val = {"foo": "bar", "items": [1, 2, 3]}
try:
parameters.Parameter('p', schema, val)
self.new_parameter('p', schema, val)
except ValueError as verr:
self.assertIn("items", str(verr))
else:
@ -334,7 +343,7 @@ class ParameterTest(testtools.TestCase):
'MinLength': 3}
val = {"foo": "bar", "items": [1, 2, 3]}
try:
parameters.Parameter('p', schema, val)
self.new_parameter('p', schema, val)
except ValueError as verr:
self.assertIn('underflows', str(verr))
else:
@ -346,7 +355,7 @@ class ParameterTest(testtools.TestCase):
'MaxLength': 1}
val = {"foo": "bar", "items": [1, 2, 3]}
try:
parameters.Parameter('p', schema, val)
self.new_parameter('p', schema, val)
except ValueError as verr:
self.assertIn('overflows', str(verr))
else:
@ -355,7 +364,7 @@ class ParameterTest(testtools.TestCase):
def test_missing_param(self):
'''Test missing user parameter.'''
self.assertRaises(exception.UserParameterMissing,
parameters.Parameter, 'p',
self.new_parameter, 'p',
{'Type': 'String'})