summaryrefslogtreecommitdiff
path: root/test/units/modules/network/ios/test_ios_bgp.py
blob: 792219778ca86f47f6a81f9b0b603d8d91ddefb1 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#
# (c) 2019, Ansible by Red Hat, inc
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.module_utils.network.ios.providers.cli.config.bgp.process import Provider
from ansible.modules.network.ios import ios_bgp
from .ios_module import TestIosModule, load_fixture


class TestIosBgpModule(TestIosModule):
    module = ios_bgp

    def setUp(self):
        super(TestIosBgpModule, self).setUp()
        self._bgp_config = load_fixture('ios_bgp_config.cfg')

    def test_ios_bgp(self):
        obj = Provider(params=dict(config=dict(bgp_as=64496, router_id='192.0.2.2', networks=None,
                                               address_family=None), operation='merge'))
        commands = obj.render(self._bgp_config)
        self.assertEqual(commands, ['router bgp 64496', 'bgp router-id 192.0.2.2', 'exit'])

    def test_ios_bgp_idempotent(self):
        obj = Provider(params=dict(config=dict(bgp_as=64496, router_id='192.0.2.1', networks=None,
                                               address_family=None), operation='merge'))
        commands = obj.render(self._bgp_config)
        self.assertEqual(commands, [])

    def test_ios_bgp_remove(self):
        obj = Provider(params=dict(config=dict(bgp_as=64496, networks=None, address_family=None), operation='delete'))
        commands = obj.render(self._bgp_config)
        self.assertEqual(commands, ['no router bgp 64496'])

    def test_ios_bgp_neighbor(self):
        obj = Provider(params=dict(config=dict(bgp_as=64496, neighbors=[dict(neighbor='192.51.100.2', remote_as=64496)],
                                               networks=None, address_family=None),
                                   operation='merge'))
        commands = obj.render(self._bgp_config)
        self.assertEqual(commands, ['router bgp 64496', 'neighbor 192.51.100.2 remote-as 64496', 'exit'])

    def test_ios_bgp_neighbor_idempotent(self):
        obj = Provider(params=dict(config=dict(bgp_as=64496, neighbors=[dict(neighbor='192.51.100.1', remote_as=64496,
                                                                             timers=dict(keepalive=120, holdtime=360,
                                                                                         min_neighbor_holdtime=360))],
                                               networks=None, address_family=None),
                                   operation='merge'))
        commands = obj.render(self._bgp_config)
        self.assertEqual(commands, [])

    def test_ios_bgp_network(self):
        obj = Provider(params=dict(config=dict(bgp_as=64496, networks=[dict(prefix='192.0.1.0', masklen=23, route_map='RMAP_1')],
                                               address_family=None),
                                   operation='merge'))
        commands = obj.render(self._bgp_config)
        self.assertEqual(sorted(commands), sorted(['router bgp 64496', 'network 192.0.1.0 mask 255.255.254.0 route-map RMAP_1',
                                                   'exit']))

    def test_ios_bgp_network_idempotent(self):
        obj = Provider(
            params=dict(config=dict(bgp_as=64496, networks=[dict(prefix='192.0.2.0', masklen=23, route_map='RMAP_1'),
                                                            dict(prefix='198.51.100.0', masklen=25,
                                                                 route_map='RMAP_2')],
                                    address_family=None),
                        operation='merge'))
        commands = obj.render(self._bgp_config)
        self.assertEqual(commands, [])

    def test_ios_bgp_address_family_redistribute(self):
        rd_1 = dict(protocol='ospf', id='233', metric=90, route_map=None)

        config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='unicast', redistribute=[rd_1])],
                      networks=None)

        obj = Provider(params=dict(config=config, operation='merge'))

        commands = obj.render(self._bgp_config)
        cmd = ['router bgp 64496', 'address-family ipv4', 'redistribute ospf 233 metric 90',
               'exit-address-family', 'exit']
        self.assertEqual(sorted(commands), sorted(cmd))

    def test_ios_bgp_address_family_redistribute_idempotent(self):
        rd_1 = dict(protocol='eigrp', metric=10, route_map='RMAP_3', id=None)
        rd_2 = dict(protocol='static', metric=100, id=None, route_map=None)

        config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='unicast', redistribute=[rd_1, rd_2])],
                      networks=None)

        obj = Provider(params=dict(config=config, operation='merge'))

        commands = obj.render(self._bgp_config)
        self.assertEqual(commands, [])

    def test_ios_bgp_address_family_neighbors(self):
        af_nbr_1 = dict(neighbor='192.51.100.1', maximum_prefix=35, activate=True)
        af_nbr_2 = dict(neighbor='192.51.100.3', route_reflector_client=True, activate=True)

        config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='multicast', neighbors=[af_nbr_1, af_nbr_2])],
                      networks=None)

        obj = Provider(params=dict(config=config, operation='merge'))

        commands = obj.render(self._bgp_config)
        cmd = ['router bgp 64496', 'address-family ipv4 multicast', 'neighbor 192.51.100.1 activate',
               'neighbor 192.51.100.1 maximum-prefix 35', 'neighbor 192.51.100.3 activate',
               'neighbor 192.51.100.3 route-reflector-client', 'exit-address-family', 'exit']
        self.assertEqual(sorted(commands), sorted(cmd))

    def test_ios_bgp_address_family_neighbors_idempotent(self):
        af_nbr_1 = dict(neighbor='203.0.113.1', remove_private_as=True, maximum_prefix=100)

        config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='unicast', neighbors=[af_nbr_1])],
                      networks=None)

        obj = Provider(params=dict(config=config, operation='merge'))

        commands = obj.render(self._bgp_config)
        self.assertEqual(commands, [])

    def test_ios_bgp_address_family_networks(self):
        net = dict(prefix='1.0.0.0', masklen=8, route_map='RMAP_1')
        net2 = dict(prefix='192.168.1.0', masklen=24, route_map='RMAP_2')

        config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='multicast', networks=[net, net2])],
                      networks=None)

        obj = Provider(params=dict(config=config, operation='merge'))

        commands = obj.render(self._bgp_config)
        cmd = ['router bgp 64496', 'address-family ipv4 multicast', 'network 1.0.0.0 mask 255.0.0.0 route-map RMAP_1',
               'network 192.168.1.0 mask 255.255.255.0 route-map RMAP_2', 'exit-address-family', 'exit']
        self.assertEqual(sorted(commands), sorted(cmd))

    def test_ios_bgp_address_family_networks_idempotent(self):
        net = dict(prefix='203.0.113.0', masklen=27, route_map='RMAP_1')
        net2 = dict(prefix='192.0.2.0', masklen=26, route_map='RMAP_2')

        config = dict(bgp_as=64496, address_family=[dict(afi='ipv4', safi='multicast', networks=[net, net2])],
                      networks=None)

        obj = Provider(params=dict(config=config, operation='merge'))

        commands = obj.render(self._bgp_config)
        self.assertEqual(commands, [])

    def test_ios_bgp_operation_override(self):
        net_1 = dict(prefix='1.0.0.0', masklen=8, route_map='RMAP_1')
        net_2 = dict(prefix='192.168.1.0', masklen=24, route_map='RMAP_2')
        nbr_1 = dict(neighbor='192.51.100.1', remote_as=64496, update_source='GigabitEthernet0/1')
        nbr_2 = dict(neighbor='192.51.100.3', remote_as=64496, timers=dict(keepalive=300, holdtime=360,
                                                                           min_neighbor_holdtime=360))
        af_nbr_1 = dict(neighbor='192.51.100.1', maximum_prefix=35)
        af_nbr_2 = dict(neighbor='192.51.100.3', route_reflector_client=True)

        af_1 = dict(afi='ipv4', safi='unicast', neighbors=[af_nbr_1, af_nbr_2])
        af_2 = dict(afi='ipv4', safi='multicast', networks=[net_1, net_2])
        config = dict(bgp_as=64496, neighbors=[nbr_1, nbr_2], address_family=[af_1, af_2], networks=None)

        obj = Provider(params=dict(config=config, operation='override'))
        commands = obj.render(self._bgp_config)

        cmd = ['no router bgp 64496', 'router bgp 64496', 'neighbor 192.51.100.1 remote-as 64496',
               'neighbor 192.51.100.1 update-source GigabitEthernet0/1', 'neighbor 192.51.100.3 remote-as 64496',
               'neighbor 192.51.100.3 timers 300 360 360', 'address-family ipv4',
               'neighbor 192.51.100.1 maximum-prefix 35', 'neighbor 192.51.100.3 route-reflector-client',
               'exit-address-family',
               'address-family ipv4 multicast', 'network 1.0.0.0 mask 255.0.0.0 route-map RMAP_1',
               'network 192.168.1.0 mask 255.255.255.0 route-map RMAP_2',
               'exit-address-family', 'exit']

        self.assertEqual(sorted(commands), sorted(cmd))

    def test_ios_bgp_operation_replace(self):
        rd = dict(protocol='ospf', id=223, metric=110, route_map=None)
        net = dict(prefix='203.0.113.0', masklen=27, route_map='RMAP_1')
        net2 = dict(prefix='192.0.2.0', masklen=26, route_map='RMAP_2')

        af_1 = dict(afi='ipv4', safi='unicast', redistribute=[rd])
        af_2 = dict(afi='ipv4', safi='multicast', networks=[net, net2])

        config = dict(bgp_as=64496, address_family=[af_1, af_2], networks=None)
        obj = Provider(params=dict(config=config, operation='replace'))
        commands = obj.render(self._bgp_config)

        cmd = ['router bgp 64496', 'address-family ipv4', 'redistribute ospf 223 metric 110',
               'no redistribute eigrp',
               'no redistribute static', 'exit-address-family', 'exit']

        self.assertEqual(sorted(commands), sorted(cmd))

    def test_ios_bgp_operation_replace_with_new_as(self):
        rd = dict(protocol='ospf', id=223, metric=110, route_map=None)

        af_1 = dict(afi='ipv4', safi='unicast', redistribute=[rd])

        config = dict(bgp_as=64497, address_family=[af_1], networks=None)
        obj = Provider(params=dict(config=config, operation='replace'))
        commands = obj.render(self._bgp_config)

        cmd = ['no router bgp 64496', 'router bgp 64497', 'address-family ipv4',
               'redistribute ospf 223 metric 110',
               'exit-address-family', 'exit']

        self.assertEqual(sorted(commands), sorted(cmd))