NSXv3: Fix typo in URI while setting GW for router

Fixes NSX v3 router integration to not pass along sub-URI
in API calls to the backend.

This patch also includes updates to the NSX v3 mock API
session handling to ensure mocked clients handle their
appropriate sub URIs.

Closes-Bug: #1516912
Closes-Bug: #1510279
Change-Id: Ib90071a68572d5b18db896ea38b91e5beaeca88a
(cherry picked from commit ce75aa97d9)
This commit is contained in:
Shih-Hao Li 2015-11-02 12:37:19 -08:00 committed by linb
parent 7a474fd228
commit 2dd1a61e88
5 changed files with 86 additions and 20 deletions

View File

@ -327,14 +327,14 @@ class LogicalRouterPort(AbstractRESTResource):
return self._client.url_delete(logical_port_id)
def get_by_lswitch_id(self, logical_switch_id):
resource = ('logical-router-ports?logical_switch_id=%s' %
logical_switch_id)
resource = '?logical_switch_id=%s' % logical_switch_id
router_ports = self._client.url_get(resource)
if int(router_ports['result_count']) >= 2:
result_count = int(router_ports.get('result_count', "0"))
if result_count >= 2:
raise nsx_exc.NsxPluginException(
err_msg=_("Can't support more than one logical router ports "
"on same logical switch %s ") % logical_switch_id)
elif int(router_ports['result_count']) == 1:
elif result_count == 1:
return router_ports['results'][0]
else:
err_msg = (_("Logical router link port not found on logical "
@ -351,8 +351,7 @@ class LogicalRouterPort(AbstractRESTResource):
self.delete(port['id'])
def get_by_router_id(self, logical_router_id):
resource = ('logical-router-ports/?logical_router_id=%s' %
logical_router_id)
resource = '?logical_router_id=%s' % logical_router_id
logical_router_ports = self._client.url_get(resource)
return logical_router_ports['results']

View File

@ -52,8 +52,7 @@ class RouterLib(object):
return
err_msg = None
try:
resource = 'logical-routers/%s' % tier0_uuid
lrouter = self._router_client.get(resource)
lrouter = self._router_client.get(tier0_uuid)
except nsx_exc.ResourceNotFound:
err_msg = _("Failed to validate tier0 router %s since it is "
"not found at the backend") % tier0_uuid

View File

@ -73,11 +73,6 @@ class NsxV3PluginTestCaseMixin(test_plugin.NeutronDbPluginV2TestCase,
super(NsxV3PluginTestCaseMixin, self).setUp(plugin=plugin,
ext_mgr=ext_mgr)
if getattr(self.plugin, '_nsx_client', None):
self.plugin._nsx_client = self.client
if getattr(self.plugin, '_port_client', None):
self.plugin._port_client._client._session = self.mock_api
self.maxDiff = None
def tearDown(self):

View File

@ -152,17 +152,39 @@ class NsxClientTestCase(NsxLibTestCase):
return client_fn(*args, **kwargs)
return _client
def _mock_client_init(*args, **kwargs):
return with_client
def _proxy_new_client_for(client):
new_client_fn = client.new_client_for
def _new_client_for(*uri_segs):
new_client = new_client_fn(*uri_segs)
new_client._session = mock_session
new_client.new_client_for = _proxy_new_client_for(new_client)
return new_client
return _new_client_for
def _proxy_init(class_name):
client_init = getattr(nsx_client, class_name)
def _init_client(*args, **kwargs):
if (not args and not kwargs and
with_client.__class__.__name__ == class_name):
return with_client
client = client_init(*args, **kwargs)
client._session = mock_session
return client
return _init_client
fn_map = {}
for fn in BRIDGE_FNS:
fn_map[fn] = _call_client(fn)
fn_map['NSX3Client'] = _mock_client_init
fn_map['JSONRESTClient'] = _mock_client_init
fn_map['RESTClient'] = _mock_client_init
fn_map['NSX3Client'] = _proxy_init('NSX3Client')
fn_map['JSONRESTClient'] = _proxy_init('JSONRESTClient')
fn_map['RESTClient'] = _proxy_init('RESTClient')
with_client.new_client_for = _mock_client_init
with_client.new_client_for = _proxy_new_client_for(with_client)
return cls.patch_client_module(in_module, fn_map)

View File

@ -300,7 +300,7 @@ class LogicalRouterPortTestCase(nsxlib_testcase.NsxClientTestCase):
def test_create_logical_router_port(self):
"""
Test creating a router returns the correct response and 201 status
Test creating a router port returns the correct response and 201 status
"""
fake_router_port = test_constants_v3.FAKE_ROUTER_PORT.copy()
@ -348,3 +348,54 @@ class LogicalRouterPortTestCase(nsxlib_testcase.NsxClientTestCase):
None,
client.JSONRESTClient._DEFAULT_HEADERS,
nsxlib_testcase.NSX_CERT)
def test_get_logical_router_port_by_router_id(self):
"""
Test getting a router port by router id
"""
fake_router_port = test_constants_v3.FAKE_ROUTER_PORT.copy()
resp_resources = {'results': [fake_router_port]}
api = resources.LogicalRouterPort(client.NSX3Client())
with self.mocked_resource(api) as mocked:
mocked.get('get').return_value = mocks.MockRequestsResponse(
200, jsonutils.dumps(resp_resources))
router_id = fake_router_port['logical_router_id']
result = api.get_by_router_id(router_id)
self.assertEqual(fake_router_port, result[0])
test_client.assert_session_call(
mocked.get('get'),
'https://1.2.3.4/api/v1/logical-router-ports/?'
'logical_router_id=%s' % router_id,
False,
None,
client.JSONRESTClient._DEFAULT_HEADERS,
nsxlib_testcase.NSX_CERT)
def test_get_logical_router_port_by_switch_id(self):
"""
Test getting a router port by switch id
"""
fake_router_port = test_constants_v3.FAKE_ROUTER_PORT.copy()
resp_resources = {
'result_count': 1,
'results': [fake_router_port]
}
api = resources.LogicalRouterPort(client.NSX3Client())
with self.mocked_resource(api) as mocked:
mocked.get('get').return_value = mocks.MockRequestsResponse(
200, jsonutils.dumps(resp_resources))
switch_id = test_constants_v3.FAKE_SWITCH_UUID
result = api.get_by_lswitch_id(switch_id)
self.assertEqual(fake_router_port, result)
test_client.assert_session_call(
mocked.get('get'),
'https://1.2.3.4/api/v1/logical-router-ports/?'
'logical_switch_id=%s' % switch_id,
False,
None,
client.JSONRESTClient._DEFAULT_HEADERS,
nsxlib_testcase.NSX_CERT)