6 from multiprocessing import Process, Pipe
7 from pickle import dumps
12 from framework import VppTestCase
16 class SerializableClassCopy(object):
18 Empty class used as a basis for a serializable copy of another class.
23 class RemoteClassAttr(object):
25 Wrapper around attribute of a remotely executed class.
28 def __init__(self, remote, attr):
29 self._path = [attr] if attr else []
32 def path_to_str(self):
33 return '.'.join(self._path)
35 def get_remote_value(self):
36 return self._remote._remote_exec(RemoteClass.GET, self.path_to_str())
39 return self._remote._remote_exec(RemoteClass.REPR, self.path_to_str())
42 return self._remote._remote_exec(RemoteClass.STR, self.path_to_str())
44 def __getattr__(self, attr):
47 self._path.append(attr)
50 def __setattr__(self, attr, val):
52 super(RemoteClassAttr, self).__setattr__(attr, val)
54 self._path.append(attr)
55 self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(),
58 def __call__(self, *args, **kwargs):
59 return self._remote._remote_exec(RemoteClass.CALL, self.path_to_str(),
60 True, *args, **kwargs)
63 class RemoteClass(Process):
65 This class can wrap around and adapt the interface of another class,
66 and then delegate its execution to a newly forked child process.
68 # Create a remotely executed instance of MyClass
69 object = RemoteClass(MyClass, arg1='foo', arg2='bar')
71 # Access the object normally as if it was an instance of your class.
72 object.my_attribute = 20
73 print object.my_attribute
74 print object.my_method(object.my_attribute)
75 object.my_attribute.nested_attribute = 'test'
76 # If you need the value of a remote attribute, use .get_remote_value
77 method. This method is automatically called when needed in the context
78 of a remotely executed class. E.g.:
79 if (object.my_attribute.get_remote_value() > 20):
80 object.my_attribute2 = object.my_attribute
81 # Destroy the instance
86 GET = 0 # Get attribute remotely
87 CALL = 1 # Call method remotely
88 SETATTR = 2 # Set attribute remotely
89 REPR = 3 # Get representation of a remote object
90 STR = 4 # Get string representation of a remote object
91 QUIT = 5 # Quit remote execution
93 PIPE_PARENT = 0 # Parent end of the pipe
94 PIPE_CHILD = 1 # Child end of the pipe
96 DEFAULT_TIMEOUT = 2 # default timeout for an operation to execute
98 def __init__(self, cls, *args, **kwargs):
99 super(RemoteClass, self).__init__()
102 self._kwargs = kwargs
103 self._timeout = RemoteClass.DEFAULT_TIMEOUT
104 self._pipe = Pipe() # pipe for input/output arguments
107 return moves.reprlib.repr(RemoteClassAttr(self, None))
110 return str(RemoteClassAttr(self, None))
112 def __call__(self, *args, **kwargs):
113 return self.RemoteClassAttr(self, None)()
115 def __getattr__(self, attr):
116 if attr[0] == '_' or not self.is_alive():
117 if hasattr(super(RemoteClass, self), '__getattr__'):
118 return super(RemoteClass, self).__getattr__(attr)
120 return RemoteClassAttr(self, attr)
122 def __setattr__(self, attr, val):
123 if attr[0] == '_' or not self.is_alive():
124 super(RemoteClass, self).__setattr__(attr, val)
126 setattr(RemoteClassAttr(self, None), attr, val)
128 def _remote_exec(self, op, path=None, ret=True, *args, **kwargs):
130 Execute given operation on a given, possibly nested, member remotely.
132 # automatically resolve remote objects in the arguments
133 mutable_args = list(args)
134 for i, val in enumerate(mutable_args):
135 if isinstance(val, RemoteClass) or \
136 isinstance(val, RemoteClassAttr):
137 mutable_args[i] = val.get_remote_value()
138 args = tuple(mutable_args)
139 for key, val in six.iteritems(kwargs):
140 if isinstance(val, RemoteClass) or \
141 isinstance(val, RemoteClassAttr):
142 kwargs[key] = val.get_remote_value()
144 args = self._make_serializable(args)
145 kwargs = self._make_serializable(kwargs)
146 self._pipe[RemoteClass.PIPE_PARENT].send((op, path, args, kwargs))
148 # no return value expected
150 timeout = self._timeout
151 # adjust timeout specifically for the .sleep method
152 if path.split('.')[-1] == 'sleep':
153 if args and isinstance(args[0], (long, int)):
155 elif 'timeout' in kwargs:
156 timeout += kwargs['timeout']
157 if not self._pipe[RemoteClass.PIPE_PARENT].poll(timeout):
160 rv = self._pipe[RemoteClass.PIPE_PARENT].recv()
161 rv = self._deserialize(rv)
166 def _get_local_object(self, path):
168 Follow the path to obtain a reference on the addressed nested attribute
172 obj = getattr(obj, attr)
175 def _get_local_value(self, path):
177 return self._get_local_object(path)
178 except AttributeError:
181 def _call_local_method(self, path, *args, **kwargs):
183 method = self._get_local_object(path)
184 return method(*args, **kwargs)
185 except AttributeError:
188 def _set_local_attr(self, path, value):
190 obj = self._get_local_object(path[:-1])
191 setattr(obj, path[-1], value)
192 except AttributeError:
196 def _get_local_repr(self, path):
198 obj = self._get_local_object(path)
199 return moves.reprlib.repr(obj)
200 except AttributeError:
203 def _get_local_str(self, path):
205 obj = self._get_local_object(path)
207 except AttributeError:
210 def _serializable(self, obj):
211 """ Test if the given object is serializable """
218 def _make_obj_serializable(self, obj):
220 Make a serializable copy of an object.
221 Members which are difficult/impossible to serialize are stripped.
223 if self._serializable(obj):
224 return obj # already serializable
226 copy = SerializableClassCopy()
229 Dictionaries can hold complex values, so we split keys and values into
230 separate lists and serialize them individually.
232 if (type(obj) is dict):
233 copy.type = type(obj)
236 for k, v in obj.items():
237 copy.k_list.append(self._make_serializable(k))
238 copy.v_list.append(self._make_serializable(v))
241 # copy at least serializable attributes and properties
242 for name, member in inspect.getmembers(obj):
243 if name[0] == '_': # skip private members
245 if callable(member) and not isinstance(member, property):
247 if not self._serializable(member):
249 setattr(copy, name, member)
252 def _make_serializable(self, obj):
254 Make a serializable copy of an object or a list/tuple of objects.
255 Members which are difficult/impossible to serialize are stripped.
257 if (type(obj) is list) or (type(obj) is tuple):
260 rv.append(self._make_serializable(item))
261 if type(obj) is tuple:
264 elif (isinstance(obj, Enum)):
267 return self._make_obj_serializable(obj)
269 def _deserialize_obj(self, obj):
270 if (hasattr(obj, 'type')):
273 for k, v in zip(obj.k_list, obj.v_list):
274 _obj[self._deserialize(k)] = self._deserialize(v)
278 def _deserialize(self, obj):
279 if (type(obj) is list) or (type(obj) is tuple):
282 rv.append(self._deserialize(item))
283 if type(obj) is tuple:
287 return self._deserialize_obj(obj)
289 def start_remote(self):
290 """ Start remote execution """
293 def quit_remote(self):
294 """ Quit remote execution """
295 self._remote_exec(RemoteClass.QUIT, None, False)
297 def get_remote_value(self):
298 """ Get value of a remotely held object """
299 return RemoteClassAttr(self, None).get_remote_value()
301 def set_request_timeout(self, timeout):
302 """ Change request timeout """
303 self._timeout = timeout
307 Create instance of the wrapped class and execute operations
308 on it as requested by the parent process.
310 self._instance = self._cls(*self._args, **self._kwargs)
314 # get request from the parent process
316 kwargs) = self._pipe[RemoteClass.PIPE_CHILD].recv()
317 args = self._deserialize(args)
318 kwargs = self._deserialize(kwargs)
319 path = path.split('.') if path else []
320 if op == RemoteClass.GET:
321 rv = self._get_local_value(path)
322 elif op == RemoteClass.CALL:
323 rv = self._call_local_method(path, *args, **kwargs)
324 elif op == RemoteClass.SETATTR and 'value' in kwargs:
325 self._set_local_attr(path, kwargs['value'])
326 elif op == RemoteClass.REPR:
327 rv = self._get_local_repr(path)
328 elif op == RemoteClass.STR:
329 rv = self._get_local_str(path)
330 elif op == RemoteClass.QUIT:
335 if not self._serializable(rv):
336 rv = self._make_serializable(rv)
337 self._pipe[RemoteClass.PIPE_CHILD].send(rv)
340 self._instance = None # destroy the instance
343 @unittest.skip("Remote Vpp Test Case Class")
344 class RemoteVppTestCase(VppTestCase):
345 """ Re-use VppTestCase to create remote VPP segment
351 # fork new process before clinet connects to VPP
352 cls.remote_test = RemoteClass(RemoteVppTestCase)
354 # start remote process
355 cls.remote_test.start_remote()
357 # set up your test case
358 super(MyTestCase, cls).setUpClass()
361 cls.remote_test.setUpClass(cls.tempdir)
364 def tearDownClass(cls):
365 # tear down remote test
366 cls.remote_test.tearDownClass()
368 # stop remote process
369 cls.remote_test.quit_remote()
371 # tear down your test case
372 super(MyTestCase, cls).tearDownClass()
376 super(RemoteVppTestCase, self).__init__("emptyTest")
379 if hasattr(self, "vpp"):
381 if cls.vpp.returncode is None:
383 cls.vpp.communicate()
386 def setUpClass(cls, tempdir):
387 # disable features unsupported in remote VPP
388 orig_env = dict(os.environ)
389 if 'STEP' in os.environ:
390 del os.environ['STEP']
391 if 'DEBUG' in os.environ:
392 del os.environ['DEBUG']
393 cls.tempdir_prefix = os.path.basename(tempdir) + "/"
394 super(RemoteVppTestCase, cls).setUpClass()
395 os.environ = orig_env
397 @unittest.skip("Empty test")
402 def setTestFunctionInfo(self, name, doc):
404 Store the name and documentation string of currently executed test
405 in the main VPP for logging purposes.
407 self._testMethodName = name
408 self._testMethodDoc = doc