Merge "Fix error msg invalid stack or res name"

This commit is contained in:
Jenkins 2014-12-02 10:16:09 +00:00 committed by Gerrit Code Review
commit dcd27c89d6
5 changed files with 32 additions and 41 deletions

View File

@ -155,9 +155,13 @@ class Resource(object):
return super(Resource, cls).__new__(ResourceClass) return super(Resource, cls).__new__(ResourceClass)
def __init__(self, name, definition, stack): def __init__(self, name, definition, stack):
if '/' in name:
raise ValueError(_('Resource name may not contain "/"'))
def _validate_name(res_name):
if '/' in res_name:
message = _('Resource name may not contain "/"')
raise exception.StackValidationFailed(message=message)
_validate_name(name)
self.stack = stack self.stack = stack
self.context = stack.context self.context = stack.context
self.name = name self.name = name

View File

@ -86,12 +86,15 @@ class Stack(collections.Mapping):
stack is already in the database. stack is already in the database.
''' '''
def _validate_stack_name(name):
if not re.match("[a-zA-Z][a-zA-Z0-9_.-]*$", name):
message = _('Invalid stack name %s must contain '
'only alphanumeric or \"_-.\" characters, '
'must start with alpha') % name
raise exception.StackValidationFailed(message=message)
if owner_id is None: if owner_id is None:
if re.match("[a-zA-Z][a-zA-Z0-9_.-]*$", stack_name) is None: _validate_stack_name(stack_name)
raise ValueError(_('Invalid stack name %s'
' must contain only alphanumeric or '
'\"_-.\" characters, must start with alpha'
) % stack_name)
self.id = stack_id self.id = stack_id
self.owner_id = owner_id self.owner_id = owner_id

View File

@ -592,7 +592,7 @@ class StackServiceCreateUpdateDeleteTest(common.HeatTestCase):
stack_name = 'service_create/test_stack' stack_name = 'service_create/test_stack'
stack = get_wordpress_stack('test_stack', self.ctx) stack = get_wordpress_stack('test_stack', self.ctx)
self.assertRaises(ValueError, self.assertRaises(dispatcher.ExpectedException,
self.man.create_stack, self.man.create_stack,
self.ctx, stack_name, stack.t.t, {}, None, {}) self.ctx, stack_name, stack.t.t, {}, None, {})
@ -603,7 +603,7 @@ class StackServiceCreateUpdateDeleteTest(common.HeatTestCase):
tmpl['Resources']['Web/Server'] = tmpl['Resources']['WebServer'] tmpl['Resources']['Web/Server'] = tmpl['Resources']['WebServer']
del tmpl['Resources']['WebServer'] del tmpl['Resources']['WebServer']
self.assertRaises(ValueError, self.assertRaises(dispatcher.ExpectedException,
self.man.create_stack, self.man.create_stack,
self.ctx, stack_name, self.ctx, stack_name,
stack.t.t, {}, None, {}) stack.t.t, {}, None, {})

View File

@ -3671,38 +3671,13 @@ class StackTest(common.HeatTestCase):
self.assertIsInstance(stack, parser.Stack) self.assertIsInstance(stack, parser.Stack)
def test_stack_name_invalid(self): def test_stack_name_invalid(self):
self.assertRaises(ValueError, parser.Stack, self.ctx, '_foo', stack_names = ['_foo', '1bad', '.kcats', 'test stack', ' teststack',
self.tmpl) '^-^', '\"stack\"', '1234', 'cat|dog', '$(foo)',
self.assertRaises(ValueError, parser.Stack, self.ctx, '1bad', 'test/stack', 'test\stack', 'test::stack', 'test;stack',
self.tmpl) 'test~stack', '#test']
self.assertRaises(ValueError, parser.Stack, self.ctx, '.kcats', for stack_name in stack_names:
self.tmpl) self.assertRaises(exception.StackValidationFailed, parser.Stack,
self.assertRaises(ValueError, parser.Stack, self.ctx, 'test stack', self.ctx, stack_name, self.tmpl)
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, ' teststack',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, '^-^',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, '\"stack\"',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, '1234',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, 'cat|dog',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, '$(foo)',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, 'test/stack',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, 'test\stack',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, 'test::stack',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, 'test;stack',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, 'test~stack',
self.tmpl)
self.assertRaises(ValueError, parser.Stack, self.ctx, '#test',
self.tmpl)
def test_resource_state_get_att(self): def test_resource_state_get_att(self):
tmpl = { tmpl = {

View File

@ -80,6 +80,15 @@ class ResourceTest(common.HeatTestCase):
self.assertIsInstance(res, generic_rsrc.GenericResource) self.assertIsInstance(res, generic_rsrc.GenericResource)
self.assertEqual("INIT", res.action) self.assertEqual("INIT", res.action)
def test_resource_invalid_name(self):
snippet = rsrc_defn.ResourceDefinition('wrong/name',
'GenericResourceType')
ex = self.assertRaises(exception.StackValidationFailed,
resource.Resource, 'wrong/name',
snippet, self.stack)
self.assertEqual('Resource name may not contain "/"',
six.text_type(ex))
def test_resource_new_stack_not_stored(self): def test_resource_new_stack_not_stored(self):
snippet = rsrc_defn.ResourceDefinition('aresource', snippet = rsrc_defn.ResourceDefinition('aresource',
'GenericResourceType') 'GenericResourceType')