|
| 1 | +#!/usr/bin/env python |
| 2 | +# SPDX-License-Identifier: ISC |
| 3 | + |
| 4 | +# |
| 5 | +# test_pim_cand_rp_bsr.py |
| 6 | +# |
| 7 | +# Copyright (c) 2024 ATCorp |
| 8 | +# Jafar Al-Gharaibeh |
| 9 | +# |
| 10 | + |
| 11 | +import os |
| 12 | +import sys |
| 13 | +import pytest |
| 14 | +import json |
| 15 | +from functools import partial |
| 16 | + |
| 17 | +# pylint: disable=C0413 |
| 18 | +# Import topogen and topotest helpers |
| 19 | +from lib import topotest |
| 20 | +from lib.topogen import Topogen, get_topogen |
| 21 | +from lib.topolog import logger |
| 22 | +from lib.pim import verify_pim_rp_info |
| 23 | +from lib.common_config import step, write_test_header, retry |
| 24 | + |
| 25 | +from time import sleep |
| 26 | + |
| 27 | +""" |
| 28 | +test_pim_cand_rp_bsr.py: Test candidate RP/BSR functionality |
| 29 | +""" |
| 30 | + |
| 31 | +TOPOLOGY = """ |
| 32 | + Candidate RP/BSR functionality |
| 33 | +
|
| 34 | + +---+---+ +---+---+ |
| 35 | + | C-BSR | 10.0.0.0/24 | C-BSR | |
| 36 | + + R1 + <------------------> + R2 | |
| 37 | + | | .1 .2 | | |
| 38 | + +---+---+ ---------- +---+---+ |
| 39 | + .1 | | 10.0.2.0/24 | .2 |
| 40 | + | 10.0.1.0/24 ----- | |
| 41 | + .3 | | .4 | .4 |
| 42 | + +---+---+ --------+---+---+ |
| 43 | + | C-RP | 10.0.3.0/24 | C-RP | |
| 44 | + + R3 + <------------------> + R4 | |
| 45 | + | | .3 .4 | | |
| 46 | + +---+---+ +---+---+ |
| 47 | + .3 | | .4 |
| 48 | + | 10.0.4.0/24 10.0.5.0/24 | |
| 49 | + .5 | | .6 |
| 50 | + +---+---+ +---+---+ |
| 51 | + | | 10.0.6.0/24 | | |
| 52 | + + R5 + <------------------> + R6 | |
| 53 | + | | .5 .6 | | |
| 54 | + +---+---+ +---+---+ |
| 55 | +""" |
| 56 | + |
| 57 | +# Save the Current Working Directory to find configuration files. |
| 58 | +CWD = os.path.dirname(os.path.realpath(__file__)) |
| 59 | +sys.path.append(os.path.join(CWD, "../")) |
| 60 | + |
| 61 | +# Required to instantiate the topology builder class. |
| 62 | +pytestmark = [pytest.mark.pimd] |
| 63 | + |
| 64 | + |
| 65 | +def build_topo(tgen): |
| 66 | + "Build function" |
| 67 | + |
| 68 | + # Create 6 routers |
| 69 | + for rn in range(1, 7): |
| 70 | + tgen.add_router("r{}".format(rn)) |
| 71 | + |
| 72 | + # Create 7 switches and connect routers |
| 73 | + sw = tgen.add_switch("s1") |
| 74 | + sw.add_link(tgen.gears["r1"]) |
| 75 | + sw.add_link(tgen.gears["r2"]) |
| 76 | + |
| 77 | + sw = tgen.add_switch("s2") |
| 78 | + sw.add_link(tgen.gears["r1"]) |
| 79 | + sw.add_link(tgen.gears["r3"]) |
| 80 | + |
| 81 | + sw = tgen.add_switch("s3") |
| 82 | + sw.add_link(tgen.gears["r2"]) |
| 83 | + sw.add_link(tgen.gears["r4"]) |
| 84 | + |
| 85 | + sw = tgen.add_switch("s4") |
| 86 | + sw.add_link(tgen.gears["r3"]) |
| 87 | + sw.add_link(tgen.gears["r4"]) |
| 88 | + |
| 89 | + sw = tgen.add_switch("s5") |
| 90 | + sw.add_link(tgen.gears["r3"]) |
| 91 | + sw.add_link(tgen.gears["r5"]) |
| 92 | + |
| 93 | + sw = tgen.add_switch("s6") |
| 94 | + sw.add_link(tgen.gears["r4"]) |
| 95 | + sw.add_link(tgen.gears["r6"]) |
| 96 | + |
| 97 | + sw = tgen.add_switch("s7") |
| 98 | + sw.add_link(tgen.gears["r5"]) |
| 99 | + sw.add_link(tgen.gears["r6"]) |
| 100 | + |
| 101 | +def setup_module(mod): |
| 102 | + logger.info("PIM Candidate RP/BSR:\n {}".format(TOPOLOGY)) |
| 103 | + |
| 104 | + tgen = Topogen(build_topo, mod.__name__) |
| 105 | + tgen.start_topology() |
| 106 | + |
| 107 | + router_list = tgen.routers() |
| 108 | + for rname, router in router_list.items(): |
| 109 | + logger.info("Loading router %s" % rname) |
| 110 | + router.load_frr_config(os.path.join(CWD, "{}/frr.conf".format(rname))) |
| 111 | + |
| 112 | + # Initialize all routers. |
| 113 | + tgen.start_router() |
| 114 | + for router in router_list.values(): |
| 115 | + if router.has_version("<", "4.0"): |
| 116 | + tgen.set_error("unsupported version") |
| 117 | + |
| 118 | + |
| 119 | +def teardown_module(mod): |
| 120 | + "Teardown the pytest environment" |
| 121 | + tgen = get_topogen() |
| 122 | + tgen.stop_topology() |
| 123 | + |
| 124 | +def test_pim_bsr_election(request): |
| 125 | + "Test PIM BSR Election" |
| 126 | + tgen = get_topogen() |
| 127 | + tc_name = request.node.name |
| 128 | + write_test_header(tc_name) |
| 129 | + |
| 130 | + if tgen.routers_have_failure(): |
| 131 | + pytest.skip("skipped because of router(s) failure") |
| 132 | + |
| 133 | + r2 = tgen.gears["r2"] |
| 134 | + |
| 135 | + expected = { |
| 136 | + "bsr":"10.0.0.1", |
| 137 | + "priority":200, |
| 138 | + "state":"ACCEPT_PREFERRED", |
| 139 | + } |
| 140 | + |
| 141 | + test_func = partial( |
| 142 | + topotest.router_json_cmp, r2, "show ip pim bsr json", expected |
| 143 | + ) |
| 144 | + _, result = topotest.run_and_expect(test_func, None, count=60, wait=1) |
| 145 | + |
| 146 | + assertmsg = "r2: bsr election mismatch " |
| 147 | + assert result is None, assertmsg |
| 148 | + |
| 149 | +def test_pim_bsr_cand_bsr(request): |
| 150 | + "Test PIM BSR candidate BSR" |
| 151 | + tgen = get_topogen() |
| 152 | + tc_name = request.node.name |
| 153 | + write_test_header(tc_name) |
| 154 | + |
| 155 | + if tgen.routers_have_failure(): |
| 156 | + pytest.skip("skipped because of router(s) failure") |
| 157 | + |
| 158 | + r2 = tgen.gears["r2"] |
| 159 | + |
| 160 | + # r2 is a candidate bsr with low priority: elected = False |
| 161 | + expected = { |
| 162 | + "address": "10.0.0.2", |
| 163 | + "priority": 100, |
| 164 | + "elected": False |
| 165 | + } |
| 166 | + test_func = partial( |
| 167 | + topotest.router_json_cmp, r2, "show ip pim bsr candidate-bsr json", expected |
| 168 | + ) |
| 169 | + _, result = topotest.run_and_expect(test_func, None, count=60, wait=1) |
| 170 | + |
| 171 | + assertmsg = "r2: candidate bsr mismatch " |
| 172 | + assert result is None, assertmsg |
| 173 | + |
| 174 | +def test_pim_bsr_cand_rp(request): |
| 175 | + "Test PIM BSR candidate RP" |
| 176 | + tgen = get_topogen() |
| 177 | + tc_name = request.node.name |
| 178 | + write_test_header(tc_name) |
| 179 | + |
| 180 | + if tgen.routers_have_failure(): |
| 181 | + pytest.skip("skipped because of router(s) failure") |
| 182 | + |
| 183 | + r3 = tgen.gears["r3"] |
| 184 | + |
| 185 | + # r3 is a candidate rp |
| 186 | + expected = { |
| 187 | + "address":"10.0.3.3", |
| 188 | + "priority":10 |
| 189 | + } |
| 190 | + test_func = partial( |
| 191 | + topotest.router_json_cmp, r3, "show ip pim bsr candidate-rp json", expected |
| 192 | + ) |
| 193 | + _, result = topotest.run_and_expect(test_func, None, count=60, wait=1) |
| 194 | + |
| 195 | + assertmsg = "r3: bsr candidate rp mismatch " |
| 196 | + assert result is None, assertmsg |
| 197 | + |
| 198 | + |
| 199 | +def test_pim_bsr_rp_info(request): |
| 200 | + "Test RP info state on r5" |
| 201 | + tgen = get_topogen() |
| 202 | + tc_name = request.node.name |
| 203 | + write_test_header(tc_name) |
| 204 | + |
| 205 | + if tgen.routers_have_failure(): |
| 206 | + pytest.skip("skipped because of router(s) failure") |
| 207 | + |
| 208 | + # At this point, all nodes, including r5 should have synced the RP state |
| 209 | + step("Verify rp-info from BSR") |
| 210 | + result = verify_pim_rp_info(tgen, None, "r5", "239.0.0.0/16", "r5-eth0", "10.0.3.3", |
| 211 | + "BSR", False, "ipv4", True, retry_timeout = 90) |
| 212 | + assert result is True, "Testcase {} :Failed \n Error: {}".format(tc_name, result) |
| 213 | + |
| 214 | + result = verify_pim_rp_info(tgen, None, "r5", "239.0.0.0/8", "r5-eth0", "10.0.3.4", |
| 215 | + "BSR", False, "ipv4", True, retry_timeout = 30) |
| 216 | + assert result is True, "Testcase {} :Failed \n Error: {}".format(tc_name, result) |
| 217 | + |
| 218 | + result = verify_pim_rp_info(tgen, None, "r5", "239.0.0.0/24", "r5-eth0", "10.0.3.4", |
| 219 | + "BSR", False, "ipv4", True, retry_timeout = 30) |
| 220 | + assert result is True, "Testcase {} :Failed \n Error: {}".format(tc_name, result) |
| 221 | + |
| 222 | + |
| 223 | +def test_memory_leak(): |
| 224 | + "Run the memory leak test and report results." |
| 225 | + tgen = get_topogen() |
| 226 | + if not tgen.is_memleak_enabled(): |
| 227 | + pytest.skip("Memory leak test/report is disabled") |
| 228 | + |
| 229 | + tgen.report_memory_leaks() |
| 230 | + |
| 231 | + |
| 232 | +if __name__ == "__main__": |
| 233 | + args = ["-s"] + sys.argv[1:] |
| 234 | + sys.exit(pytest.main(args)) |
0 commit comments