diff --git a/neutron/agent/linux/utils.py b/neutron/agent/linux/utils.py index 30d6a206df3..bff3102b0bf 100644 --- a/neutron/agent/linux/utils.py +++ b/neutron/agent/linux/utils.py @@ -29,8 +29,8 @@ from eventlet import greenthread from oslo_config import cfg from oslo_log import log as logging from oslo_rootwrap import client +from oslo_utils import encodeutils from oslo_utils import excutils -import six from six.moves import http_client as httplib from neutron._i18n import _, _LE @@ -104,11 +104,10 @@ def execute(cmd, process_input=None, addl_env=None, check_exit_code=True, return_stderr=False, log_fail_as_error=True, extra_ok_codes=None, run_as_root=False): try: - if (process_input is None or - isinstance(process_input, six.binary_type)): - _process_input = process_input + if process_input is not None: + _process_input = encodeutils.to_utf8(process_input) else: - _process_input = process_input.encode('utf-8') + _process_input = None if run_as_root and cfg.CONF.AGENT.root_helper_daemon: returncode, _stdout, _stderr = ( execute_rootwrap_daemon(cmd, process_input, addl_env)) @@ -153,8 +152,7 @@ def get_interface_mac(interface): MAC_END = 24 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) dev = interface[:constants.DEVICE_NAME_MAX_LEN] - if isinstance(dev, six.text_type): - dev = dev.encode('utf-8') + dev = encodeutils.to_utf8(dev) info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', dev)) return ''.join(['%02x:' % ord(char) for char in info[MAC_START:MAC_END]])[:-1] diff --git a/neutron/agent/metadata/agent.py b/neutron/agent/metadata/agent.py index 1f95cc3d7ab..db83093dcda 100644 --- a/neutron/agent/metadata/agent.py +++ b/neutron/agent/metadata/agent.py @@ -20,6 +20,7 @@ from oslo_config import cfg from oslo_log import log as logging import oslo_messaging from oslo_service import loopingcall +from oslo_utils import encodeutils import six import six.moves.urllib.parse as urlparse import webob @@ -223,10 +224,8 @@ class MetadataProxyHandler(object): def _sign_instance_id(self, instance_id): secret = self.conf.metadata_proxy_shared_secret - if isinstance(secret, six.text_type): - secret = secret.encode('utf-8') - if isinstance(instance_id, six.text_type): - instance_id = instance_id.encode('utf-8') + secret = encodeutils.to_utf8(secret) + instance_id = encodeutils.to_utf8(instance_id) return hmac.new(secret, instance_id, hashlib.sha256).hexdigest() diff --git a/neutron/agent/windows/utils.py b/neutron/agent/windows/utils.py index 115593ed06f..359966a73bf 100644 --- a/neutron/agent/windows/utils.py +++ b/neutron/agent/windows/utils.py @@ -18,7 +18,7 @@ import os from eventlet.green import subprocess from eventlet import greenthread from oslo_log import log as logging -import six +from oslo_utils import encodeutils from neutron._i18n import _ from neutron.common import utils @@ -50,11 +50,10 @@ def execute(cmd, process_input=None, addl_env=None, extra_ok_codes=None, run_as_root=False, do_decode=True): try: - if (process_input is None or - isinstance(process_input, six.binary_type)): - _process_input = process_input + if process_input is not None: + _process_input = encodeutils.to_utf8(process_input) else: - _process_input = process_input.encode('utf-8') + _process_input = None obj, cmd = create_process(cmd, addl_env=addl_env) _stdout, _stderr = obj.communicate(_process_input) obj.stdin.close() diff --git a/neutron/plugins/common/utils.py b/neutron/plugins/common/utils.py index 6e9d9016094..a9efcddffe8 100644 --- a/neutron/plugins/common/utils.py +++ b/neutron/plugins/common/utils.py @@ -20,7 +20,7 @@ import hashlib from oslo_config import cfg from oslo_log import log as logging -import six +from oslo_utils import encodeutils import webob.exc from neutron._i18n import _, _LI @@ -180,10 +180,7 @@ def get_interface_name(name, prefix='', max_len=n_const.DEVICE_NAME_MAX_LEN): "given length for an interface name.")) namelen = max_len - len(prefix) - INTERFACE_HASH_LEN - if isinstance(name, six.text_type): - hashed_name = hashlib.sha1(name.encode('utf-8')) - else: - hashed_name = hashlib.sha1(name) + hashed_name = hashlib.sha1(encodeutils.to_utf8(name)) new_name = ('%(prefix)s%(truncated)s%(hash)s' % {'prefix': prefix, 'truncated': name[0:namelen], 'hash': hashed_name.hexdigest()[0:INTERFACE_HASH_LEN]}) diff --git a/neutron/tests/common/agents/ovs_agent.py b/neutron/tests/common/agents/ovs_agent.py index a5c9c2514c0..e10632582db 100755 --- a/neutron/tests/common/agents/ovs_agent.py +++ b/neutron/tests/common/agents/ovs_agent.py @@ -15,6 +15,7 @@ import hashlib import sys +from oslo_utils import encodeutils from neutron.cmd.eventlet.plugins.ovs_neutron_agent import main as _main from neutron.common import constants as n_const @@ -30,10 +31,13 @@ def get_tunnel_name_full(cls, network_type, local_ip, remote_ip): # Remove length of network_type and two dashes hashlen = (n_const.DEVICE_NAME_MAX_LEN - len(network_type) - 2) // 2 - remote_ip_hex = remote_ip_hex.encode('utf-8') - remote_ip_hash = hashlib.sha1(remote_ip_hex).hexdigest()[0:hashlen] - local_ip_hex = cls.get_ip_in_hex(local_ip).encode('utf-8') - source_ip_hash = hashlib.sha1(local_ip_hex).hexdigest()[0:hashlen] + remote_ip_hex = encodeutils.to_utf8(remote_ip_hex) + remote_ip_hash = hashlib.sha1(remote_ip_hex).hexdigest()[:hashlen] + + local_ip_hex = cls.get_ip_in_hex(local_ip) + local_ip_hex = encodeutils.to_utf8(local_ip_hex) + source_ip_hash = hashlib.sha1(local_ip_hex).hexdigest()[:hashlen] + return '%s-%s-%s' % (network_type, source_ip_hash, remote_ip_hash) OVSNeutronAgent.get_tunnel_name = get_tunnel_name_full diff --git a/neutron/tests/functional/test_server.py b/neutron/tests/functional/test_server.py index 5de52eefdc6..891bdf3276d 100644 --- a/neutron/tests/functional/test_server.py +++ b/neutron/tests/functional/test_server.py @@ -35,7 +35,7 @@ CONF = cfg.CONF # This message will be written to temporary file each time # start method is called. -FAKE_START_MSG = "start".encode("utf-8") +FAKE_START_MSG = b"start" TARGET_PLUGIN = 'neutron.plugins.ml2.plugin.Ml2Plugin' diff --git a/neutron/tests/unit/agent/linux/test_utils.py b/neutron/tests/unit/agent/linux/test_utils.py index 362c512b2af..ead21afba0d 100644 --- a/neutron/tests/unit/agent/linux/test_utils.py +++ b/neutron/tests/unit/agent/linux/test_utils.py @@ -135,12 +135,11 @@ class AgentUtilsExecuteTest(base.BaseTestCase): self.mock_popen.return_value = [bytes_odata, b''] result = utils.execute(['cat'], process_input=str_idata) self.mock_popen.assert_called_once_with(bytes_idata) - self.assertEqual(str_odata, result) else: self.mock_popen.return_value = [str_odata, ''] result = utils.execute(['cat'], process_input=str_idata) self.mock_popen.assert_called_once_with(str_idata) - self.assertEqual(str_odata, result) + self.assertEqual(str_odata, result) def test_return_str_data(self): str_data = "%s\n" % self.test_file diff --git a/neutron/tests/unit/api/test_extensions.py b/neutron/tests/unit/api/test_extensions.py index c0ee4049509..3e1cc862817 100644 --- a/neutron/tests/unit/api/test_extensions.py +++ b/neutron/tests/unit/api/test_extensions.py @@ -529,7 +529,7 @@ class RequestExtensionTest(base.BaseTestCase): def extend_response_data(req, res): data = jsonutils.loads(res.body) data['FOXNSOX:extended_key'] = req.GET.get('extended_key') - res.body = jsonutils.dumps(data).encode('utf-8') + res.body = jsonutils.dump_as_bytes(data) return res app = self._setup_app_with_request_handler(extend_response_data, 'GET') @@ -555,7 +555,7 @@ class RequestExtensionTest(base.BaseTestCase): def _update_handler(req, res): data = jsonutils.loads(res.body) data['uneditable'] = req.params['uneditable'] - res.body = jsonutils.dumps(data).encode('utf-8') + res.body = jsonutils.dump_as_bytes(data) return res base_app = webtest.TestApp(setup_base_app(self)) diff --git a/neutron/tests/unit/extensions/foxinsocks.py b/neutron/tests/unit/extensions/foxinsocks.py index d42f2b4942c..d77dc764c95 100644 --- a/neutron/tests/unit/extensions/foxinsocks.py +++ b/neutron/tests/unit/extensions/foxinsocks.py @@ -77,7 +77,7 @@ class Foxinsocks(extensions.ExtensionDescriptor): # You can use content type header to test for XML. data = jsonutils.loads(res.body) data['FOXNSOX:googoose'] = req.GET.get('chewing') - res.body = jsonutils.dumps(data).encode('utf-8') + res.body = jsonutils.dump_as_bytes(data) return res req_ext1 = extensions.RequestExtension('GET', '/dummy_resources/:(id)', @@ -89,7 +89,7 @@ class Foxinsocks(extensions.ExtensionDescriptor): # You can use content type header to test for XML. data = jsonutils.loads(res.body) data['FOXNSOX:big_bands'] = 'Pig Bands!' - res.body = jsonutils.dumps(data).encode('utf-8') + res.body = jsonutils.dump_as_bytes(data) return res req_ext2 = extensions.RequestExtension('GET', '/dummy_resources/:(id)', diff --git a/neutron/tests/unit/test_wsgi.py b/neutron/tests/unit/test_wsgi.py index 73076224159..7d07aa18b42 100644 --- a/neutron/tests/unit/test_wsgi.py +++ b/neutron/tests/unit/test_wsgi.py @@ -156,7 +156,7 @@ class TestWSGIServer(base.BaseTestCase): ]) def test_app(self): - greetings = 'Hello, World!!!' + greetings = b'Hello, World!!!' def hello_world(env, start_response): if env['PATH_INFO'] != '/': @@ -171,7 +171,7 @@ class TestWSGIServer(base.BaseTestCase): response = open_no_proxy('http://127.0.0.1:%d/' % server.port) - self.assertEqual(greetings.encode('utf-8'), response.read()) + self.assertEqual(greetings, response.read()) server.stop() diff --git a/neutron/wsgi.py b/neutron/wsgi.py index 9d3fcf01b4c..291d7d70790 100644 --- a/neutron/wsgi.py +++ b/neutron/wsgi.py @@ -32,6 +32,7 @@ from oslo_service import service as common_service from oslo_service import sslutils from oslo_service import systemd from oslo_service import wsgi +from oslo_utils import encodeutils from oslo_utils import excutils import six import webob.dec @@ -69,9 +70,7 @@ def encode_body(body): WebOb requires to encode unicode body used to update response body. """ - if isinstance(body, six.text_type): - return body.encode('utf-8') - return body + return encodeutils.to_utf8(body) class WorkerService(worker.NeutronWorker):