6 from framework import VppTestCase
7 from multiprocessing import Process, Pipe
8 from pickle import dumps
13 if sys.version_info < (3,):
14 from aenum import IntEnum, IntFlag
16 from enum import IntEnum, IntFlag
19 class SerializableClassCopy(object):
21 Empty class used as a basis for a serializable copy of another class.
26 return '<SerializableClassCopy dict=%s>' % self.__dict__
29 class RemoteClassAttr(object):
31 Wrapper around attribute of a remotely executed class.
34 def __init__(self, remote, attr):
35 self._path = [attr] if attr else []
38 def path_to_str(self):
39 return '.'.join(self._path)
41 def get_remote_value(self):
42 return self._remote._remote_exec(RemoteClass.GET, self.path_to_str())
45 return self._remote._remote_exec(RemoteClass.REPR, self.path_to_str())
48 return self._remote._remote_exec(RemoteClass.STR, self.path_to_str())
50 def __getattr__(self, attr):
52 if not (attr.startswith('__') and attr.endswith('__')):
53 raise AttributeError('tried to get private attribute: %s ',
55 self._path.append(attr)
58 def __setattr__(self, attr, val):
60 if not (attr.startswith('__') and attr.endswith('__')):
61 super(RemoteClassAttr, self).__setattr__(attr, val)
63 self._path.append(attr)
64 self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(),
67 def __call__(self, *args, **kwargs):
68 return self._remote._remote_exec(RemoteClass.CALL, self.path_to_str(),
72 class RemoteClass(Process):
74 This class can wrap around and adapt the interface of another class,
75 and then delegate its execution to a newly forked child process.
77 # Create a remotely executed instance of MyClass
78 object = RemoteClass(MyClass, arg1='foo', arg2='bar')
80 # Access the object normally as if it was an instance of your class.
81 object.my_attribute = 20
82 print object.my_attribute
83 print object.my_method(object.my_attribute)
84 object.my_attribute.nested_attribute = 'test'
85 # If you need the value of a remote attribute, use .get_remote_value
86 method. This method is automatically called when needed in the context
87 of a remotely executed class. E.g.:
88 if (object.my_attribute.get_remote_value() > 20):
89 object.my_attribute2 = object.my_attribute
90 # Destroy the instance
95 GET = 0 # Get attribute remotely
96 CALL = 1 # Call method remotely
97 SETATTR = 2 # Set attribute remotely
98 REPR = 3 # Get representation of a remote object
99 STR = 4 # Get string representation of a remote object
100 QUIT = 5 # Quit remote execution
102 PIPE_PARENT = 0 # Parent end of the pipe
103 PIPE_CHILD = 1 # Child end of the pipe
105 DEFAULT_TIMEOUT = 2 # default timeout for an operation to execute
107 def __init__(self, cls, *args, **kwargs):
108 super(RemoteClass, self).__init__()
111 self._kwargs = kwargs
112 self._timeout = RemoteClass.DEFAULT_TIMEOUT
113 self._pipe = Pipe() # pipe for input/output arguments
116 return moves.reprlib.repr(RemoteClassAttr(self, None))
119 return str(RemoteClassAttr(self, None))
121 def __call__(self, *args, **kwargs):
122 return self.RemoteClassAttr(self, None)()
124 def __getattr__(self, attr):
125 if attr[0] == '_' or not self.is_alive():
126 if not (attr.startswith('__') and attr.endswith('__')):
127 if hasattr(super(RemoteClass, self), '__getattr__'):
128 return super(RemoteClass, self).__getattr__(attr)
129 raise AttributeError('missing: %s', attr)
130 return RemoteClassAttr(self, attr)
132 def __setattr__(self, attr, val):
133 if attr[0] == '_' or not self.is_alive():
134 if not (attr.startswith('__') and attr.endswith('__')):
135 super(RemoteClass, self).__setattr__(attr, val)
137 setattr(RemoteClassAttr(self, None), attr, val)
139 def _remote_exec(self, op, path=None, *args, **kwargs):
141 Execute given operation on a given, possibly nested, member remotely.
143 # automatically resolve remote objects in the arguments
144 mutable_args = list(args)
145 for i, val in enumerate(mutable_args):
146 if isinstance(val, RemoteClass) or \
147 isinstance(val, RemoteClassAttr):
148 mutable_args[i] = val.get_remote_value()
149 args = tuple(mutable_args)
150 for key, val in six.iteritems(kwargs):
151 if isinstance(val, RemoteClass) or \
152 isinstance(val, RemoteClassAttr):
153 kwargs[key] = val.get_remote_value()
155 args = self._make_serializable(args)
156 kwargs = self._make_serializable(kwargs)
157 self._pipe[RemoteClass.PIPE_PARENT].send((op, path, args, kwargs))
158 timeout = self._timeout
159 # adjust timeout specifically for the .sleep method
160 if path is not None and path.split('.')[-1] == 'sleep':
161 if args and isinstance(args[0], (long, int)):
163 elif 'timeout' in kwargs:
164 timeout += kwargs['timeout']
165 if not self._pipe[RemoteClass.PIPE_PARENT].poll(timeout):
168 rv = self._pipe[RemoteClass.PIPE_PARENT].recv()
169 rv = self._deserialize(rv)
174 def _get_local_object(self, path):
176 Follow the path to obtain a reference on the addressed nested attribute
180 obj = getattr(obj, attr)
183 def _get_local_value(self, path):
185 return self._get_local_object(path)
186 except AttributeError:
189 def _call_local_method(self, path, *args, **kwargs):
191 method = self._get_local_object(path)
192 return method(*args, **kwargs)
193 except AttributeError:
196 def _set_local_attr(self, path, value):
198 obj = self._get_local_object(path[:-1])
199 setattr(obj, path[-1], value)
200 except AttributeError:
204 def _get_local_repr(self, path):
206 obj = self._get_local_object(path)
207 return moves.reprlib.repr(obj)
208 except AttributeError:
211 def _get_local_str(self, path):
213 obj = self._get_local_object(path)
215 except AttributeError:
218 def _serializable(self, obj):
219 """ Test if the given object is serializable """
226 def _make_obj_serializable(self, obj):
228 Make a serializable copy of an object.
229 Members which are difficult/impossible to serialize are stripped.
231 if self._serializable(obj):
232 return obj # already serializable
234 copy = SerializableClassCopy()
237 Dictionaries can hold complex values, so we split keys and values into
238 separate lists and serialize them individually.
240 if (type(obj) is dict):
241 copy.type = type(obj)
244 for k, v in obj.items():
245 copy.k_list.append(self._make_serializable(k))
246 copy.v_list.append(self._make_serializable(v))
249 # copy at least serializable attributes and properties
250 for name, member in inspect.getmembers(obj):
251 # skip private members and non-writable dunder methods.
253 if name in ['__weakref__']:
255 if name in ['__dict__']:
257 if not (name.startswith('__') and name.endswith('__')):
259 if callable(member) and not isinstance(member, property):
261 if not self._serializable(member):
262 member = self._make_serializable(member)
263 setattr(copy, name, member)
266 def _make_serializable(self, obj):
268 Make a serializable copy of an object or a list/tuple of objects.
269 Members which are difficult/impossible to serialize are stripped.
271 if (type(obj) is list) or (type(obj) is tuple):
274 rv.append(self._make_serializable(item))
275 if type(obj) is tuple:
278 elif (isinstance(obj, IntEnum) or isinstance(obj, IntFlag)):
281 return self._make_obj_serializable(obj)
283 def _deserialize_obj(self, obj):
284 if (hasattr(obj, 'type')):
287 for k, v in zip(obj.k_list, obj.v_list):
288 _obj[self._deserialize(k)] = self._deserialize(v)
292 def _deserialize(self, obj):
293 if (type(obj) is list) or (type(obj) is tuple):
296 rv.append(self._deserialize(item))
297 if type(obj) is tuple:
301 return self._deserialize_obj(obj)
303 def start_remote(self):
304 """ Start remote execution """
307 def quit_remote(self):
308 """ Quit remote execution """
309 self._remote_exec(RemoteClass.QUIT, None)
311 def get_remote_value(self):
312 """ Get value of a remotely held object """
313 return RemoteClassAttr(self, None).get_remote_value()
315 def set_request_timeout(self, timeout):
316 """ Change request timeout """
317 self._timeout = timeout
321 Create instance of the wrapped class and execute operations
322 on it as requested by the parent process.
324 self._instance = self._cls(*self._args, **self._kwargs)
328 # get request from the parent process
330 kwargs) = self._pipe[RemoteClass.PIPE_CHILD].recv()
331 args = self._deserialize(args)
332 kwargs = self._deserialize(kwargs)
333 path = path.split('.') if path else []
334 if op == RemoteClass.GET:
335 rv = self._get_local_value(path)
336 elif op == RemoteClass.CALL:
337 rv = self._call_local_method(path, *args, **kwargs)
338 elif op == RemoteClass.SETATTR and 'value' in kwargs:
339 self._set_local_attr(path, kwargs['value'])
340 elif op == RemoteClass.REPR:
341 rv = self._get_local_repr(path)
342 elif op == RemoteClass.STR:
343 rv = self._get_local_str(path)
344 elif op == RemoteClass.QUIT:
349 if not self._serializable(rv):
350 rv = self._make_serializable(rv)
351 self._pipe[RemoteClass.PIPE_CHILD].send(rv)
354 self._instance = None # destroy the instance
357 @unittest.skip("Remote Vpp Test Case Class")
358 class RemoteVppTestCase(VppTestCase):
359 """ Re-use VppTestCase to create remote VPP segment
365 # fork new process before client connects to VPP
366 cls.remote_test = RemoteClass(RemoteVppTestCase)
368 # start remote process
369 cls.remote_test.start_remote()
371 # set up your test case
372 super(MyTestCase, cls).setUpClass()
375 cls.remote_test.setUpClass(cls.tempdir)
378 def tearDownClass(cls):
379 # tear down remote test
380 cls.remote_test.tearDownClass()
382 # stop remote process
383 cls.remote_test.quit_remote()
385 # tear down your test case
386 super(MyTestCase, cls).tearDownClass()
390 super(RemoteVppTestCase, self).__init__("emptyTest")
392 # Note: __del__ is a 'Finalizer" not a 'Destructor'.
393 # https://docs.python.org/3/reference/datamodel.html#object.__del__
395 if hasattr(self, "vpp"):
397 if self.vpp.returncode is None:
399 self.vpp.communicate()
402 def setUpClass(cls, tempdir):
403 # disable features unsupported in remote VPP
404 orig_env = dict(os.environ)
405 if 'STEP' in os.environ:
406 del os.environ['STEP']
407 if 'DEBUG' in os.environ:
408 del os.environ['DEBUG']
409 cls.tempdir_prefix = os.path.basename(tempdir) + "/"
410 super(RemoteVppTestCase, cls).setUpClass()
411 os.environ = orig_env
414 def tearDownClass(cls):
415 super(RemoteVppTestCase, cls).tearDownClass()
417 @unittest.skip("Empty test")
422 def setTestFunctionInfo(self, name, doc):
424 Store the name and documentation string of currently executed test
425 in the main VPP for logging purposes.
427 self._testMethodName = name
428 self._testMethodDoc = doc