6 from multiprocessing import Process, Pipe
7 from pickle import dumps
12 from framework import VppTestCase
15 class SerializableClassCopy(object):
17 Empty class used as a basis for a serializable copy of another class.
22 class RemoteClassAttr(object):
24 Wrapper around attribute of a remotely executed class.
27 def __init__(self, remote, attr):
28 self._path = [attr] if attr else []
31 def path_to_str(self):
32 return '.'.join(self._path)
34 def get_remote_value(self):
35 return self._remote._remote_exec(RemoteClass.GET, self.path_to_str())
38 return self._remote._remote_exec(RemoteClass.REPR, self.path_to_str())
41 return self._remote._remote_exec(RemoteClass.STR, self.path_to_str())
43 def __getattr__(self, attr):
46 self._path.append(attr)
49 def __setattr__(self, attr, val):
51 super(RemoteClassAttr, self).__setattr__(attr, val)
53 self._path.append(attr)
54 self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(),
57 def __call__(self, *args, **kwargs):
58 return self._remote._remote_exec(RemoteClass.CALL, self.path_to_str(),
59 True, *args, **kwargs)
62 class RemoteClass(Process):
64 This class can wrap around and adapt the interface of another class,
65 and then delegate its execution to a newly forked child process.
67 # Create a remotely executed instance of MyClass
68 object = RemoteClass(MyClass, arg1='foo', arg2='bar')
70 # Access the object normally as if it was an instance of your class.
71 object.my_attribute = 20
72 print object.my_attribute
73 print object.my_method(object.my_attribute)
74 object.my_attribute.nested_attribute = 'test'
75 # If you need the value of a remote attribute, use .get_remote_value
76 method. This method is automatically called when needed in the context
77 of a remotely executed class. E.g.:
78 if (object.my_attribute.get_remote_value() > 20):
79 object.my_attribute2 = object.my_attribute
80 # Destroy the instance
85 GET = 0 # Get attribute remotely
86 CALL = 1 # Call method remotely
87 SETATTR = 2 # Set attribute remotely
88 REPR = 3 # Get representation of a remote object
89 STR = 4 # Get string representation of a remote object
90 QUIT = 5 # Quit remote execution
92 PIPE_PARENT = 0 # Parent end of the pipe
93 PIPE_CHILD = 1 # Child end of the pipe
95 DEFAULT_TIMEOUT = 2 # default timeout for an operation to execute
97 def __init__(self, cls, *args, **kwargs):
98 super(RemoteClass, self).__init__()
101 self._kwargs = kwargs
102 self._timeout = RemoteClass.DEFAULT_TIMEOUT
103 self._pipe = Pipe() # pipe for input/output arguments
106 return moves.reprlib.repr(RemoteClassAttr(self, None))
109 return str(RemoteClassAttr(self, None))
111 def __call__(self, *args, **kwargs):
112 return self.RemoteClassAttr(self, None)()
114 def __getattr__(self, attr):
115 if attr[0] == '_' or not self.is_alive():
116 if hasattr(super(RemoteClass, self), '__getattr__'):
117 return super(RemoteClass, self).__getattr__(attr)
119 return RemoteClassAttr(self, attr)
121 def __setattr__(self, attr, val):
122 if attr[0] == '_' or not self.is_alive():
123 super(RemoteClass, self).__setattr__(attr, val)
125 setattr(RemoteClassAttr(self, None), attr, val)
127 def _remote_exec(self, op, path=None, ret=True, *args, **kwargs):
129 Execute given operation on a given, possibly nested, member remotely.
131 # automatically resolve remote objects in the arguments
132 mutable_args = list(args)
133 for i, val in enumerate(mutable_args):
134 if isinstance(val, RemoteClass) or \
135 isinstance(val, RemoteClassAttr):
136 mutable_args[i] = val.get_remote_value()
137 args = tuple(mutable_args)
138 for key, val in six.iteritems(kwargs):
139 if isinstance(val, RemoteClass) or \
140 isinstance(val, RemoteClassAttr):
141 kwargs[key] = val.get_remote_value()
143 args = self._make_serializable(args)
144 kwargs = self._make_serializable(kwargs)
145 self._pipe[RemoteClass.PIPE_PARENT].send((op, path, args, kwargs))
147 # no return value expected
149 timeout = self._timeout
150 # adjust timeout specifically for the .sleep method
151 if path.split('.')[-1] == 'sleep':
152 if args and isinstance(args[0], (long, int)):
154 elif 'timeout' in kwargs:
155 timeout += kwargs['timeout']
156 if not self._pipe[RemoteClass.PIPE_PARENT].poll(timeout):
159 rv = self._pipe[RemoteClass.PIPE_PARENT].recv()
160 rv = self._deserialize(rv)
165 def _get_local_object(self, path):
167 Follow the path to obtain a reference on the addressed nested attribute
171 obj = getattr(obj, attr)
174 def _get_local_value(self, path):
176 return self._get_local_object(path)
177 except AttributeError:
180 def _call_local_method(self, path, *args, **kwargs):
182 method = self._get_local_object(path)
183 return method(*args, **kwargs)
184 except AttributeError:
187 def _set_local_attr(self, path, value):
189 obj = self._get_local_object(path[:-1])
190 setattr(obj, path[-1], value)
191 except AttributeError:
195 def _get_local_repr(self, path):
197 obj = self._get_local_object(path)
198 return moves.reprlib.repr(obj)
199 except AttributeError:
202 def _get_local_str(self, path):
204 obj = self._get_local_object(path)
206 except AttributeError:
209 def _serializable(self, obj):
210 """ Test if the given object is serializable """
217 def _make_obj_serializable(self, obj):
219 Make a serializable copy of an object.
220 Members which are difficult/impossible to serialize are stripped.
222 if self._serializable(obj):
223 return obj # already serializable
224 copy = SerializableClassCopy()
225 # copy at least serializable attributes and properties
226 for name, member in inspect.getmembers(obj):
227 if name[0] == '_': # skip private members
229 if callable(member) and not isinstance(member, property):
231 if not self._serializable(member):
233 setattr(copy, name, member)
236 def _make_serializable(self, obj):
238 Make a serializable copy of an object or a list/tuple of objects.
239 Members which are difficult/impossible to serialize are stripped.
241 if (type(obj) is list) or (type(obj) is tuple):
244 rv.append(self._make_serializable(item))
245 if type(obj) is tuple:
249 return self._make_obj_serializable(obj)
251 def _deserialize_obj(self, obj):
254 def _deserialize(self, obj):
255 if (type(obj) is list) or (type(obj) is tuple):
258 rv.append(self._deserialize(item))
259 if type(obj) is tuple:
263 return self._deserialize_obj(obj)
265 def start_remote(self):
266 """ Start remote execution """
269 def quit_remote(self):
270 """ Quit remote execution """
271 self._remote_exec(RemoteClass.QUIT, None, False)
273 def get_remote_value(self):
274 """ Get value of a remotely held object """
275 return RemoteClassAttr(self, None).get_remote_value()
277 def set_request_timeout(self, timeout):
278 """ Change request timeout """
279 self._timeout = timeout
283 Create instance of the wrapped class and execute operations
284 on it as requested by the parent process.
286 self._instance = self._cls(*self._args, **self._kwargs)
290 # get request from the parent process
292 kwargs) = self._pipe[RemoteClass.PIPE_CHILD].recv()
293 args = self._deserialize(args)
294 kwargs = self._deserialize(kwargs)
295 path = path.split('.') if path else []
296 if op == RemoteClass.GET:
297 rv = self._get_local_value(path)
298 elif op == RemoteClass.CALL:
299 rv = self._call_local_method(path, *args, **kwargs)
300 elif op == RemoteClass.SETATTR and 'value' in kwargs:
301 self._set_local_attr(path, kwargs['value'])
302 elif op == RemoteClass.REPR:
303 rv = self._get_local_repr(path)
304 elif op == RemoteClass.STR:
305 rv = self._get_local_str(path)
306 elif op == RemoteClass.QUIT:
311 if not self._serializable(rv):
312 rv = self._make_serializable(rv)
313 self._pipe[RemoteClass.PIPE_CHILD].send(rv)
316 self._instance = None # destroy the instance
319 @unittest.skip("Remote Vpp Test Case Class")
320 class RemoteVppTestCase(VppTestCase):
321 """ Re-use VppTestCase to create remote VPP segment
327 # fork new process before clinet connects to VPP
328 cls.remote_test = RemoteClass(RemoteVppTestCase)
330 # start remote process
331 cls.remote_test.start_remote()
333 # set up your test case
334 super(MyTestCase, cls).setUpClass()
337 cls.remote_test.setUpClass(cls.tempdir)
340 def tearDownClass(cls):
341 # tear down remote test
342 cls.remote_test.tearDownClass()
344 # stop remote process
345 cls.remote_test.quit_remote()
347 # tear down your test case
348 super(MyTestCase, cls).tearDownClass()
352 super(RemoteVppTestCase, self).__init__("emptyTest")
355 if hasattr(self, "vpp"):
357 if cls.vpp.returncode is None:
359 cls.vpp.communicate()
362 def setUpClass(cls, tempdir):
363 # disable features unsupported in remote VPP
364 orig_env = dict(os.environ)
365 if 'STEP' in os.environ:
366 del os.environ['STEP']
367 if 'DEBUG' in os.environ:
368 del os.environ['DEBUG']
369 cls.tempdir_prefix = os.path.basename(tempdir) + "/"
370 super(RemoteVppTestCase, cls).setUpClass()
371 os.environ = orig_env
373 @unittest.skip("Empty test")
378 def setTestFunctionInfo(self, name, doc):
380 Store the name and documentation string of currently executed test
381 in the main VPP for logging purposes.
383 self._testMethodName = name
384 self._testMethodDoc = doc