From 834e1f2150c6f2afd92036e6b7f53afee4006682 Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Thu, 26 Jun 2014 11:59:08 -0700 Subject: [PATCH] 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 Change-Id: I9d3f29c661f1afffabbbbb499f48d37ef5a8fcee --- glance/api/v1/upload_utils.py | 4 ++-- glance/tests/unit/test_quota.py | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/glance/api/v1/upload_utils.py b/glance/api/v1/upload_utils.py index 88d9c7235d..31788c3d39 100644 --- a/glance/api/v1/upload_utils.py +++ b/glance/api/v1/upload_utils.py @@ -162,7 +162,7 @@ def upload_data_to_store(req, image_meta, image_data, store, notifier): content_type='text/plain') 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) # NOTE(dosaboy): do not delete the image since it is likely that this # conflict is a result of another concurrent upload that will be @@ -173,7 +173,7 @@ def upload_data_to_store(req, image_meta, image_data, store, notifier): content_type="text/plain") except exception.Forbidden as e: - msg = "Forbidden upload attempt: %s" % e + msg = u"Forbidden upload attempt: %s" % e LOG.debug(msg) safe_kill(req, image_id) notifier.error('image.upload', msg) diff --git a/glance/tests/unit/test_quota.py b/glance/tests/unit/test_quota.py index 202f6364e8..7097fb19fe 100644 --- a/glance/tests/unit/test_quota.py +++ b/glance/tests/unit/test_quota.py @@ -17,6 +17,8 @@ import mock from mock import patch import uuid +import six + from glance.common import exception from glance.openstack.common import units import glance.quota @@ -338,7 +340,7 @@ class TestImagePropertyQuotas(test_utils.BaseTestCase): self.image.extra_properties = {'foo': 'bar', 'foo2': 'bar2'} exc = self.assertRaises(exception.ImagePropertyLimitExceeded, self.image_repo_proxy.save, self.image) - self.assertIn("Attempted: 2, Maximum: 1", str(exc)) + self.assertIn("Attempted: 2, Maximum: 1", six.text_type(exc)) def test_save_image_unlimited_image_properties(self): self.config(image_property_quota=-1) @@ -362,7 +364,7 @@ class TestImagePropertyQuotas(test_utils.BaseTestCase): self.image.extra_properties = {'foo': 'bar', 'foo2': 'bar2'} exc = self.assertRaises(exception.ImagePropertyLimitExceeded, self.image_repo_proxy.add, self.image) - self.assertIn("Attempted: 2, Maximum: 1", str(exc)) + self.assertIn("Attempted: 2, Maximum: 1", six.text_type(exc)) def test_add_image_unlimited_image_properties(self): self.config(image_property_quota=-1) @@ -398,7 +400,7 @@ class TestImageTagQuotas(test_utils.BaseTestCase): exc = self.assertRaises(exception.ImageTagLimitExceeded, setattr, self.image, 'tags', ['foo', 'bar']) - self.assertIn('Attempted: 2, Maximum: 0', str(exc)) + self.assertIn('Attempted: 2, Maximum: 0', six.text_type(exc)) self.assertEqual(len(self.image.tags), 0) def test_replace_unlimited_image_tags(self): @@ -416,7 +418,7 @@ class TestImageTagQuotas(test_utils.BaseTestCase): self.image.tags.add('foo') exc = self.assertRaises(exception.ImageTagLimitExceeded, self.image.tags.add, 'bar') - self.assertIn('Attempted: 2, Maximum: 1', str(exc)) + self.assertIn('Attempted: 2, Maximum: 1', six.text_type(exc)) def test_add_unlimited_image_tags(self): self.config(image_tag_quota=-1) @@ -446,7 +448,7 @@ class TestQuotaImageTagsProxy(test_utils.BaseTestCase): proxy = glance.quota.QuotaImageTagsProxy(set([])) exc = self.assertRaises(exception.ImageTagLimitExceeded, proxy.add, 'bar') - self.assertIn('Attempted: 1, Maximum: 0', str(exc)) + self.assertIn('Attempted: 1, Maximum: 0', six.text_type(exc)) def test_equals(self): proxy = glance.quota.QuotaImageTagsProxy(set([])) @@ -542,7 +544,7 @@ class TestImageLocationQuotas(test_utils.BaseTestCase): ] exc = self.assertRaises(exception.ImageLocationLimitExceeded, setattr, self.image, 'locations', locations) - self.assertIn('Attempted: 3, Maximum: 1', str(exc)) + self.assertIn('Attempted: 3, Maximum: 1', six.text_type(exc)) self.assertEqual(len(self.image.locations), 1) def test_replace_unlimited_image_locations(self): @@ -565,7 +567,7 @@ class TestImageLocationQuotas(test_utils.BaseTestCase): location2 = {"url": "file:///fake2.img.tar.gz", "metadata": {}} exc = self.assertRaises(exception.ImageLocationLimitExceeded, self.image.locations.append, location2) - self.assertIn('Attempted: 2, Maximum: 1', str(exc)) + self.assertIn('Attempted: 2, Maximum: 1', six.text_type(exc)) def test_add_unlimited_image_locations(self): self.config(image_location_quota=-1)