avoid unnecessary neutron api call in revoke_expose_ports

kuryr will always call neutron.show_port and neutron.update_port in
revoke_expose_ports. This patch avoid unnecessary neutron api call,
when the security group is not changed.

Change-Id: I8fc0c85483eefb98b0f5f5214762de0703fd76c1
Closes-bug: #1662039
This commit is contained in:
Liping Mao (limao)
2017-02-06 09:15:42 +08:00
committed by Liping Mao
parent 31d033e962
commit abe22737e0
2 changed files with 25 additions and 11 deletions

View File

@@ -454,6 +454,8 @@ def revoke_expose_ports(port_id):
sgs = app.neutron.list_security_groups(
name=utils.get_sg_expose_name(port_id))
sgs = sgs.get('security_groups')
if not sgs:
return
removing_sgs = [sg['id'] for sg in sgs]
existing_sgs = []

View File

@@ -149,9 +149,10 @@ class TestExternalConnectivityKuryr(base.TestKuryrBase):
@mock.patch(
'kuryr_libnetwork.controllers.app.neutron.list_security_groups')
@mock.patch('kuryr_libnetwork.controllers.app.neutron.list_ports')
@ddt.data((False), (True))
@ddt.data((False, False), (False, True), (True, False), (True, True))
@ddt.unpack
def test_network_driver_revoke_external_connectivity(self, existing_sg,
mock_list_ports, mock_list_security_groups,
removing_sg, mock_list_ports, mock_list_security_groups,
mock_delete_security_groups, mock_show_port,
mock_update_port):
fake_docker_net_id = lib_utils.get_hash()
@@ -175,8 +176,12 @@ class TestExternalConnectivityKuryr(base.TestKuryrBase):
else:
fake_neutron_ports_response['ports'][0]['security_groups'] = [
fake_neutron_sec_group_id]
fake_neutron_sec_group_response = {
'security_groups': [{'id': fake_neutron_sec_group_id}]}
if removing_sg:
fake_neutron_sec_group_response = {
'security_groups': [{'id': fake_neutron_sec_group_id}]}
else:
fake_neutron_sec_group_response = {
'security_groups': []}
mock_list_ports.return_value = fake_neutron_ports_response
mock_list_security_groups.return_value = (
@@ -198,12 +203,19 @@ class TestExternalConnectivityKuryr(base.TestKuryrBase):
self.assertEqual(200, response.status_code)
mock_list_ports.assert_called_with(name=neutron_port_name)
mock_list_security_groups.assert_called_with(
name=utils.get_sg_expose_name(fake_neutron_port_id))
mock_delete_security_groups.assert_called_with(
fake_neutron_sec_group_id)
mock_show_port.assert_called_with(fake_neutron_port_id)
mock_update_port.assert_called_with(fake_neutron_port_id,
{'port': {'security_groups': sgs}})
if removing_sg:
mock_list_security_groups.assert_called_with(
name=utils.get_sg_expose_name(fake_neutron_port_id))
mock_delete_security_groups.assert_called_with(
fake_neutron_sec_group_id)
mock_show_port.assert_called_with(fake_neutron_port_id)
mock_update_port.assert_called_with(fake_neutron_port_id,
{'port': {'security_groups': sgs}})
else:
mock_list_security_groups.assert_called_with(
name=utils.get_sg_expose_name(fake_neutron_port_id))
mock_delete_security_groups.assert_not_called()
mock_show_port.assert_not_called()
mock_update_port.assert_not_called()
decoded_json = jsonutils.loads(response.data)
self.assertEqual(constants.SCHEMA['SUCCESS'], decoded_json)