| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
from unittest import mock |
| 14 |
import uuid |
| 15 |
|
| 16 |
from openstack.network.v2 import agent as _agent |
| 17 |
from openstack.network.v2 import bgp_peer as _bgp_peer |
| 18 |
from openstack.network.v2 import bgp_speaker as _bgp_speaker |
| 19 |
|
| 20 |
from neutronclient.tests.unit.osc.v2 import fakes |
| 21 |
|
| 22 |
|
| 23 |
class TestNeutronDynamicRoutingOSCV2(fakes.TestNeutronClientOSCV2): |
| 24 |
def setUp(self): |
| 25 |
super(TestNeutronDynamicRoutingOSCV2, self).setUp() |
| 26 |
self.neutronclient.find_resource = mock.Mock( |
| 27 |
side_effect=lambda resource, name_or_id, project_id=None, |
| 28 |
cmd_resource=None, parent_id=None, fields=None: |
| 29 |
{'id': name_or_id}) |
| 30 |
|
| 31 |
self.networkclient.find_bgp_speaker = mock.Mock( |
| 32 |
side_effect=lambda name_or_id, project_id=None, |
| 33 |
cmd_resource=None, parent_id=None, fields=None, |
| 34 |
ignore_missing=False: |
| 35 |
_bgp_speaker.BgpSpeaker(id=name_or_id)) |
| 36 |
self.networkclient.find_bgp_peer = mock.Mock( |
| 37 |
side_effect=lambda name_or_id, project_id=None, |
| 38 |
cmd_resource=None, parent_id=None, fields=None, |
| 39 |
ignore_missing=False: |
| 40 |
_bgp_peer.BgpPeer(id=name_or_id)) |
| 41 |
|
| 42 |
|
| 43 |
class FakeBgpSpeaker(object): |
| 44 |
"""Fake one or more bgp speakers.""" |
| 45 |
|
| 46 |
@staticmethod |
| 47 |
def create_one_bgp_speaker(attrs=None): |
| 48 |
attrs = attrs or {} |
| 49 |
# Set default attributes. |
| 50 |
bgp_speaker_attrs = { |
| 51 |
'peers': [], |
| 52 |
'local_as': 200, |
| 53 |
'advertise_tenant_networks': True, |
| 54 |
'networks': [], |
| 55 |
'ip_version': 4, |
| 56 |
'advertise_floating_ip_host_routes': True, |
| 57 |
'id': uuid.uuid4().hex, |
| 58 |
'name': 'bgp-speaker-' + uuid.uuid4().hex, |
| 59 |
'tenant_id': uuid.uuid4().hex, |
| 60 |
} |
| 61 |
|
| 62 |
# Overwrite default attributes. |
| 63 |
bgp_speaker_attrs.update(attrs) |
| 64 |
ret_bgp_speaker = _bgp_speaker.BgpSpeaker(**bgp_speaker_attrs) |
| 65 |
|
| 66 |
return ret_bgp_speaker |
| 67 |
|
| 68 |
@staticmethod |
| 69 |
def create_bgp_speakers(attrs=None, count=1): |
| 70 |
"""Create multiple fake bgp speakers. |
| 71 |
|
| 72 |
""" |
| 73 |
bgp_speakers = [] |
| 74 |
for i in range(count): |
| 75 |
bgp_speaker = FakeBgpSpeaker.create_one_bgp_speaker(attrs) |
| 76 |
bgp_speakers.append(bgp_speaker) |
| 77 |
|
| 78 |
return bgp_speakers |
| 79 |
|
| 80 |
|
| 81 |
class FakeBgpPeer(object): |
| 82 |
"""Fake one or more bgp peers.""" |
| 83 |
|
| 84 |
@staticmethod |
| 85 |
def create_one_bgp_peer(attrs=None): |
| 86 |
attrs = attrs or {} |
| 87 |
# Set default attributes. |
| 88 |
bgp_peer_attrs = { |
| 89 |
'auth_type': None, |
| 90 |
'peer_ip': '1.1.1.1', |
| 91 |
'remote_as': 100, |
| 92 |
'id': uuid.uuid4().hex, |
| 93 |
'name': 'bgp-peer-' + uuid.uuid4().hex, |
| 94 |
'tenant_id': uuid.uuid4().hex, |
| 95 |
} |
| 96 |
|
| 97 |
# Overwrite default attributes. |
| 98 |
bgp_peer_attrs.update(attrs) |
| 99 |
ret_bgp_peer = _bgp_peer.BgpPeer(**bgp_peer_attrs) |
| 100 |
|
| 101 |
return ret_bgp_peer |
| 102 |
|
| 103 |
@staticmethod |
| 104 |
def create_bgp_peers(attrs=None, count=1): |
| 105 |
"""Create one or multiple fake bgp peers.""" |
| 106 |
bgp_peers = [] |
| 107 |
for i in range(count): |
| 108 |
bgp_peer = FakeBgpPeer.create_one_bgp_peer(attrs) |
| 109 |
bgp_peers.append(bgp_peer) |
| 110 |
|
| 111 |
return bgp_peers |
| 112 |
|
| 113 |
|
| 114 |
class FakeDRAgent(object): |
| 115 |
"""Fake one or more dynamic routing agents.""" |
| 116 |
|
| 117 |
@staticmethod |
| 118 |
def create_one_dragent(attrs=None): |
| 119 |
attrs = attrs or {} |
| 120 |
# Set default attributes. |
| 121 |
dragent_attrs = { |
| 122 |
'binary': 'neutron-bgp-dragent', |
| 123 |
'admin_state_up': True, |
| 124 |
'availability_zone': None, |
| 125 |
'alive': True, |
| 126 |
'topic': 'bgp_dragent', |
| 127 |
'host': 'network-' + uuid.uuid4().hex, |
| 128 |
'name': 'bgp-dragent-' + uuid.uuid4().hex, |
| 129 |
'agent_type': 'BGP dynamic routing agent', |
| 130 |
'id': uuid.uuid4().hex, |
| 131 |
} |
| 132 |
|
| 133 |
# Overwrite default attributes. |
| 134 |
dragent_attrs.update(attrs) |
| 135 |
return _agent.Agent(**dragent_attrs) |
| 136 |
|
| 137 |
@staticmethod |
| 138 |
def create_dragents(attrs=None, count=1): |
| 139 |
"""Create one or multiple fake dynamic routing agents.""" |
| 140 |
agents = [] |
| 141 |
for i in range(count): |
| 142 |
agent = FakeDRAgent.create_one_dragent(attrs) |
| 143 |
agents.append(agent) |
| 144 |
|
| 145 |
return agents |