6 from multiprocessing import Process, Pipe
7 from pickle import dumps
11 from framework import VppTestCase
14 class SerializableClassCopy(object):
16 Empty class used as a basis for a serializable copy of another class.
21 class RemoteClassAttr(object):
23 Wrapper around attribute of a remotely executed class.
26 def __init__(self, remote, attr):
27 self._path = [attr] if attr else []
30 def path_to_str(self):
31 return '.'.join(self._path)
33 def get_remote_value(self):
34 return self._remote._remote_exec(RemoteClass.GET, self.path_to_str())
37 return self._remote._remote_exec(RemoteClass.REPR, self.path_to_str())
40 return self._remote._remote_exec(RemoteClass.STR, self.path_to_str())
42 def __getattr__(self, attr):
45 self._path.append(attr)
48 def __setattr__(self, attr, val):
50 super(RemoteClassAttr, self).__setattr__(attr, val)
52 self._path.append(attr)
53 self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(),
56 def __call__(self, *args, **kwargs):
57 return self._remote._remote_exec(RemoteClass.CALL, self.path_to_str(),
58 True, *args, **kwargs)
61 class RemoteClass(Process):
63 This class can wrap around and adapt the interface of another class,
64 and then delegate its execution to a newly forked child process.
66 # Create a remotely executed instance of MyClass
67 object = RemoteClass(MyClass, arg1='foo', arg2='bar')
69 # Access the object normally as if it was an instance of your class.
70 object.my_attribute = 20
71 print object.my_attribute
72 print object.my_method(object.my_attribute)
73 object.my_attribute.nested_attribute = 'test'
74 # If you need the value of a remote attribute, use .get_remote_value
75 method. This method is automatically called when needed in the context
76 of a remotely executed class. E.g.:
77 if (object.my_attribute.get_remote_value() > 20):
78 object.my_attribute2 = object.my_attribute
79 # Destroy the instance
84 GET = 0 # Get attribute remotely
85 CALL = 1 # Call method remotely
86 SETATTR = 2 # Set attribute remotely
87 REPR = 3 # Get representation of a remote object
88 STR = 4 # Get string representation of a remote object
89 QUIT = 5 # Quit remote execution
91 PIPE_PARENT = 0 # Parent end of the pipe
92 PIPE_CHILD = 1 # Child end of the pipe
94 DEFAULT_TIMEOUT = 2 # default timeout for an operation to execute
96 def __init__(self, cls, *args, **kwargs):
97 super(RemoteClass, self).__init__()
100 self._kwargs = kwargs
101 self._timeout = RemoteClass.DEFAULT_TIMEOUT
102 self._pipe = Pipe() # pipe for input/output arguments
105 return six.reprlib(RemoteClassAttr(self, None))
108 return str(RemoteClassAttr(self, None))
110 def __call__(self, *args, **kwargs):
111 return self.RemoteClassAttr(self, None)()
113 def __getattr__(self, attr):
114 if attr[0] == '_' or not self.is_alive():
115 if hasattr(super(RemoteClass, self), '__getattr__'):
116 return super(RemoteClass, self).__getattr__(attr)
118 return RemoteClassAttr(self, attr)
120 def __setattr__(self, attr, val):
121 if attr[0] == '_' or not self.is_alive():
122 super(RemoteClass, self).__setattr__(attr, val)
124 setattr(RemoteClassAttr(self, None), attr, val)
126 def _remote_exec(self, op, path=None, ret=True, *args, **kwargs):
128 Execute given operation on a given, possibly nested, member remotely.
130 # automatically resolve remote objects in the arguments
131 mutable_args = list(args)
132 for i, val in enumerate(mutable_args):
133 if isinstance(val, RemoteClass) or \
134 isinstance(val, RemoteClassAttr):
135 mutable_args[i] = val.get_remote_value()
136 args = tuple(mutable_args)
137 for key, val in kwargs.iteritems():
138 if isinstance(val, RemoteClass) or \
139 isinstance(val, RemoteClassAttr):
140 kwargs[key] = val.get_remote_value()
142 args = self._make_serializable(args)
143 kwargs = self._make_serializable(kwargs)
144 self._pipe[RemoteClass.PIPE_PARENT].send((op, path, args, kwargs))
146 # no return value expected
148 timeout = self._timeout
149 # adjust timeout specifically for the .sleep method
150 if path.split('.')[-1] == 'sleep':
151 if args and isinstance(args[0], (long, int)):
153 elif 'timeout' in kwargs:
154 timeout += kwargs['timeout']
155 if not self._pipe[RemoteClass.PIPE_PARENT].poll(timeout):
158 rv = self._pipe[RemoteClass.PIPE_PARENT].recv()
159 rv = self._deserialize(rv)
164 def _get_local_object(self, path):
166 Follow the path to obtain a reference on the addressed nested attribute
170 obj = getattr(obj, attr)
173 def _get_local_value(self, path):
175 return self._get_local_object(path)
176 except AttributeError:
179 def _call_local_method(self, path, *args, **kwargs):
181 method = self._get_local_object(path)
182 return method(*args, **kwargs)
183 except AttributeError:
186 def _set_local_attr(self, path, value):
188 obj = self._get_local_object(path[:-1])
189 setattr(obj, path[-1], value)
190 except AttributeError:
194 def _get_local_repr(self, path):
196 obj = self._get_local_object(path)
197 return six.reprlib(obj)
198 except AttributeError:
201 def _get_local_str(self, path):
203 obj = self._get_local_object(path)
205 except AttributeError:
208 def _serializable(self, obj):
209 """ Test if the given object is serializable """
216 def _make_obj_serializable(self, obj):
218 Make a serializable copy of an object.
219 Members which are difficult/impossible to serialize are stripped.
221 if self._serializable(obj):
222 return obj # already serializable
223 copy = SerializableClassCopy()
224 # copy at least serializable attributes and properties
225 for name, member in inspect.getmembers(obj):
226 if name[0] == '_': # skip private members
228 if callable(member) and not isinstance(member, property):
230 if not self._serializable(member):
232 setattr(copy, name, member)
235 def _make_serializable(self, obj):
237 Make a serializable copy of an object or a list/tuple of objects.
238 Members which are difficult/impossible to serialize are stripped.
240 if (type(obj) is list) or (type(obj) is tuple):
243 rv.append(self._make_serializable(item))
244 if type(obj) is tuple:
248 return self._make_obj_serializable(obj)
250 def _deserialize_obj(self, obj):
253 def _deserialize(self, obj):
254 if (type(obj) is list) or (type(obj) is tuple):
257 rv.append(self._deserialize(item))
258 if type(obj) is tuple:
262 return self._deserialize_obj(obj)
264 def start_remote(self):
265 """ Start remote execution """
268 def quit_remote(self):
269 """ Quit remote execution """
270 self._remote_exec(RemoteClass.QUIT, None, False)
272 def get_remote_value(self):
273 """ Get value of a remotely held object """
274 return RemoteClassAttr(self, None).get_remote_value()
276 def set_request_timeout(self, timeout):
277 """ Change request timeout """
278 self._timeout = timeout
282 Create instance of the wrapped class and execute operations
283 on it as requested by the parent process.
285 self._instance = self._cls(*self._args, **self._kwargs)
289 # get request from the parent process
291 kwargs) = self._pipe[RemoteClass.PIPE_CHILD].recv()
292 args = self._deserialize(args)
293 kwargs = self._deserialize(kwargs)
294 path = path.split('.') if path else []
295 if op == RemoteClass.GET:
296 rv = self._get_local_value(path)
297 elif op == RemoteClass.CALL:
298 rv = self._call_local_method(path, *args, **kwargs)
299 elif op == RemoteClass.SETATTR and 'value' in kwargs:
300 self._set_local_attr(path, kwargs['value'])
301 elif op == RemoteClass.REPR:
302 rv = self._get_local_repr(path)
303 elif op == RemoteClass.STR:
304 rv = self._get_local_str(path)
305 elif op == RemoteClass.QUIT:
310 if not self._serializable(rv):
311 rv = self._make_serializable(rv)
312 self._pipe[RemoteClass.PIPE_CHILD].send(rv)
315 self._instance = None # destroy the instance
318 @unittest.skip("Remote Vpp Test Case Class")
319 class RemoteVppTestCase(VppTestCase):
320 """ Re-use VppTestCase to create remote VPP segment
326 # fork new process before clinet connects to VPP
327 cls.remote_test = RemoteClass(RemoteVppTestCase)
329 # start remote process
330 cls.remote_test.start_remote()
332 # set up your test case
333 super(MyTestCase, cls).setUpClass()
336 cls.remote_test.setUpClass(cls.tempdir)
339 def tearDownClass(cls):
340 # tear down remote test
341 cls.remote_test.tearDownClass()
343 # stop remote process
344 cls.remote_test.quit_remote()
346 # tear down your test case
347 super(MyTestCase, cls).tearDownClass()
351 super(RemoteVppTestCase, self).__init__("emptyTest")
354 if hasattr(self, "vpp"):
356 if cls.vpp.returncode is None:
358 cls.vpp.communicate()
361 def setUpClass(cls, tempdir):
362 # disable features unsupported in remote VPP
363 orig_env = dict(os.environ)
364 if 'STEP' in os.environ:
365 del os.environ['STEP']
366 if 'DEBUG' in os.environ:
367 del os.environ['DEBUG']
368 cls.tempdir_prefix = os.path.basename(tempdir) + "/"
369 super(RemoteVppTestCase, cls).setUpClass()
370 os.environ = orig_env
372 @unittest.skip("Empty test")
377 def setTestFunctionInfo(self, name, doc):
379 Store the name and documentation string of currently executed test
380 in the main VPP for logging purposes.
382 self._testMethodName = name
383 self._testMethodDoc = doc