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
280
281
|
# -*- coding: utf-8 eval: (blacken-mode 1) -*-
# SPDX-License-Identifier: ISC
#
# January 23 2024, Christian Hopps <chopps@labn.net>
#
# Copyright (c) 2024, LabN Consulting, L.L.C.
#
"""
Test YANG Notifications
"""
import json
import logging
import os
import re
import pytest
from lib.micronet import Timeout, comm_error
from lib.topogen import Topogen
from lib.topotest import json_cmp
from oper import check_kernel_32
pytestmark = [pytest.mark.ripd, pytest.mark.staticd, pytest.mark.mgmtd]
CWD = os.path.dirname(os.path.realpath(__file__))
@pytest.fixture(scope="module")
def tgen(request):
"Setup/Teardown the environment and provide tgen argument to tests"
topodef = {
"s1": ("r1", "r2"),
}
tgen = Topogen(topodef, request.module.__name__)
tgen.start_topology()
router_list = tgen.routers()
for _, router in router_list.items():
router.load_frr_config("frr.conf")
tgen.start_router()
yield tgen
tgen.stop_topology()
def myreadline(f):
buf = ""
while True:
# logging.debug("READING 1 CHAR")
c = f.read(1)
if not c:
return buf if buf else None
buf += c
# logging.debug("READ CHAR: '%s'", c)
if c == "\n":
return buf
def _wait_output(f, regex, maxwait=120):
timeout = Timeout(maxwait)
while not timeout.is_expired():
# line = p.stdout.readline()
line = myreadline(f)
if not line:
assert None, "EOF waiting for '{}'".format(regex)
line = line.rstrip()
if line:
logging.debug("GOT LINE: '%s'", line)
m = re.search(regex, line)
if m:
return m
assert None, "Failed to get output matching '{}' withint {} actual {}s".format(
regex, maxwait, timeout.elapsed()
)
def get_op_and_json(output):
op = ""
path = ""
data = ""
for line in output.split("\n"):
if not line:
continue
if not op:
m = re.match("#OP=([A-Z]*): (.*)", line)
if m:
op = m.group(1)
path = m.group(2)
continue
data += line + "\n"
if not op:
assert False, f"No notifcation op present in:\n{output}"
return op, path, data
def test_frontend_datastore_notification(tgen):
if tgen.routers_have_failure():
pytest.skip(tgen.errors)
r1 = tgen.gears["r1"].net
check_kernel_32(r1, "11.11.11.11", 1, "")
fe_client_path = CWD + "/../lib/fe_client.py"
rc, _, _ = r1.cmd_status(fe_client_path + " --help")
if rc:
pytest.skip("No protoc or present cannot run test")
# Start our FE client in the background
p = r1.popen(
[fe_client_path, "--datastore", "--listen=/frr-interface:lib/interface"]
)
_wait_output(p.stderr, "Connected", maxwait=10)
r1.cmd_raises("ip link set r1-eth0 mtu 1200")
# {"frr-interface:lib":{"interface":[{"name":"r1-eth0","state":{"if-index":2,"mtu":1200,"mtu6":1200,"speed":10000,"metric":0,"phy-address":"ba:fd:de:b5:8b:90"}}]}}
try:
# Wait for FE client to exit
output, error = p.communicate(timeout=10)
op, path, data = get_op_and_json(output)
assert op == "REPLACE"
assert path.startswith("/frr-interface:lib/interface[name='r1-eth0']/state")
jsout = json.loads(data)
expected = json.loads(
'{"frr-interface:lib":{"interface":[{"name":"r1-eth0","state":{"mtu":1200}}]}}'
)
result = json_cmp(jsout, expected)
assert result is None
finally:
p.kill()
r1.cmd_raises("ip link set r1-eth0 mtu 1500")
def test_frontend_notification(tgen):
if tgen.routers_have_failure():
pytest.skip(tgen.errors)
r1 = tgen.gears["r1"].net
check_kernel_32(r1, "11.11.11.11", 1, "")
fe_client_path = CWD + "/../lib/fe_client.py"
rc, _, _ = r1.cmd_status(fe_client_path + " --help")
if rc:
pytest.skip("No protoc or present cannot run test")
# Update config to non-matching authentication.
conf = """
conf t
interface r1-eth0
ip rip authentication string bar
"""
r1.cmd_raises("vtysh", stdin=conf)
try:
output = r1.cmd_raises(
fe_client_path + " --listen /frr-ripd:authentication-failure"
)
jsout = json.loads(output)
expected = {"frr-ripd:authentication-failure": {"interface-name": "r1-eth0"}}
result = json_cmp(jsout, expected)
assert result is None
output = r1.cmd_raises(
fe_client_path + " --use-protobuf --listen /frr-ripd:authentication-failure"
)
jsout = json.loads(output)
expected = {"frr-ripd:authentication-failure": {"interface-name": "r1-eth0"}}
result = json_cmp(jsout, expected)
assert result is None
finally:
# Update config to matching authentication.
conf = """
conf t
interface r1-eth0
ip rip authentication string foo
"""
r1.cmd_raises("vtysh", stdin=conf)
def test_frontend_all_notification(tgen):
if tgen.routers_have_failure():
pytest.skip(tgen.errors)
r1 = tgen.gears["r1"].net
check_kernel_32(r1, "11.11.11.11", 1, "")
fe_client_path = CWD + "/../lib/fe_client.py"
rc, _, _ = r1.cmd_status(fe_client_path + " --help")
if rc:
pytest.skip("No protoc or present cannot run test")
# Update config to non-matching authentication.
conf = """
conf t
interface r1-eth0
ip rip authentication string bar
"""
r1.cmd_raises("vtysh", stdin=conf)
try:
# The first notifications is a frr-ripd:authentication-type-failure
# All the rest are frr-ripd:authentication-failure so we check for both.
output = r1.cmd_raises(fe_client_path + " --listen /")
jsout = json.loads(output)
expected = {
"frr-ripd:authentication-type-failure": {"interface-name": "r1-eth0"}
}
result = json_cmp(jsout, expected)
if result is not None:
expected = {
"frr-ripd:authentication-failure": {"interface-name": "r1-eth0"}
}
result = json_cmp(jsout, expected)
assert result is None
output = r1.cmd_raises(fe_client_path + " --use-protobuf --listen /")
jsout = json.loads(output)
expected = {"frr-ripd:authentication-failure": {"interface-name": "r1-eth0"}}
result = json_cmp(jsout, expected)
assert result is None
finally:
# Update config to matching authentication.
conf = """
conf t
interface r1-eth0
ip rip authentication string foo
"""
r1.cmd_raises("vtysh", stdin=conf)
def test_backend_notification(tgen):
if tgen.routers_have_failure():
pytest.skip(tgen.errors)
r1 = tgen.gears["r1"].net
check_kernel_32(r1, "11.11.11.11", 1, "")
be_client_path = "/usr/lib/frr/mgmtd_testc"
rc, _, _ = r1.cmd_status(be_client_path + " --help")
if rc:
pytest.skip("No mgmtd_testc")
# Update config to non-matching authentication.
conf = """
conf t
interface r1-eth0
ip rip authentication string bar
"""
r1.cmd_raises("vtysh", stdin=conf)
try:
output = r1.cmd_raises(
be_client_path
+ " --timeout 20 --log file:mgmt_testc.log --listen /frr-ripd"
)
jsout = json.loads(output)
expected = {"frr-ripd:authentication-failure": {"interface-name": "r1-eth0"}}
result = json_cmp(jsout, expected)
assert result is None
finally:
# Update config to matching authentication.
conf = """
conf t
interface r1-eth0
ip rip authentication string foo
"""
r1.cmd_raises("vtysh", stdin=conf)
|