Merge "Fix Pep8 Failures for Python3"
This commit is contained in:
commit
0d66f69b0c
@ -671,7 +671,9 @@ class BackupNFSSwiftBasedTestCase(test.TestCase):
|
|||||||
fake_data = bytearray(size)
|
fake_data = bytearray(size)
|
||||||
if six.PY2:
|
if six.PY2:
|
||||||
# On Python 2, zlib.compressor() accepts buffer, but not bytearray
|
# On Python 2, zlib.compressor() accepts buffer, but not bytearray
|
||||||
fake_data = buffer(fake_data)
|
# NOTE(jsbryant): Pep8 fails on py3 based installations as buffer()
|
||||||
|
# was removed. 'noqa' used here to avoid that failure.
|
||||||
|
fake_data = buffer(fake_data) # noqa
|
||||||
return fake_data
|
return fake_data
|
||||||
|
|
||||||
def test_prepare_output_data_effective_compression(self):
|
def test_prepare_output_data_effective_compression(self):
|
||||||
|
@ -427,7 +427,9 @@ def get_file_spec():
|
|||||||
file_spec = list(set(dir(_io.TextIOWrapper)).union(
|
file_spec = list(set(dir(_io.TextIOWrapper)).union(
|
||||||
set(dir(_io.BytesIO))))
|
set(dir(_io.BytesIO))))
|
||||||
else:
|
else:
|
||||||
file_spec = file
|
# NOTE(jsbryant): Pep8 on py3 based systems will fail because
|
||||||
|
# 'file' has been removed. Using noqa here to avoid the failure.
|
||||||
|
file_spec = file # noqa
|
||||||
|
|
||||||
|
|
||||||
def generate_timeout_series(timeout):
|
def generate_timeout_series(timeout):
|
||||||
|
@ -1,251 +1,251 @@
|
|||||||
# Copyright 2016 Nexenta Systems, Inc.
|
# Copyright 2016 Nexenta Systems, Inc.
|
||||||
# All Rights Reserved.
|
# All Rights Reserved.
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||||
# not use this file except in compliance with the License. You may obtain
|
# not use this file except in compliance with the License. You may obtain
|
||||||
# a copy of the License at
|
# a copy of the License at
|
||||||
#
|
#
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
#
|
#
|
||||||
# Unless required by applicable law or agreed to in writing, software
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
"""
|
"""
|
||||||
Unit tests for NexentaStor 5 REST API helper
|
Unit tests for NexentaStor 5 REST API helper
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
from mock import patch
|
from mock import patch
|
||||||
from oslo_serialization import jsonutils
|
from oslo_serialization import jsonutils
|
||||||
import requests
|
import requests
|
||||||
from requests import adapters
|
from requests import adapters
|
||||||
|
|
||||||
from cinder import exception
|
from cinder import exception
|
||||||
from cinder import test
|
from cinder import test
|
||||||
from cinder.volume.drivers.nexenta.ns5 import jsonrpc
|
from cinder.volume.drivers.nexenta.ns5 import jsonrpc
|
||||||
|
|
||||||
HOST = '1.1.1.1'
|
HOST = '1.1.1.1'
|
||||||
USERNAME = 'user'
|
USERNAME = 'user'
|
||||||
PASSWORD = 'pass'
|
PASSWORD = 'pass'
|
||||||
|
|
||||||
|
|
||||||
def gen_response(code=200, json=None):
|
def gen_response(code=200, json=None):
|
||||||
r = requests.Response()
|
r = requests.Response()
|
||||||
r.headers['Content-Type'] = 'application/json'
|
r.headers['Content-Type'] = 'application/json'
|
||||||
r.encoding = 'utf8'
|
r.encoding = 'utf8'
|
||||||
r.status_code = code
|
r.status_code = code
|
||||||
r.reason = 'FAKE REASON'
|
r.reason = 'FAKE REASON'
|
||||||
r.raw = mock.Mock()
|
r.raw = mock.Mock()
|
||||||
r._content = ''
|
r._content = ''
|
||||||
if json:
|
if json:
|
||||||
r._content = jsonutils.dumps(json)
|
r._content = jsonutils.dumps(json)
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
class TestNexentaJSONProxyAuth(test.TestCase):
|
class TestNexentaJSONProxyAuth(test.TestCase):
|
||||||
|
|
||||||
@patch('cinder.volume.drivers.nexenta.ns5.jsonrpc.requests.post')
|
@patch('cinder.volume.drivers.nexenta.ns5.jsonrpc.requests.post')
|
||||||
def test_https_auth(self, post):
|
def test_https_auth(self, post):
|
||||||
use_https = True
|
use_https = True
|
||||||
port = 8443
|
port = 8443
|
||||||
auth_uri = 'auth/login'
|
auth_uri = 'auth/login'
|
||||||
rnd_url = 'some/random/url'
|
rnd_url = 'some/random/url'
|
||||||
|
|
||||||
class PostSideEffect(object):
|
class PostSideEffect(object):
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
r = gen_response()
|
r = gen_response()
|
||||||
if args[0] == '%(scheme)s://%(host)s:%(port)s/%(uri)s' % {
|
if args[0] == '%(scheme)s://%(host)s:%(port)s/%(uri)s' % {
|
||||||
'scheme': 'https',
|
'scheme': 'https',
|
||||||
'host': HOST,
|
'host': HOST,
|
||||||
'port': port,
|
'port': port,
|
||||||
'uri': auth_uri}:
|
'uri': auth_uri}:
|
||||||
token = uuid.uuid4().hex
|
token = uuid.uuid4().hex
|
||||||
content = {'token': token}
|
content = {'token': token}
|
||||||
r._content = jsonutils.dumps(content)
|
r._content = jsonutils.dumps(content)
|
||||||
return r
|
return r
|
||||||
post_side_effect = PostSideEffect()
|
post_side_effect = PostSideEffect()
|
||||||
post.side_effect = post_side_effect
|
post.side_effect = post_side_effect
|
||||||
|
|
||||||
class TestAdapter(adapters.HTTPAdapter):
|
class TestAdapter(adapters.HTTPAdapter):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(TestAdapter, self).__init__()
|
super(TestAdapter, self).__init__()
|
||||||
self.counter = 0
|
self.counter = 0
|
||||||
|
|
||||||
def send(self, request, *args, **kwargs):
|
def send(self, request, *args, **kwargs):
|
||||||
# an url is being requested for the second time
|
# an url is being requested for the second time
|
||||||
if self.counter == 1:
|
if self.counter == 1:
|
||||||
# make the fake backend respond 401
|
# make the fake backend respond 401
|
||||||
r = gen_response(401)
|
r = gen_response(401)
|
||||||
r._content = ''
|
r._content = ''
|
||||||
r.connection = mock.Mock()
|
r.connection = mock.Mock()
|
||||||
r_ = gen_response(json={'data': []})
|
r_ = gen_response(json={'data': []})
|
||||||
r.connection.send = lambda prep, **kwargs_: r_
|
r.connection.send = lambda prep, **kwargs_: r_
|
||||||
else:
|
else:
|
||||||
r = gen_response(json={'data': []})
|
r = gen_response(json={'data': []})
|
||||||
r.request = request
|
r.request = request
|
||||||
self.counter += 1
|
self.counter += 1
|
||||||
return r
|
return r
|
||||||
|
|
||||||
nef = jsonrpc.NexentaJSONProxy(HOST, port, USERNAME, PASSWORD,
|
nef = jsonrpc.NexentaJSONProxy(HOST, port, USERNAME, PASSWORD,
|
||||||
use_https)
|
use_https)
|
||||||
adapter = TestAdapter()
|
adapter = TestAdapter()
|
||||||
nef.session.mount(
|
nef.session.mount(
|
||||||
'%(scheme)s://%(host)s:%(port)s/%(uri)s' % {
|
'%(scheme)s://%(host)s:%(port)s/%(uri)s' % {
|
||||||
'scheme': 'https',
|
'scheme': 'https',
|
||||||
'host': HOST,
|
'host': HOST,
|
||||||
'port': port,
|
'port': port,
|
||||||
'uri': rnd_url},
|
'uri': rnd_url},
|
||||||
adapter)
|
adapter)
|
||||||
|
|
||||||
# successful authorization
|
# successful authorization
|
||||||
self.assertEqual({'data': []}, nef.get(rnd_url))
|
self.assertEqual({'data': []}, nef.get(rnd_url))
|
||||||
|
|
||||||
# session timeout simulation. Client must authenticate newly
|
# session timeout simulation. Client must authenticate newly
|
||||||
self.assertEqual({'data': []}, nef.get(rnd_url))
|
self.assertEqual({'data': []}, nef.get(rnd_url))
|
||||||
# auth URL must be requested two times at this moment
|
# auth URL must be requested two times at this moment
|
||||||
self.assertEqual(2, post.call_count)
|
self.assertEqual(2, post.call_count)
|
||||||
|
|
||||||
# continue with the last (second) token
|
# continue with the last (second) token
|
||||||
self.assertEqual(nef.get(rnd_url), {'data': []})
|
self.assertEqual(nef.get(rnd_url), {'data': []})
|
||||||
# auth URL must be requested two times
|
# auth URL must be requested two times
|
||||||
self.assertEqual(2, post.call_count)
|
self.assertEqual(2, post.call_count)
|
||||||
|
|
||||||
|
|
||||||
class TestNexentaJSONProxy(test.TestCase):
|
class TestNexentaJSONProxy(test.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(TestNexentaJSONProxy, self).setUp()
|
super(TestNexentaJSONProxy, self).setUp()
|
||||||
self.nef = jsonrpc.NexentaJSONProxy(HOST, 0, USERNAME, PASSWORD, False)
|
self.nef = jsonrpc.NexentaJSONProxy(HOST, 0, USERNAME, PASSWORD, False)
|
||||||
|
|
||||||
def gen_adapter(self, code, json=None):
|
def gen_adapter(self, code, json=None):
|
||||||
class TestAdapter(adapters.HTTPAdapter):
|
class TestAdapter(adapters.HTTPAdapter):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(TestAdapter, self).__init__()
|
super(TestAdapter, self).__init__()
|
||||||
|
|
||||||
def send(self, request, *args, **kwargs):
|
def send(self, request, *args, **kwargs):
|
||||||
r = gen_response(code, json)
|
r = gen_response(code, json)
|
||||||
r.request = request
|
r.request = request
|
||||||
return r
|
return r
|
||||||
|
|
||||||
return TestAdapter()
|
return TestAdapter()
|
||||||
|
|
||||||
def _mount_adapter(self, url, adapter):
|
def _mount_adapter(self, url, adapter):
|
||||||
self.nef.session.mount(
|
self.nef.session.mount(
|
||||||
'%(scheme)s://%(host)s:%(port)s/%(uri)s' % {
|
'%(scheme)s://%(host)s:%(port)s/%(uri)s' % {
|
||||||
'scheme': 'http',
|
'scheme': 'http',
|
||||||
'host': HOST,
|
'host': HOST,
|
||||||
'port': 8080,
|
'port': 8080,
|
||||||
'uri': url},
|
'uri': url},
|
||||||
adapter)
|
adapter)
|
||||||
|
|
||||||
def test_post(self):
|
def test_post(self):
|
||||||
random_dict = {'data': uuid.uuid4().hex}
|
random_dict = {'data': uuid.uuid4().hex}
|
||||||
rnd_url = 'some/random/url'
|
rnd_url = 'some/random/url'
|
||||||
self._mount_adapter(rnd_url, self.gen_adapter(201, random_dict))
|
self._mount_adapter(rnd_url, self.gen_adapter(201, random_dict))
|
||||||
self.assertEqual(random_dict, self.nef.post(rnd_url))
|
self.assertEqual(random_dict, self.nef.post(rnd_url))
|
||||||
|
|
||||||
def test_delete(self):
|
def test_delete(self):
|
||||||
random_dict = {'data': uuid.uuid4().hex}
|
random_dict = {'data': uuid.uuid4().hex}
|
||||||
rnd_url = 'some/random/url'
|
rnd_url = 'some/random/url'
|
||||||
self._mount_adapter(rnd_url, self.gen_adapter(201, random_dict))
|
self._mount_adapter(rnd_url, self.gen_adapter(201, random_dict))
|
||||||
self.assertEqual(random_dict, self.nef.delete(rnd_url))
|
self.assertEqual(random_dict, self.nef.delete(rnd_url))
|
||||||
|
|
||||||
def test_put(self):
|
def test_put(self):
|
||||||
random_dict = {'data': uuid.uuid4().hex}
|
random_dict = {'data': uuid.uuid4().hex}
|
||||||
rnd_url = 'some/random/url'
|
rnd_url = 'some/random/url'
|
||||||
self._mount_adapter(rnd_url, self.gen_adapter(201, random_dict))
|
self._mount_adapter(rnd_url, self.gen_adapter(201, random_dict))
|
||||||
self.assertEqual(random_dict, self.nef.put(rnd_url))
|
self.assertEqual(random_dict, self.nef.put(rnd_url))
|
||||||
|
|
||||||
def test_get_200(self):
|
def test_get_200(self):
|
||||||
random_dict = {'data': uuid.uuid4().hex}
|
random_dict = {'data': uuid.uuid4().hex}
|
||||||
rnd_url = 'some/random/url'
|
rnd_url = 'some/random/url'
|
||||||
self._mount_adapter(rnd_url, self.gen_adapter(200, random_dict))
|
self._mount_adapter(rnd_url, self.gen_adapter(200, random_dict))
|
||||||
self.assertEqual(random_dict, self.nef.get(rnd_url))
|
self.assertEqual(random_dict, self.nef.get(rnd_url))
|
||||||
|
|
||||||
def test_get_201(self):
|
def test_get_201(self):
|
||||||
random_dict = {'data': uuid.uuid4().hex}
|
random_dict = {'data': uuid.uuid4().hex}
|
||||||
rnd_url = 'some/random/url'
|
rnd_url = 'some/random/url'
|
||||||
self._mount_adapter(rnd_url, self.gen_adapter(201, random_dict))
|
self._mount_adapter(rnd_url, self.gen_adapter(201, random_dict))
|
||||||
self.assertEqual(random_dict, self.nef.get(rnd_url))
|
self.assertEqual(random_dict, self.nef.get(rnd_url))
|
||||||
|
|
||||||
def test_get_500(self):
|
def test_get_500(self):
|
||||||
class TestAdapter(adapters.HTTPAdapter):
|
class TestAdapter(adapters.HTTPAdapter):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(TestAdapter, self).__init__()
|
super(TestAdapter, self).__init__()
|
||||||
|
|
||||||
def send(self, request, *args, **kwargs):
|
def send(self, request, *args, **kwargs):
|
||||||
json = {
|
json = {
|
||||||
'code': 'NEF_ERROR',
|
'code': 'NEF_ERROR',
|
||||||
'message': 'Some error'
|
'message': 'Some error'
|
||||||
}
|
}
|
||||||
r = gen_response(500, json)
|
r = gen_response(500, json)
|
||||||
r.request = request
|
r.request = request
|
||||||
return r
|
return r
|
||||||
|
|
||||||
adapter = TestAdapter()
|
adapter = TestAdapter()
|
||||||
rnd_url = 'some/random/url'
|
rnd_url = 'some/random/url'
|
||||||
self._mount_adapter(rnd_url, adapter)
|
self._mount_adapter(rnd_url, adapter)
|
||||||
self.assertRaises(exception.NexentaException, self.nef.get, rnd_url)
|
self.assertRaises(exception.NexentaException, self.nef.get, rnd_url)
|
||||||
|
|
||||||
def test_get__not_nef_error(self):
|
def test_get__not_nef_error(self):
|
||||||
class TestAdapter(adapters.HTTPAdapter):
|
class TestAdapter(adapters.HTTPAdapter):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(TestAdapter, self).__init__()
|
super(TestAdapter, self).__init__()
|
||||||
|
|
||||||
def send(self, request, *args, **kwargs):
|
def send(self, request, *args, **kwargs):
|
||||||
r = gen_response(404)
|
r = gen_response(404)
|
||||||
r._content = 'Page Not Found'
|
r._content = 'Page Not Found'
|
||||||
r.request = request
|
r.request = request
|
||||||
return r
|
return r
|
||||||
|
|
||||||
adapter = TestAdapter()
|
adapter = TestAdapter()
|
||||||
rnd_url = 'some/random/url'
|
rnd_url = 'some/random/url'
|
||||||
self._mount_adapter(rnd_url, adapter)
|
self._mount_adapter(rnd_url, adapter)
|
||||||
self.assertRaises(exception.VolumeBackendAPIException, self.nef.get,
|
self.assertRaises(exception.VolumeBackendAPIException, self.nef.get,
|
||||||
rnd_url)
|
rnd_url)
|
||||||
|
|
||||||
def test_get__not_nef_error_empty_body(self):
|
def test_get__not_nef_error_empty_body(self):
|
||||||
class TestAdapter(adapters.HTTPAdapter):
|
class TestAdapter(adapters.HTTPAdapter):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(TestAdapter, self).__init__()
|
super(TestAdapter, self).__init__()
|
||||||
|
|
||||||
def send(self, request, *args, **kwargs):
|
def send(self, request, *args, **kwargs):
|
||||||
r = gen_response(404)
|
r = gen_response(404)
|
||||||
r.request = request
|
r.request = request
|
||||||
return r
|
return r
|
||||||
|
|
||||||
adapter = TestAdapter()
|
adapter = TestAdapter()
|
||||||
rnd_url = 'some/random/url'
|
rnd_url = 'some/random/url'
|
||||||
self._mount_adapter(rnd_url, adapter)
|
self._mount_adapter(rnd_url, adapter)
|
||||||
self.assertRaises(exception.VolumeBackendAPIException, self.nef.get,
|
self.assertRaises(exception.VolumeBackendAPIException, self.nef.get,
|
||||||
rnd_url)
|
rnd_url)
|
||||||
|
|
||||||
def test_202(self):
|
def test_202(self):
|
||||||
redirect_url = 'redirect/url'
|
redirect_url = 'redirect/url'
|
||||||
|
|
||||||
class RedirectTestAdapter(adapters.HTTPAdapter):
|
class RedirectTestAdapter(adapters.HTTPAdapter):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(RedirectTestAdapter, self).__init__()
|
super(RedirectTestAdapter, self).__init__()
|
||||||
|
|
||||||
def send(self, request, *args, **kwargs):
|
def send(self, request, *args, **kwargs):
|
||||||
json = {
|
json = {
|
||||||
'links': [{'href': redirect_url}]
|
'links': [{'href': redirect_url}]
|
||||||
}
|
}
|
||||||
r = gen_response(202, json)
|
r = gen_response(202, json)
|
||||||
r.request = request
|
r.request = request
|
||||||
return r
|
return r
|
||||||
|
|
||||||
rnd_url = 'some/random/url'
|
rnd_url = 'some/random/url'
|
||||||
self._mount_adapter(rnd_url, RedirectTestAdapter())
|
self._mount_adapter(rnd_url, RedirectTestAdapter())
|
||||||
self._mount_adapter(redirect_url, self.gen_adapter(201))
|
self._mount_adapter(redirect_url, self.gen_adapter(201))
|
||||||
self.assertIsNone(self.nef.get(rnd_url))
|
self.assertIsNone(self.nef.get(rnd_url))
|
||||||
|
@ -49,7 +49,7 @@ URI_TASKS_BY_OPID = '/vdc/tasks/{0}'
|
|||||||
def _decode_list(data):
|
def _decode_list(data):
|
||||||
rv = []
|
rv = []
|
||||||
for item in data:
|
for item in data:
|
||||||
if isinstance(item, unicode):
|
if isinstance(item, six.text_type):
|
||||||
item = item.encode('utf-8')
|
item = item.encode('utf-8')
|
||||||
elif isinstance(item, list):
|
elif isinstance(item, list):
|
||||||
item = _decode_list(item)
|
item = _decode_list(item)
|
||||||
@ -62,9 +62,9 @@ def _decode_list(data):
|
|||||||
def _decode_dict(data):
|
def _decode_dict(data):
|
||||||
rv = {}
|
rv = {}
|
||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
if isinstance(key, unicode):
|
if isinstance(key, six.text_type):
|
||||||
key = key.encode('utf-8')
|
key = key.encode('utf-8')
|
||||||
if isinstance(value, unicode):
|
if isinstance(value, six.text_type):
|
||||||
value = value.encode('utf-8')
|
value = value.encode('utf-8')
|
||||||
elif isinstance(value, list):
|
elif isinstance(value, list):
|
||||||
value = _decode_list(value)
|
value = _decode_list(value)
|
||||||
@ -204,7 +204,7 @@ def service_json_request(ip_addr, port, http_method, uri, body,
|
|||||||
" service your request")
|
" service your request")
|
||||||
else:
|
else:
|
||||||
error_msg = response.text
|
error_msg = response.text
|
||||||
if isinstance(error_msg, unicode):
|
if isinstance(error_msg, six.text_type):
|
||||||
error_msg = error_msg.encode('utf-8')
|
error_msg = error_msg.encode('utf-8')
|
||||||
raise CoprHdError(CoprHdError.HTTP_ERR,
|
raise CoprHdError(CoprHdError.HTTP_ERR,
|
||||||
(_("HTTP code: %(status_code)s"
|
(_("HTTP code: %(status_code)s"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user