API: Cleanup APIs interface.api
[vpp.git] / test / remote_test.py
1 #!/usr/bin/env python
2
3 import inspect
4 import os
5 import unittest
6 from framework import VppTestCase
7 from multiprocessing import Process, Pipe
8 from pickle import dumps
9 import six
10 from six import moves
11 import sys
12 if sys.version_info <= (3, 4):
13     from aenum import IntEnum
14 else:
15     from enum import IntEnum
16
17 if sys.version_info <= (3, 6):
18     from aenum import IntFlag
19 else:
20     from enum import IntFlag
21
22
23 class SerializableClassCopy(object):
24     """
25     Empty class used as a basis for a serializable copy of another class.
26     """
27     pass
28
29
30 class RemoteClassAttr(object):
31     """
32     Wrapper around attribute of a remotely executed class.
33     """
34
35     def __init__(self, remote, attr):
36         self._path = [attr] if attr else []
37         self._remote = remote
38
39     def path_to_str(self):
40         return '.'.join(self._path)
41
42     def get_remote_value(self):
43         return self._remote._remote_exec(RemoteClass.GET, self.path_to_str())
44
45     def __repr__(self):
46         return self._remote._remote_exec(RemoteClass.REPR, self.path_to_str())
47
48     def __str__(self):
49         return self._remote._remote_exec(RemoteClass.STR, self.path_to_str())
50
51     def __getattr__(self, attr):
52         if attr[0] == '_':
53             if not (attr.startswith('__') and attr.endswith('__')):
54                 raise AttributeError
55         self._path.append(attr)
56         return self
57
58     def __setattr__(self, attr, val):
59         if attr[0] == '_':
60             if not (attr.startswith('__') and attr.endswith('__')):
61                 super(RemoteClassAttr, self).__setattr__(attr, val)
62                 return
63         self._path.append(attr)
64         self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(),
65                                   True, value=val)
66
67     def __call__(self, *args, **kwargs):
68         return self._remote._remote_exec(RemoteClass.CALL, self.path_to_str(),
69                                          True, *args, **kwargs)
70
71
72 class RemoteClass(Process):
73     """
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.
76     Usage:
77         # Create a remotely executed instance of MyClass
78         object = RemoteClass(MyClass, arg1='foo', arg2='bar')
79         object.start_remote()
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
91         object.quit_remote()
92         object.terminate()
93     """
94
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
101
102     PIPE_PARENT = 0  # Parent end of the pipe
103     PIPE_CHILD = 1  # Child end of the pipe
104
105     DEFAULT_TIMEOUT = 2  # default timeout for an operation to execute
106
107     def __init__(self, cls, *args, **kwargs):
108         super(RemoteClass, self).__init__()
109         self._cls = cls
110         self._args = args
111         self._kwargs = kwargs
112         self._timeout = RemoteClass.DEFAULT_TIMEOUT
113         self._pipe = Pipe()  # pipe for input/output arguments
114
115     def __repr__(self):
116         return moves.reprlib.repr(RemoteClassAttr(self, None))
117
118     def __str__(self):
119         return str(RemoteClassAttr(self, None))
120
121     def __call__(self, *args, **kwargs):
122         return self.RemoteClassAttr(self, None)()
123
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
130         return RemoteClassAttr(self, attr)
131
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)
136                 return
137         setattr(RemoteClassAttr(self, None), attr, val)
138
139     def _remote_exec(self, op, path=None, ret=True, *args, **kwargs):
140         """
141         Execute given operation on a given, possibly nested, member remotely.
142         """
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()
154         # send request
155         args = self._make_serializable(args)
156         kwargs = self._make_serializable(kwargs)
157         self._pipe[RemoteClass.PIPE_PARENT].send((op, path, args, kwargs))
158         if not ret:
159             # no return value expected
160             return None
161         timeout = self._timeout
162         # adjust timeout specifically for the .sleep method
163         if path.split('.')[-1] == 'sleep':
164             if args and isinstance(args[0], (long, int)):
165                 timeout += args[0]
166             elif 'timeout' in kwargs:
167                 timeout += kwargs['timeout']
168         if not self._pipe[RemoteClass.PIPE_PARENT].poll(timeout):
169             return None
170         try:
171             rv = self._pipe[RemoteClass.PIPE_PARENT].recv()
172             rv = self._deserialize(rv)
173             return rv
174         except EOFError:
175             return None
176
177     def _get_local_object(self, path):
178         """
179         Follow the path to obtain a reference on the addressed nested attribute
180         """
181         obj = self._instance
182         for attr in path:
183             obj = getattr(obj, attr)
184         return obj
185
186     def _get_local_value(self, path):
187         try:
188             return self._get_local_object(path)
189         except AttributeError:
190             return None
191
192     def _call_local_method(self, path, *args, **kwargs):
193         try:
194             method = self._get_local_object(path)
195             return method(*args, **kwargs)
196         except AttributeError:
197             return None
198
199     def _set_local_attr(self, path, value):
200         try:
201             obj = self._get_local_object(path[:-1])
202             setattr(obj, path[-1], value)
203         except AttributeError:
204             pass
205         return None
206
207     def _get_local_repr(self, path):
208         try:
209             obj = self._get_local_object(path)
210             return moves.reprlib.repr(obj)
211         except AttributeError:
212             return None
213
214     def _get_local_str(self, path):
215         try:
216             obj = self._get_local_object(path)
217             return str(obj)
218         except AttributeError:
219             return None
220
221     def _serializable(self, obj):
222         """ Test if the given object is serializable """
223         try:
224             dumps(obj)
225             return True
226         except:
227             return False
228
229     def _make_obj_serializable(self, obj):
230         """
231         Make a serializable copy of an object.
232         Members which are difficult/impossible to serialize are stripped.
233         """
234         if self._serializable(obj):
235             return obj  # already serializable
236
237         copy = SerializableClassCopy()
238
239         """
240         Dictionaries can hold complex values, so we split keys and values into
241         separate lists and serialize them individually.
242         """
243         if (type(obj) is dict):
244             copy.type = type(obj)
245             copy.k_list = list()
246             copy.v_list = list()
247             for k, v in obj.items():
248                 copy.k_list.append(self._make_serializable(k))
249                 copy.v_list.append(self._make_serializable(v))
250             return copy
251
252         # copy at least serializable attributes and properties
253         for name, member in inspect.getmembers(obj):
254             if name[0] == '_':  # skip private members
255                 if not (name.startswith('__') and name.endswith('__')):
256                     continue
257             if callable(member) and not isinstance(member, property):
258                 continue
259             if not self._serializable(member):
260                 continue
261             setattr(copy, name, member)
262         return copy
263
264     def _make_serializable(self, obj):
265         """
266         Make a serializable copy of an object or a list/tuple of objects.
267         Members which are difficult/impossible to serialize are stripped.
268         """
269         if (type(obj) is list) or (type(obj) is tuple):
270             rv = []
271             for item in obj:
272                 rv.append(self._make_serializable(item))
273             if type(obj) is tuple:
274                 rv = tuple(rv)
275             return rv
276         elif (isinstance(obj, IntEnum) or isinstance(obj, IntFlag)):
277             return obj.value
278         else:
279             return self._make_obj_serializable(obj)
280
281     def _deserialize_obj(self, obj):
282         if (hasattr(obj, 'type')):
283             if obj.type is dict:
284                 _obj = dict()
285                 for k, v in zip(obj.k_list, obj.v_list):
286                     _obj[self._deserialize(k)] = self._deserialize(v)
287             return _obj
288         return obj
289
290     def _deserialize(self, obj):
291         if (type(obj) is list) or (type(obj) is tuple):
292             rv = []
293             for item in obj:
294                 rv.append(self._deserialize(item))
295             if type(obj) is tuple:
296                 rv = tuple(rv)
297             return rv
298         else:
299             return self._deserialize_obj(obj)
300
301     def start_remote(self):
302         """ Start remote execution """
303         self.start()
304
305     def quit_remote(self):
306         """ Quit remote execution """
307         self._remote_exec(RemoteClass.QUIT, None, False)
308
309     def get_remote_value(self):
310         """ Get value of a remotely held object """
311         return RemoteClassAttr(self, None).get_remote_value()
312
313     def set_request_timeout(self, timeout):
314         """ Change request timeout """
315         self._timeout = timeout
316
317     def run(self):
318         """
319         Create instance of the wrapped class and execute operations
320         on it as requested by the parent process.
321         """
322         self._instance = self._cls(*self._args, **self._kwargs)
323         while True:
324             try:
325                 rv = None
326                 # get request from the parent process
327                 (op, path, args,
328                  kwargs) = self._pipe[RemoteClass.PIPE_CHILD].recv()
329                 args = self._deserialize(args)
330                 kwargs = self._deserialize(kwargs)
331                 path = path.split('.') if path else []
332                 if op == RemoteClass.GET:
333                     rv = self._get_local_value(path)
334                 elif op == RemoteClass.CALL:
335                     rv = self._call_local_method(path, *args, **kwargs)
336                 elif op == RemoteClass.SETATTR and 'value' in kwargs:
337                     self._set_local_attr(path, kwargs['value'])
338                 elif op == RemoteClass.REPR:
339                     rv = self._get_local_repr(path)
340                 elif op == RemoteClass.STR:
341                     rv = self._get_local_str(path)
342                 elif op == RemoteClass.QUIT:
343                     break
344                 else:
345                     continue
346                 # send return value
347                 if not self._serializable(rv):
348                     rv = self._make_serializable(rv)
349                 self._pipe[RemoteClass.PIPE_CHILD].send(rv)
350             except EOFError:
351                 break
352         self._instance = None  # destroy the instance
353
354
355 @unittest.skip("Remote Vpp Test Case Class")
356 class RemoteVppTestCase(VppTestCase):
357     """ Re-use VppTestCase to create remote VPP segment
358
359         In your test case:
360
361         @classmethod
362         def setUpClass(cls):
363             # fork new process before clinet connects to VPP
364             cls.remote_test = RemoteClass(RemoteVppTestCase)
365
366             # start remote process
367             cls.remote_test.start_remote()
368
369             # set up your test case
370             super(MyTestCase, cls).setUpClass()
371
372             # set up remote test
373             cls.remote_test.setUpClass(cls.tempdir)
374
375         @classmethod
376         def tearDownClass(cls):
377             # tear down remote test
378             cls.remote_test.tearDownClass()
379
380             # stop remote process
381             cls.remote_test.quit_remote()
382
383             # tear down your test case
384             super(MyTestCase, cls).tearDownClass()
385     """
386
387     def __init__(self):
388         super(RemoteVppTestCase, self).__init__("emptyTest")
389
390     # Note: __del__ is a 'Finalizer" not a 'Destructor'.
391     # https://docs.python.org/3/reference/datamodel.html#object.__del__
392     def __del__(self):
393         if hasattr(self, "vpp"):
394             self.vpp.poll()
395             if self.vpp.returncode is None:
396                 self.vpp.terminate()
397                 self.vpp.communicate()
398
399     @classmethod
400     def setUpClass(cls, tempdir):
401         # disable features unsupported in remote VPP
402         orig_env = dict(os.environ)
403         if 'STEP' in os.environ:
404             del os.environ['STEP']
405         if 'DEBUG' in os.environ:
406             del os.environ['DEBUG']
407         cls.tempdir_prefix = os.path.basename(tempdir) + "/"
408         super(RemoteVppTestCase, cls).setUpClass()
409         os.environ = orig_env
410
411     @unittest.skip("Empty test")
412     def emptyTest(self):
413         """ Do nothing """
414         pass
415
416     def setTestFunctionInfo(self, name, doc):
417         """
418         Store the name and documentation string of currently executed test
419         in the main VPP for logging purposes.
420         """
421         self._testMethodName = name
422         self._testMethodDoc = doc