3 # Supporting module for running tests against a running VPP.
4 # This module is used by the test framework. Do not invoke this module
5 # directly for running tests against a running vpp. Use run.py for
6 # running all unit tests.
12 from config import config
16 """Update VPPTestCase to use running VPP's sock files & methods.
19 cls -- VPPTestCase Class
21 if config.running_vpp:
22 if os.path.isdir(config.socket_dir):
23 RunningVPP.socket_dir = config.socket_dir
25 RunningVPP.socket_dir = RunningVPP.get_default_socket_dir()
26 RunningVPP.get_set_vpp_sock_files()
27 cls.get_stats_sock_path = RunningVPP.get_stats_sock_path
28 cls.get_api_sock_path = RunningVPP.get_api_sock_path
29 cls.run_vpp = RunningVPP.run_vpp
30 cls.quit_vpp = RunningVPP.quit_vpp
32 cls.running_vpp = True
38 api_sock = "" # api_sock file path
39 stats_sock = "" # stats sock_file path
40 socket_dir = "" # running VPP's socket directory
41 pid = None # running VPP's pid
42 returncode = None # indicates to the framework that VPP is running
45 def get_stats_sock_path(cls):
49 def get_api_sock_path(cls):
54 """VPP is already running -- skip this action."""
59 """Indicate quitting to framework by setting returncode=1."""
64 """Indicate termination to framework by setting returncode=1."""
68 def get_default_socket_dir(cls):
69 """Return running VPP's default socket directory.
71 Default socket dir is:
72 /var/run/user/${UID}/vpp (or)
73 /var/run/vpp, if VPP is started as a root user
75 if cls.is_running_vpp():
77 subprocess.check_output(["ps", "-o", "uid=", "-p", str(cls.pid)])
81 if vpp_user_id == "0":
84 return f"/var/run/user/{vpp_user_id}/vpp"
87 "Error: getting default socket dir, as "
88 "a running VPP process could not be found"
93 def get_set_vpp_sock_files(cls):
94 """Look for *.sock files in the socket_dir and set cls attributes.
96 Returns a tuple: (api_sock_file, stats_sock_file)
97 Sets cls.api_sock and cls.stats_sock attributes
99 # Return if the sock files are already set
100 if cls.api_sock and cls.stats_sock:
101 return (cls.api_sock, cls.stats_sock)
102 # Find running VPP's sock files in the socket dir
103 if os.path.isdir(cls.socket_dir):
104 if not cls.is_running_vpp():
106 "Error: The socket dir for a running VPP directory is, "
107 "set but a running VPP process could not be found"
110 sock_files = glob(os.path.join(cls.socket_dir + "/" + "*.sock"))
111 for sock_file in sock_files:
112 if "api.sock" in sock_file:
113 cls.api_sock = os.path.abspath(sock_file)
114 elif "stats.sock" in sock_file:
115 cls.stats_sock = os.path.abspath(sock_file)
118 f"Error: Could not find a valid api.sock file "
119 f"in running VPP's socket directory {cls.socket_dir}"
122 if not cls.stats_sock:
124 f"Error: Could not find a valid stats.sock file "
125 f"in running VPP's socket directory {cls.socket_dir}"
128 return (cls.api_sock, cls.stats_sock)
130 print("Error: The socket dir for a running VPP directory is unset")
134 def is_running_vpp(cls):
135 """Return True if VPP's pid is visible else False."""
136 vpp_pid = subprocess.Popen(
137 ["pgrep", "-d,", "-x", "vpp_main"],
138 stdout=subprocess.PIPE,
139 stderr=subprocess.PIPE,
140 universal_newlines=True,
142 stdout, stderr = vpp_pid.communicate()
143 cls.pid = int(stdout.split(",")[0]) if stdout else None
148 """Return None to indicate that the process hasn't terminated."""
149 return cls.returncode
152 if __name__ == "__main__":
153 RunningVPP.socket_dir = RunningVPP.get_default_socket_dir()
154 RunningVPP.get_set_vpp_sock_files()
155 print(f"Running VPP's sock files")
156 print(f"api_sock_file {RunningVPP.api_sock}")
157 print(f"stats_sock_file {RunningVPP.stats_sock}")