summaryrefslogtreecommitdiffstats
path: root/tests/topotests/lib/grpc-query.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/topotests/lib/grpc-query.py')
-rwxr-xr-xtests/topotests/lib/grpc-query.py96
1 files changed, 60 insertions, 36 deletions
diff --git a/tests/topotests/lib/grpc-query.py b/tests/topotests/lib/grpc-query.py
index 8c4701c..13b6361 100755
--- a/tests/topotests/lib/grpc-query.py
+++ b/tests/topotests/lib/grpc-query.py
@@ -10,35 +10,46 @@ import argparse
import logging
import os
import sys
+import tempfile
import pytest
CWD = os.path.dirname(os.path.realpath(__file__))
-# This is painful but works if you have installed grpc and grpc_tools would be *way*
-# better if we actually built and installed these but ... python packaging.
try:
- import grpc
- import grpc_tools
-
- sys.path.append(os.path.dirname(CWD))
- from munet.base import commander
-
- commander.cmd_raises(f"cp {CWD}/../../../grpc/frr-northbound.proto .")
- commander.cmd_raises(
- f"python3 -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I . frr-northbound.proto"
- )
-except Exception as error:
- logging.error("can't create proto definition modules %s", error)
- raise
-
-try:
- sys.path[0:0] = "."
- import frr_northbound_pb2
- import frr_northbound_pb2_grpc
-except Exception as error:
- logging.error("can't import proto definition modules %s", error)
- raise
+ # Make sure we don't run-into ourselves in parallel operating environment
+ tmpdir = tempfile.mkdtemp(prefix="grpc-client-")
+
+ # This is painful but works if you have installed grpc and grpc_tools would be *way*
+ # better if we actually built and installed these but ... python packaging.
+ try:
+ import grpc_tools
+ from munet.base import commander
+
+ import grpc
+
+ commander.cmd_raises(f"cp {CWD}/../../../grpc/frr-northbound.proto .")
+ commander.cmd_raises(
+ "python3 -m grpc_tools.protoc"
+ f" --python_out={tmpdir} --grpc_python_out={tmpdir}"
+ f" -I {CWD}/../../../grpc frr-northbound.proto"
+ )
+ except Exception as error:
+ logging.error("can't create proto definition modules %s", error)
+ raise
+
+ try:
+ sys.path[0:0] = [tmpdir]
+ print(sys.path)
+ import frr_northbound_pb2
+ import frr_northbound_pb2_grpc
+
+ sys.path = sys.path[1:]
+ except Exception as error:
+ logging.error("can't import proto definition modules %s", error)
+ raise
+finally:
+ commander.cmd_nostatus(f"rm -rf {tmpdir}")
class GRPCClient:
@@ -57,16 +68,16 @@ class GRPCClient:
logging.debug("GRPC Capabilities: %s", response)
return response
- def get(self, xpath):
+ def get(self, xpath, encoding, gtype):
request = frr_northbound_pb2.GetRequest()
request.path.append(xpath)
- request.type = frr_northbound_pb2.GetRequest.ALL
- request.encoding = frr_northbound_pb2.XML
- xml = ""
+ request.type = gtype
+ request.encoding = encoding
+ result = ""
for r in self.stub.Get(request):
- logging.info('GRPC Get path: "%s" value: %s', request.path, r)
- xml += str(r.data.data)
- return xml
+ logging.debug('GRPC Get path: "%s" value: %s', request.path, r)
+ result += str(r.data.data)
+ return result
def next_action(action_list=None):
@@ -95,6 +106,7 @@ def main(*args):
)
parser.add_argument("-v", "--verbose", action="store_true", help="be verbose")
parser.add_argument("--check", action="store_true", help="check runable")
+ parser.add_argument("--xml", action="store_true", help="encode XML instead of JSON")
parser.add_argument("actions", nargs="*", help="GETCAP|GET,xpath")
args = parser.parse_args(*args)
@@ -107,20 +119,32 @@ def main(*args):
if args.check:
sys.exit(0)
+ encoding = frr_northbound_pb2.XML if args.xml else frr_northbound_pb2.JSON
+
c = GRPCClient(args.server, args.port)
for action in next_action(args.actions):
action = action.casefold()
- logging.info("GOT ACTION: %s", action)
+ logging.debug("GOT ACTION: %s", action)
if action == "getcap":
caps = c.get_capabilities()
- print("Capabilities:", caps)
+ print(caps)
elif action.startswith("get,"):
- # Print Interface State and Config
+ # Get and print config and state
+ _, xpath = action.split(",", 1)
+ logging.debug("Get XPath: %s", xpath)
+ print(c.get(xpath, encoding, gtype=frr_northbound_pb2.GetRequest.ALL))
+ elif action.startswith("get-config,"):
+ # Get and print config
+ _, xpath = action.split(",", 1)
+ logging.debug("Get Config XPath: %s", xpath)
+ print(c.get(xpath, encoding, gtype=frr_northbound_pb2.GetRequest.CONFIG))
+ # for _ in range(0, 1):
+ elif action.startswith("get-state,"):
+ # Get and print state
_, xpath = action.split(",", 1)
- print("Get XPath: ", xpath)
- xml = c.get(xpath)
- print("{}: {}".format(xpath, xml))
+ logging.debug("Get State XPath: %s", xpath)
+ print(c.get(xpath, encoding, gtype=frr_northbound_pb2.GetRequest.STATE))
# for _ in range(0, 1):