Add length constraint to Nova Keypair's name property

Sets min/max bounds of the Nova Keypair Resource's name
property to 1 and 255 respectively.

Change-Id: I48fb25eee465938fd7e48e105187d1646c8c64c0
Closes-Bug: #1303865
This commit is contained in:
Sirushti Murugesan 2014-09-01 18:47:51 +05:30
parent 65ee563925
commit 805115d6ff
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)