summaryrefslogtreecommitdiffstats
path: root/tests/topotests/eigrp_topo1/test_eigrp_topo1.py
blob: 3c9392c3f58db8123d04d965389f6754b7a6551d (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
#!/usr/bin/env python
# SPDX-License-Identifier: ISC

#
# test_eigrp_topo1.py
#
# Copyright (c) 2017 by
# Cumulus Networks, Inc.
# Donald Sharp
#

"""
test_eigrp_topo1.py: Testing EIGRP

"""

import os
import re
import sys
import pytest
import json

pytestmark = [pytest.mark.eigrpd]

# 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.

#####################################################
##
##   Network Topology Definition
##
#####################################################


def build_topo(tgen):
    for routern in range(1, 4):
        tgen.add_router("r{}".format(routern))

    # On main router
    # First switch is for a dummy interface (for local network)
    switch = tgen.add_switch("sw1")
    switch.add_link(tgen.gears["r1"])

    # Switches for EIGRP
    # switch 2 switch is for connection to EIGRP router
    switch = tgen.add_switch("sw2")
    switch.add_link(tgen.gears["r1"])
    switch.add_link(tgen.gears["r2"])

    # switch 4 is stub on remote EIGRP router
    switch = tgen.add_switch("sw4")
    switch.add_link(tgen.gears["r3"])

    # switch 3 is between EIGRP routers
    switch = tgen.add_switch("sw3")
    switch.add_link(tgen.gears["r2"])
    switch.add_link(tgen.gears["r3"])


#####################################################
##
##   Tests starting
##
#####################################################


def setup_module(module):
    "Setup topology"
    tgen = Topogen(build_topo, module.__name__)
    tgen.start_topology()

    # This is a sample of configuration loading.
    router_list = tgen.routers()
    for rname, router in router_list.items():
        router.load_config(
            TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
        )
        router.load_config(
            TopoRouter.RD_EIGRP, os.path.join(CWD, "{}/eigrpd.conf".format(rname))
        )

    tgen.start_router()


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

    # This function tears down the whole topology.
    tgen.stop_topology()


def test_converge_protocols():
    "Wait for protocol convergence"

    tgen = get_topogen()
    # Don't run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    topotest.sleep(5, "Waiting for EIGRP convergence")


def test_eigrp_routes():
    "Test EIGRP 'show ip eigrp'"

    tgen = get_topogen()
    # Don't run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    # Verify EIGRP Status
    logger.info("Verifying EIGRP routes")

    router_list = tgen.routers().values()
    for router in router_list:
        refTableFile = "{}/{}/show_ip_eigrp.json".format(CWD, router.name)

        # Read expected result from file
        expected = json.loads(open(refTableFile).read())

        # Actual output from router
        actual = ip_eigrp_topo(router)

        assertmsg = '"show ip eigrp topo" mismatches on {}'.format(router.name)
        assert topotest.json_cmp(actual, expected) is None, assertmsg


def test_zebra_ipv4_routingTable():
    "Test 'show ip route'"

    tgen = get_topogen()
    # Don't run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    failures = 0
    router_list = tgen.routers().values()
    for router in router_list:
        output = router.vtysh_cmd("show ip route json", isjson=True)
        refTableFile = "{}/{}/show_ip_route.json_ref".format(CWD, router.name)
        expected = json.loads(open(refTableFile).read())

        assertmsg = "Zebra IPv4 Routing Table verification failed for router {}".format(
            router.name
        )
        assert topotest.json_cmp(output, expected) is None, assertmsg


def test_shut_interface_and_recover():
    "Test shutdown of an interface and recovery of the interface"

    tgen = get_topogen()
    router = tgen.gears["r1"]
    router.run("ip link set r1-eth1 down")
    topotest.sleep(5, "Waiting for EIGRP convergence")
    router.run("ip link set r1-eth1 up")


def test_shutdown_check_stderr():
    if os.environ.get("TOPOTESTS_CHECK_STDERR") is None:
        pytest.skip("Skipping test for Stderr output and memory leaks")

    tgen = get_topogen()
    # Don't run this test if we have any failure.
    if tgen.routers_have_failure():
        pytest.skip(tgen.errors)

    logger.info("Verifying unexpected STDERR output from daemons")

    router_list = tgen.routers().values()
    for router in router_list:
        router.stop()

        log = tgen.net[router.name].getStdErr("eigrpd")
        if log:
            logger.error("EIGRPd StdErr Log:" + log)
        log = tgen.net[router.name].getStdErr("zebra")
        if log:
            logger.error("Zebra StdErr Log:" + log)


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

#
# Auxiliary Functions
#
def ip_eigrp_topo(node):
    """
    Parse 'show ip eigrp topo' from `node` and returns a dict with the
    result.

    Example:
    {
        'P': {
            '192.168.1.0/24': {
                'sucessors': 1,
                'fd': 112233,
                'serno': 0,
                'via': 'Connected',
                'interface': 'eth0',
            },
            '192.168.2.0/24': {
                'sucessors': 1,
                'fd': 112234,
                'serno': 0,
                'via': 'Connected',
                'interface': 'eth1',
            }
        }
    }
    """
    output = topotest.normalize_text(node.vtysh_cmd("show ip eigrp topo")).splitlines()
    result = {}
    for idx, line in enumerate(output):
        columns = line.split(" ", 1)

        # Parse the following format into python dicts
        # code A.B.C.D/E, X successors, FD is Y, serno: Z
        #       via FOO, interface-name
        code = columns[0]
        if code not in ["P", "A", "U", "Q", "R", "r", "s"]:
            continue

        if code not in result:
            result[code] = {}

        # Split network from the rest
        columns = columns[1].split(",")

        # Parse first line data
        network = columns[0]
        result[code][network] = {}
        for column in columns:
            # Skip the network column
            if column == columns[0]:
                continue

            match = re.search(r"(\d+) successors", column)
            if match is not None:
                result[code][network]["successors"] = match.group(1)
                continue

            match = re.search(r"FD is (\d+)", column)
            if match is not None:
                result[code][network]["fd"] = match.group(1)
                continue

            match = re.search(r"serno: (\d+)", column)
            if match is not None:
                result[code][network]["serno"] = match.group(1)
                continue

        # Parse second line data
        nextline = output[idx + 1]
        columns = topotest.normalize_text(nextline).split(",")
        for column in columns:
            match = re.search(r"via (.+)", column)
            if match is not None:
                result[code][network]["via"] = match.group(1)
                continue

            match = re.search(r"(.+)", column)
            if match is not None:
                result[code][network]["interface"] = match.group(1)
                continue

    return result