7 from framework import VppTestCase
8 from multiprocessing import Process, Pipe
9 from pickle import dumps
12 from enum import IntEnum, IntFlag
15 class SerializableClassCopy:
17 Empty class used as a basis for a serializable copy of another class.
23 return "<SerializableClassCopy dict=%s>" % self.__dict__
26 class RemoteClassAttr:
28 Wrapper around attribute of a remotely executed class.
31 def __init__(self, remote, attr):
32 self._path = [attr] if attr else []
35 def path_to_str(self):
36 return ".".join(self._path)
38 def get_remote_value(self):
39 return self._remote._remote_exec(RemoteClass.GET, self.path_to_str())
42 return self._remote._remote_exec(RemoteClass.REPR, self.path_to_str())
45 return self._remote._remote_exec(RemoteClass.STR, self.path_to_str())
47 def __getattr__(self, attr):
49 if not (attr.startswith("__") and attr.endswith("__")):
50 raise AttributeError("tried to get private attribute: %s ", attr)
51 self._path.append(attr)
54 def __setattr__(self, attr, val):
56 if not (attr.startswith("__") and attr.endswith("__")):
57 super(RemoteClassAttr, self).__setattr__(attr, val)
59 self._path.append(attr)
60 self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(), value=val)
62 def __call__(self, *args, **kwargs):
63 return self._remote._remote_exec(
64 RemoteClass.CALL, self.path_to_str(), *args, **kwargs
68 class RemoteClass(Process):
70 This class can wrap around and adapt the interface of another class,
71 and then delegate its execution to a newly forked child process.
75 #. Create a remotely executed instance of MyClass. ::
77 object = RemoteClass(MyClass, arg1='foo', arg2='bar')
80 #. Access the object normally as if it was an instance of your
83 object.my_attribute = 20
84 print object.my_attribute
85 print object.my_method(object.my_attribute)
86 object.my_attribute.nested_attribute = 'test'
88 #. If you need the value of a remote attribute, use .get_remote_value
89 method. This method is automatically called when needed in the
90 context of a remotely executed class. E.g. ::
92 if (object.my_attribute.get_remote_value() > 20):
93 object.my_attribute2 = object.my_attribute
95 #. Destroy the instance. ::
101 GET = 0 # Get attribute remotely
102 CALL = 1 # Call method remotely
103 SETATTR = 2 # Set attribute remotely
104 REPR = 3 # Get representation of a remote object
105 STR = 4 # Get string representation of a remote object
106 QUIT = 5 # Quit remote execution
108 PIPE_PARENT = 0 # Parent end of the pipe
109 PIPE_CHILD = 1 # Child end of the pipe
111 DEFAULT_TIMEOUT = 2 # default timeout for an operation to execute
113 def __init__(self, cls, *args, **kwargs):
114 super(RemoteClass, self).__init__()
117 self._kwargs = kwargs
118 self._timeout = RemoteClass.DEFAULT_TIMEOUT
119 self._pipe = Pipe() # pipe for input/output arguments
122 return reprlib.repr(RemoteClassAttr(self, None))
125 return str(RemoteClassAttr(self, None))
127 def __call__(self, *args, **kwargs):
128 return self.RemoteClassAttr(self, None)()
130 def __getattr__(self, attr):
131 if attr[0] == "_" or not self.is_alive():
132 if not (attr.startswith("__") and attr.endswith("__")):
133 if hasattr(super(RemoteClass, self), "__getattr__"):
134 return super(RemoteClass, self).__getattr__(attr)
135 raise AttributeError("missing: %s", attr)
136 return RemoteClassAttr(self, attr)
138 def __setattr__(self, attr, val):
139 if attr[0] == "_" or not self.is_alive():
140 if not (attr.startswith("__") and attr.endswith("__")):
141 super(RemoteClass, self).__setattr__(attr, val)
143 setattr(RemoteClassAttr(self, None), attr, val)
145 def _remote_exec(self, op, path=None, *args, **kwargs):
147 Execute given operation on a given, possibly nested, member remotely.
149 # automatically resolve remote objects in the arguments
150 mutable_args = list(args)
151 for i, val in enumerate(mutable_args):
152 if isinstance(val, RemoteClass) or isinstance(val, RemoteClassAttr):
153 mutable_args[i] = val.get_remote_value()
154 args = tuple(mutable_args)
155 for key, val in kwargs.items():
156 if isinstance(val, RemoteClass) or isinstance(val, RemoteClassAttr):
157 kwargs[key] = val.get_remote_value()
159 args = self._make_serializable(args)
160 kwargs = self._make_serializable(kwargs)
161 self._pipe[RemoteClass.PIPE_PARENT].send((op, path, args, kwargs))
162 timeout = self._timeout
163 # adjust timeout specifically for the .sleep method
164 if path is not None and path.split(".")[-1] == "sleep":
165 if args and isinstance(args[0], (long, int)):
167 elif "timeout" in kwargs:
168 timeout += kwargs["timeout"]
169 if not self._pipe[RemoteClass.PIPE_PARENT].poll(timeout):
172 rv = self._pipe[RemoteClass.PIPE_PARENT].recv()
173 rv = self._deserialize(rv)
178 def _get_local_object(self, path):
180 Follow the path to obtain a reference on the addressed nested attribute
184 obj = getattr(obj, attr)
187 def _get_local_value(self, path):
189 return self._get_local_object(path)
190 except AttributeError:
193 def _call_local_method(self, path, *args, **kwargs):
195 method = self._get_local_object(path)
196 return method(*args, **kwargs)
197 except AttributeError:
200 def _set_local_attr(self, path, value):
202 obj = self._get_local_object(path[:-1])
203 setattr(obj, path[-1], value)
204 except AttributeError:
208 def _get_local_repr(self, path):
210 obj = self._get_local_object(path)
211 return reprlib.repr(obj)
212 except AttributeError:
215 def _get_local_str(self, path):
217 obj = self._get_local_object(path)
219 except AttributeError:
222 def _serializable(self, obj):
223 """Test if the given object is serializable"""
230 def _make_obj_serializable(self, obj):
232 Make a serializable copy of an object.
233 Members which are difficult/impossible to serialize are stripped.
235 if self._serializable(obj):
236 return obj # already serializable
238 copy = SerializableClassCopy()
241 Dictionaries can hold complex values, so we split keys and values into
242 separate lists and serialize them individually.
244 if type(obj) is dict:
245 copy.type = type(obj)
248 for k, v in obj.items():
249 copy.k_list.append(self._make_serializable(k))
250 copy.v_list.append(self._make_serializable(v))
253 # copy at least serializable attributes and properties
254 for name, member in inspect.getmembers(obj):
255 # skip private members and non-writable dunder methods.
257 if name in ["__weakref__"]:
259 if name in ["__dict__"]:
261 if not (name.startswith("__") and name.endswith("__")):
263 if callable(member) and not isinstance(member, property):
265 if not self._serializable(member):
266 member = self._make_serializable(member)
267 setattr(copy, name, member)
270 def _make_serializable(self, obj):
272 Make a serializable copy of an object or a list/tuple of objects.
273 Members which are difficult/impossible to serialize are stripped.
275 if (type(obj) is list) or (type(obj) is tuple):
278 rv.append(self._make_serializable(item))
279 if type(obj) is tuple:
282 elif isinstance(obj, IntEnum) or isinstance(obj, IntFlag):
285 return self._make_obj_serializable(obj)
287 def _deserialize_obj(self, obj):
288 if hasattr(obj, "type"):
291 for k, v in zip(obj.k_list, obj.v_list):
292 _obj[self._deserialize(k)] = self._deserialize(v)
296 def _deserialize(self, obj):
297 if (type(obj) is list) or (type(obj) is tuple):
300 rv.append(self._deserialize(item))
301 if type(obj) is tuple:
305 return self._deserialize_obj(obj)
307 def start_remote(self):
308 """Start remote execution"""
311 def quit_remote(self):
312 """Quit remote execution"""
313 self._remote_exec(RemoteClass.QUIT, None)
315 def get_remote_value(self):
316 """Get value of a remotely held object"""
317 return RemoteClassAttr(self, None).get_remote_value()
319 def set_request_timeout(self, timeout):
320 """Change request timeout"""
321 self._timeout = timeout
325 Create instance of the wrapped class and execute operations
326 on it as requested by the parent process.
328 self._instance = self._cls(*self._args, **self._kwargs)
332 # get request from the parent process
333 (op, path, args, kwargs) = self._pipe[RemoteClass.PIPE_CHILD].recv()
334 args = self._deserialize(args)
335 kwargs = self._deserialize(kwargs)
336 path = path.split(".") if path else []
337 if op == RemoteClass.GET:
338 rv = self._get_local_value(path)
339 elif op == RemoteClass.CALL:
340 rv = self._call_local_method(path, *args, **kwargs)
341 elif op == RemoteClass.SETATTR and "value" in kwargs:
342 self._set_local_attr(path, kwargs["value"])
343 elif op == RemoteClass.REPR:
344 rv = self._get_local_repr(path)
345 elif op == RemoteClass.STR:
346 rv = self._get_local_str(path)
347 elif op == RemoteClass.QUIT:
352 if not self._serializable(rv):
353 rv = self._make_serializable(rv)
354 self._pipe[RemoteClass.PIPE_CHILD].send(rv)
357 self._instance = None # destroy the instance
360 @unittest.skip("Remote Vpp Test Case Class")
361 class RemoteVppTestCase(VppTestCase):
362 """Re-use VppTestCase to create remote VPP segment
368 # fork new process before client connects to VPP
369 cls.remote_test = RemoteClass(RemoteVppTestCase)
371 # start remote process
372 cls.remote_test.start_remote()
374 # set up your test case
375 super(MyTestCase, cls).setUpClass()
378 cls.remote_test.setUpClass(cls.tempdir)
381 def tearDownClass(cls):
382 # tear down remote test
383 cls.remote_test.tearDownClass()
385 # stop remote process
386 cls.remote_test.quit_remote()
388 # tear down your test case
389 super(MyTestCase, cls).tearDownClass()
393 super(RemoteVppTestCase, self).__init__("emptyTest")
395 # Note: __del__ is a 'Finalizer" not a 'Destructor'.
396 # https://docs.python.org/3/reference/datamodel.html#object.__del__
398 if hasattr(self, "vpp"):
400 if self.vpp.returncode is None:
402 self.vpp.communicate()
405 def setUpClass(cls, tempdir):
406 # disable features unsupported in remote VPP
407 orig_env = dict(os.environ)
408 if "STEP" in os.environ:
409 del os.environ["STEP"]
410 if "DEBUG" in os.environ:
411 del os.environ["DEBUG"]
412 cls.tempdir_prefix = os.path.basename(tempdir) + "/"
413 super(RemoteVppTestCase, cls).setUpClass()
414 os.environ = orig_env
417 def tearDownClass(cls):
418 super(RemoteVppTestCase, cls).tearDownClass()
420 @unittest.skip("Empty test")
425 def setTestFunctionInfo(self, name, doc):
427 Store the name and documentation string of currently executed test
428 in the main VPP for logging purposes.
430 self._testMethodName = name
431 self._testMethodDoc = doc