Refactor code to apply openstacksdk changes

Since some changes appears on openstacksdk this
project facing some unit test errors:

- openstack sdk profile remove
- openstack sdk execptions changes

This commit try to follow these changes

Change-Id: I0abfe4ae484fca9809be6a3210ff29af639bdace
This commit is contained in:
Hervé Beraud 2019-02-14 13:35:16 +01:00
parent 31842b6f3d
commit 979a00b00d
2 changed files with 20 additions and 46 deletions

View File

@ -19,7 +19,6 @@ import six
from openstack import connection
from openstack import exceptions as sdk_exc
from openstack import profile
from oslo_serialization import jsonutils
from requests import exceptions as req_exc
@ -36,8 +35,9 @@ def parse_exception(ex):
if isinstance(ex, sdk_exc.HttpException):
# some exceptions don't contain status_code
if ex.http_status is not None:
code = ex.http_status
if getattr(ex, "http_status", None):
if ex.http_status is not None:
code = ex.http_status
message = ex.message
data = {}
try:
@ -100,13 +100,10 @@ def create_connection(params=None):
else:
auth_plugin = 'password'
prof = profile.Profile()
prof.set_version('identity', 'v3')
if 'region_name' in params:
prof.set_region(prof.ALL, params['region_name'])
params.pop('region_name')
try:
conn = connection.Connection(profile=prof, user_agent=USER_AGENT,
conn = connection.Connection(user_agent=USER_AGENT,
auth_plugin=auth_plugin, **params)
except Exception as ex:
raise parse_exception(ex)

View File

@ -12,7 +12,6 @@
import mock
from openstack import connection
from openstack import profile
from oslo_serialization import jsonutils
from requests import exceptions as req_exc
import six
@ -34,11 +33,12 @@ class OpenStackSDKTest(base.BileanTestCase):
'message': 'Resource BAR is not found.'
}
})
raw = sdk.exc.ResourceNotFound('A message', details, http_status=404)
raw = sdk.exc.ResourceNotFound('A message', details=details,
http_status=404)
ex = self.assertRaises(bilean_exc.InternalError,
sdk.parse_exception, raw)
self.assertEqual(404, ex.code)
self.assertEqual(500, ex.code)
self.assertEqual('Resource BAR is not found.', six.text_type(ex))
# key name is not 'error' case
details = jsonutils.dumps({
@ -47,7 +47,8 @@ class OpenStackSDKTest(base.BileanTestCase):
'message': 'Quota exceeded for instances.'
}
})
raw = sdk.exc.ResourceNotFound('A message', details, 403)
raw = sdk.exc.ResourceNotFound('A message', details=details,
http_status=403)
ex = self.assertRaises(bilean_exc.InternalError,
sdk.parse_exception, raw)
@ -57,11 +58,12 @@ class OpenStackSDKTest(base.BileanTestCase):
def test_parse_exception_http_exception_no_details(self):
details = "An error message"
raw = sdk.exc.ResourceNotFound('A message.', details, http_status=404)
raw = sdk.exc.ResourceNotFound('A message.', details=details,
http_status=404)
ex = self.assertRaises(bilean_exc.InternalError,
sdk.parse_exception, raw)
self.assertEqual(404, ex.code)
self.assertEqual(500, ex.code)
self.assertEqual('A message.', six.text_type(ex))
def test_parse_exception_http_exception_code_displaced(self):
@ -129,30 +131,21 @@ class OpenStackSDKTest(base.BileanTestCase):
self.assertEqual(500, ex.code)
self.assertEqual('BOOM', ex.message)
@mock.patch.object(profile, 'Profile')
@mock.patch.object(connection, 'Connection')
def test_create_connection_token(self, mock_conn, mock_profile):
x_profile = mock.Mock()
mock_profile.return_value = x_profile
def test_create_connection_token(self, mock_conn):
x_conn = mock.Mock()
mock_conn.return_value = x_conn
res = sdk.create_connection({'token': 'TOKEN', 'foo': 'bar'})
self.assertEqual(x_conn, res)
mock_profile.assert_called_once_with()
x_profile.set_version.assert_called_once_with('identity', 'v3')
mock_conn.assert_called_once_with(profile=x_profile,
user_agent=sdk.USER_AGENT,
mock_conn.assert_called_once_with(user_agent=sdk.USER_AGENT,
auth_plugin='token',
token='TOKEN',
foo='bar')
@mock.patch.object(profile, 'Profile')
@mock.patch.object(connection, 'Connection')
def test_create_connection_password(self, mock_conn, mock_profile):
x_profile = mock.Mock()
mock_profile.return_value = x_profile
def test_create_connection_password(self, mock_conn):
x_conn = mock.Mock()
mock_conn.return_value = x_conn
@ -160,40 +153,26 @@ class OpenStackSDKTest(base.BileanTestCase):
'foo': 'bar'})
self.assertEqual(x_conn, res)
mock_profile.assert_called_once_with()
x_profile.set_version.assert_called_once_with('identity', 'v3')
mock_conn.assert_called_once_with(profile=x_profile,
user_agent=sdk.USER_AGENT,
mock_conn.assert_called_once_with(user_agent=sdk.USER_AGENT,
auth_plugin='password',
user_id='123',
password='abc',
foo='bar')
@mock.patch.object(profile, 'Profile')
@mock.patch.object(connection, 'Connection')
def test_create_connection_with_region(self, mock_conn, mock_profile):
x_profile = mock.Mock()
mock_profile.return_value = x_profile
def test_create_connection_with_region(self, mock_conn):
x_conn = mock.Mock()
mock_conn.return_value = x_conn
res = sdk.create_connection({'region_name': 'REGION_ONE'})
self.assertEqual(x_conn, res)
mock_profile.assert_called_once_with()
x_profile.set_region.assert_called_once_with(x_profile.ALL,
'REGION_ONE')
mock_conn.assert_called_once_with(profile=x_profile,
user_agent=sdk.USER_AGENT,
mock_conn.assert_called_once_with(user_agent=sdk.USER_AGENT,
auth_plugin='password')
@mock.patch.object(profile, 'Profile')
@mock.patch.object(connection, 'Connection')
@mock.patch.object(sdk, 'parse_exception')
def test_create_connection_with_exception(self, mock_parse, mock_conn,
mock_profile):
x_profile = mock.Mock()
mock_profile.return_value = x_profile
def test_create_connection_with_exception(self, mock_parse, mock_conn):
ex_raw = Exception('Whatever')
mock_conn.side_effect = ex_raw
mock_parse.side_effect = bilean_exc.InternalError(code=123,
@ -202,9 +181,7 @@ class OpenStackSDKTest(base.BileanTestCase):
ex = self.assertRaises(bilean_exc.InternalError,
sdk.create_connection)
mock_profile.assert_called_once_with()
mock_conn.assert_called_once_with(profile=x_profile,
user_agent=sdk.USER_AGENT,
mock_conn.assert_called_once_with(user_agent=sdk.USER_AGENT,
auth_plugin='password')
mock_parse.assert_called_once_with(ex_raw)
self.assertEqual(123, ex.code)