clean up swift middleware to avoid unicode errors

the swift middleware does not handle environment variables correctly.
this is causing ceilometer errors to be thrown in syslogs in the
check-swift-dsvm-full tests.

this patch adds in unicode support.

Closes-Bug: #1369124
Change-Id: Icf0874efcc69d9889df591b2cb4bbdd3930eaf68
This commit is contained in:
gordon chung 2014-09-15 17:28:20 -04:00
parent ee16c9aced
commit 7ced6cb6bb
2 changed files with 30 additions and 2 deletions

View File

@ -43,6 +43,7 @@ from __future__ import absolute_import
import logging
from oslo.utils import timeutils
import six
from ceilometer.openstack.common import context
from ceilometer import pipeline
@ -145,8 +146,14 @@ class CeilometerMiddleware(object):
def publish_sample(self, env, bytes_received, bytes_sent):
path = env['PATH_INFO']
method = env['REQUEST_METHOD']
headers = dict((header.strip('HTTP_'), env[header]) for header
in env if header.startswith('HTTP_'))
headers = {}
for header in env:
if header.startswith('HTTP_') and env[header]:
key = header.strip('HTTP_')
if isinstance(env[header], six.text_type):
headers[key] = env[header].encode('utf-8')
else:
headers[key] = str(env[header])
try:
container = obj = None

View File

@ -246,6 +246,27 @@ class TestSwiftMiddleware(tests_base.BaseTestCase):
data.resource_metadata['http_header_x_var2'])
self.assertFalse('http_header_x_var3' in data.resource_metadata)
def test_metadata_headers_unicode(self):
app = swift_middleware.CeilometerMiddleware(FakeApp(), {
'metadata_headers': 'unicode'
})
uni = u'\xef\xbd\xa1\xef\xbd\xa5'
req = FakeRequest('/1.0/account/container',
environ={'REQUEST_METHOD': 'GET'},
headers={'UNICODE': uni})
list(app(req.environ, self.start_response))
samples = self.pipeline_manager.pipelines[0].samples
self.assertEqual(2, len(samples))
data = samples[0]
http_headers = [k for k in data.resource_metadata.keys()
if k.startswith('http_header_')]
self.assertEqual(1, len(http_headers))
self.assertEqual('1.0', data.resource_metadata['version'])
self.assertEqual('container', data.resource_metadata['container'])
self.assertIsNone(data.resource_metadata['object'])
self.assertEqual(uni.encode('utf-8'),
data.resource_metadata['http_header_unicode'])
def test_metadata_headers_on_not_existing_header(self):
app = swift_middleware.CeilometerMiddleware(FakeApp(), {
'metadata_headers': 'x-var3'