Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes for router migration #33

Open
wants to merge 5 commits into
base: neutron-ha-tool-maintenance
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions files/default/neutron-ha-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,33 +699,32 @@ def migrate_router(qclient, router, agent, target,
qclient.remove_router_from_l3_agent(agent['id'], router['id'])
LOG.debug("Removed router from agent=%s" % agent['id'])

router_still_on_source_agent = router_is_on_agent(qclient, router, agent)

# ensure it is removed
router_ids = [
r['id'] for r in list_routers_on_l3_agent(qclient, agent['id'])
]
if router['id'] in router_ids:
if router['distributed']:
# Because of bsc#1016943, the router is not completely removed
# from the agent. As a workaround, issuing a second remove
# seems to bring the router/agent intro correct state
qclient.remove_router_from_l3_agent(agent['id'], router['id'])
LOG.debug("The router was not correctly deleted from agent=%s, "
"retrying." % agent['id'])

if router in list_routers_on_l3_agent(qclient, agent['id']):
raise RuntimeError("Failed to remove router_id=%s from agent_id="
"%s" % (router['id'], agent['id']))
if router_still_on_source_agent and router['distributed']:
# Because of bsc#1016943, the router is not completely removed
# from the agent. As a workaround, issuing a second remove
# seems to bring the router/agent intro correct state
qclient.remove_router_from_l3_agent(agent['id'], router['id'])
LOG.debug("The router was not correctly deleted from agent=%s, "
"retrying." % agent['id'])

router_still_on_source_agent = router_is_on_agent(
qclient, router, agent
)

if router_still_on_source_agent:
raise RuntimeError("Failed to remove router_id=%s from agent_id="
"%s" % (router['id'], agent['id']))

# add the router id to a live agent
router_body = {'router_id': router['id']}
qclient.add_router_to_l3_agent(target['id'], router_body)
LOG.debug("Added router to agent=%s" % target['id'])

# ensure it is added or log an error
router_ids = [
r['id'] for r in list_routers_on_l3_agent(qclient, target['id'])
]
if router['id'] not in router_ids:
if not router_is_on_agent(qclient, router, target):
raise RuntimeError("Failed to add router_id=%s from agent_id=%s" %
(router['id'], agent['id']))
if wait_for_router:
Expand Down Expand Up @@ -859,6 +858,13 @@ def list_routers_on_l3_agent(qclient, agent_id):
return [r for r in resp['routers'] if not r.get('ha') == True] # noqa


def router_is_on_agent(qclient, router, agent):
router_ids = [
r['id'] for r in list_routers_on_l3_agent(qclient, agent['id'])
]
return router['id'] in router_ids


def list_agents(qclient, agent_type=None):
"""Return a list of agent objects

Expand Down
90 changes: 89 additions & 1 deletion files/default/test-neutron-ha-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class FakeNeutronClient(object):
def __init__(self, fake_neutron):
self.fake_neutron = fake_neutron
self.list_agent_calls = 0
self.list_routers_on_l3_agent_calls = 0
self._router_removal_failures_to_emulate = 0

def mock_make_router_removal_fail_once(self):
self._router_removal_failures_to_emulate = 1

def mock_make_router_removal_fail_twice(self):
self._router_removal_failures_to_emulate = 2

def list_agents(self):
self.list_agent_calls += 1
Expand All @@ -47,6 +55,7 @@ def list_agents(self):
}

def list_routers_on_l3_agent(self, agent_id):
self.list_routers_on_l3_agent_calls += 1
return {
'routers': [
self.fake_neutron.routers[router_id]
Expand All @@ -55,7 +64,10 @@ def list_routers_on_l3_agent(self, agent_id):
}

def remove_router_from_l3_agent(self, agent_id, router_id):
self.fake_neutron.routers_by_agent[agent_id].remove(router_id)
if self._router_removal_failures_to_emulate:
self._router_removal_failures_to_emulate -= 1
else:
self.fake_neutron.routers_by_agent[agent_id].remove(router_id)

def add_router_to_l3_agent(self, agent_id, router_body):
self.fake_neutron.routers_by_agent[agent_id].add(
Expand Down Expand Up @@ -112,6 +124,82 @@ def setup_fake_neutron(live_agents=0, dead_agents=0):
return fake_neutron


class TestMigrateRouter(unittest.TestCase):
def test_migration_fails_on_a_non_distributed_router(self):
fake_neutron = setup_fake_neutron(live_agents=1, dead_agents=1)
neutron_client = FakeNeutronClient(fake_neutron)
neutron_client.mock_make_router_removal_fail_once()
fake_neutron.add_router(
'dead-agent-0', 'router-1', {'distributed': False}
)

with self.assertRaises(RuntimeError):
ha_tool.migrate_router(
neutron_client,
{'id': 'router-1', 'distributed': False},
{'id': 'dead-agent-0'},
{'id': 'live-agent-0'},
wait_for_router=False
)

def test_migration_fails_non_distributed_router_lists_routers_once(self):
fake_neutron = setup_fake_neutron(live_agents=1, dead_agents=1)
neutron_client = FakeNeutronClient(fake_neutron)
neutron_client.mock_make_router_removal_fail_once()
fake_neutron.add_router(
'dead-agent-0', 'router-1', {'distributed': False}
)

with self.assertRaises(RuntimeError):
ha_tool.migrate_router(
neutron_client,
{'id': 'router-1', 'distributed': False},
{'id': 'dead-agent-0'},
{'id': 'live-agent-0'},
wait_for_router=False
)

self.assertEqual(1, neutron_client.list_routers_on_l3_agent_calls)

def test_migration_fails_distributed_router_retried(self):
fake_neutron = setup_fake_neutron(live_agents=1, dead_agents=1)
neutron_client = FakeNeutronClient(fake_neutron)
neutron_client.mock_make_router_removal_fail_once()
fake_neutron.add_router(
'dead-agent-0', 'router-1', {'distributed': True}
)

ha_tool.migrate_router(
neutron_client,
{'id': 'router-1', 'distributed': True},
{'id': 'dead-agent-0'},
{'id': 'live-agent-0'},
wait_for_router=False
)

self.assertEqual(
1,
len(neutron_client.list_routers_on_l3_agent('live-agent-0'))
)

def test_migration_fails_twice_with_distributed_router_is_failure(self):
fake_neutron = setup_fake_neutron(live_agents=1, dead_agents=1)
neutron_client = FakeNeutronClient(fake_neutron)
neutron_client.mock_make_router_removal_fail_twice()
fake_neutron.add_router(
'dead-agent-0', 'router-1', {'distributed': True}
)

with self.assertRaises(RuntimeError):
ha_tool.migrate_router(
neutron_client,
{'id': 'router-1', 'distributed': True},
{'id': 'dead-agent-0'},
{'id': 'live-agent-0'},
wait_for_router=False
)


class TestL3AgentMigrate(unittest.TestCase):

def test_no_dead_agents_migrate_returns_without_errors(self):
Expand Down