|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2021 Broadcom. The term Broadcom refers to Broadcom Inc. and/or |
| 4 | +# its subsidiaries. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +""" |
| 20 | +Test link-local address as a BGP peer over VRF. |
| 21 | +""" |
| 22 | + |
| 23 | +import os |
| 24 | +import sys |
| 25 | +import json |
| 26 | +import pytest |
| 27 | +import functools |
| 28 | + |
| 29 | +CWD = os.path.dirname(os.path.realpath(__file__)) |
| 30 | +sys.path.append(os.path.join(CWD, "../")) |
| 31 | + |
| 32 | +# pylint: disable=C0413 |
| 33 | +from lib import topotest |
| 34 | +from lib.topogen import Topogen, TopoRouter, get_topogen |
| 35 | +from lib.topolog import logger |
| 36 | +from lib.topotest import iproute2_is_vrf_capable |
| 37 | +from mininet.topo import Topo |
| 38 | + |
| 39 | + |
| 40 | +class TemplateTopo(Topo): |
| 41 | + def build(self, *_args, **_opts): |
| 42 | + tgen = get_topogen(self) |
| 43 | + |
| 44 | + for routern in range(1, 3): |
| 45 | + tgen.add_router("r{}".format(routern)) |
| 46 | + switch = tgen.add_switch("s1") |
| 47 | + switch.add_link(tgen.gears["r1"]) |
| 48 | + switch.add_link(tgen.gears["r2"]) |
| 49 | + |
| 50 | + # r1-r2 2 |
| 51 | + switch = tgen.add_switch("s2") |
| 52 | + switch.add_link(tgen.gears["r1"]) |
| 53 | + switch.add_link(tgen.gears["r2"]) |
| 54 | + |
| 55 | + |
| 56 | +def setup_module(mod): |
| 57 | + # iproute2 needs to support VRFs for this suite to run. |
| 58 | + if not iproute2_is_vrf_capable(): |
| 59 | + pytest.skip("Installed iproute2 version does not support VRFs") |
| 60 | + |
| 61 | + tgen = Topogen(TemplateTopo, mod.__name__) |
| 62 | + tgen.start_topology() |
| 63 | + |
| 64 | + r1 = tgen.gears["r1"] |
| 65 | + r2 = tgen.gears["r2"] |
| 66 | + |
| 67 | + # blue vrf |
| 68 | + r1.run("ip link add blue type vrf table 1001") |
| 69 | + r1.run("ip link set up dev blue") |
| 70 | + r2.run("ip link add blue type vrf table 1001") |
| 71 | + r2.run("ip link set up dev blue") |
| 72 | + |
| 73 | + r1.run("ip link add lo1 type dummy") |
| 74 | + r1.run("ip link set lo1 master blue") |
| 75 | + r1.run("ip link set up dev lo1") |
| 76 | + r2.run("ip link add lo1 type dummy") |
| 77 | + r2.run("ip link set up dev lo1") |
| 78 | + r2.run("ip link set lo1 master blue") |
| 79 | + |
| 80 | + r1.run("ip link set r1-eth1 master blue") |
| 81 | + r2.run("ip link set r2-eth1 master blue") |
| 82 | + |
| 83 | + r1.run("ip link set up dev r1-eth1") |
| 84 | + r2.run("ip link set up dev r2-eth1") |
| 85 | + router_list = tgen.routers() |
| 86 | + |
| 87 | + for i, (rname, router) in enumerate(router_list.items(), 1): |
| 88 | + router.load_config( |
| 89 | + TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname)) |
| 90 | + ) |
| 91 | + router.load_config( |
| 92 | + TopoRouter.RD_BGP, os.path.join(CWD, "{}/bgpd.conf".format(rname)) |
| 93 | + ) |
| 94 | + tgen.start_router() |
| 95 | + |
| 96 | + |
| 97 | +def teardown_module(mod): |
| 98 | + tgen = get_topogen() |
| 99 | + tgen.stop_topology() |
| 100 | + |
| 101 | + |
| 102 | +def test_bgp_vrf_link_local(): |
| 103 | + tgen = get_topogen() |
| 104 | + |
| 105 | + if tgen.routers_have_failure(): |
| 106 | + pytest.skip(tgen.errors) |
| 107 | + |
| 108 | + def _bgp_ll_neighbor_configured(): |
| 109 | + output = json.loads( |
| 110 | + tgen.gears["r1"].vtysh_cmd("show ip bgp vrf blue neighbor json") |
| 111 | + ) |
| 112 | + expected = { |
| 113 | + "fe80:1::2": {"bgpState": "Established"}, |
| 114 | + } |
| 115 | + return topotest.json_cmp(output, expected) |
| 116 | + |
| 117 | + test_func = functools.partial(_bgp_ll_neighbor_configured) |
| 118 | + success, result = topotest.run_and_expect(test_func, None, count=60, wait=0.5) |
| 119 | + |
| 120 | + assert result is None, 'Failed bgp convergence in "{}"'.format(tgen.gears["r1"]) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + args = ["-s"] + sys.argv[1:] |
| 125 | + sys.exit(pytest.main(args)) |
| 126 | + |
0 commit comments