]> git.proxmox.com Git - mirror_frr.git/blame - tests/topotests/bgp_snmp_bgp4v2mib/test_bgp_snmp_bgp4v2mib.py
*: auto-convert to SPDX License IDs
[mirror_frr.git] / tests / topotests / bgp_snmp_bgp4v2mib / test_bgp_snmp_bgp4v2mib.py
CommitLineData
54257fae 1#!/usr/bin/env python
acddc0ed 2# SPDX-License-Identifier: ISC
54257fae
DA
3
4#
5# Copyright (c) 2022 Donatas Abraitis <donatas@opensourcerouting.org>
6#
54257fae
DA
7
8"""
9Test some of the BGP4V2-MIB entries.
10"""
11
12import os
13import sys
14import json
15from time import sleep
16import pytest
17import functools
18
19CWD = os.path.dirname(os.path.realpath(__file__))
20sys.path.append(os.path.join(CWD, "../"))
21
22# pylint: disable=C0413
23# Import topogen and topotest helpers
24from lib.topogen import Topogen, TopoRouter, get_topogen
25from lib.snmptest import SnmpTester
26from lib import topotest
27
28pytestmark = [pytest.mark.bgpd, pytest.mark.snmp]
29
30
31def build_topo(tgen):
32 tgen.add_router("r1")
33 tgen.add_router("r2")
34
35 switch = tgen.add_switch("s1")
36 switch.add_link(tgen.gears["r1"])
37 switch.add_link(tgen.gears["r2"])
38
39
40def setup_module(mod):
41 snmpd = os.system("which snmpd")
42 if snmpd:
43 error_msg = "SNMP not installed - skipping"
44 pytest.skip(error_msg)
45
46 tgen = Topogen(build_topo, mod.__name__)
47 tgen.start_topology()
48
49 for rname, router in tgen.routers().items():
50 router.load_config(
51 TopoRouter.RD_ZEBRA, os.path.join(CWD, "{}/zebra.conf".format(rname))
52 )
53 router.load_config(
54 TopoRouter.RD_BGP,
55 os.path.join(CWD, "{}/bgpd.conf".format(rname)),
56 "-M snmp",
57 )
58 router.load_config(
59 TopoRouter.RD_SNMP,
60 os.path.join(CWD, "{}/snmpd.conf".format(rname)),
61 "-Le -Ivacm_conf,usmConf,iquery -V -DAgentX",
62 )
63
64 tgen.start_router()
65
66
67def teardown_module(mod):
68 tgen = get_topogen()
69 tgen.stop_topology()
70
71
72def test_bgp_snmp_bgp4v2():
73 tgen = get_topogen()
74
75 r2 = tgen.gears["r2"]
76
77 def _bgp_converge_summary():
78 output = json.loads(r2.vtysh_cmd("show bgp summary json"))
79 expected = {
80 "ipv4Unicast": {
81 "peers": {
82 "192.168.12.1": {
83 "state": "Established",
84 "pfxRcd": 2,
85 }
86 }
87 },
88 "ipv6Unicast": {
89 "peers": {
90 "2001:db8::12:1": {
91 "state": "Established",
92 "pfxRcd": 2,
93 }
94 }
95 },
96 }
97 return topotest.json_cmp(output, expected)
98
99 test_func = functools.partial(_bgp_converge_summary)
100 _, result = topotest.run_and_expect(test_func, None, count=30, wait=1)
101 assert result is None, "Can't see connections established"
102
103 def _bgp_converge_prefixes():
104 output = json.loads(r2.vtysh_cmd("show bgp all json"))
105 expected = {
106 "ipv4Unicast": {
107 "routes": {
108 "10.0.0.0/31": [
109 {
110 "metric": 1,
111 "origin": "IGP",
112 }
113 ],
114 "10.0.0.2/32": [
115 {
116 "metric": 2,
117 "origin": "incomplete",
118 }
119 ],
120 }
121 },
122 "ipv6Unicast": {
123 "routes": {
124 "2001:db8::1/128": [
125 {
126 "metric": 1,
127 "origin": "IGP",
128 }
129 ],
130 "2001:db8:1::/56": [
131 {
132 "metric": 2,
133 "origin": "incomplete",
134 }
135 ],
136 }
137 },
138 }
139 return topotest.json_cmp(output, expected)
140
141 test_func = functools.partial(_bgp_converge_prefixes)
142 _, result = topotest.run_and_expect(test_func, None, count=30, wait=1)
143 assert result is None, "Can't see prefixes from R1"
144
145 snmp = SnmpTester(r2, "localhost", "public", "2c", "-Ln -On")
146
147 def _snmpwalk_remote_addr():
148 expected = {
149 "1.3.6.1.3.5.1.1.2.1.5.1.4.192.168.12.1": "C0 A8 0C 01",
150 "1.3.6.1.3.5.1.1.2.1.5.2.16.32.1.13.184.0.0.0.0.0.0.0.0.0.18.0.1": "20 01 0D B8 00 00 00 00 00 00 00 00 00 12 00 01",
151 }
152
153 # bgp4V2PeerRemoteAddr
154 output, _ = snmp.walk(".1.3.6.1.3.5.1.1.2.1.5")
155 return output == expected
156
157 _, result = topotest.run_and_expect(_snmpwalk_remote_addr, True, count=10, wait=1)
158 assertmsg = "Can't fetch SNMP for bgp4V2PeerRemoteAddr"
159 assert result, assertmsg
160
161 def _snmpwalk_peer_state():
162 expected = {
163 "1.3.6.1.3.5.1.1.2.1.13.1.4.192.168.12.1": "6",
164 "1.3.6.1.3.5.1.1.2.1.13.2.16.32.1.13.184.0.0.0.0.0.0.0.0.0.18.0.1": "6",
165 }
166
167 # bgp4V2PeerState
168 output, _ = snmp.walk(".1.3.6.1.3.5.1.1.2.1.13")
169 return output == expected
170
171 _, result = topotest.run_and_expect(_snmpwalk_peer_state, True, count=10, wait=1)
172 assertmsg = "Can't fetch SNMP for bgp4V2PeerState"
173 assert result, assertmsg
174
175 def _snmpwalk_peer_last_error_code_received():
176 expected = {
177 "1.3.6.1.3.5.1.1.3.1.1.1.4.192.168.12.1": "0",
178 "1.3.6.1.3.5.1.1.3.1.1.2.16.32.1.13.184.0.0.0.0.0.0.0.0.0.18.0.1": "0",
179 }
180
181 # bgp4V2PeerLastErrorCodeReceived
182 output, _ = snmp.walk(".1.3.6.1.3.5.1.1.3.1.1")
183 return output == expected
184
185 _, result = topotest.run_and_expect(
186 _snmpwalk_peer_last_error_code_received, True, count=10, wait=1
187 )
188 assertmsg = "Can't fetch SNMP for bgp4V2PeerLastErrorCodeReceived"
189 assert result, assertmsg
190
191 def _snmpwalk_origin():
192 expected = {
193 "1.3.6.1.3.5.1.1.9.1.9.1.4.10.0.0.0.31.192.168.12.1": "1",
194 "1.3.6.1.3.5.1.1.9.1.9.1.4.10.0.0.2.32.192.168.12.1": "3",
195 "1.3.6.1.3.5.1.1.9.1.9.2.16.32.1.13.184.0.0.0.0.0.0.0.0.0.0.0.1.128.32.1.13.184.0.0.0.0.0.0.0.0.0.18.0.1": "1",
196 "1.3.6.1.3.5.1.1.9.1.9.2.16.32.1.13.184.0.1.0.0.0.0.0.0.0.0.0.0.56.32.1.13.184.0.0.0.0.0.0.0.0.0.18.0.1": "3",
197 }
198
199 # bgp4V2NlriOrigin
200 output, _ = snmp.walk(".1.3.6.1.3.5.1.1.9.1.9")
201 return output == expected
202
203 _, result = topotest.run_and_expect(_snmpwalk_origin, True, count=10, wait=1)
204 assertmsg = "Can't fetch SNMP for bgp4V2NlriOrigin"
205 assert result, assertmsg
206
207 def _snmpwalk_med():
208 expected = {
209 "1.3.6.1.3.5.1.1.9.1.17.1.4.10.0.0.0.31.192.168.12.1": "1",
210 "1.3.6.1.3.5.1.1.9.1.17.1.4.10.0.0.2.32.192.168.12.1": "2",
211 "1.3.6.1.3.5.1.1.9.1.17.2.16.32.1.13.184.0.0.0.0.0.0.0.0.0.0.0.1.128.32.1.13.184.0.0.0.0.0.0.0.0.0.18.0.1": "1",
212 "1.3.6.1.3.5.1.1.9.1.17.2.16.32.1.13.184.0.1.0.0.0.0.0.0.0.0.0.0.56.32.1.13.184.0.0.0.0.0.0.0.0.0.18.0.1": "2",
213 }
214
215 # bgp4V2NlriMed
216 output, _ = snmp.walk(".1.3.6.1.3.5.1.1.9.1.17")
217 return output == expected
218
219 _, result = topotest.run_and_expect(_snmpwalk_med, True, count=10, wait=1)
220 assertmsg = "Can't fetch SNMP for bgp4V2NlriMed"
221 assert result, assertmsg
222
223
224def test_memory_leak():
225 "Run the memory leak test and report results."
226 tgen = get_topogen()
227 if not tgen.is_memleak_enabled():
228 pytest.skip("Memory leak test/report is disabled")
229
230 tgen.report_memory_leaks()
231
232
233if __name__ == "__main__":
234 args = ["-s"] + sys.argv[1:]
235 sys.exit(pytest.main(args))