Merge "Add length constraint to Nova Keypair's name property"

This commit is contained in:
Jenkins 2014-09-02 00:28:01 +00:00 committed by Gerrit Code Review
commit c887dfb2a5
2 changed files with 38 additions and 1 deletions

View File

@ -49,7 +49,10 @@ class KeyPair(resource.Resource):
NAME: properties.Schema(
properties.Schema.STRING,
_('The name of the key pair.'),
required=True
required=True,
constraints=[
constraints.Length(min=1, max=255)
]
),
SAVE_PRIVATE_KEY: properties.Schema(
properties.Schema.BOOLEAN,

View File

@ -13,7 +13,9 @@
import collections
import copy
import six
from heat.common import exception
from heat.engine.clients.os import nova
from heat.engine.resources import nova_keypair
from heat.engine import scheduler
@ -91,6 +93,38 @@ class NovaKeyPairTest(HeatTestCase):
self.assertEqual(tp_test.resource_id, created_key.name)
self.m.VerifyAll()
def test_create_key_empty_name(self):
"""Test creation of a keypair whose name is of length zero."""
key_name = ""
template = copy.deepcopy(self.kp_template)
template['resources']['kp']['properties']['name'] = key_name
stack = utils.parse_stack(template)
definition = stack.t.resource_definitions(stack)['kp']
kp_res = nova_keypair.KeyPair('kp', definition, stack)
self.m.ReplayAll()
create = scheduler.TaskRunner(kp_res.create)
error = self.assertRaises(exception.ResourceFailure, create)
self.assertIn("Property error", six.text_type(error))
self.assertIn("name length (0) is out of range (min: 1, max: 255)",
six.text_type(error))
self.m.VerifyAll()
def test_create_key_excess_name_length(self):
"""Test creation of a keypair whose name is of excess length."""
key_name = 'k' * 256
template = copy.deepcopy(self.kp_template)
template['resources']['kp']['properties']['name'] = key_name
stack = utils.parse_stack(template)
definition = stack.t.resource_definitions(stack)['kp']
kp_res = nova_keypair.KeyPair('kp', definition, stack)
self.m.ReplayAll()
create = scheduler.TaskRunner(kp_res.create)
error = self.assertRaises(exception.ResourceFailure, create)
self.assertIn("Property error", six.text_type(error))
self.assertIn("name length (256) is out of range (min: 1, max: 255)",
six.text_type(error))
self.m.VerifyAll()
def test_delete_key(self):
"""Test basic delete."""
test_res = self._get_test_resource(self.kp_template)