Merge "Improve cleanup network resources"

This commit is contained in:
Jenkins 2016-11-03 14:23:21 +00:00 committed by Gerrit Code Review
commit 844d1769f4
2 changed files with 27 additions and 30 deletions

View File

@ -5,7 +5,7 @@ from glanceclient import client as glance_client
from keystoneauth1 import exceptions as keystoneauth_exceptions
from keystoneauth1.identity import v3
from keystoneauth1 import session as keystone_session
from neutronclient.v2_0 import client as neutron_client
from neutronclient.neutron import client as neutron_client
from novaclient import client as nova_client
import pykube
@ -69,17 +69,26 @@ def _cleanup_servers(session):
def _cleanup_network_resources(session):
neutron = neutron_client.Client(session=session)
LOG.info('Cleaning up subnets')
for subnet in neutron.list_subnets()['subnets']:
LOG.info('Removing subnet %s (%s)', subnet['name'], subnet['id'])
neutron.delete_subnet(subnet['id'])
subnet_list = _wait_until_empty(10, 'subnets', neutron.list_subnets)
if subnet_list:
raise RuntimeError(
'Some subnets were not removed: %s'
% ', '.join(['%s (%s)' % (subnet['name'], subnet['id'])
for subnet in subnet_list['subnets']]))
neutron = neutron_client.Client("2.0", session=session)
LOG.debug("Cleaning up floatingips")
for fip in neutron.list_floatingips()["floatingips"]:
LOG.debug("Removing floatingip %s", fip["id"])
neutron.delete_floatingip(fip["id"])
LOG.debug("Cleaning up routers")
for router in neutron.list_routers()["routers"]:
LOG.debug("Removing router %s", router["id"])
neutron.remove_gateway_router(router["id"])
for port in neutron.list_ports(device_id=router["id"])["ports"]:
neutron.remove_interface_router(router["id"],
{"port_id": port["id"]})
neutron.delete_router(router["id"])
LOG.debug("Cleaning up ports")
for port in neutron.list_ports()["ports"]:
LOG.debug("Removing port %s", port["id"])
neutron.delete_port(port["id"])
LOG.info('Cleaning up networks')
for network in neutron.list_networks()['networks']:

View File

@ -37,27 +37,15 @@ class TestCleanup(base.TestCase):
cleanup._wait_for_namespace_delete, k8s_api)
@mock.patch('time.sleep')
@mock.patch('neutronclient.v2_0.client.Client')
@mock.patch('neutronclient.neutron.client.Client')
def test_cleanup_network_resources(self, m_client, m_sleep):
# subnets were not removed
neutron = mock.Mock()
neutron.list_subnets.return_value = {
'subnets': [{'id': 1, 'name': 'subnet1'}]}
m_client.return_value = neutron
self.assertRaisesRegexp(
RuntimeError, "Some subnets were not removed: subnet1 \(1\)",
cleanup._cleanup_network_resources, mock.Mock())
# subnets were removed but networks were not
neutron.list_subnets.return_value = {'subnets': []}
neutron.list_networks.return_value = {
'networks': [{'id': 1, 'name': 'net1'}]}
self.assertRaisesRegexp(
RuntimeError, "Some networks were not removed: net1 \(1\)",
cleanup._cleanup_network_resources, mock.Mock())
# subnets and networks were removed
neutron.list_networks.return_value = {'networks': []}
neutron.list_floatingips.return_value = {"floatingips": []}
neutron.list_routers.return_value = {"routers": []}
neutron.list_ports.return_value = {"ports": []}
neutron.list_networks.return_value = {"networks": []}
m_client.return_value = neutron
cleanup._cleanup_network_resources(mock.Mock())
@mock.patch('time.sleep')