6 from multiprocessing import Process, Pipe
7 from pickle import dumps
12 from framework import VppTestCase
13 from aenum import Enum
16 class SerializableClassCopy(object):
18 Empty class used as a basis for a serializable copy of another class.
23 return '<SerializableClassCopy dict=%s>' % self.__dict__
26 class RemoteClassAttr(object):
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 ',
52 self._path.append(attr)
55 def __setattr__(self, attr, val):
57 if not (attr.startswith('__') and attr.endswith('__')):
58 super(RemoteClassAttr, self).__setattr__(attr, val)
60 self._path.append(attr)
61 self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(),
64 def __call__(self, *args, **kwargs):
65 ret = True if 'vapi' in self.path_to_str() else False
66 return self._remote._remote_exec(RemoteClass.CALL, self.path_to_str(),
70 class RemoteClass(Process):
72 This class can wrap around and adapt the interface of another class,
73 and then delegate its execution to a newly forked child process.
75 # Create a remotely executed instance of MyClass
76 object = RemoteClass(MyClass, arg1='foo', arg2='bar')
78 # Access the object normally as if it was an instance of your class.
79 object.my_attribute = 20
80 print object.my_attribute
81 print object.my_method(object.my_attribute)
82 object.my_attribute.nested_attribute = 'test'
83 # If you need the value of a remote attribute, use .get_remote_value
84 method. This method is automatically called when needed in the context
85 of a remotely executed class. E.g.:
86 if (object.my_attribute.get_remote_value() > 20):
87 object.my_attribute2 = object.my_attribute
88 # Destroy the instance
93 GET = 0 # Get attribute remotely
94 CALL = 1 # Call method remotely
95 SETATTR = 2 # Set attribute remotely
96 REPR = 3 # Get representation of a remote object
97 STR = 4 # Get string representation of a remote object
98 QUIT = 5 # Quit remote execution
100 PIPE_PARENT = 0 # Parent end of the pipe
101 PIPE_CHILD = 1 # Child end of the pipe
103 DEFAULT_TIMEOUT = 2 # default timeout for an operation to execute
105 def __init__(self, cls, *args, **kwargs):
106 super(RemoteClass, self).__init__()
109 self._kwargs = kwargs
110 self._timeout = RemoteClass.DEFAULT_TIMEOUT
111 self._pipe = Pipe() # pipe for input/output arguments
114 return moves.reprlib.repr(RemoteClassAttr(self, None))
117 return str(RemoteClassAttr(self, None))
119 def __call__(self, *args, **kwargs):
120 return self.RemoteClassAttr(self, None)()
122 def __getattr__(self, attr):
123 if attr[0] == '_' or not self.is_alive():
124 if not (attr.startswith('__') and attr.endswith('__')):
125 if hasattr(super(RemoteClass, self), '__getattr__'):
126 return super(RemoteClass, self).__getattr__(attr)
127 raise AttributeError('missing: %s', attr)
128 return RemoteClassAttr(self, attr)
130 def __setattr__(self, attr, val):
131 if attr[0] == '_' or not self.is_alive():
132 if not (attr.startswith('__') and attr.endswith('__')):
133 super(RemoteClass, self).__setattr__(attr, val)
135 setattr(RemoteClassAttr(self, None), attr, val)
137 def _remote_exec(self, op, path=None, ret=True, *args, **kwargs):
139 Execute given operation on a given, possibly nested, member remotely.
141 # automatically resolve remote objects in the arguments
142 mutable_args = list(args)
143 for i, val in enumerate(mutable_args):
144 if isinstance(val, RemoteClass) or \
145 isinstance(val, RemoteClassAttr):
146 mutable_args[i] = val.get_remote_value()
147 args = tuple(mutable_args)
148 for key, val in six.iteritems(kwargs):
149 if isinstance(val, RemoteClass) or \
150 isinstance(val, RemoteClassAttr):
151 kwargs[key] = val.get_remote_value()
153 args = self._make_serializable(args)
154 kwargs = self._make_serializable(kwargs)
155 self._pipe[RemoteClass.PIPE_PARENT].send((op, path, args, kwargs))
157 # no return value expected
159 timeout = self._timeout
160 # adjust timeout specifically for the .sleep method
161 if path.split('.')[-1] == 'sleep':
162 if args and isinstance(args[0], (long, int)):
164 elif 'timeout' in kwargs:
165 timeout += kwargs['timeout']
166 if not self._pipe[RemoteClass.PIPE_PARENT].poll(timeout):
169 rv = self._pipe[RemoteClass.PIPE_PARENT].recv()
170 rv = self._deserialize(rv)
175 def _get_local_object(self, path):
177 Follow the path to obtain a reference on the addressed nested attribute
181 obj = getattr(obj, attr)
184 def _get_local_value(self, path):
186 return self._get_local_object(path)
187 except AttributeError:
190 def _call_local_method(self, path, *args, **kwargs):
192 method = self._get_local_object(path)
193 return method(*args, **kwargs)
194 except AttributeError:
197 def _set_local_attr(self, path, value):
199 obj = self._get_local_object(path[:-1])
200 setattr(obj, path[-1], value)
201 except AttributeError:
205 def _get_local_repr(self, path):
207 obj = self._get_local_object(path)
208 return moves.reprlib.repr(obj)
209 except AttributeError:
212 def _get_local_str(self, path):
214 obj = self._get_local_object(path)
216 except AttributeError:
219 def _serializable(self, obj):
220 """ Test if the given object is serializable """
227 def _make_obj_serializable(self, obj):
229 Make a serializable copy of an object.
230 Members which are difficult/impossible to serialize are stripped.
232 if self._serializable(obj):
233 return obj # already serializable
235 copy = SerializableClassCopy()
238 Dictionaries can hold complex values, so we split keys and values into
239 separate lists and serialize them individually.
241 if (type(obj) is dict):
242 copy.type = type(obj)
245 for k, v in obj.items():
246 copy.k_list.append(self._make_serializable(k))
247 copy.v_list.append(self._make_serializable(v))
250 # copy at least serializable attributes and properties
251 for name, member in inspect.getmembers(obj):
252 # skip private members and non-writable dunder methods.
254 if name in ['__weakref__']:
256 if not (name.startswith('__') and name.endswith('__')):
258 if callable(member) and not isinstance(member, property):
260 if not self._serializable(member):
262 setattr(copy, name, member)
265 def _make_serializable(self, obj):
267 Make a serializable copy of an object or a list/tuple of objects.
268 Members which are difficult/impossible to serialize are stripped.
270 if (type(obj) is list) or (type(obj) is tuple):
273 rv.append(self._make_serializable(item))
274 if type(obj) is tuple:
277 elif (isinstance(obj, Enum)):
280 return self._make_obj_serializable(obj)
282 def _deserialize_obj(self, obj):
283 if (hasattr(obj, 'type')):
286 for k, v in zip(obj.k_list, obj.v_list):
287 _obj[self._deserialize(k)] = self._deserialize(v)
291 def _deserialize(self, obj):
292 if (type(obj) is list) or (type(obj) is tuple):
295 rv.append(self._deserialize(item))
296 if type(obj) is tuple:
300 return self._deserialize_obj(obj)
302 def start_remote(self):
303 """ Start remote execution """
306 def quit_remote(self):
307 """ Quit remote execution """
308 self._remote_exec(RemoteClass.QUIT, None, False)
310 def get_remote_value(self):
311 """ Get value of a remotely held object """
312 return RemoteClassAttr(self, None).get_remote_value()
314 def set_request_timeout(self, timeout):
315 """ Change request timeout """
316 self._timeout = timeout
320 Create instance of the wrapped class and execute operations
321 on it as requested by the parent process.
323 self._instance = self._cls(*self._args, **self._kwargs)
327 # get request from the parent process
329 kwargs) = self._pipe[RemoteClass.PIPE_CHILD].recv()
330 args = self._deserialize(args)
331 kwargs = self._deserialize(kwargs)
332 path = path.split('.') if path else []
333 if op == RemoteClass.GET:
334 rv = self._get_local_value(path)
335 elif op == RemoteClass.CALL:
336 rv = self._call_local_method(path, *args, **kwargs)
337 elif op == RemoteClass.SETATTR and 'value' in kwargs:
338 self._set_local_attr(path, kwargs['value'])
339 elif op == RemoteClass.REPR:
340 rv = self._get_local_repr(path)
341 elif op == RemoteClass.STR:
342 rv = self._get_local_str(path)
343 elif op == RemoteClass.QUIT:
348 if not self._serializable(rv):
349 rv = self._make_serializable(rv)
350 self._pipe[RemoteClass.PIPE_CHILD].send(rv)
353 self._instance = None # destroy the instance
356 @unittest.skip("Remote Vpp Test Case Class")
357 class RemoteVppTestCase(VppTestCase):
358 """ Re-use VppTestCase to create remote VPP segment
364 # fork new process before client connects to VPP
365 cls.remote_test = RemoteClass(RemoteVppTestCase)
367 # start remote process
368 cls.remote_test.start_remote()
370 # set up your test case
371 super(MyTestCase, cls).setUpClass()
374 cls.remote_test.setUpClass(cls.tempdir)
377 def tearDownClass(cls):
378 # tear down remote test
379 cls.remote_test.tearDownClass()
381 # stop remote process
382 cls.remote_test.quit_remote()
384 # tear down your test case
385 super(MyTestCase, cls).tearDownClass()
389 super(RemoteVppTestCase, self).__init__("emptyTest")
391 # Note: __del__ is a 'Finalizer" not a 'Destructor'.
392 # https://docs.python.org/3/reference/datamodel.html#object.__del__
394 if hasattr(self, "vpp"):
396 if self.vpp.returncode is None:
398 self.vpp.communicate()
401 def setUpClass(cls, tempdir):
402 # disable features unsupported in remote VPP
403 orig_env = dict(os.environ)
404 if 'STEP' in os.environ:
405 del os.environ['STEP']
406 if 'DEBUG' in os.environ:
407 del os.environ['DEBUG']
408 cls.tempdir_prefix = os.path.basename(tempdir) + "/"
409 super(RemoteVppTestCase, cls).setUpClass()
410 os.environ = orig_env
413 def tearDownClass(cls):
414 super(RemoteVppTestCase, cls).tearDownClass()
416 @unittest.skip("Empty test")
421 def setTestFunctionInfo(self, name, doc):
423 Store the name and documentation string of currently executed test
424 in the main VPP for logging purposes.
426 self._testMethodName = name
427 self._testMethodDoc = doc