]>
Commit | Line | Data |
---|---|---|
abd2a1ff DS |
1 | #!/usr/bin/env python |
2 | # | |
3 | # test_zebra_rib.py | |
4 | # | |
5 | # Copyright (c) 2019 by | |
6 | # Cumulus Networks, Inc | |
7 | # Donald Sharp | |
8 | # | |
9 | # Permission to use, copy, modify, and/or distribute this software | |
10 | # for any purpose with or without fee is hereby granted, provided | |
11 | # that the above copyright notice and this permission notice appear | |
12 | # in all copies. | |
13 | # | |
14 | # THE SOFTWARE IS PROVIDED "AS IS" AND NETDEF DISCLAIMS ALL WARRANTIES | |
15 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
16 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NETDEF BE LIABLE FOR | |
17 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY | |
18 | # DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
19 | # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
20 | # ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
21 | # OF THIS SOFTWARE. | |
22 | # | |
23 | ||
24 | """ | |
25 | test_zebra_rib.py: Test some basic zebra <-> kernel interactions | |
26 | """ | |
27 | ||
28 | import os | |
29 | import re | |
30 | import sys | |
31 | from functools import partial | |
32 | import pytest | |
33 | import json | |
34 | ||
35 | # Save the Current Working Directory to find configuration files. | |
36 | CWD = os.path.dirname(os.path.realpath(__file__)) | |
787e7624 | 37 | sys.path.append(os.path.join(CWD, "../")) |
abd2a1ff DS |
38 | |
39 | # pylint: disable=C0413 | |
40 | # Import topogen and topotest helpers | |
41 | from lib import topotest | |
42 | from lib.topogen import Topogen, TopoRouter, get_topogen | |
43 | from lib.topolog import logger | |
44 | ||
45 | # Required to instantiate the topology builder class. | |
46 | from mininet.topo import Topo | |
47 | ||
787e7624 | 48 | |
abd2a1ff DS |
49 | class ZebraTopo(Topo): |
50 | "Test topology builder" | |
787e7624 | 51 | |
abd2a1ff DS |
52 | def build(self, *_args, **_opts): |
53 | "Build function" | |
54 | tgen = get_topogen(self) | |
55 | ||
787e7624 | 56 | tgen.add_router("r1") |
abd2a1ff DS |
57 | |
58 | # Create a empty network for router 1 | |
787e7624 | 59 | switch = tgen.add_switch("s1") |
60 | switch.add_link(tgen.gears["r1"]) | |
61 | switch.add_link(tgen.gears["r1"]) | |
62 | switch.add_link(tgen.gears["r1"]) | |
63 | switch.add_link(tgen.gears["r1"]) | |
64 | switch.add_link(tgen.gears["r1"]) | |
65 | switch.add_link(tgen.gears["r1"]) | |
66 | switch.add_link(tgen.gears["r1"]) | |
67 | switch.add_link(tgen.gears["r1"]) | |
68 | ||
abd2a1ff DS |
69 | |
70 | def setup_module(mod): | |
71 | "Sets up the pytest environment" | |
72 | tgen = Topogen(ZebraTopo, mod.__name__) | |
73 | tgen.start_topology() | |
74 | ||
75 | router_list = tgen.routers() | |
e5f0ed14 | 76 | for rname, router in router_list.items(): |
abd2a1ff | 77 | router.load_config( |
787e7624 | 78 | TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) |
abd2a1ff DS |
79 | ) |
80 | ||
81 | # Initialize all routers. | |
82 | tgen.start_router() | |
83 | ||
787e7624 | 84 | |
abd2a1ff DS |
85 | def teardown_module(mod): |
86 | "Teardown the pytest environment" | |
87 | tgen = get_topogen() | |
88 | tgen.stop_topology() | |
89 | ||
787e7624 | 90 | |
abd2a1ff DS |
91 | def test_zebra_kernel_admin_distance(): |
92 | "Test some basic kernel routes added that should be accepted" | |
93 | logger.info("Test some basic kernel routes that should be accepted") | |
94 | tgen = get_topogen() | |
95 | if tgen.routers_have_failure(): | |
787e7624 | 96 | pytest.skip("skipped because of router(s) failure") |
abd2a1ff | 97 | |
787e7624 | 98 | r1 = tgen.gears["r1"] |
abd2a1ff DS |
99 | |
100 | # Route with 255/8192 metric | |
787e7624 | 101 | r1.run("ip route add 4.5.1.0/24 via 192.168.210.2 dev r1-eth0 metric 4278198272") |
abd2a1ff | 102 | # Route with 1/1 metric |
787e7624 | 103 | r1.run("ip route add 4.5.2.0/24 via 192.168.211.2 dev r1-eth1 metric 16777217") |
abd2a1ff | 104 | # Route with 10/1 metric |
787e7624 | 105 | r1.run("ip route add 4.5.3.0/24 via 192.168.212.2 dev r1-eth2 metric 167772161") |
abd2a1ff | 106 | # Same route with a 160/1 metric |
787e7624 | 107 | r1.run("ip route add 4.5.3.0/24 via 192.168.213.2 dev r1-eth3 metric 2684354561") |
abd2a1ff | 108 | |
787e7624 | 109 | # Currently I believe we have a bug here with the same route and different |
110 | # metric. That needs to be properly resolved. Making a note for | |
111 | # coming back around later and fixing this. | |
112 | # tgen.mininet_cli() | |
abd2a1ff | 113 | for i in range(1, 2): |
787e7624 | 114 | json_file = "{}/r1/v4_route_{}.json".format(CWD, i) |
abd2a1ff DS |
115 | expected = json.loads(open(json_file).read()) |
116 | ||
787e7624 | 117 | test_func = partial( |
118 | topotest.router_json_cmp, | |
119 | r1, | |
120 | "show ip route 4.5.{}.0 json".format(i), | |
121 | expected, | |
122 | ) | |
123 | _, result = topotest.run_and_expect(test_func, None, count=2, wait=0.5) | |
abd2a1ff DS |
124 | assertmsg = '"r1" JSON output mismatches' |
125 | assert result is None, assertmsg | |
787e7624 | 126 | # tgen.mininet_cli() |
127 | ||
abd2a1ff DS |
128 | |
129 | def test_zebra_kernel_override(): | |
130 | "Test that a FRR route with a lower admin distance takes over" | |
131 | logger.info("Test kernel override with a better admin distance") | |
132 | tgen = get_topogen() | |
787e7624 | 133 | if tgen.routers_have_failure(): |
abd2a1ff DS |
134 | ptyest.skip("skipped because of preview test failure") |
135 | ||
787e7624 | 136 | r1 = tgen.gears["r1"] |
abd2a1ff | 137 | r1.vtysh_cmd("conf\nip route 4.5.1.0/24 192.168.216.3") |
787e7624 | 138 | json_file = "{}/r1/v4_route_1_static_override.json".format(CWD) |
abd2a1ff DS |
139 | expected = json.loads(open(json_file).read()) |
140 | ||
787e7624 | 141 | test_func = partial( |
142 | topotest.router_json_cmp, r1, "show ip route 4.5.1.0 json", expected | |
143 | ) | |
144 | _, result = topotest.run_and_expect(test_func, None, count=2, wait=0.5) | |
abd2a1ff DS |
145 | assert result is None, '"r1" JSON output mismatches' |
146 | ||
787e7624 | 147 | logger.info( |
148 | "Test that the removal of the static route allows the kernel to take back over" | |
149 | ) | |
abd2a1ff | 150 | r1.vtysh_cmd("conf\nno ip route 4.5.1.0/24 192.168.216.3") |
787e7624 | 151 | json_file = "{}/r1/v4_route_1.json".format(CWD) |
abd2a1ff DS |
152 | expected = json.loads(open(json_file).read()) |
153 | ||
787e7624 | 154 | test_func = partial( |
155 | topotest.router_json_cmp, r1, "show ip route 4.5.1.0 json", expected | |
156 | ) | |
157 | _, result = topotest.run_and_expect(test_func, None, count=2, wait=0.5) | |
abd2a1ff DS |
158 | assert result is None, '"r1" JSON output mismatches' |
159 | ||
160 | ||
161 | def test_memory_leak(): | |
162 | "Run the memory leak test and report results." | |
163 | tgen = get_topogen() | |
164 | if not tgen.is_memleak_enabled(): | |
787e7624 | 165 | pytest.skip("Memory leak test/report is disabled") |
abd2a1ff DS |
166 | |
167 | tgen.report_memory_leaks() | |
168 | ||
787e7624 | 169 | |
170 | if __name__ == "__main__": | |
abd2a1ff DS |
171 | args = ["-s"] + sys.argv[1:] |
172 | sys.exit(pytest.main(args)) |