From a6b1d38d7f038c335143d963b7b7a13377ef5f22 Mon Sep 17 00:00:00 2001 From: Philippe Guibert Date: Mon, 10 Jun 2024 08:38:22 +0200 Subject: [PATCH] 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 --- tests/topotests/lib/topotest.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/topotests/lib/topotest.py b/tests/topotests/lib/topotest.py index 087d8454fc..1d4bc2eac6 100644 --- a/tests/topotests/lib/topotest.py +++ b/tests/topotests/lib/topotest.py @@ -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 -- 2.39.5