Fix lazy translation UnicodeErrors

upload_utils is concatenating str and Message objects which doesn't work
on python 2, so change the str to a unicode object.

test_quota.py was casting Message objects to str which fails for the
same reason, so rather than cast to str, cast using six.text_type.

See oslo commit 2cfc1a78d8063cf20083cf7df796d730a576551c for history and
details on why the Message object doesn't support __str__ casting.

This is a small part of a larger effort that the oslo team will be
rolling out with graduation and adoption of the oslo-i18n library.

Closes-Bug: #1334774

Conflicts:
	glance/api/v1/upload_utils.py
	glance/tests/unit/test_quota.py

Change-Id: I9d3f29c661f1afffabbbbb499f48d37ef5a8fcee
(cherry picked from commit 834e1f2150)
This commit is contained in:
Matt Riedemann 2014-06-26 11:59:08 -07:00 committed by Ihar Hrachyshka
parent cced523ca5
commit 5508653052
2 changed files with 11 additions and 9 deletions

View File

@ -161,7 +161,7 @@ def upload_data_to_store(req, image_meta, image_data, store, notifier):
content_type='text/plain') content_type='text/plain')
except exception.Duplicate as e: except exception.Duplicate as e:
msg = _("Attempt to upload duplicate image: %s") % e msg = u"Attempt to upload duplicate image: %s" % e
LOG.debug(msg) LOG.debug(msg)
# NOTE(dosaboy): do not delete the image since it is likely that this # NOTE(dosaboy): do not delete the image since it is likely that this
# conflict is a result of another concurrent upload that will be # conflict is a result of another concurrent upload that will be
@ -172,7 +172,7 @@ def upload_data_to_store(req, image_meta, image_data, store, notifier):
content_type="text/plain") content_type="text/plain")
except exception.Forbidden as e: except exception.Forbidden as e:
msg = _("Forbidden upload attempt: %s") % e msg = u"Forbidden upload attempt: %s" % e
LOG.debug(msg) LOG.debug(msg)
safe_kill(req, image_id) safe_kill(req, image_id)
notifier.error('image.upload', msg) notifier.error('image.upload', msg)

View File

@ -17,6 +17,8 @@ import mock
from mock import patch from mock import patch
import uuid import uuid
import six
from glance.common import exception from glance.common import exception
import glance.quota import glance.quota
import glance.store import glance.store
@ -296,7 +298,7 @@ class TestImagePropertyQuotas(test_utils.BaseTestCase):
self.image.extra_properties = {'foo': 'bar', 'foo2': 'bar2'} self.image.extra_properties = {'foo': 'bar', 'foo2': 'bar2'}
exc = self.assertRaises(exception.ImagePropertyLimitExceeded, exc = self.assertRaises(exception.ImagePropertyLimitExceeded,
self.image_repo_proxy.save, self.image) self.image_repo_proxy.save, self.image)
self.assertTrue("Attempted: 2, Maximum: 1" in str(exc)) self.assertTrue("Attempted: 2, Maximum: 1" in six.text_type(exc))
def test_save_image_unlimited_image_properties(self): def test_save_image_unlimited_image_properties(self):
self.config(image_property_quota=-1) self.config(image_property_quota=-1)
@ -320,7 +322,7 @@ class TestImagePropertyQuotas(test_utils.BaseTestCase):
self.image.extra_properties = {'foo': 'bar', 'foo2': 'bar2'} self.image.extra_properties = {'foo': 'bar', 'foo2': 'bar2'}
exc = self.assertRaises(exception.ImagePropertyLimitExceeded, exc = self.assertRaises(exception.ImagePropertyLimitExceeded,
self.image_repo_proxy.add, self.image) self.image_repo_proxy.add, self.image)
self.assertTrue("Attempted: 2, Maximum: 1" in str(exc)) self.assertTrue("Attempted: 2, Maximum: 1" in six.text_type(exc))
def test_add_image_unlimited_image_properties(self): def test_add_image_unlimited_image_properties(self):
self.config(image_property_quota=-1) self.config(image_property_quota=-1)
@ -356,7 +358,7 @@ class TestImageTagQuotas(test_utils.BaseTestCase):
exc = self.assertRaises(exception.ImageTagLimitExceeded, exc = self.assertRaises(exception.ImageTagLimitExceeded,
setattr, self.image, 'tags', ['foo', 'bar']) setattr, self.image, 'tags', ['foo', 'bar'])
self.assertTrue('Attempted: 2, Maximum: 0' in str(exc)) self.assertTrue('Attempted: 2, Maximum: 0' in six.text_type(exc))
self.assertEqual(len(self.image.tags), 0) self.assertEqual(len(self.image.tags), 0)
def test_replace_unlimited_image_tags(self): def test_replace_unlimited_image_tags(self):
@ -374,7 +376,7 @@ class TestImageTagQuotas(test_utils.BaseTestCase):
self.image.tags.add('foo') self.image.tags.add('foo')
exc = self.assertRaises(exception.ImageTagLimitExceeded, exc = self.assertRaises(exception.ImageTagLimitExceeded,
self.image.tags.add, 'bar') self.image.tags.add, 'bar')
self.assertTrue('Attempted: 2, Maximum: 1' in str(exc)) self.assertTrue('Attempted: 2, Maximum: 1' in six.text_type(exc))
def test_add_unlimited_image_tags(self): def test_add_unlimited_image_tags(self):
self.config(image_tag_quota=-1) self.config(image_tag_quota=-1)
@ -404,7 +406,7 @@ class TestQuotaImageTagsProxy(test_utils.BaseTestCase):
proxy = glance.quota.QuotaImageTagsProxy(set([])) proxy = glance.quota.QuotaImageTagsProxy(set([]))
exc = self.assertRaises(exception.ImageTagLimitExceeded, exc = self.assertRaises(exception.ImageTagLimitExceeded,
proxy.add, 'bar') proxy.add, 'bar')
self.assertTrue('Attempted: 1, Maximum: 0' in str(exc)) self.assertTrue('Attempted: 1, Maximum: 0' in six.text_type(exc))
def test_equals(self): def test_equals(self):
proxy = glance.quota.QuotaImageTagsProxy(set([])) proxy = glance.quota.QuotaImageTagsProxy(set([]))
@ -500,7 +502,7 @@ class TestImageLocationQuotas(test_utils.BaseTestCase):
] ]
exc = self.assertRaises(exception.ImageLocationLimitExceeded, exc = self.assertRaises(exception.ImageLocationLimitExceeded,
setattr, self.image, 'locations', locations) setattr, self.image, 'locations', locations)
self.assertTrue('Attempted: 3, Maximum: 1' in str(exc)) self.assertTrue('Attempted: 3, Maximum: 1' in six.text_type(exc))
self.assertEqual(len(self.image.locations), 1) self.assertEqual(len(self.image.locations), 1)
def test_replace_unlimited_image_locations(self): def test_replace_unlimited_image_locations(self):
@ -523,7 +525,7 @@ class TestImageLocationQuotas(test_utils.BaseTestCase):
location2 = {"url": "file:///fake2.img.tar.gz", "metadata": {}} location2 = {"url": "file:///fake2.img.tar.gz", "metadata": {}}
exc = self.assertRaises(exception.ImageLocationLimitExceeded, exc = self.assertRaises(exception.ImageLocationLimitExceeded,
self.image.locations.append, location2) self.image.locations.append, location2)
self.assertTrue('Attempted: 2, Maximum: 1' in str(exc)) self.assertTrue('Attempted: 2, Maximum: 1' in six.text_type(exc))
def test_add_unlimited_image_locations(self): def test_add_unlimited_image_locations(self):
self.config(image_location_quota=-1) self.config(image_location_quota=-1)