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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
__metaclass__ = type
import unittest
from unittest.mock import MagicMock
from ansible_collections.vyos.rest.plugins.modules.vyos_bgp_address_family import (
_device_to_argspec,
_global_af_from_device,
_global_af_to_device,
_neighbor_af_from_device,
_neighbor_af_to_device,
build_commands,
get_running_config,
)
from .base import load_fixture
_BASE = ["protocols", "bgp"]
class VyOSModuleTestCase(unittest.TestCase):
def setUp(self):
self.mock_vyos = MagicMock()
self.fixture = load_fixture("bgp_af_running.json")
self.mock_vyos.get_config = MagicMock(return_value=self.fixture)
class TestGetRunningConfig(VyOSModuleTestCase):
def test_returns_raw_device_dict(self):
self.assertEqual(get_running_config(self.mock_vyos), self.fixture)
def test_empty_config(self):
self.mock_vyos.get_config = MagicMock(return_value=None)
self.assertEqual(get_running_config(self.mock_vyos), {})
class TestNeighborAfToDevice(unittest.TestCase):
"""The three genuine device-shape exceptions, individually, plus proof
that everything else is untouched by _autoclean alone."""
def test_soft_reconfiguration_nests_under_inbound(self):
result = _neighbor_af_to_device([{"afi": "ipv4", "soft_reconfiguration": True}])
self.assertEqual(result, {"ipv4-unicast": {"soft_reconfiguration": {"inbound": {}}}})
def test_allowas_in_wraps_under_number(self):
result = _neighbor_af_to_device([{"afi": "ipv4", "allowas_in": 3}])
self.assertEqual(result, {"ipv4-unicast": {"allowas_in": {"number": 3}}})
def test_capability_orf_value_becomes_dict_key(self):
result = _neighbor_af_to_device([{"afi": "ipv4", "capability": {"orf": "send"}}])
self.assertEqual(
result,
{"ipv4-unicast": {"capability": {"orf": {"prefix-list": {"send": {}}}}}},
)
def test_generic_options_pass_through_autoclean_only(self):
result = _neighbor_af_to_device(
[
{
"afi": "ipv4",
"nexthop_self": True,
"weight": 50,
"route_map": {"import": "RM-IN"},
"distribute_list": {"import": 10, "export": 20},
"attribute_unchanged": {"as_path": True, "next_hop": False},
},
],
)
self.assertEqual(
result,
{
"ipv4-unicast": {
"nexthop_self": {},
"weight": 50,
"route_map": {"import": "RM-IN"},
"distribute_list": {"import": 10, "export": 20},
"attribute_unchanged": {"as_path": {}},
},
},
)
def test_no_options_is_bare_presence(self):
self.assertEqual(_neighbor_af_to_device([{"afi": "ipv4"}]), {"ipv4-unicast": {}})
class TestNeighborAfFromDevice(unittest.TestCase):
def test_soft_reconfiguration_from_nested_inbound(self):
result = _neighbor_af_from_device(
{"ipv4-unicast": {"soft-reconfiguration": {"inbound": {}}}},
)
self.assertEqual(result, [{"afi": "ipv4", "soft_reconfiguration": True}])
def test_allowas_in_from_number_wrapper(self):
result = _neighbor_af_from_device({"ipv4-unicast": {"allowas-in": {"number": "3"}}})
self.assertEqual(result, [{"afi": "ipv4", "allowas_in": 3}])
def test_allowas_in_bare_presence_defaults_to_one(self):
result = _neighbor_af_from_device({"ipv4-unicast": {"allowas-in": {}}})
self.assertEqual(result, [{"afi": "ipv4", "allowas_in": 1}])
def test_capability_orf_receive_and_send(self):
r1 = _neighbor_af_from_device(
{"ipv4-unicast": {"capability": {"orf": {"prefix-list": {"receive": {}}}}}},
)
self.assertEqual(r1[0]["capability"], {"orf": "receive"})
r2 = _neighbor_af_from_device(
{"ipv4-unicast": {"capability": {"orf": {"prefix-list": {"send": {}}}}}},
)
self.assertEqual(r2[0]["capability"], {"orf": "send"})
def test_ints_cast_via_argspec_not_hardcoded_list(self):
result = _neighbor_af_from_device(
{
"ipv4-unicast": {
"maximum-prefix": "100",
"weight": "50",
"distribute-list": {"import": "10", "export": "20"},
},
},
)
entry = result[0]
self.assertEqual(entry["maximum_prefix"], 100)
self.assertEqual(entry["weight"], 50)
self.assertEqual(entry["distribute_list"], {"import": 10, "export": 20})
class TestGlobalAfToDeviceFromDevice(unittest.TestCase):
def test_networks_keyed_by_prefix(self):
result = _global_af_to_device(
[{"afi": "ipv4", "networks": [{"prefix": "192.0.2.0/24", "backdoor": True}]}],
)
self.assertEqual(
result,
{"ipv4-unicast": {"network": {"192.0.2.0/24": {"backdoor": {}}}}},
)
def test_redistribute_keyed_by_protocol(self):
result = _global_af_to_device(
[{"afi": "ipv4", "redistribute": [{"protocol": "connected", "metric": 10}]}],
)
self.assertEqual(
result,
{"ipv4-unicast": {"redistribute": {"connected": {"metric": 10}}}},
)
def test_from_device_metric_cast_via_argspec(self):
result = _global_af_from_device(
{"ipv4-unicast": {"redistribute": {"connected": {"metric": "10"}}}},
)
self.assertEqual(result[0]["redistribute"], [{"protocol": "connected", "metric": 10}])
class TestDeviceToArgspecFixture(VyOSModuleTestCase):
def test_as_number(self):
self.assertEqual(_device_to_argspec(self.fixture)["as_number"], 65000)
def test_global_networks_and_redistribute(self):
af = _device_to_argspec(self.fixture)["address_family"][0]
prefixes = {n["prefix"]: n for n in af["networks"]}
self.assertEqual(
prefixes["192.0.3.0/24"],
{"prefix": "192.0.3.0/24", "route_map": "RM-OUT", "backdoor": True},
)
protocols = {r["protocol"]: r for r in af["redistribute"]}
self.assertEqual(protocols["connected"]["metric"], 10)
def test_neighbor_wired_options(self):
nb = _device_to_argspec(self.fixture)["neighbors"][0]
ipv4 = next(af for af in nb["address_family"] if af["afi"] == "ipv4")
self.assertTrue(ipv4["nexthop_self"])
self.assertTrue(ipv4["soft_reconfiguration"])
self.assertEqual(ipv4["attribute_unchanged"], {"as_path": True, "med": True})
self.assertEqual(ipv4["capability"], {"orf": "receive"})
self.assertEqual(ipv4["distribute_list"], {"import": 10, "export": 20})
def test_empty_config(self):
self.assertEqual(_device_to_argspec({}), {})
self.assertEqual(_device_to_argspec(None), {})
class TestBuildCommands(VyOSModuleTestCase):
"""End-to-end, exactly as main() calls it."""
def test_merged_idempotent_against_own_fixture(self):
have = _device_to_argspec(self.fixture)
self.assertEqual(build_commands(have, self.fixture, "merged"), [])
def test_replaced_idempotent_against_own_fixture(self):
have = _device_to_argspec(self.fixture)
self.assertEqual(build_commands(have, self.fixture, "replaced"), [])
def test_merged_new_option(self):
have = _device_to_argspec(self.fixture)
have["neighbors"][0]["address_family"][0]["weight"] = 200
cmds = build_commands(have, self.fixture, "merged")
self.assertIn(
(
"set",
_BASE
+ ["neighbor", "192.0.2.1", "address-family", "ipv4-unicast", "weight", "200"],
),
cmds,
)
def test_replaced_never_touches_neighbor_siblings(self):
"""Regression test: dict_op is scoped strictly to each neighbor's
address-family subtree, never the whole neighbor.<addr> entry, so
fields owned by other modules (remote-as, timers, ...) are safe."""
cmds = build_commands({"as_number": 65000}, self.fixture, "replaced")
self.assertTrue(all("remote-as" not in c[1] for c in cmds))
self.assertIn(
("delete", _BASE + ["neighbor", "192.0.2.1", "address-family", "ipv4-unicast"]),
cmds,
)
self.assertIn(
("delete", _BASE + ["neighbor", "192.0.2.1", "address-family", "ipv6-unicast"]),
cmds,
)
def test_deleted_scoped_to_address_family_only(self):
cmds = build_commands({}, self.fixture, "deleted")
self.assertIn(("delete", _BASE + ["address-family"]), cmds)
self.assertIn(
("delete", _BASE + ["neighbor", "192.0.2.1", "address-family"]),
cmds,
)
self.assertTrue(all(c[1] != _BASE + ["neighbor", "192.0.2.1"] for c in cmds))
def test_normalize_have_prevents_char_iteration_bug(self):
"""A single-child tag node collapsed to a bare string by the
device must not be iterated character-by-character."""
raw_have = {"address-family": {"ipv4-unicast": {"network": "192.0.2.0/24"}}}
config = {
"as_number": 65000,
"address_family": [{"afi": "ipv4", "networks": [{"prefix": "192.0.2.0/24"}]}],
}
self.assertEqual(build_commands(config, raw_have, "merged"), [])
def test_fresh_merged_add(self):
config = {
"as_number": 65000,
"neighbors": [
{
"neighbor_address": "10.0.0.1",
"address_family": [{"afi": "ipv4", "weight": 200}],
},
],
}
cmds = build_commands(config, {}, "merged")
self.assertIn(
(
"set",
_BASE + ["neighbor", "10.0.0.1", "address-family", "ipv4-unicast", "weight", "200"],
),
cmds,
)
if __name__ == "__main__":
unittest.main()
|