summaryrefslogtreecommitdiffstats
path: root/tests/topotests/ospf_netns_vrf/test_ospf_netns_vrf.py
blob: 2716f63348c802e5cd9d0e42a2a8a773200caf78 (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
#!/usr/bin/env python
# SPDX-License-Identifier: ISC

#
# test_ospf_netns_vrf.py
# Part of NetDEF Topology Tests
#
# Copyright (c) 2017 by
# Network Device Education Foundation, Inc. ("NetDEF")
#

"""
test_ospf_netns_vrf.py: Test OSPF with Network Namespace VRFs.
"""

import os
import sys
from functools import partial
import pytest

# Save the Current Working Directory to find configuration files.
CWD = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(CWD, "../"))

# pylint: disable=C0413
# Import topogen and topotest helpers
from lib import topotest
from lib.topogen import Topogen, TopoRouter, get_topogen
from lib.topolog import logger

# Required to instantiate the topology builder class.

pytestmark = [pytest.mark.ospfd]


def build_topo(tgen):
    "Build function"

    # Create 3 routers
    for routern in range(1, 4):
        tgen.add_router("r{}".format(routern))

    # Create a empty network for router 1
    switch = tgen.add_switch("s1")
    switch.add_link(tgen.gears["r1"])

    # Create a empty network for router 2
    switch = tgen.add_switch("s2")
    switch.add_link(tgen.gears["r2"])

    # Interconect router 1, 2 and 3
    switch = tgen.add_switch("s3")
    switch.add_link(tgen.gears["r1"])
    switch.add_link(tgen.gears["r2"])
    switch.add_link(tgen.gears["r3"])

    # Create empty netowrk for router3
    switch = tgen.add_switch("s4")
    switch.add_link(tgen.gears["r3"])


def setup_module(mod):
    "Sets up the pytest environment"
    tgen = Topogen(build_topo, mod.__name__)
    tgen.start_topology()

    router_list = tgen.routers()

    # check for zebra capability
    for rname, router in router_list.items():
        if router.check_capability(TopoRouter.RD_ZEBRA, "--vrfwnetns") == False:
            return pytest.skip(
                "Skipping OSPF VRF NETNS feature. VRF NETNS backend not available on FRR"
            )

    if os.system("ip netns list") != 0:
        return pytest.skip(
            "Skipping OSPF VRF NETNS Test. NETNS not available on System"
        )

    logger.info("Testing with VRF Namespace support")

    for rname, router in router_list.items():
        # create VRF rx-ospf-cust1 and link rx-eth{0,1} to rx-ospf-cust1
        ns = "{}-ospf-cust1".format(rname)
        router.net.add_netns(ns)
        router.net.set_intf_netns(rname + "-eth0", ns, up=True)
        router.net.set_intf_netns(rname + "-eth1", ns, up=True)

        router.load_config(
            TopoRouter.RD_ZEBRA,
            os.path.join(CWD, "{}/zebra.conf".format(rname)),
            "--vrfwnetns",
        )
        router.load_config(
            TopoRouter.RD_OSPF, os.path.join(CWD, "{}/ospfd.conf".format(rname))
        )

    # Initialize all routers.
    tgen.start_router()
    for router in router_list.values():
        if router.has_version("<", "4.0"):
            tgen.set_error("unsupported version")


def teardown_module(mod):
    "Teardown the pytest environment"
    tgen = get_topogen()

    # Move interfaces out of vrf namespace and delete the namespace
    router_list = tgen.routers()
    for rname, router in router_list.items():
        tgen.net[rname].reset_intf_netns(rname + "-eth0")
        tgen.net[rname].reset_intf_netns(rname + "-eth1")
        tgen.net[rname].delete_netns(rname + "-ospf-cust1")
    tgen.stop_topology()


# Shared test function to validate expected output.
def compare_show_ip_route_vrf(rname, expected):
    """
    Calls 'show ip ospf vrf [rname]-ospf-cust1 route' for router `rname` and compare the obtained
    result with the expected output.
    """
    tgen = get_topogen()
    vrf_name = "{0}-ospf-cust1".format(rname)
    current = topotest.ip4_route_zebra(tgen.gears[rname], vrf_name)
    ret = topotest.difflines(
        current, expected, title1="Current output", title2="Expected output"
    )
    return ret


def test_ospf_convergence():
    "Test OSPF daemon convergence"
    tgen = get_topogen()

    if tgen.routers_have_failure():
        pytest.skip("skipped because of router(s) failure")

    for rname, router in tgen.routers().items():
        logger.info('Waiting for router "%s" convergence', rname)

        # Load expected results from the command
        reffile = os.path.join(CWD, "{}/ospfroute.txt".format(rname))
        expected = open(reffile).read()

        # Run test function until we get an result. Wait at most 60 seconds.
        test_func = partial(
            topotest.router_output_cmp,
            router,
            "show ip ospf vrf {0}-ospf-cust1 route".format(rname),
            expected,
        )
        result, diff = topotest.run_and_expect(test_func, "", count=160, wait=0.5)
        assertmsg = "OSPF did not converge on {}:\n{}".format(rname, diff)
        assert result, assertmsg


def test_ospf_kernel_route():
    "Test OSPF kernel route installation"
    tgen = get_topogen()

    if tgen.routers_have_failure():
        pytest.skip("skipped because of router(s) failure")

    rlist = tgen.routers().values()
    for router in rlist:
        logger.info('Checking OSPF IPv4 kernel routes in "%s"', router.name)
        reffile = os.path.join(CWD, "{}/zebraroute.txt".format(router.name))
        expected = open(reffile).read()
        # Run test function until we get an result. Wait at most 60 seconds.
        test_func = partial(compare_show_ip_route_vrf, router.name, expected)
        result, diff = topotest.run_and_expect(test_func, "", count=140, wait=0.5)
        assertmsg = 'OSPF IPv4 route mismatch in router "{}": {}'.format(
            router.name, diff
        )
        assert result, assertmsg


def test_ospf_json():
    "Test 'show ip ospf json' output for coherency."
    tgen = get_topogen()

    if tgen.routers_have_failure():
        pytest.skip("skipped because of router(s) failure")

    for rname, router in tgen.routers().items():
        logger.info(
            'Comparing router "%s" "show ip ospf vrf %s-ospf-cust1 json" output',
            router.name,
            router.name,
        )
        expected = {
            "{}-ospf-cust1".format(router.name): {
                "vrfName": "{}-ospf-cust1".format(router.name),
                "routerId": "10.0.255.{}".format(rname[1:]),
                "tosRoutesOnly": True,
                "rfc2328Conform": True,
                "spfScheduleDelayMsecs": 0,
                "holdtimeMinMsecs": 50,
                "holdtimeMaxMsecs": 5000,
                "lsaMinIntervalMsecs": 5000,
                "lsaMinArrivalMsecs": 1000,
                "writeMultiplier": 20,
                "refreshTimerMsecs": 10000,
                "asbrRouter": "injectingExternalRoutingInformation",
                "attachedAreaCounter": 1,
                "areas": {},
            }
        }
        # Area specific additional checks
        if router.name == "r1" or router.name == "r2" or router.name == "r3":
            expected["{}-ospf-cust1".format(router.name)]["areas"]["0.0.0.0"] = {
                "areaIfActiveCounter": 2,
                "areaIfTotalCounter": 2,
                "authentication": "authenticationNone",
                "backbone": True,
                "lsaAsbrNumber": 0,
                "lsaNetworkNumber": 1,
                "lsaNssaNumber": 0,
                "lsaNumber": 4,
                "lsaOpaqueAreaNumber": 0,
                "lsaOpaqueLinkNumber": 0,
                "lsaRouterNumber": 3,
                "lsaSummaryNumber": 0,
                "nbrFullAdjacentCounter": 2,
            }

        test_func = partial(
            topotest.router_json_cmp,
            router,
            "show ip ospf vrf {0}-ospf-cust1 json".format(rname),
            expected,
        )
        _, diff = topotest.run_and_expect(test_func, None, count=10, wait=0.5)
        assertmsg = '"{}" JSON output mismatches'.format(rname)
        assert diff is None, assertmsg


def test_ospf_link_down():
    "Test OSPF convergence after a link goes down"
    tgen = get_topogen()

    if tgen.routers_have_failure():
        pytest.skip("skipped because of router(s) failure")

    # Simulate a network down event on router3 switch3 interface.
    router3 = tgen.gears["r3"]
    topotest.interface_set_status(
        router3, "r3-eth0", ifaceaction=False, vrf_name="r3-ospf-cust1"
    )

    # Expect convergence on all routers
    for rname, router in tgen.routers().items():
        logger.info('Waiting for router "%s" convergence after link failure', rname)
        # Load expected results from the command
        reffile = os.path.join(CWD, "{}/ospfroute_down.txt".format(rname))
        expected = open(reffile).read()

        # Run test function until we get an result. Wait at most 60 seconds.
        test_func = partial(
            topotest.router_output_cmp,
            router,
            "show ip ospf vrf {0}-ospf-cust1 route".format(rname),
            expected,
        )
        result, diff = topotest.run_and_expect(test_func, "", count=140, wait=0.5)
        assertmsg = "OSPF did not converge on {}:\n{}".format(rname, diff)
        assert result, assertmsg


def test_ospf_link_down_kernel_route():
    "Test OSPF kernel route installation"
    tgen = get_topogen()

    if tgen.routers_have_failure():
        pytest.skip("skipped because of router(s) failure")

    rlist = tgen.routers().values()
    for router in rlist:
        logger.info(
            'Checking OSPF IPv4 kernel routes in "%s" after link down', router.name
        )

        str = "{0}-ospf-cust1".format(router.name)
        reffile = os.path.join(CWD, "{}/zebraroutedown.txt".format(router.name))
        expected = open(reffile).read()
        # Run test function until we get an result. Wait at most 60 seconds.
        test_func = partial(compare_show_ip_route_vrf, router.name, expected)
        result, diff = topotest.run_and_expect(test_func, "", count=140, wait=0.5)
        assertmsg = (
            'OSPF IPv4 route mismatch in router "{}" after link down: {}'.format(
                router.name, diff
            )
        )
        assert result, assertmsg


def test_memory_leak():
    "Run the memory leak test and report results."
    tgen = get_topogen()
    if not tgen.is_memleak_enabled():
        pytest.skip("Memory leak test/report is disabled")

    tgen.report_memory_leaks()


if __name__ == "__main__":
    args = ["-s"] + sys.argv[1:]
    sys.exit(pytest.main(args))