tests: fix multiple grpc-client.py running in parallel

Signed-off-by: Christian Hopps <chopps@labn.net>
This commit is contained in:
Christian Hopps 2024-05-29 02:38:01 -04:00
parent 52703f0c20
commit cd5791c12e

View File

@ -10,36 +10,46 @@ import argparse
import logging import logging
import os import os
import sys import sys
import tempfile
import pytest import pytest
CWD = os.path.dirname(os.path.realpath(__file__)) 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: try:
import grpc_tools # Make sure we don't run-into ourselves in parallel operating environment
tmpdir = tempfile.mkdtemp(prefix="grpc-client-")
import grpc # 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
sys.path.append(os.path.dirname(CWD)) import grpc
from munet.base import commander
commander.cmd_raises(f"cp {CWD}/../../../grpc/frr-northbound.proto .") commander.cmd_raises(f"cp {CWD}/../../../grpc/frr-northbound.proto .")
commander.cmd_raises( commander.cmd_raises(
"python3 -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I . frr-northbound.proto" "python3 -m grpc_tools.protoc"
) f" --python_out={tmpdir} --grpc_python_out={tmpdir}"
except Exception as error: f" -I {CWD}/../../../grpc frr-northbound.proto"
logging.error("can't create proto definition modules %s", error) )
raise except Exception as error:
logging.error("can't create proto definition modules %s", error)
raise
try: try:
sys.path[0:0] = "." sys.path[0:0] = [tmpdir]
import frr_northbound_pb2 print(sys.path)
import frr_northbound_pb2_grpc import frr_northbound_pb2
except Exception as error: import frr_northbound_pb2_grpc
logging.error("can't import proto definition modules %s", error)
raise 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: class GRPCClient: