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
37 api_sock = "" # api_sock file path
38 stats_sock = "" # stats sock_file path
39 socket_dir = "" # running VPP's socket directory
40 pid = None # running VPP's pid
41 returncode = None # indicates to the framework that VPP is running
44 def get_stats_sock_path(cls):
48 def get_api_sock_path(cls):
53 """VPP is already running -- skip this action."""
58 """Indicate quitting to framework by setting returncode=1."""
63 """Indicate termination to framework by setting returncode=1."""
67 def get_default_socket_dir(cls):
68 """Return running VPP's default socket directory.
70 Default socket dir is:
71 /var/run/user/${UID}/vpp (or)
72 /var/run/vpp, if VPP is started as a root user
74 if cls.is_running_vpp():
76 subprocess.check_output(["ps", "-o", "uid=", "-p", str(cls.pid)])
80 if vpp_user_id == "0":
83 return f"/var/run/user/{vpp_user_id}/vpp"
86 "Error: getting default socket dir, as "
87 "a running VPP process could not be found"
92 def get_set_vpp_sock_files(cls):
93 """Look for *.sock files in the socket_dir and set cls attributes.
95 Returns a tuple: (api_sock_file, stats_sock_file)
96 Sets cls.api_sock and cls.stats_sock attributes
98 # Return if the sock files are already set
99 if cls.api_sock and cls.stats_sock:
100 return (cls.api_sock, cls.stats_sock)
101 # Find running VPP's sock files in the socket dir
102 if os.path.isdir(cls.socket_dir):
103 if not cls.is_running_vpp():
105 "Error: The socket dir for a running VPP directory is, "
106 "set but a running VPP process could not be found"
109 sock_files = glob(os.path.join(cls.socket_dir + "/" + "*.sock"))
110 for sock_file in sock_files:
111 if "api.sock" in sock_file:
112 cls.api_sock = os.path.abspath(sock_file)
113 elif "stats.sock" in sock_file:
114 cls.stats_sock = os.path.abspath(sock_file)
117 f"Error: Could not find a valid api.sock file "
118 f"in running VPP's socket directory {cls.socket_dir}"
121 if not cls.stats_sock:
123 f"Error: Could not find a valid stats.sock file "
124 f"in running VPP's socket directory {cls.socket_dir}"
127 return (cls.api_sock, cls.stats_sock)
129 print("Error: The socket dir for a running VPP directory is unset")
133 def is_running_vpp(cls):
134 """Return True if VPP's pid is visible else False."""
135 vpp_pid = subprocess.Popen(
136 ["pgrep", "-d,", "-x", "vpp_main"],
137 stdout=subprocess.PIPE,
138 stderr=subprocess.PIPE,
139 universal_newlines=True,
141 stdout, stderr = vpp_pid.communicate()
142 cls.pid = int(stdout.split(",")[0]) if stdout else None
147 """Return None to indicate that the process hasn't terminated."""
148 return cls.returncode
151 if __name__ == "__main__":
152 RunningVPP.socket_dir = RunningVPP.get_default_socket_dir()
153 RunningVPP.get_set_vpp_sock_files()
154 print(f"Running VPP's sock files")
155 print(f"api_sock_file {RunningVPP.api_sock}")
156 print(f"stats_sock_file {RunningVPP.stats_sock}")