|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 eval: (blacken-mode 1) -*- |
| 3 | +# SPDX-License-Identifier: ISC |
| 4 | +# |
| 5 | +# test_static_route_description.py: |
| 6 | +# Static Route Description Test |
| 7 | +# |
| 8 | +# Copyright (c) 2025 by Dustin Rosarius |
| 9 | +# |
| 10 | + |
| 11 | +r""" |
| 12 | +test_static_route_description.py: Test to verify that static route description command works correctly. |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import pytest |
| 18 | +import functools |
| 19 | + |
| 20 | +# Import topogen and required test moduless |
| 21 | +CWD = os.path.dirname(os.path.realpath(__file__)) |
| 22 | +sys.path.append(os.path.join(CWD, "../")) |
| 23 | + |
| 24 | +from lib import topotest |
| 25 | +from lib.topogen import Topogen, TopoRouter |
| 26 | +from lib.common_config import step |
| 27 | + |
| 28 | +pytestmark = [pytest.mark.staticd] |
| 29 | + |
| 30 | + |
| 31 | +def build_topo(tgen): |
| 32 | + """Build the topology for Static Route decription test.""" |
| 33 | + |
| 34 | + # Create router |
| 35 | + r1 = tgen.add_router("r1") |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(scope="module") |
| 39 | +def tgen(request): |
| 40 | + "Setup/Teardown the environment and provide tgen argument to tests" |
| 41 | + |
| 42 | + tgen = Topogen(build_topo, request.module.__name__) |
| 43 | + tgen.start_topology() |
| 44 | + |
| 45 | + router_list = tgen.routers() |
| 46 | + |
| 47 | + # For all routers arrange for: |
| 48 | + # - starting zebra using config file from <rtrname>/zebra.conf |
| 49 | + # - starting ripd using an empty config file. |
| 50 | + # - loading frr config file from <rtrname>/frr.conf |
| 51 | + for rname, router in router_list.items(): |
| 52 | + router.load_config(TopoRouter.RD_ZEBRA) |
| 53 | + router.load_config(TopoRouter.RD_STATIC) |
| 54 | + router.load_config(TopoRouter.RD_MGMTD) |
| 55 | + router.load_frr_config(os.path.join(CWD, f"{rname}/frr.conf")) |
| 56 | + |
| 57 | + # Start and configure the router daemons |
| 58 | + tgen.start_router() |
| 59 | + |
| 60 | + # Provide tgen as argument to each test function |
| 61 | + yield tgen |
| 62 | + |
| 63 | + # Teardown after last test runs |
| 64 | + tgen.stop_topology() |
| 65 | + |
| 66 | + |
| 67 | +# =================== |
| 68 | +# The tests functions |
| 69 | +# =================== |
| 70 | + |
| 71 | + |
| 72 | +def test_static_route_description(tgen): |
| 73 | + |
| 74 | + r1 = tgen.gears["r1"] |
| 75 | + |
| 76 | + def _check_config(pattern, command, should_exist=True): |
| 77 | + |
| 78 | + output = r1.vtysh_cmd(command) |
| 79 | + found = pattern in output |
| 80 | + |
| 81 | + if should_exist and found: |
| 82 | + return None |
| 83 | + if not should_exist and not found: |
| 84 | + return None |
| 85 | + |
| 86 | + return "'{}' {} (Expected: {})".format( |
| 87 | + pattern, |
| 88 | + "found" if found else "NOT found", |
| 89 | + "Found" if should_exist else "Not Found", |
| 90 | + ) |
| 91 | + |
| 92 | + step( |
| 93 | + "Test static route description command: Verify r1 has 'ip route 1.1.1.1/32 Null0' with description" |
| 94 | + "'TEST_DESCRIPTION' in running-config" |
| 95 | + ) |
| 96 | + |
| 97 | + expected = "ip route 1.1.1.1/32 Null0 description TEST_DESCRIPTION" |
| 98 | + command = "show running-config" |
| 99 | + test_func = functools.partial(_check_config, expected, command) |
| 100 | + _, result = topotest.run_and_expect(test_func, None, count=30, wait=1) |
| 101 | + |
| 102 | + assert result is None, result |
| 103 | + |
| 104 | + step( |
| 105 | + "Test update static route description: Verify r1 static route 'ip route 1.1.1.1/32 Null0' has an updated description of " |
| 106 | + "'NEW_DESCRIPTION' in running-config" |
| 107 | + ) |
| 108 | + r1.vtysh_cmd( |
| 109 | + """ |
| 110 | + configure terminal |
| 111 | + ip route 1.1.1.1/32 Null0 description NEW_DESCRIPTION |
| 112 | + exit |
| 113 | + """ |
| 114 | + ) |
| 115 | + |
| 116 | + expected = "ip route 1.1.1.1/32 Null0 description NEW_DESCRIPTION" |
| 117 | + command = "show running-config" |
| 118 | + test_func = functools.partial(_check_config, expected, command) |
| 119 | + _, result = topotest.run_and_expect(test_func, None, count=30, wait=1) |
| 120 | + |
| 121 | + assert result is None, result |
| 122 | + |
| 123 | + step( |
| 124 | + "Test delete static route: Verify r1 static route 'ip route 1.1.1.1/32 Null0 description NEW_DESCRIPTION' has been removed from running-config" |
| 125 | + ) |
| 126 | + r1.vtysh_cmd( |
| 127 | + """ |
| 128 | + configure terminal |
| 129 | + no ip route 1.1.1.1/32 Null0 description NEW_DESCRIPTION |
| 130 | + exit |
| 131 | + """ |
| 132 | + ) |
| 133 | + |
| 134 | + expected = "ip route 1.1.1.1/32 Null0 description NEW_DESCRIPTION" |
| 135 | + command = "show running-config" |
| 136 | + test_func = functools.partial(_check_config, expected, command, False) |
| 137 | + _, result = topotest.run_and_expect(test_func, None, count=30, wait=1) |
| 138 | + |
| 139 | + assert result is None, result |
| 140 | + |
| 141 | + step( |
| 142 | + "Test static route with a too long description: Verify r1 will display an error message and the static route will not be in running-config" |
| 143 | + ) |
| 144 | + command = """ |
| 145 | + configure terminal |
| 146 | + ip route 1.1.1.1/32 Null0 description aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa |
| 147 | + exit |
| 148 | + """ |
| 149 | + |
| 150 | + expected = "% Description too long (Max 80 characters)" |
| 151 | + test_func = functools.partial(_check_config, expected, command) |
| 152 | + _, result = topotest.run_and_expect(test_func, None, count=30, wait=1) |
| 153 | + assert result is None, result |
| 154 | + |
| 155 | + expected = "ip route 1.1.1.1/32 Null0" |
| 156 | + command = "show running-config" |
| 157 | + test_func = functools.partial(_check_config, expected, command, False) |
| 158 | + _, result = topotest.run_and_expect(test_func, None, count=30, wait=1) |
| 159 | + assert result is None, result |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + args = ["-s"] + sys.argv[1:] |
| 164 | + sys.exit(pytest.main(args)) |
0 commit comments