fib: fib api updates
[vpp.git] / test / remote_test.py
index 43cb9b9..092d3f8 100644 (file)
@@ -10,7 +10,7 @@ import six
 from six import moves
 
 from framework import VppTestCase
-from enum import Enum
+from aenum import Enum
 
 
 class SerializableClassCopy(object):
@@ -19,6 +19,9 @@ class SerializableClassCopy(object):
     """
     pass
 
+    def __repr__(self):
+        return '<SerializableClassCopy dict=%s>' % self.__dict__
+
 
 class RemoteClassAttr(object):
     """
@@ -43,21 +46,25 @@ class RemoteClassAttr(object):
 
     def __getattr__(self, attr):
         if attr[0] == '_':
-            raise AttributeError
+            if not (attr.startswith('__') and attr.endswith('__')):
+                raise AttributeError('tried to get private attribute: %s ',
+                                     attr)
         self._path.append(attr)
         return self
 
     def __setattr__(self, attr, val):
         if attr[0] == '_':
-            super(RemoteClassAttr, self).__setattr__(attr, val)
-            return
+            if not (attr.startswith('__') and attr.endswith('__')):
+                super(RemoteClassAttr, self).__setattr__(attr, val)
+                return
         self._path.append(attr)
         self._remote._remote_exec(RemoteClass.SETATTR, self.path_to_str(),
                                   True, value=val)
 
     def __call__(self, *args, **kwargs):
+        ret = True if 'vapi' in self.path_to_str() else False
         return self._remote._remote_exec(RemoteClass.CALL, self.path_to_str(),
-                                         True, *args, **kwargs)
+                                         ret, *args, **kwargs)
 
 
 class RemoteClass(Process):
@@ -114,15 +121,17 @@ class RemoteClass(Process):
 
     def __getattr__(self, attr):
         if attr[0] == '_' or not self.is_alive():
-            if hasattr(super(RemoteClass, self), '__getattr__'):
-                return super(RemoteClass, self).__getattr__(attr)
-            raise AttributeError
+            if not (attr.startswith('__') and attr.endswith('__')):
+                if hasattr(super(RemoteClass, self), '__getattr__'):
+                    return super(RemoteClass, self).__getattr__(attr)
+                raise AttributeError('missing: %s', attr)
         return RemoteClassAttr(self, attr)
 
     def __setattr__(self, attr, val):
         if attr[0] == '_' or not self.is_alive():
-            super(RemoteClass, self).__setattr__(attr, val)
-            return
+            if not (attr.startswith('__') and attr.endswith('__')):
+                super(RemoteClass, self).__setattr__(attr, val)
+                return
         setattr(RemoteClassAttr(self, None), attr, val)
 
     def _remote_exec(self, op, path=None, ret=True, *args, **kwargs):
@@ -133,12 +142,12 @@ class RemoteClass(Process):
         mutable_args = list(args)
         for i, val in enumerate(mutable_args):
             if isinstance(val, RemoteClass) or \
-               isinstance(val, RemoteClassAttr):
+                    isinstance(val, RemoteClassAttr):
                 mutable_args[i] = val.get_remote_value()
         args = tuple(mutable_args)
         for key, val in six.iteritems(kwargs):
             if isinstance(val, RemoteClass) or \
-               isinstance(val, RemoteClassAttr):
+                    isinstance(val, RemoteClassAttr):
                 kwargs[key] = val.get_remote_value()
         # send request
         args = self._make_serializable(args)
@@ -240,8 +249,12 @@ class RemoteClass(Process):
 
         # copy at least serializable attributes and properties
         for name, member in inspect.getmembers(obj):
-            if name[0] == '_':  # skip private members
-                continue
+            # skip private members and non-writable dunder methods.
+            if name[0] == '_':
+                if name in ['__weakref__']:
+                    continue
+                if not (name.startswith('__') and name.endswith('__')):
+                    continue
             if callable(member) and not isinstance(member, property):
                 continue
             if not self._serializable(member):
@@ -348,7 +361,7 @@ class RemoteVppTestCase(VppTestCase):
 
         @classmethod
         def setUpClass(cls):
-            # fork new process before clinet connects to VPP
+            # fork new process before client connects to VPP
             cls.remote_test = RemoteClass(RemoteVppTestCase)
 
             # start remote process
@@ -375,12 +388,14 @@ class RemoteVppTestCase(VppTestCase):
     def __init__(self):
         super(RemoteVppTestCase, self).__init__("emptyTest")
 
+    # Note: __del__ is a 'Finalizer" not a 'Destructor'.
+    # https://docs.python.org/3/reference/datamodel.html#object.__del__
     def __del__(self):
         if hasattr(self, "vpp"):
-            cls.vpp.poll()
-            if cls.vpp.returncode is None:
-                cls.vpp.terminate()
-                cls.vpp.communicate()
+            self.vpp.poll()
+            if self.vpp.returncode is None:
+                self.vpp.terminate()
+                self.vpp.communicate()
 
     @classmethod
     def setUpClass(cls, tempdir):
@@ -394,6 +409,10 @@ class RemoteVppTestCase(VppTestCase):
         super(RemoteVppTestCase, cls).setUpClass()
         os.environ = orig_env
 
+    @classmethod
+    def tearDownClass(cls):
+        super(RemoteVppTestCase, cls).tearDownClass()
+
     @unittest.skip("Empty test")
     def emptyTest(self):
         """ Do nothing """