Reject excessively long image names.

Fixes bug 966240

Image names longer than 255 characters are rejected with 400 BadRequest
on creation or update.

Change-Id: I460ffee547496829cbf198b50bca564978abe7f3
This commit is contained in:
Eoghan Glynn 2012-03-29 17:39:11 +01:00
parent 9fd589c869
commit 8b7a830c04
2 changed files with 56 additions and 0 deletions

View File

@ -465,6 +465,11 @@ def validate_image(values):
"and disk formats must match.")
raise exception.Invalid(msg)
name = values.get('name')
if name and len(name) > 255:
msg = _('Image name too long: %d') % len(name)
raise exception.Invalid(msg)
def _image_update(context, values, image_id, purge_props=False):
"""

View File

@ -1061,6 +1061,57 @@ class TestRegistryClient(base.IsolatedUnitTest):
self.client.add_image,
fixture)
def test_add_image_with_acceptably_long_name(self):
"""Tests adding image with acceptably long name"""
name = 'x' * 255
fixture = {'name': name,
'is_public': True,
'disk_format': 'vmdk',
'container_format': 'ovf',
'size': 19,
'location': "file:///tmp/glance-tests/2",
}
new_image = self.client.add_image(fixture)
data = self.client.get_image(new_image['id'])
self.assertEquals(name, data['name'])
def test_add_image_with_excessively_long_name(self):
"""Tests adding image with excessively long name"""
name = 'x' * 256
fixture = {'name': name,
'is_public': True,
'disk_format': 'vmdk',
'container_format': 'ovf',
'size': 19,
'location': "file:///tmp/glance-tests/2",
}
self.assertRaises(exception.Invalid,
self.client.add_image,
fixture)
def test_update_image_with_acceptably_long_name(self):
"""Tests updating image with acceptably long name"""
name = 'x' * 255
fixture = {'name': name}
self.assertTrue(self.client.update_image(UUID2, fixture))
data = self.client.get_image(UUID2)
self.assertEquals(name, data['name'])
def test_update_image_with_excessively_long_name(self):
"""Tests updating image with excessively long name"""
name = 'x' * 256
fixture = {'name': name}
self.assertRaises(exception.Invalid,
self.client.update_image,
UUID2,
fixture)
def test_update_image(self):
"""Tests that the /images PUT registry API updates the image"""
fixture = {'name': 'fake public image #2',