summaryrefslogtreecommitdiff
path: root/neutronclient/tests/unit/osc/v2/dynamic_routing/fakes.py
blob: 188f068710a30cb72bc4da65186f7f1d1cc89211 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from unittest import mock
import uuid

from openstack.network.v2 import agent as _agent
from openstack.network.v2 import bgp_peer as _bgp_peer
from openstack.network.v2 import bgp_speaker as _bgp_speaker

from neutronclient.tests.unit.osc.v2 import fakes


class TestNeutronDynamicRoutingOSCV2(fakes.TestNeutronClientOSCV2):
    def setUp(self):
        super(TestNeutronDynamicRoutingOSCV2, self).setUp()
        self.neutronclient.find_resource = mock.Mock(
            side_effect=lambda resource, name_or_id, project_id=None,
            cmd_resource=None, parent_id=None, fields=None:
            {'id': name_or_id})

        self.networkclient.find_bgp_speaker = mock.Mock(
            side_effect=lambda name_or_id, project_id=None,
            cmd_resource=None, parent_id=None, fields=None,
            ignore_missing=False:
            _bgp_speaker.BgpSpeaker(id=name_or_id))
        self.networkclient.find_bgp_peer = mock.Mock(
            side_effect=lambda name_or_id, project_id=None,
            cmd_resource=None, parent_id=None, fields=None,
            ignore_missing=False:
            _bgp_peer.BgpPeer(id=name_or_id))


class FakeBgpSpeaker(object):
    """Fake one or more bgp speakers."""

    @staticmethod
    def create_one_bgp_speaker(attrs=None):
        attrs = attrs or {}
        # Set default attributes.
        bgp_speaker_attrs = {
            'peers': [],
            'local_as': 200,
            'advertise_tenant_networks': True,
            'networks': [],
            'ip_version': 4,
            'advertise_floating_ip_host_routes': True,
            'id': uuid.uuid4().hex,
            'name': 'bgp-speaker-' + uuid.uuid4().hex,
            'tenant_id': uuid.uuid4().hex,
        }

        # Overwrite default attributes.
        bgp_speaker_attrs.update(attrs)
        ret_bgp_speaker = _bgp_speaker.BgpSpeaker(**bgp_speaker_attrs)

        return ret_bgp_speaker

    @staticmethod
    def create_bgp_speakers(attrs=None, count=1):
        """Create multiple fake bgp speakers.

        """
        bgp_speakers = []
        for i in range(count):
            bgp_speaker = FakeBgpSpeaker.create_one_bgp_speaker(attrs)
            bgp_speakers.append(bgp_speaker)

        return bgp_speakers


class FakeBgpPeer(object):
    """Fake one or more bgp peers."""

    @staticmethod
    def create_one_bgp_peer(attrs=None):
        attrs = attrs or {}
        # Set default attributes.
        bgp_peer_attrs = {
            'auth_type': None,
            'peer_ip': '1.1.1.1',
            'remote_as': 100,
            'id': uuid.uuid4().hex,
            'name': 'bgp-peer-' + uuid.uuid4().hex,
            'tenant_id': uuid.uuid4().hex,
        }

        # Overwrite default attributes.
        bgp_peer_attrs.update(attrs)
        ret_bgp_peer = _bgp_peer.BgpPeer(**bgp_peer_attrs)

        return ret_bgp_peer

    @staticmethod
    def create_bgp_peers(attrs=None, count=1):
        """Create one or multiple fake bgp peers."""
        bgp_peers = []
        for i in range(count):
            bgp_peer = FakeBgpPeer.create_one_bgp_peer(attrs)
            bgp_peers.append(bgp_peer)

        return bgp_peers


class FakeDRAgent(object):
    """Fake one or more dynamic routing agents."""

    @staticmethod
    def create_one_dragent(attrs=None):
        attrs = attrs or {}
        # Set default attributes.
        dragent_attrs = {
            'binary': 'neutron-bgp-dragent',
            'admin_state_up': True,
            'availability_zone': None,
            'alive': True,
            'topic': 'bgp_dragent',
            'host': 'network-' + uuid.uuid4().hex,
            'name': 'bgp-dragent-' + uuid.uuid4().hex,
            'agent_type': 'BGP dynamic routing agent',
            'id': uuid.uuid4().hex,
        }

        # Overwrite default attributes.
        dragent_attrs.update(attrs)
        return _agent.Agent(**dragent_attrs)

    @staticmethod
    def create_dragents(attrs=None, count=1):
        """Create one or multiple fake dynamic routing agents."""
        agents = []
        for i in range(count):
            agent = FakeDRAgent.create_one_dragent(attrs)
            agents.append(agent)

        return agents