| 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 neutron_lib import context |
| 17 |
import testscenarios |
| 18 |
|
| 19 |
from neutron.db import agents_db |
| 20 |
from neutron.tests.unit import testlib_api |
| 21 |
|
| 22 |
from neutron_dynamic_routing.db import bgp_db |
| 23 |
from neutron_dynamic_routing.db import bgp_dragentscheduler_db as bgp_dras_db |
| 24 |
from neutron_dynamic_routing.services.bgp.scheduler import bgp_dragent_scheduler as bgp_dras # noqa |
| 25 |
from neutron_dynamic_routing.tests.common import helpers |
| 26 |
|
| 27 |
# Required to generate tests from scenarios. Not compatible with nose. |
| 28 |
load_tests = testscenarios.load_tests_apply_scenarios |
| 29 |
|
| 30 |
|
| 31 |
class TestAutoSchedule(testlib_api.SqlTestCase, |
| 32 |
bgp_dras_db.BgpDrAgentSchedulerDbMixin, |
| 33 |
agents_db.AgentDbMixin): |
| 34 |
"""Test various scenarios for schedule_unscheduled_bgp_speakers. |
| 35 |
|
| 36 |
Below is the brief description of the scenario variables |
| 37 |
-------------------------------------------------------- |
| 38 |
host_count |
| 39 |
number of hosts. |
| 40 |
|
| 41 |
agent_count |
| 42 |
number of BGP dynamic routing agents. |
| 43 |
|
| 44 |
down_agent_count |
| 45 |
number of DRAgents which are inactive. |
| 46 |
|
| 47 |
bgp_speaker_count |
| 48 |
Number of bgp_speakers. |
| 49 |
|
| 50 |
hosted_bgp_speakers |
| 51 |
A mapping of agent id to the ids of the bgp_speakers that they |
| 52 |
should be initially hosting. |
| 53 |
|
| 54 |
expected_schedule_return_value |
| 55 |
Expected return value of 'schedule_unscheduled_bgp_speakers'. |
| 56 |
|
| 57 |
expected_hosted_bgp_speakers |
| 58 |
This stores the expected bgp_speakers that should have been |
| 59 |
scheduled (or that could have already been scheduled) for each |
| 60 |
agent after the 'schedule_unscheduled_bgp_speakers' function is |
| 61 |
called. |
| 62 |
""" |
| 63 |
|
| 64 |
scenarios = [ |
| 65 |
('No BgpDrAgent scheduled, if no DRAgent is present', |
| 66 |
dict(host_count=1, |
| 67 |
agent_count=0, |
| 68 |
down_agent_count=0, |
| 69 |
bgp_speaker_count=1, |
| 70 |
hosted_bgp_speakers={}, |
| 71 |
expected_schedule_return_value=False)), |
| 72 |
|
| 73 |
('No BgpDrAgent scheduled, if no BGP speaker are present', |
| 74 |
dict(host_count=1, |
| 75 |
agent_count=1, |
| 76 |
down_agent_count=0, |
| 77 |
bgp_speaker_count=0, |
| 78 |
hosted_bgp_speakers={}, |
| 79 |
expected_schedule_return_value=False, |
| 80 |
expected_hosted_bgp_speakers={'agent-0': []})), |
| 81 |
|
| 82 |
('No BgpDrAgent scheduled, if BGP speaker already hosted', |
| 83 |
dict(host_count=1, |
| 84 |
agent_count=1, |
| 85 |
down_agent_count=0, |
| 86 |
bgp_speaker_count=1, |
| 87 |
hosted_bgp_speakers={'agent-0': ['bgp-speaker-0']}, |
| 88 |
expected_schedule_return_value=False, |
| 89 |
expected_hosted_bgp_speakers={'agent-0': ['bgp-speaker-0']})), |
| 90 |
|
| 91 |
('BgpDrAgent scheduled to the speaker, if the speaker is not hosted', |
| 92 |
dict(host_count=1, |
| 93 |
agent_count=1, |
| 94 |
down_agent_count=0, |
| 95 |
bgp_speaker_count=1, |
| 96 |
hosted_bgp_speakers={}, |
| 97 |
expected_schedule_return_value=True, |
| 98 |
expected_hosted_bgp_speakers={'agent-0': ['bgp-speaker-0']})), |
| 99 |
|
| 100 |
('No BgpDrAgent scheduled, if all the agents are down', |
| 101 |
dict(host_count=2, |
| 102 |
agent_count=2, |
| 103 |
down_agent_count=2, |
| 104 |
bgp_speaker_count=1, |
| 105 |
hosted_bgp_speakers={}, |
| 106 |
expected_schedule_return_value=False, |
| 107 |
expected_hosted_bgp_speakers={'agent-0': [], |
| 108 |
'agent-1': [], })), |
| 109 |
] |
| 110 |
|
| 111 |
def _strip_host_index(self, name): |
| 112 |
"""Strips the host index. |
| 113 |
|
| 114 |
Eg. if name = '2-agent-3', then 'agent-3' is returned. |
| 115 |
""" |
| 116 |
return name[name.find('-') + 1:] |
| 117 |
|
| 118 |
def _extract_index(self, name): |
| 119 |
"""Extracts the index number and returns. |
| 120 |
|
| 121 |
Eg. if name = '2-agent-3', then 3 is returned |
| 122 |
""" |
| 123 |
return int(name.split('-')[-1]) |
| 124 |
|
| 125 |
def _get_hosted_bgp_speakers_on_dragent(self, agent_id): |
| 126 |
query = self.ctx.session.query( |
| 127 |
bgp_dras_db.BgpSpeakerDrAgentBinding.bgp_speaker_id) |
| 128 |
query = query.filter( |
| 129 |
bgp_dras_db.BgpSpeakerDrAgentBinding.agent_id == |
| 130 |
agent_id) |
| 131 |
|
| 132 |
return [item[0] for item in query] |
| 133 |
|
| 134 |
def _create_and_set_agents_down(self, hosts, agent_count=0, |
| 135 |
down_agent_count=0, admin_state_up=True): |
| 136 |
agents = [] |
| 137 |
if agent_count: |
| 138 |
for i, host in enumerate(hosts): |
| 139 |
is_alive = i >= down_agent_count |
| 140 |
agents.append(helpers.register_bgp_dragent( |
| 141 |
host, |
| 142 |
admin_state_up=admin_state_up, |
| 143 |
alive=is_alive)) |
| 144 |
return agents |
| 145 |
|
| 146 |
def _save_bgp_speakers(self, bgp_speakers): |
| 147 |
cls = bgp_db.BgpDbMixin() |
| 148 |
bgp_speaker_body = { |
| 149 |
'bgp_speaker': {'name': 'fake_bgp_speaker', |
| 150 |
'ip_version': '4', |
| 151 |
'local_as': '123', |
| 152 |
'advertise_floating_ip_host_routes': False, |
| 153 |
'advertise_tenant_networks': False, |
| 154 |
'peers': [], |
| 155 |
'networks': []}} |
| 156 |
i = 1 |
| 157 |
for bgp_speaker_id in bgp_speakers: |
| 158 |
bgp_speaker_body['bgp_speaker']['local_as'] = i |
| 159 |
cls._save_bgp_speaker(self.ctx, bgp_speaker_body, |
| 160 |
uuid=bgp_speaker_id) |
| 161 |
i = i + 1 |
| 162 |
|
| 163 |
def _test_auto_schedule(self, host_index): |
| 164 |
scheduler = bgp_dras.ChanceScheduler() |
| 165 |
self.ctx = context.get_admin_context() |
| 166 |
msg = 'host_index = %s' % host_index |
| 167 |
|
| 168 |
# create hosts |
| 169 |
hosts = ['{}-agent-{}'.format(host_index, i) |
| 170 |
for i in range(self.host_count)] |
| 171 |
bgp_dragents = self._create_and_set_agents_down(hosts, |
| 172 |
self.agent_count, |
| 173 |
self.down_agent_count) |
| 174 |
|
| 175 |
# create bgp_speakers |
| 176 |
self._bgp_speakers = ['{}-bgp-speaker-{}'.format(host_index, i) |
| 177 |
for i in range(self.bgp_speaker_count)] |
| 178 |
self._save_bgp_speakers(self._bgp_speakers) |
| 179 |
|
| 180 |
# pre schedule the bgp_speakers to the agents defined in |
| 181 |
# self.hosted_bgp_speakers before calling auto_schedule_bgp_speaker |
| 182 |
for agent, bgp_speakers in self.hosted_bgp_speakers.items(): |
| 183 |
agent_index = self._extract_index(agent) |
| 184 |
for bgp_speaker in bgp_speakers: |
| 185 |
bs_index = self._extract_index(bgp_speaker) |
| 186 |
scheduler.bind(self.ctx, [bgp_dragents[agent_index]], |
| 187 |
self._bgp_speakers[bs_index]) |
| 188 |
|
| 189 |
retval = scheduler.schedule_unscheduled_bgp_speakers(self.ctx, |
| 190 |
hosts[host_index]) |
| 191 |
self.assertEqual(self.expected_schedule_return_value, retval, |
| 192 |
message=msg) |
| 193 |
|
| 194 |
if self.agent_count: |
| 195 |
agent_id = bgp_dragents[host_index].id |
| 196 |
hosted_bgp_speakers = self._get_hosted_bgp_speakers_on_dragent( |
| 197 |
agent_id) |
| 198 |
hosted_bs_ids = [self._strip_host_index(net) |
| 199 |
for net in hosted_bgp_speakers] |
| 200 |
expected_hosted_bgp_speakers = self.expected_hosted_bgp_speakers[ |
| 201 |
'agent-%s' % host_index] |
| 202 |
self.assertCountEqual(hosted_bs_ids, expected_hosted_bgp_speakers, |
| 203 |
msg) |
| 204 |
|
| 205 |
def test_auto_schedule(self): |
| 206 |
for i in range(self.host_count): |
| 207 |
self._test_auto_schedule(i) |