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.get_memif_sock_path = RunningVPP.get_memif_sock_path
30 cls.run_vpp = RunningVPP.run_vpp
31 cls.quit_vpp = RunningVPP.quit_vpp
33 cls.running_vpp = True
38 api_sock = "" # api_sock file path
39 stats_sock = "" # stats sock_file path
40 memif_sock = "" # memif sock path
41 socket_dir = "" # running VPP's socket directory
42 pid = None # running VPP's pid
43 returncode = None # indicates to the framework that VPP is running
46 def get_stats_sock_path(cls):
50 def get_api_sock_path(cls):
54 def get_memif_sock_path(cls):
59 """VPP is already running -- skip this action."""
64 """Indicate quitting to framework by setting returncode=1."""
69 """Indicate termination to framework by setting returncode=1."""
73 def get_default_socket_dir(cls):
74 """Return running VPP's default socket directory.
76 Default socket dir is:
77 /var/run/user/${UID}/vpp (or)
78 /var/run/vpp, if VPP is started as a root user
80 if cls.is_running_vpp():
82 subprocess.check_output(["ps", "-o", "uid=", "-p", str(cls.pid)])
86 if vpp_user_id == "0":
89 return f"/var/run/user/{vpp_user_id}/vpp"
92 "Error: getting default socket dir, as "
93 "a running VPP process could not be found"
98 def get_set_vpp_sock_files(cls):
99 """Look for *.sock files in the socket_dir and set cls attributes.
101 Returns a tuple: (api_sock_file, stats_sock_file)
102 Sets cls.api_sock and cls.stats_sock attributes
104 # Return if the sock files are already set
105 if cls.api_sock and cls.stats_sock:
106 return (cls.api_sock, cls.stats_sock)
107 # Find running VPP's sock files in the socket dir
108 if os.path.isdir(cls.socket_dir):
109 if not cls.is_running_vpp():
111 "Error: The socket dir for a running VPP directory is, "
112 "set but a running VPP process could not be found"
115 sock_files = glob(os.path.join(cls.socket_dir + "/" + "*.sock"))
116 for sock_file in sock_files:
117 if "api.sock" in sock_file:
118 cls.api_sock = os.path.abspath(sock_file)
119 elif "stats.sock" in sock_file:
120 cls.stats_sock = os.path.abspath(sock_file)
121 elif "memif.sock" in sock_file:
122 cls.memif_sock = os.path.abspath(sock_file)
125 f"Error: Could not find a valid api.sock file "
126 f"in running VPP's socket directory {cls.socket_dir}"
129 if not cls.stats_sock:
131 f"Error: Could not find a valid stats.sock file "
132 f"in running VPP's socket directory {cls.socket_dir}"
135 return (cls.api_sock, cls.stats_sock)
137 print("Error: The socket dir for a running VPP directory is unset")
141 def is_running_vpp(cls):
142 """Return True if VPP's pid is visible else False."""
143 vpp_pid = subprocess.Popen(
144 ["pgrep", "-d,", "-x", "vpp_main"],
145 stdout=subprocess.PIPE,
146 stderr=subprocess.PIPE,
147 universal_newlines=True,
149 stdout, stderr = vpp_pid.communicate()
150 cls.pid = int(stdout.split(",")[0]) if stdout else None
155 """Return None to indicate that the process hasn't terminated."""
156 return cls.returncode
159 if __name__ == "__main__":
160 RunningVPP.socket_dir = RunningVPP.get_default_socket_dir()
161 RunningVPP.get_set_vpp_sock_files()
162 print(f"Running VPP's sock files")
163 print(f"api_sock_file {RunningVPP.api_sock}")
164 print(f"stats_sock_file {RunningVPP.stats_sock}")