topotests: add API to detect if iproute2 is json capable

Some tests may want to use the json facility of iproute2 to
dump some results.
Add an internal API in lib/topotest.py that tells whether iproute2
is json capable or not.

Signed-off-by: Philippe Guibert <philippe.guibert@6wind.com>
This commit is contained in:
Philippe Guibert 2024-06-10 08:38:22 +02:00
parent 9fb7d677d3
commit a6b1d38d7f

View File

@ -602,6 +602,30 @@ def is_linux():
return False
def iproute2_is_json_capable():
"""
Checks if the iproute2 version installed on the system is capable of
handling JSON outputss
Returns True if capability can be detected, returns False otherwise.
"""
if is_linux():
try:
subp = subprocess.Popen(
["ip", "-json", "route", "show"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE,
)
iproute2_err = subp.communicate()[1].splitlines()[0].split()[0]
if iproute2_err != "Error:":
return True
except Exception:
pass
return False
def iproute2_is_vrf_capable():
"""
Checks if the iproute2 version installed on the system is capable of