blob: 134d90ffa834edb493aae8ccd73a93ce5e8a7673 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02002
Paul Vinciguerra00671cf2018-11-25 12:47:04 -08003import inspect
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02004import os
Paul Vinciguerra090096b2020-12-03 00:42:46 -05005import reprlib
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02006import unittest
Jakub Grajciar053204a2019-03-18 13:17:53 +01007from framework import VppTestCase
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02008from multiprocessing import Process, Pipe
Paul Vinciguerra00671cf2018-11-25 12:47:04 -08009from pickle import dumps
Jakub Grajciar053204a2019-03-18 13:17:53 +010010import sys
snaramred6df3ac2019-08-29 18:00:26 +000011
Paul Vinciguerra27860dd2020-12-03 00:46:03 -050012from enum import IntEnum, IntFlag
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020013
14
Paul Vinciguerrae061dad2020-12-04 14:57:51 -050015class SerializableClassCopy:
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020016 """
17 Empty class used as a basis for a serializable copy of another class.
18 """
19 pass
20
Neale Ranns097fa662018-05-01 05:17:55 -070021 def __repr__(self):
22 return '<SerializableClassCopy dict=%s>' % self.__dict__
23
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020024
Paul Vinciguerrae061dad2020-12-04 14:57:51 -050025class RemoteClassAttr:
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020026 """
27 Wrapper around attribute of a remotely executed class.
28 """
29
30 def __init__(self, remote, attr):
31 self._path = [attr] if attr else []
32 self._remote = remote
33
34 def path_to_str(self):
35 return '.'.join(self._path)
36
37 def get_remote_value(self):
38 return self._remote._remote_exec(RemoteClass.GET, self.path_to_str())
39
40 def __repr__(self):
41 return self._remote._remote_exec(RemoteClass.REPR, self.path_to_str())
42
43 def __str__(self):
44 return self._remote._remote_exec(RemoteClass.STR, self.path_to_str())
45
46 def __getattr__(self, attr):
47 if attr[0] == '_':
Paul Vinciguerrad3a9be22019-03-10 07:03:22 -070048 if not (attr.startswith('__') and attr.endswith('__')):
Neale Ranns097fa662018-05-01 05:17:55 -070049 raise AttributeError('tried to get private attribute: %s ',
50 attr)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020051 self._path.append(attr)
52 return self
53
54 def __setattr__(self, attr, val):
55 if attr[0] == '_':
Paul Vinciguerrad3a9be22019-03-10 07:03:22 -070056 if not (attr.startswith('__') and attr.endswith('__')):
57 super(RemoteClassAttr, self).__setattr__(attr, val)
58 return
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020059 self._path.append(attr)
60 self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(),
Jakub Grajciar7db35de2019-06-25 10:22:11 +020061 value=val)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020062
63 def __call__(self, *args, **kwargs):
64 return self._remote._remote_exec(RemoteClass.CALL, self.path_to_str(),
Jakub Grajciar7db35de2019-06-25 10:22:11 +020065 *args, **kwargs)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020066
67
68class RemoteClass(Process):
69 """
70 This class can wrap around and adapt the interface of another class,
71 and then delegate its execution to a newly forked child process.
72 Usage:
73 # Create a remotely executed instance of MyClass
74 object = RemoteClass(MyClass, arg1='foo', arg2='bar')
75 object.start_remote()
76 # Access the object normally as if it was an instance of your class.
77 object.my_attribute = 20
78 print object.my_attribute
79 print object.my_method(object.my_attribute)
80 object.my_attribute.nested_attribute = 'test'
81 # If you need the value of a remote attribute, use .get_remote_value
82 method. This method is automatically called when needed in the context
83 of a remotely executed class. E.g.:
84 if (object.my_attribute.get_remote_value() > 20):
85 object.my_attribute2 = object.my_attribute
86 # Destroy the instance
87 object.quit_remote()
88 object.terminate()
89 """
90
91 GET = 0 # Get attribute remotely
92 CALL = 1 # Call method remotely
93 SETATTR = 2 # Set attribute remotely
94 REPR = 3 # Get representation of a remote object
95 STR = 4 # Get string representation of a remote object
96 QUIT = 5 # Quit remote execution
97
98 PIPE_PARENT = 0 # Parent end of the pipe
99 PIPE_CHILD = 1 # Child end of the pipe
100
101 DEFAULT_TIMEOUT = 2 # default timeout for an operation to execute
102
103 def __init__(self, cls, *args, **kwargs):
104 super(RemoteClass, self).__init__()
105 self._cls = cls
106 self._args = args
107 self._kwargs = kwargs
108 self._timeout = RemoteClass.DEFAULT_TIMEOUT
109 self._pipe = Pipe() # pipe for input/output arguments
110
111 def __repr__(self):
Paul Vinciguerra090096b2020-12-03 00:42:46 -0500112 return reprlib.repr(RemoteClassAttr(self, None))
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200113
114 def __str__(self):
115 return str(RemoteClassAttr(self, None))
116
117 def __call__(self, *args, **kwargs):
118 return self.RemoteClassAttr(self, None)()
119
120 def __getattr__(self, attr):
121 if attr[0] == '_' or not self.is_alive():
Paul Vinciguerrad3a9be22019-03-10 07:03:22 -0700122 if not (attr.startswith('__') and attr.endswith('__')):
123 if hasattr(super(RemoteClass, self), '__getattr__'):
124 return super(RemoteClass, self).__getattr__(attr)
Neale Ranns097fa662018-05-01 05:17:55 -0700125 raise AttributeError('missing: %s', attr)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200126 return RemoteClassAttr(self, attr)
127
128 def __setattr__(self, attr, val):
129 if attr[0] == '_' or not self.is_alive():
Paul Vinciguerrad3a9be22019-03-10 07:03:22 -0700130 if not (attr.startswith('__') and attr.endswith('__')):
131 super(RemoteClass, self).__setattr__(attr, val)
132 return
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200133 setattr(RemoteClassAttr(self, None), attr, val)
134
Jakub Grajciar7db35de2019-06-25 10:22:11 +0200135 def _remote_exec(self, op, path=None, *args, **kwargs):
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200136 """
137 Execute given operation on a given, possibly nested, member remotely.
138 """
139 # automatically resolve remote objects in the arguments
140 mutable_args = list(args)
141 for i, val in enumerate(mutable_args):
142 if isinstance(val, RemoteClass) or \
Neale Ranns097fa662018-05-01 05:17:55 -0700143 isinstance(val, RemoteClassAttr):
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200144 mutable_args[i] = val.get_remote_value()
145 args = tuple(mutable_args)
Paul Vinciguerra090096b2020-12-03 00:42:46 -0500146 for key, val in kwargs.items():
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200147 if isinstance(val, RemoteClass) or \
Neale Ranns097fa662018-05-01 05:17:55 -0700148 isinstance(val, RemoteClassAttr):
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200149 kwargs[key] = val.get_remote_value()
150 # send request
151 args = self._make_serializable(args)
152 kwargs = self._make_serializable(kwargs)
153 self._pipe[RemoteClass.PIPE_PARENT].send((op, path, args, kwargs))
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200154 timeout = self._timeout
155 # adjust timeout specifically for the .sleep method
Jakub Grajciar7db35de2019-06-25 10:22:11 +0200156 if path is not None and path.split('.')[-1] == 'sleep':
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200157 if args and isinstance(args[0], (long, int)):
158 timeout += args[0]
159 elif 'timeout' in kwargs:
160 timeout += kwargs['timeout']
161 if not self._pipe[RemoteClass.PIPE_PARENT].poll(timeout):
162 return None
163 try:
164 rv = self._pipe[RemoteClass.PIPE_PARENT].recv()
165 rv = self._deserialize(rv)
166 return rv
167 except EOFError:
168 return None
169
170 def _get_local_object(self, path):
171 """
172 Follow the path to obtain a reference on the addressed nested attribute
173 """
174 obj = self._instance
175 for attr in path:
176 obj = getattr(obj, attr)
177 return obj
178
179 def _get_local_value(self, path):
180 try:
181 return self._get_local_object(path)
182 except AttributeError:
183 return None
184
185 def _call_local_method(self, path, *args, **kwargs):
186 try:
187 method = self._get_local_object(path)
188 return method(*args, **kwargs)
189 except AttributeError:
190 return None
191
192 def _set_local_attr(self, path, value):
193 try:
194 obj = self._get_local_object(path[:-1])
195 setattr(obj, path[-1], value)
196 except AttributeError:
197 pass
198 return None
199
200 def _get_local_repr(self, path):
201 try:
202 obj = self._get_local_object(path)
Paul Vinciguerra090096b2020-12-03 00:42:46 -0500203 return reprlib.repr(obj)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200204 except AttributeError:
205 return None
206
207 def _get_local_str(self, path):
208 try:
209 obj = self._get_local_object(path)
210 return str(obj)
211 except AttributeError:
212 return None
213
214 def _serializable(self, obj):
215 """ Test if the given object is serializable """
216 try:
217 dumps(obj)
218 return True
219 except:
220 return False
221
222 def _make_obj_serializable(self, obj):
223 """
224 Make a serializable copy of an object.
225 Members which are difficult/impossible to serialize are stripped.
226 """
227 if self._serializable(obj):
228 return obj # already serializable
Jakub Grajciar0b1f8a72019-02-21 12:01:31 +0100229
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200230 copy = SerializableClassCopy()
Jakub Grajciar0b1f8a72019-02-21 12:01:31 +0100231
232 """
233 Dictionaries can hold complex values, so we split keys and values into
234 separate lists and serialize them individually.
235 """
236 if (type(obj) is dict):
237 copy.type = type(obj)
238 copy.k_list = list()
239 copy.v_list = list()
240 for k, v in obj.items():
241 copy.k_list.append(self._make_serializable(k))
242 copy.v_list.append(self._make_serializable(v))
243 return copy
244
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200245 # copy at least serializable attributes and properties
246 for name, member in inspect.getmembers(obj):
Neale Ranns097fa662018-05-01 05:17:55 -0700247 # skip private members and non-writable dunder methods.
248 if name[0] == '_':
249 if name in ['__weakref__']:
250 continue
Jakub Grajciar546f9552019-08-21 10:51:21 +0200251 if name in ['__dict__']:
252 continue
Paul Vinciguerrad3a9be22019-03-10 07:03:22 -0700253 if not (name.startswith('__') and name.endswith('__')):
254 continue
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200255 if callable(member) and not isinstance(member, property):
256 continue
257 if not self._serializable(member):
Jakub Grajciar546f9552019-08-21 10:51:21 +0200258 member = self._make_serializable(member)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200259 setattr(copy, name, member)
260 return copy
261
262 def _make_serializable(self, obj):
263 """
264 Make a serializable copy of an object or a list/tuple of objects.
265 Members which are difficult/impossible to serialize are stripped.
266 """
267 if (type(obj) is list) or (type(obj) is tuple):
268 rv = []
269 for item in obj:
270 rv.append(self._make_serializable(item))
271 if type(obj) is tuple:
272 rv = tuple(rv)
273 return rv
Jakub Grajciar053204a2019-03-18 13:17:53 +0100274 elif (isinstance(obj, IntEnum) or isinstance(obj, IntFlag)):
Jakub Grajciar0b1f8a72019-02-21 12:01:31 +0100275 return obj.value
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200276 else:
277 return self._make_obj_serializable(obj)
278
279 def _deserialize_obj(self, obj):
Jakub Grajciar0b1f8a72019-02-21 12:01:31 +0100280 if (hasattr(obj, 'type')):
281 if obj.type is dict:
282 _obj = dict()
283 for k, v in zip(obj.k_list, obj.v_list):
284 _obj[self._deserialize(k)] = self._deserialize(v)
285 return _obj
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200286 return obj
287
288 def _deserialize(self, obj):
289 if (type(obj) is list) or (type(obj) is tuple):
290 rv = []
291 for item in obj:
292 rv.append(self._deserialize(item))
293 if type(obj) is tuple:
294 rv = tuple(rv)
295 return rv
296 else:
297 return self._deserialize_obj(obj)
298
299 def start_remote(self):
300 """ Start remote execution """
301 self.start()
302
303 def quit_remote(self):
304 """ Quit remote execution """
Jakub Grajciar7db35de2019-06-25 10:22:11 +0200305 self._remote_exec(RemoteClass.QUIT, None)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200306
307 def get_remote_value(self):
308 """ Get value of a remotely held object """
309 return RemoteClassAttr(self, None).get_remote_value()
310
311 def set_request_timeout(self, timeout):
312 """ Change request timeout """
313 self._timeout = timeout
314
315 def run(self):
316 """
317 Create instance of the wrapped class and execute operations
318 on it as requested by the parent process.
319 """
320 self._instance = self._cls(*self._args, **self._kwargs)
321 while True:
322 try:
323 rv = None
324 # get request from the parent process
325 (op, path, args,
326 kwargs) = self._pipe[RemoteClass.PIPE_CHILD].recv()
327 args = self._deserialize(args)
328 kwargs = self._deserialize(kwargs)
329 path = path.split('.') if path else []
330 if op == RemoteClass.GET:
331 rv = self._get_local_value(path)
332 elif op == RemoteClass.CALL:
333 rv = self._call_local_method(path, *args, **kwargs)
334 elif op == RemoteClass.SETATTR and 'value' in kwargs:
335 self._set_local_attr(path, kwargs['value'])
336 elif op == RemoteClass.REPR:
337 rv = self._get_local_repr(path)
338 elif op == RemoteClass.STR:
339 rv = self._get_local_str(path)
340 elif op == RemoteClass.QUIT:
341 break
342 else:
343 continue
344 # send return value
345 if not self._serializable(rv):
346 rv = self._make_serializable(rv)
347 self._pipe[RemoteClass.PIPE_CHILD].send(rv)
348 except EOFError:
349 break
350 self._instance = None # destroy the instance
351
352
353@unittest.skip("Remote Vpp Test Case Class")
354class RemoteVppTestCase(VppTestCase):
355 """ Re-use VppTestCase to create remote VPP segment
356
357 In your test case:
358
359 @classmethod
360 def setUpClass(cls):
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -0700361 # fork new process before client connects to VPP
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200362 cls.remote_test = RemoteClass(RemoteVppTestCase)
363
364 # start remote process
365 cls.remote_test.start_remote()
366
367 # set up your test case
368 super(MyTestCase, cls).setUpClass()
369
370 # set up remote test
371 cls.remote_test.setUpClass(cls.tempdir)
372
373 @classmethod
374 def tearDownClass(cls):
375 # tear down remote test
376 cls.remote_test.tearDownClass()
377
378 # stop remote process
379 cls.remote_test.quit_remote()
380
381 # tear down your test case
382 super(MyTestCase, cls).tearDownClass()
383 """
384
385 def __init__(self):
386 super(RemoteVppTestCase, self).__init__("emptyTest")
387
Paul Vinciguerraf70cead2019-03-10 07:32:59 -0700388 # Note: __del__ is a 'Finalizer" not a 'Destructor'.
389 # https://docs.python.org/3/reference/datamodel.html#object.__del__
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200390 def __del__(self):
391 if hasattr(self, "vpp"):
Paul Vinciguerraf70cead2019-03-10 07:32:59 -0700392 self.vpp.poll()
393 if self.vpp.returncode is None:
394 self.vpp.terminate()
395 self.vpp.communicate()
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200396
397 @classmethod
398 def setUpClass(cls, tempdir):
399 # disable features unsupported in remote VPP
400 orig_env = dict(os.environ)
401 if 'STEP' in os.environ:
402 del os.environ['STEP']
403 if 'DEBUG' in os.environ:
404 del os.environ['DEBUG']
405 cls.tempdir_prefix = os.path.basename(tempdir) + "/"
406 super(RemoteVppTestCase, cls).setUpClass()
407 os.environ = orig_env
408
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -0700409 @classmethod
410 def tearDownClass(cls):
411 super(RemoteVppTestCase, cls).tearDownClass()
412
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200413 @unittest.skip("Empty test")
414 def emptyTest(self):
415 """ Do nothing """
416 pass
417
418 def setTestFunctionInfo(self, name, doc):
419 """
420 Store the name and documentation string of currently executed test
421 in the main VPP for logging purposes.
422 """
423 self._testMethodName = name
424 self._testMethodDoc = doc