| 1 |
# Copyright 2016 Huawei Technologies India Pvt. Ltd. |
| 2 |
# All Rights Reserved. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
from neutron.tests.unit import testlib_api |
| 19 |
from neutron_lib.callbacks import events |
| 20 |
from neutron_lib.callbacks import registry |
| 21 |
from neutron_lib import context |
| 22 |
from oslo_utils import importutils |
| 23 |
import testscenarios |
| 24 |
|
| 25 |
from neutron_dynamic_routing.api.rpc.callbacks import resources as dr_resources |
| 26 |
from neutron_dynamic_routing.db import bgp_db |
| 27 |
from neutron_dynamic_routing.db import bgp_dragentscheduler_db as bgp_dras_db |
| 28 |
from neutron_dynamic_routing.services.bgp import bgp_plugin |
| 29 |
from neutron_dynamic_routing.services.bgp.scheduler import bgp_dragent_scheduler as bgp_dras # noqa |
| 30 |
from neutron_dynamic_routing.tests.common import helpers |
| 31 |
|
| 32 |
# Required to generate tests from scenarios. Not compatible with nose. |
| 33 |
load_tests = testscenarios.load_tests_apply_scenarios |
| 34 |
|
| 35 |
|
| 36 |
class TestBgpDrAgentSchedulerBaseTestCase(testlib_api.SqlTestCase): |
| 37 |
|
| 38 |
def setUp(self): |
| 39 |
super().setUp() |
| 40 |
self.ctx = context.get_admin_context() |
| 41 |
self.bgp_speaker = {'id': 'foo_bgp_speaker_id'} |
| 42 |
self.bgp_speaker_id = 'foo_bgp_speaker_id' |
| 43 |
self._save_bgp_speaker(self.bgp_speaker_id) |
| 44 |
|
| 45 |
def _create_and_set_agents_down(self, hosts, down_agent_count=0, |
| 46 |
admin_state_up=True): |
| 47 |
agents = [] |
| 48 |
for i, host in enumerate(hosts): |
| 49 |
is_alive = i >= down_agent_count |
| 50 |
agents.append(helpers.register_bgp_dragent( |
| 51 |
host, |
| 52 |
admin_state_up=admin_state_up, |
| 53 |
alive=is_alive)) |
| 54 |
return agents |
| 55 |
|
| 56 |
def _save_bgp_speaker(self, bgp_speaker_id): |
| 57 |
cls = bgp_db.BgpDbMixin() |
| 58 |
bgp_speaker_body = {'bgp_speaker': { |
| 59 |
'name': 'fake_bgp_speaker', |
| 60 |
'ip_version': '4', |
| 61 |
'local_as': '123', |
| 62 |
'advertise_floating_ip_host_routes': False, |
| 63 |
'advertise_tenant_networks': False, |
| 64 |
'peers': [], |
| 65 |
'networks': []}} |
| 66 |
cls._save_bgp_speaker(self.ctx, bgp_speaker_body, uuid=bgp_speaker_id) |
| 67 |
|
| 68 |
def _get_dragent_bgp_speaker_bindings(self, bgp_speaker_id): |
| 69 |
return self.ctx.session.query( |
| 70 |
bgp_dras_db.BgpSpeakerDrAgentBinding).filter_by( |
| 71 |
bgp_speaker_id=bgp_speaker_id).all() |
| 72 |
|
| 73 |
def _test_schedule_bind_bgp_speaker(self, agents, bgp_speaker_id): |
| 74 |
scheduler = bgp_dras.ChanceScheduler() |
| 75 |
scheduler.resource_filter.bind(self.ctx, agents, bgp_speaker_id) |
| 76 |
results = self._get_dragent_bgp_speaker_bindings(bgp_speaker_id) |
| 77 |
|
| 78 |
for result in results: |
| 79 |
self.assertEqual(bgp_speaker_id, result.bgp_speaker_id) |
| 80 |
|
| 81 |
|
| 82 |
class TestSchedulerCallback(TestBgpDrAgentSchedulerBaseTestCase): |
| 83 |
|
| 84 |
def setUp(self): |
| 85 |
super().setUp() |
| 86 |
bgp_notify_p = mock.patch('neutron_dynamic_routing.api.rpc.' |
| 87 |
'agentnotifiers.bgp_dr_rpc_agent_api.' |
| 88 |
'BgpDrAgentNotifyApi') |
| 89 |
bgp_notify_p.start() |
| 90 |
rpc_conn_p = mock.patch('neutron_lib.rpc.Connection') |
| 91 |
rpc_conn_p.start() |
| 92 |
self.plugin = bgp_plugin.BgpPlugin() |
| 93 |
self.scheduler = bgp_dras.ChanceScheduler() |
| 94 |
|
| 95 |
def _create_test_payload(self, context='test_ctx'): |
| 96 |
bgp_speaker = {'id': '11111111-2222-3333-4444-555555555555'} |
| 97 |
payload = events.DBEventPayload( |
| 98 |
context, |
| 99 |
metadata={'plugin': self.plugin}, |
| 100 |
states=(bgp_speaker,)) |
| 101 |
return payload |
| 102 |
|
| 103 |
def test__register_callbacks(self): |
| 104 |
with mock.patch.object(registry, 'subscribe') as subscribe: |
| 105 |
scheduler = bgp_dras.ChanceScheduler() |
| 106 |
expected_calls = [ |
| 107 |
mock.call(scheduler.schedule_bgp_speaker_callback, |
| 108 |
dr_resources.BGP_SPEAKER, events.AFTER_CREATE), |
| 109 |
] |
| 110 |
self.assertEqual(subscribe.call_args_list, expected_calls) |
| 111 |
with mock.patch.object(registry, 'subscribe') as subscribe: |
| 112 |
scheduler = bgp_dras.WeightScheduler() |
| 113 |
expected_calls = [ |
| 114 |
mock.call(scheduler.schedule_bgp_speaker_callback, |
| 115 |
dr_resources.BGP_SPEAKER, events.AFTER_CREATE), |
| 116 |
] |
| 117 |
self.assertEqual(subscribe.call_args_list, expected_calls) |
| 118 |
|
| 119 |
def test_schedule_bgp_speaker_callback_with_valid_event(self): |
| 120 |
payload = self._create_test_payload() |
| 121 |
with mock.patch.object(self.plugin, |
| 122 |
'schedule_bgp_speaker') as sched_bgp: |
| 123 |
self.scheduler.schedule_bgp_speaker_callback( |
| 124 |
dr_resources.BGP_SPEAKER, |
| 125 |
events.AFTER_CREATE, |
| 126 |
self.scheduler, payload) |
| 127 |
sched_bgp.assert_called_once_with(mock.ANY, |
| 128 |
payload.latest_state) |
| 129 |
|
| 130 |
def test_schedule_bgp_speaker_callback_with_invalid_event(self): |
| 131 |
payload = self._create_test_payload() |
| 132 |
with mock.patch.object(self.plugin, |
| 133 |
'schedule_bgp_speaker') as sched_bgp: |
| 134 |
self.scheduler.schedule_bgp_speaker_callback( |
| 135 |
dr_resources.BGP_SPEAKER, |
| 136 |
events.BEFORE_CREATE, |
| 137 |
self.scheduler, payload) |
| 138 |
sched_bgp.assert_not_called() |
| 139 |
|
| 140 |
|
| 141 |
class TestBgpDrAgentScheduler(TestBgpDrAgentSchedulerBaseTestCase, |
| 142 |
bgp_db.BgpDbMixin): |
| 143 |
|
| 144 |
def test_schedule_bind_bgp_speaker_single_agent(self): |
| 145 |
agents = self._create_and_set_agents_down(['host-a']) |
| 146 |
self._test_schedule_bind_bgp_speaker(agents, self.bgp_speaker_id) |
| 147 |
|
| 148 |
def test_schedule_bind_bgp_speaker_multi_agents(self): |
| 149 |
agents = self._create_and_set_agents_down(['host-a', 'host-b']) |
| 150 |
self._test_schedule_bind_bgp_speaker(agents, self.bgp_speaker_id) |
| 151 |
|
| 152 |
|
| 153 |
class TestBgpAgentFilter(TestBgpDrAgentSchedulerBaseTestCase, |
| 154 |
bgp_db.BgpDbMixin, |
| 155 |
bgp_dras_db.BgpDrAgentSchedulerDbMixin): |
| 156 |
|
| 157 |
def setUp(self): |
| 158 |
super().setUp() |
| 159 |
self.bgp_drscheduler = importutils.import_object( |
| 160 |
'neutron_dynamic_routing.services.bgp.scheduler.' |
| 161 |
'bgp_dragent_scheduler.ChanceScheduler' |
| 162 |
) |
| 163 |
self.plugin = self |
| 164 |
|
| 165 |
def _test_filter_agents_helper(self, bgp_speaker, |
| 166 |
expected_filtered_dragent_ids=None, |
| 167 |
expected_num_agents=1): |
| 168 |
filtered_agents = ( |
| 169 |
self.plugin.bgp_drscheduler.resource_filter.filter_agents( |
| 170 |
self.plugin, self.ctx, bgp_speaker)) |
| 171 |
self.assertEqual(expected_num_agents, |
| 172 |
filtered_agents['n_agents']) |
| 173 |
actual_filtered_dragent_ids = [ |
| 174 |
agent.id for agent in filtered_agents['hostable_agents']] |
| 175 |
if expected_filtered_dragent_ids is None: |
| 176 |
expected_filtered_dragent_ids = [] |
| 177 |
self.assertEqual(len(expected_filtered_dragent_ids), |
| 178 |
len(actual_filtered_dragent_ids)) |
| 179 |
for filtered_agent_id in actual_filtered_dragent_ids: |
| 180 |
self.assertIn(filtered_agent_id, expected_filtered_dragent_ids) |
| 181 |
|
| 182 |
def test_filter_agents_single_agent(self): |
| 183 |
agents = self._create_and_set_agents_down(['host-a']) |
| 184 |
expected_filtered_dragent_ids = [agents[0].id] |
| 185 |
self._test_filter_agents_helper( |
| 186 |
self.bgp_speaker, |
| 187 |
expected_filtered_dragent_ids=expected_filtered_dragent_ids) |
| 188 |
|
| 189 |
def test_filter_agents_no_agents(self): |
| 190 |
expected_filtered_dragent_ids = [] |
| 191 |
self._test_filter_agents_helper( |
| 192 |
self.bgp_speaker, |
| 193 |
expected_filtered_dragent_ids=expected_filtered_dragent_ids, |
| 194 |
expected_num_agents=0) |
| 195 |
|
| 196 |
def test_filter_agents_two_agents(self): |
| 197 |
agents = self._create_and_set_agents_down(['host-a', 'host-b']) |
| 198 |
expected_filtered_dragent_ids = [agent.id for agent in agents] |
| 199 |
self._test_filter_agents_helper( |
| 200 |
self.bgp_speaker, |
| 201 |
expected_filtered_dragent_ids=expected_filtered_dragent_ids) |
| 202 |
|
| 203 |
def test_filter_agents_agent_already_scheduled(self): |
| 204 |
agents = self._create_and_set_agents_down(['host-a', 'host-b']) |
| 205 |
self._test_schedule_bind_bgp_speaker([agents[0]], self.bgp_speaker_id) |
| 206 |
self._test_filter_agents_helper(self.bgp_speaker, |
| 207 |
expected_num_agents=0) |
| 208 |
|
| 209 |
def test_filter_agents_multiple_agents_bgp_speakers(self): |
| 210 |
agents = self._create_and_set_agents_down(['host-a', 'host-b']) |
| 211 |
self._test_schedule_bind_bgp_speaker([agents[0]], self.bgp_speaker_id) |
| 212 |
bgp_speaker = {'id': 'bar-speaker-id'} |
| 213 |
self._save_bgp_speaker(bgp_speaker['id']) |
| 214 |
expected_filtered_dragent_ids = [agents[1].id] |
| 215 |
self._test_filter_agents_helper( |
| 216 |
bgp_speaker, |
| 217 |
expected_filtered_dragent_ids=expected_filtered_dragent_ids) |
| 218 |
|
| 219 |
|
| 220 |
class TestAutoScheduleBgpSpeakers(TestBgpDrAgentSchedulerBaseTestCase): |
| 221 |
"""Unit test scenarios for schedule_unscheduled_bgp_speakers. |
| 222 |
|
| 223 |
bgp_speaker_present |
| 224 |
BGP speaker is present or not |
| 225 |
|
| 226 |
scheduled_already |
| 227 |
BGP speaker is already scheduled to the agent or not |
| 228 |
|
| 229 |
agent_down |
| 230 |
BGP DRAgent is down or alive |
| 231 |
|
| 232 |
valid_host |
| 233 |
If true, then an valid host is passed to schedule BGP speaker, |
| 234 |
else an invalid host is passed. |
| 235 |
""" |
| 236 |
scenarios = [ |
| 237 |
('BGP speaker present', |
| 238 |
dict(bgp_speaker_present=True, |
| 239 |
scheduled_already=False, |
| 240 |
agent_down=False, |
| 241 |
valid_host=True, |
| 242 |
expected_result=True)), |
| 243 |
|
| 244 |
('No BGP speaker', |
| 245 |
dict(bgp_speaker_present=False, |
| 246 |
scheduled_already=False, |
| 247 |
agent_down=False, |
| 248 |
valid_host=True, |
| 249 |
expected_result=False)), |
| 250 |
|
| 251 |
('BGP speaker already scheduled', |
| 252 |
dict(bgp_speaker_present=True, |
| 253 |
scheduled_already=True, |
| 254 |
agent_down=False, |
| 255 |
valid_host=True, |
| 256 |
expected_result=False)), |
| 257 |
|
| 258 |
('BGP DR agent down', |
| 259 |
dict(bgp_speaker_present=True, |
| 260 |
scheduled_already=False, |
| 261 |
agent_down=True, |
| 262 |
valid_host=False, |
| 263 |
expected_result=False)), |
| 264 |
|
| 265 |
('Invalid host', |
| 266 |
dict(bgp_speaker_present=True, |
| 267 |
scheduled_already=False, |
| 268 |
agent_down=False, |
| 269 |
valid_host=False, |
| 270 |
expected_result=False)), |
| 271 |
] |
| 272 |
|
| 273 |
def test_auto_schedule_bgp_speaker(self): |
| 274 |
scheduler = bgp_dras.ChanceScheduler() |
| 275 |
if self.bgp_speaker_present: |
| 276 |
down_agent_count = 1 if self.agent_down else 0 |
| 277 |
agents = self._create_and_set_agents_down( |
| 278 |
['host-a'], down_agent_count=down_agent_count) |
| 279 |
if self.scheduled_already: |
| 280 |
self._test_schedule_bind_bgp_speaker(agents, |
| 281 |
self.bgp_speaker_id) |
| 282 |
|
| 283 |
expected_hosted_agents = (1 if self.bgp_speaker_present and |
| 284 |
self.valid_host else 0) |
| 285 |
host = "host-a" if self.valid_host else "host-b" |
| 286 |
observed_ret_value = scheduler.schedule_unscheduled_bgp_speakers( |
| 287 |
self.ctx, host) |
| 288 |
self.assertEqual(self.expected_result, observed_ret_value) |
| 289 |
hosted_agents = self.ctx.session.query( |
| 290 |
bgp_dras_db.BgpSpeakerDrAgentBinding).all() |
| 291 |
self.assertEqual(expected_hosted_agents, len(hosted_agents)) |
| 292 |
|
| 293 |
|
| 294 |
class TestRescheduleBgpSpeaker(TestBgpDrAgentSchedulerBaseTestCase, |
| 295 |
bgp_db.BgpDbMixin): |
| 296 |
|
| 297 |
def setUp(self): |
| 298 |
super().setUp() |
| 299 |
bgp_notify_p = mock.patch('neutron_dynamic_routing.api.rpc.' |
| 300 |
'agentnotifiers.bgp_dr_rpc_agent_api.' |
| 301 |
'BgpDrAgentNotifyApi') |
| 302 |
bgp_notify_p.start() |
| 303 |
rpc_conn_p = mock.patch('neutron_lib.rpc.Connection') |
| 304 |
rpc_conn_p.start() |
| 305 |
self.plugin = bgp_plugin.BgpPlugin() |
| 306 |
self.scheduler = bgp_dras.ChanceScheduler() |
| 307 |
self.host1 = 'host-a' |
| 308 |
self.host2 = 'host-b' |
| 309 |
|
| 310 |
def _kill_bgp_dragent(self, hosts): |
| 311 |
agents = [] |
| 312 |
for host in hosts: |
| 313 |
agents.append( |
| 314 |
helpers.register_bgp_dragent(host=host, alive=False)) |
| 315 |
return agents |
| 316 |
|
| 317 |
def _schedule_bind_bgp_speaker(self, agents, bgp_speaker_id): |
| 318 |
self.scheduler.resource_filter.bind(self.ctx, agents, bgp_speaker_id) |
| 319 |
return self._get_dragent_bgp_speaker_bindings(bgp_speaker_id) |
| 320 |
|
| 321 |
def test_reschedule_bgp_speaker_bound_to_down_dragent(self): |
| 322 |
agents = self._create_and_set_agents_down([self.host1, self.host2]) |
| 323 |
self._schedule_bind_bgp_speaker([agents[0]], self.bgp_speaker_id) |
| 324 |
self._kill_bgp_dragent([self.host1]) |
| 325 |
self.plugin.remove_bgp_speaker_from_down_dragents() |
| 326 |
binds = self._get_dragent_bgp_speaker_bindings(self.bgp_speaker_id) |
| 327 |
self.assertEqual(binds[0].agent_id, agents[1].id) |
| 328 |
|
| 329 |
def test_no_schedule_with_non_available_dragent(self): |
| 330 |
agents = self._create_and_set_agents_down([self.host1, self.host2]) |
| 331 |
self._schedule_bind_bgp_speaker([agents[0]], self.bgp_speaker_id) |
| 332 |
self._kill_bgp_dragent([self.host1, self.host2]) |
| 333 |
self.plugin.remove_bgp_speaker_from_down_dragents() |
| 334 |
binds = self._get_dragent_bgp_speaker_bindings(self.bgp_speaker_id) |
| 335 |
self.assertEqual(binds, []) |
| 336 |
|
| 337 |
def test_schedule_unbind_bgp_speaker(self): |
| 338 |
agents = self._create_and_set_agents_down([self.host1, self.host2]) |
| 339 |
self._schedule_bind_bgp_speaker([agents[0]], self.bgp_speaker_id) |
| 340 |
self._kill_bgp_dragent([self.host1, self.host2]) |
| 341 |
self.plugin.remove_bgp_speaker_from_down_dragents() |
| 342 |
binds = self._get_dragent_bgp_speaker_bindings(self.bgp_speaker_id) |
| 343 |
self.assertEqual(binds, []) |
| 344 |
# schedule a unbind bgp speaker |
| 345 |
agents = self._create_and_set_agents_down([self.host1]) |
| 346 |
self.scheduler.schedule_all_unscheduled_bgp_speakers(self.ctx) |
| 347 |
binds = self._get_dragent_bgp_speaker_bindings(self.bgp_speaker_id) |
| 348 |
self.assertEqual(binds[0].agent_id, agents[0].id) |