From f036e9898a69f5041f9cde02e3652c29e2de1643 Mon Sep 17 00:00:00 2001 From: Ross Brattain Date: Mon, 5 Dec 2016 16:11:54 -0500 Subject: Add support for Python 3 Porting to Python3 using Openstack guidelines: https://wiki.openstack.org/wiki/Python3 This passes unittests on Python 3.5 and passes opnfv_smoke suite Updates: use six for urlparse and urlopen fix exception.message attribute removal run unittests on python3 use unitest.mock on python 3 fix open mock for vsperf fix float division by using delta/eplison comparison use unicode in StringIO use plugin/sample_config.yaml relative path from test case fixed apexlake unittests upgraded to mock 2.0.0 to match python3 unittest.mock features fixed flake8 issues implement safe JSON decode with oslo_serialization.jsonutils.dump_as_bytes() implement safe unicode encode/decode with oslo_utils.encodeutils heat: convert pub key file from bytes to unicode pkg_resources returns raw bytes, in python3 we have to decode this to utf-8 unicode so JSON can encode it for heat template JIRA: YARDSTICK-452 Change-Id: Ib80dd1d0c0eb0592acd832b82f6a7f8f7c20bfda Signed-off-by: Ross Brattain --- yardstick/common/constants.py | 1 + yardstick/common/httpClient.py | 6 ++++-- yardstick/common/openstack_utils.py | 1 + yardstick/common/task_template.py | 2 ++ yardstick/common/template_format.py | 6 ++++-- yardstick/common/utils.py | 18 +++++++++++------- 6 files changed, 23 insertions(+), 11 deletions(-) (limited to 'yardstick/common') diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py index 174d39bfe..d99e216f4 100644 --- a/yardstick/common/constants.py +++ b/yardstick/common/constants.py @@ -6,6 +6,7 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +from __future__ import absolute_import import os DOCKER_URL = 'unix://var/run/docker.sock' diff --git a/yardstick/common/httpClient.py b/yardstick/common/httpClient.py index 6acd0303d..11c2d752d 100644 --- a/yardstick/common/httpClient.py +++ b/yardstick/common/httpClient.py @@ -6,9 +6,11 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -import json +from __future__ import absolute_import + import logging +from oslo_serialization import jsonutils import requests logger = logging.getLogger(__name__) @@ -17,7 +19,7 @@ logger = logging.getLogger(__name__) class HttpClient(object): def post(self, url, data): - data = json.dumps(data) + data = jsonutils.dump_as_bytes(data) headers = {'Content-Type': 'application/json'} try: response = requests.post(url, data=data, headers=headers) diff --git a/yardstick/common/openstack_utils.py b/yardstick/common/openstack_utils.py index d8dc61ef6..e351d16d3 100644 --- a/yardstick/common/openstack_utils.py +++ b/yardstick/common/openstack_utils.py @@ -7,6 +7,7 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +from __future__ import absolute_import import os import logging diff --git a/yardstick/common/task_template.py b/yardstick/common/task_template.py index 2739323bd..bda8a1b13 100755 --- a/yardstick/common/task_template.py +++ b/yardstick/common/task_template.py @@ -7,12 +7,14 @@ # http://www.apache.org/licenses/LICENSE-2.0 # yardstick: this file is copied from rally and slightly modified ############################################################################## +from __future__ import absolute_import import re import jinja2 import jinja2.meta class TaskTemplate(object): + @classmethod def render(cls, task_template, **kwargs): """Render jinja2 task template to Yardstick input task. diff --git a/yardstick/common/template_format.py b/yardstick/common/template_format.py index 2deaf393c..2432c5dc6 100644 --- a/yardstick/common/template_format.py +++ b/yardstick/common/template_format.py @@ -12,8 +12,10 @@ # yardstick: this file is copied from python-heatclient and slightly modified -import json +from __future__ import absolute_import + import yaml +from oslo_serialization import jsonutils if hasattr(yaml, 'CSafeLoader'): yaml_loader = yaml.CSafeLoader @@ -46,7 +48,7 @@ def parse(tmpl_str): JSON or YAML format. ''' if tmpl_str.startswith('{'): - tpl = json.loads(tmpl_str) + tpl = jsonutils.loads(tmpl_str) else: try: tpl = yaml.load(tmpl_str, Loader=yaml_loader) diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index 3ecb0ae20..57ace14e6 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -15,17 +15,21 @@ # yardstick comment: this is a modified copy of rally/rally/common/utils.py -import os -import sys -import yaml +from __future__ import absolute_import +from __future__ import print_function + import errno -import subprocess import logging +import os +import subprocess +import sys +from functools import reduce -from oslo_utils import importutils +import yaml from keystoneauth1 import identity from keystoneauth1 import session from neutronclient.v2_0 import client +from oslo_utils import importutils import yardstick @@ -94,12 +98,12 @@ def get_para_from_yaml(file_path, args): value = reduce(func, args.split('.'), value) if value is None: - print 'parameter not found' + print('parameter not found') return None return value else: - print 'file not exist' + print('file not exist') return None -- cgit 1.2.3-korg