6 from multiprocessing import Process, Pipe
7 from pickle import dumps, PicklingError
8 from framework import VppTestCase
11 class SerializableClassCopy(object):
13 Empty class used as a basis for a serializable copy of another class.
18 class RemoteClassAttr(object):
20 Wrapper around attribute of a remotely executed class.
23 def __init__(self, remote, attr):
24 self._path = [attr] if attr else []
27 def path_to_str(self):
28 return '.'.join(self._path)
30 def get_remote_value(self):
31 return self._remote._remote_exec(RemoteClass.GET, self.path_to_str())
34 return self._remote._remote_exec(RemoteClass.REPR, self.path_to_str())
37 return self._remote._remote_exec(RemoteClass.STR, self.path_to_str())
39 def __getattr__(self, attr):
42 self._path.append(attr)
45 def __setattr__(self, attr, val):
47 super(RemoteClassAttr, self).__setattr__(attr, val)
49 self._path.append(attr)
50 self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(),
53 def __call__(self, *args, **kwargs):
54 return self._remote._remote_exec(RemoteClass.CALL, self.path_to_str(),
55 True, *args, **kwargs)
58 class RemoteClass(Process):
60 This class can wrap around and adapt the interface of another class,
61 and then delegate its execution to a newly forked child process.
63 # Create a remotely executed instance of MyClass
64 object = RemoteClass(MyClass, arg1='foo', arg2='bar')
66 # Access the object normally as if it was an instance of your class.
67 object.my_attribute = 20
68 print object.my_attribute
69 print object.my_method(object.my_attribute)
70 object.my_attribute.nested_attribute = 'test'
71 # If you need the value of a remote attribute, use .get_remote_value
72 method. This method is automatically called when needed in the context
73 of a remotely executed class. E.g.:
74 if (object.my_attribute.get_remote_value() > 20):
75 object.my_attribute2 = object.my_attribute
76 # Destroy the instance
81 GET = 0 # Get attribute remotely
82 CALL = 1 # Call method remotely
83 SETATTR = 2 # Set attribute remotely
84 REPR = 3 # Get representation of a remote object
85 STR = 4 # Get string representation of a remote object
86 QUIT = 5 # Quit remote execution
88 PIPE_PARENT = 0 # Parent end of the pipe
89 PIPE_CHILD = 1 # Child end of the pipe
91 DEFAULT_TIMEOUT = 2 # default timeout for an operation to execute
93 def __init__(self, cls, *args, **kwargs):
94 super(RemoteClass, self).__init__()
98 self._timeout = RemoteClass.DEFAULT_TIMEOUT
99 self._pipe = Pipe() # pipe for input/output arguments
102 return repr(RemoteClassAttr(self, None))
105 return str(RemoteClassAttr(self, None))
107 def __call__(self, *args, **kwargs):
108 return self.RemoteClassAttr(self, None)()
110 def __getattr__(self, attr):
111 if attr[0] == '_' or not self.is_alive():
112 if hasattr(super(RemoteClass, self), '__getattr__'):
113 return super(RemoteClass, self).__getattr__(attr)
115 return RemoteClassAttr(self, attr)
117 def __setattr__(self, attr, val):
118 if attr[0] == '_' or not self.is_alive():
119 super(RemoteClass, self).__setattr__(attr, val)
121 setattr(RemoteClassAttr(self, None), attr, val)
123 def _remote_exec(self, op, path=None, ret=True, *args, **kwargs):
125 Execute given operation on a given, possibly nested, member remotely.
127 # automatically resolve remote objects in the arguments
128 mutable_args = list(args)
129 for i, val in enumerate(mutable_args):
130 if isinstance(val, RemoteClass) or \
131 isinstance(val, RemoteClassAttr):
132 mutable_args[i] = val.get_remote_value()
133 args = tuple(mutable_args)
134 for key, val in kwargs.iteritems():
135 if isinstance(val, RemoteClass) or \
136 isinstance(val, RemoteClassAttr):
137 kwargs[key] = val.get_remote_value()
139 args = self._make_serializable(args)
140 kwargs = self._make_serializable(kwargs)
141 self._pipe[RemoteClass.PIPE_PARENT].send((op, path, args, kwargs))
143 # no return value expected
145 timeout = self._timeout
146 # adjust timeout specifically for the .sleep method
147 if path.split('.')[-1] == 'sleep':
148 if args and isinstance(args[0], (long, int)):
150 elif 'timeout' in kwargs:
151 timeout += kwargs['timeout']
152 if not self._pipe[RemoteClass.PIPE_PARENT].poll(timeout):
155 rv = self._pipe[RemoteClass.PIPE_PARENT].recv()
156 rv = self._deserialize(rv)
161 def _get_local_object(self, path):
163 Follow the path to obtain a reference on the addressed nested attribute
167 obj = getattr(obj, attr)
170 def _get_local_value(self, path):
172 return self._get_local_object(path)
173 except AttributeError:
176 def _call_local_method(self, path, *args, **kwargs):
178 method = self._get_local_object(path)
179 return method(*args, **kwargs)
180 except AttributeError:
183 def _set_local_attr(self, path, value):
185 obj = self._get_local_object(path[:-1])
186 setattr(obj, path[-1], value)
187 except AttributeError:
191 def _get_local_repr(self, path):
193 obj = self._get_local_object(path)
195 except AttributeError:
198 def _get_local_str(self, path):
200 obj = self._get_local_object(path)
202 except AttributeError:
205 def _serializable(self, obj):
206 """ Test if the given object is serializable """
213 def _make_obj_serializable(self, obj):
215 Make a serializable copy of an object.
216 Members which are difficult/impossible to serialize are stripped.
218 if self._serializable(obj):
219 return obj # already serializable
220 copy = SerializableClassCopy()
221 # copy at least serializable attributes and properties
222 for name, member in inspect.getmembers(obj):
223 if name[0] == '_': # skip private members
225 if callable(member) and not isinstance(member, property):
227 if not self._serializable(member):
229 setattr(copy, name, member)
232 def _make_serializable(self, obj):
234 Make a serializable copy of an object or a list/tuple of objects.
235 Members which are difficult/impossible to serialize are stripped.
237 if (type(obj) is list) or (type(obj) is tuple):
240 rv.append(self._make_serializable(item))
241 if type(obj) is tuple:
245 return self._make_obj_serializable(obj)
247 def _deserialize_obj(self, obj):
250 def _deserialize(self, obj):
251 if (type(obj) is list) or (type(obj) is tuple):
254 rv.append(self._deserialize(item))
255 if type(obj) is tuple:
259 return self._deserialize_obj(obj)
261 def start_remote(self):
262 """ Start remote execution """
265 def quit_remote(self):
266 """ Quit remote execution """
267 self._remote_exec(RemoteClass.QUIT, None, False)
269 def get_remote_value(self):
270 """ Get value of a remotely held object """
271 return RemoteClassAttr(self, None).get_remote_value()
273 def set_request_timeout(self, timeout):
274 """ Change request timeout """
275 self._timeout = timeout
279 Create instance of the wrapped class and execute operations
280 on it as requested by the parent process.
282 self._instance = self._cls(*self._args, **self._kwargs)
286 # get request from the parent process
288 kwargs) = self._pipe[RemoteClass.PIPE_CHILD].recv()
289 args = self._deserialize(args)
290 kwargs = self._deserialize(kwargs)
291 path = path.split('.') if path else []
292 if op == RemoteClass.GET:
293 rv = self._get_local_value(path)
294 elif op == RemoteClass.CALL:
295 rv = self._call_local_method(path, *args, **kwargs)
296 elif op == RemoteClass.SETATTR and 'value' in kwargs:
297 self._set_local_attr(path, kwargs['value'])
298 elif op == RemoteClass.REPR:
299 rv = self._get_local_repr(path)
300 elif op == RemoteClass.STR:
301 rv = self._get_local_str(path)
302 elif op == RemoteClass.QUIT:
307 if not self._serializable(rv):
308 rv = self._make_serializable(rv)
309 self._pipe[RemoteClass.PIPE_CHILD].send(rv)
312 self._instance = None # destroy the instance
315 @unittest.skip("Remote Vpp Test Case Class")
316 class RemoteVppTestCase(VppTestCase):
317 """ Re-use VppTestCase to create remote VPP segment
323 # fork new process before clinet connects to VPP
324 cls.remote_test = RemoteClass(RemoteVppTestCase)
326 # start remote process
327 cls.remote_test.start_remote()
329 # set up your test case
330 super(MyTestCase, cls).setUpClass()
333 cls.remote_test.setUpClass(cls.tempdir)
336 def tearDownClass(cls):
337 # tear down remote test
338 cls.remote_test.tearDownClass()
340 # stop remote process
341 cls.remote_test.quit_remote()
343 # tear down your test case
344 super(MyTestCase, cls).tearDownClass()
348 super(RemoteVppTestCase, self).__init__("emptyTest")
351 if hasattr(self, "vpp"):
353 if cls.vpp.returncode is None:
355 cls.vpp.communicate()
358 def setUpClass(cls, tempdir):
359 # disable features unsupported in remote VPP
360 orig_env = dict(os.environ)
361 if 'STEP' in os.environ:
362 del os.environ['STEP']
363 if 'DEBUG' in os.environ:
364 del os.environ['DEBUG']
365 cls.tempdir_prefix = os.path.basename(tempdir) + "/"
366 super(RemoteVppTestCase, cls).setUpClass()
367 os.environ = orig_env
369 @unittest.skip("Empty test")
374 def setTestFunctionInfo(self, name, doc):
376 Store the name and documentation string of currently executed test
377 in the main VPP for logging purposes.
379 self._testMethodName = name
380 self._testMethodDoc = doc