Python3: encode / decode data as utf8

Because python3 is more strict about string / bytes we need to start
encoding / decoding things as utf8.

Change-Id: Id1102be142bef5eeb96de69aaa8f653bdb1903d8
Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
Paul Belanger 2017-05-18 14:07:16 -04:00
parent 1aa79c686b
commit aa741d6210
No known key found for this signature in database
GPG Key ID: 611A80832067AF38
4 changed files with 20 additions and 18 deletions

View File

@ -132,7 +132,7 @@ class StatsdFixture(fixtures.Fixture):
def _cleanup(self):
self.running = False
os.write(self.wake_write, '1\n')
os.write(self.wake_write, b'1\n')
self.thread.join()
@ -252,7 +252,7 @@ class BaseTestCase(testtools.TestCase):
start = time.time()
while time.time() < (start + 5):
for stat in self.statsd.stats:
k, v = stat.split(':')
k, v = stat.decode('utf8').split(':')
if key == k:
if value is None and kind is None:
return
@ -302,12 +302,13 @@ class DBTestCase(BaseTestCase):
configfile = os.path.join(os.path.dirname(__file__),
'fixtures', filename)
(fd, path) = tempfile.mkstemp()
with open(configfile) as conf_fd:
config = conf_fd.read()
os.write(fd, config.format(images_dir=images_dir.path,
with open(configfile, 'rb') as conf_fd:
config = conf_fd.read().decode('utf8')
data = config.format(images_dir=images_dir.path,
zookeeper_host=self.zookeeper_host,
zookeeper_port=self.zookeeper_port,
zookeeper_chroot=self.zookeeper_chroot))
zookeeper_chroot=self.zookeeper_chroot)
os.write(fd, data.encode('utf8'))
os.close(fd)
self._config_images_dir = images_dir
validator = ConfigValidator(path)
@ -324,7 +325,7 @@ class DBTestCase(BaseTestCase):
configfile = os.path.join(os.path.dirname(__file__),
'fixtures', 'secure.conf')
(fd, path) = tempfile.mkstemp()
with open(configfile) as conf_fd:
with open(configfile, 'rb') as conf_fd:
config = conf_fd.read()
os.write(fd, config)
#os.write(fd, config.format(dburi=self.dburi))

View File

@ -38,10 +38,10 @@ class TestWebApp(tests.DBTestCase):
req = request.Request(
"http://localhost:%s/image-list" % port)
f = request.urlopen(req)
self.assertEqual(f.info().getheader('Content-Type'),
self.assertEqual(f.info().get('Content-Type'),
'text/plain; charset=UTF-8')
data = f.read()
self.assertTrue('fake-image' in data)
self.assertTrue('fake-image' in data.decode('utf8'))
def test_dib_image_list_json(self):
configfile = self.setup_config('node.yaml')
@ -58,10 +58,10 @@ class TestWebApp(tests.DBTestCase):
req = request.Request(
"http://localhost:%s/dib-image-list.json" % port)
f = request.urlopen(req)
self.assertEqual(f.info().getheader('Content-Type'),
self.assertEqual(f.info().get('Content-Type'),
'application/json')
data = f.read()
objs = json.loads(data)
objs = json.loads(data.decode('utf8'))
# make sure this is valid json and has some of the
# non-changing keys
self.assertDictContainsSubset({'id': 'fake-image-0000000001',

View File

@ -96,6 +96,7 @@ class WebApp(threading.Thread):
content_type = 'text/plain'
response = webob.Response(body=output,
charset='UTF-8',
content_type=content_type)
response.headers['Access-Control-Allow-Origin'] = '*'

View File

@ -183,7 +183,7 @@ class BaseModel(object):
Used for storing the object data in ZooKeeper.
'''
return json.dumps(self.toDict())
return json.dumps(self.toDict()).encode('utf8')
class ImageBuild(BaseModel):
@ -588,8 +588,8 @@ class ZooKeeper(object):
def _requestLockPath(self, request):
return "%s/%s" % (self.REQUEST_LOCK_ROOT, request)
def _strToDict(self, data):
return json.loads(data)
def _bytesToDict(self, data):
return json.loads(data.decode('utf8'))
def _getImageBuildLock(self, image, blocking=True, timeout=None):
lock_path = self._imageBuildLockPath(image)
@ -893,7 +893,7 @@ class ZooKeeper(object):
except kze.NoNodeError:
return None
d = ImageBuild.fromDict(self._strToDict(data), build_number)
d = ImageBuild.fromDict(self._bytesToDict(data), build_number)
d.stat = stat
return d
@ -1012,7 +1012,7 @@ class ZooKeeper(object):
return None
d = ImageUpload.fromDict(
self._strToDict(data), build_number, provider, image, upload_number
self._bytesToDict(data), build_number, provider, image, upload_number
)
d.stat = stat
return d
@ -1341,7 +1341,7 @@ class ZooKeeper(object):
except kze.NoNodeError:
return None
d = NodeRequest.fromDict(self._strToDict(data), request)
d = NodeRequest.fromDict(self._bytesToDict(data), request)
d.stat = stat
return d
@ -1507,7 +1507,7 @@ class ZooKeeper(object):
if not data:
return None
d = Node.fromDict(self._strToDict(data), node)
d = Node.fromDict(self._bytesToDict(data), node)
d.id = node
d.stat = stat
return d