Merge "Fix error handling on add/remove router iface calls"

This commit is contained in:
Zuul 2020-02-05 18:08:45 +00:00 committed by Gerrit Code Review
commit f34d399f52
2 changed files with 13 additions and 12 deletions

View File

@ -10,7 +10,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
from openstack.exceptions import SDKException from openstack import exceptions
from openstack import resource from openstack import resource
from openstack import utils from openstack import utils
@ -77,12 +77,7 @@ class Router(resource.Resource, resource.TagMixin):
def _put(self, session, url, body): def _put(self, session, url, body):
resp = session.put(url, json=body) resp = session.put(url, json=body)
if not resp.ok: exceptions.raise_from_response(resp)
resp_body = resp.json()
message = None
if 'NeutronError' in resp_body:
message = resp_body['NeutronError']['message']
raise SDKException(message=message)
return resp return resp
def add_interface(self, session, **body): def add_interface(self, session, **body):

View File

@ -13,7 +13,7 @@
import mock import mock
import testtools import testtools
from openstack.exceptions import SDKException from openstack import exceptions
from openstack.tests.unit import base from openstack.tests.unit import base
from openstack.network.v2 import router from openstack.network.v2 import router
@ -121,6 +121,7 @@ class TestRouter(base.TestCase):
response = mock.Mock() response = mock.Mock()
response.body = {"subnet_id": "3", "port_id": "2"} response.body = {"subnet_id": "3", "port_id": "2"}
response.json = mock.Mock(return_value=response.body) response.json = mock.Mock(return_value=response.body)
response.status_code = 200
sess = mock.Mock() sess = mock.Mock()
sess.put = mock.Mock(return_value=response) sess.put = mock.Mock(return_value=response)
body = {"subnet_id": "3"} body = {"subnet_id": "3"}
@ -136,6 +137,7 @@ class TestRouter(base.TestCase):
response = mock.Mock() response = mock.Mock()
response.body = {"subnet_id": "3", "port_id": "3"} response.body = {"subnet_id": "3", "port_id": "3"}
response.json = mock.Mock(return_value=response.body) response.json = mock.Mock(return_value=response.body)
response.status_code = 200
sess = mock.Mock() sess = mock.Mock()
sess.put = mock.Mock(return_value=response) sess.put = mock.Mock(return_value=response)
@ -152,6 +154,7 @@ class TestRouter(base.TestCase):
response = mock.Mock() response = mock.Mock()
response.body = {"subnet_id": "3", "port_id": "2"} response.body = {"subnet_id": "3", "port_id": "2"}
response.json = mock.Mock(return_value=response.body) response.json = mock.Mock(return_value=response.body)
response.status_code = 200
sess = mock.Mock() sess = mock.Mock()
sess.put = mock.Mock(return_value=response) sess.put = mock.Mock(return_value=response)
body = {"subnet_id": "3"} body = {"subnet_id": "3"}
@ -167,6 +170,7 @@ class TestRouter(base.TestCase):
response = mock.Mock() response = mock.Mock()
response.body = {"subnet_id": "3", "port_id": "3"} response.body = {"subnet_id": "3", "port_id": "3"}
response.json = mock.Mock(return_value=response.body) response.json = mock.Mock(return_value=response.body)
response.status_code = 200
sess = mock.Mock() sess = mock.Mock()
sess.put = mock.Mock(return_value=response) sess.put = mock.Mock(return_value=response)
body = {"network_id": 3, "enable_snat": True} body = {"network_id": 3, "enable_snat": True}
@ -180,15 +184,16 @@ class TestRouter(base.TestCase):
# Neutron may return 4xx, we have to raise if that happens # Neutron may return 4xx, we have to raise if that happens
sot = router.Router(**EXAMPLE) sot = router.Router(**EXAMPLE)
response = mock.Mock() response = mock.Mock()
msg = 'borked' msg = '.*borked'
response.body = {'NeutronError': {'message': msg}} response.body = {'NeutronError': {'message': msg}}
response.json = mock.Mock(return_value=response.body) response.json = mock.Mock(return_value=response.body)
response.ok = False response.ok = False
response.status_code = 409 response.status_code = 409
response.headers = {'content-type': 'application/json'}
sess = mock.Mock() sess = mock.Mock()
sess.put = mock.Mock(return_value=response) sess.put = mock.Mock(return_value=response)
body = {'subnet_id': '3'} body = {'subnet_id': '3'}
with testtools.ExpectedException(SDKException, msg): with testtools.ExpectedException(exceptions.ConflictException, msg):
sot.add_interface(sess, **body) sot.add_interface(sess, **body)
def test_remove_interface_4xx(self): def test_remove_interface_4xx(self):
@ -196,15 +201,16 @@ class TestRouter(base.TestCase):
# extra routes referring to it as a nexthop # extra routes referring to it as a nexthop
sot = router.Router(**EXAMPLE) sot = router.Router(**EXAMPLE)
response = mock.Mock() response = mock.Mock()
msg = 'borked' msg = '.*borked'
response.body = {'NeutronError': {'message': msg}} response.body = {'NeutronError': {'message': msg}}
response.json = mock.Mock(return_value=response.body) response.json = mock.Mock(return_value=response.body)
response.ok = False response.ok = False
response.status_code = 409 response.status_code = 409
response.headers = {'content-type': 'application/json'}
sess = mock.Mock() sess = mock.Mock()
sess.put = mock.Mock(return_value=response) sess.put = mock.Mock(return_value=response)
body = {'subnet_id': '3'} body = {'subnet_id': '3'}
with testtools.ExpectedException(SDKException, msg): with testtools.ExpectedException(exceptions.ConflictException, msg):
sot.remove_interface(sess, **body) sot.remove_interface(sess, **body)
def test_add_extra_routes(self): def test_add_extra_routes(self):