From 805115d6ff57e742bc59761ec16fedc15d47e638 Mon Sep 17 00:00:00 2001 From: Sirushti Murugesan Date: Mon, 1 Sep 2014 18:47:51 +0530 Subject: [PATCH] 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 --- heat/engine/resources/nova_keypair.py | 5 +++- heat/tests/test_nova_keypair.py | 34 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/heat/engine/resources/nova_keypair.py b/heat/engine/resources/nova_keypair.py index 5d67d9c2d..1b6fadb6d 100644 --- a/heat/engine/resources/nova_keypair.py +++ b/heat/engine/resources/nova_keypair.py @@ -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, diff --git a/heat/tests/test_nova_keypair.py b/heat/tests/test_nova_keypair.py index 422a90fe0..ea4066219 100644 --- a/heat/tests/test_nova_keypair.py +++ b/heat/tests/test_nova_keypair.py @@ -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)