Fix unittests on Python 3.x

Change-Id: Id9ba3d56006adc8e440c87971d033b217c2589bd
This commit is contained in:
Alexandru Coman 2017-03-20 17:28:05 +02:00
parent b019452af0
commit 349a9035d7
No known key found for this signature in database
GPG Key ID: A7B6A9021F704507
3 changed files with 19 additions and 4 deletions

View File

@ -346,7 +346,7 @@ class Resource(model.Model):
def _load_models(self):
models = globals().copy()
for _, model_cls in models.iteritems():
for _, model_cls in models.items():
endpoint = getattr(model_cls, "_endpoint", None)
if endpoint is not None:
regexp = endpoint.format(
@ -365,7 +365,7 @@ class Resource(model.Model):
"""Return the associated resource."""
references = {"resource_id": None, "parent_id": None,
"grandparent_id": None}
for model_cls, regexp in self._regexp.iteritems():
for model_cls, regexp in self._regexp.items():
match = regexp.search(self.resource_ref)
if match is not None:
references.update(match.groupdict())

View File

@ -208,3 +208,15 @@ def run_once(function, state={}, errors={}):
def get_client(url, username, password, allow_insecure, ca_bundle):
"""Create a new client for the HNV REST API."""
return _HNVClient(url, username, password, allow_insecure, ca_bundle)
def get_as_string(value):
if value is None or isinstance(value, six.text_type):
return value
else:
try:
return value.decode()
except Exception:
# This is important, because None will be returned,
# but not that serious to raise an exception.
LOG.error("Couldn't decode: %r", value)

View File

@ -17,6 +17,8 @@
import json
import pkg_resources
from hnv.common import utils
class FakeResponse(object):
@ -29,9 +31,10 @@ class FakeResponse(object):
def _load_resource(self, resource):
"""Load the json response for the required resource."""
if resource not in self._cache:
json_response = pkg_resources.resource_stream(
resource_stream = pkg_resources.resource_stream(
self._resources, resource)
self._cache[resource] = json.load(json_response)
json_response = utils.get_as_string(resource_stream.read())
self._cache[resource] = json.loads(json_response)
return self._cache[resource]
def logical_networks(self):