summaryrefslogtreecommitdiff
path: root/tests/unit/modules/test_vyos_vrf.py
blob: 7906f371026c0262f2ea31f029089aedc3800547 (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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function


__metaclass__ = type

import unittest

from ansible_collections.vyos.rest.plugins.modules.vyos_vrf import (
    _device_to_argspec,
    _proto_from_device,
    _proto_to_device,
    _protocols_from_device,
    build_commands,
)

from .base import load_fixture


# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------

_RAW_HAVE = load_fixture("vrf_running.json")

# ---------------------------------------------------------------------------
# _device_to_argspec — top-level VRF properties
# ---------------------------------------------------------------------------


class TestDeviceToArgspec(unittest.TestCase):
    def setUp(self):
        self.result = _device_to_argspec(_RAW_HAVE)

    def test_bind_to_all(self):
        self.assertTrue(self.result["bind_to_all"])

    def test_instances_count(self):
        self.assertEqual(len(self.result["instances"]), 2)

    def test_vrf1_properties(self):
        vrf1 = next(i for i in self.result["instances"] if i["name"] == "vrf1")
        self.assertEqual(vrf1["description"], "red")
        self.assertTrue(vrf1["disable"])
        self.assertEqual(vrf1["table_id"], 101)
        self.assertEqual(vrf1["vni"], 501)

    def test_vrf2_address_family(self):
        vrf2 = next(i for i in self.result["instances"] if i["name"] == "vrf2")
        afis = {af["afi"]: af for af in vrf2["address_family"]}
        self.assertIn("ipv4", afis)
        self.assertTrue(afis["ipv4"]["disable_forwarding"])
        self.assertTrue(afis["ipv4"]["nht_no_resolve_via_default"])
        self.assertIn("ipv6", afis)
        self.assertTrue(afis["ipv6"]["disable_forwarding"])
        self.assertTrue(afis["ipv6"]["nht_no_resolve_via_default"])

    def test_empty_input(self):
        self.assertEqual(_device_to_argspec({}), {})
        self.assertEqual(_device_to_argspec(None), {})


# ---------------------------------------------------------------------------
# _proto_from_device — BGP
# ---------------------------------------------------------------------------


class TestBgpFromDevice(unittest.TestCase):
    def setUp(self):
        self.raw = _RAW_HAVE["name"]["vrf1"]["protocols"]["bgp"]
        self.result = _proto_from_device(self.raw, "bgp")

    def test_system_as(self):
        self.assertEqual(self.result["system_as"], 65001)

    def test_neighbor_list(self):
        self.assertEqual(len(self.result["neighbor"]), 1)
        n = self.result["neighbor"][0]
        self.assertEqual(n["address"], "10.0.0.1")
        self.assertEqual(n["remote_as"], 65002)
        self.assertEqual(n["description"], "peer1")

    def test_empty_input(self):
        self.assertEqual(_proto_from_device({}, "bgp"), {})
        self.assertEqual(_proto_from_device(None, "bgp"), {})


# ---------------------------------------------------------------------------
# _proto_to_device + build_commands — BGP
# ---------------------------------------------------------------------------


class TestBgpToDevice(unittest.TestCase):
    def test_system_as_to_device(self):
        result = _proto_to_device({"system_as": 65001}, "bgp")
        self.assertIn("system-as", result)
        self.assertEqual(result["system-as"], 65001)

    def test_neighbor_to_device(self):
        result = _proto_to_device(
            {
                "system_as": 65001,
                "neighbor": [{"address": "10.0.0.1", "remote_as": 65002}],
            },
            "bgp",
        )
        self.assertIn("neighbor", result)
        self.assertIn("10.0.0.1", result["neighbor"])
        self.assertEqual(result["neighbor"]["10.0.0.1"]["remote-as"], 65002)

    def test_idempotent(self):
        want = _proto_from_device(_RAW_HAVE["name"]["vrf1"]["protocols"]["bgp"], "bgp")
        cmds = build_commands(
            {"instances": [{"name": "vrf1", "table_id": 101, "protocols": {"bgp": want}}]},
            _RAW_HAVE,
            "merged",
        )
        bgp_cmds = [c for c in cmds if "bgp" in str(c)]
        self.assertEqual(bgp_cmds, [])

    def test_add_neighbor(self):
        want_bgp = {
            "system_as": 65001,
            "neighbor": [
                {"address": "10.0.0.1", "remote_as": 65002, "description": "peer1"},
                {"address": "10.0.0.2", "remote_as": 65003},
            ],
        }
        cmds = build_commands(
            {"instances": [{"name": "vrf1", "table_id": 101, "protocols": {"bgp": want_bgp}}]},
            _RAW_HAVE,
            "merged",
        )
        paths = [c[1] for c in cmds]
        self.assertIn(
            [
                "vrf",
                "name",
                "vrf1",
                "protocols",
                "bgp",
                "neighbor",
                "10.0.0.2",
                "remote-as",
                "65003",
            ],
            paths,
        )
        # existing neighbor should not be re-set
        self.assertNotIn(
            [
                "vrf",
                "name",
                "vrf1",
                "protocols",
                "bgp",
                "neighbor",
                "10.0.0.1",
                "remote-as",
                "65002",
            ],
            paths,
        )


# ---------------------------------------------------------------------------
# _proto_from_device — OSPFv2
# ---------------------------------------------------------------------------


class TestOspfFromDevice(unittest.TestCase):
    def setUp(self):
        self.raw = _RAW_HAVE["name"]["vrf1"]["protocols"]["ospf"]
        self.result = _proto_from_device(self.raw, "ospf")

    def test_areas(self):
        self.assertEqual(len(self.result["areas"]), 1)
        area = self.result["areas"][0]
        self.assertEqual(area["area_id"], "0")
        self.assertIn("10.0.0.0/24", area["networks"])
        self.assertIn("172.16.0.0/24", area["networks"])

    def test_parameters(self):
        self.assertEqual(self.result["parameters"]["router_id"], "10.0.0.1")

    def test_empty_input(self):
        self.assertEqual(_proto_from_device({}, "ospf"), {})


# ---------------------------------------------------------------------------
# build_commands — OSPFv2
# ---------------------------------------------------------------------------


class TestOspfBuildCommands(unittest.TestCase):
    def test_idempotent(self):
        want = _proto_from_device(_RAW_HAVE["name"]["vrf1"]["protocols"]["ospf"], "ospf")
        cmds = build_commands(
            {"instances": [{"name": "vrf1", "table_id": 101, "protocols": {"ospf": want}}]},
            _RAW_HAVE,
            "merged",
        )
        ospf_cmds = [c for c in cmds if "ospf" in str(c)]
        self.assertEqual(ospf_cmds, [])

    def test_add_network(self):
        want_ospf = {
            "areas": [
                {"area_id": "0", "networks": ["10.0.0.0/24", "172.16.0.0/24", "192.168.0.0/24"]},
            ],
        }
        cmds = build_commands(
            {"instances": [{"name": "vrf1", "table_id": 101, "protocols": {"ospf": want_ospf}}]},
            _RAW_HAVE,
            "merged",
        )
        paths = [c[1] for c in cmds]
        self.assertIn(
            ["vrf", "name", "vrf1", "protocols", "ospf", "area", "0", "network", "192.168.0.0/24"],
            paths,
        )

    def test_add_area(self):
        want_ospf = {
            "areas": [
                {"area_id": "0", "networks": ["10.0.0.0/24", "172.16.0.0/24"]},
                {"area_id": "1", "networks": ["10.1.0.0/24"]},
            ],
        }
        cmds = build_commands(
            {"instances": [{"name": "vrf1", "table_id": 101, "protocols": {"ospf": want_ospf}}]},
            _RAW_HAVE,
            "merged",
        )
        paths = [c[1] for c in cmds]
        self.assertIn(
            ["vrf", "name", "vrf1", "protocols", "ospf", "area", "1", "network", "10.1.0.0/24"],
            paths,
        )


# ---------------------------------------------------------------------------
# _proto_from_device — static routes
# ---------------------------------------------------------------------------


class TestStaticFromDevice(unittest.TestCase):
    def setUp(self):
        self.raw = _RAW_HAVE["name"]["vrf1"]["protocols"]["static"]
        self.result = _proto_from_device(self.raw, "static")

    def test_routes(self):
        self.assertEqual(len(self.result["routes"]), 1)
        route = self.result["routes"][0]
        self.assertEqual(route["dest"], "192.168.10.0/24")
        self.assertIn("10.0.0.254", route["next_hops"])

    def test_empty_input(self):
        self.assertEqual(_proto_from_device({}, "static"), {})


# ---------------------------------------------------------------------------
# build_commands — static routes
# ---------------------------------------------------------------------------


class TestStaticBuildCommands(unittest.TestCase):
    def test_idempotent(self):
        want = _proto_from_device(_RAW_HAVE["name"]["vrf1"]["protocols"]["static"], "static")
        cmds = build_commands(
            {"instances": [{"name": "vrf1", "table_id": 101, "protocols": {"static": want}}]},
            _RAW_HAVE,
            "merged",
        )
        static_cmds = [c for c in cmds if "static" in str(c)]
        self.assertEqual(static_cmds, [])

    def test_add_route(self):
        want_static = {
            "routes": [
                {"dest": "192.168.10.0/24", "next_hops": ["10.0.0.254"]},
                {"dest": "192.168.20.0/24", "next_hops": ["10.0.0.254"]},
            ],
        }
        cmds = build_commands(
            {
                "instances": [
                    {"name": "vrf1", "table_id": 101, "protocols": {"static": want_static}},
                ],
            },
            _RAW_HAVE,
            "merged",
        )
        paths = [c[1] for c in cmds]
        self.assertIn(
            [
                "vrf",
                "name",
                "vrf1",
                "protocols",
                "static",
                "route",
                "192.168.20.0/24",
                "next-hop",
                "10.0.0.254",
            ],
            paths,
        )


# ---------------------------------------------------------------------------
# _protocols_from_device
# ---------------------------------------------------------------------------


class TestProtocolsFromDevice(unittest.TestCase):
    def test_all_protocols(self):
        raw_vrf = _RAW_HAVE["name"]["vrf1"]
        result = _protocols_from_device(raw_vrf)
        self.assertIn("bgp", result)
        self.assertIn("ospf", result)
        self.assertIn("static", result)

    def test_no_protocols(self):
        raw_vrf = _RAW_HAVE["name"]["vrf2"]
        result = _protocols_from_device(raw_vrf)
        self.assertIsNone(result)


# ---------------------------------------------------------------------------
# build_commands — top-level VRF operations
# ---------------------------------------------------------------------------


class TestBuildCommands(unittest.TestCase):
    def test_merged_new_vrf(self):
        config = {"instances": [{"name": "vrf3", "table_id": 200, "vni": 2000}]}
        cmds = build_commands(config, _RAW_HAVE, "merged")
        paths = [c[1] for c in cmds]
        self.assertIn(["vrf", "name", "vrf3", "table", "200"], paths)
        self.assertIn(["vrf", "name", "vrf3", "vni", "2000"], paths)

    def test_merged_idempotent(self):
        config = {
            "bind_to_all": True,
            "instances": [
                {"name": "vrf1", "description": "red", "table_id": 101, "vni": 501},
            ],
        }
        cmds = build_commands(config, _RAW_HAVE, "merged")
        self.assertEqual(cmds, [])

    def test_deleted_specific_vrf(self):
        config = {"instances": [{"name": "vrf1"}]}
        cmds = build_commands(config, _RAW_HAVE, "deleted")
        self.assertIn(("delete", ["vrf", "name", "vrf1"]), cmds)
        self.assertNotIn(("delete", ["vrf", "name", "vrf2"]), cmds)

    def test_deleted_all(self):
        cmds = build_commands({}, _RAW_HAVE, "deleted")
        self.assertIn(("delete", ["vrf"]), cmds)

    def test_overridden_removes_extra_vrf(self):
        config = {"instances": [{"name": "vrf1", "table_id": 101}]}
        cmds = build_commands(config, _RAW_HAVE, "overridden")
        paths = [c[1] for c in cmds]
        self.assertIn(["vrf", "name", "vrf2"], paths)

    def test_merged_does_not_delete_unreferenced_vrf(self):
        config = {"instances": [{"name": "vrf1", "table_id": 101}]}
        cmds = build_commands(config, _RAW_HAVE, "merged")
        paths = [c[1] for c in cmds]
        self.assertNotIn(["vrf", "name", "vrf2"], paths)


if __name__ == "__main__":
    unittest.main()