From 6e047e0b87d2caffe9d60c305f7ba4dcb17d8167 Mon Sep 17 00:00:00 2001 From: Yaguang Tang Date: Wed, 14 Aug 2013 17:23:12 +0800 Subject: [PATCH] Fix unicode key of azcache can't be stored to memcache The memcache library we are using assumes all strings passed to it are plain ascii, so we need to convert unicode to string. Fix bug #1212164 Change-Id: Ibf36e6b096893cb9f056371bb43cbb441cae6e32 --- nova/availability_zones.py | 2 +- nova/tests/test_availability_zones.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/nova/availability_zones.py b/nova/availability_zones.py index d627fa0d79d1..68b4475655ae 100644 --- a/nova/availability_zones.py +++ b/nova/availability_zones.py @@ -56,7 +56,7 @@ def _reset_cache(): def _make_cache_key(host): - return "azcache-%s" % host + return "azcache-%s" % host.encode('utf-8') def set_availability_zones(context, services): diff --git a/nova/tests/test_availability_zones.py b/nova/tests/test_availability_zones.py index 7b29616ff7b0..18c675a8f1b5 100644 --- a/nova/tests/test_availability_zones.py +++ b/nova/tests/test_availability_zones.py @@ -103,6 +103,16 @@ class AvailabilityZoneTestCases(test.TestCase): self._destroy_service(service) + def test_set_availability_zone_unicode_key(self): + """Test set availability zone cache key is unicode.""" + service = self._create_service_with_topic('network', self.host) + services = db.service_get_all(self.context) + new_service = az.set_availability_zones(self.context, services)[0] + self.assertEquals(type(services[0]['host']), unicode) + cached_key = az._make_cache_key(services[0]['host']) + self.assertEquals(type(cached_key), str) + self._destroy_service(service) + def test_set_availability_zone_not_compute_service(self): """Test not compute service get right availability zone.""" service = self._create_service_with_topic('network', self.host)