Skip to content

Commit

Permalink
Reject excessively long image names.
Browse files Browse the repository at this point in the history
Fixes bug 966240

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

Change-Id: I460ffee547496829cbf198b50bca564978abe7f3
  • Loading branch information
Eoghan Glynn committed Mar 29, 2012
1 parent d60d5b1 commit d21cf6b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions glance/registry/db/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,11 @@ def _required_format_absent(format, formats):
"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):
"""
Expand Down
51 changes: 51 additions & 0 deletions glance/tests/unit/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,57 @@ def test_add_image_with_bad_status(self):
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',
Expand Down

0 comments on commit d21cf6b

Please sign in to comment.